What does (string) $ max! = 'INF' mean?

2

Does anyone help me understand this?

I was practicing with PHP, I came across this code. I'm trying to solve it:

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

echo float_max(); 

I do not know what this line of code does:

(string)$max != 'INF'

When I send to print% co_of result% I get echo float_max();

    
asked by isgb 02.07.2017 в 08:27
source

4 answers

3

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.

        
    answered by 02.07.2017 / 16:06
    source
    4

    (string)$max != 'INF' is a comparison expression. Compare if two values are different.

    First of all we have: (string)$max that converts to string the value contained in the variable $max .

    Second INF is a defined PHP constant: link .

    What I do not know is that INF is enclosed in quotes. In this case it's just a string ... a bad implementation?

        
    answered by 02.07.2017 в 10:16
    0
      

    The function you have is to find the largest floating point number supported by the CPU you use.

    INF is a PHP constant that represents infinity.

    Source: link

        
    answered by 02.07.2017 в 09:59
    0

    It is a logical comparison but to be able to do it it is necessary to convert the variable of type int to string :

    (string)$max
    

    Then the logical operator NOT != to compare it with the text string 'INF' .

    In case the author of the code wanted to compare it with the constant INF of PHP he had to have removed the single quotes.

    Then:

    (string)$max != 'INF'
    

    If the variable $max converted to string is different ( != ) of the other string 'INF' the condition will be fulfilled.

        
    answered by 02.07.2017 в 13:08