Skip to content

Feature request: Compare floating numbers using BeEquivalentTo based on a given approximation #58

@souhailelk

Description

@souhailelk

We would like to add a new feature while comparing two json objects containing floating numbers as values. We would like to compare the floating numbers with some margin of error.

Let’s take an example:
jsonA and jsonB two json objects that contain as values floating numbers with 9 decimal values.

image

Thus, this unit test fails because json1 and jsonB are differents at the ninth decimal value.
We would like to be able to consider jsonA and JsonB equal if their floating numbers values are equal at the eighth decimal value.

So, we want to have a feature that allows us to control the approximate equality of floating numbers.
Like for instance something like this for absolute tolerance:

jsonA.Should().BeEquivalentTo(jsonB, options => options.Using<double>(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-8, false)).WhenTypeIs<double>());

And for relative tolerance:

jsonA.Should().BeEquivalentTo(jsonB, options => options.Using<double>(d => d.Subject.Should().BeApproximately(d.Expectation, 1e-8, true)).WhenTypeIs<double>());

note : We will be happy to contribute adding this feature by a pull request 😀.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions