Its not just your programming group that can’t get it right. I work in a semi-disfunctional group on contract for a client who, not matter how hard we try, doesn’t seem to listen to basic software engineering principles.
I feel a little better (and a great deal worse after thinking about it) when I see that the largest software company in the world deals with some of the same problems.
I found this gem in the WPFToolkit (it is MSPL) source.
// The standard CLR double.IsNaN() function is approximately 100 times slower than our own wrapper,
// so please make sure to use DoubleUtil.IsNaN() in performance sensitive code.
// PS item that tracks the CLR improvement is DevDiv Schedule : 26916.
// IEEE 754 : If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL
// or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result will be NaN.
public static bool IsNaN(double value)
{
NanUnion t = new NanUnion();
t.DoubleValue = value;ulong exp = t.UintValue & 0xfff0000000000000;
ulong man = t.UintValue & 0x000fffffffffffff;
return (exp == 0x7ff0000000000000 || exp == 0xfff0000000000000) && (man != 0);
}
My jaw was open pretty far for quite a few seconds as I read this.
And eventually someone will run into this when they’re busily working with incredibly large numbers, and wonder why in the hell they’re coming back NaN. Creative.