In C++ if both operands i, j
are of the same type then no conversion is done.
If they are different then the lower range (e.g. int
) is promoted to higher range (e.g. long
or float
or double
)
This promotion, from down to up, most times doesn't lose precision. But some int-to-float conversions may lose some digits.
There's no difference between i+=j
and i=i+j
.
In some cases you must be aware of loosing precision. For example:
int i = 2;
double d= 4.8;
i += d;
Because d
is higher-range than i
the operation is made with doubles: 2+4.8 = 6.8
But at the time of storing it back to i
the result is truncated, so i=6
For the case int i
, long j
the operations (i+j) are done at long
precision, but some lose may happen at i
if j
doesn't fits into an int
.