Why get a wrong answer when I put + 100
into UInt64()
?
当我把+ 100放入UInt64()时为什么得到错误的答案?
1 个解决方案
#1
I fear you are hitting the float
precision limit:
我担心你达到浮动精度限制:
powf(2, 35) + 100 == powf(2, 35) // true
Try the double version:
试试双版本:
let testLength: UInt64 = UInt64(pow(2, 35) + 100.0)
As a role of thumb it might help having a look at what the playground shows you for:
作为拇指的角色,它可能有助于了解游乐场向您展示的内容:
powf(2, 35) // 3.435974e+10
pow(2.0, 35.0) // 34359738368
In the float version your 100 is out
在浮动版本中你的100出局了
#1
I fear you are hitting the float
precision limit:
我担心你达到浮动精度限制:
powf(2, 35) + 100 == powf(2, 35) // true
Try the double version:
试试双版本:
let testLength: UInt64 = UInt64(pow(2, 35) + 100.0)
As a role of thumb it might help having a look at what the playground shows you for:
作为拇指的角色,它可能有助于了解游乐场向您展示的内容:
powf(2, 35) // 3.435974e+10
pow(2.0, 35.0) // 34359738368
In the float version your 100 is out
在浮动版本中你的100出局了