• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

float point inexact result

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reading JLC and have a doubt:
double d = 1/3.;
System.out.println((d*3 == 1)); //true
double d = 1/49.;
System.out.println((d*49 == 1)); //false
not quite understand, can anybody explain? thanks.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Haining,
Welcome to the round-off world. When working with non-integral numbers multiplying and dividing cause round-off errors. The surprising one between your two examples is the first true not the second false. No matter how many significant digits we can support it won't be enough to avoid round-off error ...
You would do well in your programming experience to use < or > instead of any comparison using ==. This is the reason why most computer programs (well written ones!) that compare two non-integral numbers allow for some 'slop' or 'delta' or 'epsilon' value.
Regards,
Manfred.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS explains it here:
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208
The problem is that you are attempting to store an infinitely long number (1/49) within a fixed length field. This is obviously impossible. Rounding/truncation has to occur on the fraction. So when you multiply (1/49) * 49 you do not get 1.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is lots of good information on the IEEE 754 standard that is used for the storage of floating point numbers. Do a search.
Here are a couple of papers to get you started:
http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html

http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html
 
Haining Mu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
The JLS explains it here:
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208
The problem is that you are attempting to store an infinitely long number (1/49) within a fixed length field. This is obviously impossible. Rounding/truncation has to occur on the fraction. So when you multiply (1/49) * 49 you do not get 1.



thanks. i was expecting this happen for (1/49) * 49 not equal 1. but why (1/3.) * 3. equal 1. Just remembering somewhere in the book mention these kind of comparison is NEVER true. Hope we shuold have no quesiton in EXAM like this.
[This message has been edited by Haining Mu (edited June 15, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with rounding and precision. Change them to floats and they both return true:

[This message has been edited by Thomas Paul (edited June 15, 2001).]
 
Don't MAKE me come back there with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic