perchance has asked for the wisdom of the Perl Monks concerning the following question:


The documentation states that pack only supports real numbers in the native format.
What should I use, or rather, where should I look if I want to (un)pack cross-platform?

truly,
me

--- Endgame

Replies are listed 'Best First'.
Re: newbie: unpacking real numbers
by jmcnamara (Monsignor) on May 10, 2001 at 16:35 UTC

    Apart from the usual corncern about endianness, the main problem with packing/unpacking floating point numbers is that there is no universal representation of how they are stored. However, the majority of modern platforms use the IEEE specification.*

    Therefore, if you know in advance that you are writing/reading a IEEE floating point number in a particular endianness you could use a scheme like the following to guarantee some level of portability:
    #!/usr/bin/perl -w use strict; # Check if "pack" gives the required IEEE 64bit float # my $little_endian; # Some test data my $teststr = pack "d", 1.2345; my @hexdata =(0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F); my $number = pack "C8", @hexdata; if ($number eq $teststr) { # Little Endian $little_endian = 1; } elsif ($number eq reverse($teststr)) { # Big Endian $little_endian = 0; } else { # Give up, gracefully or otherwise. die "Required floating point format not supported on this platform +."; } # Now use this information to pack a double # my $double = 3.1415; my $packed_double; if ($little_endian) { $packed_double = pack "d", $double; } else { $packed_double = reverse pack "d", $double; }
    If you are sure of the IEEE issue you can can probably just determine the endianness using the Config module and pack or reverse pack as necessary:
    #!/usr/bin/perl -w use strict; use Config; my $double = 3.1415; my $packed_double; if ($Config{byteorder} == 1234) { $packed_double = pack "d", $double; } else { $packed_double = reverse pack "d", $double; }


    John.
    --

    * Does anyone have an example of modern hardware that doesn't support this format?

(tye)Re: newbie: unpacking real numbers
by tye (Sage) on May 10, 2001 at 21:28 UTC

    For portability, you can just skip pack and store your floating-point numbers as strings. With no extra work, you risk losing only about 3 bits of accuracy in the process (and you can lose that much with just a few arithmatic calculations so this might not be a problem at all).

    If those three bits are important, then you can write your floating-point numbers via sprintf "%.20e",$num and read them just the normal Perl way and you won't lose any bits of accuracy between platforms with the same accuracy of floating-point numbers (the best you can do). ["%.20e" should be exact for up-to-64-bit mantissas, which should be overkill for most platforms. "%.16e" is probably all you really need.]

            - tye (but my friends call me "Tye")
Re: newbie: unpacking real numbers
by larsen (Parson) on May 10, 2001 at 18:17 UTC
    If you qualify yourself as a newbie only because you can't use pack, probably you should consider this :).