# bad math in Java?

Scott Appleton

Ranch Hand

Posts: 195

posted 14 years ago

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?

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

Posts: 20

posted 14 years ago

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

Posts: 195

posted 14 years ago

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.

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.