Skip to content
Lesson 3 of 15

Step 1 of 5 · Reading · ~3 min

Learn

Getting Started

Numbers and Math

Swift gives you two number types you will use constantly — Int for whole numbers, Double for decimals — and it keeps them strictly apart.

let a = 17
let b = 5

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)

//> 22
//> 12
//> 85
//> 3
//> 2

Look at the fourth line. 17 / 5 is 3, not 3.4.

Why 17 / 5 is 3

Both operands are Int, so Swift performs integer division: it computes the quotient and discards the fractional part. The result's type is decided by the operands' types, never by what the answer "ought" to be.

ExpressionResult
17 / 53
1 / 20
17.0 / 5.03.4
9.0 / 2.04.5
1.0 / 20.5

1 / 2 giving 0 is the single most common beginner surprise in Swift. If you want a fraction, at least one side must already be a Double.

The literal trap

Those last two rows work because a bare 1.0 or 2 in source code is an untyped literal — Swift picks a type from context. Once a value has a name, its type is fixed and no promotion happens:

Compiles17 is still a literal, so Swift makes it a Double

print(17 / 5.0)

//> 3.4

Does not compilea is already an Int

let a = 17
let q = a / 5.0
// error: binary operator '/' cannot be applied to operands of type 'Int' and 'Double'

The fix is always the same: convert explicitly.

let a = 17
let b = 5
print(Double(a) / Double(b))

//> 3.4

Double(a) looks like a function call because it is one — an initialiser that builds a Double from an Int. There is no cast syntax and no automatic widening; Swift makes every conversion visible.

The remainder operator keeps the dividend's sign

% is a remainder, not a mathematical modulo. The sign of the result follows the left operand:

print(-9 % 4)
print(9 % -4)
print(-9 / 4)

//> -1
//> 1
//> -2

So -9 % 4 is -1, not 3. And integer division truncates toward zero, which is why -9 / 4 is -2 rather than -3. If you are testing "is this even", n % 2 == 0 is safe for negatives, but n % 2 == 1 is not — an odd negative gives -1.

Overflow stops the program

Swift does not silently wrap around on overflow the way C does. It traps.

let big = Int.max
print(big + 1)          // program dies here

Int.max is 9223372036854775807 and Int.min is -9223372036854775808. Adding one to the maximum terminates the process rather than producing a nonsense negative number. That is a deliberate choice: a crash you can find beats a wrong answer you cannot.

Reaching for more maths

import Foundation at the top of the file unlocks the usual library:

import Foundation

print(sqrt(16.0))
print(pow(2.0, 10.0))
print(abs(-7))
print(Double.pi)

//> 4.0
//> 1024.0
//> 7
//> 3.141592653589793

Note the types: sqrt and pow take and return Double, while abs(-7) stays an Int because its argument was one.

Your exercise

Read a width and a height and print the rectangle's area. The starter reads both values for you.

The mistake the grader catches is turning an Int answer into a Double. Both inputs are read as Int, so w * h is an Int and prints as 15. The moment you write Double(w) * Double(h) the output becomes 15.0 and every test fails, including the hidden 1 × 999 case which would print 999.0 instead of 999. Multiplication is exact here — there is nothing to gain by converting.

Up nextStringsStrings and Optionals

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…