mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user