Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A single-precision <code>float</code> has 23 bits of precision. That means that every calculation is rounded to 23 binary digits. This means that if you have a computation that, say, adds a very small number to a very large number, rounding may result in strange results.</p> <p>Imagine that you are doing math in scientific notation decimal by hand, under the rule that you may only have four significant figures. Let's say I ask you to write twelve in scientific notation, with four significant figures. Remembering junior high school, you write:</p> <p>1.200 &times; 10<sup>1</sup></p> <p>Now I say compute the square of 12, and then add 0.5. That is easy enough:</p> <p>1.440&times;10<sup>2</sup> + 0.005&times;10<sup>2</sup> = 1.445&times;10<sup>2</sup></p> <p>How about twelve cubed plus 0.75:</p> <p>1.728&times;10<sup>3</sup> + 0.00075&times;10<sup>3</sup> = 1.72875&times;10<sup>3</sup></p> <p>But remember, I only gave you room for <b>four significant digits</b>, so you must <b>round</b>; then we get:</p> <p>1.728&times;10<sup>3</sup> + 7.5&times;10<sup>-1</sup> = 1.729&times;10<sup>3</sup></p> <p>See? The lack of precision can make the computation come out with unexpected results.</p> <p>In your example, you've got 999999 in a calculation where you're trying to be precise to 0.01. log<sub>2</sub>(999999) = 19.93 and log<sub>2</sub>(0.01) = -6.64. The difference is more than 23; therefore you would need more than 23 binary digits to perform this calculation accurately.</p> <p>Because floating point mathematics rounds-off precision by its very nature, it is usually a bad choice for currency computation, where you must be accurate to the last cent. But are you really concerned with fractions of a cent in your application? If not, then why not do away with the decimal point altogether, and simply store cents (instead of dollars) in a 64-bit integer? 2<sup>64</sup>&cent; is more than the GDP of the entire planet.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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