Tuesday, April 20, 2010

Floating point precision

I was recently asked a question about the precision of floating point numbers. As most people know the representation used by most computers to store real numbers does not have continuous precision across the number line.

I thought it would be interesting therefore to plot the density of numbers across the number line. This is a nice illustration of the fact that numbers near 0 have higher precision that large numbers. The following plots are for 32bit floats (this was on a 64bit Snow Leopard Mac):




The following code was used to generate the data, which was plotted with Gnuplot:

#include <iostream>
#include <limits>

using namespace std;

int main() {


//float smallest = 0.00000000000000000000001;
float smallest = 0.00000001;

float block_size = 10;
for(float range_start=-100000;range_start<100000;range_start+=block_size) {
float range_end = range_start+block_size;

size_t different_n=0;
float n;
for(float n=range_start;n<range_end;) {

float new_n=n;
for(float i=1;new_n <= n;i++) {
new_n = n + (smallest*i);
}
n = new_n;
different_n++;
}

cout << range_start+((range_end-range_start)/2) << " " << different_n << endl;
}
}

No comments: