The problem is that your second expression is not equivalent to the first one: it uses doubles
throughout, while the first one has a conversion to float
after the division, forcing the intermediate result to lower precision.
To build a fully equivalent expression, add a cast to float
after division, like this:
printf("%f", ((float)(100.0/3.0))*3.0);
// ^^^^^
This produces the same output as your first example, i.e. "99.999996
(demo)
If you use double
for x
in your first example, you get the output 100.000000
, too:
double x = 100.0/3.0;
printf("%f",x*3.0);
(another demo).