Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
floating-point-equality [2017/04/19 11:15] awffloating-point-equality [2017/11/21 12:27] awf
Line 13: Line 13:
 </code> </code>
  
-But what are you trying to avoid here?   Is it the divide by zero exception?  Then avoid that:+But what are you trying to avoid here?   Is it the divide by zero?  Then avoid that...
 <code C++> <code C++>
 double func(double a, double b) { double func(double a, double b) {
Line 23: Line 23:
 } }
 </code> </code>
-"But", you say, "what if diff is really small"?  You mean like 1e-128?  Then this function will return 1e+128.  Floating point can deal with it.   If you didn't need that dynamic range, you'ld be using ''float'' instead of ''double''.  And it would still be fine  If you're worried about floating underflow, then compare to a constant you named MAX_OF_FLT_MIN_AND_RECIPROCAL_OF_FLT_MAXrather than some random EPSILON that is not related to either of these quantities.+"But", you say, "what if diff is really small"?  You mean like 1e-128?  Then this function will return 1e+128.   
 +Floating point can deal with it.   If you didn't need that dynamic range, you'ld be using ''float'' instead of ''double''.   
 + 
 +OK, what about 1e-309?  Aha, different issue (denormal floats)  
 +Now you won't get a divide by zeromost likely you'll return Inf, and... your code might run very slowly. 
 +Aha! No-one wants slow code, right?  OK, but then why the "if" in the first place?   
 +That's slowing down every call to the function.   
 +If your workloads mostly don't call the function with nearly equal very small numbers,  
 +then you can go a lot faster in the common case by not checking. 
 + 
 +"Ahbut I want the additional error checking" OK, fine.  I'm with you.  But is this the right place?   
 +Why did the caller call the routine with nearly equal very small numbers?   
 +Is that physically likely in whatever real-world problem you're trying to solve?   
 +If not, one should probably protect at the call site with an assert.
  
 Or maybe it's a precondition of the function call that a not be equal to b, and it's a coding bug if not satisfied. Or maybe it's a precondition of the function call that a not be equal to b, and it's a coding bug if not satisfied.
 <code C++> <code C++>
 double func(double a, double b) { double func(double a, double b) {
-   assert(a != b);+   assert(a - b != 0);
        
    return 1/(a-b);    return 1/(a-b);
 } }
 </code> </code>
-Don't forget to [[assert-always|leave your asserts on in all builds]] unless the profiler tells you otherwise.  But hang on, you're on a platform which will give you line number here when you get the divby0 right?   So the assert is superfluous.+Don't forget to [[assert-always|leave your asserts on in all builds]] unless the profiler tells you otherwise.   
 +But hang on, you're on a platform which will give you line number here when you get the divby0 right?    
 +So the assert is superfluous -- just enable exceptions on divide by zero, underflow, and overflow.
  
 ==== But my case is more subtle than that ==== ==== But my case is more subtle than that ====
Line 46: Line 61:
 </code> </code>
  
-Even here, you can think about what a better value to compare to might be.   First note that sin(x)/x works fine for all normalized floats except zero, so you can avoid a dependency on fabs and a definition of epsilon by just comparing to zero.+Even here, you can think about what a better value to compare to might be.   First note that sin(x)/x works fine for all floats except zero, so you can avoid a dependency on fabs and a definition of epsilon by just comparing to zero.
  
 But perhaps the profiler has told you that a big chunk of program time is spent in computing sin() and dividing, and furthermore you know that most times it's called with values near zero.  Well then, let's do what we do with any special function that goes slow: chop up the domain and special case.   It's true that sin(x)/x is exactly floating point 1.0 when x is small.  In fact, compute the taylor series, see that But perhaps the profiler has told you that a big chunk of program time is spent in computing sin() and dividing, and furthermore you know that most times it's called with values near zero.  Well then, let's do what we do with any special function that goes slow: chop up the domain and special case.   It's true that sin(x)/x is exactly floating point 1.0 when x is small.  In fact, compute the taylor series, see that
floating-point-equality.txt · Last modified: 2021/03/05 09:13 by awf
CC Attribution 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0