let highDouble = 1.7976931348623e+308 // Just under Double.greatestFiniteMagnitude
print(highDouble) // 1.7976931348623e+308
let highDecimal = Decimal(highDouble)
print(highDecimal) // 17976931348623005696000000000000000000000000000000000
这不是我输入的内容。为清楚起见,如果我将其带回Double
:
let newHighDouble = Double(exactly: highDecimal as NSNumber)!
print(newHighDouble) // 1.7976931348623e+52
所以 308 的震级减少到只有 52!这里发生了什么?我以为Decimal
可以存储非常大的值,但似乎它甚至无法存储什么Double
罐头!
短片:Double(exactly: Decimal(1.7976931348623e+308) as NSNumber)!