Velocity Reviews > Java > Question re Java number precision

Question re Java number precision

inspired
Guest
Posts: n/a

 11-26-2010
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

Stefan Ram
Guest
Posts: n/a

 11-26-2010
inspired <(E-Mail Removed)> 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

inspired
Guest
Posts: n/a

 11-27-2010
Thank you.

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

Arne Vajhøj
Guest
Posts: n/a

 11-27-2010
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

Roedy Green
Guest
Posts: n/a

 11-28-2010
On Fri, 26 Nov 2010 15:47:54 -0800 (PST), inspired
<(E-Mail Removed)> 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.
--
http://mindprod.com

If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.