Search code examples
rfloating-pointtype-conversion

Can I truncate 10-byte "extended" number and convert as an 8-byte double?


I'm playing with AIFF files, which include an 80-bit "extended" format value which defines the sample rate. Since neither my language of choice, R, nor my desktop Mac, natively supports this size, I need to write conversion code. Given the data structure of the bits, as shown here, and in this image from that page enter image description here ,

Can I simply truncate the 'fraction' part on the assumption that I'll never see a sample rate of that high a precision? That is, can I grab the 64 bits starting on the left and treat them as a double float? Note that the number of valid bits in the exponent must be truncated as well, per the chart in this question

A minimal example: I verified the sample rate in a test file is 44100 .

The 10 bytes read from the AIFF header are:

40 0e ac 44 00 00 00 00 00 00

edit ac 44 as 4 raw bytes converted to decimal becomes 44100, which is the sample rate I'd expect.


Solution

  • Here is a direct approach which doesn't require any truncation.

    1. convert the 15-bit exponent. Follow the standard procedure for doubles, but now calculate ExpMul = 2^(exponent - 16383) ; the latter number being 2^14 -1 .

    2. Store the lead bit of the mantissa (equivalent to the implicit "1" in doubles). Calculate the value of the remaining bits as a decimal fraction. (Note: I used bigBits:fracB2B for this).

    3. multiply (lead_bit + fraction) * (ExpMul) .

    My sample value in fact produced 44100 , as I expected to find.