Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you're using <code>double</code> you are invoking what is called "Floating Point Arithmetics". In practice, this is an imprecise way to represent numbers, simply because not every real number can be precisely represented in a finite number of bits. </p> <p>As a result, some numbers, for instance, can never be represented precisely, and instead of "25" your computer (not the compiler), has a result of, say, 25.00000000000071 - close enough for all practical purposes, but clearly not equal to 25. </p> <p>There are few other quirks related to IEEE-754 floating point - adding 0.1 ten times to 0.0 doesn't necessarily reach 1.0, etc.</p> <p>Which is why, in practice, you should never compare for equality, but you should compare whether the absolute value of the difference between two numbers is smaller than an (small, but large enough) epsilon.</p> <pre><code>const double espilon = 0.00000001 if (fabs(x - y) &lt; epsilon) { // Assume approximately equal } </code></pre> <p>A good (MATLAB based, but the concepts apply throughout everything that uses IEEE-754, nowadays, that means about everywhere) introduction to what is going on is here:</p> <p><a href="https://www2.bc.edu/~quillen/sp11/mt414/pres/floatarith.pdf" rel="nofollow">https://www2.bc.edu/~quillen/sp11/mt414/pres/floatarith.pdf</a></p> <p>Also, <code>pow()</code> is not the most reasonable way to do squares - <code>pow(x, m)</code> internally computes <code>exp(m log(x))</code> in order to handle arbitrary real-number bases and exponents. This particular conversion is exactly why it looses precision. </p> <p>This also means <code>pow()</code> is ridiculously slow when compared with multiplication-based approach. </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.
    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