Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: How does Math::GMP overload the assignment operator ? (more guessing)

by syphilis (Archbishop)
on Nov 09, 2020 at 14:36 UTC ( [id://11123526]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How does Math::GMP overload the assignment operator ? (more guessing)
in thread How does Math::GMP overload the assignment operator ?

That sub will be called whenever an object is used in a context that expects a number.

The only way I can find of having that sub called is by doing int($obj).
Do you have an example of another way ?

On Windows (where sizeof(long) is 4):
C:\>perl -MMath::GMP -le "print int(Math::GMP->new(311) ** 100);" 385227681
Math::GMPz achieves the same result with:
C:\>perl -MMath::GMPz=":mpz" -le "print Rmpz_get_ui(Math::GMPz->new(31 +1) ** 100);" 385227681 alternatively: C:\>perl -MMath::GMPz -le "$x = (Math::GMPz->new(311) ** 100) & 429496 +7295; print $x;" 385227681
though, in this very last example, $x is a Math::GMPz object, not an IV.

Cheers,
Rob

Replies are listed 'Best First'.
Re^5: How does Math::GMP overload the assignment operator ? (more guessing)
by jcb (Parson) on Nov 10, 2020 at 04:09 UTC

    If there were arithmetic operators that were not overloaded, Perl would invoke numeric conversion before using the builtin arithmetic operations, but I expect Math::GMP to overload all of them, so int may be the only built-in that actually invokes the numeric conversion.

    I expect (but am not certain) that the '0+' overload is also invoked if an XSUB attempts to convert an object to an IV or UV. This is probably (but I have not traced the code) also how builtin arithmetic operations (which are not used on Math::GMP objects) would end up invoking it.

      I expect (but am not certain) that the '0+' overload is also invoked if an XSUB attempts to convert an object to an IV or UV

      No, I'm quite certain that doesn't happen. (And if I'm wrong about that, then I'm also now quite certain that I'll be told about it ;-)
      It's just that the '0+' overloading calls an XSUB that converts the value in the object to a signed/unsigned long and returns that signed/unsigned long as an IV/UV.
      Of course, you can always call intify or uintify (though the Math::GMP docs recommend avoiding the latter) without invoking any overloading.

      Still not sure if calling int($obj) is the only way to activate the overload '0+' sub, but it's the only way that I could find.
      There's probably good lessons to be learned from working through this, but I think I'll give it a miss for now, mainly because I don't see much value in what this Math::GMP overloading of '0+' provides.

      Interesting stuff, but.

      UPDATE: I took a look at Math::BigInt, and found that the '0+' overloading allows stuff like:
      C:\>perl -MMath::GMP -wle "@x = qw(a b c d e f g h i j k l m n); print + $x[Math::GMP->new(-2)];" m
      Can do the same with Math::GMPz ... not exactly sure how it works there (as there's no explicit '0+' overloading) :
      C:\>perl -MMath::GMPz -wle "@x = ('a' .. 'n'); for(Math::GMPz->new(0) +.. Math::GMPz->new(@x - 1)) { print $x[$_] }" a b c d e f g h i j k l m n
      UPDATE 2: This capability of '0+' overloading definitely provides a useful feature.
      And then Haarg has also pointed out that it contributes to sprintf() and pack() functionality - which adds to its usefulness.

      Cheers,
      Rob
        sprintf and pack can trigger the 0+ overload, even if all other ops are overloaded.

        Is there a string conversion overload in Math::GMPz? Perl has quite a few ways to construct overloaded operators from other operators, and one possibility is to first convert the object to a string and then use the builtin string->number conversion.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found