Class \Prado\Util\Math\TRational
TRational implements a fraction in the form of one integer \Prado\Util\Math\getNumerator divided by another integer \Prado\Util\Math\getDenominator.
The class can be \Prado\Util\Math__construct or its \Prado\Util\Math\setValue
set as a string, a float value, or an array. A string in the format
$numerator . '/' . $denominator
, eg. "21/13", will set the both the numerator
and denominator. A string that is simply a numeric will be interpreted as a
float value. An array in the format of [$numerator, $denominator]
can be used
to set the numerator and denominator as well.
Setting Float values are processed through a Continued Fraction function to a specified tolerance to calculate the integer numerator and integer denominator. INF is "-1/0" and NAN (Not A Number) has the denominator equal zero (to avoid a divide by zero error).
The numerator and denominator can be accessed by getNumerator and getDenominator, respectively. These values can be accessed by array as well,
where the numerator is mapped to [0]
and ['numerator']
and the denominator is
mapped to [1]
and ['denominator']
. By setting []
the value can be set
and numerator and denominator computed. By getting [null]
the value may be
retrieved. Setting the value with a specific tolerance requires setValue.
TRational implements __toString and outputs a string of $numerator . '/' . $denominator
, the string format for rationals. eg. "13/8".
$rational = new TRational(1.618033988749895);
$value = $rational->getValue();
$value = $rational[null];
$numerator = $rational->getNumerator();
$numerator = $rational[0];
$denominator = $rational->getDenominator();
$denominator = $rational[1];
$rational[] = 1.5;
$rational[0] === 3;
$rational[1] === 2;
$rational[null] = '21/13';
$rational[0] === 21;
$rational[1] === 13;
The Rational data format is used by EXIF and, in particular, the GPS Image File Directory of EXIF.
Class hierarchy
- \Prado\Util\Math\TRational implements ArrayAccess
Since: 4.3.0
public
|
__construct([null|array<string|int, mixed>|false|float|int|string $numerator = null ][, null|false|(numeric) $denominator = null ]) : mixed
This initializes a TRational with no value [null], a float that gets deconstructed
into a numerator and denominator, a string with the numerator and denominator
with a '/' between them, an array with [0 => $numerator, 1 => $denominator],
or both the numerator and denominator as two parameters.
|
public
|
|
public
static
|
float2rational(float $value[, float $tolerance = null ][, bool|null $unsigned = false ]) : array<string|int, mixed>
This uses the Continued Fraction to make a float into a fraction of two integers.
|
public
|
getDenominator() : float|int
Unless specifically set, the denominator usually only has a positive value.
|
public
static
|
|
public
|
|
public
|
getValue() : float
This returns the float value of the Numerator divided by the denominator.
Returns INF (Infinity) float value if the \Prado\Util\Math\getNumerator is 0xFFFFFFFF (-1) and \Prado\Util\Math\getDenominator is 0. Returns NAN (Not A Number) float value if the \Prado\Util\Math\getDenominator is zero. |
public
|
offsetExists(mixed $offset) : bool
Checks for the existence of the values TRational uses: 0, 1, 'numerator', and
'denominator'.
|
public
|
offsetGet(mixed $offset) : mixed
This is a convenience method for getting the numerator and denominator.
Index '0' and 'numerator' will get the \Prado\Util\Math\getNumerator, and Index '1' and 'denominator' will get the \Prado\Util\Math\getDenominator. |
public
|
offsetSet(mixed $offset, mixed $value) : void
This is a convenience method for setting the numerator and denominator.
Index '0' and 'numerator' will set the \Prado\Util\Math\setNumerator, and Index '1' and 'denominator' will set the \Prado\Util\Math\setDenominator. |
public
|
offsetUnset(mixed $offset) : void
This is a convenience method for resetting the numerator and denominator to
default. Index '0' and 'numerator' will reset the {@see setNumerator Numerator}
to "0", and Index '1' and 'denominator' will reset the {@see setDenominator
Denominator} to "1".
|
public
|
|
public
|
|
public
|
|
public
|
|
public
mixed
|
DEFAULT_TOLERANCE
|
1.0E-6
|
public
mixed
|
DENOMINATOR
|
'denominator'
|
public
mixed
|
NUMERATOR
|
'numerator'
|