I have a function that computes a point in 3d spaced based on a value in range [0, 1]
. The problem I\'m facing is, that a binary floating-point number canno
binary floating-point number cannot represent exactly 1
Proof that it can can be found here.
Most accurate representation = 1.0E0
There could be problems with
But 1.0
is none of them!
However 0.1
is a problem case, violating point number 1, look at this:
Most accurate representation = 1.00000001490116119384765625E-1
So if you add up 0.1 ten times, you will get 1.00000001490116119384765625E-0
which is greater than 1.0
.
(examples are in IEEE754 single precision 32 bit floating point numbers)
int i;
for (i=0; i <= 10; i++) {
t=i/10.0;
curves_error error = curves_bezier(points, point, t);
if (error != curves_no_error) {
printf("Error with t = %f.\n", t);
}
else {
printf("t = %f is ok.\n", t);
}
}
This way, the error of the binary format does not get summed up!
(Note: I used extra curly braces for the if
and else
statements. Do that, you'll thank yourself one day.)