note 101392 added to function.exp
| From: | zooly at globmi dot com | Date: | Tue, 14 Dec 2010 15:12:58 +0000 |
| Subject: | note 101392 added to function.exp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174904@lists.php.net to get a copy of this message | ||
PHP does not have the following math function in any extensions:
frexp() - Extract Mantissa and Exponent of the Floating-Point Value
I've digged many C source codes, and found the simplest implementation as follows:
<?php
function frexp ( $float ) {
$exponent = ( floor(log($float, 2)) + 1 );
$mantissa = ( $float * pow(2, -$exponent) );
return(
array($mantissa, $exponent)
);
}
print_r(frexp(0.0345));
print_r(frexp(21.539));
?>
Array
(
[0] => 0.552
[1] => -4
)
Array
(
[0] => 0.67309375
[1] => 5
)
I have compared the results using a lot of floats against C's frexp function - they are the
same.
Note that C and PHP uses different float precisions, for example "4619.3" gives:
C: 0.56387939453125, 13
PHP: 0.563879394531, 13
/Assuming default configurations./
----
Server IP: 212.124.37.9
Probable Submitter: 80.77.114.203
----
Manual Page -- http://www.php.net/manual/en/function.exp.php
Edit -- https://master.php.net/note/edit/101392
Del: integrated -- https://master.php.net/note/delete/101392/integrated
Del: useless -- https://master.php.net/note/delete/101392/useless
Del: bad code -- https://master.php.net/note/delete/101392/bad+code
Del: spam -- https://master.php.net/note/delete/101392/spam
Del: non-english -- https://master.php.net/note/delete/101392/non-english
Del: in docs -- https://master.php.net/note/delete/101392/in+docs
Del: other reasons-- https://master.php.net/note/delete/101392
Reject -- https://master.php.net/note/reject/101392
Search -- https://master.php.net/manage/user-notes.php