Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

'use bignum' scoping question

by si_lence (Deacon)
on Jan 10, 2010 at 22:12 UTC ( #816650=perlquestion: print w/replies, xml ) Need Help??
si_lence has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm not sure how I can understand the output I'm getting from the sample program below. Maybe (probably) something quite simple that I'm missing..
I need to calculate square roots with more than the standard precision so I'm using use bignum. This works fine unless it is in a loop. Anybody care to explain?
use strict; use warnings; use Data::Dumper; use bignum(a=>200); my $s = sqrt(2);; print Dumper(\$s); for my $num (2..2) { $s = sqrt($num); print Dumper(\$s); }
The output is:
$VAR1 = \bless( { '_es' => '-', '_m' => [ 605715, 1152782, 9995050, 5927557, 5927505, 1322266, 9358314, 1497099, 1264412, 8507372, 4836055, 9702492, 2309122, 5013846, 4157273, 5343276, 8850387, 6210703, 7324784, 9737990, 7317667, 3769480, 9671875, 9807856, 7242096, 8801688, 7309504, 2135623, 1414 ], '_a' => 200, '_e' => [ 199 ], 'sign' => '+' }, 'Math::BigFloat' ); $VAR1 = \'1.4142135623731';
It looks as if the blessing of $s is lost in the loop, but I don't understand why.
si_lence (confused)
Update: Thanks everybody for pointing out the reason and the suggestions. I did read the documentation, but obviously glossed over the important part.

Replies are listed 'Best First'.
Re: 'use bignum' scoping question
by lamprecht (Friar) on Jan 10, 2010 at 23:00 UTC

    from perldoc bignum:

    Please note the following does not work as expected (prints nothing), +since overloading of '..' is not yet possible in Perl (as of v5.8.0): perl -Mbignum -le 'for (1..2) { print ref($_); }'

    this works:

    for my $num (2..2) { $s = sqrt($num * 1); print Dumper(\$s); }

    Cheers, Christoph

      And so does

      for my $num (2..2) { $s = sqrt(Math::BigFloat->new($num)); print Dumper(\$s); }

      That's what bignum does to constants.

Re: 'use bignum' scoping question
by syphilis (Chancellor) on Jan 10, 2010 at 23:17 UTC
    Looks like a bignum bug to me. It's certainly not the behaviour I would expect, though I haven't checked to see what (if anything) the bignum documentation has to say on the matter.
    Update:Aaah ... but lamprecht has :-)

    Safest workaround would be to explicitly use Math::BigFloat:
    use strict; use warnings; use Math::BigFloat; Math::BigFloat->accuracy(200); my $s = sqrt(Math::BigFloat->new(2)); print $s, "\n"; for my $num (2..2) { $s = sqrt(Math::BigFloat->new($num)); print $s; }
Re: 'use bignum' scoping question
by Anonymous Monk on Jan 10, 2010 at 23:02 UTC
    From bignum ...loaded and any constant number will be converted to an object (Math::BigFloat for floats like 3.1415 and Math::BigInt for integers like 1234).

    $num is not a constant, 0 is a constant, and 0 + $num is a Math::BigInt

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816650]
Approved by AnomalousMonk
Front-paged by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-04-25 18:42 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (462 votes). Check out past polls.