Skip to content

Fix precision of float to string to max significant decimals #348

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

Merged
merged 6 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stdlib/public/core/FloatingPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ public struct ${Self} {
extension ${Self} : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return _float${bits}ToString(self)
return _float${bits}ToString(self, debug: false)
}
}

extension ${Self} : CustomDebugStringConvertible {
/// A textual representation of `self`.
public var debugDescription: String {
return _float${bits}ToString(self, debug: true)
}
}

Expand Down
7 changes: 4 additions & 3 deletions stdlib/public/core/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,20 @@ struct _Buffer72 {
@_silgen_name("swift_float${bits}ToString")
func _float${bits}ToStringImpl(
buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
_ bufferLength: UInt, _ value: Float${bits}
_ bufferLength: UInt, _ value: Float${bits},
_ debug: Bool
) -> UInt

@warn_unused_result
func _float${bits}ToString(value: Float${bits}) -> String {
func _float${bits}ToString(value: Float${bits}, debug: Bool) -> String {
_sanityCheck(sizeof(_Buffer32.self) == 32)
_sanityCheck(sizeof(_Buffer72.self) == 72)

var buffer = _Buffer32()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value)
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value, debug)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
Expand Down
22 changes: 13 additions & 9 deletions stdlib/public/stubs/Stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,16 @@ static int swift_snprintf_l(char *Str, size_t StrSize, locale_t Locale,

template <typename T>
static uint64_t swift_floatingPointToString(char *Buffer, size_t BufferLength,
T Value, const char *Format) {
T Value, const char *Format,
bool Debug) {
if (BufferLength < 32)
swift::crash("swift_floatingPointToString: insufficient buffer size");

const int Precision = std::numeric_limits<T>::digits10;

int Precision = std::numeric_limits<T>::digits10;
if (Debug) {
Precision = std::numeric_limits<T>::max_digits10;
}

// Pass a null locale to use the C locale.
int i = swift_snprintf_l(Buffer, BufferLength, /*locale=*/nullptr, Format,
Precision, Value);
Expand All @@ -170,21 +174,21 @@ static uint64_t swift_floatingPointToString(char *Buffer, size_t BufferLength,
}

extern "C" uint64_t swift_float32ToString(char *Buffer, size_t BufferLength,
float Value) {
float Value, bool Debug) {
return swift_floatingPointToString<float>(Buffer, BufferLength, Value,
"%0.*g");
"%0.*g", Debug);
}

extern "C" uint64_t swift_float64ToString(char *Buffer, size_t BufferLength,
double Value) {
double Value, bool Debug) {
return swift_floatingPointToString<double>(Buffer, BufferLength, Value,
"%0.*g");
"%0.*g", Debug);
}

extern "C" uint64_t swift_float80ToString(char *Buffer, size_t BufferLength,
long double Value) {
long double Value, bool Debug) {
return swift_floatingPointToString<long double>(Buffer, BufferLength, Value,
"%0.*Lg");
"%0.*Lg", Debug);
}

/// \param[out] LinePtr Replaced with the pointer to the malloc()-allocated
Expand Down
8 changes: 4 additions & 4 deletions test/1_stdlib/Interval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ IntervalTestSuite.test("CustomStringConvertible/CustomDebugStringConvertible") {
expectEqual("0.0...0.1", String(X(0.0)...X(0.1)))

expectEqual(
"HalfOpenInterval(X(0.0)..<X(0.1))",
String(reflecting: HalfOpenInterval(X(0.0)..<X(0.1))))
"HalfOpenInterval(X(0.0)..<X(0.5))",
String(reflecting: HalfOpenInterval(X(0.0)..<X(0.5))))
expectEqual(
"ClosedInterval(X(0.0)...X(0.1))",
String(reflecting: ClosedInterval(X(0.0)...X(0.1))))
"ClosedInterval(X(0.0)...X(0.5))",
String(reflecting: ClosedInterval(X(0.0)...X(0.5))))
}

IntervalTestSuite.test("rdar12016900") {
Expand Down
19 changes: 18 additions & 1 deletion test/1_stdlib/Print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func debugPrintedIs<T>(
) {
var actual = ""
debugPrint(object, terminator: "", toStream: &actual)
if expected1 != actual && (expected2 != nil && expected2! != actual) {
if expected1 != actual && (expected2 == nil || expected2! != actual) {
print(
"check failed at \(file), line \(line)",
"expected: \"\(expected1)\" or \"\(expected2)\"",
Expand Down Expand Up @@ -409,6 +409,23 @@ func test_FloatingPointPrinting() {
printedIs(asFloat80(0.0000000000000000125), "1.25e-17")
#endif

debugPrintedIs(asFloat32(1.1), "1.10000002")
debugPrintedIs(asFloat32(125000000000000000.0), "1.24999998e+17")
debugPrintedIs(asFloat32(1.25), "1.25")
debugPrintedIs(asFloat32(0.0000125), "1.24999997e-05")

debugPrintedIs(asFloat64(1.1), "1.1000000000000001")
debugPrintedIs(asFloat64(125000000000000000.0), "1.25e+17")
debugPrintedIs(asFloat64(1.25), "1.25")
debugPrintedIs(asFloat64(0.0000125), "1.2500000000000001e-05")

#if arch(i386) || arch(x86_64)
debugPrintedIs(asFloat80(1.1), "1.10000000000000000002")
debugPrintedIs(asFloat80(125000000000000000.0), "125000000000000000.0")
debugPrintedIs(asFloat80(1.25), "1.25")
debugPrintedIs(asFloat80(0.0000125), "1.25000000000000000001e-05")
#endif

print("test_FloatingPointPrinting done")
}
test_FloatingPointPrinting()
Expand Down
8 changes: 5 additions & 3 deletions test/Interpreter/SDK/objc_cast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,18 @@ if let strArr = objImplicitOpt as? [String] {
}

// Casting an array of numbers to different numbers.
// CHECK: Numbers-as-doubles cast produces [3.14159, 2.71828, 0.0]
obj = ([3.14159, 2.71828, 0] as [Double]) as AnyObject
// CHECK: Numbers-as-doubles cast produces [3.9375, 2.71828, 0.0]
obj = ([3.9375, 2.71828, 0] as [Double]) as AnyObject
if let doubleArr = obj as? [Double] {
print(sizeof(Double.self))
print("Numbers-as-doubles cast produces \(doubleArr)")
} else {
print("Numbers-as-doubles failed")
}

// CHECK: Numbers-as-floats cast produces [3.14159{{.*}}, 2.71828{{.*}}, 0.0]
// CHECK: Numbers-as-floats cast produces [3.9375, 2.71828{{.*}}, 0.0]
if let floatArr = obj as? [Float] {
print(sizeof(Float.self))
print("Numbers-as-floats cast produces \(floatArr)")
} else {
print("Numbers-as-floats failed")
Expand Down