API reference

Every public method of Fraction, grouped by purpose.

Every method returns a new Fraction unless the return type says otherwise. All methods belong to PHPMath\Fraction\Fraction, a final readonly class that implements Stringable. A numerator is the number above the line of a fraction and a denominator is the number below it. Both are BigInteger objects from PHPMath\BigInteger\BigInteger.

Construction#

Create a fraction from a numerator and a denominator, or from text. The constructor is private, so use one of these named constructors. The result is always normalised, which means it is in lowest terms and its denominator is positive.

of()#

public static function of(BigInteger|int|string $numerator, BigInteger|int|string $denominator = 1): self

Creates a normalised fraction. Strings must be integers and may have a leading sign. Use fromString() for fractions and decimals.

fromString()#

public static function fromString(string $value): self

Creates a fraction from text such as '3/4', '-2' or '0.125'. Decimals are converted exactly. Whitespace, exponents and radix prefixes are not accepted.

Reading values#

Read the numerator and the denominator, or convert the fraction to text.

numerator()#

public function numerator(): BigInteger

The numerator. It carries the sign of the fraction.

  • Returns: BigInteger

denominator()#

public function denominator(): BigInteger

The denominator. It is always positive.

  • Returns: BigInteger

__toString()#

public function __toString(): string

Returns numerator/denominator, or only the numerator for a whole number.

  • Returns: string

Arithmetic#

Add, subtract, multiply, divide, and take reciprocals and powers. All results are exact and normalised.

add()#

public function add(Fraction $value): self

Adds a fraction.

  • Parameters: $value the other operand
  • Returns: Fraction

subtract()#

public function subtract(Fraction $value): self

Subtracts a fraction.

  • Parameters: $value the other operand
  • Returns: Fraction

multiply()#

public function multiply(Fraction $value): self

Multiplies by a fraction.

  • Parameters: $value the other operand
  • Returns: Fraction

divide()#

public function divide(Fraction $value): self

Divides by a fraction.

negate()#

public function negate(): self

Flips the sign. Zero stays zero.

  • Returns: Fraction

abs()#

public function abs(): self

The absolute value.

  • Returns: Fraction

reciprocal()#

public function reciprocal(): self

Returns 1 divided by this fraction. The sign stays on the numerator.

pow()#

public function pow(int $exponent): self

Raises the fraction to an integer power. A negative exponent takes the reciprocal first. Any fraction to the power of zero is 1, including zero.

  • Parameters: $exponent any integer, which may be negative
  • Returns: Fraction
  • Throws: DivisionByZeroException if the fraction is zero and the exponent is negative

Comparison and sign#

Compare fractions and inspect their sign and kind.

compareTo()#

public function compareTo(Fraction $value): int

Compares with another fraction by exact value.

  • Parameters: $value the fraction to compare with
  • Returns: -1 if this is smaller, 0 if equal, 1 if greater

equals()#

public function equals(Fraction $value): bool

Tests whether two fractions have the same value.

  • Parameters: $value the fraction to compare with
  • Returns: bool

isZero()#

public function isZero(): bool

Tests whether the fraction is zero.

  • Returns: bool

isInteger()#

public function isInteger(): bool

Tests whether the fraction is a whole number, which means its denominator is 1.

  • Returns: bool

sign()#

public function sign(): Sign

The sign of the fraction.

  • Returns: PHPMath\BigInteger\Sign: Positive, Negative or Zero

Rounding#

Turn a fraction into a whole number. Each method returns a BigInteger.

floor()#

public function floor(): BigInteger

The largest integer that is less than or equal to the fraction.

  • Returns: BigInteger

ceil()#

public function ceil(): BigInteger

The smallest integer that is greater than or equal to the fraction. A result of zero has no minus sign.

  • Returns: BigInteger

truncate()#

public function truncate(): BigInteger

The integer part, rounded toward zero. A result of zero has no minus sign.

  • Returns: BigInteger

round()#

public function round(): BigInteger

The nearest integer. Halves are rounded away from zero. A result of zero has no minus sign.

  • Returns: BigInteger

Conversion#

Convert a fraction to a decimal string or a float.

toDecimalString()#

public function toDecimalString(int $scale): string

Writes the fraction as a decimal with exactly $scale decimals, rounded half away from zero. A value that rounds to zero has no minus sign.

  • Parameters: $scale the number of decimals, zero or more
  • Returns: string
  • Throws: InvalidScaleException if the scale is negative

toFloat()#

public function toFloat(): float

Returns the float that is nearest to the fraction. The result is correctly rounded for any size of numerator and denominator, and an exact tie goes to the float with an even last bit. A value above PHP_FLOAT_MAX that rounds up gives INF or -INF, subnormal results are rounded to the available precision, a value below half of the smallest subnormal gives 0.0, and negative zero is never returned. See Rounding and conversion for examples.

  • Returns: float