-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-106] description returns a rounded value of Double #42728
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
Comments
Comment by Aaron Brager (JIRA) I'm just poking around in the Swift code base for the first time so I could be way off base here. I think the LLDB output uses the
@_transparent extension CGFloat : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return native.description
}
} I'm not sure what Indeed, only (lldb) p point.x
(CGFloat) $0 = 350.00000000000017
(lldb) p @(point.x)
(__NSCFNumber *) $1 = 0x00007fc7d04baa50 (double)350 So I think this may be an NSNumber issue, not a Swift issue. |
Comment by Wojtek Czekalski (JIRA) aaron (JIRA User) it's a good point. However, .native is of (lldb) po any_CGFloat.native.dynamicType
Swift.Double |
Comment by Wojtek Czekalski (JIRA) aaron (JIRA User) I tried to investigate it a bit but I didn't manage to find `Double` definition along with `description` implementation :/ |
Comment by Aaron Brager (JIRA) Ah, so is this implementation the relevant one? extension ${Self} : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return _float${bits}ToString(self)
}
} This is from extern "C" uint64_t swift_float64ToString(char *Buffer, size_t BufferLength,
double Value) {
return swift_floatingPointToString<double>(Buffer, BufferLength, Value,
"%0.*g");
} I don't know C++ very well but it looks like this |
Comment by Wojtek Czekalski (JIRA) I was clearly wrong with the label. |
Comment by Wojtek Czekalski (JIRA) aaron (JIRA User) yup, we found the issue! I put a breakpoint on #​0 0x000000010f860c10 in swift_float64ToString ()
#​1 0x000000010faee7d5 in Double.description.getter () When we look at the signature: extern "C" uint64_t swift_float64ToString(char *Buffer, size_t BufferLength,
double Value) So, the second argument is (lldb) p $arg2
(unsigned long) $0 = 32 And boom! |
Comment by Wojtek Czekalski (JIRA) #348 |
Useful background information: In #43071, Kevin Ballard provided links to some of the relevant algorithms, including Florian Loitsch' Grisu family of algorithms which provide good performance and accurate results. In particular, Grisu2 is simple, fast, and provides the optimal results 99% of the time. A recent paper by Andrysco, Jhala, and Lerner analyzes the shortcomings of Loitsch' Grisu2 and proposes a way to address them. Their Errol algorithm shows that Grisu-style algorithms can provide completely optimal formatting with very high speed. For the last year, I've been experimenting with another Grisu2 variant that uses a lot of the same ideas in a slightly different way. I'd like to replace the This would provide the following:
Of course, when used by the |
Please see #15474 |
Done with commit 97a934c. |
Attachment: Download
Environment
Additional Detail from JIRA
md5: 295299b47bb427386aa275afabcc8598
is duplicated by:
relates to:
1.1
swift-corelibs-foundation#4412Issue Description:
It looks like the string representation of a
Double
returned bydescription
is less precise than theDouble
itself. Look at the example below.The text was updated successfully, but these errors were encountered: