{
    "mode": "perldoc",
    "parameter": "bignum",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/bignum/json",
    "generated": "2026-07-05T13:15:10Z",
    "synopsis": "use bignum;\n$x = 2 + 4.5,\"\\n\";                    # BigFloat 6.5\nprint 2  512 * 0.1,\"\\n\";            # really is what you think it is\nprint inf * inf,\"\\n\";                 # prints inf\nprint NaN * 3,\"\\n\";                   # prints NaN\n{\nno bignum;\nprint 2  256,\"\\n\";                # a normal Perl scalar now\n}\n# for older Perls, import into current package:\nuse bignum qw/hex oct/;\nprint hex(\"0x1234567890123490\"),\"\\n\";\nprint oct(\"01234567890123490\"),\"\\n\";",
    "sections": {
        "NAME": {
            "content": "bignum - Transparent BigNumber support for Perl\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use bignum;\n\n$x = 2 + 4.5,\"\\n\";                    # BigFloat 6.5\nprint 2  512 * 0.1,\"\\n\";            # really is what you think it is\nprint inf * inf,\"\\n\";                 # prints inf\nprint NaN * 3,\"\\n\";                   # prints NaN\n\n{\nno bignum;\nprint 2  256,\"\\n\";                # a normal Perl scalar now\n}\n\n# for older Perls, import into current package:\nuse bignum qw/hex oct/;\nprint hex(\"0x1234567890123490\"),\"\\n\";\nprint oct(\"01234567890123490\"),\"\\n\";\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "All operators (including basic math operations) are overloaded. Integer and floating-point\nconstants are created as proper BigInts or BigFloats, respectively.\n\nIf you do\n\nuse bignum;\n\nat the top of your script, Math::BigFloat and Math::BigInt will be loaded and any constant\nnumber will be converted to an object (Math::BigFloat for floats like 3.1415 and Math::BigInt\nfor integers like 1234).\n\nSo, the following line:\n\n$x = 1234;\n\ncreates actually a Math::BigInt and stores a reference to in $x. This happens transparently and\nbehind your back, so to speak.\n\nYou can see this with the following:\n\nperl -Mbignum -le 'print ref(1234)'\n\nDon't worry if it says Math::BigInt::Lite, bignum and friends will use Lite if it is installed\nsince it is faster for some operations. It will be automatically upgraded to BigInt whenever\nnecessary:\n\nperl -Mbignum -le 'print ref(2255)'\n\nThis also means it is a bad idea to check for some specific package, since the actual contents\nof $x might be something unexpected. Due to the transparent way of bignum \"ref()\" should not be\nnecessary, anyway.\n\nSince Math::BigInt and BigFloat also overload the normal math operations, the following line\nwill still work:\n\nperl -Mbignum -le 'print ref(1234+1234)'\n\nSince numbers are actually objects, you can call all the usual methods from BigInt/BigFloat on\nthem. This even works to some extent on expressions:\n\nperl -Mbignum -le '$x = 1234; print $x->bdec()'\nperl -Mbignum -le 'print 1234->copy()->binc();'\nperl -Mbignum -le 'print 1234->copy()->binc->badd(6);'\nperl -Mbignum -le 'print +(1234)->copy()->binc()'\n\n(Note that print doesn't do what you expect if the expression starts with '(' hence the \"+\")\n\nYou can even chain the operations together as usual:\n\nperl -Mbignum -le 'print 1234->copy()->binc->badd(6);'\n1241\n\nUnder bignum (or bigint or bigrat), Perl will \"upgrade\" the numbers appropriately. This means\nthat:\n\nperl -Mbignum -le 'print 1234+4.5'\n1238.5\n\nwill work correctly. These mixed cases don't do always work when using Math::BigInt or\nMath::BigFloat alone, or at least not in the way normal Perl scalars work.\n\nIf you do want to work with large integers like under \"use integer;\", try \"use bigint;\":\n\nperl -Mbigint -le 'print 1234.5+4.5'\n1238\n\nThere is also \"use bigrat;\" which gives you big rationals:\n\nperl -Mbigrat -le 'print 1234+4.1'\n12381/10\n\nThe entire upgrading/downgrading is still experimental and might not work as you expect or may\neven have bugs. You might get errors like this:\n\nCan't use an undefined value as an ARRAY reference at\n/usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864\n\nThis means somewhere a routine got a BigFloat/Lite but expected a BigInt (or vice versa) and the\nupgrade/downgrad path was missing. This is a bug, please report it so that we can fix it.\n\nYou might consider using just Math::BigInt or Math::BigFloat, since they allow you finer control\nover what get's done in which module/space. For instance, simple loop counters will be\nMath::BigInts under \"use bignum;\" and this is slower than keeping them as Perl scalars:\n\nperl -Mbignum -le 'for ($i = 0; $i < 10; $i++) { print ref($i); }'\n\nPlease note the following does not work as expected (prints nothing), since overloading of '..'\nis not yet possible in Perl (as of v5.8.0):\n\nperl -Mbignum -le 'for (1..2) { print ref($); }'\n",
            "subsections": [
                {
                    "name": "Options",
                    "content": "bignum recognizes some options that can be passed while loading it via use. The options can\n(currently) be either a single letter form, or the long form. The following options exist:\n\na or accuracy\nThis sets the accuracy for all math operations. The argument must be greater than or equal to\nzero. See Math::BigInt's bround() function for details.\n\nperl -Mbignum=a,50 -le 'print sqrt(20)'\n\nNote that setting precision and accuracy at the same time is not possible.\n\np or precision\nThis sets the precision for all math operations. The argument can be any integer. Negative\nvalues mean a fixed number of digits after the dot, while a positive value rounds to this\ndigit left from the dot. 0 or 1 mean round to integer. See Math::BigInt's bfround() function\nfor details.\n\nperl -Mbignum=p,-50 -le 'print sqrt(20)'\n\nNote that setting precision and accuracy at the same time is not possible.\n\nt or trace\nThis enables a trace mode and is primarily for debugging bignum or\nMath::BigInt/Math::BigFloat.\n\nl or lib\nLoad a different math lib, see \"Math Library\".\n\nperl -Mbignum=l,GMP -e 'print 2  512'\n\nCurrently there is no way to specify more than one library on the command line. This means the\nfollowing does not work:\n\nperl -Mbignum=l,GMP,Pari -e 'print 2  512'\n\nThis will be hopefully fixed soon ;)\n\nhex\nOverride the built-in hex() method with a version that can handle big numbers. This overrides\nit by exporting it to the current package. Under Perl v5.10.0 and higher, this is not so\nnecessary, as hex() is lexically overridden in the current scope whenever the bignum pragma is\nactive.\n\noct\nOverride the built-in oct() method with a version that can handle big numbers. This overrides\nit by exporting it to the current package. Under Perl v5.10.0 and higher, this is not so\nnecessary, as oct() is lexically overridden in the current scope whenever the bigint pragma is\nactive.\n\nv or version\nThis prints out the name and version of all modules used and then exits.\n\nperl -Mbignum=v\n"
                },
                {
                    "name": "Methods",
                    "content": "Beside import() and AUTOLOAD() there are only a few other methods.\n\nSince all numbers are now objects, you can use all functions that are part of the BigInt or\nBigFloat API. It is wise to use only the bxxx() notation, and not the fxxx() notation, though.\nThis makes it possible that the underlying object might morph into a different class than\nBigFloat.\n"
                },
                {
                    "name": "Caveats",
                    "content": "But a warning is in order. When using the following to make a copy of a number, only a shallow\ncopy will be made.\n\n$x = 9; $y = $x;\n$x = $y = 7;\n\nIf you want to make a real copy, use the following:\n\n$y = $x->copy();\n\nUsing the copy or the original with overloaded math is okay, e.g. the following work:\n\n$x = 9; $y = $x;\nprint $x + 1, \" \", $y,\"\\n\";     # prints 10 9\n\nbut calling any method that modifies the number directly will result in both the original and\nthe copy being destroyed:\n\n$x = 9; $y = $x;\nprint $x->badd(1), \" \", $y,\"\\n\";        # prints 10 10\n\n$x = 9; $y = $x;\nprint $x->binc(1), \" \", $y,\"\\n\";        # prints 10 10\n\n$x = 9; $y = $x;\nprint $x->bmul(2), \" \", $y,\"\\n\";        # prints 18 18\n\nUsing methods that do not modify, but test the contents works:\n\n$x = 9; $y = $x;\n$z = 9 if $x->iszero();                # works fine\n\nSee the documentation about the copy constructor and \"=\" in overload, as well as the\ndocumentation in BigInt for further details.\n"
                },
                {
                    "name": "inf",
                    "content": "A shortcut to return Math::BigInt->binf(). Useful because Perl does not always handle bareword\n\"inf\" properly.\n\nNaN()\nA shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword\n\"NaN\" properly.\n\ne\n# perl -Mbignum=e -wle 'print e'\n\nReturns Euler's number \"e\", aka exp(1).\n\nPI()\n# perl -Mbignum=PI -wle 'print PI'\n\nReturns PI.\n"
                },
                {
                    "name": "bexp",
                    "content": "bexp($power,$accuracy);\n\nReturns Euler's number \"e\" raised to the appropriate power, to the wanted accuracy.\n\nExample:\n\n# perl -Mbignum=bexp -wle 'print bexp(1,80)'\n"
                },
                {
                    "name": "bpi",
                    "content": "bpi($accuracy);\n\nReturns PI to the wanted accuracy.\n\nExample:\n\n# perl -Mbignum=bpi -wle 'print bpi(80)'\n"
                },
                {
                    "name": "upgrade",
                    "content": "Return the class that numbers are upgraded to, is in fact returning $Math::BigInt::upgrade.\n"
                },
                {
                    "name": "in_effect",
                    "content": "use bignum;\n\nprint \"in effect\\n\" if bignum::ineffect;       # true\n{\nno bignum;\nprint \"in effect\\n\" if bignum::ineffect;     # false\n}\n\nReturns true or false if \"bignum\" is in effect in the current scope.\n\nThis method only works on Perl v5.9.4 or later.\n"
                },
                {
                    "name": "Math Library",
                    "content": "Math with the numbers is done (by default) by a module called Math::BigInt::Calc. This is\nequivalent to saying:\n\nuse bignum lib => 'Calc';\n\nYou can change this by using:\n\nuse bignum lib => 'GMP';\n\nThe following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar, and when this\nalso fails, revert to Math::BigInt::Calc:\n\nuse bignum lib => 'Foo,Math::BigInt::Bar';\n\nPlease see respective module documentation for further details.\n\nUsing \"lib\" warns if none of the specified libraries can be found and Math::BigInt did fall back\nto one of the default libraries. To suppress this warning, use \"try\" instead:\n\nuse bignum try => 'GMP';\n\nIf you want the code to die instead of falling back, use \"only\" instead:\n\nuse bignum only => 'GMP';\n\nINTERNAL FORMAT\nThe numbers are stored as objects, and their internals might change at anytime, especially\nbetween math operations. The objects also might belong to different classes, like Math::BigInt,\nor Math::BigFloat. Mixing them together, even with normal scalars is not extraordinary, but\nnormal and expected.\n\nYou should not depend on the internal format, all accesses must go through accessor methods.\nE.g. looking at $x->{sign} is not a bright idea since there is no guaranty that the object in\nquestion has such a hashkey, nor is a hash underneath at all.\n\nSIGN\nThe sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored separately. You can access it\nwith the sign() method.\n\nA sign of 'NaN' is used to represent the result when input arguments are not numbers or as a\nresult of 0/0. '+inf' and '-inf' represent plus respectively minus infinity. You will get '+inf'\nwhen dividing a positive number by 0, and '-inf' when dividing any negative number by 0.\n"
                }
            ]
        },
        "CAVEATS": {
            "content": "Operator vs literal overloading\n\"bignum\" works by overloading handling of integer and floating point literals, converting them\nto Math::BigInt or Math::BigFloat objects.\n\nThis means that arithmetic involving only string values or string literals will be performed\nusing Perl's built-in operators.\n\nFor example:\n\nuse bignum;\nmy $x = \"900000000000000009\";\nmy $y = \"900000000000000007\";\nprint $x - $y;\n\nwill output 0 on default 32-bit builds, since \"bigrat\" never sees the string literals. To\nensure the expression is all treated as \"Math::BigInt\" or \"BigFloat\" objects, use a literal\nnumber in the expression:\n\nprint +(0+$x) - $y;\n",
            "subsections": [
                {
                    "name": "in_effect",
                    "content": "This method only works on Perl v5.9.4 or later.\n"
                },
                {
                    "name": "hex",
                    "content": "\"bigint\" overrides these routines with versions that can also handle big integer values. Under\nPerl prior to version v5.9.4, however, this will not happen unless you specifically ask for it\nwith the two import tags \"hex\" and \"oct\" - and then it will be global and cannot be disabled\ninside a scope with \"no bigint\":\n\nuse bigint qw/hex oct/;\n\nprint hex(\"0x1234567890123456\");\n{\nno bigint;\nprint hex(\"0x1234567890123456\");\n}\n\nThe second call to hex() will warn about a non-portable constant.\n\nCompare this to:\n\nuse bigint;\n\n# will warn only under older than v5.9.4\nprint hex(\"0x1234567890123456\");\n"
                }
            ]
        },
        "MODULES USED": {
            "content": "\"bignum\" is just a thin wrapper around various modules of the Math::BigInt family. Think of it\nas the head of the family, who runs the shop, and orders the others to do the work.\n\nThe following modules are currently used by bignum:\n\nMath::BigInt::Lite      (for speed, and only if it is loadable)\nMath::BigInt\nMath::BigFloat\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "Some cool command line examples to impress the Python crowd ;)\n\nperl -Mbignum -le 'print sqrt(33)'\nperl -Mbignum -le 'print 2*255'\nperl -Mbignum -le 'print 4.5+2*255'\nperl -Mbignum -le 'print 3/7 + 5/7 + 8/3'\nperl -Mbignum -le 'print 123->isodd()'\nperl -Mbignum -le 'print log(2)'\nperl -Mbignum -le 'print exp(1)'\nperl -Mbignum -le 'print 2  0.5'\nperl -Mbignum=a,65 -le 'print 2  0.2'\nperl -Mbignum=a,65,l,GMP -le 'print 7  7777'\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Please report any bugs or feature requests to \"bug-math-bigint at rt.cpan.org\", or through the\nweb interface at <https://rt.cpan.org/Ticket/Create.html?Queue=bignum> (requires login). We will\nbe notified, and then you'll automatically be notified of progress on your bug as I make\nchanges.\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "You can find documentation for this module with the perldoc command.\n\nperldoc bignum\n\nYou can also look for information at:\n\n*   RT: CPAN's request tracker\n\n<https://rt.cpan.org/Public/Dist/Display.html?Name=bignum>\n\n*   AnnoCPAN: Annotated CPAN documentation\n\n<http://annocpan.org/dist/bignum>\n\n*   CPAN Ratings\n\n<http://cpanratings.perl.org/dist/bignum>\n\n*   Search CPAN\n\n<http://search.cpan.org/dist/bignum/>\n\n*   CPAN Testers Matrix\n\n<http://matrix.cpantesters.org/?dist=bignum>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This program is free software; you may redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "bigint and bigrat.\n\nMath::BigInt, Math::BigFloat, Math::BigRat and Math::Big as well as Math::BigInt::FastCalc,\nMath::BigInt::Pari and Math::BigInt::GMP.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   (C) by Tels <http://bloodgate.com/> in early 2002 - 2007.\n\n*   Maintained by Peter John Acklam <pjacklam@gmail.com<gt>, 2014-.\n",
            "subsections": []
        }
    },
    "summary": "bignum - Transparent BigNumber support for Perl",
    "flags": [],
    "examples": [
        "Some cool command line examples to impress the Python crowd ;)",
        "perl -Mbignum -le 'print sqrt(33)'",
        "perl -Mbignum -le 'print 2*255'",
        "perl -Mbignum -le 'print 4.5+2*255'",
        "perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'",
        "perl -Mbignum -le 'print 123->isodd()'",
        "perl -Mbignum -le 'print log(2)'",
        "perl -Mbignum -le 'print exp(1)'",
        "perl -Mbignum -le 'print 2  0.5'",
        "perl -Mbignum=a,65 -le 'print 2  0.2'",
        "perl -Mbignum=a,65,l,GMP -le 'print 7  7777'"
    ],
    "see_also": []
}