0

According to this thread Why don't Java's +=, -=, *=, /= compound assignment operators require casting?, the i+=j is safer but less precise than i=i+j in JAVA due to typecasting.

Is it also true in case of C++?

EDIT:

Assumption: int i; long j; i.e.the variables are of different datatypes

Hashir
  • 390
  • 5
  • 20
  • 1
    In C++, there is no observable difference in end result between `i += j` and `i = i + j`, so if there is loss of precision in `i = i + j` then there is also loss of precision in `i += j`. The main difference between C++ and Java, AFAIK, is that Java gives compilation errors in some cases where C++ does not (C++ simply gives implementation-defined or undefined behaviour [depending on the types] in relevant edge cases) – Peter May 20 '20 at 01:52

1 Answers1

4

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • The important difference is in C++ you can implicitly down-convert in the assignment (potentially losing data) which you cannot do in java. – Chris Dodd May 20 '20 at 00:16
  • if i is int and j is long. I perform i=i+j .. shouldnt it typecast long to int? and since long is higher ranged the precision is lost somewhat? – Hashir May 20 '20 at 00:20
  • @MuhammadHashirHassan - In C++, a number of such conversions happen implicitly, whereas Java just forces them to be explicit. However the conversion is done, precision is "lost somewhat" - in particular edge cases, not for all possible results of the addition. Personally, I consider Java gives a false sense of security in this regard - the conversion, whether done explicitly or implicitly, will "lose somewhat" - but by rejecting code without typecast and accepting code with typecast, Java implies that the typecast makes the conversion safe, which it doesn't (necessarily) – Peter May 20 '20 at 02:09
  • @Peter I think in java its to make sure that programmers do not accidentally typecast (lose precision), that is why they have to explicitly mention typecasting otherwise they can use the shortcut of compound operators – Hashir May 20 '20 at 03:48
  • 1
    @MuhammadHashirHassan - Comes to the same thing. Explicit use of typecast doesn't make code safer. It is the thought process (beyond, "the compiler rejects the code if I don't") that matters. But I've seen too few Java programmers who think about it beyond getting the compiler to accept the typecast. – Peter May 20 '20 at 04:56