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
In Swift, the message telling you to use truncatingRemainder usually appears when % is used with floating-point values. The % operator is for integer modulo, while floating-point remainder uses a different API. Understanding this difference avoids type errors and subtle numeric bugs.
Why the Error Appears
Swift separates integer arithmetic from floating-point arithmetic intentionally. % is defined for integer types such as Int and UInt, but not for Double or Float.
Invalid example:
Correct floating-point version:
Integer Modulo vs Floating Remainder
For integers, % works and is usually what you want.
For floating values, use truncatingRemainder(dividingBy:).
This distinction keeps APIs explicit and prevents accidental coercions.
Negative Number Behavior
Remainder with negative inputs can surprise developers. Validate behavior in your domain.
If your business rule needs always-positive modulo, normalize result manually.
Choosing Numeric Types Deliberately
Many errors come from mixing Int and Double in the same calculation. Define numeric intent early.
- Use integer math for counts, indices, and discrete cycles.
- Use floating math for measurements and fractional values.
Converting between them should be explicit.
Helper Functions for Readability
Practical Use Cases
Remainder logic appears in progress indicators, animation phases, cyclic buffers, and periodic scheduling math. When values are fractional, using truncatingRemainder avoids unsafe integer coercion and keeps behavior aligned with floating-point domain expectations.
For user-facing calculations, round display values separately from internal remainder math so numeric precision and UI formatting concerns stay decoupled.
This separation improves correctness and readability in production code.If remainder logic appears often, wrap it in clear helpers.
This keeps call sites readable and centralizes edge-case handling.
Testing Numeric Edge Cases
Remainder operations should be tested with:
- Zero divisors where invalid input should fail fast.
- Negative operands.
- Very small floating values.
- Values near precision boundaries.
Floating-point tests should include tolerance checks rather than strict equality where appropriate.
Integer Conversion Caution
If you intentionally convert floating values to integers before using %, document rounding behavior clearly. Int conversion truncates toward zero, which can change expected remainder semantics in financial or scientific calculations.
Use this only when truncation is explicitly desired.## Common Pitfalls
- Using
%withDoubleorFloatvalues. - Assuming integer and floating remainder semantics are identical.
- Ignoring negative-result behavior in modulo logic.
- Mixing numeric types without explicit conversions.
- Skipping tests for divisor zero and boundary values.
Summary
- '
%in Swift is for integer modulo, not floating-point types.' - Use
truncatingRemainder(dividingBy:)forDoubleandFloat. - Validate remainder behavior for negative inputs.
- Keep numeric type intent explicit in code design.
- Add focused tests for edge cases and precision-sensitive values.

