{
    "mode": "perldoc",
    "parameter": "Math::GMP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Math%3A%3AGMP/json",
    "generated": "2026-06-11T12:16:49Z",
    "synopsis": "use Math::GMP;\nmy $n = Math::GMP->new('2');\n$n = $n  (256*1024);\n$n = $n - 1;\nprint \"n is now $n\\n\";",
    "sections": {
        "NAME": {
            "content": "Math::GMP - High speed arbitrary size integer math\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.24\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Math::GMP;\nmy $n = Math::GMP->new('2');\n\n$n = $n  (256*1024);\n$n = $n - 1;\nprint \"n is now $n\\n\";\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Math::GMP was designed to be a drop-in replacement both for Math::BigInt and for regular integer\narithmetic. Unlike BigInt, though, Math::GMP uses the GNU gmp library for all of its\ncalculations, as opposed to straight Perl functions. This can result in speed improvements.\n\nThe downside is that this module requires a C compiler to install -- a small tradeoff in most\ncases. Also, this module is not 100% compatible with Math::BigInt.\n\nA Math::GMP object can be used just as a normal numeric scalar would be -- the module overloads\nmost of the normal arithmetic operators to provide as seamless an interface as possible.\nHowever, if you need a perfect interface, you can do the following:\n\nuse Math::GMP qw(:constant);\n\n$n = 2  (256 * 1024);\nprint \"n is $n\\n\";\n\nThis would fail without the ':constant' since Perl would use normal doubles to compute the\n250,000 bit number, and thereby overflow it into meaninglessness (smaller exponents yield less\naccurate data due to floating point rounding).\n",
            "subsections": []
        },
        "METHODS": {
            "content": "Although the non-overload interface is not complete, the following functions do exist:\n\nnew\n$x = Math::GMP->new(123);\n\nCreates a new Math::GMP object from the passed string or scalar.\n\n$x = Math::GMP->new('abcd', 36);\n\nCreates a new Math::GMP object from the first parameter which should be represented in the base\nspecified by the second parameter.\n\nbfac\n$x = Math::GMP->new(5);\nmy $val = $x->bfac();      # 1*2*3*4*5 = 120\nprint $val;\n\nCalculates the factorial of $x and returns the result.\n\n$n->bnok($k)\n$x = Math::GMP->new(5);\nmy $val = $x->bnok(2);      # 1*2*3*4*5/(1*2)/(1*2*3) = 10\nprint $val;\n\nCalculates the binomial coefficient of $n over $k and returns the result. Equals to\n$n!/($k!*($n-$k)!).\n\n( Added in version 2.23 .)\n\nmy $val = $x->band($y, $swap)\n$x = Math::GMP->new(6);\nmy $val = $x->band(3, 0);      # 0b110 & 0b11 = 1\nprint $val;\n\nCalculates the bit-wise AND of its two arguments and returns the result. $swap should be\nprovided but is ignored.\n\nmy $ret = $x->bxor($y, $swap);\n$x = Math::GMP->new(6);\nmy $val = $x->bxor(3, 0);      # 0b110 ^ 0b11 = 0b101\nprint $val;\n\nCalculates the bit-wise XOR of its two arguments and returns the result.\n\nmy $ret = $x->bior($y, $swap);\n$x = Math::GMP->new(6);\nmy $val = $x->bior(3);      # 0b110 | 0b11 = 0b111\nprint $val;\n\nCalculates the bit-wise OR of its two arguments and returns the result.\n\nblshift\n$x = Math::GMP->new(0b11);\nmy $result = $x->blshift(4, 0);\n# $result = 0b11 << 4 = 0b110000\n\nCalculates the bit-wise left-shift of its two arguments and returns the result. Second argument\nis swap.\n\nbrshift\n$x = Math::GMP->new(0b11001);\nmy $result = $x->brshift(3, 0);\n# $result = 0b11001 << 3 = 0b11\n\nCalculates the bit-wise right-shift of its two arguments and returns the result. Second argument\nis swap.\n\nbgcd\nmy $x = Math::GMP->new(6);\nmy $gcd = $x->bgcd(4);\n# 6 / 2 = 3, 4 / 2 = 2 => 2\nprint $gcd\n\nReturns the Greatest Common Divisor of the two arguments.\n\nblcm\nmy $x = Math::GMP->new(6);\nmy $lcm = $x->blcm(4);      # 6 * 2 = 12, 4 * 3 = 12 => 12\nprint $lcm;\n\nReturns the Least Common Multiple of the two arguments.\n\nbmodinv\nmy $x = Math::GMP->new(5);\nmy $modinv = $x->bmodinv(7);   # 5 * 3 == 1 (mod 7) => 3\nprint $modinv;\n\nReturns the modular inverse of $x (mod $y), if defined. This currently returns 0 if there is no\ninverse (but that may change in the future). Behaviour is undefined when $y is 0.\n\nbroot\nmy $x = Math::GMP->new(100);\nmy $root = $x->root(3);    # int(100  (1/3)) => 4\nprint $root;\n\nReturns the integer n'th root of its argument, given a positive integer n.\n\nbrootrem\nmy $x = Math::GMP->new(100);\nmy($root, $rem) = $x->rootrem(3); # 4  3 + 36 = 100\nprint \"$x is $rem more than the cube of $root\";\n\nReturns the integer n'th root of its argument, and the difference such that \" $root  $n + $rem\n== $x \".\n\nbsqrt\nmy $x = Math::GMP->new(6);\nmy $root = $x->bsqrt();      # int(sqrt(6)) => 2\nprint $root;\n\nReturns the integer square root of its argument.\n\nbsqrtrem\nmy $x = Math::GMP->new(7);\nmy($root, $rem) = $x->sqrtrem(); # 2  2 + 3 = 7\nprint \"$x is $rem more than the square of $root\";\n\nReturns the integer square root of its argument, and the difference such that \" $root  2 +\n$rem == $x \".\n\nisperfectpower\nmy $x = Math::GMP->new(100);\nmy $ispower = $x->isperfectpower();\nprint \"$x is \" . ($ispower ? \"\" : \"not \") . \"a perfect power\";\n\nReturns \"TRUE\" if its argument is a power, ie if there exist integers a and b with b > 1 such\nthat \" $x == $a  $b \".\n\nisperfectsquare\nmy $x = Math::GMP->new(100);\nmy $issquare = $x->isperfectsquare();\nprint \"$x is \" . ($issquare ? \"\" : \"not \") . \"a perfect square\";\n\nReturns \"TRUE\" if its argument is the square of an integer.\n\nlegendre\n$x = Math::GMP->new(6);\nmy $ret = $x->legendre(3);\n\nReturns the value of the Legendre symbol ($x/$y). The value is defined only when $y is an odd\nprime; when the value is not defined, this currently returns 0 (but that may change in the\nfuture).\n\njacobi\nmy $x = Math::GMP->new(6);\nmy $jacobiverdict = $x->jacobi(3);\n\nReturns the value of the Jacobi symbol ($x/$y). The value is defined only when $y is odd; when\nthe value is not defined, this currently returns 0 (but that may change in the future).\n\nfibonacci\nmy $fib = Math::GMP::fibonacci(16);\n\nCalculates the n'th number in the Fibonacci sequence.\n\nprobabprime\nmy $x = Math::GMP->new(7);\nmy $isprimeverdict = $x->probabprime(10);\n\nProbabilistically determines if the number is a prime. Argument is the number of checks to\nperform. Returns 0 if the number is definitely not a prime, 1 if it may be, and 2 if it\ndefinitely is a prime.\n\n$x->adduigmp($n)\nAdds to $x and mutates it in-place. $n must be a regular non-GMP, positive, integer.\n\n($quotient, $remainder) = $x->bdiv($y);\nmy $x = Math::GMP->new(7);\nmy ($quo, $rem) = $x->bdiv(3);\n\nReturns both the division and the modulo of an integer division operation.\n\nmy $ret = $x->div2expgmp($n);\nmy $x = Math::GMP->new(200);\nmy $ret = $x->div2expgmp(2);\n\nReturns a right-shift of the Math::GMP object by an unsigned regular integer. Also look at",
            "subsections": [
                {
                    "name": "blshift",
                    "content": "my $str = $x->getstrgmp($base)\nmy $initn = 3 * 7 + 2 * 7 * 7 + 6 * 7 * 7 * 7;\nmy $x = Math::GMP->new($initn);\nmy $ret = $x->getstrgmp(7);\n\nprint $ret; # Prints \"6230\".\n\nReturns a string representation of the number in base $base.\n\nmy $clone = $x->gmpcopy()\nReturns a copy of $x that can be modified without affecting the original.\n\nmy $verdict = $x->gmptstbit($bitindex);\nReturns whether or not bit No. $bitindex is 1 in $x.\n\nmy $remainder = $dividend->mmodgmp($divisor)\nmy $x = Math::GMP->new(2 . ('0' x 200) . 4);\nmy $y = Math::GMP->new(5);\n\nmy $ret = $x->mmodgmp($y);\n# $ret is now Math::GMP of 4.\n\nFrom the GMP documentation:\n\nDivide dividend and divisor and put the remainder in remainder. The remainder is always\npositive, and its value is less than the value of the divisor.\n\nmy $result = $x->mod2expgmp($shift);\nmy $x = Math::GMP->new(0b10001011);\nmy $ret = $x->mod2expgmp(4);\n\n# $ret is now Math::GMP of 0b1011\n\nReturns a Math::GMP object containing the lower $shift bits of $x (while not modifying $x).\n\nmy $leftshifted = $x->mul2expgmp($shift);\nmy $x = Math::GMP->new(0b10001011);\nmy $ret = $x->mul2expgmp(4);\n\n# $ret is now Math::GMP of 0b100010110000\n\nReturns a Math::GMP object containing $x shifted by $shift bits (where $shift is a plain\ninteger).\n\nmy $multiplied = $x->bmulf($float)\nmy $x = Math::GMP->new(3)->bpow(100);\nmy $ret = $x->bmulf(1.5);\n\n# $ret is now Math::GMP of floor(3^101 / 2)\n\nReturns a Math::GMP object representing $x multiplied by the floating point value $float (with\nthe result truncated towards zero).\n\n( Added in version 2.23 .)\n\nmy $ret = $base->powmgmp($exp, $mod);\nmy $base = Math::GMP->new(157);\nmy $exp = Math::GMP->new(100);\nmy $mod = Math::GMP->new(5013);\n\nmy $ret = $base->powmgmp($exp, $mod);\n\n# $ret is now (($base  $exp) % $mod)\n\nReturns $base raised to the power of $exp modulo $mod.\n\nmy $plainintret = $x->sizeinbasegmp($plainintbase);\nReturns the size of $x in base $plainintbase .\n\nmy $int = $x->intify();\nReturns the value of the object as an unblessed (and limited-in-precision) integer.\n\ngmpbuildversion()\nmy $gmpversion = Math::GMP::gmpbuildversion;\nif ($gmpversion ge 6.0.0) {\nprint \"Math::GMP was built against libgmp-6.0.0 or later\";\n}\n\nClass method that returns as a vstring the version of libgmp against which this module was\nbuilt.\n\ngmplibversion()\nmy $gmpversion = Math::GMP::gmplibversion;\nif ($gmpversion ge 6.0.0) {\nprint \"Math::GMP is now running with libgmp-6.0.0 or later\";\n}\n\nClass method that returns as a vstring the version of libgmp it is currently running.\n\ngcd()\nAn alias to bgcd() .\n\nlcm()\nAn alias to blcm() .\n\nconstant\nFor internal use. Do not use directly.\n\ndestroy\nFor internal use. Do not use directly.\n\nnewfromscalar\nFor internal use. Do not use directly.\n\nnewfromscalarwithbase\nFor internal use. Do not use directly.\n\nopadd\nFor internal use. Do not use directly.\n\nopdiv\nFor internal use. Do not use directly.\n\nopeq\nFor internal use. Do not use directly.\n\nopmod\nFor internal use. Do not use directly.\n\nopmul\nFor internal use. Do not use directly.\n\noppow\nFor internal use. Do not use directly.\n\nopspaceship\nFor internal use. Do not use directly.\n\nopsub\nFor internal use. Do not use directly.\n\nstringify\nFor internal use. Do not use directly.\n\nuintify\nFor internal use. Do not use directly.\n"
                }
            ]
        },
        "DIVISION BY ZERO": {
            "content": "Whereas perl normally catches division by zero to provide a standard perl-level error message,\n\"libgmp\" does not; the result is usually a SIGFPE (floating point exception) giving a core dump\nif you ever attempt to divide a \"Math::GMP\" object by anything that evaluates to zero. This can\nmake it hard to diagnose where the error has occurred in your perl code.\n\nAs of perl-5.36.0, SIGFPE is delivered in a way that can be caught by a %SIG handler. So you can\nget a stack trace with code like:\n\nuse Carp;  # load it up front\nlocal $SIG{FPE} = sub { confess(@) };\n\nBefore perl-5.36.0 this approach won't work: you'll need to use \"sigaction\" in POSIX instead:\n\nuse Carp;\nuse POSIX qw{ sigaction SIGFPE };\nsigaction(SIGFPE, POSIX::SigAction->new(sub { confess(@) }));\n\nIn either case, you should not attempt to return from the signal handler, since the signal will\njust be thrown again.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Please report any bugs or feature requests on the bugtracker website\n<https://rt.cpan.org/Public/Dist/Display.html?Name=Math-GMP> or by email to\nbug-math-gmp@rt.cpan.org <mailto:bug-math-gmp@rt.cpan.org>.\n\nWhen submitting a bug or request, please include a test-file or a patch to an existing test-file\nthat illustrates the bug or desired feature.\n",
            "subsections": []
        },
        "VERSION CONTROL": {
            "content": "The version control repository of this module is a git repository hosted on GitHub at:\n<https://github.com/turnstep/Math-GMP>. Pull requests are welcome.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Math::BigInt has a new interface to use a different library than the default pure Perl\nimplementation. You can use, for instance, Math::GMP with it:\n\nuse Math::BigInt lib => 'GMP';\n\nIf Math::GMP is not installed, it will fall back to its own Perl implementation.\n\nSee Math::BigInt and Math::BigInt::GMP or Math::BigInt::Pari or Math::BigInt::BitVect.\n\nSee Math::GMPz, Math::GMPq, and friends ( <https://metacpan.org/search?q=math%3A%3Agmp> ) for\nbindings of other parts of GMP / MPFR / etc.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Shlomi Fish <shlomif@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is Copyright (c) 2000 by James H. Turner.\n\nThis is free software, licensed under:\n\nThe GNU Lesser General Public License, Version 2.1, February 1999\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "",
            "subsections": [
                {
                    "name": "Perldoc",
                    "content": "You can find documentation for this module with the perldoc command.\n\nperldoc Math::GMP\n"
                },
                {
                    "name": "Websites",
                    "content": "The following websites have more information about this module, and may be of help to you. As\nalways, in addition to those websites please use your favorite search engine to discover more\nresources.\n\n*   MetaCPAN\n\nA modern, open-source CPAN search engine, useful to view POD in HTML format.\n\n<https://metacpan.org/release/Math-GMP>\n\n*   RT: CPAN's Bug Tracker\n\nThe RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.\n\n<https://rt.cpan.org/Public/Dist/Display.html?Name=Math-GMP>\n\n*   CPANTS\n\nThe CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.\n\n<http://cpants.cpanauthors.org/dist/Math-GMP>\n\n*   CPAN Testers\n\nThe CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN\ndistributions.\n\n<http://www.cpantesters.org/distro/M/Math-GMP>\n\n*   CPAN Testers Matrix\n\nThe CPAN Testers Matrix is a website that provides a visual overview of the test results for\na distribution on various Perls/platforms.\n\n<http://matrix.cpantesters.org/?dist=Math-GMP>\n\n*   CPAN Testers Dependencies\n\nThe CPAN Testers Dependencies is a website that shows a chart of the test results of all\ndependencies for a distribution.\n\n<http://deps.cpantesters.org/?module=Math::GMP>\n\nBugs / Feature Requests\nPlease report any bugs or feature requests by email to \"bug-math-gmp at rt.cpan.org\", or through\nthe web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Math-GMP>. You will be\nautomatically notified of any progress on the request by the system.\n"
                },
                {
                    "name": "Source Code",
                    "content": "The code is open to the world, and available for you to hack on. Please feel free to browse it\nand play with it, or whatever. If you want to contribute patches, please send me a diff or prod\nme to pull from your repository :)\n\n<https://github.com/turnstep/Math-GMP>\n\ngit clone https://github.com/turnstep/Math-GMP.git\n"
                }
            ]
        }
    },
    "summary": "Math::GMP - High speed arbitrary size integer math",
    "flags": [],
    "examples": [],
    "see_also": []
}