Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The /= operator is a compound assignment operator that divides the value of the left-hand operand by the value of the right-hand operand, subsequently assigning the resulting quotient directly back to the left-hand operand.

Syntax

lhs /= rhs
This operation is semantically equivalent to the expanded assignment and division expression:
lhs = lhs / rhs

Technical Characteristics

  • Mutability Requirement: The left-hand operand (lhs) must be declared as a mutable variable (using var). It cannot be a constant (let) or an immutable literal, as the operator mutates the operand in place. Under the hood, the left-hand operand is passed as an inout parameter to the operator’s underlying function.
  • Type Strictness: Swift requires strict type matching. Both lhs and rhs must be of the same type, or the compiler will throw an error. Swift does not perform implicit type coercion between different numeric types (e.g., Int and Double).
  • Integer vs. Floating-Point Behavior:
    • When applied to integer types (e.g., Int, UInt8), the operator performs truncating division. Any fractional component of the quotient is discarded towards zero.
    • When applied to floating-point types (e.g., Double, Float), the operator retains the fractional component according to IEEE 754 standard arithmetic.
  • Division by Zero:
    • For integer types, attempting to divide by zero using /= triggers a runtime trap (fatal error), intentionally crashing the application to prevent undefined behavior.
    • For floating-point types, division by zero results in inf (infinity) or -inf, while dividing zero by zero results in nan (Not a Number), adhering to standard floating-point specifications without crashing.

Operator Overloading

The /= operator can be implemented for custom data types (such as custom structs or classes) by overloading the operator. This is achieved by defining a static func where the left-hand operand is marked as inout.
static func /= (lhs: inout CustomType, rhs: CustomType) {
    // Implementation mutating lhs
}
Master Swift with Deep Grasping Methodology!Learn More