BigRational
An arbitrarily large rational number.
This class is immutable.
- Full name:
\Brick\Math\BigRational
- Parent class:
\Brick\Math\BigNumber
- This class is marked as final and can't be subclassed
- This class is a Final class
Properties
numerator
The numerator.
denominator
The denominator. Always strictly positive.
Methods
__construct
Protected constructor. Use a factory method to obtain an instance.
protected __construct(\Brick\Math\BigInteger $numerator, \Brick\Math\BigInteger $denominator, bool $checkDenominator): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$numerator |
\Brick\Math\BigInteger | The numerator. |
$denominator |
\Brick\Math\BigInteger | The denominator. |
$checkDenominator |
bool | Whether to check the denominator for negative and zero. |
Throws:
If the denominator is zero.
of
Creates a BigRational of the given value.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$value |
\Brick\Math\BigNumber|int|float|string |
Throws:
If the value cannot be converted to a BigRational.
nd
Creates a BigRational out of a numerator and a denominator.
public static nd(\Brick\Math\BigNumber|int|float|string $numerator, \Brick\Math\BigNumber|int|float|string $denominator): \Brick\Math\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.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$numerator |
\Brick\Math\BigNumber|int|float|string | The numerator. Must be convertible to a BigInteger. |
$denominator |
\Brick\Math\BigNumber|int|float|string | The denominator. Must be convertible to a BigInteger. |
Throws:
If an argument does not represent a valid number.
If an argument represents a non-integer number.
If the denominator is zero.
zero
Returns a BigRational representing zero.
- This method is static.
one
Returns a BigRational representing one.
- This method is static.
ten
Returns a BigRational representing ten.
- This method is static.
getNumerator
getDenominator
quotient
Returns the quotient of the division of the numerator by the denominator.
remainder
Returns the remainder of the division of the numerator by the denominator.
quotientAndRemainder
Returns the quotient and remainder of the division of the numerator by the denominator.
plus
Returns the sum of this number and the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string | The number to add. |
Throws:
If the number is not valid.
minus
Returns the difference of this number and the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string | The number to subtract. |
Throws:
If the number is not valid.
multipliedBy
Returns the product of this number and the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string | The multiplier. |
Throws:
If the multiplier is not a valid number.
dividedBy
Returns the result of the division of this number by the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string | The divisor. |
Throws:
If the divisor is not a valid number, or is zero.
power
Returns this number exponentiated to the given value.
Parameters:
Parameter | Type | Description |
---|---|---|
$exponent |
int |
Throws:
If the exponent is not in the range 0 to 1,000,000.
reciprocal
Returns the reciprocal of this BigRational.
The reciprocal has the numerator and denominator swapped.
Throws:
If the numerator is zero.
abs
Returns the absolute value of this BigRational.
negated
Returns the negated value of this BigRational.
simplified
Returns the simplified value of this BigRational.
compareTo
Compares this number to the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
Return Value:
[-1,0,1] If $this
is lower than, equal to, or greater than $that
.
getSign
Returns the sign of this number.
Return Value:
-1 if the number is negative, 0 if zero, 1 if positive.
toBigInteger
Converts this number to a BigInteger.
toBigDecimal
Converts this number to a BigDecimal.
toBigRational
Converts this number to a BigRational.
toScale
Converts this number to a BigDecimal with the given scale, using rounding if necessary.
Parameters:
Parameter | Type | Description |
---|---|---|
$scale |
int | The scale of the resulting BigDecimal . |
$roundingMode |
int | A RoundingMode constant. |
toInt
Returns the exact value of this number as a native integer.
If this number cannot be converted to a native integer without losing precision, an exception is thrown. Note that the acceptable range for an integer depends on the platform and differs for 32-bit and 64-bit.
toFloat
Returns an approximation of this number as a floating-point value.
Note that this method can discard information as the precision of a floating-point value is inherently limited.
If the number is greater than the largest representable floating point number, positive infinity is returned. If the number is less than the smallest representable floating point number, negative infinity is returned.
__toString
Returns a string representation of this number.
The output of this method can be parsed by the of()
factory method;
this will yield an object equal to this one, without any information loss.
Inherited methods
of
Creates a BigNumber of the given value.
The concrete return type is dependent on the given value, with the following rules:
- BigNumber instances are returned as is
- integer numbers are returned as BigInteger
- floating point numbers are converted to a string then parsed as such
- strings containing a
/
character are returned as BigRational - strings containing a
.
character or using an exponential notation are returned as BigDecimal -
strings containing only digits with an optional leading
+
or-
sign are returned as BigInteger -
This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$value |
\Brick\Math\BigNumber|int|float|string |
Throws:
If the format of the number is not valid.
If the value represents a rational number with a denominator of zero.
floatToString
Safely converts float to string, avoiding locale-dependent issues.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$float |
float |
See Also:
- https://github.com/brick/math/pull/20 -
min
Returns the minimum of the given values.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$values |
\Brick\Math\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. |
Throws:
If no values are given.
If an argument is not valid.
max
Returns the maximum of the given values.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$values |
\Brick\Math\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. |
Throws:
If no values are given.
If an argument is not valid.
sum
Returns the sum of the given values.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$values |
\Brick\Math\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. |
Throws:
If no values are given.
If an argument is not valid.
add
Adds two BigNumber instances in the correct order to avoid a RoundingNecessaryException.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$a |
\Brick\Math\BigNumber | |
$b |
\Brick\Math\BigNumber |
cleanUp
Removes optional leading zeros and + sign from the given number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number, validated as a non-empty string of digits with optional leading sign. |
isEqualTo
Checks if this number is equal to the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
isLessThan
Checks if this number is strictly lower than the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
isLessThanOrEqualTo
Checks if this number is lower than or equal to the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
isGreaterThan
Checks if this number is strictly greater than the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
isGreaterThanOrEqualTo
Checks if this number is greater than or equal to the given one.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
isZero
Checks if this number equals zero.
isNegative
Checks if this number is strictly negative.
isNegativeOrZero
Checks if this number is negative or zero.
isPositive
Checks if this number is strictly positive.
isPositiveOrZero
Checks if this number is positive or zero.
getSign
Returns the sign of this number.
- This method is abstract.
Return Value:
-1 if the number is negative, 0 if zero, 1 if positive.
compareTo
Compares this number to the given one.
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$that |
\Brick\Math\BigNumber|int|float|string |
Return Value:
[-1,0,1] If $this
is lower than, equal to, or greater than $that
.
Throws:
If the number is not valid.
toBigInteger
Converts this number to a BigInteger.
- This method is abstract.
Throws:
If this number cannot be converted to a BigInteger without rounding.
toBigDecimal
Converts this number to a BigDecimal.
- This method is abstract.
Throws:
If this number cannot be converted to a BigDecimal without rounding.
toBigRational
Converts this number to a BigRational.
- This method is abstract.
toScale
Converts this number to a BigDecimal with the given scale, using rounding if necessary.
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$scale |
int | The scale of the resulting BigDecimal . |
$roundingMode |
int | A RoundingMode constant. |
Throws:
If this number cannot be converted to the given scale without rounding. This only applies when RoundingMode::UNNECESSARY is used.
toInt
Returns the exact value of this number as a native integer.
If this number cannot be converted to a native integer without losing precision, an exception is thrown. Note that the acceptable range for an integer depends on the platform and differs for 32-bit and 64-bit.
- This method is abstract.
Throws:
If this number cannot be exactly converted to a native integer.
toFloat
Returns an approximation of this number as a floating-point value.
Note that this method can discard information as the precision of a floating-point value is inherently limited.
If the number is greater than the largest representable floating point number, positive infinity is returned. If the number is less than the smallest representable floating point number, negative infinity is returned.
- This method is abstract.
__toString
Returns a string representation of this number.
The output of this method can be parsed by the of()
factory method;
this will yield an object equal to this one, without any information loss.
- This method is abstract.
jsonSerialize
Automatically generated on 2025-03-18