Skip to content

[SR-6671] Fix Decimal implementation of FloatingPoint constants #3067

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 1 commit into from
Aug 30, 2021

Conversation

xwu
Copy link
Contributor

@xwu xwu commented Aug 28, 2021

This PR is a reprise of #1386, which has long ago gone stale. There are several issues with Decimal implementations of FloatingPoint constants:

First, as reported in SR-6671, the leastNormalMagnitude and the leastNonzeroMagnitude actually aren't. This is corrected in both the overlay and in swift-corelibs, with amendments to the corresponding tests.

Second, as pointed out by @stephentyrone in #1386, the implementation of leastFiniteMagnitude is incorrect. (It is also not a constant required by FloatingPoint, but with API stability it's too late now to remove from Decimal's API. It is left to the code owners as a separate matter whether they wish to deprecate this API.) Apple's documentation of the API states that it is "the decimal that contains the smallest possible non-infinite magnitude for the underlying representation." Magnitude is never negative, and the correct value here is zero. In this PR we make this API an explicit synonym for .zero. This is corrected in both the overlay and in swift-corelibs, with amendments to the corresponding tests.

@xwu xwu requested a review from spevans August 28, 2021 19:35
@xwu
Copy link
Contributor Author

xwu commented Aug 28, 2021

@swift-ci test

@xwu xwu force-pushed the decimal-floatingpoint branch 2 times, most recently from 0aa7855 to d79cb1c Compare August 29, 2021 02:33
@xwu
Copy link
Contributor Author

xwu commented Aug 29, 2021

@swift-ci test

@xwu
Copy link
Contributor Author

xwu commented Aug 29, 2021

@swift-ci test macOS

@xwu xwu force-pushed the decimal-floatingpoint branch from d79cb1c to 3f3dab9 Compare August 29, 2021 15:07
@xwu
Copy link
Contributor Author

xwu commented Aug 29, 2021

@swift-ci test

@xwu xwu force-pushed the decimal-floatingpoint branch from 3f3dab9 to 87850da Compare August 29, 2021 15:10
@xwu
Copy link
Contributor Author

xwu commented Aug 29, 2021

@swift-ci test

@xwu xwu merged commit b9451fd into swiftlang:main Aug 30, 2021
@xwu xwu deleted the decimal-floatingpoint branch August 30, 2021 14:15
@stephentyrone
Copy link
Contributor

(Just back from vacation, sorry for delay in reading this)

I'm worried about changing the value of leastFiniteMagnitude introducing subtle bugs to existing code that "works" with the current (incorrect) value. My advice would be instead to deprecate the property while keeping its value as is.

@xwu
Copy link
Contributor Author

xwu commented Aug 30, 2021

@stephentyrone Yeah, I as well. On the other hand, the value of the property is currently documented in a way that emphasizes its "magnitudeness" and there may be current code subtly broken relying on that. I think this should be deprecated regardless with a good error message, but it's not clear to me which value of the property is best in its deprecated state.

@stephentyrone
Copy link
Contributor

stephentyrone commented Aug 30, 2021

Default assumption should be that already-compiled code is happy with the existing behavior (wrong though it may be), and we shouldn't break them. New code will get a deprecation warning, so we can catch them there. @spevans, how do you want to handle this?

(Note that leastFiniteMagnitude is not a value that comes from any protocol in stdlib or foundation, so there's little possibility of generic code misbehaving due to its nonsensical value.)

@xwu
Copy link
Contributor Author

xwu commented Aug 30, 2021

@stephentyrone See #3071.

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