Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the best way to achieve this is to rely on the fact that according to the IEEE 754 floating point standard, the integer representation of floating point bits are lexicographically ordered as a 2-complement integer.</p> <p>I.e. you could simply add one ulp (units in the last place) to get the next floating point representation (which will always be slightly larger than your treshold if it was smaller, since the round error is at most 1/2 ulp)</p> <p>e.g.</p> <pre><code> float floatValue = 7.f/10; std::cout &lt;&lt; std::setprecision(20) &lt;&lt; floatValue &lt;&lt; std::endl; int asInt = *(int*)&amp;floatValue; asInt += 1; floatValue = *(float*)&amp;asInt; std::cout &lt;&lt; floatValue &lt;&lt; std::endl; </code></pre> <p>prints (on my system)</p> <pre><code> 0.69999998807907104492 0.70000004768371582031 </code></pre> <p>To know when you need to add one ulp, you'll have to rely on the difference of <code>floor</code> and a rounded <code>floor</code></p> <pre><code> if (std::floor(floatValue * 100.) != std::floor(floatValue * 100. + 0.5)) { int asInt = *(int*)&amp;floatValue; asInt += 1; floatValue = *(float*)&amp;asInt; } </code></pre> <p>Would correctly convert 0.69.. to 0.70.. but leave 0.80.. alone.</p> <p>Note that the float gets promoted to a double via the multiplication with <code>100.</code> before the <code>floor</code> is applied. </p> <p>If you don't do this you risk getting in the situation that for</p> <pre><code> 7.f/10.f * 100.f </code></pre> <p>The (limited in precision) float representation would be 70.00...</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