I
inspired
When I run the following code
final double MAX_ANGLE = 360.00;
// Do for every angle from max to zero
for (double angle = MAX_ANGLE; angle >= 0; angle = angle - 0.01)
{
System.out.print("Input Angle = " + angle + " : ");
}
I get inconsistent output like this:
Input Angle = 360.0 :
Input Angle = 359.99 :
Input Angle = 359.98 :
Input Angle = 359.97 :
Input Angle = 359.96000000000004 : <-- why all the extra digits?
Input Angle = 359.95000000000005 : <-- why all the extra digits?
Input Angle = 359.94000000000005 : <-- why all the extra digits?
Input Angle = 359.93000000000006 : <-- why all the extra digits?
Input Angle = 359.9200000000001 : <-- why all the extra digits?
Input Angle = 359.9100000000001 : <-- why all the extra digits?
Input Angle = 359.9000000000001 : <-- why all the extra digits?
Input Angle = 359.8900000000001 : <-- why all the extra digits?
while I expected:
Input Angle = 360.0 :
Input Angle = 359.99 :
Input Angle = 359.98 :
Input Angle = 359.97 :
Input Angle = 359.96 :
Input Angle = 359.95 :
Input Angle = 359.94 :
Input Angle = 359.93 :
Input Angle = 359.92 :
Input Angle = 359.91 :
Input Angle = 359.90 :
Input Angle = 359.89 :
Why do I get all the extra digits? How can I avoid this?
Thanks, Alan
final double MAX_ANGLE = 360.00;
// Do for every angle from max to zero
for (double angle = MAX_ANGLE; angle >= 0; angle = angle - 0.01)
{
System.out.print("Input Angle = " + angle + " : ");
}
I get inconsistent output like this:
Input Angle = 360.0 :
Input Angle = 359.99 :
Input Angle = 359.98 :
Input Angle = 359.97 :
Input Angle = 359.96000000000004 : <-- why all the extra digits?
Input Angle = 359.95000000000005 : <-- why all the extra digits?
Input Angle = 359.94000000000005 : <-- why all the extra digits?
Input Angle = 359.93000000000006 : <-- why all the extra digits?
Input Angle = 359.9200000000001 : <-- why all the extra digits?
Input Angle = 359.9100000000001 : <-- why all the extra digits?
Input Angle = 359.9000000000001 : <-- why all the extra digits?
Input Angle = 359.8900000000001 : <-- why all the extra digits?
while I expected:
Input Angle = 360.0 :
Input Angle = 359.99 :
Input Angle = 359.98 :
Input Angle = 359.97 :
Input Angle = 359.96 :
Input Angle = 359.95 :
Input Angle = 359.94 :
Input Angle = 359.93 :
Input Angle = 359.92 :
Input Angle = 359.91 :
Input Angle = 359.90 :
Input Angle = 359.89 :
Why do I get all the extra digits? How can I avoid this?
Thanks, Alan