Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Double is doing something weird!

 
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my code sample:
double sum = 0;
for ( int x = 0 ; x < 1000 ; x++ )
{
sum += 0.1;
System.out.println("sum: " + sum);
}
Here's some of the output:
sum: 0.1
sum: 0.2
sum: 0.30000000000000004
sum: 0.4
sum: 0.5
sum: 0.6
sum: 0.7
sum: 0.7999999999999999
sum: 0.8999999999999999
sum: 0.9999999999999999
sum: 1.0999999999999999
sum: 1.2
Can somebody please 'splain dis to me?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some floating point numbers (float/double) cannot be represented with infinite precision. This is very common in all languages. That's why one has to be sure when to use floats or doubles instead of int.
Try to execute the following code:
<PRE>
double d = 1/3;
if(d*3 == 1.0)
System.out.println("Equal");
else
System.out.println("Not Equal");
</PRE>
 
Ray Marsh
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok but what can be done about it? I've been programming for 6 years and have never seen this happen. Can a rounding function be used to keep it where it belongs?
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rounding would do it, I guess. Another solution is to use integers if possible.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use the BigDecimal class...
Rounding or using the DecimalFormat class may be able to get you what you want, but is't really recommended in many situations.
The fact of the matter is that floating point numbers are really meant for scientific calculations and not for monetary (or similar) calculations... and this exact same behavior occurs on ANY development platform that uses floating point numbers, including C/C++.
BigDecimal is Sun's answer to this problem... although rolling your own class using longs or integers isn't terribly difficult. BigDecimal is also being enhanced/replaced by IBM's more fully functional class as well, although I don't know all the details... check the JCP.
 
Ray Marsh
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JCP? Please elaborate.
I will investigate the BigDecimal idea.
Thank you
 
Rich Wardwell
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry... JCP = Java Community Process - the web site itself will do more justice than any explanation I might give:
http://www.javasoft.com/aboutJava/communityprocess/
 
Trailboss
Posts: 23463
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from one of the cattle drive assignments. The purpose of this particular assignment is to learn the boundaries of double. In a previous assignment the student learned the boundaries of int and long.
The important thing is that as you add all these imprecisions up, they turn into bigger inprecisions. Finally, if you have a value that is approximately 100.0, you cannot test for equality. Instead, you will need to test if the value you have is in the range of, say, 99.9999 and 100.0001.
Several classes and methods within the java core library will help you with the rounding you may need to do.
 
reply
    Bookmark Topic Watch Topic
  • New Topic