Search by

phpmath / fraction

waltertamboer

A PHP library to work with exact, immutable rational numbers.

Package info

github.com/phpmath/fraction

pkg:composer/phpmath/fraction

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-23 11:30 UTC

This package is auto-updated.

Last update: 2026-11-24 12:59:35 UTC


README

Latest Version on Packagist Software License CI Total Downloads

A PHP library to work with exact, immutable rational numbers. It is built on top of phpmath/biginteger, so numerators and denominators can be arbitrarily large.

Documentation

The full documentation is at https://phpmath.github.io/fraction/. Every change is listed in the changelog and every release on the releases page.

Install

composer require phpmath/fraction

Requirements

  • PHP 8.4 or newer
  • The gmp extension
  • phpmath/biginteger, which Composer installs for you

Usage

use PHPMath\Fraction\Fraction;

$a = Fraction::of(1, 3);
$b = Fraction::fromString('0.125'); // 1/8

echo $a->add($b);            // 11/24
echo $a->toDecimalString(5); // 0.33333

Features

  • Exact, immutable rational numbers, always stored in lowest terms
  • Arbitrarily large numerators and denominators
  • Arithmetic, comparison, powers and reciprocals
  • floor(), ceil(), round() and truncate()
  • Exact decimal strings and a correctly rounded toFloat()

Every exception implements FractionException, see the documentation for the full list.

Development

composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style

Contributing

All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.

Security

If you discover a security issue, please create an issue in the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.