Converting Int to Float loses precision for large numbers in Swift
Asked Answered
U

2

7

XCode 6.3.1 Swift 1.2

let value: Int = 220904525
let intmax = Int.max
let float = Float(value) // Here is an error probably
let intFromFloat = Int(float)
let double = Double(value)
println("intmax=\(intmax) value=\(value) float=\(float) intFromFloat=\(intFromFloat) double=\(double)")
// intmax=9223372036854775807 value=220904525 float=2.20905e+08 intFromFloat=220904528 double=220904525.0

The initial value is 220904525. But when I convert it to float it becomes 220904528. Why?

Undersized answered 15/5, 2015 at 13:28 Comment(4)
yeah, that's how floating point works.Selfmoving
possible duplicate of Loss of precision - int -> float or doubleSelfmoving
@TheParamagneticCroissant I think it is not duplicate because in Swift these types (Float Double Int) are structures and this problem of plain float values may be solvedUndersized
that's entirely irrelevant. Swift's Float is also IEEE754-32, the fact that it is (may be) wrapped in a struct doesn't make it any better (have more precision).Selfmoving
T
14

This is due to the way the floating-point format works. A Float is a 32-bit floating-point number, stored in the IEEE 754 format, which is basically scientific notation, with some bits allocated to the value, and some to the exponent (in base 2), as this diagram from the single-precision floating-point number Wikipedia article shows:

32-bit float format

So the actual number is represented as

(sign) * (value) * (2 ^ (exponent))

Because the number of bits allocated to actually storing an integer value (24) is smaller than the number of bits allocated for this in a normal integer (all 32), in order to make room for the exponent, the less significant digits of large numbers will be sacrificed, in exchange for the ability to represent almost infinite numbers (a normal Int can only represent integers in the range -2^31 to 2^31 - 1).

Some rough testing indicates that every integer up to and including 16777216 (2 ^ 24) can be represented exactly in a 32-bit float, while larger integers will be rounded to the nearest multiple of some power of 2.

Note that this isn't specific to Swift. This floating-point format is a standard format used in almost every programming language. Here's the output I get from LLDB with plain C:

If you need higher precision, use a Double. Double precision floats use 64 bits of memory, and have higher precision.

Tragacanth answered 15/5, 2015 at 13:32 Comment(0)
S
3

A 32bit floating point number can hold only about 7-9 significant digits. If you need more digits, the precision will be lost.

See How many significant digits have floats and doubles in java? for the calculations.

In Swift, you can use Double which can hold more digits (64bit float) or NSDecimalNumber which holds number in decimal format and doesn't lose precision so it is especially good for financial calculations. However, the performance is much worse.

Smoot answered 15/5, 2015 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.