Hello BrowserUk,

At first glance some will be surprised that it is the body of the equal (==) comparison that has been optimised away completely and the body of the unequal (!=) comparison that has been left behind...

Yes, I was, until I found it documented in perlop#Equality-Operators:

If your platform supports NaN 's (not-a-numbers) as numeric values, using them with "<=>" returns undef. NaN is not "<", "==", ">", "<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN returns true, as does NaN != anything else. If your platform doesn't support NaN 's then NaN is just a string with numeric value 0.

I guess that’s logical: if something is not a number then using it in a numeric comparison doesn’t make much sense.

But then I realised that there isn't a floating point constant that can be embedded as source code that will translate to SNAN. That's why I need to use pack/unpack to define it.

I note that on my system (Windows 8.1, Strawberry Perl 5.24.0, 64-bit, with USE_LONG_DOUBLE defined) the call to pack is also optimised away:

17:44 >perl -MO=Deparse -le"use constant SNaN => unpack'd', pack'Q',0x +7ff0000000 000001; print SNaN; if( SNaN != SNaN ) { print 'here'; }" BEGIN { $/ = "\n"; $\ = "\n"; } use constant ('SNaN', unpack('d', "\cA\000\000\000\000\000\360\177")); print unpack("F", pack("h*", "008000000000000cfff7000000000000")); do { print 'here' }; -e syntax OK 17:45 >perl -MO=Deparse -le"use constant SNaN => unpack'd', pack'Q', 0 +x7ff000000 0000001; print SNaN; if( SNaN == SNaN ) { print 'here'; }" BEGIN { $/ = "\n"; $\ = "\n"; } use constant ('SNaN', unpack('d', "\cA\000\000\000\000\000\360\177")); print unpack("F", pack("h*", "008000000000000cfff7000000000000")); '???'; -e syntax OK 17:46 >

Hope that’s of interest,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Clever optimisation: by design or luck? by Athanasius
in thread Clever optimisation: by design or luck? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.