What does (rounding/=10.0;) mean? Why is there a slash?
- 991
- 8
- 28
- 37
- 4
-
4Oups, it's time to start reading you beginner's C++ text book. – Jabberwocky May 09 '19 at 15:33
-
Do we seriously not have a duplicate for these? – Max Langhof May 09 '19 at 15:42
2 Answers
rounding /= 10.0; means:
- convert
roundingtodouble(if it is notdoubleorlong doublealready), - divide it by
double10.0, - and assign the quotient back to
rounding. Ifroundingis 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.
- 131,725
- 17
- 180
- 271
-
21. Or if `rounding` is not a primitive type without an appropriate conversion operator. – Bathsheba May 09 '19 at 15:31
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)));
- 10,717
- 5
- 39
- 72