Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 691
MrDatabase Avatar asked Oct 30 '08 07:10

MrDatabase


People also ask

What is an example of a rounding error?

For example, the irrational number pi equals approximately 3.14, rounded to two decimal places or three significant digits. As an example of rounding error, consider the speed of light in a vacuum. The official value is 299,792,458 meters per second.

What is an example of a floating-point?

A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not. Floating point numbers get their name from the way the decimal point can "float" to any position necessary.

Do floating-point numbers have rounding errors?

Even if some numbers can be represented exactly by floating-point numbers and such numbers are called machine numbers, performing floating-point arithmetic may lead to roundoff error in the final result.

What can be the floating-point errors?

Floating-point error mitigation is the minimization of errors caused by the fact that real numbers cannot, in general, be accurately represented in a fixed space. By definition, floating-point error cannot be eliminated, and, at best, can only be managed.


1 Answers

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!

like image 148
Agnius Vasiliauskas Avatar answered Sep 17 '22 15:09

Agnius Vasiliauskas