# Question re Java number precision

Discussion in 'Java' started by inspired, Nov 26, 2010.

1. ### inspiredGuest

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

inspired, Nov 26, 2010

2. ### Stefan RamGuest

inspired <> writes:
>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

Stefan Ram, Nov 26, 2010

3. ### inspiredGuest

Thank you.

On Nov 26, 6:52 pm, -berlin.de (Stefan Ram) wrote:
> inspired <> writes:
> >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.)

inspired, Nov 27, 2010
4. ### Arne VajhøjGuest

On 26-11-2010 18:47, inspired wrote:
> 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

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

Arne

Arne Vajhøj, Nov 27, 2010
5. ### Roedy GreenGuest

On Fri, 26 Nov 2010 15:47:54 -0800 (PST), inspired
<> wrote, quoted or indirectly quoted someone
who said :

><-- why all the extra digits

see http://mindprod.com/jgloss/floatingpoint.html

The short answer is 0.1 is a repeater in binary.
--