type comparisons
Steven Schveighoffer
schveiguy at yahoo.com
Mon Jan 28 12:00:27 PST 2008
"Denton Cockburn" wrote
> The problem I'm having is that we are comparing the same numerical value
> for equality, and getting a (logically) false result.
This is inherent in floating point. This happens in other languages as
well.
The problem is that .1 is like 1/3 in decimal. Decimal cannot accurately
represent 1/3 because it is a repeating decimal (0.3333...). Likewise,
floating point, which is base-2, cannot represent all decimal values
accurately.
For an in-depth explanation, see http://en.wikipedia.org/wiki/Floating_point
Skip to the section on Accuracy.
To work correctly, you should always compare floating point values by adding
some small error. So instead of:
assert(x > y)
you write
const myError = 1e-10;
assert(x + myError > y)
Equality looks something more like:
assert(fabs(x - y) < myError)
-Steve
More information about the Digitalmars-d-learn
mailing list