Subject: | bignum/bigrat can lead to a number that is both 1 and 0 |
The following test code, when run on the stock cygwin build of perl
5.8.7, fails. Specifically, the value $ev prints as "1", but so does
the value 1-$ev.
#!perl
use strict;
use warnings;
use Test::More tests => 4;
use bignum;
my $lnev = -7 / (10**17);
my $ev=exp($lnev);
is( sprintf('%0.5f',$ev) , '1.00000', '($ev) is approx. 1' );
is( sprintf('%0.5f',1-$ev) , '0.00000', '(1-$ev) is approx. 0' );
is( sprintf('%0.5f',1-"$ev") , '0.00000', '(1-"$ev") is approx. 0' );
cmp_ok( $ev, '!=', 0, '$ev should not equal 0');
__END__
Note that on my copy of 5.8.7, tests 2 and 4 fail. I have it on good
authority that on perl 5.8.6, test 4 succeeds, though test 2 still fails.
I _think_ it has something to do with the magic bignum applies to the
exp function, rather than any magic being applied to constants since the
following code properly prints 0:
#!perl
use Math::BigFloat;
print ((new Math::BigFloat("1")) -
exp(new Math::BigFloat("-7")/
(new Math::BigFloat("10")**(new Math::BigFloat("17")))));
__END__
But this code prints "1"; the only difference is "use bignum":
#!perl
use bignum;
use Math::BigFloat;
print ((new Math::BigFloat("1")) -
exp(new Math::BigFloat("-7")/
(new Math::BigFloat("10")**(new Math::BigFloat("17")))));
__END__