large float's

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    large float's

    According to php.net, the largest possible float is ~1.8e308.
    wikipedia.org offers more insight, suggesting that is, exactly,
    0x7FEFFFFFFFFFF FFF. This sorta begs the question... how can this
    number be represented in PHP without resorting to the decimal notation?

    <?
    echo 0x7FEFFFFFFFFFF FFF;
    ?>

    ....doesn't work. Nor does...

    <?
    echo hexdec('7FEFFFF FFFFFFFFF');
    ?>

    So... any ideas as to what will work?

  • Oli Filth

    #2
    Re: large float's

    yawnmoth said the following on 23/02/2006 06:00:[color=blue]
    > According to php.net, the largest possible float is ~1.8e308.
    > wikipedia.org offers more insight, suggesting that is, exactly,
    > 0x7FEFFFFFFFFFF FFF. This sorta begs the question... how can this
    > number be represented in PHP without resorting to the decimal notation?
    >[/color]

    The maximum floating-point value is platform dependent. For a 64-bit
    platform, it's ~1.8e308, but for a 32-bit platform it's ~3.4e38
    (assuming you're using the IEEE 754 standard).

    This code converts from a hex-representation to a floating-point
    representation (for 32-bit):


    define("LENGTH" , 32);
    define("SIGN_BI T", 31);
    define("EXPONEN T_MSB", 30);
    define("EXPONEN T_LSB", 23);
    define("MANTISS A_MSB", 22);
    define("MANTISS A_LSB", 0);

    function hex2float($strH ex)
    {
    $dec = hexdec($strHex) ;
    $sign = ($dec & (1 << SIGN_BIT)) != 0;
    $exp = (($dec & ((2 << EXPONENT_MSB) - (1 << EXPONENT_LSB))) >>
    EXPONENT_LSB)
    - (1 << (EXPONENT_MSB - EXPONENT_LSB))
    - (MANTISSA_MSB - MANTISSA_LSB);
    $man = (($dec & ((2 << MANTISSA_MSB) - (1 << MANTISSA_LSB))) >>
    MANTISSA_LSB)
    + (2 << (MANTISSA_MSB - MANTISSA_LSB));
    $float = floatval($man * pow(2, $exp) * ($sign ? -1 : 1));
    }


    For a 64-bit version, you'll need to alter the constants; you can get
    the required values from
    http://stevehollasch.com/cgindex/coding/ieeefloat.html.

    Note that this code doesn't deal with special cases, i.e. NaN, infinity,
    etc.

    --
    Oli

    Comment

    • Chung Leong

      #3
      Re: large float's

      Oli Filth wrote:[color=blue]
      > The maximum floating-point value is platform dependent. For a 64-bit
      > platform, it's ~1.8e308, but for a 32-bit platform it's ~3.4e38
      > (assuming you're using the IEEE 754 standard).[/color]

      Actually the bit-ness of a CPU only refers to its integer handling
      capability. Nearly all FPUs--even the ancient 8087--can handle both
      single and double precision floating points.

      Float points in PHP are defined as doubles, which in C are always
      64-bit IIRC.

      Comment

      Working...