Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you print the EXACT value of a floating point number?
    primarykey
    data
    text
    <p>First of all, this is not a floating point newbie question. I know results of floating point arithmetic (not to mention transcendental functions) usually cannot be represented exactly, and that most terminating decimals cannot be represented exactly as binary floating point numbers.</p> <p>That said, each possible floating point value corresponds exactly to a diadic rational (a rational number <code>p/q</code> where <code>q</code> is a power of 2), which in turn has an exact decimal representation.</p> <p>My question is: How do you find this exact decimal representation efficiently? <code>sprintf</code> and similar functions are usually only specified up to a number of significant digits to uniquely determine the original floating point value; they don't necessarily print the exact decimal representation. I know one algorithm I've used, but it's very slow, <code>O(e^2)</code> where <code>e</code> is the exponent. Here's an outline:</p> <ol> <li>Convert the mantissa to a decimal integer. You can either do this by pulling apart the bits to read the mantissa directly, or you can write a messy floating point loop that first multiplies the value by a power of two to put it in the range 1&lt;=x&lt;10, then pulls off a digit at a time by casting to int, subtracting, and multiplying by 10.</li> <li>Apply the exponent by repeatedly multiplying or dividing by 2. This is an operation on the <strong>string</strong> of decimal digits you generated. Every ~3 multiplications will add an extra digit to the left. Every single dividion will add an extra digit to the right.</li> </ol> <p>Is this really the best possible? I doubt it, but I'm not a floating-point expert and I can't find a way to do the base-10 computations on the floating point representation of the number without running into a possibility of inexact results (multiplying or dividing by anything but a power of 2 is a lossy operation on floating point numbers unless you know you have free bits to work with).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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