-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-15132][SR-15134] Fix Decimal
implementation of significand APIs
#3068
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
Conversation
…ponent:significand:).
@swift-ci test |
@swift-ci test |
I agree your fix is correct in that it makes it compatible with the operation of the types conforming to
Do you think the documentation is assuming the I wonder if a better fix would be to trap on a negative I'd be interested to hear @stephentyrone thoughts on this. Also, at some point the documentation for https://developer.apple.com/documentation/foundation/decimal/2143030-init needs to be expanded since its single line summary could be interpreted as the |
@spevans The It's certainly a possible point of confusion and the documentation of the arguments could use at least a caveat (maybe something like, "The sign to use for the new value when However, the single line summary is an accurate description in that, when a user passes the |
Decimal
implementation of significand APIsDecimal
implementation of significand APIs
Note that I'm rolling in a fix for yet another bug in |
@swift-ci test |
…ent:significand:).
fc1f6fe
to
9fe6948
Compare
@swift-ci test |
This PR fixes
twothree newly discovered issues withDecimal
implementations of APIs that deliberately mimic those ofFloatingPoint
.First, as reported in SR-15132,
significand
shouldn't be negative, butDecimal
returns a negative "significand" for a negative value:Second and not reported in a separate bug, for types conforming to
FloatingPoint
, the initializerinit(sign:exponent:significand:)
returns a value where the sign is determined by the following notional calculation:In other words, the sign of the result is not determined by
sign
alone, but rather the result is negative if and only ifsign
andsignificand.sign
are of opposite signs. This is not respected byDecimal
:Third, now reported in SR-15134,
init(sign:exponent:significand:)
currently barfs when exponent is incremented out of range:Instead, the value should properly underflow or overflow (to NaN since
Decimal
has no representation of infinity), respectively, in line with the behavior of binary floating-point types:To fix this, delegate the work of increasing or decreasing the exponent to
NSDecimalMultiplyByPowerOf10
.These issues are addressed for both the overlay and the swift-corelibs implementation; tests are added.