|
|
|
|
|
by exDM69
3369 days ago
|
|
Here's my less-than-scientific floating point near-equality test I use. bool zero(float x) ( return x*x < FLT_EPSILON; }
bool equal_float(float a, float b) {
return (zero(a) && zero(b)) || // both are zero
zero((a-b)*(a-b) / (a*a + b*b)); // or relative error squared is zero
}
This checks equality to about four decimal digits for 32 bit single precision and seven digits for 64 bit floats. Inf/NaN special values are not considered.Critique and comments welcome. |
|
A better comparison would check for zero somehow like this: