When parsing floating-point from String, underflow to 0, overflow to infinity (#34339)

Previously, overflow and underflow both caused this to return `nil`, which causes several problems:
* It does not distinguish between a large but valid input and a malformed input.  `Float("3.402824e+38")` is perfectly well-formed but returns nil
* It differs from how the compiler handles literals.  As a result, `Float(3.402824e+38)` is very different from `Float("3.402824e+38")`
* It's inconsistent with Foundation Scanner()
* It's inconsistent with other programming languages

This is exactly the same as #25313

Fixes rdar://problem/36990878
This commit is contained in:
tbkka
2020-10-19 06:44:57 -07:00
committed by GitHub
parent f55e65177f
commit 5d30503894
3 changed files with 9 additions and 22 deletions

View File

@@ -181,17 +181,11 @@ tests.test("${Self}/Basics") {
expectNil(${Self}("0 ")) // Trailing whitespace
expectNil(${Self}("\u{1D7FF}")) // MATHEMATICAL MONOSPACE DIGIT NINE
// Overflow and underflow. Interleave with other checks to make
// sure we're not abusing errno
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("2e99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("2e-99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("-2e99999999999999"))
expectEqual(0.0, ${Self}("0"))
expectNil(${Self}("-2e-99999999999999"))
expectEqual(0.0, ${Self}("0"))
// Overflow to infinity, underflow to zero.
expectEqual(.infinity, ${Self}("2e99999999999999"))
expectEqual(0.0, ${Self}("2e-99999999999999"))
expectEqual(-.infinity, ${Self}("-2e99999999999999"))
expectEqual(0.0, ${Self}("-2e-99999999999999"))
}
% if Self == 'Float80':