This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

fraction 1/3

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the sample test of Bill Brogden's Exam Cram, I compiled and ran the following :
float A = 1.0F / 3.0F;
if ( (A * 3.0F) == 1.0F )
System.out.println("Equal");
else System.out.println("Not Equal");

and I got "Equal" printed.
But the answer is "Not Equal". The reason given is a floating-point number cannot exactly represent the fraction 1/3 -- multiplying by 3 does not recover the original number. I think the explanation makes sense. Could anybody help me understand why I got "Equal"?
Thanks.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange. I get the same result. I think the key is, you get roundoff error both ways - when you divide and when you multiply. It just so happens that the two cancel each other out in this case. Surprisingly, it seems to cancel out in most of the cases I tried. Try using 41.0F in place of 3.0F, and you may get "Not Equal" instead. At least, that's what I got on my machine.
The thing to remember is that whenever using floats or doubles, the == operator is unreliable either way. It may give false positives or false negatives. To get meaningful results from comparisons with floats, use > or < instead. E.g.:

This returns true if x and y are within .00001 % of each other.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brogden does make a slight mod to this in the errata for the book. He changes the second line from
if ( (A * 3.0F) == 1.0F )
to
if ( (A * 3.0) == 1.0F )
Hope that helps ...
Greg
 
Brenda Bowers
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim & Greg.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic