Skip to content

float with inclusive tolerance? #1046

@JRaspass

Description

@JRaspass

While converting a couple of Test::Deep tests to Test2::V0 at $work I noticed that T2's float isn't quite a drop in replacement for Deep's num when tolerance is passed to both.

Test::Deep's check is inclusive:

abs($got - $self->{val}) <= $tolerance

https://metacpan.org/release/RJBS/Test-Deep-1.205/source/lib/Test/Deep/Number.pm#L31

Whereas Test2's is exclusive:

$equal = 1 if
  $got > $input - $tolerance &&
  $got < $input + $tolerance;

https://metacpan.org/release/EXODIST/Test-Simple-1.302214/source/lib/Test2/Compare/Float.pm#L93

My current workaround is to apply a tiny fudge factor to the tolerance in the test file with float( $exp, tolerance => $tol + 1e-8 ) but this is less than ideal as it shows up in the diagnostics and is generally harder to read.

I wonder if it would make sense to have either an inclusive flag or inclusive_tolerance arg to float?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions