in reply to Re^2: Unexpected behaviour with PERL5OPT
in thread Unexpected behaviour with PERL5OPT

If you compare floats in a test, you should compare for "close enough", not ==. See float in Test2::V0​.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^4: Unexpected behaviour with PERL5OPT
by syphilis (Archbishop) on Oct 25, 2024 at 09:43 UTC
    If you compare floats in a test, you should compare for "close enough", not ==.

    If that's universally true, then Test::More should throw an exception whenever 2 NVs are compared for equivalence.
    Your assertion is not even generally true - though there are times when it may be deemed more appropriate to compare approximations.
    Anyway, I'm nearly a grown-up now, so I'll be the one who decides what and how I should and shouldn't compare.

    I've no objection that the following test fails:
    >perl -MPOSIX -MTest::More -le "cmp_ok(1.4/10, '==', 0.14, 'division') +; done_testing();" not ok 1 - division # Failed test 'division' # at -e line 1. # got: 0.14 # expected: 0.14 1..1 # Looks like you failed 1 test of 1.
    In fact it should fail and I expect it to fail.
    But I object to being told that the 2 values are not equivalent because they're identical. WTF??
    That is never going to be helpful information.

    With my patched Builder.pm (and Math::Ryu installed), the above one-liner produces:
    >perl -MPOSIX -MTest::More -le "cmp_ok(1.4/10, '==', 0.14, 'division') +; done_testing();" not ok 1 - division # Failed test 'division' # at -e line 1. # got: 0.13999999999999999 # expected: 0.14 1..1 # Looks like you failed 1 test of 1.
    which actually tells us what the 2 values were.
    It's a pretty rare scenario. The only recent example I have that turned up in the wild is from https://github.com/Perl/perl5/issues/22463:
    not ok 40 - tan(1) == -tan(-1) # Failed test 'tan(1) == -tan(-1)' # at ext/POSIX/t/math.t line 52. # got: 1.55740772465490223050697480745836 # expected: 1.55740772465490223050697480745836
    Usual practice seems to be that "-tan(-1)" is mostly calculated as "tan(1)", and that would mean that the test should pass by tautology (as it usually does).
    It was surmised that a different process, which would allow for a small discrepancy, was being followed in this particular failure.
    Again, my objection is not that it failed, but that it's claimed that the 2 values were deemed non-equivalent yet identical.
    If the "surmising" was correct, then my patched approach would have presented:
    not ok 40 - tan(1) == -tan(-1) # Failed test 'tan(1) == -tan(-1)' # at ext/POSIX/t/math.t line 52. # got: 1.55740772465490223050697480745836023 # expected: 1.55740772465490223050697480745836081
    I'll post again to this thread with a link to the Test::Simple PR once I've submitted it.

    Cheers,
    Rob
      You are right (and in fact, I expected such an answer, especially from you).

      Interestingly, the error exists in Test2::V0​, too, where the output of failed tests is "more friendly":

      perl -wE 'use Test2::V0; plan 1;cmp_ok 1.4/10, "==", 0.14, "same"' # Seeded srand with seed '20241025' from local date. 1..1 not ok 1 - same # Failed test 'same' # at -e line 1. # +------+----+-------+ # | GOT | OP | CHECK | # +------+----+-------+ # | 0.14 | == | 0.14 | # +------+----+-------+

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Interestingly, the error exists in Test2::V0​, too ...

        Oh, Christ .... is there no end to the madness !!

        ;-)

        My patch to Test/Builder.pm doesn't permeate through to Test2::V0.
        (Something for 'Ron.)

        Cheers,
        Rob