note 98216 added to language.types.float
| From: | magicaltux@php.net | Date: | Wed, 02 Jun 2010 02:02:14 +0000 |
| Subject: | note 98216 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169813@lists.php.net to get a copy of this message | ||
In some cases you may want to get the maximum value for a float without getting "INF".
var_dump(1.8e308); will usually show: float(INF)
I wrote a tiny function that will iterate in order to find the biggest non-infinite float value. It
comes with a configurable multiplicator and affine values so you can share more CPU to get a more
accurate estimate.
I haven't seen better values with more affine, but well, the possibility is here so if you
really thing it's worth the cpu time, just try to affine more.
Best results seems to be with mul=2/affine=1. You can play with the values and see what you get. The
good thing is this method will work on any system.
<?php
function float_max($mul = 2, $affine = 1) {
$max = 1; $omax = 0;
while((string)$max != 'INF') { $omax = $max; $max *= $mul; }
for($i = 0; $i < $affine; $i++) {
$pmax = 1; $max = $omax;
while((string)$max != 'INF') {
$omax = $max;
$max += $pmax;
$pmax *= $mul;
}
}
return $omax;
}
?>
----
Server IP: 219.94.145.73
Probable Submitter: 122.18.184.200
----
Manual Page -- http://www.php.net/manual/en/language.types.float.php
Edit -- https://master.php.net/note/edit/98216
Del: integrated -- https://master.php.net/note/delete/98216/integrated
Del: useless -- https://master.php.net/note/delete/98216/useless
Del: bad code -- https://master.php.net/note/delete/98216/bad+code
Del: spam -- https://master.php.net/note/delete/98216/spam
Del: non-english -- https://master.php.net/note/delete/98216/non-english
Del: in docs -- https://master.php.net/note/delete/98216/in+docs
Del: other reasons-- https://master.php.net/note/delete/98216
Reject -- https://master.php.net/note/reject/98216
Search -- https://master.php.net/manage/user-notes.php