NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Why can’t Swift add a Double to an Int?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Swift has a number of ways of storing data, such as strings, Booleans, and arrays. But when it comes to working with numbers, it has several very specific types, including Double, Float, and Int – there are many more than those, but they are the most commonly used.

Swift has these different numerical types because they store their data differently. For example, both Double and Int take the same amount of memory to store their number, but Int only stores whole numbers whereas Double can store values after the decimal place.

So, at the simplest level you can see that adding a Double to an Int isn’t safe because the Double can store things the Int can’t and that would be lost in the resulting integer.

Now, you might then think “well, how about when we add an Int to a Double we get back a new Double that can store all the data?” And that’s a great question!

The problem is that although Double uses the same amount of memory to store its value as Int, the way it stores its data is a little fuzzy – it has really great precision with smaller numbers, but increasingly fuzzy precision when you start working with large numbers. In fact, there are certain numbers that Double isn’t even able to hold, so instead it stores a very slightly different value.

Helpfully, Swift even warns us when this happens. For example, try this code:

let value: Double = 90000000000000001

When you build that, Swift shows a warning: '90000000000000001' is not exactly representable as 'Double'; it becomes '90000000000000000’.

Integers lose the ability to store fractional values, but they gain the ability to store precise values. This means the following code won’t produce a warning, because the number can be stored exactly:

let value: Int = 90000000000000001

So, it isn’t safe to add a Double to an Int because we lose any numbers after the decimal point, and it isn’t safe to add an Int to a Double because we lose some accuracy.

At this point, a third question might come to you: how about Swift lets us add an Int to a Double only when it’s sure the resulting value can be stored safely? After all, it’s very rare we need to work with numbers as big as 90000000000000001.

And that’s true, but the problem is that Swift can’t tell what your numbers will be when you build your code, so we’re back to the problem of safety – sure, you might be working with safe numbers most of the time, but Swift is specifically designed not to take risks even when the unexpected happens.

As a result of all this, Swift will refuse to automatically convert between its various numeric types – you can’t add an Int and a Double, you can’t multiply a Float and an Int, and so on.

Hacking with Swift is sponsored by Waldo

SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.

Try for free today!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.