On computers, floating point numbers are never accurate. They are always just close. (1e-16 is close.)
Sometimes there are hidden bits that you do not see. Sometimes the basic rules of algebra are no longer applied: a * b! = B * a. Sometimes comparing case with memory, these subtle differences are revealed. Or using a math coprocessor against a floating point time library. (I do this waayyy tooo long.)
C99 defines: (Look at math.h)
double round(double x); float roundf(float x); long double roundl(long double x);
.
Or you can roll it yourself:
template<class TYPE> inline int ROUND(const TYPE & x) { return int( (x > 0) ? (x + 0.5) : (x - 0.5) ); }
For floating point equivalence try:
template<class TYPE> inline TYPE ABS(const TYPE & t) { return t>=0 ? t : - t; } template<class TYPE> inline bool FLOAT_EQUIVALENT( const TYPE & x, const TYPE & y, const TYPE & epsilon ) { return ABS(xy) < epsilon; }
Mr.Ree Feb 26 '09 at 16:36 2009-02-26 16:36
source share