奇怪的 Java 错误 - float 设置为 Infinity
我是 Java 新手,我正在使用处理来进行一些数据可视化。不过,我在代码中遇到了这个奇怪的错误,想知道是否有人可以帮助我。似乎 Xspacing 浮点数一直设置为无穷大,但是当我打印出表达式时,它被设置为正确的值被打印...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
结果是:
49.0
无限
任何帮助将不胜感激!
抱歉,我写得很快,省略了一些非常必要的信息:
49.0 就是应该的。除了整数值之外,所有其他类型都是浮点数。 代码确实可以编译,并且 println 被构建到处理中,这是我正在使用的框架(正确的术语?)。它基本上是一个在处理 GUI 中打印到控制台的函数。
Xspacing 的目的是作为我的类“Graph”的数据,但是当我在公共函数“drawBasic”中定义变量时,一切正常。现在我只是好奇......
使用 System.out.println(0 产生相同的结果。初始值或变量是:
float startX = 120.00001
浮动结束X = 740.0
整数值 = 12
width 是一个整数(虽然不明确),设置为 800
奇怪的是,在函数定义中这似乎工作正常,只有当我尝试在类中定义它时它才不起作用......
I'm new to Java, and I'm using Processing to make some data visualizations. I'm getting this strange error in my code though, was wondering if anyone could help me out. It seems the Xspacing float keeps getting set to Infinity, however when I print out the expression it gets set to the proper value gets printed...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Result is:
49.0
Infinity
Any help would be appreciated!
Sorry, I wrote this out very quickly and omitted some pretty necessary info:
49.0 IS what is should be. All other types are floats, besides values which is an integer.
The code DOES compile, and println is build into Processing, which is the framework (correct term?) that I'm using. It is basically a function that prints to the console in the Processing GUI.
Xspacing was intended to be data for my class "Graph," however when I define the variable within a public function "drawBasic" everything works fine. Now I am just curious....
Using System.out.println(0 yields the same results. Initial values or variables are:
float startX = 120.00001
float endX = 740.0
int values = 12
width is an integer (although not explicit) that is set to 800
The odd thing seems to be that within a function definition this works fine, its only when I try to define it within the class that it doesn't work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码不能是这样,因为数字 *.04 创建一个双精度数,这意味着您需要将表达式转换为浮点型。
对于要编译的代码,它必须类似于
“现在,结果”。例如,如果您的代码有:
Java 会给您一个 java.lang.ArithmeticException: / by 0
但是,如果您有
那么 Java 会给您“无穷大”。为什么? http://grouper.ieee.org/groups/754/
Your code couldn't be like that because a number *.04 creates a double, and that would mean you'd need to cast the expression into a float.
For your code to compile it would have to be something like
Now, on the result. If your code had, for example:
Java would give you a java.lang.ArithmeticException: / by zero
However, if you have
Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/
试试这个:
Try this:
即使假设变量是浮点数,该行也无法编译,因为 0.4 双文字。
此外,“println”不是一个独立的方法,因此您必须编写自己的方法。
您的实际代码是什么?
Even assuming the variables are floats that line does not compile, because of the 0.4 double literal.
Also 'println' is not a standalone method, so you must have written your own.
What is your actual code?
你忘记了
)
并且你应该put System.out.println(xspacing);
fyi 你也可以只输入
syso
和ctrl spacebar
它会为你打印出打印语句。you forget a
)
and you should'veput System.out.println(xspacing);
fyi you can also just type
syso
andctrl spacebar
and it will print out the print statement for you.