The source of your code is a contribution made in the PHP Manual ago today 8 years It appears in the part of the Manual that explains the floating point numbers .
The author of the same explains the following:
In some cases you may want to get the maximum value of one
floating without getting " INF
".
var_dump (1.8e308);
Will usually show: float (INF)
I wrote a small function that will iterate to find the greatest value
of non-infinite floating. It comes with a configurable multiplier and
related values so you can share more CPU to get a
more accurate estimate.
I have not seen better values with more affinities, but hey, the possibility
is here, so if what's really worthwhile is the time of the
CPU, just try to tune it more.
The best results appear to be with mul=2/affine=1
. You can play
with the values and see what you get. The good thing is that this method
it will work on any system.
This is the original function ... I think it's practically the same code as your question:
<?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;
}
?>
For what the same author explains:
- It is clear that here
'INF'
refers to the constant INF
PHP , which represents Infinity .
-
(string) $max
makes a type conversion , converting the variable $max
to a string of characters.
-
!=
is a comparison operator , which will return TRUE
if the values compared are < strong> different after type manipulation.
What the line of code ultimately does:
while( (string) $max != 'INF' ) { ...
It is the following:
Convert $max
to string
Enter a loop while the value of $max
converted to string is different from a value INFINITO
.
Why do you type 'INF'
and not INF
, being a constant? I'm not sure, but apparently, when it comes to infinite values, what PHP returns is a string with this value: INF
. That's what seems to be explained here , by saying:
That will return true for any string ending with "INF".
It's logical , in the case of comparisons. Because infinite is not really a value ... right? The only thing you can receive is some indication that it is infinite.