Can some enlighten me with the multiplication behavior using double datatype in c++? In an example program below, I have three variables (x,y,z) of type double. when I multiply these variables the products are different depending on the order. Keep in mind the values I'm using in the example are my sample computational values.
void main()
{
double x, y, z;
double prod1, prod2;
x = 8.3832170034737544e-05;
y = 5.6690000000000000e-08;
z = 0.29999999999999999;
prod1 = x*y*z;
prod2 = y*z*x;
}
results:
prod1 : 1.4257337157807814e-12
prod2 : 1.4257337157807812e-12
In the example above, results for both prod1 and prod2 are different. How can I fix this issue in which I can always have the same result whatever the order of (x,y,z) is and using the exact same values.