## Archive for **July 2012**

## Non-numbers

Here is how **double.IsNaN()** implemented, it’s funny:

`/// Returns a value indicating whether the specified `

///numberevaluates to a value that is not a numberpublic static bool IsNaN(double d) { return d != d; }

The double type can get you in trouble because it can store things like NaN and Infinity. It will silently divide by zero, take square roots and logarithms of negatives, and keep calculating, and you will have a hard time finding out where exactly your math is failing. It gets particularly funny when your formulas are right, but because of loss of precision your value under Math.Acos() becomes slightly greater than 1…

Sometimes you’d get a number when you would expect a NaN, e.g.

Math.Pow(0, double.PositiveInfinity) //is zero and not NaN Math.Pow(1, double.PositiveInfinity) //is one and not NaN

And there is no such thing as double.IsNumber(), so if you want to check whether your calculation succeeded you need to check .IsNaN() && .IsInfinity()

So it’s a lot of fun…

## Closest neighbors

A simple but curious statement:

**In any finite set of points on the plane, there are points that have less than 4 closest neighbors.**

Not sure if this is a deep or useful observation.

Not sure if I have a rigorous proof. I’ll publish a sketch of a proof later.

And here is how you prove it: