Skip to content

Precision error when calculating small volumes #1199

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

Closed
mortenbock opened this issue Feb 10, 2023 · 2 comments
Closed

Precision error when calculating small volumes #1199

mortenbock opened this issue Feb 10, 2023 · 2 comments
Labels

Comments

@mortenbock
Copy link

Describe the bug
Given this unit test:

[Test]
public void Foo()
{
    Length length = Length.FromMillimeters(10);
    Length width = Length.FromMillimeters(10);
    Length height = Length.FromMillimeters(10);

    Volume volume = length * width * height;

    Assert.That(volume.CubicMillimeters, Is.EqualTo(1000));
}

The test fails with:

Expected: 1000
But was:  1000.0000000000001d

It seems that working with small units means that the precision of the calculations are not correct?

Using version 5.1.0

@mortenbock mortenbock added the bug label Feb 10, 2023
@angularsen
Copy link
Owner

Duplicate of #1193, please continue the discussion there.

@angularsen
Copy link
Owner

angularsen commented Feb 12, 2023

Actually, your question is not the exact same issue as 1193 since it does not involve units in the equality, so I will answer here.

This is generally due to double precision in arithmetic, you can't assume it will always be perfectly 1000. When comparing double values, you should always specify a max error tolerance. Most unit test frameworks provide this or you can write your own.

For example, FluentAssertions has value.Should().BeApproximately(3.14, 0.01);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants