Uploaded image for project: 'Swift'
  1. Swift
  2. SR-491

Double#description is not roundtrip-safe

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Standard Library
    • Labels:
      None

      Description

      In order to stringily a double roundtrip-safe, we need up to 17 decimal digits. But Double#description
      of current Swift (on all platforms) gives us one digit short.

      import Foundation
      
      let log2   =    log(2.0)
      let log2Sd =    log2.description
      let log2Sp17g = String(format:"%.17g",log2)
      let log2Spa =   String(format:"%a",log2)
      
      Double(log2Sd)    == log2   // false
      Double(log2Sp17g) == log2   // true
      Double(log2Spa)   == log2   // true
      

      http://swiftlang.ng.bluemix.net/#/repl/937bf49dd2f110c6f4c0fd942de051d734b3e051887b1c6b762c39e2c7956146

      As you see in the attached screenshot, Playground is okay. So is REPL.

      Perl 5 and Python 2 have the same symptom:

      While most other modern languages do not:

      IMHO this is rather an undocumented feature than a bug but with JSON so ubiquitous and all of its numbers are doubles, we should make Double#description roundtrip-safe.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              tbkka Tim Kientzle
              Reporter:
              dankogai Dan Kogai
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: