Home >Backend Development >PHP Tutorial >php 中这个用法的浮点运算的函数是什么

php 中这个用法的浮点运算的函数是什么

WBOY
WBOYOriginal
2016-06-23 13:41:15857browse

php 中这个用法的浮点运算的函数是什么

请先看看百度百科里的浮点运算例子

http://wenku.baidu.com/view/9ba120a108a1284ac85043d8.html?re=view

也就是把 十六进制的 42C88000 
使用IEEE754标准的32位浮点数格式 算出来结果等于 100.25

把十六进制的C1C90000 算出是 -25.125 

有没有现成的函数?

帮我写个例子, 万分感激


谢谢


回复讨论(解决方案)

  function hexToDecFloat($strHex) {	$v = hexdec($strHex);	$x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);	$exp = ($v >> 23 & 0xFF) - 127;	return $x * pow(2, $exp - 23);}$a='42C88000 ';echo hexToDecFloat($a); 



等于100.25

$s = '42C88000';echo current(unpack('f', pack('V', hexdec($s))));
100.25
$s = 'C1C90000';echo current(unpack('f', pack('V', hexdec($s))));
-25.125

也可以

$s = 'C1C90000';echo current(unpack('f', pack('H*', join('', array_reverse(str_split($s, 2))))));
-25.125

#1 的代码只对正数有效
$a = 'C1C90000';echo hexToDecFloat($a);
-6.875
有待完善

谢谢版主大人的帮助. 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn