Skip to content

When parsing floating-point from String, underflow to 0, overflow to ∞ #34339

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

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Oct 16, 2020

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

Note: This is exactly the same as #25313 by @stephentyrone

Fixes rdar://problem/36990878

…infinity

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 swiftlang#25313

Fixes rdar://problem/36990878
@tbkka tbkka requested a review from stephentyrone October 16, 2020 22:56
@tbkka
Copy link
Contributor Author

tbkka commented Oct 16, 2020

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a655122

tbkka added a commit to tbkka/swift-corelibs-foundation that referenced this pull request Oct 17, 2020
The value `2.7976931348623158e+308` is a perfectly valid JSON number
and should be accepted.  This test is only passing today because the Swift
standard library's `Double(_ String:)` initializer has a bug that
rejected numbers larger than the maximum Double value.

But we're getting ready to fix that bug in the Swift standard library,
(swiftlang/swift#34339) which will break this test when it lands.
@tbkka
Copy link
Contributor Author

tbkka commented Oct 17, 2020

Right now, this is failing only on an invalid JSON parsing test in swift-corelibs-foundation. I've filed swiftlang/swift-corelibs-foundation#2903 to remove the invalid test, at which point this should be fine.

Copy link
Contributor

@stephentyrone stephentyrone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for dusting this off, Tim!

@stephentyrone
Copy link
Contributor

@swift-ci please test Linux

@stephentyrone stephentyrone merged commit 5d30503 into swiftlang:main Oct 19, 2020
spevans added a commit to spevans/swift-corelibs-foundation that referenced this pull request Mar 8, 2021
- The behaviour of the floating point string parsing for Float and
  Double was changed to return +/-infinity instead of nil on overflow.
  (swiftlang/swift#34339)

- Add an isFinite check to ensure that the result of a floating point
  conversion still fits in the type, matching current behaviour.
@tbkka tbkka deleted the tbkka/floating-point-parsing-overunder branch August 1, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants