Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
swift-ci opened this issue Jan 7, 2016 · 2 comments
Closed

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

swift-ci opened this issue Jan 7, 2016 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. CustomStringConvertible Area → standard library: The `CustomStringConvertible` protocol Double Area → standard library: The `Double` type duplicate Resolution: Duplicates another issue standard library Area: Standard library umbrella unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 7, 2016

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.

@tbkka
Copy link
Contributor

tbkka commented Mar 8, 2018

This is the same issue reported in #43108. I'm working on a fix for that issue and will discuss it further there.

@tbkka
Copy link
Contributor

tbkka commented Apr 2, 2018

Done with commit 97a934c

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added duplicate Resolution: Duplicates another issue Double Area → standard library: The `Double` type CustomStringConvertible Area → standard library: The `CustomStringConvertible` protocol unexpected behavior Bug: Unexpected behavior or incorrect output labels May 26, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. CustomStringConvertible Area → standard library: The `CustomStringConvertible` protocol Double Area → standard library: The `Double` type duplicate Resolution: Duplicates another issue standard library Area: Standard library umbrella unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

No branches or pull requests

3 participants