4

I have a program in Perl that works with probabilities that can occasionally be very small. Because of rounding error, sometimes one of the probabilities comes out to be zero. I'd like to do a check for the following:

use constant TINY_FLOAT => 1e-200;
my $prob = calculate_prob();
if ( $prob == 0 ) {
    $prob = TINY_FLOAT;
}

This works fine, but I actually see Perl producing numbers that are smaller than 1e-200 (I just saw a 8.14e-314 fly by). For my application I can change calculate_prob() so that it returns the maximum of TINY_FLOAT and the actual probability, but this made me curious about how floating point numbers are handled in Perl.

What's the smallest positive floating-point value in Perl? Is it platform-dependent? If so, is there a quick program that I can use to figure it out on my machine?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
James Thompson
  • 46,512
  • 18
  • 65
  • 82
  • 3
    Note that when working with small numbers, it may be reasonable to explicitly work in the log-domain. This turns multiplication (which is quite common with probabilities) into addition, but makes addition complicated. A straight log( exp(x) + exp(y) ) is likely to lose precision. But this is readily avoided: log( exp(x) + exp(y) ) = x + log( 1 + exp(y - x)). log (1 + x) has a very nice Taylor series, and `Math::Libm` has a binding to C's `log1p` which is very good at avoiding keeping precision. – wnoise Mar 17 '11 at 20:34

4 Answers4

11

According to perldoc perlnumber, Perl uses the native floating point format where native is defined as whatever the C compiler that was used to compile it used. If you are more worried about precision/accuracy than speed, take a look at bignum.

Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
10

The other answers are good. Here is how to find out the approximate ε if you did not know any of that information and could not post your question on SO ;-)

 #!/usr/bin/perl

use strict;
use warnings;

use constant MAX_COUNT => 2000;

my ($x, $c);

for (my $y = 1; $y; $y /= 2) {
    $x = $y;
    # guard against too many iterations
    last if ++$c > MAX_COUNT;
}

printf "%d : %.20g\n", $c, $x;

Output:

C:\Temp> thj
1075 : 4.9406564584124654e-324
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
8

It may be important to note that that smallest number is what's called a subnormal number, and math done on it may produce surprising results:

$ perl -wle'$x = 4.94e-324; print for $x, $x*1.4, $x*.6'
4.94065645841247e-324
4.94065645841247e-324
4.94065645841247e-324

That's because it uses the smallest allowed (base-2) exponent and a mantissa of the form (base-2) 0.0000000...0001. Larger, but still subnormal, numbers will also have a mantissa beginning 0. and an increasing range of precision.

ysth
  • 96,171
  • 6
  • 121
  • 214
4

I actually don't know how perl represents floating point numbers (and I think this is something you configure when you build perl), but if we assume that IEEE 754 is used then epsilon for a 64 bit floating point number is 4.94065645841247E-324.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256