What does is unavailable Use truncatingRemainder instead mean?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- What does is unavailable Use truncatingRemainder instead mean?
- What does it mean to inflate a view from an xml file?
- What does LayoutInflater in Android do?
- What does private mean in Objective-C?
- What does it mean The serializable class does not declare a static final serialVersionUID field?
- What does it mean when MySQL is in the state Sending data?
- What does Protocol ... can only be used as a generic constraint because it has Self or associated type requirements mean?
- What does synchronized do as a singleton method in objective C?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.