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.
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.
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.
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.
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.
Practical Example: Repeating Time Slots
Suppose you map minutes to repeating 15-minute buckets.
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.
Also test boundary values near zero and exact multiples.
Migration Guidance for Existing Code
When fixing old codebases:
- search for floating expressions using unsupported modulo patterns
- replace with
truncatingRemainderwhere appropriate - add explicit wrap helper for positive-range requirements
- 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 andtruncatingRemainder(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.

