I want to make sure that my understanding of the return type of C++ division,
int / int => return is int?
float / float => return is which type? float?
d
You are correct in all cases. The rules for operations involving at least one floating point type are that if either type is a long double
, the result is long double
; otherwise, if either type is double
the result is double
otherwise the result has type float
.
Arithmetic operations between two int
s produce an int
result.
The rules between other types are slightly more complex and can be implementation dependent; for almost all operations integer promotions mean that the operands are promoted to at least an int
sized types producing at least an int
sized result.