wood burning stoves*
The moose likes Beginning Java and the fly likes bad math in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bad math in Java?" Watch "bad math in Java?" New topic
Author

bad math in Java?

Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
I'm writing a program that captures the Point where a user clicks on a Canvas and determines the angle between this point and another point. After seeing some very strange results, I kept reducing the math down to its simplest statement and still ended up with bizarre results. The following program illustrates the issue.

I'm no genius at math, but even I know that there's something wrong with the results above. Running the Math.atan() function against .2 and against .8 should not yield a result of 0! Nor should it yield the exact same result (to at least 16 significant digits) when run against 51/50 and against 70/50. Is something wrong with the atan method in the Math class?
Tom McQueary
Greenhorn

Joined: Nov 24, 2000
Posts: 20
I suspect your problem in the test code you submitted is caused by the way integer division is done in Java. 10/50 and 40/50 both evaluate to 0. 51/50 and 70/50 both evaluate to 1. In both cases the fractional part of the quotient is discarded resulting in an integer. Try using a floating point values for the numerator and denominator of your parameter, ie 10.0/50.0, and see if this makes a difference.
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Tom, I think you're right. In my program, the arguments are actually int variables. For some reason, I assumed that because
1) Math.atan takes a double argument;
2) The return value is a double; and
3) The result of the division will almost always be a fractional value,
the result of all the operations would return a fractional (float or double) value. But instead it appears that the division of integers causes the resulting argument to be rounded to an int before the Math.atan() method is executed. By setting both of the arguments to double primitives (even though they will always be Point integers), the fractional values of the division operation appear to be preserved.
Thanks for pointing out the hole in my logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bad math in Java?
 
Similar Threads
-0.0 is what makes java very different :)
Not passing the value
Math functions
Math min,max
Some Codes