Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should not get floating point comparison effects without optimization. This is because you have the same function in both cases.</p> <p>Without optimization, the following is true:</p> <pre><code>float func(float); float a = ...; func(a) == func(a); //&lt; always true </code></pre> <p>This is what you have, where your function is the shift and divide by 255.</p> <p>However - with optimization it's a different story. GCC has optimizations (see e.g. -freciprocal-math, -fassociative-math) that can rearrange your expressions.</p> <p>In your case, you have:</p> <pre><code>float rr = (X) / 255.0f; ... r == rr </code></pre> <p>For example, it could do something that amounts to this upon optimization:</p> <pre><code>255.0f * r == (X) </code></pre> <p>This now <em>does</em> suffer from floating point comparison effects. However, by introducing the stdout in the middle, you are forcing it to evaluate the expressions closer to the way they are written, which again gets you back to the sanity of evaluating truth on the same function against itself.</p> <p>You could either change the definition of the class to store the values as integers and convert to float only when you need floats, or store both the hex representation and the floats and using the hex for comparison. Or you could do comparisons testing whether the two floating point values are within 1/255 of each other using greater than/less than instead of double-equals. E.g. something like this:</p> <pre><code>return (abs(r - rr) &lt; 1/255.0f &amp;&amp; ...); </code></pre>
    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. 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.
    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