Drawing Blanks

Premature Optimization is a Prerequisite for Success

Archive for July 2012

Non-numbers

leave a comment »

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

/// Returns a value indicating whether the specified 
/// number evaluates to a value that is not a number 
public 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…

Written by bbzippo

07/19/2012 at 4:10 am

Posted in programming

Closest neighbors

leave a comment »

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:

 

Read the rest of this entry »

Written by bbzippo

07/02/2012 at 3:27 am

Posted in math