What is a simple example of floating point/rounding error?

MrDatabase picture MrDatabase · Oct 30, 2008 · Viewed 40.7k times · Source

I've heard of "error" when using floating point variables. Now I'm trying to solve this puzzle and I think I'm getting some rounding/floating point error. So I'm finally going to figure out the basics of floating point error.

What is a simple example of floating point/rounding error (preferably in C++) ?

Edit: For example say I have an event that has probability p of succeeding. I do this event 10 times (p does not change and all trials are independent). What is the probability of exactly 2 successful trials? I have this coded as:

double p_2x_success = pow(1-p, (double)8) * pow(p, (double)2) * (double)choose(8, 2);

Is this an opportunity for floating point error?

Answer

Agnius Vasiliauskas picture Agnius Vasiliauskas · Apr 17, 2011

Picture is worth a thousand words - try to draw equation f(k) :
enter image  description here
and you will get such XY graph (X and Y are in logarithmic scale).
enter image description here
If computer could represent 32-bit floats without rounding error then for every k we should get zero. But instead error increases with bigger values of k because of floating point error accumulation.

hth!