-
Notifications
You must be signed in to change notification settings - Fork 915
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
[Java] Choose The Correct RoundingMode For Checking Decimal OutOfBounds #14731
[Java] Choose The Correct RoundingMode For Checking Decimal OutOfBounds #14731
Conversation
Signed-off-by: Raza Jafri <[email protected]> Added tests
20c6862
to
160a3c5
Compare
/ok to test |
/ok to test |
// In ex:1 If we rounded down the rhs 100.19 would become 100.1, and now 100.1 is not < 100.1 | ||
// In ex:2 If we rounded up the rhs -100.19 would become -100.2, and now -100.2 is not < -100.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what are the correct rounding for these examples? I'm confused. Please be explicit.
|
||
// First we have to round the scalar (rhs) to the same scale as lhs. | ||
// Because this is a greater-than, and it is rhs that we are rounding, we will round towards 0 (DOWN) in case rhs is | ||
// positive and away from 0 (UP) if rhs is negative to make sure we always return the correct value. | ||
// For example: | ||
// ex:1 100.2 > 100.19 | ||
// ex:2 -100.1 > -100.19 | ||
// In ex:1 If we rounded up the rhs 100.19 would become 100.2, and now 100.2 is not > 100.2 | ||
// In ex:2 If we rounded down the rhs -100.19 would become -100.1, and now -100.1 is not > -100.1 | ||
BigDecimal roundedRhs = rhs.setScale(-cvScale, rhs.signum() > 0 ? BigDecimal.ROUND_DOWN : BigDecimal.ROUND_UP); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just repeated of the code above. We should not copy-paste code around. If there is the same code used multiple times, let's extract it into a function and call it multiple times instead.
Co-authored-by: Jason Lowe <[email protected]>
/ok to test |
/ok to test |
/ok to test |
/merge |
Description
This PR fixes an error in the
outOfBounds
method in which theRoundingMode
was selected based on positive values only. The RHS should be rounded towards positive infinity (ROUND_CEILING) for the lower bound and towards negative infinity (ROUND_FLOOR) for the upper boundcloses #14732
Checklist