• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

double confuse

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the sample:

public static void main(String[] args) throws Exception{
double value = 0.66d;
System.out.println(value);
value += 1d;
System.out.println(value);
}


why there appears 00000001?! If change 1d to 2d or if change 0.66d to 0.2d, then that tail doesn't appear. What's going on?!
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that when I compare 1.66 with this value, I get false!
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might help
http://www.velocityreviews.com/forums/t139008-java-double-precision.html
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might if the decimal fractions was the matter. But here I get different values because of adding an integer value, e.g. one.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gasan Gouseinov wrote:It might if the decimal fractions was the matter. But here I get different values because of adding an integer value, e.g. one.



Unfortunately, this is an issue with all floating points -- you don't get infinite precision. And it doesn't matter whether you add an integer to it, or not. You can get this even if you add one -- as it depends on both operands of the addition.

A possible solution -- instead of doing a direct comparison, take a difference, and check if the result is within a certain tolerance from zero.



Henry
 
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Floating-point arithmetic is intended for engineers. When I was an undergraduate, there was this joke:

What is the difference between engineers and mathematicians?

If you ask a mathematician what 2 × 2 means, he says 4.
If you ask an engineer the same question, he gets his slide rule out (this was several years ago ) and says, "Err, 2, multiply, err, umm, 2, . . . . 3.99"

"Well, that's 4 near as makes no difference."

Engineers can cope with the fact that you can write 0.1 and really get 0.09999999999998735872873956243562195. When you try exact comparisons (by the way, you were not adding an integer, but a double 1.0), your 0.66 might have turned to 1.6600000000000000003635346857325 or 1.65999999999999999973456324875435. But it probably wasn't exactly 0.66 to start with.

 
My, my, aren't you a big fella. Here, have a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic