Skip to content

[SR-15156] Another fix for Decimal(sign:exponent:significand:). #3074

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
Sep 4, 2021

Conversation

xwu
Copy link
Contributor

@xwu xwu commented Sep 4, 2021

This issue has been present all along, and my previous fix in #3068 didn't address it because I didn't know of the problem--and (perhaps unsurprisingly) reproduced the same problem despite a totally new implementation of the initializer. Consider the following:

import Foundation

let x = Double.greatestFiniteMagnitude
Double(sign: .plus, exponent: 10, significand: x)
// +inf
Double(sign: .plus, exponent: .max, significand: x)
// +inf

let y = Decimal.greatestFiniteMagnitude
Decimal(sign: .plus, exponent: 10, significand: y)
// nan (correct, since `Decimal` has no representation of infinity)
Decimal(sign: .plus, exponent: .max, significand: y)
// runtime error (!)

This is because the implementation of Decimal(sign:exponent:significand:) converts between Int and an integer type of smaller bit width (but still plenty wide enough to represent more than twice the largest possible exponent) using a trapping initializer instead of clamping.

The present PR fixes that oversight now (in both the corelibs and overlay implementations) and adds tests.

@xwu
Copy link
Contributor Author

xwu commented Sep 4, 2021

@swift-ci test

@xwu xwu merged commit ac4445d into swiftlang:main Sep 4, 2021
@xwu xwu deleted the decimal-significand-again branch September 4, 2021 22:54
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.

2 participants