1

I print two tuples in the Xcode playground. But I get a strange result, see the following code snippet and the print result:

Code Snippet:

let x : (Double,Double) = (1.2,1.3)
print(x)

let y : (Double,Double) = (1.2,3.1)
print(y)

Print Result:

(1.2, 1.3)
(1.2, 3.1000000000000001)

Why it prints the 3.1000000000000001 which is NOT the actual value (3.1) I set for the tuple?

In the meantime, the 1.3 is printed out correctly.

My Swift compiler version info:

Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Kevin Li
  • 2,258
  • 1
  • 9
  • 18
  • 1
    The linked answer is about math, but the floating point accuracy issue is basically the same. – vadian Mar 10 '17 at 07:23
  • See also http://stackoverflow.com/questions/40959254/why-are-doubles-printed-differently-in-dictionaries, which explains why `print(y)` and `print(y.1)` show the number 3.1 with different precision. – Martin R Mar 10 '17 at 07:54

0 Answers0