{
    "mode": "perldoc",
    "parameter": "Math::BigRat",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigRat/json",
    "generated": "2026-06-10T16:24:17Z",
    "synopsis": "use Math::BigRat;\nmy $x = Math::BigRat->new('3/7'); $x += '5/9';\nprint $x->bstr(), \"\\n\";\nprint $x  2, \"\\n\";\nmy $y = Math::BigRat->new('inf');\nprint \"$y \", ($y->isinf ? 'is' : 'is not'), \" infinity\\n\";\nmy $z = Math::BigRat->new(144); $z->bsqrt();",
    "sections": {
        "NAME": {
            "content": "Math::BigRat - Arbitrary big rational numbers\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Math::BigRat;\n\nmy $x = Math::BigRat->new('3/7'); $x += '5/9';\n\nprint $x->bstr(), \"\\n\";\nprint $x  2, \"\\n\";\n\nmy $y = Math::BigRat->new('inf');\nprint \"$y \", ($y->isinf ? 'is' : 'is not'), \" infinity\\n\";\n\nmy $z = Math::BigRat->new(144); $z->bsqrt();\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Math::BigRat complements Math::BigInt and Math::BigFloat by providing support for arbitrary big\nrational numbers.\n\nMATH LIBRARY\nYou can change the underlying module that does the low-level math operations by using:\n\nuse Math::BigRat try => 'GMP';\n\nNote: This needs Math::BigInt::GMP installed.\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 Math::BigRat try => 'Foo,Math::BigInt::Bar';\n\nIf you want to get warned when the fallback occurs, replace \"try\" with \"lib\":\n\nuse Math::BigRat lib => 'Foo,Math::BigInt::Bar';\n\nIf you want the code to die instead, replace \"try\" with \"only\":\n\nuse Math::BigRat only => 'Foo,Math::BigInt::Bar';\n",
            "subsections": []
        },
        "METHODS": {
            "content": "Any methods not listed here are derived from Math::BigFloat (or Math::BigInt), so make sure you\ncheck these two modules for further information.\n",
            "subsections": [
                {
                    "name": "new",
                    "content": "$x = Math::BigRat->new('1/3');\n\nCreate a new Math::BigRat object. Input can come in various forms:\n\n$x = Math::BigRat->new(123);                            # scalars\n$x = Math::BigRat->new('inf');                          # infinity\n$x = Math::BigRat->new('123.3');                        # float\n$x = Math::BigRat->new('1/3');                          # simple string\n$x = Math::BigRat->new('1 / 3');                        # spaced\n$x = Math::BigRat->new('1 / 0.1');                      # w/ floats\n$x = Math::BigRat->new(Math::BigInt->new(3));           # BigInt\n$x = Math::BigRat->new(Math::BigFloat->new('3.1'));     # BigFloat\n$x = Math::BigRat->new(Math::BigInt::Lite->new('2'));   # BigLite\n\n# You can also give D and N as different objects:\n$x = Math::BigRat->new(\nMath::BigInt->new(-123),\nMath::BigInt->new(7),\n);                      # => -123/7\n"
                },
                {
                    "name": "numerator",
                    "content": "$n = $x->numerator();\n\nReturns a copy of the numerator (the part above the line) as signed BigInt.\n"
                },
                {
                    "name": "denominator",
                    "content": "$d = $x->denominator();\n\nReturns a copy of the denominator (the part under the line) as positive BigInt.\n"
                },
                {
                    "name": "parts",
                    "content": "($n, $d) = $x->parts();\n\nReturn a list consisting of (signed) numerator and (unsigned) denominator as BigInts.\n"
                },
                {
                    "name": "numify",
                    "content": "my $y = $x->numify();\n\nReturns the object as a scalar. This will lose some data if the object cannot be represented\nby a normal Perl scalar (integer or float), so use \"asint()\" or \"asfloat()\" instead.\n\nThis routine is automatically used whenever a scalar is required:\n\nmy $x = Math::BigRat->new('3/1');\n@array = (0, 1, 2, 3);\n$y = $array[$x];                # set $y to 3\n"
                },
                {
                    "name": "as_int",
                    "content": ""
                },
                {
                    "name": "as_number",
                    "content": "$x = Math::BigRat->new('13/7');\nprint $x->asint(), \"\\n\";               # '1'\n\nReturns a copy of the object as BigInt, truncated to an integer.\n\n\"asnumber()\" is an alias for \"asint()\".\n"
                },
                {
                    "name": "as_float",
                    "content": "$x = Math::BigRat->new('13/7');\nprint $x->asfloat(), \"\\n\";             # '1'\n\n$x = Math::BigRat->new('2/3');\nprint $x->asfloat(5), \"\\n\";            # '0.66667'\n\nReturns a copy of the object as BigFloat, preserving the accuracy as wanted, or the default\nof 40 digits.\n\nThis method was added in v0.22 of Math::BigRat (April 2008).\n"
                },
                {
                    "name": "as_hex",
                    "content": "$x = Math::BigRat->new('13');\nprint $x->ashex(), \"\\n\";               # '0xd'\n\nReturns the BigRat as hexadecimal string. Works only for integers.\n"
                },
                {
                    "name": "as_bin",
                    "content": "$x = Math::BigRat->new('13');\nprint $x->asbin(), \"\\n\";               # '0x1101'\n\nReturns the BigRat as binary string. Works only for integers.\n"
                },
                {
                    "name": "as_oct",
                    "content": "$x = Math::BigRat->new('13');\nprint $x->asoct(), \"\\n\";               # '015'\n\nReturns the BigRat as octal string. Works only for integers.\n"
                },
                {
                    "name": "from_hex",
                    "content": "my $h = Math::BigRat->fromhex('0x10');\n\nCreate a BigRat from a hexadecimal number in string form.\n"
                },
                {
                    "name": "from_oct",
                    "content": "my $o = Math::BigRat->fromoct('020');\n\nCreate a BigRat from an octal number in string form.\n"
                },
                {
                    "name": "from_bin",
                    "content": "my $b = Math::BigRat->frombin('0b10000000');\n\nCreate a BigRat from an binary number in string form.\n"
                },
                {
                    "name": "bnan",
                    "content": "$x = Math::BigRat->bnan();\n\nCreates a new BigRat object representing NaN (Not A Number). If used on an object, it will\nset it to NaN:\n\n$x->bnan();\n"
                },
                {
                    "name": "bzero",
                    "content": "$x = Math::BigRat->bzero();\n\nCreates a new BigRat object representing zero. If used on an object, it will set it to zero:\n\n$x->bzero();\n"
                },
                {
                    "name": "binf",
                    "content": "$x = Math::BigRat->binf($sign);\n\nCreates a new BigRat object representing infinity. The optional argument is either '-' or\n'+', indicating whether you want infinity or minus infinity. If used on an object, it will\nset it to infinity:\n\n$x->binf();\n$x->binf('-');\n"
                },
                {
                    "name": "bone",
                    "content": "$x = Math::BigRat->bone($sign);\n\nCreates a new BigRat object representing one. The optional argument is either '-' or '+',\nindicating whether you want one or minus one. If used on an object, it will set it to one:\n\n$x->bone();                 # +1\n$x->bone('-');              # -1\n"
                },
                {
                    "name": "length",
                    "content": "$len = $x->length();\n\nReturn the length of $x in digits for integer values.\n"
                },
                {
                    "name": "digit",
                    "content": "print Math::BigRat->new('123/1')->digit(1);     # 1\nprint Math::BigRat->new('123/1')->digit(-1);    # 3\n\nReturn the N'ths digit from X when X is an integer value.\n"
                },
                {
                    "name": "bnorm",
                    "content": "$x->bnorm();\n\nReduce the number to the shortest form. This routine is called automatically whenever it is\nneeded.\n"
                },
                {
                    "name": "bfac",
                    "content": "$x->bfac();\n\nCalculates the factorial of $x. For instance:\n\nprint Math::BigRat->new('3/1')->bfac(), \"\\n\";   # 1*2*3\nprint Math::BigRat->new('5/1')->bfac(), \"\\n\";   # 1*2*3*4*5\n\nWorks currently only for integers.\n"
                },
                {
                    "name": "bround",
                    "content": "Are not yet implemented.\n"
                },
                {
                    "name": "bmod",
                    "content": "$x->bmod($y);\n\nReturns $x modulo $y. When $x is finite, and $y is finite and non-zero, the result is\nidentical to the remainder after floored division (F-division). If, in addition, both $x and\n$y are integers, the result is identical to the result from Perl's % operator.\n"
                },
                {
                    "name": "bmodinv",
                    "content": "$x->bmodinv($mod);          # modular multiplicative inverse\n\nReturns the multiplicative inverse of $x modulo $mod. If\n\n$y = $x -> copy() -> bmodinv($mod)\n\nthen $y is the number closest to zero, and with the same sign as $mod, satisfying\n\n($x * $y) % $mod = 1 % $mod\n\nIf $x and $y are non-zero, they must be relative primes, i.e., \"bgcd($y, $mod)==1\". '\"NaN\"'\nis returned when no modular multiplicative inverse exists.\n"
                },
                {
                    "name": "bmodpow",
                    "content": "$num->bmodpow($exp,$mod);           # modular exponentiation\n# ($num$exp % $mod)\n\nReturns the value of $num taken to the power $exp in the modulus $mod using binary\nexponentiation. \"bmodpow\" is far superior to writing\n\n$num  $exp % $mod\n\nbecause it is much faster - it reduces internal variables into the modulus whenever\npossible, so it operates on smaller numbers.\n\n\"bmodpow\" also supports negative exponents.\n\nbmodpow($num, -1, $mod)\n\nis exactly equivalent to\n\nbmodinv($num, $mod)\n"
                },
                {
                    "name": "bneg",
                    "content": "$x->bneg();\n\nUsed to negate the object in-place.\n"
                },
                {
                    "name": "is_one",
                    "content": "print \"$x is 1\\n\" if $x->isone();\n\nReturn true if $x is exactly one, otherwise false.\n"
                },
                {
                    "name": "is_zero",
                    "content": "print \"$x is 0\\n\" if $x->iszero();\n\nReturn true if $x is exactly zero, otherwise false.\n"
                },
                {
                    "name": "is_pos",
                    "content": "print \"$x is >= 0\\n\" if $x->ispositive();\n\nReturn true if $x is positive (greater than or equal to zero), otherwise false. Please note\nthat '+inf' is also positive, while 'NaN' and '-inf' aren't.\n\n\"ispositive()\" is an alias for \"ispos()\".\n"
                },
                {
                    "name": "is_neg",
                    "content": "print \"$x is < 0\\n\" if $x->isnegative();\n\nReturn true if $x is negative (smaller than zero), otherwise false. Please note that '-inf'\nis also negative, while 'NaN' and '+inf' aren't.\n\n\"isnegative()\" is an alias for \"isneg()\".\n"
                },
                {
                    "name": "is_int",
                    "content": "print \"$x is an integer\\n\" if $x->isint();\n\nReturn true if $x has a denominator of 1 (e.g. no fraction parts), otherwise false. Please\nnote that '-inf', 'inf' and 'NaN' aren't integer.\n"
                },
                {
                    "name": "is_odd",
                    "content": "print \"$x is odd\\n\" if $x->isodd();\n\nReturn true if $x is odd, otherwise false.\n"
                },
                {
                    "name": "is_even",
                    "content": "print \"$x is even\\n\" if $x->iseven();\n\nReturn true if $x is even, otherwise false.\n"
                },
                {
                    "name": "bceil",
                    "content": "$x->bceil();\n\nSet $x to the next bigger integer value (e.g. truncate the number to integer and then\nincrement it by one).\n"
                },
                {
                    "name": "bfloor",
                    "content": "$x->bfloor();\n\nTruncate $x to an integer value.\n"
                },
                {
                    "name": "bint",
                    "content": "$x->bint();\n\nRound $x towards zero.\n"
                },
                {
                    "name": "bsqrt",
                    "content": "$x->bsqrt();\n\nCalculate the square root of $x.\n"
                },
                {
                    "name": "broot",
                    "content": "$x->broot($n);\n\nCalculate the N'th root of $x.\n"
                },
                {
                    "name": "badd",
                    "content": "$x->badd($y);\n\nAdds $y to $x and returns the result.\n"
                },
                {
                    "name": "bmul",
                    "content": "$x->bmul($y);\n\nMultiplies $y to $x and returns the result.\n"
                },
                {
                    "name": "bsub",
                    "content": "$x->bsub($y);\n\nSubtracts $y from $x and returns the result.\n"
                },
                {
                    "name": "bdiv",
                    "content": "$q = $x->bdiv($y);\n($q, $r) = $x->bdiv($y);\n\nIn scalar context, divides $x by $y and returns the result. In list context, does floored\ndivision (F-division), returning an integer $q and a remainder $r so that $x = $q * $y + $r.\nThe remainer (modulo) is equal to what is returned by \"$x-\"bmod($y)>.\n"
                },
                {
                    "name": "bdec",
                    "content": "$x->bdec();\n\nDecrements $x by 1 and returns the result.\n"
                },
                {
                    "name": "binc",
                    "content": "$x->binc();\n\nIncrements $x by 1 and returns the result.\n"
                },
                {
                    "name": "copy",
                    "content": "my $z = $x->copy();\n\nMakes a deep copy of the object.\n\nPlease see the documentation in Math::BigInt for further details.\n"
                },
                {
                    "name": "bstr",
                    "content": "my $x = Math::BigRat->new('8/4');\nprint $x->bstr(), \"\\n\";             # prints 1/2\nprint $x->bsstr(), \"\\n\";            # prints 1/2\n\nReturn a string representing this object.\n"
                },
                {
                    "name": "bcmp",
                    "content": "$x->bcmp($y);\n\nCompares $x with $y and takes the sign into account. Returns -1, 0, 1 or undef.\n"
                },
                {
                    "name": "bacmp",
                    "content": "$x->bacmp($y);\n\nCompares $x with $y while ignoring their sign. Returns -1, 0, 1 or undef.\n"
                },
                {
                    "name": "beq",
                    "content": "$x -> beq($y);\n\nReturns true if and only if $x is equal to $y, and false otherwise.\n"
                },
                {
                    "name": "bne",
                    "content": "$x -> bne($y);\n\nReturns true if and only if $x is not equal to $y, and false otherwise.\n"
                },
                {
                    "name": "blt",
                    "content": "$x -> blt($y);\n\nReturns true if and only if $x is equal to $y, and false otherwise.\n"
                },
                {
                    "name": "ble",
                    "content": "$x -> ble($y);\n\nReturns true if and only if $x is less than or equal to $y, and false otherwise.\n"
                },
                {
                    "name": "bgt",
                    "content": "$x -> bgt($y);\n\nReturns true if and only if $x is greater than $y, and false otherwise.\n"
                },
                {
                    "name": "bge",
                    "content": "$x -> bge($y);\n\nReturns true if and only if $x is greater than or equal to $y, and false otherwise.\n"
                },
                {
                    "name": "blsft",
                    "content": "Used to shift numbers left/right.\n\nPlease see the documentation in Math::BigInt for further details.\n"
                },
                {
                    "name": "band",
                    "content": "$x->band($y);               # bitwise and\n"
                },
                {
                    "name": "bior",
                    "content": "$x->bior($y);               # bitwise inclusive or\n"
                },
                {
                    "name": "bxor",
                    "content": "$x->bxor($y);               # bitwise exclusive or\n"
                },
                {
                    "name": "bnot",
                    "content": "$x->bnot();                 # bitwise not (two's complement)\n"
                },
                {
                    "name": "bpow",
                    "content": "$x->bpow($y);\n\nCompute $x  $y.\n\nPlease see the documentation in Math::BigInt for further details.\n"
                },
                {
                    "name": "blog",
                    "content": "$x->blog($base, $accuracy);         # logarithm of x to the base $base\n\nIf $base is not defined, Euler's number (e) is used:\n\nprint $x->blog(undef, 100);         # log(x) to 100 digits\n"
                },
                {
                    "name": "bexp",
                    "content": "$x->bexp($accuracy);        # calculate e  X\n\nCalculates two integers A and B so that A/B is equal to \"e  $x\", where \"e\" is Euler's\nnumber.\n\nThis method was added in v0.20 of Math::BigRat (May 2007).\n\nSee also \"blog()\".\n"
                },
                {
                    "name": "bnok",
                    "content": "$x->bnok($y);               # x over y (binomial coefficient n over k)\n\nCalculates the binomial coefficient n over k, also called the \"choose\" function. The result\nis equivalent to:\n\n( n )      n!\n| - |  = -------\n( k )    k!(n-k)!\n\nThis method was added in v0.20 of Math::BigRat (May 2007).\n"
                },
                {
                    "name": "config",
                    "content": "Math::BigRat->config(\"trapnan\" => 1);      # set\n$accu = Math::BigRat->config(\"accuracy\");   # get\n\nSet or get configuration parameter values. Read-only parameters are marked as RO. Read-write\nparameters are marked as RW. The following parameters are supported.\n\nParameter       RO/RW   Description\nExample\n============================================================\nlib             RO      Name of the math backend library\nMath::BigInt::Calc\nlibversion     RO      Version of the math backend library\n0.30\nclass           RO      The class of config you just called\nMath::BigRat\nversion         RO      version number of the class you used\n0.10\nupgrade         RW      To which class numbers are upgraded\nundef\ndowngrade       RW      To which class numbers are downgraded\nundef\nprecision       RW      Global precision\nundef\naccuracy        RW      Global accuracy\nundef\nroundmode      RW      Global round mode\neven\ndivscale       RW      Fallback accuracy for div, sqrt etc.\n40\ntrapnan        RW      Trap NaNs\nundef\ntrapinf        RW      Trap +inf/-inf\nundef\n"
                }
            ]
        },
        "BUGS": {
            "content": "Please report any bugs or feature requests to \"bug-math-bigrat at rt.cpan.org\", or through the\nweb interface at <https://rt.cpan.org/Ticket/Create.html?Queue=Math-BigRat> (requires login). We\nwill be 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 Math::BigRat\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=Math-BigRat>\n\n*   AnnoCPAN: Annotated CPAN documentation\n\n<http://annocpan.org/dist/Math-BigRat>\n\n*   CPAN Ratings\n\n<http://cpanratings.perl.org/dist/Math-BigRat>\n\n*   Search CPAN\n\n<http://search.cpan.org/dist/Math-BigRat/>\n\n*   CPAN Testers Matrix\n\n<http://matrix.cpantesters.org/?dist=Math-BigRat>\n\n*   The Bignum mailing list\n\n*   Post to mailing list\n\n\"bignum at lists.scsys.co.uk\"\n\n*   View mailing list\n\n<http://lists.scsys.co.uk/pipermail/bignum/>\n\n*   Subscribe/Unsubscribe\n\n<http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/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": "bigrat, Math::BigFloat and Math::BigInt as well as the backends Math::BigInt::FastCalc,\nMath::BigInt::GMP, and Math::BigInt::Pari.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Tels <http://bloodgate.com/> 2001-2009.\n\n*   Maintained by Peter John Acklam <pjacklam@online.no> 2011-\n",
            "subsections": []
        }
    },
    "summary": "Math::BigRat - Arbitrary big rational numbers",
    "flags": [],
    "examples": [],
    "see_also": []
}