.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Math::BigRat 3" .TH Math::BigRat 3 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Math::BigRat \- arbitrary size rational number math package .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Math::BigRat; \& \& # Generic constructor method (always returns a new object) \& \& $x = Math::BigRat\->new($str); # defaults to 0 \& $x = Math::BigRat\->new(\*(Aq256\*(Aq); # from decimal \& $x = Math::BigRat\->new(\*(Aq0256\*(Aq); # from decimal \& $x = Math::BigRat\->new(\*(Aq0xcafe\*(Aq); # from hexadecimal \& $x = Math::BigRat\->new(\*(Aq0x1.fap+7\*(Aq); # from hexadecimal \& $x = Math::BigRat\->new(\*(Aq0o377\*(Aq); # from octal \& $x = Math::BigRat\->new(\*(Aq0o1.35p+6\*(Aq); # from octal \& $x = Math::BigRat\->new(\*(Aq0b101\*(Aq); # from binary \& $x = Math::BigRat\->new(\*(Aq0b1.101p+3\*(Aq); # from binary \& \& # Specific constructor methods (no prefix needed; when used as \& # instance method, the value is assigned to the invocand) \& \& $x = Math::BigRat\->from_dec(\*(Aq234\*(Aq); # from decimal \& $x = Math::BigRat\->from_hex(\*(Aqcafe\*(Aq); # from hexadecimal \& $x = Math::BigRat\->from_hex(\*(Aq1.fap+7\*(Aq); # from hexadecimal \& $x = Math::BigRat\->from_oct(\*(Aq377\*(Aq); # from octal \& $x = Math::BigRat\->from_oct(\*(Aq1.35p+6\*(Aq); # from octal \& $x = Math::BigRat\->from_bin(\*(Aq1101\*(Aq); # from binary \& $x = Math::BigRat\->from_bin(\*(Aq1.101p+3\*(Aq); # from binary \& $x = Math::BigRat\->from_bytes($bytes); # from byte string \& $x = Math::BigRat\->from_base(\*(Aqwhy\*(Aq, 36); # from any base \& $x = Math::BigRat\->from_base_num([1, 0], 2); # from any base \& $x = Math::BigRat\->from_ieee754($b, $fmt); # from IEEE\-754 bytes \& $x = Math::BigRat\->bzero(); # create a +0 \& $x = Math::BigRat\->bone(); # create a +1 \& $x = Math::BigRat\->bone(\*(Aq\-\*(Aq); # create a \-1 \& $x = Math::BigRat\->binf(); # create a +inf \& $x = Math::BigRat\->binf(\*(Aq\-\*(Aq); # create a \-inf \& $x = Math::BigRat\->bnan(); # create a Not\-A\-Number \& $x = Math::BigRat\->bpi(); # returns pi \& \& $y = $x\->copy(); # make a copy (unlike $y = $x) \& $y = $x\->as_int(); # return as a Math::BigInt \& $y = $x\->as_float(); # return as a Math::BigFloat \& $y = $x\->as_rat(); # return as a Math::BigRat \& \& # Boolean methods (these don\*(Aqt modify the invocand) \& \& $x\->is_zero(); # true if $x is 0 \& $x\->is_one(); # true if $x is +1 \& $x\->is_one("+"); # true if $x is +1 \& $x\->is_one("\-"); # true if $x is \-1 \& $x\->is_inf(); # true if $x is +inf or \-inf \& $x\->is_inf("+"); # true if $x is +inf \& $x\->is_inf("\-"); # true if $x is \-inf \& $x\->is_nan(); # true if $x is NaN \& \& $x\->is_finite(); # true if \-inf < $x < inf \& $x\->is_positive(); # true if $x > 0 \& $x\->is_pos(); # true if $x > 0 \& $x\->is_negative(); # true if $x < 0 \& $x\->is_neg(); # true if $x < 0 \& $x\->is_non_positive() # true if $x <= 0 \& $x\->is_non_negative() # true if $x >= 0 \& \& $x\->is_odd(); # true if $x is odd \& $x\->is_even(); # true if $x is even \& $x\->is_int(); # true if $x is an integer \& \& # Comparison methods (these don\*(Aqt modify the invocand) \& \& $x\->bcmp($y); # compare numbers (undef, < 0, == 0, > 0) \& $x\->bacmp($y); # compare abs values (undef, < 0, == 0, > 0) \& $x\->beq($y); # true if $x == $y \& $x\->bne($y); # true if $x != $y \& $x\->blt($y); # true if $x < $y \& $x\->ble($y); # true if $x <= $y \& $x\->bgt($y); # true if $x > $y \& $x\->bge($y); # true if $x >= $y \& \& # Arithmetic methods (these modify the invocand) \& \& $x\->bneg(); # negation \& $x\->babs(); # absolute value \& $x\->bsgn(); # sign function (\-1, 0, 1, or NaN) \& $x\->bdigitsum(); # sum of decimal digits \& $x\->binc(); # increment $x by 1 \& $x\->bdec(); # decrement $x by 1 \& $x\->badd($y); # addition (add $y to $x) \& $x\->bsub($y); # subtraction (subtract $y from $x) \& $x\->bmul($y); # multiplication (multiply $x by $y) \& $x\->bmuladd($y, $z); # $x = $x * $y + $z \& $x\->bdiv($y); # division (floored) \& $x\->bmod($y); # modulus (x % y) \& $x\->bmodinv($mod); # modular multiplicative inverse \& $x\->bmodpow($y, $mod); # modular exponentiation (($x ** $y) % $mod) \& $x\->btdiv($y); # division (truncated), set $x to quotient \& $x\->btmod($y); # modulus (truncated) \& $x\->binv() # inverse (1/$x) \& $x\->bpow($y); # power of arguments (x ** y) \& $x\->blog(); # logarithm of $x to base e (Euler\*(Aqs number) \& $x\->blog($base); # logarithm of $x to base $base (e.g., base 2) \& $x\->bexp(); # calculate e ** $x where e is Euler\*(Aqs number \& $x\->bilog2(); # log2($x) rounded down to nearest int \& $x\->bilog10(); # log10($x) rounded down to nearest int \& $x\->bclog2(); # log2($x) rounded up to nearest int \& $x\->bclog10(); # log10($x) rounded up to nearest int \& $x\->bnok($y); # combinations (binomial coefficient n over k) \& $x\->bperm($y); # permutations \& $x\->buparrow($n, $y); # Knuth\*(Aqs up\-arrow notation \& $x\->bhyperop($n, $y); # n\*(Aqth hyperoprator \& $x\->backermann($y); # the Ackermann function \& $x\->bsin(); # sine \& $x\->bcos(); # cosine \& $x\->batan(); # inverse tangent \& $x\->batan2($y); # two\-argument inverse tangent \& $x\->bsqrt(); # calculate square root \& $x\->broot($y); # $y\*(Aqth root of $x (e.g. $y == 3 => cubic root) \& $x\->bfac(); # factorial of $x (1*2*3*4*..$x) \& $x\->bdfac(); # double factorial of $x ($x*($x\-2)*($x\-4)*...) \& $x\->btfac(); # triple factorial of $x ($x*($x\-3)*($x\-6)*...) \& $x\->bmfac($k); # $k\*(Aqth multi\-factorial of $x ($x*($x\-$k)*...) \& $x\->bfib($k); # $k\*(Aqth Fibonacci number \& $x\->blucas($k); # $k\*(Aqth Lucas number \& \& $x\->blsft($n); # left shift $n places in base 2 \& $x\->blsft($n, $b); # left shift $n places in base $b \& $x\->brsft($n); # right shift $n places in base 2 \& $x\->brsft($n, $b); # right shift $n places in base $b \& \& # Bitwise methods (these modify the invocand) \& \& $x\->bblsft($y); # bitwise left shift \& $x\->bbrsft($y); # bitwise right shift \& $x\->band($y); # bitwise and \& $x\->bior($y); # bitwise inclusive or \& $x\->bxor($y); # bitwise exclusive or \& $x\->bnot(); # bitwise not (two\*(Aqs complement) \& \& # Rounding methods (these modify the invocand) \& \& $x\->round($A, $P, $R); # round to accuracy or precision using \& # rounding mode $R \& $x\->bround($n); # accuracy: preserve $n digits \& $x\->bfround($n); # $n > 0: round to $nth digit left of dec. point \& # $n < 0: round to $nth digit right of dec. point \& $x\->bfloor(); # round towards minus infinity \& $x\->bceil(); # round towards plus infinity \& $x\->bint(); # round towards zero \& \& # Other mathematical methods (these don\*(Aqt modify the invocand) \& \& $x\->bgcd($y); # greatest common divisor \& $x\->blcm($y); # least common multiple \& \& # Object property methods (these don\*(Aqt modify the invocand) \& \& $x\->sign(); # the sign, either +, \- or NaN \& $x\->digit($n); # the nth digit, counting from the right \& $x\->digit(\-$n); # the nth digit, counting from the left \& $x\->digitsum(); # sum of decimal digits \& $x\->length(); # return number of digits in number \& $x\->mantissa(); # return (signed) mantissa as a Math::BigInt \& $x\->exponent(); # return exponent as a Math::BigInt \& $x\->parts(); # return (mantissa,exponent) as a Math::BigInt \& $x\->sparts(); # mantissa and exponent (as integers) \& $x\->nparts(); # mantissa and exponent (normalised) \& $x\->eparts(); # mantissa and exponent (engineering notation) \& $x\->dparts(); # integer and fraction part \& $x\->fparts(); # numerator and denominator \& $x\->numerator(); # numerator \& $x\->denominator(); # denominator \& \& # Conversion methods (these don\*(Aqt modify the invocand) \& \& $x\->bstr(); # decimal notation (possibly zero padded) \& $x\->bnstr(); # string in normalized notation \& $x\->bestr(); # string in engineering notation \& $x\->bdstr(); # string in decimal notation (no padding) \& $x\->bfstr(); # string in fractional notation \& \& $x\->to_hex(); # as signed hexadecimal string \& $x\->to_bin(); # as signed binary string \& $x\->to_oct(); # as signed octal string \& $x\->to_bytes(); # as byte string \& $x\->to_base($b); # as string in any base \& $x\->to_base_num($b); # as array of integers in any base \& $x\->to_ieee754($fmt); # to bytes encoded according to IEEE 754\-2008 \& \& $x\->as_hex(); # as signed hexadecimal string with "0x" prefix \& $x\->as_bin(); # as signed binary string with "0b" prefix \& $x\->as_oct(); # as signed octal string with "0" prefix \& \& # Other conversion methods (these don\*(Aqt modify the invocand) \& \& $x\->numify(); # return as scalar (might overflow or underflow) .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Math::BigRat complements Math::BigInt and Math::BigFloat by providing support for arbitrary big rational numbers. .SS "Math Library" .IX Subsection "Math Library" You can change the underlying module that does the low\-level math operations by using: .PP .Vb 1 \& use Math::BigRat try => \*(AqGMP\*(Aq; .Ve .PP Note: This needs Math::BigInt::GMP installed. .PP The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: .PP .Vb 1 \& use Math::BigRat try => \*(AqFoo,Math::BigInt::Bar\*(Aq; .Ve .PP If you want to get warned when the fallback occurs, replace "try" with "lib": .PP .Vb 1 \& use Math::BigRat lib => \*(AqFoo,Math::BigInt::Bar\*(Aq; .Ve .PP If you want the code to die instead, replace "try" with "only": .PP .Vb 1 \& use Math::BigRat only => \*(AqFoo,Math::BigInt::Bar\*(Aq; .Ve .SH METHODS .IX Header "METHODS" Any methods not listed here are derived from Math::BigFloat (or Math::BigInt), so make sure you check these two modules for further information. .IP \fBnew()\fR 4 .IX Item "new()" .Vb 1 \& $x = Math::BigRat\->new(\*(Aq1/3\*(Aq); .Ve .Sp Create a new Math::BigRat object. Input can come in various forms: .Sp .Vb 9 \& $x = Math::BigRat\->new(123); # scalars \& $x = Math::BigRat\->new(\*(Aqinf\*(Aq); # infinity \& $x = Math::BigRat\->new(\*(Aq123.3\*(Aq); # float \& $x = Math::BigRat\->new(\*(Aq1/3\*(Aq); # simple string \& $x = Math::BigRat\->new(\*(Aq1 / 3\*(Aq); # spaced \& $x = Math::BigRat\->new(\*(Aq1 / 0.1\*(Aq); # w/ floats \& $x = Math::BigRat\->new(Math::BigInt\->new(3)); # BigInt \& $x = Math::BigRat\->new(Math::BigFloat\->new(\*(Aq3.1\*(Aq)); # BigFloat \& $x = Math::BigRat\->new(Math::BigInt::Lite\->new(\*(Aq2\*(Aq)); # BigLite \& \& # You can also give D and N as different objects: \& $x = Math::BigRat\->new( \& Math::BigInt\->new(\-123), \& Math::BigInt\->new(7), \& ); # => \-123/7 .Ve .IP \fBfrom_dec()\fR 4 .IX Item "from_dec()" .Vb 1 \& my $h = Math::BigRat\->from_dec("1.2"); .Ve .Sp Create a BigRat from a decimal number in string form. It is equivalent to "\fBnew()\fR", but does not accept anything but strings representing finite, decimal numbers. .IP \fBfrom_hex()\fR 4 .IX Item "from_hex()" .Vb 1 \& my $h = Math::BigRat\->from_hex("0x10"); .Ve .Sp Create a BigRat from a hexadecimal number in string form. .IP \fBfrom_oct()\fR 4 .IX Item "from_oct()" .Vb 1 \& my $o = Math::BigRat\->from_oct("020"); .Ve .Sp Create a BigRat from an octal number in string form. .IP \fBfrom_bin()\fR 4 .IX Item "from_bin()" .Vb 1 \& my $b = Math::BigRat\->from_bin("0b10000000"); .Ve .Sp Create a BigRat from an binary number in string form. .IP \fBfrom_bytes()\fR 4 .IX Item "from_bytes()" .Vb 1 \& $x = Math::BigRat\->from_bytes("\exf3\ex6b"); # $x = 62315 .Ve .Sp Interpret the input as a byte string, assuming big endian byte order. The output is always a non\-negative, finite integer. .Sp See "\fBfrom_bytes()\fR" in Math::BigInt. .IP \fBfrom_ieee754()\fR 4 .IX Item "from_ieee754()" .Vb 3 \& # set $x to 13176795/4194304, the closest value to pi that can be \& # represented in the binary32 (single) format \& $x = Math::BigRat \-> from_ieee754("40490fdb", "binary32"); .Ve .Sp Interpret the input as a value encoded as described in IEEE754\-2008. .Sp See "\fBfrom_ieee754()\fR" in Math::BigFloat. .IP \fBfrom_base()\fR 4 .IX Item "from_base()" See "\fBfrom_base()\fR" in Math::BigInt. .IP \fBbzero()\fR 4 .IX Item "bzero()" .Vb 1 \& $x = Math::BigRat\->bzero(); .Ve .Sp Creates a new BigRat object representing zero. If used on an object, it will set it to zero: .Sp .Vb 1 \& $x\->bzero(); .Ve .IP \fBbone()\fR 4 .IX Item "bone()" .Vb 1 \& $x = Math::BigRat\->bone($sign); .Ve .Sp Creates a new BigRat object representing one. The optional argument is either \*(Aq\-\*(Aq or \*(Aq+\*(Aq, indicating whether you want one or minus one. If used on an object, it will set it to one: .Sp .Vb 2 \& $x\->bone(); # +1 \& $x\->bone(\*(Aq\-\*(Aq); # \-1 .Ve .IP \fBbinf()\fR 4 .IX Item "binf()" .Vb 1 \& $x = Math::BigRat\->binf($sign); .Ve .Sp Creates a new BigRat object representing infinity. The optional argument is either \*(Aq\-\*(Aq or \*(Aq+\*(Aq, indicating whether you want infinity or minus infinity. If used on an object, it will set it to infinity: .Sp .Vb 2 \& $x\->binf(); \& $x\->binf(\*(Aq\-\*(Aq); .Ve .IP \fBbnan()\fR 4 .IX Item "bnan()" .Vb 1 \& $x = Math::BigRat\->bnan(); .Ve .Sp Creates a new BigRat object representing NaN (Not A Number). If used on an object, it will set it to NaN: .Sp .Vb 1 \& $x\->bnan(); .Ve .IP \fBbpi()\fR 4 .IX Item "bpi()" .Vb 2 \& $x = Math::BigRat \-> bpi(); # default accuracy \& $x = Math::BigRat \-> bpi(7); # specified accuracy .Ve .Sp Returns a rational approximation of PI accurate to the specified accuracy or the default accuracy if no accuracy is specified. If called as an instance method, the value is assigned to the invocand. .Sp .Vb 3 \& $x = Math::BigRat \-> bpi(1); # returns "3" \& $x = Math::BigRat \-> bpi(3); # returns "22/7" \& $x = Math::BigRat \-> bpi(7); # returns "355/113" .Ve .IP \fBcopy()\fR 4 .IX Item "copy()" .Vb 1 \& my $z = $x\->copy(); .Ve .Sp Makes a deep copy of the object. .Sp Please see the documentation in Math::BigInt for further details. .IP \fBas_int()\fR 4 .IX Item "as_int()" .Vb 1 \& $y = $x \-> as_int(); # $y is a Math::BigInt .Ve .Sp Returns \f(CW$x\fR as a Math::BigInt object regardless of upgrading and downgrading. If \&\f(CW$x\fR is finite, but not an integer, \f(CW$x\fR is truncated. .IP \fBas_rat()\fR 4 .IX Item "as_rat()" .Vb 1 \& $y = $x \-> as_rat(); # $y is a Math::BigRat .Ve .Sp Returns \f(CW$x\fR a Math::BigRat object regardless of upgrading and downgrading. The invocand is not modified. .IP \fBas_float()\fR 4 .IX Item "as_float()" .Vb 2 \& $x = Math::BigRat\->new(\*(Aq13/7\*(Aq); \& print $x\->as_float(), "\en"; # \*(Aq1\*(Aq \& \& $x = Math::BigRat\->new(\*(Aq2/3\*(Aq); \& print $x\->as_float(5), "\en"; # \*(Aq0.66667\*(Aq .Ve .Sp Returns a copy of the object as Math::BigFloat object regardless of upgrading and downgrading, preserving the accuracy as wanted, or the default of 40 digits. .IP \fBbround()\fR/\fBround()\fR/\fBbfround()\fR 4 .IX Item "bround()/round()/bfround()" Are not yet implemented. .IP \fBis_zero()\fR 4 .IX Item "is_zero()" .Vb 1 \& print "$x is 0\en" if $x\->is_zero(); .Ve .Sp Return true if \f(CW$x\fR is exactly zero, otherwise false. .IP \fBis_one()\fR 4 .IX Item "is_one()" .Vb 1 \& print "$x is 1\en" if $x\->is_one(); .Ve .Sp Return true if \f(CW$x\fR is exactly one, otherwise false. .IP \fBis_finite()\fR 4 .IX Item "is_finite()" .Vb 1 \& $x\->is_finite(); # true if $x is not +inf, \-inf or NaN .Ve .Sp Returns true if the invocand is a finite number, i.e., it is neither +inf, \&\-inf, nor NaN. .IP \fBis_positive()\fR 4 .IX Item "is_positive()" .PD 0 .IP \fBis_pos()\fR 4 .IX Item "is_pos()" .PD .Vb 1 \& print "$x is >= 0\en" if $x\->is_positive(); .Ve .Sp Return true if \f(CW$x\fR is positive (greater than or equal to zero), otherwise false. Please note that \*(Aq+inf\*(Aq is also positive, while \*(AqNaN\*(Aq and \*(Aq\-inf\*(Aq aren\*(Aqt. .Sp "\fBis_positive()\fR" is an alias for "\fBis_pos()\fR". .IP \fBis_negative()\fR 4 .IX Item "is_negative()" .PD 0 .IP \fBis_neg()\fR 4 .IX Item "is_neg()" .PD .Vb 1 \& print "$x is < 0\en" if $x\->is_negative(); .Ve .Sp Return true if \f(CW$x\fR is negative (smaller than zero), otherwise false. Please note that \*(Aq\-inf\*(Aq is also negative, while \*(AqNaN\*(Aq and \*(Aq+inf\*(Aq aren\*(Aqt. .Sp "\fBis_negative()\fR" is an alias for "\fBis_neg()\fR". .IP \fBis_odd()\fR 4 .IX Item "is_odd()" .Vb 1 \& print "$x is odd\en" if $x\->is_odd(); .Ve .Sp Return true if \f(CW$x\fR is odd, otherwise false. .IP \fBis_even()\fR 4 .IX Item "is_even()" .Vb 1 \& print "$x is even\en" if $x\->is_even(); .Ve .Sp Return true if \f(CW$x\fR is even, otherwise false. .IP \fBis_int()\fR 4 .IX Item "is_int()" .Vb 1 \& print "$x is an integer\en" if $x\->is_int(); .Ve .Sp Return true if \f(CW$x\fR has a denominator of 1 (e.g. no fraction parts), otherwise false. Please note that \*(Aq\-inf\*(Aq, \*(Aqinf\*(Aq and \*(AqNaN\*(Aq aren\*(Aqt integer. .SS "Comparison methods" .IX Subsection "Comparison methods" None of these methods modify the invocand object. Note that a \f(CW\*(C`NaN\*(C'\fR is neither less than, greater than, or equal to anything else, even a \f(CW\*(C`NaN\*(C'\fR. .IP \fBbcmp()\fR 4 .IX Item "bcmp()" .Vb 1 \& $x\->bcmp($y); .Ve .Sp Compares \f(CW$x\fR with \f(CW$y\fR and takes the sign into account. Returns \-1, 0, 1 or undef. .IP \fBbacmp()\fR 4 .IX Item "bacmp()" .Vb 1 \& $x\->bacmp($y); .Ve .Sp Compares \f(CW$x\fR with \f(CW$y\fR while ignoring their sign. Returns \-1, 0, 1 or undef. .IP \fBbeq()\fR 4 .IX Item "beq()" .Vb 1 \& $x \-> beq($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is equal to \f(CW$y\fR, and false otherwise. .IP \fBbne()\fR 4 .IX Item "bne()" .Vb 1 \& $x \-> bne($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is not equal to \f(CW$y\fR, and false otherwise. .IP \fBblt()\fR 4 .IX Item "blt()" .Vb 1 \& $x \-> blt($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is equal to \f(CW$y\fR, and false otherwise. .IP \fBble()\fR 4 .IX Item "ble()" .Vb 1 \& $x \-> ble($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is less than or equal to \f(CW$y\fR, and false otherwise. .IP \fBbgt()\fR 4 .IX Item "bgt()" .Vb 1 \& $x \-> bgt($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is greater than \f(CW$y\fR, and false otherwise. .IP \fBbge()\fR 4 .IX Item "bge()" .Vb 1 \& $x \-> bge($y); .Ve .Sp Returns true if and only if \f(CW$x\fR is greater than or equal to \f(CW$y\fR, and false otherwise. .IP \fBblsft()\fR/\fBbrsft()\fR 4 .IX Item "blsft()/brsft()" Used to shift numbers left/right. .Sp Please see the documentation in Math::BigInt for further details. .IP \fBbneg()\fR 4 .IX Item "bneg()" .Vb 1 \& $x\->bneg(); .Ve .Sp Used to negate the object in\-place. .IP \fBbnorm()\fR 4 .IX Item "bnorm()" .Vb 1 \& $x\->bnorm(); .Ve .Sp Reduce the number to the shortest form. This routine is called automatically whenever it is needed. .IP \fBbinc()\fR 4 .IX Item "binc()" .Vb 1 \& $x\->binc(); .Ve .Sp Increments \f(CW$x\fR by 1 and returns the result. .IP \fBbdec()\fR 4 .IX Item "bdec()" .Vb 1 \& $x\->bdec(); .Ve .Sp Decrements \f(CW$x\fR by 1 and returns the result. .IP \fBbadd()\fR 4 .IX Item "badd()" .Vb 1 \& $x\->badd($y); .Ve .Sp Adds \f(CW$y\fR to \f(CW$x\fR and returns the result. .IP \fBbsub()\fR 4 .IX Item "bsub()" .Vb 1 \& $x\->bsub($y); .Ve .Sp Subtracts \f(CW$y\fR from \f(CW$x\fR and returns the result. .IP \fBbmul()\fR 4 .IX Item "bmul()" .Vb 1 \& $x\->bmul($y); .Ve .Sp Multiplies \f(CW$y\fR to \f(CW$x\fR and returns the result. .IP \fBbdiv()\fR 4 .IX Item "bdiv()" .Vb 2 \& $q = $x\->bdiv($y); \& ($q, $r) = $x\->bdiv($y); .Ve .Sp In scalar context, divides \f(CW$x\fR by \f(CW$y\fR and returns the result. In list context, does floored division (F\-division), returning an integer \f(CW$q\fR and a remainder \f(CW$r\fR so that \f(CW$x\fR = \f(CW$q\fR * \f(CW$y\fR + \f(CW$r\fR. The remainer (modulo) is equal to what is returned by \f(CW\*(C`$x\->bmod($y)\*(C'\fR. .IP \fBbmod()\fR 4 .IX Item "bmod()" .Vb 1 \& $x\->bmod($y); .Ve .Sp Returns \f(CW$x\fR modulo \f(CW$y\fR. When \f(CW$x\fR is finite, and \f(CW$y\fR is finite and non\-zero, the result is identical to the remainder after floored division (F\-division). If, in addition, both \f(CW$x\fR and \f(CW$y\fR are integers, the result is identical to the result from Perl\*(Aqs % operator. .IP \fBbinv()\fR 4 .IX Item "binv()" .Vb 1 \& $x\->binv(); .Ve .Sp Inverse of \f(CW$x\fR. .IP \fBbsqrt()\fR 4 .IX Item "bsqrt()" .Vb 1 \& $x\->bsqrt(); .Ve .Sp Calculate the square root of \f(CW$x\fR. .IP \fBbpow()\fR 4 .IX Item "bpow()" .Vb 1 \& $x\->bpow($y); .Ve .Sp Compute \f(CW$x\fR ** \f(CW$y\fR. .Sp Please see the documentation in Math::BigInt for further details. .IP \fBbroot()\fR 4 .IX Item "broot()" .Vb 1 \& $x\->broot($n); .Ve .Sp Calculate the N\*(Aqth root of \f(CW$x\fR. .IP \fBbmodpow()\fR 4 .IX Item "bmodpow()" .Vb 2 \& $num\->bmodpow($exp,$mod); # modular exponentiation \& # ($num**$exp % $mod) .Ve .Sp Returns the value of \f(CW$num\fR taken to the power \f(CW$exp\fR in the modulus \&\f(CW$mod\fR using binary exponentiation. \f(CW\*(C`bmodpow\*(C'\fR is far superior to writing .Sp .Vb 1 \& $num ** $exp % $mod .Ve .Sp because it is much faster \- it reduces internal variables into the modulus whenever possible, so it operates on smaller numbers. .Sp \&\f(CW\*(C`bmodpow\*(C'\fR also supports negative exponents. .Sp .Vb 1 \& bmodpow($num, \-1, $mod) .Ve .Sp is exactly equivalent to .Sp .Vb 1 \& bmodinv($num, $mod) .Ve .IP \fBbmodinv()\fR 4 .IX Item "bmodinv()" .Vb 1 \& $x\->bmodinv($mod); # modular multiplicative inverse .Ve .Sp Returns the multiplicative inverse of \f(CW$x\fR modulo \f(CW$mod\fR. If .Sp .Vb 1 \& $y = $x \-> copy() \-> bmodinv($mod) .Ve .Sp then \f(CW$y\fR is the number closest to zero, and with the same sign as \f(CW$mod\fR, satisfying .Sp .Vb 1 \& ($x * $y) % $mod = 1 % $mod .Ve .Sp If \f(CW$x\fR and \f(CW$y\fR are non\-zero, they must be relative primes, i.e., \&\f(CW\*(C`bgcd($y, $mod)==1\*(C'\fR. \*(Aq\f(CW\*(C`NaN\*(C'\fR\*(Aq is returned when no modular multiplicative inverse exists. .IP \fBblog()\fR 4 .IX Item "blog()" .Vb 1 \& $x\->blog($base, $accuracy); # logarithm of x to the base $base .Ve .Sp If \f(CW$base\fR is not defined, Euler\*(Aqs number (e) is used: .Sp .Vb 1 \& print $x\->blog(undef, 100); # log(x) to 100 digits .Ve .IP \fBbexp()\fR 4 .IX Item "bexp()" .Vb 1 \& $x\->bexp($accuracy); # calculate e ** X .Ve .Sp Calculates two integers A and B so that A/B is equal to \f(CW\*(C`e ** $x\*(C'\fR, where \f(CW\*(C`e\*(C'\fR is Euler\*(Aqs number. .Sp This method was added in v0.20 of Math::BigRat (May 2007). .Sp See also "\fBblog()\fR". .IP \fBbnok()\fR 4 .IX Item "bnok()" See "\fBbnok()\fR" in Math::BigInt. .IP \fBbperm()\fR 4 .IX Item "bperm()" See "\fBbperm()\fR" in Math::BigInt. .IP \fBbfac()\fR 4 .IX Item "bfac()" .Vb 1 \& $x\->bfac(); .Ve .Sp Calculates the factorial of \f(CW$x\fR. For instance: .Sp .Vb 2 \& print Math::BigRat\->new(\*(Aq3/1\*(Aq)\->bfac(), "\en"; # 1*2*3 \& print Math::BigRat\->new(\*(Aq5/1\*(Aq)\->bfac(), "\en"; # 1*2*3*4*5 .Ve .Sp Works currently only for integers. .IP \fBband()\fR 4 .IX Item "band()" .Vb 1 \& $x\->band($y); # bitwise and .Ve .IP \fBbior()\fR 4 .IX Item "bior()" .Vb 1 \& $x\->bior($y); # bitwise inclusive or .Ve .IP \fBbxor()\fR 4 .IX Item "bxor()" .Vb 1 \& $x\->bxor($y); # bitwise exclusive or .Ve .IP \fBbnot()\fR 4 .IX Item "bnot()" .Vb 1 \& $x\->bnot(); # bitwise not (two\*(Aqs complement) .Ve .IP \fBbfloor()\fR 4 .IX Item "bfloor()" .Vb 1 \& $x\->bfloor(); .Ve .Sp Round \f(CW$x\fR towards minus infinity, i.e., set \f(CW$x\fR to the largest integer less than or equal to \f(CW$x\fR. .IP \fBbceil()\fR 4 .IX Item "bceil()" .Vb 1 \& $x\->bceil(); .Ve .Sp Round \f(CW$x\fR towards plus infinity, i.e., set \f(CW$x\fR to the smallest integer greater than or equal to \f(CW$x\fR. .IP \fBbint()\fR 4 .IX Item "bint()" .Vb 1 \& $x\->bint(); .Ve .Sp Round \f(CW$x\fR towards zero. .IP \fBbgcd()\fR 4 .IX Item "bgcd()" .Vb 2 \& $x \-> bgcd($y); # GCD of $x and $y \& $x \-> bgcd($y, $z, ...); # GCD of $x, $y, $z, ... .Ve .Sp Returns the greatest common divisor (GCD), which is the number with the largest absolute value such that \f(CW$x\fR/$gcd, \f(CW$y\fR/$gcd, ... is an integer. For example, when the operands are 4/5 and 6/5, the GCD is 2/5. This is a generalisation of the ordinary GCD for integers. See "\fBgcd()\fR" in Math::BigInt. .IP \fBdigit()\fR 4 .IX Item "digit()" .Vb 2 \& print Math::BigRat\->new(\*(Aq123/1\*(Aq)\->digit(1); # 1 \& print Math::BigRat\->new(\*(Aq123/1\*(Aq)\->digit(\-1); # 3 .Ve .Sp Return the N\*(Aqths digit from X when X is an integer value. .IP \fBlength()\fR 4 .IX Item "length()" .Vb 1 \& $len = $x\->length(); .Ve .Sp Return the length of \f(CW$x\fR in digits for integer values. .IP \fBparts()\fR 4 .IX Item "parts()" .Vb 1 \& ($n, $d) = $x\->parts(); .Ve .Sp Return a list consisting of (signed) numerator and (unsigned) denominator as BigInts. .IP \fBdparts()\fR 4 .IX Item "dparts()" Returns the integer part and the fraction part. .IP \fBfparts()\fR 4 .IX Item "fparts()" Returns the smallest possible numerator and denominator so that the numerator divided by the denominator gives back the original value. For finite numbers, both values are integers. Mnemonic: fraction. .IP \fBnumerator()\fR 4 .IX Item "numerator()" .Vb 1 \& $n = $x\->numerator(); .Ve .Sp Returns a copy of the numerator (the part above the line) as signed BigInt. .IP \fBdenominator()\fR 4 .IX Item "denominator()" .Vb 1 \& $d = $x\->denominator(); .Ve .Sp Returns a copy of the denominator (the part under the line) as positive BigInt. .SS "String conversion methods" .IX Subsection "String conversion methods" .IP \fBbstr()\fR 4 .IX Item "bstr()" .Vb 2 \& my $x = Math::BigRat\->new(\*(Aq8/4\*(Aq); \& print $x\->bstr(), "\en"; # prints 1/2 .Ve .Sp Returns a string representing the number. .IP \fBbnstr()\fR 4 .IX Item "bnstr()" See "\fBbnstr()\fR" in Math::BigInt. .IP \fBbestr()\fR 4 .IX Item "bestr()" See "\fBbestr()\fR" in Math::BigInt. .IP \fBbdstr()\fR 4 .IX Item "bdstr()" See "\fBbdstr()\fR" in Math::BigInt. .IP \fBto_bytes()\fR 4 .IX Item "to_bytes()" See "\fBto_bytes()\fR" in Math::BigInt. .IP \fBto_ieee754()\fR 4 .IX Item "to_ieee754()" See "\fBto_ieee754()\fR" in Math::BigFloat. .IP \fBas_hex()\fR 4 .IX Item "as_hex()" .Vb 2 \& $x = Math::BigRat\->new(\*(Aq13\*(Aq); \& print $x\->as_hex(), "\en"; # \*(Aq0xd\*(Aq .Ve .Sp Returns the BigRat as hexadecimal string. Works only for integers. .IP \fBas_oct()\fR 4 .IX Item "as_oct()" .Vb 2 \& $x = Math::BigRat\->new(\*(Aq13\*(Aq); \& print $x\->as_oct(), "\en"; # \*(Aq015\*(Aq .Ve .Sp Returns the BigRat as octal string. Works only for integers. .IP \fBas_bin()\fR 4 .IX Item "as_bin()" .Vb 2 \& $x = Math::BigRat\->new(\*(Aq13\*(Aq); \& print $x\->as_bin(), "\en"; # \*(Aq0x1101\*(Aq .Ve .Sp Returns the BigRat as binary string. Works only for integers. .IP \fBnumify()\fR 4 .IX Item "numify()" .Vb 1 \& my $y = $x\->numify(); .Ve .Sp Returns the object as a scalar. This will lose some data if the object cannot be represented by a normal Perl scalar (integer or float), so use "\fBas_int()\fR" or "\fBas_float()\fR" instead. .Sp This routine is automatically used whenever a scalar is required: .Sp .Vb 3 \& my $x = Math::BigRat\->new(\*(Aq3/1\*(Aq); \& @array = (0, 1, 2, 3); \& $y = $array[$x]; # set $y to 3 .Ve .IP \fBconfig()\fR 4 .IX Item "config()" .Vb 2 \& Math::BigRat\->config("trap_nan" => 1); # set \& $accu = Math::BigRat\->config("accuracy"); # get .Ve .Sp Set or get configuration parameter values. Read\-only parameters are marked as RO. Read\-write parameters are marked as RW. The following parameters are supported. .Sp .Vb 10 \& Parameter RO/RW Description \& Example \& ============================================================ \& lib RO Name of the math backend library \& Math::BigInt::Calc \& lib_version RO Version of the math backend library \& 0.30 \& class RO The class of config you just called \& Math::BigRat \& version RO version number of the class you used \& 0.10 \& upgrade RW To which class numbers are upgraded \& undef \& downgrade RW To which class numbers are downgraded \& undef \& precision RW Global precision \& undef \& accuracy RW Global accuracy \& undef \& round_mode RW Global round mode \& even \& div_scale RW Fallback accuracy for div, sqrt etc. \& 40 \& trap_nan RW Trap NaNs \& undef \& trap_inf RW Trap +inf/\-inf \& undef .Ve .SH "NUMERIC LITERALS" .IX Header "NUMERIC LITERALS" After \f(CW\*(C`use Math::BigRat \*(Aq:constant\*(Aq\*(C'\fR all numeric literals in the given scope are converted to \f(CW\*(C`Math::BigRat\*(C'\fR objects. This conversion happens at compile time. Every non\-integer is convert to a NaN. .PP For example, .PP .Vb 1 \& perl \-MMath::BigRat=:constant \-le \*(Aqprint 2**150\*(Aq .Ve .PP prints the exact value of \f(CW\*(C`2**150\*(C'\fR. Note that without conversion of constants to objects the expression \f(CW\*(C`2**150\*(C'\fR is calculated using Perl scalars, which leads to an inaccurate result. .PP Please note that strings are not affected, so that .PP .Vb 1 \& use Math::BigRat qw/:constant/; \& \& $x = "1234567890123456789012345678901234567890" \& + "123456789123456789"; .Ve .PP does give you what you expect. You need an explicit Math::BigRat\->\fBnew()\fR around at least one of the operands. You should also quote large constants to prevent loss of precision: .PP .Vb 1 \& use Math::BigRat; \& \& $x = Math::BigRat\->new("1234567889123456789123456789123456789"); .Ve .PP Without the quotes Perl first converts the large number to a floating point constant at compile time, and then converts the result to a Math::BigRat object at run time, which results in an inaccurate result. .SS "Hexadecimal, octal, and binary floating point literals" .IX Subsection "Hexadecimal, octal, and binary floating point literals" Perl (and this module) accepts hexadecimal, octal, and binary floating point literals, but use them with care with Perl versions before v5.32.0, because some versions of Perl silently give the wrong result. Below are some examples of different ways to write the number decimal 314. .PP Hexadecimal floating point literals: .PP .Vb 3 \& 0x1.3ap+8 0X1.3AP+8 \& 0x1.3ap8 0X1.3AP8 \& 0x13a0p\-4 0X13A0P\-4 .Ve .PP Octal floating point literals (with "0" prefix): .PP .Vb 3 \& 01.164p+8 01.164P+8 \& 01.164p8 01.164P8 \& 011640p\-4 011640P\-4 .Ve .PP Octal floating point literals (with "0o" prefix) (requires v5.34.0): .PP .Vb 3 \& 0o1.164p+8 0O1.164P+8 \& 0o1.164p8 0O1.164P8 \& 0o11640p\-4 0O11640P\-4 .Ve .PP Binary floating point literals: .PP .Vb 3 \& 0b1.0011101p+8 0B1.0011101P+8 \& 0b1.0011101p8 0B1.0011101P8 \& 0b10011101000p\-2 0B10011101000P\-2 .Ve .SH BUGS .IX Header "BUGS" Please report any bugs or feature requests to \&\f(CW\*(C`bug\-math\-bigint at rt.cpan.org\*(C'\fR, or through the web interface at (requires login). We will be notified, and then you\*(Aqll automatically be notified of progress on your bug as I make changes. .SH SUPPORT .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc Math::BigInt .Ve .PP You can also look for information at: .IP \(bu 4 GitHub .Sp .IP \(bu 4 RT: CPAN\*(Aqs request tracker .Sp .IP \(bu 4 MetaCPAN .Sp .IP \(bu 4 CPAN Testers Matrix .Sp .SH LICENSE .IX Header "LICENSE" This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" Math::BigInt and Math::BigFloat as well as the backend libraries Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari, Math::BigInt::GMPz, and Math::BigInt::BitVect. .PP The pragmas bigint, bigfloat, and bigrat might also be of interest. In addition there is the bignum pragma which does upgrading and downgrading. .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Tels 2001\-2009. .IP \(bu 4 Maintained by Peter John Acklam 2011\-