Does anybody can explain to me why the execution of this program enters the infeasible if?
I know the existence of precisions problems with double and float, but I can’t figure out the problem in this example..
Thank you in advance!
NB: I compiled it under Ubuntu 10.10 with GCC 4.4.5
#include <stdio.h>
#define NUM_JOINTS 1
typedef struct {
double t1;
double t2;
double t3;
} jointProfile;
jointProfile jp[NUM_JOINTS];
int main() {
jp[0].t1 = 0.51639777949432230652604403076111339032649993896484375;
jp[0].t2 = 0.00000000000000000000000000000000000000000000000000000;
jp[0].t3 = 0.77459666924148340427791481488384306430816650390625000;
double maxTime = (jp[0].t1 + jp[0].t2 + jp[0].t3);
if ((jp[0].t1 + jp[0].t2 + jp[0].t3) < maxTime) {
printf("Infeasible\n");
}
return 0;
}
Well, apparently you don’t know enough about the subtleties of floating point computations.
See e.g. http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
If you’re doing this on a 32-bit Ubuntu, the problem is likely that maxtime is spilled to memory and thus loses bits.
One fix is to change the conditional to