Say we have three int values x, y, and z, then we cast the three values from int to double and we get dx, dy, and dz. Is this formula (dx * dy) * dz == dx * (dy * dz)
always true?
The generation process and my verification is as below:
int x = 0x64e73387;
int y = 0xd31cb264;
int z = 0xd22f1fcd;
double dx = (double) x;
double dy = (double) y;
double dz = (double) z;
printf("x: %i; dx: %f\n", x, dx);
printf("y: %i; dy: %f\n", y, dy);
printf("z: %i; dz: %f\n", z, dz);
double dx_y_z = (dx * dy) * dz;
double dx_z_y = (dx * dz) * dy;
printf("dx_y_z: %f; dx_z_y: %f\n", dx_y_z, dx_z_y);
printf("dxy based on dx_y_z: %f\n", dx_y_z / dz);
printf("dxy based on dx_z_y: %f\n", dx_z_y / dz);
return 0;
From my point of view, I know it is stupid to compare two floating-point numbers directly as they are just the approximation based on IEEE standard and are not the exact math value. But when I try to print out the values, I get the results as below and I am confused:
x: 1692873607; dx: 1692873607.000000
y: -753094044; dy: -753094044.000000
z: -768663603; dz: -768663603.000000
dx_y_z: 979963870399385375233540096.000000
dx_z_y: 979963870399385512672493568.000000
dxy based on dx_y_z: -1274893030676496640.000000
dxy based on dx_z_y: -1274893030676496640.000000
Now I have two questions:
- Why the values of dx_y_z and dx_z_y differ that much?
- Why when I try to calculte the dxy based on two different value dx_y_z and dx_z_y, I get exactly the same value?