> ## 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.

# Swift Division

The `/` operator in Swift is a binary arithmetic operator that performs division, calculating the quotient of a left-hand operand (dividend) divided by a right-hand operand (divisor). For standard library numeric types (such as `Int` and `Double`), both operands must be of the exact same type, yielding a result of that matching type.

```swift theme={"dark"}
let dividend = 10
let divisor = 2
let quotient = dividend / divisor
```

## Type-Specific Evaluation Semantics

The behavior of the `/` operator changes fundamentally depending on the protocol conformance of the operands:

**1. Integer Division (`BinaryInteger` protocol)**
When applied to integer types (e.g., `Int`, `UInt8`), the `/` operator performs **truncating division**. It calculates the quotient and discards any fractional remainder, rounding strictly toward zero.

```swift theme={"dark"}
let positiveInteger = 10 / 3  // Evaluates to 3
let negativeInteger = -10 / 3 // Evaluates to -3
```

**2. Floating-Point Division (`FloatingPoint` protocol)**
When applied to floating-point types (e.g., `Double`, `Float`, `CGFloat`), the `/` operator performs division according to IEEE 754 standard semantics. While it preserves the fractional component, the result is subject to floating-point precision limits and is often a correctly rounded approximation rather than a mathematically exact value.

```swift theme={"dark"}
let floatResult = 10.0 / 3.0 // Evaluates to 3.3333333333333335
```

## Safety, Traps, and Edge Cases

Swift enforces strict safety checks for division operations, handling edge cases differently based on the underlying data type:

* **Integer Division by Zero:** Dividing any `BinaryInteger` by zero is an invalid operation. If the divisor is a literal `0`, the Swift compiler catches this at compile-time and emits a build error. If the divisor is a variable that evaluates to `0` at runtime, it triggers a runtime trap, resulting in a fatal error and application crash.
* **Integer Overflow:** Dividing the minimum representable value of a signed integer type by `-1` causes an integer overflow. This occurs because the mathematically correct positive result exceeds the maximum representable value for that specific integer type (e.g., `Int8.min` is `-128`, but `Int8.max` is `127`). If the `-1` is provided as a literal, the compiler performs constant folding and emits a compile-time error. If the `-1` is evaluated at runtime via a variable, it triggers a runtime trap.
* **Floating-Point Infinity/NaN:** Dividing a `FloatingPoint` by `0.0` does not trap. Instead, it returns standard IEEE 754 non-finite values: positive infinity (`inf`), negative infinity (`-inf`), or Not a Number (`nan`).

```swift theme={"dark"}
// Integer division edge cases
let zero = 0
// let compileZeroError = 5 / 0 // Compile-time error: division by zero
// let runtimeZeroCrash = 5 / zero // Runtime trap: Fatal error (Division by zero)

// Integer overflow edge cases
// let compileOverflowError = Int8.min / -1 // Compile-time error: arithmetic overflow
let minusOne: Int8 = -1
// let runtimeOverflowCrash = Int8.min / minusOne // Runtime trap: Fatal error (Arithmetic overflow)

// Floating-point division by zero
let floatInf = 5.0 / 0.0       // Evaluates to inf
let floatNegInf = -5.0 / 0.0   // Evaluates to -inf
let floatNaN = 0.0 / 0.0       // Evaluates to nan
```

## Operator Overloading and Compound Assignment

The `/` operator is implemented as a static function and can be overloaded for custom structures or classes. Swift's operator overloading supports mixed-type operands, allowing developers to define division between different types.

The `/=` compound assignment operator mutates the left-hand operand in place. It is **not** automatically synthesized by the compiler when the `/` operator is defined; it must be explicitly implemented as a separate static function with an `inout` left-hand operand.

```swift theme={"dark"}
struct CustomVector {
    var x: Double
    var y: Double
    
    // Same-type overload
    static func / (lhs: CustomVector, rhs: CustomVector) -> CustomVector {
        return CustomVector(x: lhs.x / rhs.x, y: lhs.y / rhs.y)
    }
    
    // Mixed-type overload
    static func / (lhs: CustomVector, rhs: Double) -> CustomVector {
        return CustomVector(x: lhs.x / rhs, y: lhs.y / rhs)
    }
    
    // Compound assignment overload
    static func /= (lhs: inout CustomVector, rhs: Double) {
        lhs = lhs / rhs
    }
}

// Usage
var vector = CustomVector(x: 10.0, y: 20.0)
let scaledVector = vector / 2.0 // Evaluates to CustomVector(x: 5.0, y: 10.0)

vector /= 2.0 // Mutates 'vector' in place to CustomVector(x: 5.0, y: 10.0)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
