开发者

Strange Java error- float getting set to Infinity

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...


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

float Xspacing = (float)((endX-(width*.04) - startX)/ values);  
println((endX-(width*.04) - startX)/ values);  
println(Xspacing);  

Now, on the result. If your code had, for example:

    System.out.println(3/0);

Java would give you a java.lang.ArithmeticException: / by zero

However, if you have

    System.out.println(3f/0);

Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/


Try this:

float Xspacing = (endX-(width*.04) - startX)/ values;  
println((float)((endX-(width*.04) - startX)/ values));  
println(Xspacing);  


float Xspacing = (endX-(width*.04) - startX)/ values; 

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?


you forget a ) and you should've put System.out.println(xspacing);

fyi you can also just type syso and ctrl spacebar and it will print out the print statement for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜