What arithmetic operators do
Arithmetic operators let a program calculate with numbers, either written directly in the code or stored in variables. Scala has five of them:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 2 + 6 | 8 |
- | Subtraction | 10 - 7 | 3 |
* | Multiplication | 3 * 3 | 9 |
/ | Division | 6 / 2 | 3 |
% | Remainder (modulo) | 8 % 3 | 2 |
Put the expression inside println to see its value:
println(2 + 6)
println(10 - 7)
println(3 * 3)
println(6 / 2)
println(8 % 3)
8
3
9
3
2
Using variables
An operator works the same way on a variable as on a literal number. Scala reads the variable's current value and calculates with it:
var num = 6
println(num + 5)
println(10 - num)
println(3 * num)
println(num / 2)
println(num)
11
4
18
3
6
The last line shows that num is still 6. An expression produces a new value and leaves the variable alone. Only an assignment with = changes it, and only a var can be assigned again. Assigning to a val is a compile error: Reassignment to val x.
Division: Int against Double
Division is where beginners get surprised. The type of the operands decides what / does.
- If both operands are
Int, the result is anIntand the fractional part is dropped (truncated toward zero). - If at least one operand is a
Double, the result is aDoubleand keeps the decimals.
val a = 2
val b = 3
println(20 / 3)
println(b / a)
println(7.0 / 3)
println(b.toDouble / a)
println(b / a.toDouble)
6
1
2.3333333333333335
1.5
1.5
To get a real quotient from two Int variables, convert one of them with toDouble. Scala does not use the C-style cast (Double) x: writing (Double) 7 / 3 fails to compile with ',' or ')' expected, but integer literal found.
Truncation goes toward zero, not downward, so negative numbers show it clearly:
println(-7 / 2)
println(-7 % 2)
-3
-1
The remainder takes the sign of the left operand. Dividing an Int by zero throws java.lang.ArithmeticException: / by zero, while dividing a Double by zero gives Infinity, -Infinity or NaN.
Precedence and Int size
*, / and % are evaluated before + and -, and operators of equal rank go left to right. Parentheses change the order. An Int is 32 bits, so a result beyond 2147483647 wraps around without any error: Int.MaxValue + 1 is -2147483648. Use Long for larger values.
Common mistakes
- Expecting decimals from integer division.
1 / 2is0, not0.5. If a result looks too small, check whether both operands areInt. - Forgetting the result is not stored. A line like
num + 5on its own calculates a value and throws it away. Print it, or assign it to a variable such asval total = num + 5. - Mixing up
/and%. For 15 and 4,15 / 4is the quotient3and15 % 4is the remainder3. For 20 and 3 they differ:6and2.
The exercise
The variable price holds the cost of one product. Andy buys 4 products at that price and one bag for 15 dollars. The total is four times the price plus 15, so the expression combines a multiplication and an addition. Because * binds tighter than +, no parentheses are needed:
val price: Int = 83
println(4 * price + 15)
With price equal to 83 this prints 347: 4 * 83 is 332, and 332 + 15 is 347.
Key takeaways
- Scala has
+,-,*,/and%. - Calculating with a variable does not change it; only
=does, and only on avar. Int / Intdrops the fraction; make one side aDoubleto keep it.- Multiplication, division and remainder come before addition and subtraction.
