如何在PHP中获取浮点数的二进制表示?

时间:2011-01-18 21:59:24

标签: php floating-point numerical-methods

有没有办法在PHP中获取浮点数的二进制表示?类似Java的Double.doubleToRawLongBits()

给定一个正的浮点数,我想获得最大的可表示浮点数,该数字小于该数。在Java中,我可以这样做:

double x = Double.longBitsToDouble(Double.doubleToRawLongBits(d) - 1);

但我在PHP中没有看到任何类似内容。

4 个答案:

答案 0 :(得分:4)

这是我使用Peter Bailey的建议提出的解决方案。它需要64位版本的PHP。我并不认为这是生产质量的任何方式,但我分享以防任何人想要建立它。 (事实上​​,在我发布问题之后,我最终完成了一些不同的事情,但我把它留在这里作为一项智力练习。)

// Returns the largest double-precision floating-point number which
// is less than the given value. Only works for positive values.
// Requires integers to be represented internally with 64 bits (on Linux
// this usually means you're on 64-bit OS with PHP built for 64-bit OS).
// Also requires 64-bit double-precision floating point numbers, but I
// think this is the case pretty much everywhere.
// Returns false on error.
function prevDouble($d) {
  $INT32_MASK = 0xffffffff;
  if((0x1deadbeef >> 32) !== 1) {
    echo 'error: only works on 64-bit systems!';
    return false;
  }
  if($d <= 0) {
    return false;
  }
  $beTest = bin2hex(pack('d', 1.0)); //test for big-endian
  if(strlen($beTest) != 16) {
    echo 'error: system does not use 8 bytes for double precision!';
    return false;
  }

  if($beTest == '3ff0000000000000') {
    $isBE = true;
  }
  else if($beTest == '000000000000f03f') {
    $isBE = false;
  }
  else {
    echo 'error: could not determine endian mode!';
    return false;
  }

  $bin = pack('d', $d);

  //convert to 64-bit int
  $int = 0;
  for($i = 0; $i < 8; $i++)
    $int = ($int << 8) | ord($bin[$isBE ? $i : 7 - $i]);

  $int--;
  //convert back to double
  if($isBE)
    $out = unpack('d', pack('N', ($int >> 32) & $INT32_MASK) . pack('N', $int & $INT32_MASK));
  else
    $out = unpack('d', pack('V', $int & $INT32_MASK) . pack('V', ($int >> 32) & $INT32_MASK));

  return $out[1];
}

答案 1 :(得分:3)

作为一个额外的答案,不是整个问题,而是标题:

如果你想看看你的花车看起来像二进制文件:

function floatToBinStr($value) {
   $bin = '';
    $packed = pack('d', $value); // use 'f' for 32 bit
    foreach(str_split(strrev($packed)) as $char) {
        $bin .= str_pad(decbin(ord($char)), 8, 0, STR_PAD_LEFT);
    }
    return $bin;
}

echo floatToBinStr(0.0000000000000000000000000000000000025).PHP_EOL;
echo floatToBinStr(0.25).PHP_EOL;
echo floatToBinStr(0.5).PHP_EOL;
echo floatToBinStr(-0.5).PHP_EOL;

输出:

0011100010001010100101011010010110110111111110000111101000001111
0011111111010000000000000000000000000000000000000000000000000000
0011111111100000000000000000000000000000000000000000000000000000
1011111111100000000000000000000000000000000000000000000000000000

答案 2 :(得分:2)

这不是一个完整的答案,但我知道将浮动放入二进制文件的唯一方法是使用pack()

答案 3 :(得分:0)

的java:

long lnBits = Double.doubleToLongBits(longValue);
Byte[] bits = new byte [] {
   (byte) ((value << 56) >>> 56),
   (byte) ((value << 48) >>> 56),
   (byte) ((value << 40) >>> 56),
   (byte) ((value << 32) >>> 56),
   (byte) ((value << 24) >>> 56),
   (byte) ((value << 16) >>> 56),
   (byte) ((value << 8) >>> 56),
   (byte) (value >>> 56)}

PHP:

$bits = $bitsFromJava;
$str="";
for($i=0;$i<8;i++){
    $str.=chr($bits[$i]);
}
$longValue=unpack('d',$str);

$bitsToJava=array();
for(str_split(pack($longValue)) as $chr){
    $bitsToJava[]=ord($chr);
}