Differences

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

Link to this comparison view

Both sides previous revision Previous revision
floating-point-equality [2018/09/05 14:53]
awf [But my case is more subtle than that]
floating-point-equality [2018/09/05 15:14] (current)
awf
Line 49: Line 49:
 But hang on, you're on a platform which will give you line number here when you get the divby0 right? ​   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. So the assert is superfluous -- just enable exceptions on divide by zero, underflow, and overflow.
 +
 +And there'​s also a very simple case. If I stored 0.3f somewhere, I expect to get it back.  It's irrelevant that 0.3f doesn'​t have an exact binary representation -- whatever it translates to, I should get exactly that back
 +<code C++>
 +void my_unit_test() {
 +  my_matrix<​2,​2,​float>​ a = {3.0f, 1.1f, 2.2f, 4.0f};
 +  TEST_ASSERT_EQ_APPROX(a(0,​0),​ 3.0f, 1e-8f); // No, no, no.  There is no reason for the stored value to be different.
 +  TEST_ASSERT_EQ(a(0,​0),​ 3.0f); // I gave you whatever 3.0f is.  I want it back.
 +}
 +</​code>​
 +You may worry about excess precision on x86, but there'​s no computation going on here, so we can and should ask for exact equality.
  
 ==== But my case is more subtle than that ==== ==== But my case is more subtle than that ====
Line 111: Line 121:
 } }
 </​code>​ </​code>​
 +
 +See also
 +* https://​stackoverflow.com/​questions/​51134021/​floating-point-equality
  
 ~~DISCUSSION~~ ~~DISCUSSION~~