Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>when a variable is assigned a number entered with one decimal place, such as 4.2, the float or double used in the comparison actually may have a value such as 4.1999998092651367</p> </blockquote> <p>Not <em>may</em>. <strong>will</strong>. To be specific:</p> <pre><code>float f = 4.2; // f is exactly 4.19999980926513671875 double d = 4.2; // d is exactly 4.20000000000000017763568394002504646778106689453125 </code></pre> <p>The problem comes when you write something like:</p> <pre><code>float f = 4.2; if (f &gt;= 4.2) { // this block of code is not executed. } </code></pre> <p><code>f</code> is exactly <code>4.19999980926513671875</code>, but you're comparing it to the <strong>double-precision literal</strong> "4.2", which has the value <code>4.20000000000000017763568394002504646778106689453125</code>, so the comparison fails. If instead you compare against the single precision literal "4.2f":</p> <pre><code>float f = 4.2; if (f &gt;= 4.2f) { // this block of code is exectued. } </code></pre> <p>the comparison succeeds, because the values are exactly equal. Floating-point is complicated, but it is entirely deterministic; one of the simplest things you can do to make it more intuitive is to not mix precisions. If you're working with <code>float</code>, make sure all of your literals are suffixed with <code>f</code> to make them single precision, too.</p> <p>(This can also improve performance, but that's not the reason to do it; the reason to do it is because it will make your code more correct).</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