A PHP library for precision mathematical calculations using rational numbers. This library operates on the rational domain, providing exact decimal representation without the precision loss inherent in floating-point arithmetic.
- Exact Precision: No floating-point rounding errors
- Rational Number Arithmetic: Operations on fractions with significand, denominator, and exponent
- Multiple Input Formats: Create rational numbers from integers, floats, or explicit fraction components
- JSON Serializable: Easy integration with APIs and data storage
- Comprehensive Operations: Addition, subtraction, multiplication, division, and comparison
- Flexible Rounding: Configurable rounding modes when converting from floats
Install via Composer:
composer require scruoge/lib-fractions- PHP 8.0 or higher
<?php
use Scruoge\Fractions\RationalExp;
// Create rational numbers
$a = RationalExp::fromNumber(2.3); // 2.3 as exact fraction
$b = RationalExp::create(120, -2); // 120 * 10^(-2) = 1.2
$c = RationalExp::zero(); // 0
$d = RationalExp::one(); // 1
// Perform arithmetic operations
$sum = $a->add($b); // 2.3 + 1.2 = 3.5
$difference = $a->sub($b); // 2.3 - 1.2 = 1.1
$product = $a->mul($b); // 2.3 * 1.2 = 2.76
$quotient = $a->div($b); // 2.3 / 1.2 = 1.916...
// Convert back to float
echo $sum->toFloat(); // 3.5
// Check equality
$isEqual = $a->equals($b); // falseCreates a rational number with explicit components.
// Creates 123 * 10^(-2) / 4 = 1.23/4 = 0.3075
$rational = RationalExp::create(123, -2, 4);Creates a rational number from an integer or float.
// Automatic precision detection
$auto = RationalExp::fromNumber(1234.2345);
// Fixed precision with rounding
$rounded = RationalExp::fromNumber(1234.2345, 3); // 3 decimal places
$roundDown = RationalExp::fromNumber(1234.2345, 3, PHP_ROUND_HALF_DOWN);Convenience methods for common values.
$zero = RationalExp::zero(); // 0
$one = RationalExp::one(); // 1All arithmetic operations return a new RationalExp instance:
$a = RationalExp::fromNumber(2.5);
$b = RationalExp::fromNumber(1.5);
$addition = $a->add($b); // 4.0
$subtraction = $a->sub($b); // 1.0
$multiplication = $a->mul($b); // 3.75
$division = $a->div($b); // 1.666...Checks if two rational numbers are equal.
$a = RationalExp::fromNumber(0.1);
$b = RationalExp::fromNumber(0.1);
$isEqual = $a->equals($b); // trueConverts the rational number to a float (may lose precision).
$rational = RationalExp::create(1, 0, 3);
$float = $rational->toFloat(); // 0.3333...Serializes the rational number for JSON encoding.
$rational = RationalExp::create(1, 2, 3);
$json = json_encode($rational);
// {"significand":1,"denominator":3,"exponent":2}// Create 3/8
$fraction = RationalExp::create(3, 0, 8);
// Create 1/3 (which cannot be represented exactly as a decimal)
$oneThird = RationalExp::create(1, 0, 3);When converting from floats, you can control precision and rounding:
$value = 1234.2345;
// Automatic precision (detects significant digits)
$auto = RationalExp::fromNumber($value);
// Fixed precision
$fixed = RationalExp::fromNumber($value, 2); // 1234.23
// Different rounding modes
$halfUp = RationalExp::fromNumber($value, 2, PHP_ROUND_HALF_UP); // 1234.23
$halfDown = RationalExp::fromNumber($value, 2, PHP_ROUND_HALF_DOWN); // 1234.23
$up = RationalExp::fromNumber($value, 2, PHP_ROUND_UP); // 1234.24Perfect for monetary calculations where precision is crucial:
$price = RationalExp::fromNumber(19.99);
$taxRate = RationalExp::fromNumber(0.08);
$quantity = RationalExp::fromNumber(3);
$subtotal = $price->mul($quantity); // 59.97
$tax = $subtotal->mul($taxRate); // 4.7976
$total = $subtotal->add($tax); // 64.7676
// Round to cents for final amount
$finalTotal = RationalExp::fromNumber($total->toFloat(), 2); // 64.77Clone the repository and install dependencies:
git clone https://github.com/scruoge/lib-fractions.git
cd lib-fractions
composer installcomposer test
# or
./vendor/bin/phpunitThis project uses PHP-CS-Fixer for code formatting:
composer cs-fix
# or
./vendor/bin/php-cs-fixer fixlib-fractions/
├── src/
│ └── RationalExp.php # Main rational number class
├── test/
│ └── RationalExpTest.php # Unit tests
├── composer.json # Project configuration
├── phpunit.xml # PHPUnit configuration
└── .php-cs-fixer.dist.php # Code style configuration
This library is particularly useful for:
- Financial applications where exact decimal representation is required
- Scientific calculations requiring rational number precision
- Data processing where floating-point errors must be avoided
- Mathematical applications working with fractions and ratios
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
composer test) - Fix code style (
composer cs-fix) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure all tests pass and follow the existing code style.
This project is licensed under the MIT License - see the LICENSE file for details.
Sergey N. Kruk - scruoge@gmail.com
For more examples and detailed documentation, please refer to the test files in the test/ directory.