note 106106 modified in language.types.float by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Oct 2011 16:00:34 +0000 |
| Subject: | note 106106 modified in language.types.float by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183793@lists.php.net to get a copy of this message | ||
Converting IEEE754 64 bit binary to float in 32bit php version:
<?php
function getDouble($bits) {
if( strlen(binary_data)!=64 )
throw new Exception("wrong binary data");
$sign = $digits[0];
$exp = bindec(substr($bits, 1, 11)) - 1023;
$mantissa_length = 30; // This is max workable length for 32bit representation of mantissa.
We not use other (52-$mantissa_length bits)
$man = (2 << ($mantissa_length-1)) + bindec(substr($bits, 12, $mantissa_length));
return $man * pow(2, $exp - $mantissa_length) * ($sign ? -1 : 1);
}
?>
--was--
Converting IEEE754 64 bit binary to float in 32bit php version:
function getDouble($bits) {
if( strlen(binary_data)!=64 )
throw new Exception("wrong binary data");
$sign = $digits[0];
$exp = bindec(substr($bits, 1, 11)) - 1023;
$mantissa_length = 30; // This is max workable length for 32bit representation of mantissa.
We not use other (52-$mantissa_length bits)
$man = (2 << ($mantissa_length-1)) + bindec(substr($bits, 12, $mantissa_length));
return $man * pow(2, $exp - $mantissa_length) * ($sign ? -1 : 1);
}
http://php.net/manual/en/language.types.float.php