How to correctly and standardly compare floats?
Posted
by
DIMEDROLL
on Stack Overflow
See other posts from Stack Overflow
or by DIMEDROLL
Published on 2010-12-28T17:38:13Z
Indexed on
2010/12/28
19:54 UTC
Read the original article
Hit count: 227
Every time I start a new project and when I need to compare some float or double variables I write the code like this one:
if (fabs(prev.min[i] - cur->min[i]) < 0.000001 &&
fabs(prev.max[i] - cur->max[i]) < 0.000001) {
continue;
}
Then I want to get rid of these magic variables 0.000001(and 0.00000000001 for double) and fabs, so I write an inline function and some defines:
#define FLOAT_TOL 0.000001
So I wonder if there is any standard way of doing this? May be some standard header file? It would be also nice to have float and double limits(min and max values)
© Stack Overflow or respective owner