I'm facing a weird issue with the pow()
. Please check the following function
let r = 8.5/1200
let n = ((5*12.0)+1)
let pp = (pow(Float(1 + r), Float(n)))
debugPrint(pp) // 1.53811562
let pq = (pow((1 + r), n))
debugPrint(pq) // 1.5381189771003985
Here pow(Float, Float)
function return max. 8 fractional number for Float
and pow(Double , Double)
returns value with 16 fractional number for Double
.
Why it is so could anyone explain it?
And is it possible to get upto 16 fractional number for Float
value?
Note: You can copy and paste it in playground to see result.
Thanks in advance.
Float
uses an IEEE 754 four byte single precision number. 24 bits are assigned to the significand and 8 bits to the exponent. With 24 bits of precision, it can represent a little over 7 decimal digits of precision
Double
- which is the default floating point format for Swift uses an IEEE double precision eight byte number. The significand uses 53 bits and the exponent 11 bits. With 53 bits of precision, it can represent just under 16 decimal digits of precision.
The reason why pq
is printed with 16 digits is that by default the compiler has inferred the argument type to be Double
.
And is it possible to get upto 16 fractional number for Float value?
No. There aren't enough bits of precision.