Skip to content

Checking expect.toBeCloseTo uses absolute difference, there should be a expect.toBeRelativelyCloseTo for checking ratios are close to 1 #10957

Description

@AncientSwordRage

Clear and concise description of the problem

I would expect expect(1.0000011e100).toBeCloseTo(1.0000012e100, 3) to pass, but the check is for absolute difference, not relative (not a bug, as this seems intentional):

Math.abs(expected - received) < 0.005 (that is, 10 ** -2 / 2)

But for large numbers in scientific notation, having a relative difference would be handy, so I don't need to do either:

const actual = 1.0000011e100;
const expected = 1.0000012e100;
expect(actual/1e100).toBeCloseTo(expected/1e100, 3);

or

const actual = 1.0000011e100;
const expected = 1.0000012e100;
expect(actual/expected).toBeCloseTo(1, 3);

Suggested solution

Implement a new method that uses similar method to the above workarounds.

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions