double * double appears to be a bug

double num=36775.715;

double num1 = num*100.0;

System.out.println("PRODUCT = "+num1);

Above simple code produces output as 3677571.4999999995

while

double num=36775.815;

double num1 = num*100.0;

System.out.println("PRODUCT = "+num1);

produces

3677581.5 correctly

Can anyone explain why this happens and what is the solution?

[400 byte] By [mandar_gharse] at [2007-9-26 2:46:41]
# 1

Floating point numbers are not exact. The solution is to not expect the results of floating point operations to be exact. Really, it is!

If you want to output a pretty number, use the java.text.NumberFormat class. If you want to determine if two numbers are equal, check to see if they are "close enough".

schapel at 2007-6-29 10:30:21 > top of Java-index,Archived Forums,Java Programming...