1

I noticed that some floating points converted differently. This question helps me about floating points however still don't know why this happens? I added two screenshots from debug mode about the sample code. Example values : 7.37 and 9.37. Encountered it in swift and surely swift uses IEEE 754 floating point standard Pls explain how this happens? How conversion ended differently ?

if let text = textField.text {
  if let number = formatter.number(from: text) {
     return Double(number)
  }
  return nil
}

enter image description here

enter image description here

Community
  • 1
  • 1
yilmazburk
  • 907
  • 9
  • 17
  • 1
    Floating point numbers have *finite precision* - what part of the answer you linked to is not clear ? – Paul R Jan 04 '17 at 08:50
  • Compare http://stackoverflow.com/a/39777334/1187415: Depending on how the number is printed, `description` or `debugDescription` is used, and these two methods use different precision for the conversion to a string. – Martin R Jan 04 '17 at 08:54
  • Linked Q is clear but I don't understand why 7.37 is not converged like 9.37? I mean 7.37 converges from right (7.3700000..1) but 9.37 converges from left (9.36999..2) . @martin-r thanks for ur answer but still is not clear for me. I did not choose any different precision for conversion as u explain in ur comment – yilmazburk Jan 04 '17 at 09:13
  • 1
    Neither 7.37 nor 9.37 can be represented *exactly* as a binary floating point number. The actual number stored can be slightly smaller *or* larger. – Martin R Jan 04 '17 at 09:35
  • 3
    The mantissa of the floating point number is represented by a finite number of bits. Since 9 takes 4 bits to represent 0x1001 and 7 takes 3 bits to represent 0x111, there is 1 fewer bit left to represent `.37` so the approximation will not be as accurate if the lost bit was a 1. – vacawama Jan 04 '17 at 10:51

1 Answers1

1

Double floating point numbers are stored in base-2, and cannot represent all decimals exactly.

In this case, 7.37 and 9.37 are rounded to the nearest floating point numbers which are 7.37000000000000010658141036401502788066864013671875 and 9.3699999999999992184029906638897955417633056640625, respectively.

Of course, such decimal representations are too unwieldy for general use, so programming languages typically print shorter approximate decimal representations. Two popular choices are

  1. The shortest string that will be correctly rounded to the original number (which in this case are 7.37 and 9.37, respectively).
  2. Rounded 17 significant digits, which is guaranteed to give the correct value when converting back to binary.

These appear to correspond to the 2 debug output values that you are seeing.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50