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

Re: Pragma clash across modules: bignum and Math::BigFloat

by Anonymous Monk
on Dec 06, 2016 at 19:24 UTC ( [id://1177322]=note: print w/replies, xml ) Need Help??


in reply to Pragma clash across modules: bignum and Math::BigFloat

What's going on here is that bignum is meddling with the globals $Math::BigFloat::downgrade and $Math::BigInt::upgrade. It sets downgrade to Math::BigInt, which causes the BigFloat constructor to return BigInts instead, when they don't have a decimal point. Setting $Math::BigInt::upgrade makes BigInt::bsqrt() convert to BigFloat before taking the square root; the problem is, now BigFloat::bsqrt() is working on a copy and doesn't modify the original.

use bignum; my $f = Math::BigFloat->new('2'); print $f, "\n"; print ref($f), "\n"; print $f->bsqrt(), "\n"; print $f, "\n";
output:
2 Math::BigInt 1.41421356237309504880168872420969807857 2

There's really no way for bignum to do the right thing. It would have to muck around with those globals every time the flow of control entered or left its lexical scope. And the automatic upgrade feature was just a bad idea from the start. Solution: Don't use bignum, I'm afraid.

Replies are listed 'Best First'.
Re^2: Pragma clash across modules: bignum and Math::BigFloat (hinthash)
by LanX (Saint) on Dec 07, 2016 at 22:10 UTC
    > bignum is meddling with the globals $Math::BigFloat::downgrade and $Math::BigInt::upgrade.

    I haven't checked in detail yet (the code is a bit obscure) but this sounds reasonable.

    But I disagree when you say

    > There's really no way for bignum to do the right thing. It would have to muck around with those globals every time the flow of control entered or left its lexical scope. And the automatic upgrade feature was just a bad idea from the start. Solution: Don't use bignum, I'm afraid.

    Athanasius point was not why bignum fails but why the use of pragma bignum is effecting a completely other package, since pragmas are guaranteed to be limited to a scope.

    It's rather a limitation of BigFloat and BigInt to use global variable which keep their state even if the scope is left.

    perlpragma shows a clear mechanism to limit effects to the scope using a pragma with the help of the hinthash %H .

    Since "bignum is just a thin wrapper around various modules of the Math::BigInt family" those modules should be modified in a way to check a delegated hinthash when called from a pragma. (N.B. they share the same authors)

    Hence a bug, in my humble opinion.

    Anyway using globals for such profound effects is never a good idea.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    update

    according to perlglossary#P

    • pragma
      A standard module whose practical hints and suggestions are received (and possibly ignored) at compile time. Pragmas are named in all lowercase.

    I'm not sure if bignum qualifies as a pragma, because of it's rather runtime effects.

      Pragmas are named in all lowercase.
      But not everything that is named in all lowercase is a pragma. Take use lib, for instance. Not a pragma; it globally changes @INC. bignum tried to be a pragma, but gave up halfway.
        The definition is indeed fuzzy.

        But in the case of lib I'd opt for pragma because it's a compile time effect and the globality is intended.

        > bignum tried to be a pragma, but gave up halfway.

        I agree.

        For instance a Tie::Scalar on $upgrade and $downgrade to a routine checking the hinthash could do without even changing BigFloat and family.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found