Note that there are some explanatory texts on larger screens.

plurals
  1. POFind min/max of a float/double that has the same internal representation
    primarykey
    data
    text
    <p>Refreshing on <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" rel="nofollow noreferrer">floating points</a> (also <a href="http://www.validlab.com/goldberg/paper.pdf" rel="nofollow noreferrer">PDF</a>), IEEE-754 and taking part <a href="https://stackoverflow.com/questions/1421520/formatting-doubles-for-output-in-c">in this discussion on floating point rounding when converting to strings</a>, brought me to tinker: how can I get the maximum and minimum value for a given floating point number whose binary representations are equal.</p> <p><strong>Disclaimer</strong>: for this discussion, I like to stick to 32 bit and 64 bit floating point as described by IEEE-754. I'm not interested in extended floating point (80-bits) or quads (128 bits IEEE-754-2008) or any other standard (IEEE-854).</p> <p><strong>Background</strong>: Computers are bad at representing <code>0.1</code> in binary representation. In C#, a float represents this as <code>3DCCCCCD</code> internally (C# uses round-to-nearest) and a double as <code>3FB999999999999A</code>. The same bit patterns are used for decimal <code>0.100000005</code> (float) and <code>0.1000000000000000124</code> (double), but not for <code>0.1000000000000000144</code> (double). </p> <p>For convenience, the following C# code gives these internal representations:</p> <pre><code>string GetHex(float f) { return BitConverter.ToUInt32(BitConverter.GetBytes(f), 0).ToString("X"); } string GetHex(double d) { return BitConverter.ToUInt64(BitConverter.GetBytes(d), 0).ToString("X"); } // float Console.WriteLine(GetHex(0.1F)); // double Console.WriteLine(GetHex(0.1)); </code></pre> <p>In the case of <code>0.1</code>, there is no lower decimal number that is represented with the same bit pattern, any <code>0.99...99</code> will yield a different bit representation (i.e., float for <code>0.999999937</code> yields <code>3F7FFFFF</code> internally).</p> <p><strong>My question</strong> is simple: how can I find the lowest and highest decimal value for a given float (or double) that is internally stored in the same binary representation.</p> <p><strong>Why</strong>: (I know you'll ask) to find the error in rounding in .NET when it converts to a string and when it converts from a string, to find the internal exact value and to understand my own rounding errors better.</p> <p>My guess is something like: take the mantissa, remove the rest, get its exact value, get one (mantissa-bit) higher, and calculate the mean: anything below that will yield the same bit pattern. My main problem is: how to get the fractional part as integer (bit manipulation it not my strongest asset). <a href="http://www.yoda.arachsys.com/csharp/DoubleConverter.cs" rel="nofollow noreferrer">Jon Skeet's DoubleConverter</a> class may be helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload