Table of Contents
3-1: Addition
3-2: Subtraction
3-3: Multiplication
3-4: Division
3-5: Modulus
3-6: Exponents
3-7: Square Roots
3-8: Absolute Value
3-9: Math Library
3-10: Order of Operations
input
2+7
output
res1: Int=9
input
4–2
output
res1: Int=2
Multiplication uses the asterisk (“*”) symbol.
input
4 * 5
output
res1: Int=20
Division is a bit less straight-forward.
input
9 / 4
output
res6: Int=2
Note that 9/4 = 2.25, but Scala rounds down our result. This is because we divided two integers and the result will also be an integer. In order to get a floating point number, we need to make at least one of the numbers a “double.”
input
9.0/4
output
res1: Double = 2.25
We can see now that we get the proper result. Let’s move onto remainders.
To find the remainder of a number after division, we use the “%” operator.
input
7 % 2
output
res1: Int = 1
Since 7 divided by 2 is equal to 3 with a remainder of 1, we get an output of “1”.
Exponents are a bit less simple in Scala than in Python. We need to access the math library and use the “.pow” method.
input
math.pow(3,3)
output
res1: Double=27.0
Similar process for square roots.
input
math.sqrt(64)
output
res1: Double = 8.0
Absolute value is another good one.
input
math.abs(-7.8)
output
res1: Double = 7.8
You can check out more mathematical operations in the math library. Other features include rounding, logarithms, and trigonometric operations (e.g. sin, cos, tan).
Finally, order of operations are pretty straight-forward.
input
3+4*1+1
output
res10: Int=9
input
(3+4)*(1+1)
output
res11: Int=14
Part 1: Installation, Set-Up, and "Hello World"
Part 2: Data Types
Part 3: Arithmetic Operators
Part 4: Variables vs Values
Part 5: String Operations
Part 6: Tuples
Part 7: Lists
Part 8: Arrays
Part 9: Sets
Part 10: Maps
Part 11: Logical Operators
Part 12: If Statements
Part 13: For Loops
Part 14: While Loops
Part 15: Functions