Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators







Basic Arithmetic Operations

  • Overview: Introduction to performing basic arithmetic operations like addition, subtraction, multiplication, and division in Scala.
  • Operations:
    • Addition (+): Adds two numbers.
    • Subtraction (-): Subtracts the second number from the first.
    • Multiplication (*): Multiplies two numbers.
    • Division (/): Divides the first number by the second. Note the behavior with integer division (result is truncated).
    • Modulus (%): Returns the remainder of the division of the first number by the second.







Examples:

val sum = 5 + 3     // 8
val diff = 5 - 3    // 2
val product = 5 * 3 // 15
val quotient = 5 / 3  // 1 (if integers), or 1.666... (if floats or doubles)
val remainder = 5 % 3 // 2







Comparison Operations

  • Overview: Describes how to compare two numbers in Scala using comparison operators.
  • Operations:
    • Equal to (==): Checks if two numbers are equal.
    • Not equal to (!=): Checks if two numbers are not equal.
    • Greater than (>): Checks if the first number is greater than the second.
    • Less than (<): Checks if the first number is less than the second.
    • Greater than or equal to (>=): Checks if the first number is greater than or equal to the second.
    • Less than or equal to (<=): Checks if the first number is less than or equal to the second.
  • Examples:
    val isEqual = 5 == 3      // false
    val isNotEqual = 5 != 3   // true
    val isGreaterThan = 5 > 3 // true
    val isLessThan = 5 < 3    // false
    







Advanced Mathematical Operations

  • Overview: Introduction to more complex mathematical operations available in Scala, including those in the scala.math package.
  • Key Functions:
    • Exponential (math.pow, math.exp): Raises a number to the power of another, calculates the exponential.
    • Logarithm (math.log, math.log10): Calculates the natural or base 10 logarithm of a number.
    • Square Root (math.sqrt): Calculates the square root of a number.
    • Trigonometry (math.sin, math.cos, math.tan): Performs trigonometric operations.
  • Examples:
    val power = scala.math.pow(2, 3)  // 8.0
    val squareRoot = scala.math.sqrt(16) // 4.0
    val sinValue = scala.math.sin(scala.math.Pi / 2) // 1.0
    







Numeric Type Conversions

  • Overview: Details on how to convert between different numeric types in Scala.
  • Conversions:
    • Implicit conversions happen automatically when required by the context.
    • Explicit conversions can be performed using methods like toInt, toDouble, etc.
  • Examples:
    val intVal: Int = 10
    val doubleVal: Double = intVal.toDouble // Explicit conversion to Double
    







Best Practices

  • Precision and Accuracy: Choose the appropriate numeric type (Float vs. Double, Int vs. Long) based on the required precision and magnitude.
  • Immutability: Favor immutable values (val) over mutable ones (var) for better predictability and safety in concurrent environments.
  • Numeric Literals: Use suffixes (L for Long, D for Double, F for Float) to clearly indicate the type of numeric literals.