0

In the Swift REPL:

$ swift
Welcome to Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42). Type :help for assistance.
  1> Double.greatestFiniteMagnitude == (Double.greatestFiniteMagnitude - Double(1))
$R0: Bool = true
  2>  

The same is true for Float32 (and I would assume the same for other similar types)

I've read this and various other pages about the subject of floating point inaccuracy, but they seem to all be talking about numbers after the decimal point being inaccurate.

My questions are:

  1. How is it possible that you cannot store a number that is less than your alleged maximum value?
  2. Does this start happening after a certain number?
  3. If only some numbers are not representable, can they be predicted? How many individual values can be stored by a Double or a Float32?
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59
  • https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Precision_limits_on_integer_values – billykun Apr 26 '18 at 06:52
  • 4
    `Double.greatestFiniteMagnitude` is approx. 1.7976931348623157e+308. The next smaller representable number is `Double.greatestFiniteMagnitude.nextDown`, which is approx 1.7976931348623155e+308. – Representing `Double.greatestFiniteMagnitude - 1.0` exactly would require a precision of about 308 decimal digits, the actual precision is about 16 decimal digits. So this is exactly the same issue as in the thread that you linked to, or as described in [Is floating point math broken?](https://stackoverflow.com/q/588004) – Martin R Apr 26 '18 at 07:08

0 Answers0