BigRational
extends BigNumber
in package
An arbitrarily large rational number.
This class is immutable.
Tags
Table of Contents
- PARSE_REGEXP = '/^' . '(?<sign>[\\-\\+])?' . '(?:' . '(?:' . '(?<integral>[0-9]+)?' . '(?<point>\\.)?' . '(?<fractional>[0-9]+)?' . '(?:[eE](?<exponent>[\\-\\+]?[0-9]+))?' . ')|(?:' . '(?<numerator>[0-9]+)' . '\\/?' . '(?<denominator>[0-9]+)' . ')' . ')' . '$/'
- The regular expression used to parse integer, decimal and rational numbers.
- $denominator : BigInteger
- The denominator. Always strictly positive.
- $numerator : BigInteger
- The numerator.
- __toString() : string
- Returns a string representation of this number.
- abs() : BigRational
- Returns the absolute value of this BigRational.
- compareTo() : int
- Compares this number to the given one.
- dividedBy() : BigRational
- Returns the result of the division of this number by the given one.
- getDenominator() : BigInteger
- getNumerator() : BigInteger
- getSign() : int
- Returns the sign of this number.
- isEqualTo() : bool
- Checks if this number is equal to the given one.
- isGreaterThan() : bool
- Checks if this number is strictly greater than the given one.
- isGreaterThanOrEqualTo() : bool
- Checks if this number is greater than or equal to the given one.
- isLessThan() : bool
- Checks if this number is strictly lower than the given one.
- isLessThanOrEqualTo() : bool
- Checks if this number is lower than or equal to the given one.
- isNegative() : bool
- Checks if this number is strictly negative.
- isNegativeOrZero() : bool
- Checks if this number is negative or zero.
- isPositive() : bool
- Checks if this number is strictly positive.
- isPositiveOrZero() : bool
- Checks if this number is positive or zero.
- isZero() : bool
- Checks if this number equals zero.
- jsonSerialize() : string
- {@inheritdoc}
- max() : static
- Returns the maximum of the given values.
- min() : static
- Returns the minimum of the given values.
- minus() : BigRational
- Returns the difference of this number and the given one.
- multipliedBy() : BigRational
- Returns the product of this number and the given one.
- nd() : BigRational
- Creates a BigRational out of a numerator and a denominator.
- negated() : BigRational
- Returns the negated value of this BigRational.
- of() : BigRational
- Creates a BigRational of the given value.
- one() : BigRational
- Returns a BigRational representing one.
- plus() : BigRational
- Returns the sum of this number and the given one.
- power() : BigRational
- Returns this number exponentiated to the given value.
- quotient() : BigInteger
- Returns the quotient of the division of the numerator by the denominator.
- quotientAndRemainder() : array<string|int, BigInteger>
- Returns the quotient and remainder of the division of the numerator by the denominator.
- reciprocal() : BigRational
- Returns the reciprocal of this BigRational.
- remainder() : BigInteger
- Returns the remainder of the division of the numerator by the denominator.
- simplified() : BigRational
- Returns the simplified value of this BigRational.
- sum() : static
- Returns the sum of the given values.
- ten() : BigRational
- Returns a BigRational representing ten.
- toBigDecimal() : BigDecimal
- Converts this number to a BigDecimal.
- toBigInteger() : BigInteger
- Converts this number to a BigInteger.
- toBigRational() : BigRational
- Converts this number to a BigRational.
- toFloat() : float
- Returns an approximation of this number as a floating-point value.
- toInt() : int
- Returns the exact value of this number as a native integer.
- toScale() : BigDecimal
- Converts this number to a BigDecimal with the given scale, using rounding if necessary.
- zero() : BigRational
- Returns a BigRational representing zero.
- __construct() : mixed
- Protected constructor. Use a factory method to obtain an instance.
- add() : BigNumber
- Adds two BigNumber instances in the correct order to avoid a RoundingNecessaryException.
- cleanUp() : string
- Removes optional leading zeros and + sign from the given number.
- floatToString() : string
- Safely converts float to string, avoiding locale-dependent issues.
Constants
PARSE_REGEXP
The regular expression used to parse integer, decimal and rational numbers.
private
mixed
PARSE_REGEXP
= '/^' . '(?<sign>[\\-\\+])?' . '(?:' . '(?:' . '(?<integral>[0-9]+)?' . '(?<point>\\.)?' . '(?<fractional>[0-9]+)?' . '(?:[eE](?<exponent>[\\-\\+]?[0-9]+))?' . ')|(?:' . '(?<numerator>[0-9]+)' . '\\/?' . '(?<denominator>[0-9]+)' . ')' . ')' . '$/'
Properties
$denominator
The denominator. Always strictly positive.
private
BigInteger
$denominator
$numerator
The numerator.
private
BigInteger
$numerator
Methods
__toString()
Returns a string representation of this number.
public
__toString() : string
Return values
string —abs()
Returns the absolute value of this BigRational.
public
abs() : BigRational
Return values
BigRational —compareTo()
Compares this number to the given one.
public
compareTo(mixed $that) : int
Parameters
- $that : mixed
Return values
int —[-1,0,1] If $this
is lower than, equal to, or greater than $that
.
dividedBy()
Returns the result of the division of this number by the given one.
public
dividedBy(BigNumber|int|float|string $that) : BigRational
Parameters
- $that : BigNumber|int|float|string
-
The divisor.
Tags
Return values
BigRational —The result.
getDenominator()
public
getDenominator() : BigInteger
Return values
BigInteger —getNumerator()
public
getNumerator() : BigInteger
Return values
BigInteger —getSign()
Returns the sign of this number.
public
getSign() : int
Return values
int —-1 if the number is negative, 0 if zero, 1 if positive.
isEqualTo()
Checks if this number is equal to the given one.
public
isEqualTo(BigNumber|int|float|string $that) : bool
Parameters
- $that : BigNumber|int|float|string
Return values
bool —isGreaterThan()
Checks if this number is strictly greater than the given one.
public
isGreaterThan(BigNumber|int|float|string $that) : bool
Parameters
- $that : BigNumber|int|float|string
Return values
bool —isGreaterThanOrEqualTo()
Checks if this number is greater than or equal to the given one.
public
isGreaterThanOrEqualTo(BigNumber|int|float|string $that) : bool
Parameters
- $that : BigNumber|int|float|string
Return values
bool —isLessThan()
Checks if this number is strictly lower than the given one.
public
isLessThan(BigNumber|int|float|string $that) : bool
Parameters
- $that : BigNumber|int|float|string
Return values
bool —isLessThanOrEqualTo()
Checks if this number is lower than or equal to the given one.
public
isLessThanOrEqualTo(BigNumber|int|float|string $that) : bool
Parameters
- $that : BigNumber|int|float|string
Return values
bool —isNegative()
Checks if this number is strictly negative.
public
isNegative() : bool
Return values
bool —isNegativeOrZero()
Checks if this number is negative or zero.
public
isNegativeOrZero() : bool
Return values
bool —isPositive()
Checks if this number is strictly positive.
public
isPositive() : bool
Return values
bool —isPositiveOrZero()
Checks if this number is positive or zero.
public
isPositiveOrZero() : bool
Return values
bool —isZero()
Checks if this number equals zero.
public
isZero() : bool
Return values
bool —jsonSerialize()
{@inheritdoc}
public
jsonSerialize() : string
Return values
string —max()
Returns the maximum of the given values.
public
static max(BigNumber|int|float|string ...$values) : static
Parameters
- $values : BigNumber|int|float|string
-
The numbers to compare. All the numbers need to be convertible to an instance of the class this method is called on.
Tags
Return values
static —The maximum value.
min()
Returns the minimum of the given values.
public
static min(BigNumber|int|float|string ...$values) : static
Parameters
- $values : BigNumber|int|float|string
-
The numbers to compare. All the numbers need to be convertible to an instance of the class this method is called on.
Tags
Return values
static —The minimum value.
minus()
Returns the difference of this number and the given one.
public
minus(BigNumber|int|float|string $that) : BigRational
Parameters
- $that : BigNumber|int|float|string
-
The number to subtract.
Tags
Return values
BigRational —The result.
multipliedBy()
Returns the product of this number and the given one.
public
multipliedBy(BigNumber|int|float|string $that) : BigRational
Parameters
- $that : BigNumber|int|float|string
-
The multiplier.
Tags
Return values
BigRational —The result.
nd()
Creates a BigRational out of a numerator and a denominator.
public
static nd(BigNumber|int|float|string $numerator, BigNumber|int|float|string $denominator) : BigRational
If the denominator is negative, the signs of both the numerator and the denominator will be inverted to ensure that the denominator is always positive.
Parameters
- $numerator : BigNumber|int|float|string
-
The numerator. Must be convertible to a BigInteger.
- $denominator : BigNumber|int|float|string
-
The denominator. Must be convertible to a BigInteger.
Tags
Return values
BigRational —negated()
Returns the negated value of this BigRational.
public
negated() : BigRational
Return values
BigRational —of()
Creates a BigRational of the given value.
public
static of(BigNumber|int|float|string $value) : BigRational
Parameters
- $value : BigNumber|int|float|string
Tags
Return values
BigRational —one()
Returns a BigRational representing one.
public
static one() : BigRational
Tags
Return values
BigRational —plus()
Returns the sum of this number and the given one.
public
plus(BigNumber|int|float|string $that) : BigRational
Parameters
- $that : BigNumber|int|float|string
-
The number to add.
Tags
Return values
BigRational —The result.
power()
Returns this number exponentiated to the given value.
public
power(int $exponent) : BigRational
Parameters
- $exponent : int
-
The exponent.
Tags
Return values
BigRational —The result.
quotient()
Returns the quotient of the division of the numerator by the denominator.
public
quotient() : BigInteger
Return values
BigInteger —quotientAndRemainder()
Returns the quotient and remainder of the division of the numerator by the denominator.
public
quotientAndRemainder() : array<string|int, BigInteger>
Return values
array<string|int, BigInteger> —reciprocal()
Returns the reciprocal of this BigRational.
public
reciprocal() : BigRational
The reciprocal has the numerator and denominator swapped.
Tags
Return values
BigRational —remainder()
Returns the remainder of the division of the numerator by the denominator.
public
remainder() : BigInteger
Return values
BigInteger —simplified()
Returns the simplified value of this BigRational.
public
simplified() : BigRational
Return values
BigRational —sum()
Returns the sum of the given values.
public
static sum(BigNumber|int|float|string ...$values) : static
Parameters
- $values : BigNumber|int|float|string
-
The numbers to add. All the numbers need to be convertible to an instance of the class this method is called on.
Tags
Return values
static —The sum.
ten()
Returns a BigRational representing ten.
public
static ten() : BigRational
Tags
Return values
BigRational —toBigDecimal()
Converts this number to a BigDecimal.
public
toBigDecimal() : BigDecimal
Return values
BigDecimal —The converted number.
toBigInteger()
Converts this number to a BigInteger.
public
toBigInteger() : BigInteger
Return values
BigInteger —The converted number.
toBigRational()
Converts this number to a BigRational.
public
toBigRational() : BigRational
Return values
BigRational —The converted number.
toFloat()
Returns an approximation of this number as a floating-point value.
public
toFloat() : float
Return values
float —The converted value.
toInt()
Returns the exact value of this number as a native integer.
public
toInt() : int
Return values
int —The converted value.
toScale()
Converts this number to a BigDecimal with the given scale, using rounding if necessary.
public
toScale(int $scale[, int $roundingMode = RoundingMode::UNNECESSARY ]) : BigDecimal
Parameters
- $scale : int
-
The scale of the resulting
BigDecimal
. - $roundingMode : int = RoundingMode::UNNECESSARY
-
A
RoundingMode
constant.
Return values
BigDecimal —zero()
Returns a BigRational representing zero.
public
static zero() : BigRational
Tags
Return values
BigRational —__construct()
Protected constructor. Use a factory method to obtain an instance.
protected
__construct(BigInteger $numerator, BigInteger $denominator, bool $checkDenominator) : mixed
Parameters
- $numerator : BigInteger
-
The numerator.
- $denominator : BigInteger
-
The denominator.
- $checkDenominator : bool
-
Whether to check the denominator for negative and zero.
Tags
Return values
mixed —add()
Adds two BigNumber instances in the correct order to avoid a RoundingNecessaryException.
private
static add(BigNumber $a, BigNumber $b) : BigNumber
Parameters
Tags
Return values
BigNumber —cleanUp()
Removes optional leading zeros and + sign from the given number.
private
static cleanUp(string $number) : string
Parameters
- $number : string
-
The number, validated as a non-empty string of digits with optional leading sign.
Tags
Return values
string —floatToString()
Safely converts float to string, avoiding locale-dependent issues.
private
static floatToString(float $float) : string
Parameters
- $float : float