Пример #1
0
 function BigEndian2Float($byteword)
 {
     // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
     // http://www.psc.edu/general/software/packages/ieee/ieee.html
     // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
     $bitword = BigEndian2Bin($byteword);
     $signbit = $bitword[0];
     switch (strlen($byteword) * 8) {
         case 32:
             $exponentbits = 8;
             $fractionbits = 23;
             break;
         case 64:
             $exponentbits = 11;
             $fractionbits = 52;
             break;
         case 80:
             $exponentbits = 16;
             $fractionbits = 64;
             break;
         default:
             return false;
             break;
     }
     $exponentstring = substr($bitword, 1, $exponentbits - 1);
     $fractionstring = substr($bitword, $exponentbits, $fractionbits);
     $exponent = Bin2Dec($exponentstring);
     $fraction = Bin2Dec($fractionstring);
     if ($exponentbits == 16 && $fractionbits == 64) {
         // 80-bit
         // As used in Apple AIFF for sample_rate
         // A bit of a hack, but it works ;)
         return pow(2, $exponent - 16382) * DecimalBinary2Float($fractionstring);
     }
     if ($exponent == pow(2, $exponentbits) - 1 && $fraction != 0) {
         // Not a Number
         $floatvalue = false;
     } elseif ($exponent == pow(2, $exponentbits) - 1 && $fraction == 0) {
         if ($signbit == '1') {
             $floatvalue = '-infinity';
         } else {
             $floatvalue = '+infinity';
         }
     } elseif ($exponent == 0 && $fraction == 0) {
         if ($signbit == '1') {
             $floatvalue = -0;
         } else {
             $floatvalue = 0;
         }
         $floatvalue = $signbit ? 0 : -0;
     } elseif ($exponent == 0 && $fraction != 0) {
         // These are 'unnormalized' values
         $floatvalue = pow(2, -1 * (pow(2, $exponentbits - 1) - 2)) * DecimalBinary2Float($fractionstring);
         if ($signbit == '1') {
             $floatvalue *= -1;
         }
     } elseif ($exponent != 0) {
         $floatvalue = pow(2, $exponent - (pow(2, $exponentbits - 1) - 1)) * (1 + DecimalBinary2Float($fractionstring));
         if ($signbit == '1') {
             $floatvalue *= -1;
         }
     }
     return (double) $floatvalue;
 }
function BigEndian2Float($byteword)
{
    // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
    // http://www.psc.edu/general/software/packages/ieee/ieee.html
    // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
    $bitword = BigEndian2Bin($byteword);
    $signbit = $bitword[0];
    if (strlen($byteword) == 4) {
        // 32-bit DWORD
        $exponentbits = 8;
        $fractionbits = 23;
    } else {
        if (strlen($byteword) == 8) {
            // 64-bit QWORD
            $exponentbits = 11;
            $fractionbits = 52;
        } else {
            return FALSE;
        }
    }
    $exponentstring = substr($bitword, 1, $exponentbits);
    $fractionstring = substr($bitword, 9, $fractionbits);
    $exponent = Bin2Dec($exponentstring);
    $fraction = Bin2Dec($fractionstring);
    if ($exponent == pow(2, $exponentbits) - 1 && $fraction != 0) {
        // Not a Number
        $floatvalue = FALSE;
    } else {
        if ($exponent == pow(2, $exponentbits) - 1 && $fraction == 0) {
            if ($signbit == '1') {
                $floatvalue = '-infinity';
            } else {
                $floatvalue = '+infinity';
            }
        } else {
            if ($exponent == 0 && $fraction == 0) {
                if ($signbit == '1') {
                    $floatvalue = -0;
                } else {
                    $floatvalue = 0;
                }
                $floatvalue = $signbit ? 0 : -0;
            } else {
                if ($exponent == 0 && $fraction != 0) {
                    // These are 'unnormalized' values
                    $floatvalue = pow(2, -1 * (pow(2, $exponentbits - 1) - 2)) * DecimalBinary2Float($fractionstring);
                    if ($signbit == '1') {
                        $floatvalue *= -1;
                    }
                } else {
                    if ($exponent != 0) {
                        $floatvalue = pow(2, $exponent - (pow(2, $exponentbits - 1) - 1)) * (1 + DecimalBinary2Float($fractionstring));
                        if ($signbit == '1') {
                            $floatvalue *= -1;
                        }
                    }
                }
            }
        }
    }
    return (double) $floatvalue;
}