Swift - Convert to absolute value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, converting a number to its absolute value is usually just a call to abs. Most of the nuance is not in the normal case, but in knowing how it behaves for different numeric types and for edge cases such as Int.min.
The Normal Case: Use abs
For standard integers and floating-point values, abs is the correct default.
You do not need a custom helper for ordinary absolute-value conversion.
It Works Across Common Numeric Types
abs preserves the general numeric type category you pass in.
That means you can use it naturally with:
- '
Int' - '
Float' - '
Double'
without unnecessary manual conversions.
The Important Integer Edge Case: Int.min
Signed integers in two's complement have one more negative value than positive value. That means Int.min does not have a positive counterpart representable as Int.
So abs(Int.min) is a special case that can trap or overflow depending on the context.
If your domain can reach that value, handle it explicitly.
Most application code never encounters Int.min, but numeric or library code should still respect it.
Absolute Value in Collection Transformations
A common use case is normalizing a sequence of values.
This is concise and idiomatic Swift.
Floating-Point Special Values
With floating-point numbers, abs behaves normally for finite values, but special cases still exist.
abs(Double.nan) is still nan. So if your code cannot tolerate non-finite values, check isFinite separately.
Generic Numeric Intent Still Matters
Even though abs is simple to call, choose the surrounding numeric type deliberately. If your domain value is fundamentally an Int, keep it as Int. If it represents measurements with fractions, keep it as Double or Float rather than converting only to make the absolute-value call look uniform.
Keep the Rule Simple
For most Swift code, a practical rule is:
- use
absfor normal numbers - handle
Int.minif signed-integer edge cases matter - validate non-finite floating-point values if the domain requires it
That covers almost all real-world use cases without inventing custom math helpers.
Why Not Reimplement It Yourself
A manual expression like:
looks simple, but it reintroduces the same edge-case problem for Int.min and spreads custom numeric behavior through the codebase.
The standard library function is clearer and less error-prone.
Common Pitfalls
Rewriting absolute-value logic manually instead of using abs adds risk for no real benefit.
Forgetting about Int.min can create crashes or overflow in low-level numeric code.
Assuming abs also sanitizes nan or infinity for floating-point values is incorrect.
Converting between numeric types unnecessarily before calling abs can hide edge cases and reduce clarity.
Summary
- Use Swift's built-in
absfor ordinary absolute-value conversion. - It works naturally with common integer and floating-point types.
- '
Int.minis the main integer edge case that needs explicit care.' - Collection transformations often use
map { abs($0) }cleanly. - Keep special-case handling explicit instead of replacing
abswith custom logic.
Related reading
- Swift - encode URL
- Swift - encode URL
- Swift - How creating custom viewForHeaderInSection, Using a XIB file?
- Swift - How to convert String to Double
- Swift - How to detect orientation changes
- Swift - How to detect orientation changes
- Swift - How to dismiss all of view controllers to go back to root
- Swift - How to dismiss all of view controllers to go back to root
.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.