.... but swapping entire bytes ...

In addition to the endianness issue, there's also the "precision" issue that can trip one up when print()ing NV values.
On a perl whose nvtype is double:
D:\>perl -le "print unpack 'd', pack 'd', sqrt 2;" 1.4142135623731
On a perl whose nvtype is the 80-bit extended precision long double:
D:\>perl -le "print unpack 'd', pack 'd', sqrt 2;" 1.41421356237309515
On a perl whose nvtype is either the IEEE 754 long double or the __float128:
D:\>perl -le "print unpack 'd', pack 'd', sqrt 2;" 1.41421356237309514547462185873883
You might think that the differences in those 3 one liners occur because pack 'd', sqrt 2 has returned different bytes in each of those 3 one liners, but that's not so.
In each of those 3 one-liners pack 'd', sqrt 2 returned exactly the same byte.
So, it must be that the unpacking returned different values ? Wrong again.
It's the way that perl's print() function conceals the actual value returned by unpack 'd', pack 'd', sqrt 2 that accounts for the anomalies.

Perl is bullshitting in all 3 cases (and knows it).
On a perl whose nvtype is double:
D:\>perl -le "print 'WTF' if (unpack 'd', pack 'd', sqrt 2) != 1.41421 +35623731;" WTF
On a perl whose nvtype is the 80-bit extended precision long double:
D:\>perl -le "print 'WTF' if (unpack 'd', pack 'd', sqrt 2) != 1.41421 +356237309515;" WTF
On a perl whose nvtype is either the IEEE 754 long double or the __float128:
D:\>perl -le "print 'WTF' if (unpack 'd', pack 'd', sqrt 2) != 1.41421 +356237309514547462185873883;" WTF
3 different renditions of the same value - none of them accurate.
I recall a quote (probably from Larry) from a while ago, along the lines of "perl makes the hard things easy".
When it comes to certain aspects of the way perl deals with NVs, it's more a case of "perl makes the easy things hard".
I think it's ridiculous and, although I'm scathing of it, I'm also used to it and sort of like the way that it keeps me on my toes ;-)

So ... for a given $byte, what one expects to be returned by unpack "d", $byte can depend not only upon machine endianness, but also on NV type.

Cheers,
Rob

In reply to Re^6: Converting bytes to floats by syphilis
in thread Converting bytes to floats by Anonymous Monk

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.