0

If I have a java float and I want to increment/decrement it by the minumim possible value how can I determine that value? I mean it is a difference if the value of the float is already a very large number like 3.4028235E38 or a very small number like 1.4E-45. In the first case to note any difference I need to subtract a far lager number as I would need to add to the second case right?

KIC
  • 5,887
  • 7
  • 58
  • 98
  • 1
    Real numbers which are exactly representable by double-precision floats are not uniformly distributed on the real line. What is the minimum possible difference between any two floats and what are the differences between a specific float and the two closest representable real numbers are two different questions. Perhaps the following classic paper will help: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – John Coleman Oct 14 '17 at 09:22
  • @JohnColeman excatly so the question really is, what is the closest next bigger/smaller number I can get? Will check this paper thx. – KIC Oct 14 '17 at 09:33
  • I'm voting to close this as a duplicate (since it is), but you might enjoy the following, somewhat less technical source: http://www.volkerschatz.com/science/float.html – John Coleman Oct 14 '17 at 09:36
  • You could using something like fvalue = float.intBitsToFloat(float.floattointbits(fvalue)+1). – rcgldr Oct 14 '17 at 09:42
  • 4
    @rcgldr that's a long and ugly way to write `Java.lang.Math.nextAfter`, and it goes in the opposite direction for negative numbers. – Pascal Cuoq Oct 14 '17 at 09:43
  • @PascalCuoq - it was just an example to show the function names. The actual code should store the reinterpreted float into a integer, then check the sign bit, increment or decrement (or other adjustment) accordingly, check for exponent underflow / overflow, then convert back. nextafter would be simpler though. – rcgldr Oct 14 '17 at 09:47
  • Math.nextAfter never noticed that one but yes thats it! – KIC Oct 14 '17 at 10:34
  • Two of the votes to close suggested it was a duplicate of a question that answered the question for C. This question asks about Java and is not actually answered by C code, so I provided another question that covers Java. – Eric Postpischil Oct 14 '17 at 14:08

0 Answers0