|
NAMEMath::BigRat - arbitrary size rational number math package SYNOPSIS use Math::BigRat;
# Generic constructor method (always returns a new object)
$x = Math::BigRat->new($str); # defaults to 0
$x = Math::BigRat->new('256'); # from decimal
$x = Math::BigRat->new('0256'); # from decimal
$x = Math::BigRat->new('0xcafe'); # from hexadecimal
$x = Math::BigRat->new('0x1.fap+7'); # from hexadecimal
$x = Math::BigRat->new('0o377'); # from octal
$x = Math::BigRat->new('0o1.35p+6'); # from octal
$x = Math::BigRat->new('0b101'); # from binary
$x = Math::BigRat->new('0b1.101p+3'); # 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('234'); # from decimal
$x = Math::BigRat->from_hex('cafe'); # from hexadecimal
$x = Math::BigRat->from_hex('1.fap+7'); # from hexadecimal
$x = Math::BigRat->from_oct('377'); # from octal
$x = Math::BigRat->from_oct('1.35p+6'); # from octal
$x = Math::BigRat->from_bin('1101'); # from binary
$x = Math::BigRat->from_bin('1.101p+3'); # from binary
$x = Math::BigRat->from_bytes($bytes); # from byte string
$x = Math::BigRat->from_base('why', 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->from_fp80($b); # from x86 80-bit
$x = Math::BigRat->bzero(); # create a +0
$x = Math::BigRat->bone(); # create a +1
$x = Math::BigRat->bone('-'); # create a -1
$x = Math::BigRat->binf(); # create a +inf
$x = Math::BigRat->binf('-'); # 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't 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't 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's number)
$x->blog($base); # logarithm of $x to base $base (e.g., base 2)
$x->bexp(); # calculate e ** $x where e is Euler's 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's up-arrow notation
$x->bhyperop($n, $y); # n'th 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'th 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'th multi-factorial of $x ($x*($x-$k)*...)
$x->bfib($k); # $k'th Fibonacci number
$x->blucas($k); # $k'th 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's 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't modify the invocand)
$x->bgcd($y); # greatest common divisor
$x->blcm($y); # least common multiple
# Object property methods (these don't 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't 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->to_fp80(); # encode value in x86 80-bit format
$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't modify the invocand)
$x->numify(); # return as scalar (might overflow or underflow)
DESCRIPTIONMath::BigRat complements Math::BigInt and Math::BigFloat by providing support for arbitrary big rational numbers. Math LibraryYou can change the underlying module that does the low-level math operations by using: use Math::BigRat try => 'GMP'; Note: This needs Math::BigInt::GMP installed. The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: use Math::BigRat try => 'Foo,Math::BigInt::Bar'; If you want to get warned when the fallback occurs, replace "try" with "lib": use Math::BigRat lib => 'Foo,Math::BigInt::Bar'; If you want the code to die instead, replace "try" with "only": use Math::BigRat only => 'Foo,Math::BigInt::Bar'; METHODSAny methods not listed here are derived from Math::BigFloat (or Math::BigInt), so make sure you check these two modules for further information.
Comparison methodsNone of these methods modify the invocand object. Note that a "NaN" is neither less than, greater than, or equal to anything else, even a "NaN".
String conversion methods
NUMERIC LITERALSAfter "use Math::BigRat ':constant'" all numeric literals in the given scope are converted to "Math::BigRat" objects. This conversion happens at compile time. Every non-integer is convert to a NaN. For example, perl -MMath::BigRat=:constant -le 'print 2**150' prints the exact value of "2**150". Note that without conversion of constants to objects the expression "2**150" is calculated using Perl scalars, which leads to an inaccurate result. Please note that strings are not affected, so that use Math::BigRat qw/:constant/;
$x = "1234567890123456789012345678901234567890"
+ "123456789123456789";
does give you what you expect. You need an explicit Math::BigRat->new() around at least one of the operands. You should also quote large constants to prevent loss of precision: use Math::BigRat;
$x = Math::BigRat->new("1234567889123456789123456789123456789");
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. Hexadecimal, octal, and binary floating point literalsPerl (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. Hexadecimal floating point literals: 0x1.3ap+8 0X1.3AP+8
0x1.3ap8 0X1.3AP8
0x13a0p-4 0X13A0P-4
Octal floating point literals (with "0" prefix): 01.164p+8 01.164P+8
01.164p8 01.164P8
011640p-4 011640P-4
Octal floating point literals (with "0o" prefix) (requires v5.34.0): 0o1.164p+8 0O1.164P+8
0o1.164p8 0O1.164P8
0o11640p-4 0O11640P-4
Binary floating point literals: 0b1.0011101p+8 0B1.0011101P+8
0b1.0011101p8 0B1.0011101P8
0b10011101000p-2 0B10011101000P-2
BUGSPlease report any bugs or feature requests to "bug-math-bigint at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Ticket/Create.html?Queue=Math-BigInt> (requires login). We will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORTYou can find documentation for this module with the perldoc command. perldoc Math::BigInt You can also look for information at:
LICENSEThis program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. SEE ALSOMath::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. The pragmas bigint, bigfloat, and bigrat might also be of interest. In addition there is the bignum pragma which does upgrading and downgrading. AUTHORS
|