in reply to Unpacking extended-double precision floats

If your Perl supports extended double precision floats natively, then you can unpack them using the D pack code. Quoting pack's documentation, D is used to pack/unpack the following:

A long double-precision float in the native format. (Long doubles are available only if your system supports long double values _and_ if Perl has been compiled to support those. Causes a fatal error otherwise.)

Otherwise, I'm curious as to what you're expecting to get. Are you expecting a double, a string or a Math::BigFloat?

If you can read those floats in C, one solution would be to write a conversion routine in C and call it via XS or Inline::C.

Replies are listed 'Best First'.
Re^2: Unpacking extended-double precision floats
by herveus (Prior) on Nov 29, 2006 at 18:24 UTC
    Howdy!

    I'm reading a binary file. Part of the file consists of 10-byte sequences that are clearly floats, probably IEEE extended-double-precision floats. I'm looking to be able to convert to unpack that to a numeric value and pack it to a 10-byte sequence.

    That is, unpack 0x4001c888888888888888 to 12.5 (if I'm remembering actual data correctly), and pack 12.5 back to 0x4001c888888888888888.

    I may just resort to picking the bits apart and converting it myself. I'm not sure if my C compiler (gcc on Mac) does that particular datatype. I can afford to simply do the bit-slinging myself, if it comes to that. I was just hoping to find that someone else has already solved the problem. So far, no joy.

    update: added more words to clarify

    yours,
    Michael

      My question was about 12.5's format. You implied (in a private message) that the lost of precision is of no concern, so I'm deducing that you want an NV (double).

      Have you tried unpack 'D' yet?

        If loss of precision is of no concern, and the number of bits of exponent and sign is the same between d and D formats, you may be able to use unpack d plus two skip bytes, and pack it as a double d followed by two appropriate fill bytes (00 or 88 ?). Bill N1VUX
        Howdy!

        Yep. I tried that. The Perl on my mac (where I'm actually doing this) told me to pound sand. I'll try Convert::Binary::C when I get home.

        ...so, yes, I want an NV on the Perl side of things.

        yours,
        Michael