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 cannot represent exactly 1.
The mathematical expression that is evaluated in the function is able to compute a value for t=1.0, but the value will never be accepted by the function because it checks if for the range before computing.
curves_error curves_bezier(curves_PointList* list, curves_Point* dest, curves_float t) {
/* ... */
if (t < 0 || t > 1)
return curves_invalid_args;
/* ... */
return curves_no_error;
}
How can I, with this function, compute the 3d point at t=1.0? I heard something about an ELLIPSIS some time ago that I think had to do with such an issue, but I’m not sure.
Thanks
EDIT: Ok, I’m sorry. I assumed a float cannot represent exactly 1, because of the issue I’m facing. The problem may be because I was doing an iteration like this:
for (t=0; t <= 1.0; t += 0.1) {
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);
}
your problem is that a binary floating point number cannot exactly represent
0.1.The closest 32-bit single precision IEEE754 floating point number is 0.100000001490116119384765625 and the closest 64-bit double precision one 0.1000000000000000055511151231257827021181583404541015625. If the arithmetic is performed strictly at 32-bit precision, the result of adding
0.1ften times to 0 isIf intermediate computations are performed at greater precision than
floathas, it could result in exactly1.0or even slightly smaller numbers.To fix your problem, in this case you could use
because
10 * 0.1fis exactly1.0.Another option is to use a small tolerance in your
curves_bezierfunction,for a suitably small epsilon, maybe
float epsilon = 1e-6;.