4 ms·
> Unfortunately C++ STL does not offer anything close to isclose maybe because you can trivially write it yourself with whatever precision you want: float
by PaulDavisThe1st 5y ago
> Unfortunately C++ STL does not offer anything close to isclose
maybe because you can trivially write it yourself with whatever precision you want:
float a = 0.1;
float b = 0.2;
// now test for equality to within half of the smallest
// value float can represent
bool equal = fabs (0.3 - (a + b)) < (FLT_EPSILON/2);
Alternatively:
bool isclose (float v1, float v2, float prec = FLT_EPSILON/2) {
return fabs (v1 - v2) < prec;
}
Variations are left to the reader. There are a few optimizations one can do but this version conveys the basic point: you get to specify the semantics of isclose()