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 precision issues with Money #268

Merged
merged 1 commit into from
Jun 21, 2017
Merged

Conversation

garyKeorkunian
Copy link
Contributor

Also temporarily reverted to Scalariform 1.5.1

Fixes #265

Copy link
Collaborator

@cquiroz cquiroz left a comment

Choose a reason for hiding this comment

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

LGTM

USD(5) - USD(2) should be(USD(3))
USD(5).minus(USD(2)) should be(USD(3))
}

it should "return proper result when dividing like currencies with no MoneyContext in scope" in {
it should "return a proper result with no additional precision loss when multiplying by a primitive numeric" in {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should make this a property based test?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a good suggestion in general for test, scala check could be appropriate for that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a Scalacheck test.

Copy link
Contributor Author

@garyKeorkunian garyKeorkunian Jun 16, 2017

Choose a reason for hiding this comment

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

So, the Scalachecks revealed that this issue can also exist with BigDecimal.

val x = BigDecimal(3)
val m = BigDecimal(2)

m / x + m / x = 1.3333333333333333333333333333333334
(m / x) * 2 = 1.333333333333333333333333333333333

Interestingly, that same calculation with Double works correctly

val x = 3d
val m = 2d

m / x + m / x = 1.3333333333333333
(m / x) * 2 = 1.3333333333333333

I can resolve this by using an approximation with a tolerance of USD(1e-32) to the comparison. Most use cases will require much less than 32 digits and will typically round to the nearest penny (or formattedDecimal for the Currency), so this is still a big improvement over the 16/17 digits we get with Double math.

The multiplication use case noted in the original issue does not have this problem, so no approximation was needed there.

I'll have an updated PR in a moment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's great, we should probably extend scalacheck's usage to other tests

Also temporarily reverted to Scalariform 1.5.1
@derekmorr derekmorr added this to the 1.4 milestone Jun 17, 2017
@derekmorr
Copy link
Collaborator

LGTM. Are we good to merge this?

@derekmorr derekmorr added the bug label Jun 17, 2017
@garyKeorkunian
Copy link
Contributor Author

Any thoughts on that Checks issue I mentioned?

@garyKeorkunian garyKeorkunian merged commit 720a6ba into master Jun 21, 2017
@garyKeorkunian garyKeorkunian deleted the wip-money-precision branch June 21, 2017 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants