I wanted to know what would be the fastest approach of comparing floats to three decimal places.Say I have something like this
float lhs = 2.567xxxx
float rhs = 2.566xxxx
The above should be different and if its something like this
float lhs = 2.566xxxx
float rhs = 2.566xxxx
They should be the same
Update:
I am trying the following
double trunc(double d)
{
return (d>0) ? floor(d) : ceil(d) ;
}
bool comparedigits(float a , float b)
{
if (trunc(1000.0 * a) == trunc(1000.0 * b))
{
return true;
}
return false;
}
float g = 2.346;
float h= 2.34599;
bool t = comparedigits(g,h) ; //Not the same and should return false;
However it is returning true.
See Question&Answers more detail:os