For example, when I\'m dividing two ints and want a float returned, I superstitiously write something like this:
int a = 2, b = 3;
float c = (float)a / (floa
int a = 2, b = 3;
float c = static_cast(a) / b; // need to convert 1 operand to a float
Five rules of thumb to remember:
The ANSI C rules are as follows:
Most of these rules also apply to C++ though not all types are officially supported (yet).
Overflow is always a problem. Note. The type of the result is the same as the input operands so all the operations can overflow, so yes you do need to worry about it (though the language does not provide any explicit way to catch this happening.
As a side note:
Unsigned division can not overflow but signed division can.
std::numeric_limits::max() / -1 // No Overflow
std::numeric_limits::min() / -1 // Will Overflow