|< Why do Swift’s enums have raw values?||Why does Swift have a dedicated division remainder operator? >|
Updated for Xcode 13.3
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
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
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.
SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.
Link copied to your pasteboard.