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:
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.
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