in reply to Perl binary file

Hi

After some digging, would this work?

print $out pack('f<',2.5) ; #single, little endian print $out pack('d<',2.5) ; #double, little endian print $out pack('f>',2.5) ; #single, big endian print $out pack('d>',2.5) ; #single, big endian

Is there a diference between 'd' or 'D' (or 'f' and 'F')which can create incompatible issues? And as far the append, will it be sequencial?

Kind regards

Kepler

Replies are listed 'Best First'.
Re^2: Perl binary file
by soonix (Chancellor) on Dec 29, 2017 at 10:01 UTC
    perldoc pack says F is "Perl internal format", perlpacktut under "Floating point Numbers" is a bit more helpful, but not very…
Re^2: Perl binary file
by Anonymous Monk on Dec 31, 2017 at 09:35 UTC

    'd' or 'D'
    These are C double and long double, respectively. They are probably different. On Intel machines, long double is usually an 80-bit non-IEEE754 type while double is standard IEEE-754 double precision type.

    'f' and 'F'
    These are C float and "whatever Perl uses internally". Again, on Intel machines float usually represents IEEE-754 single precision type, while NV would be even less portable than long double (because a user could rebuild their Perl with different definition for NV) but priceless for exact data caching in between the runs of your own application.

    To answer your original question, use f for IEEE-754 single precision, d for IEEE-754 double precision types compatible with C and VB (if float and double do not correspond to IEEE types on a given system, chances are it doesn't support IEEE types at all and such processors are rare enough that you probably shouldn't worry about them) and > for big-endian (most significant byte first; "network byte order"), < for little-endian (least significant byte first; "Intel byte order"). Also, you were right, to sequentially append to binary files, just use open (my $fh, ">>:raw", $filename);.