Skip to content

Commit d6d6f4f

Browse files
committed
Merge pull request #1922 from rintaro/float-tostring
[stdlib] Return constant for infinity and NaN to string conversion
2 parents 51df010 + 4cddc52 commit d6d6f4f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

stdlib/public/core/Runtime.swift.gyb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ func _float${bits}ToStringImpl(
361361

362362
@warn_unused_result
363363
func _float${bits}ToString(value: Float${bits}, debug: Bool) -> String {
364+
365+
% # FIXME: support Float80
366+
% if bits != 80:
367+
if value.isInfinite {
368+
return value.isSignMinus ? "-inf" : "inf"
369+
}
370+
if value.isNaN {
371+
return "nan"
372+
}
373+
% end
374+
364375
_sanityCheck(sizeof(_Buffer32.self) == 32)
365376
_sanityCheck(sizeof(_Buffer72.self) == 72)
366377

test/1_stdlib/PrintFloat.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
// RUN: %target-run %t/main.out
55
// RUN: %target-run %t/main.out --locale ru_RU.UTF-8
66
// REQUIRES: executable_test
7-
// XFAIL: linux
87

98
import StdlibUnittest
10-
import Darwin
9+
#if os(Linux) || os(FreeBSD)
10+
import Glibc
11+
#else
12+
import Darwin
13+
#endif
1114
import PrintTestTypes
1215

1316

@@ -52,6 +55,7 @@ PrintTests.test("Printable") {
5255
expectPrinted("inf", Float.infinity)
5356
expectPrinted("-inf", -Float.infinity)
5457
expectPrinted("nan", Float.nan)
58+
expectPrinted("nan", -Float.nan)
5559
expectPrinted("0.0", asFloat32(0.0))
5660
expectPrinted("1.0", asFloat32(1.0))
5761
expectPrinted("-1.0", asFloat32(-1.0))
@@ -61,6 +65,7 @@ PrintTests.test("Printable") {
6165
expectPrinted("inf", Double.infinity)
6266
expectPrinted("-inf", -Double.infinity)
6367
expectPrinted("nan", Double.nan)
68+
expectPrinted("nan", -Double.nan)
6469
expectPrinted("0.0", asFloat64(0.0))
6570
expectPrinted("1.0", asFloat64(1.0))
6671
expectPrinted("-1.0", asFloat64(-1.0))

test/1_stdlib/tgmath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ MathTests.test("Unary functions") {
130130
expectEqual("tan 0.100334672085451 0.100335 tan", print3("tan", d1, f1, g1))
131131

132132
(d1, f1, g1) = (acosh(dx), acosh(fx), acosh(gx))
133-
expectEqual("acosh true true acosh", print3("acosh", d1.isNaN, f1.isNaN, g1.isNaN))
133+
expectEqual("acosh nan nan acosh", print3("acosh", d1, f1, g1))
134134

135135
(d1, f1, g1) = (asinh(dx), asinh(fx), asinh(gx))
136136
expectEqual("asinh 0.0998340788992076 0.0998341 asinh",

0 commit comments

Comments
 (0)