Swift
Absolute Value
Convert
Programming
iOS Development

Swift - Convert to absolute value

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

swift
1let x = -42
2let y = -3.75
3
4print(abs(x))
5print(abs(y))

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.

swift
1let intVal: Int = -12
2let doubleVal: Double = -12.5
3
4let absInt = abs(intVal)
5let absDouble = abs(doubleVal)
6
7print(absInt)
8print(absDouble)

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.

swift
let minimum = Int.min
print(minimum)

If your domain can reach that value, handle it explicitly.

swift
1func safeAbsolute(_ value: Int) -> UInt {
2    if value == Int.min {
3        return UInt(Int.max) + 1
4    }
5    return UInt(abs(value))
6}
7
8print(safeAbsolute(-10))

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.

swift
let values = [-5, 3, -9, 0, 8]
let normalized = values.map { abs($0) }
print(normalized)

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.

swift
1let z = -0.0
2print(abs(z))
3print(abs(Double.infinity))
4print(abs(Double.nan))

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 abs for normal numbers
  • handle Int.min if 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:

swift
value < 0 ? -value : value

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 abs for ordinary absolute-value conversion.
  • It works naturally with common integer and floating-point types.
  • 'Int.min is 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 abs with custom logic.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.