Boost logo

Boost :

From: Kevin Atkinson (kevinatk_at_[hidden])
Date: 2000-04-24 22:24:51

One well known problem with floating point numbers is that the normal
comparisons won't work when the equality case is important. Therefore I
propose a special adapter class be create called safe_double which will
act almost exactly like double (which includes implicit conversion both
ways) but will have "safe" comparison that will take into account
rounding error. For example the ==, <, and <= would be defined as

bool operator == (safe_double rhs, safe_double lhs)
  return abs(rhs-lhs) < SMALL_VAL

bool operator < (safe_double rhs, safe_double lhs)
  return rhs - lhs < -SMALL_VAL

bool operator <= (safe_double rhs, safe_double lhs)
  return rhs - lhs < SMALL_VAL

By implicit conversion both ways I mean the following methods will be

  operator double & ()
  operator const double & () const

This is 100% safe because the safe_double does absolute NOTHING different
from a double unless they are compared with each other or another
double. Anything less will make safe_double too inconvenient to use.

The only real issue, in my view, is what SMALL_VAL should be.

What do you think? Do you think it would be a useful additional to the
boost library. Naturally safe_float should also be created to compliment

Kevin Atkinson

Boost list run by bdawes at, gregod at, cpdaniel at, john at