[go: nahoru, domu]

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

fix(up, down, halfUp): fix handling of numbers close to 0 and rounding of integer results #711

Merged
merged 6 commits into from
Feb 19, 2023

Conversation

shasderias
Copy link
Contributor
@shasderias shasderias commented Jan 11, 2023

up/down - Functions should not round integer results.

up - up should only round positive numbers, 0 is not a positive number.

halfUp - halfUp was incorrectly rounding numbers in the [0,1) range due to the incorrect definition of isPositive.

Fixes #710
Fixes #713

Written by throwing tests at the wall and making them pass, would appreciate if someone could review their correctness.

In the rounding functions, is it necessary to handle the case where factor is negative? If so, the definition of isPositive in some of the functions may not handle this case.

From a big picture going to beta POV, some of these rounding functions are really obscure (e.g. halfOdd) and have little practical application. They also feel non-trivial to get right, do we really need all of them?

Also, rounding to -0 is correct but surprising, should something be done here?

…g of integer results

up/down - Functions should not round integer results.

up - up should only round positive numbers, 0 is not a positive number.

halfUp - halfUp was incorrectly rounding numbers in the [0,1) range due to the incorrect
definition of isPositive.

Fixes dinerojs#710
@vercel
Copy link
vercel bot commented Jan 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
dinerojs ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 19, 2023 at 4:20PM (UTC)

@codesandbox-ci
Copy link
codesandbox-ci bot commented Jan 11, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5b05403:

Sandbox Source
@dinero.js/example-cart-react Configuration
@dinero.js/example-cart-vue Configuration
@dinero.js/example-pricing-react Configuration
@dinero.js/example-starter Configuration
laughing-fog-nsqxfk Issue #710
gracious-thunder-jlghkd Issue #713

@johnhooks
Copy link
Contributor

@shasderias this PR fixes #713 too. I added failing tests to confirm the fix. I'll add a review of this PR too.

Copy link
Contributor
@johnhooks johnhooks left a comment

Choose a reason for hiding this comment

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

This is awesome work, you were very thorough.

I'm reading up on half even and half odd rounding, of the new tests, I had a little trouble wrapping my head around if they are being properly tested.

Comment on lines +27 to +28
(isPositive && !isLessThanHalf) ||
(!isPositive && isLessThanHalf)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these flipped? All the tests pass if I leave them as they were.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be consistent with preceding comment explaining what I'm doing.

I also reckon humans handle positive numbers better, so this can be viewed as: (1) addressing the problem, then (2) the common case then (3) the less common case.

I don't feel strongly about this, except that the comment should be amended if this change is reverted.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense to me, I was just checking that there was a reason. It wasn't apparent.

packages/core/src/divide/halfUp.ts Show resolved Hide resolved
@shasderias
Copy link
Contributor Author

This is awesome work, you were very thorough.

I'm reading up on half even and half odd rounding, of the new tests, I had a little trouble wrapping my head around if they are being properly tested.

My internal logic is:
(1) if the last digit is not 5, we're not dealing with half, round to closest integer
(2) 0 is even, 1 is odd, -1 is also odd

@johnhooks
Copy link
Contributor

@shasderias I understand it better now, when I took a look at the edits in context of the existing halfOdd and halfEven tests. The existing tests already checked for rounding toward odd or even, the tests you added were to make sure it handles rounding a single base unit in the expected way.

@sarahdayan
Copy link
Collaborator

Fantastic work @shasderias, thanks for the contirbution! I took the liberty to add a few commits instead of making suggestions, to avoid unnecessary back-and-forths. My changes are mostly refactors:

  • Move your comment to JSDoc, which has the nice sife-effect of documenting the rounding functions.
  • Use property tests instead of fixed test cases for when we want to work with arbitrary values that follow certain conditions.

@sarahdayan sarahdayan merged commit 6b30aa0 into dinerojs:main Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants