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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.