Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best approach I think is to compare <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place" rel="noreferrer">ULPs</a>.</p> <pre><code>bool is_nan(float f) { return (*reinterpret_cast&lt;unsigned __int32*&gt;(&amp;f) &amp; 0x7f800000) == 0x7f800000 &amp;&amp; (*reinterpret_cast&lt;unsigned __int32*&gt;(&amp;f) &amp; 0x007fffff) != 0; } bool is_finite(float f) { return (*reinterpret_cast&lt;unsigned __int32*&gt;(&amp;f) &amp; 0x7f800000) != 0x7f800000; } // if this symbol is defined, NaNs are never equal to anything (as is normal in IEEE floating point) // if this symbol is not defined, NaNs are hugely different from regular numbers, but might be equal to each other #define UNEQUAL_NANS 1 // if this symbol is defined, infinites are never equal to finite numbers (as they're unimaginably greater) // if this symbol is not defined, infinities are 1 ULP away from +/- FLT_MAX #define INFINITE_INFINITIES 1 // test whether two IEEE floats are within a specified number of representable values of each other // This depends on the fact that IEEE floats are properly ordered when treated as signed magnitude integers bool equal_float(float lhs, float rhs, unsigned __int32 max_ulp_difference) { #ifdef UNEQUAL_NANS if(is_nan(lhs) || is_nan(rhs)) { return false; } #endif #ifdef INFINITE_INFINITIES if((is_finite(lhs) &amp;&amp; !is_finite(rhs)) || (!is_finite(lhs) &amp;&amp; is_finite(rhs))) { return false; } #endif signed __int32 left(*reinterpret_cast&lt;signed __int32*&gt;(&amp;lhs)); // transform signed magnitude ints into 2s complement signed ints if(left &lt; 0) { left = 0x80000000 - left; } signed __int32 right(*reinterpret_cast&lt;signed __int32*&gt;(&amp;rhs)); // transform signed magnitude ints into 2s complement signed ints if(right &lt; 0) { right = 0x80000000 - right; } if(static_cast&lt;unsigned __int32&gt;(std::abs(left - right)) &lt;= max_ulp_difference) { return true; } return false; } </code></pre> <p>A similar technique can be used for doubles. The trick is to convert the floats so that they're ordered (as if integers) and then just see how different they are.</p> <p>I have no idea why this damn thing is screwing up my underscores. Edit: Oh, perhaps that is just an artefact of the preview. That's OK then.</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