Does the presence of one floating-point data type (e.g. double
) ensure that all +, -, *, /, %, etc math operations assume double operands?
If the story is more complicated than that, is there a resource that describes these rules? Should I not ask such questions and always explicitly cast int
to double
when the result of the equation is double
. Here are some equations I'm thinking about. I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent.
int a(1), b(2), c(3);
double d(4.);
double result1 = a + b/d + c; // equal to 4 or to 4.5?
double result2 = (a + b)/d + c; // equal to 3 or to 3.75?
double result3 = a/b + d; // equal to 4 or to 4.5?
See Question&Answers more detail:os