perlquestion
fizbin
The following code (when run without any arguments, so that the default @ARGV is picked up below) triggers some kind of strange bizarre bug in <c>Math::BigFloat</c>. It appears that I'm creating a value that is both 1 and 0 at the same time. Can someone help make it shorter into a test case I can submit?
<readmore>
<code>
#!perl
use bignum;
# This is Stirling's approximation for log(n!), but
# without the "-n" piece that cancels out below in any
# case.
sub xe{
$_[0]*log($_[0])+log(atan2(1,1)*8*$_[0])/2
+ 1/(12*$_[0]) - 1/(360*($_[0]**3)) + 1/(1260*($_[0]**5));
}
if (!@ARGV) {
@ARGV = qw{ 2**96 3*(10**6) 1*(10**6) };
}
my($m,$n,$r)=map {eval "use bignum;$_"}@ARGV;
my $e=exp(xe($m-$n)+xe($m-$r)-xe($m)-xe($m-$n-$r));
printf "%3.5g %3.5g %3.5g\n",$e,1-$e,(1-"$e");
</code>
<p>The bug is that the numbers printed in the last line don't make sense. Somehow, both <c>$e</c> and <c>1-$e</c> print as "1", though <c>1-"$e"</c> properly prints as "0". (The true value of <c>$e</c> should in fact be somewhere between <c>1-2**(-54)</c> and 1)</p>
<p>When I use <c>Data::Dumper</c>, then <c>$e</c> is shown as <c>'1'</c> (note the quotes). However, that doesn't really help isolate the bug, as this also creates something that dumps as <c>'1'</c>, but doesn't show the "both $e and 1-$e are 1" anomaly:
<code>
use bignum;
$e = exp(2 ** (-128));
use Data::Dumper;
print Dumper($e);
print $e,"\n",1-$e,"\n";
</code>
<p>My perl environment is perl 5.8.7 on cygwin.</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-246930">
--<br>
<code>@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/</code>
</div></div>