Re: help : getting float max

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2009-05-24 at 14:33 +0100, Nathan Rixham wrote:
> Afternoon all,
> 
> This is a quick survey, think it would be useful to have the values of 
> MAX_FLOAT for each platform, and indeed see if it does differ.
> 
> to do this can you please run the following code (bc* required) and 
> reply back with the output (and your platform / php version)
> 
> code:
> <?php
> 
> function getMaxFloat() {
> 	$value = '1';
> 	$multiplier = '1';
> 	while(1) {
> 		$value = bcadd( $value , $multiplier );
> 		if( (float)$value == INF ) {
> 			$value = $oldvalue;
> 			$multiplier = '1';
> 		} elseif( ((float)bcadd( $value , '10' )) == INF ) {
> 			while(1) {
> 				$value = bcadd( $value , '1' );
> 				if( (float)$value == INF ) {
> 					return $oldvalue;	
> 				}
> 				$oldvalue = $value;
> 			}
> 		}
> 		$oldvalue = $value;
> 		$multiplier = bcmul( $multiplier , '10' );
> 	}
> }
> 
> define( 'FLOAT_MAX' , getMaxFloat() );
> 
> echo FLOAT_MAX . PHP_EOL;
> 
> ?>
> 
> my results:
> 
> Win Vista 32 bit on Intel Quad Core Duo - PHP 5.2.8
> 179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791
> 
> Thanks!

Here's a faster version that takes a split second to evaluate:

<?php

function getMaxFloat()
{
    $val1 = 2;
    $val2 = 2;

    for( ; ; )
    {
        $val2 = bcmul( $val1, $val1, 0 );

        if( (string)((float)$val2) === 'INF' )
        {
            break;
        }

        $val1 = $val2;
    }

    for( ; ; )
    {
        $val2 = bcadd( $val1, $val1, 0 );

        if( (string)((float)$val2) === 'INF' )
        {
            break;
        }

        $val1 = $val2;
    }

    $mod = bcdiv( $val1, 2, 0 );

    for( ; ; )
    {
        if( (float)$mod < 1 )
        {
            break;
        }

        $val2 = bcadd( $val1, $mod, 0 );

        if( (string)((float)$val2) === 'INF' )
        {
            $mod = bcdiv( $mod, 2, 0 );
        }
        else
        {
            $val1 = $val2;
        }
    }

    return $val1;
}

define( 'FLOAT_MAX' , getMaxFloat() );

echo FLOAT_MAX . PHP_EOL;

?>

Also, when I tried using your INF comparison it always evaluated to
false so I did conversions to string... it's strange though since the
INF constant displayed "INF" when echoed and the (float)$val2 displayed
INF when echoed (when it passed the available limits) but comparing them
yielded false. Anyways:

AMD Phenom(tm) 9850 Quad-Core Processor
PHP 5.2.9
Ubuntu 8.04.2 (Hardy)

179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791

This is a match to your results.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux