-5

What does (rounding/=10.0;) mean? Why is there a slash?

Mode77
  • 991
  • 8
  • 28

2 Answers2

6

rounding /= 10.0; means:

  1. convert rounding to double (if it is not double or long double already),
  2. divide it by double 10.0,
  3. and assign the quotient back to rounding. If rounding is an integer that truncates the fractional part of the quotient.

If rounding is a user-defined type with an overloaded operator/= that could mean anything else.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
4

It is the division assignment operator. It is one of the compound assignment operators.

So if we have

a /= b

then this is exactly the same as the behavior of the expression

a = a / b

except that the expression a is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls.


So in this case

rounding/=10.0;

means

rounding = rounding / 10.0;

The reason it probably has 10.0 and not 10 is so that integer division is avoided and floating point division is used instead.


Something else to keep in mind would be that /= is an operator with a lower precedence than / and that /= operates right-to-left while / operates left-to-right.

What this means in practice is that something like

a /= 1 / 3 / 3;

would be the same as

a /= (1 / (3 / 3));

which, given that = is also at the same level of precedence as /=, is the same as

a = (a / (1 / (3 / 3)));
wally
  • 10,717
  • 5
  • 39
  • 72