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

When I take the sqrt of a BigFloat of 0.9999, the program hangs and the CPU utilization pegs to 99%.   If I sqrt the value as a string, it works.   Is this a sqrt bug for BigFloats greater than 0, but less than 1?

Here's my code:

#!/tool/pandora/bin/perl5.8.0 use strict; use warnings; use Data::Dumper; use Math::BigFloat; print "sqrt(0.96303) = ".sqrt(0.96303)."\n"; my $value = Math::BigFloat->new(); $value += 0.9999; print "value = ".Dumper($value); my $sqrt_value = sqrt("$value"); #Works because $value is converted t +o a string. print "Stringified BigFloat sqrt($value) = $sqrt_value\n"; $sqrt_value = sqrt($value); #Hangs here because $value is less than 1 +. 99% CPU utilization print "BigFloat sqrt($value) = $sqrt_value\n";

janitored by ybiC: Balanced <code> tags around code as per Monastery convention

Replies are listed 'Best First'.
Re: sqrt of BigFloat 0<x<1
by pg (Canon) on Oct 07, 2003 at 02:08 UTC

    Update

    Miss some info, I actually tested before reply.
    • The version of BigFloat coming with my perl (5.8.0) is version 1.35, which has the same problem bart described.
    • I downloaded 1.40, it worked as Roger described.
    • A reasonable guess is that at the time Roger download Perl, it had already been rebundled with bunch of fixes, including some fixes to BigFloat.
    • Chatted with Roger, he confirmed he used 1.38, which is higher than my 1.35.

    Original

    Both bart and Roger are right, as they used different versions of Math::BigFloat.

    Just go to CPAN, get the latest version, which I believe is 1.40, and your problem will go away.

Re: sqrt of BigFloat 0<x<1
by bart (Canon) on Oct 07, 2003 at 00:11 UTC
    I get a seemingly endless list of the following warning, when running under -w:
    Use of uninitialized value in numeric ge (>=) at D:/programs/activeperl/Perl/lib /Math/BigFloat.pm line 1136.

    BTW I'm running it with ActivePerl5.8.0, and the stock Math::BigFloat that comes with it: $Math::BigFloat::VERSION==1.35.

Re: sqrt of BigFloat 0<x<1
by Roger (Parson) on Oct 07, 2003 at 01:32 UTC
    I seemed to have no problem at all running the perl script. I ran the script under Active Perl 5.8.0 on Windows with version 1.38 of Math::Bigfloat, which produced the following output:
    sqrt(0.96303) = 0.981340919354737 value = $VAR1 = bless( { '_m' => bless( { 'value' => [ 9999 ], '_a' => undef, '_f' => 1, '_p' => undef, 'sign' => '+' }, 'Math::BigInt' ), '_e' => bless( { 'value' => [ 4 ], '_a' => undef, '_f' => 1, '_p' => undef, 'sign' => '-' }, 'Math::BigInt' ), 'sign' => '+' }, 'Math::BigFloat' ); Stringified BigFloat sqrt(0.9999) = 0.999949998749938 BigFloat sqrt(0.9999) = 0.9999499987499374960934765419905760409437
Re: sqrt of BigFloat 0<x<1
by Anonymous Monk on Oct 07, 2003 at 02:37 UTC
    Thanks for the info! It is interesting that BigFloat had the issue. I guess it must be related to BigFloat not determining what sqrt "want"ed correctly.