• 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

The workings of floats

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJ2 question

This prints true or false?
I would say false as:
4.0/3.0 is a reoccuring number 1.333333333.........
the varialbe f of float type holds a specific number of decimal places,
and so when f is then multiplyed by 3.0F the value will be 3.9999999999999.......... reoccuring and not 4.0F
But when compiled prints true???
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A computer can not store a recurring number so f actually contains 1.3333334. Multiply that by 3 and you get 4.0000002 which rounds to 4.0 because 4.0000002 is beyond the precision of a float.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well said thomas
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether the exact details given by either answer are the way it actually happens. In fact, I'd bet it depends on the environment you run this in. Two different machines may give two different answers.
The moral of the story is that you should NEVER compare floating point numbers with the == operator. As the above discussions illustrate, round off error makes such a comparison unreliable.
 
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

Originally posted by Layne Lund:
I don't know whether the exact details given by either answer are the way it actually happens. In fact, I'd bet it depends on the environment you run this in. Two different machines may give two different answers.


They should't be different. Java religiously follows the IEE 754 floating point standard for the behaviour of floats and doubles.
This is a pretty good write-up on the standard even if it is by Microsoft:
http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It turns out that yes, 4.0F/3.0F should be exactly the same in any Java implementation - but there are other cases where similar expressions are not so bound. It depends on whether a given expression is considered FP-strict or not. (Any compile-time constant expression is FP-strict). From the JLS:

Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.


I'm not sure what would be a good example of an expression that can evaluate differently in a non-FP-strict context. But it seems there's at least a possibility of different results...
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
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