Joe White’s Blog

Life, .NET, and Cats


Infinity and negative infinity are… equal?

As I write DUnitAssertions, I’m baking in knowledge of how to compare floats intelligently. This means always providing an epsilon. If you choose to specify an epsilon of zero (or don’t provide an epsilon in the first place), I call Math.SameValue, which provides its own reasonable default epsilon.

While writing some tests for my code, I found an interesting bug in SameValue. It thinks that Infinity and NegInfinity are equal!

Just a bit off, that.

Now curious, I did some debugging.

SameValue starts out by seeing if you have zero as an epsilon (which you will, if you don’t specify the default parameter). If so, it calculates its own reasonable epsilon. Here’s the code it uses:

Epsilon := Max(Min(Abs(A), Abs(B)) * ExtendedResolution, ExtendedResolution);

So it takes ExtendedResolution (a very small number) and multiplies it by +INF. The result? +INF, of course. So anything within infinity is considered “close enough”.

Hmm. I think I may have found the problem.

And if Delphi’s standard library functions work this poorly with infinities, I may just not spend too much time worrying about how my framework handles them…

There are no responses to “Infinity and negative infinity are… equal?” yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Joe White's Blog copyright © 2004-2008. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS).