Skip to content

[SR-491] Double#description is not roundtrip-safe #43108

Closed
@swift-ci

Description

@swift-ci
Previous ID SR-491
Radar None
Original Reporter dankogai (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @tbkka
Priority Medium

md5: 1c1d9ebdc5e0745501c97e00c87b6dc7

duplicates:

Issue 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.

Metadata

Metadata

Assignees

Labels

CustomStringConvertibleArea → standard library: The `CustomStringConvertible` protocolDoubleArea → standard library: The `Double` typebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrellaunexpected behaviorBug: Unexpected behavior or incorrect output

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions