Swift
programming
error handling
coding
truncatingRemainder

What does is unavailable Use truncatingRemainder instead mean?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This Swift compiler message appears when code applies integer-style remainder semantics to floating-point values. Swift intentionally separates integer modulo from floating-point remainder so numeric behavior is explicit. The fix is usually to replace the unsupported operation with truncatingRemainder(dividingBy:) and verify that sign and wrap behavior match your domain rules.

Why Swift Shows This Error

In Swift, % is defined for integer types. For Double and Float, the language provides dedicated remainder methods.

swift
1let intRemainder = 17 % 5
2print(intRemainder) // 2
3
4let floatRemainder = 17.5.truncatingRemainder(dividingBy: 5.0)
5print(floatRemainder) // 2.5

If old code or assumptions from other languages treat floating modulo exactly like integer modulo, the compiler points you to truncatingRemainder.

Integer Modulo and Floating Remainder Are Different

Integer modulo operates on exact integer arithmetic. Floating remainder involves binary floating-point representation and can produce values affected by precision and sign rules.

swift
print((-7).quotientAndRemainder(dividingBy: 5).remainder)      // -2
print((-7.0).truncatingRemainder(dividingBy: 5.0))             // -2.0

If your business rule expects a nonnegative wrapped value, you need an extra normalization step.

Build a Positive Wrap Helper

A common requirement is mapping values into cyclic ranges such as angles or time windows.

swift
1func positiveWrap(_ value: Double, modulus: Double) -> Double {
2    precondition(modulus > 0)
3    let r = value.truncatingRemainder(dividingBy: modulus)
4    return r >= 0 ? r : r + modulus
5}
6
7print(positiveWrap(-30.0, modulus: 360.0)) // 330.0
8print(positiveWrap(725.0, modulus: 360.0)) // 5.0

This avoids repeated ad hoc fixes and keeps range behavior explicit.

Mixed Numeric Types Cause Extra Friction

The error often appears in expressions mixing integers and floating values. Swift does not perform implicit widening between numeric types.

swift
1let total: Double = 123.0
2let divisor: Int = 10
3
4let result = total.truncatingRemainder(dividingBy: Double(divisor))
5print(result)

Converting types at expression boundaries keeps formulas readable and avoids compiler confusion.

Picking the Right Remainder Semantics

Not every wrap use case wants truncation semantics. If code migrated from another platform, verify expected behavior for:

  • negative values
  • exact multiples
  • tiny decimal rounding differences

Document intent in code comments near remainder logic so later refactors preserve behavior.

swift
// Keep truncation semantics to match existing backend calculations.
let normalized = value.truncatingRemainder(dividingBy: 24.0)

Practical Example: Repeating Time Slots

Suppose you map minutes to repeating 15-minute buckets.

swift
1func slotOffset(_ minutes: Double, period: Double = 15.0) -> Double {
2    let raw = minutes.truncatingRemainder(dividingBy: period)
3    return raw >= 0 ? raw : raw + period
4}
5
6print(slotOffset(47.2))  // 2.2
7print(slotOffset(-2.5))  // 12.5

Without normalization, negative values can land in unexpected buckets.

Testing Numeric Behavior Safely

Remainder logic should be covered with tolerance-aware tests for floating-point values.

swift
1import XCTest
2
3final class ModTests: XCTestCase {
4    func testWrap() {
5        XCTAssertEqual(positiveWrap(725.0, modulus: 360.0), 5.0, accuracy: 1e-12)
6        XCTAssertEqual(positiveWrap(-30.0, modulus: 360.0), 330.0, accuracy: 1e-12)
7    }
8}

Also test boundary values near zero and exact multiples.

Migration Guidance for Existing Code

When fixing old codebases:

  1. search for floating expressions using unsupported modulo patterns
  2. replace with truncatingRemainder where appropriate
  3. add explicit wrap helper for positive-range requirements
  4. validate behavior with regression tests before release

This protects geometry, animation, and scheduling logic from subtle numeric regressions.

Common Pitfalls

A common pitfall is assuming % should behave identically for Double and Int.

Another pitfall is ignoring negative-input behavior after replacing syntax, resulting in incorrect cyclic ranges.

A third pitfall is relying on exact floating equality in tests, which can create brittle assertions.

Teams also apply conversion fixes without documenting intended semantics, causing repeated confusion in future maintenance.

Summary

  • The compiler message indicates unsupported floating modulo usage.
  • Use % for integers and truncatingRemainder(dividingBy:) for floating-point values.
  • Add normalization when cyclic domains require nonnegative results.
  • Keep numeric conversions explicit and test boundary behavior.
  • Document remainder semantics to keep behavior stable during refactors.

Course illustration
Course illustration

All Rights Reserved.