• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Weird Floating Point Conversion

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I execute the following line of code:


I get: 0.9499999999999999555910790149937383830547332763671875

Anyone else seeing this? I'm using 1.4.2
 
Sheriff
Posts: 27463
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The people who wrote the API documentation for the constructor BigDecimal(double) noticed that and wrote about it. #3 in the "Notes" addresses your question.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new BigDecimal("0.95") !
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same in Java5



Only off by a 100 Trillionth. Would that our government could account for their money that well.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
The people who wrote the API documentation for the constructor BigDecimal(double) noticed that and wrote about it. #3 in the "Notes" addresses your question.



I'd prefer they wrote a solution in the code rather than a note in the API. Or, gotten rid of the constructor all together. It seems odd to me the note should say "the results of this constructor can be somewhat unpredictable". That's a good enough reason to *never* use it.

Although they do pass the blame off to the people who wrote double, it does seem odd to me that System.out.println(0.95) does not display as "0.9499..." normally. I'd say BigDecimal's constructor should use a solution similar to what double is doing, perhaps explicitly converting to string first.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that primitive doubles have limited accuracy is the whole reason why people use BigDecimal. It doesn't seem like a surprise that any value derived from a double would be similarly unreliable. Getting rid of the constructor seems like overkill to me - it makes sense to have a way to convert a double to BigDecimal, if only to see what value the double "really" is (since toString() does some rounding). The fact that it can be used incorrectly doesn't mean that no one else should be allowed to use it, IMO.
[ March 20, 2008: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the problem with the Real number representation in computers: it works till it doesn't
Basically, you're approximating a infinite line by using points, the number gets rounded to the closest point. That's why you have .95=0.9499999999999999555910790149937383830547332763671875
(any number between those 2 will give the same result)

Want to have more fun? Try:
double x1=1000;
double x2=2e19;
System.out.println("x1="+x1);
System.out.println("x2="+x2);
System.out.println("x1+x2-x1="+(x1+x2-x2));
(it's called catastrophic cancellation)
 
catch it before it slithers away! Oh wait, it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic