5
my $num = log(1_000_000) / log(10);
print "num: $num\n";
print "int(num): " . int($num) . "\n";
print "sprintf(num): " . sprintf("%0.16f", $num) . "\n";

produces:

num: 6
int(num): 5
sprintf(num): 5.9999999999999991

To what precision does perl print floating-point numbers?

Using: v5.8.8 built for x86_64-linux-thread-multi

Jens
  • 69,818
  • 15
  • 125
  • 179
garrett
  • 207
  • 1
  • 5
  • 12

1 Answers1

10

When stringifying floating point numbers, whether to print or otherwise, Perl generally uses the value of DBL_DIG or LDBL_DIG from the float.h or limits.h file where it was compiled.

This is typically the precision of the floating point type perl will use rounded down. For instance, if using a typical double type, the precision is 53 bits = 15.95 digits, and Perl will usually stringify with 15 digits of precision.

ysth
  • 96,171
  • 6
  • 121
  • 214