Question re Java number precision

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
 
S

Stefan Ram

inspired said:
Why do I get all the extra digits?

You used »double« instead of »java.lang.BigDecimal«.
Double values are binary floating point values and therefore
cannot represent the values of your decimal numerals precisely.
How can I avoid this?

Either use »java.lang.BigDecimal« or a format specifier
like »%.02f« in the standard method »printf«.

(In a Java newsgroup, you do not need to mention »Java« in
the subject of your posts.)
 
I

inspired

Thank you.

  You used »double« instead of »java.lang.BigDecimal«.
  Double values are binary floating point values and therefore
  cannot represent the values of your decimal numerals precisely.


  Either use »java.lang.BigDecimal« or a format specifier
  like »%.02f« in the standard method »printf«.

  (In a Java newsgroup, you do not need to mention »Java« in
  the subject of your posts.)
 
A

Arne Vajhøj

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?

That is how floating point work.

Does the difference between 359.96000000000004 and 359.96
really matters for your computations?

If the answer is yes, then you should not use FP.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top