Re: How to overload == for System.Double

Marc Brooks <[email protected]> Fri, 2 Feb 2007 11:44:25 -0600
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
The CTS (common type system) specification declares that all
System.Single is a IEEE 32-bit format and System.Double is an IEEE
64-bit format, and that all float math is done using IEEE rules.   The
code I presented is built accordingly.

The idea that some architectures natively use different
representations is a non-issue.  Additionally, any modern CPU uses the
IEEE format, so the issue of native representation is largely a
historical "what-if" at this point and not a real issue.

> Given that, is this implementation totally reliable, and has it been
> seriously tested?

It's been is use in several real life applications since 2003 or so...
I've had it published for a long time in several places (CodePlex,
CodeProject) and nobody has ever reported an issue.  But that could be
because nobody uses it.

> Besides that, is it really much better than Math.Abs(a-b) < 1e-6
> (or whatever tolerance you want to apply)?

Depends... if all you care about is that two values VERY NEAR ZERO (in
the scope of all float representations) are very close together, then
yes the classic comparison against a tiny Epsilon is fine... but if
you have very large numbers that are not very near each other, but are
relatively close within the resolution of the IEEE format being used
(e.g. the Mantissa is really close), then the comparion using Epsilon
will not be correct, because they are different by a huge aboslute
value (dependant on the Exponent), but very close (based on the
resolution i.e. the lowest bits are nearly identical).

--
"I am Dyslexic of Borg. Resistors are fertile. Prepare to have your
ass laminated." -- Dan Nitschke

Marc C. Brooks
http://musingmarc.blogspot.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com