Joost has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I'm working at some Perl/XS code that needs knowledge of the minimum and maximum values for floating point numbers (that is, floats). I know this is system-dependent, but I would like to know how to get at this information from Perl.

I've been searching the docs and the only thing I can find is this entry in the Config manpage:

"i_float" From i_float.U: This variable conditionally defines the "I_FLOAT" symbol, and indicates whether a C program may include <float.h> to get symbols like "DBL_MAX" or "DBL_MIN", i.e. machine dependent floating point values.
The same thing seems to be the case for integers and Perl's integer and float types too, by the way.

It seems strange to me that this information is not available. Does anyone know of a way to get at this info from Perl? I might be tempted to create a seperate XS module for it otherwise.

Yours,
Joost.

Replies are listed 'Best First'.
Re: How to determine MIN and MAX values for floating point number
by sauoq (Abbot) on Dec 09, 2003 at 21:19 UTC
    $ perl -MPOSIX -le 'print POSIX::FLT_MAX; print POSIX::FLT_MIN'
    -sauoq
    "My two cents aren't worth a dime.";
    
      I think you'll want DBL_MAX and DBL_MIN (or LDBL_MAX and LDBL_MIN if $Config{uselongdouble}). And LDBL_MIN isn't the minimum value, it's the minimum possible normal value greater than 0. "Normal" here means a number with the full mantissa's worth of precision. Most implementations allow "subnormal" numbers that can get smaller. For example:
      $ perl -MPOSIX=DBL_MIN -wle'print DBL_MIN()' 2.2250738585072e-308 $ perl -MPOSIX=DBL_MIN -wle'print DBL_MIN()/2**52' 4.94065645841247e-324 $ perl -MPOSIX=DBL_MIN -wle'print DBL_MIN()/2**53' 0
Re: How to determine MIN and MAX values for floating point number
by sleepingsquirrel (Chaplain) on Dec 09, 2003 at 21:50 UTC
    This probably isn't what you're looking for, but I'm curious as to when the following code would fail (i.e. for which architectures is I_FLOAT undefined).
    #!/usr/bin/perl use Inline C; $max=f_max(); $min=f_min(); print "max: $max\nmin: $min\n"; __END__ __C__ double f_max() { return FLT_MAX; } double f_min() { return FLT_MIN; }
Re: How to determine MIN and MAX values for floating point number
by zentara (Cardinal) on Dec 10, 2003 at 21:07 UTC
    I was studying this thread offline, and found this old snippet in my "snippet library". It runs until it "hits the wall" and yields close to the DBL_MAX and DBL_MIN values. Wriitten by some unknown monk. FWIW
    #!/usr/bin/perl # get the largest and smallest float usable without # Math::BigFloat my $i = 1.0; my $lasti; my $bignum; for ( ; ; ) { $bignum = $lasti; $lasti = $i; $i *= 2; #print $i,"\n"; last if ( $i == $lasti ); } print "$bignum\n"; #The last number before "inf" is your largest representable #float (8.98846567431158e+307 on my system). #A similar trick with division by 2 will give you the smallest #representable float (4.94065645841247e-324 on my system). $i = 1.0; $lasti; my $smallnum; for ( ; ; ) { $smallnum = $lasti; $lasti = $i; $i /= 2; #print $i,"\n"; last if ( $i == $lasti ); } print "$smallnum\n";
Re: How to determine MIN and MAX values for floating point number
by hardburn (Abbot) on Dec 09, 2003 at 21:26 UTC

    Umm, it looks like you've got the answer right there: Config. Update: Never mind.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Umm, it looks like you've got the answer right there: Config.

      Umm, no.

      Config doesn't provide vars for max/min values. It does provide some XXXsize variables that'll tell you the size in bytes of various types though.

      -sauoq
      "My two cents aren't worth a dime.";