2

I've come across an error when converting Int.max to a double then back to integer:

print(Int(Double(Int.max)))

and getting the following error:

Fatal error: Double value cannot be converted to Int because the result would be greater than Int.max

I'd be appreciated if you can explain me the the reason of the above error. Is it something related to floating point arithmetic?

Tested this on Swift 4.2.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • I think above 2^53 you won't be ale to represent every integer value using a Double `Int(pow(2.0,54.0)+2) == Int(pow(2.0,54.0))` returns true. If you try `Int(pow(2.0,53.0)+2) == Int(pow(2.0,53.0))` returns false as expected – Leo Dabus Mar 09 '19 at 13:13

0 Answers0