What is the floating point function used in php?

Source: Internet
Author: User
In php, what is the floating point operation function used in php?

Please take a look at the floating point operation examples in Baidu Encyclopedia.

Http://wenku.baidu.com/view/9ba120a108a1284ac85043d8.html? Re = view

That is, the hexadecimal 42C88000
The result is equivalent to 100.25 in the IEEE754 standard 32-bit floating point format.

Calculate the hexadecimal C1C90000 value as-25.125.

Are there ready-made functions?

I am very grateful for writing an example for me.


Thank you.


Reply to discussion (solution)

  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); 



Equals 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

Yes.

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

# The code of 1 is only valid for positive numbers.
$a = 'C1C90000';echo hexToDecFloat($a);
-6.875
To be improved

Thank you for your help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.