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

The `%` operator in Swift is the **remainder operator**, which calculates the integer remainder after dividing one number (the dividend) by another (the divisor). Unlike a strict mathematical modulo operator found in some other languages, Swift's `%` operator computes the remainder using truncating division. This means the sign of the result is determined strictly by the dividend, while the sign of the divisor is ignored.

## Syntax and Underlying Equation

```swift theme={"dark"}
let result = a % b
```

Internally, the `%` operator satisfies the following equation, where `multiplier` is the quotient (the number of times `b` fits into `a` using truncating division), and `(b * multiplier)` represents the actual multiple:

`a = (b * multiplier) + remainder`

## Integer Evaluation

The operator evaluates the operands and returns the exact integer left over.

```swift theme={"dark"}
let positiveRemainder = 9 % 4
// 9 = (4 * 2) + 1
// positiveRemainder equals 1
```

## Handling of Negative Operands

Because Swift uses truncating division, the remainder will always carry the sign of the left-hand operand (`a`). The sign of the right-hand operand (`b`) is entirely disregarded during the calculation.

```swift theme={"dark"}
// Negative dividend, positive divisor
let negativeDividend = -9 % 4
// -9 = (4 * -2) + (-1)
// negativeDividend equals -1

// Positive dividend, negative divisor
let negativeDivisor = 9 % -4
// 9 = (-4 * -2) + 1
// negativeDivisor equals 1

// Negative dividend, negative divisor
let bothNegative = -9 % -4
// -9 = (-4 * 2) + (-1)
// bothNegative equals -1
```

## Type Constraints

The `%` operator is strictly constrained to integer types (e.g., `Int`, `Int8`, `UInt64`). Attempting to use the `%` operator on floating-point types (`Double`, `Float`, `CGFloat`) will result in a compile-time error.

To achieve the equivalent mathematical operation on floating-point numbers, Swift requires the use of the `truncatingRemainder(dividingBy:)` instance method. When demonstrating this, it is best to use values with exact binary representations to avoid standard IEEE 754 floating-point precision artifacts.

```swift theme={"dark"}
// INVALID: Compiler error
// let floatRemainder = 8.5 % 2.5 

// VALID: Floating-point equivalent
let floatRemainder = 8.5.truncatingRemainder(dividingBy: 2.5)
// floatRemainder equals 1.0
```

## Division by Zero

Like the standard division operator (`/`), passing `0` as the right-hand operand to the `%` operator is an illegal operation. Doing so will trigger a runtime trap (crash) rather than returning `nil` or `NaN`.

If the divisor is a constant (`let`) known at compile time, Swift's constant folding will catch the operation and emit a compile-time error. To observe the runtime trap, the divisor's value must be evaluated at runtime (e.g., using a `var` or a dynamically passed argument).

```swift theme={"dark"}
let x = 10
var y = 0
// let z = x % y // Execution halts at runtime: Fatal error: Division by zero
```

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