Notice: Undefined index: understanding-swift in /var/sites/hackingwithswift.com/site/src/Controller/NewBooksReadController.php on line 89
Why can’t Swift add a Double to an Int? - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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

Paul Hudson    @twostraws   

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 Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

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

BUY OUR BOOKS
Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.