{
    "mode": "perldoc",
    "parameter": "bigrat",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/bigrat/json",
    "generated": "2026-06-16T03:26:06Z",
    "synopsis": "use bigrat;\nprint 2 + 4.5,\"\\n\";                   # BigFloat 6.5\nprint 1/3 + 1/4,\"\\n\";                 # produces 7/12\n{\nno bigrat;\nprint 1/3,\"\\n\";                     # 0.33333...\n}\n# Import into current package:\nuse bigrat qw/hex oct/;\nprint hex(\"0x1234567890123490\"),\"\\n\";\nprint oct(\"01234567890123490\"),\"\\n\";",
    "sections": {
        "NAME": {
            "content": "bigrat - Transparent BigNumber/BigRational support for Perl\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use bigrat;\n\nprint 2 + 4.5,\"\\n\";                   # BigFloat 6.5\nprint 1/3 + 1/4,\"\\n\";                 # produces 7/12\n\n{\nno bigrat;\nprint 1/3,\"\\n\";                     # 0.33333...\n}\n\n# Import into current package:\nuse bigrat 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\nOther than bignum, this module upgrades to Math::BigRat, meaning that instead of 2.5 you will\nget 2+1/2 as output.\n",
            "subsections": [
                {
                    "name": "Modules Used",
                    "content": "\"bigrat\" 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\nMath::BigRat\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 bigrat 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 bigrat lib => 'Foo,Math::BigInt::Bar';\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\nPlease see respective module documentation for further details.\n"
                },
                {
                    "name": "Sign",
                    "content": "The sign is either '+', '-', 'NaN', '+inf' or '-inf'.\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"
                },
                {
                    "name": "Methods",
                    "content": "Since all numbers are not 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 you independent on the fact that the underlying object might morph into a different\nclass than BigFloat.\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 -Mbigrat=e -wle 'print e'\n\nReturns Euler's number \"e\", aka exp(1).\n\nPI\n# perl -Mbigrat=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 -Mbigrat=bexp -wle 'print bexp(1,80)'\n"
                },
                {
                    "name": "bpi",
                    "content": "bpi($accuracy);\n\nReturns PI to the wanted accuracy.\n\nExample:\n\n# perl -Mbigrat=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 bigrat;\n\nprint \"in effect\\n\" if bigrat::ineffect;       # true\n{\nno bigrat;\nprint \"in effect\\n\" if bigrat::ineffect;     # false\n}\n\nReturns true or false if \"bigrat\" is in effect in the current scope.\n\nThis method only works on Perl v5.9.4 or later.\n\nMATH LIBRARY\nMath with the numbers is done (by default) by a module called\n"
                },
                {
                    "name": "Caveat",
                    "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 testthe 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": "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 -Mbigrat=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 -Mbigrat=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 -Mbigrat=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 bigrat 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 bigrat pragma is\nactive.\n\nv or version\nThis prints out the name and version of all modules used and then exits.\n\nperl -Mbigrat=v\n"
                }
            ]
        },
        "CAVEATS": {
            "content": "Operator vs literal overloading\n\"bigrat\" works by overloading handling of integer and floating point literals, converting them\nto Math::BigInt or Math::BigRat 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 bigrat;\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 \"Math::BigRat\" objects, use a\nliteral number 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 Perl older than v5.9.4\nprint hex(\"0x1234567890123456\");\n"
                }
            ]
        },
        "EXAMPLES": {
            "content": "perl -Mbigrat -le 'print sqrt(33)'\nperl -Mbigrat -le 'print 2*255'\nperl -Mbigrat -le 'print 4.5+2*255'\nperl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'\nperl -Mbigrat -le 'print 12->isodd()';\nperl -Mbignum=l,GMP -le 'print 7  7777'\n",
            "subsections": []
        },
        "BUGS": {
            "content": "For information about bugs and how to report them, see the BUGS section in the documentation\navailable with the perldoc command.\n\nperldoc bignum\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "You can find documentation for this module with the perldoc command.\n\nperldoc bigrat\n\nFor more information, see the SUPPORT section in the documentation available with the perldoc\ncommand.\n\nperldoc 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": "bignum and bigint.\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*   Peter John Acklam <pjacklam@gmail.com<gt>, 2014-.\n",
            "subsections": []
        }
    },
    "summary": "bigrat - Transparent BigNumber/BigRational support for Perl",
    "flags": [],
    "examples": [
        "perl -Mbigrat -le 'print sqrt(33)'",
        "perl -Mbigrat -le 'print 2*255'",
        "perl -Mbigrat -le 'print 4.5+2*255'",
        "perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'",
        "perl -Mbigrat -le 'print 12->isodd()';",
        "perl -Mbignum=l,GMP -le 'print 7  7777'"
    ],
    "see_also": []
}