Skip to content
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

Avoid crashing for RETDecimal.max().formated(...) #1044

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

CyonAlexRDX
Copy link
Contributor

Jira ticket: paste link here

Complements / depends on / relates to #link to other PR

Description

This PR solves / fixes / adds / includes functionality 1 and functionality 2.

Notes

Additional notes.

How to test

Happy path (or test variation 1)

  1. Step 1
  2. Step 2
  3. Verify

Unhappy path (or test variation 2)

  1. Step 1
  2. Step 2
  3. Verify

Screenshot

Screen 1 Before Screen 1 After
paste link here paste link here

Video

paste link here

PR submission checklist

  • I have tested account to account transfer flow and have confirmed that it works


try doTest(RETDecimal.max(), expected: "3.138e39")
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a new test, which fails on main branch, but not anymore.

// `toZero` to avoid overflow.
let safeBase = try! base.round(
decimalPlaces: Int32(decimalPlaces),
roundingMode: .toZero
Copy link
Contributor

Choose a reason for hiding this comment

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

N.B. .toNearestMidpointTowardZero will not work either, must be .toZero.

@@ -246,7 +246,23 @@ extension RETDecimal {
let integerCount = UInt(Swift.max(digits.count - RETDecimal.scale, 1))
if integerCount > totalPlaces {
let scale = RETDecimal(exponent: integerCount - totalPlaces)
return (self / scale).rounded(decimalPlaces: 0) * scale
let base = self / scale
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say that we don't want to protect ourselves against any kind of error here, only the specific case of max, no? We could just

guard self != max else {
  // return the number rounded towards zero
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No because if the value is close to max we don't want to fail either. Hmm

@GhenadieVP GhenadieVP merged commit 1e8eff3 into main Mar 18, 2024
6 checks passed
@GhenadieVP GhenadieVP deleted the decimal_crash_fixes branch March 18, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants