Download the PHP package scruoge/lib-fractions without Composer
On this page you can find all versions of the php package scruoge/lib-fractions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download scruoge/lib-fractions
More information about scruoge/lib-fractions
Files in scruoge/lib-fractions
Package lib-fractions
Short Description Library for precision math calculations based on natural fractions (operates on rational domain)
License MIT
Informations about the package lib-fractions
lib-fractions
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.
Features
- 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
Installation
Install via Composer:
Requirements
- PHP 8.0 or higher
Quick Start
API Reference
Constructor Methods
create(int $significand, int $exponent = 0, int $denominator = 1): RationalExp
Creates a rational number with explicit components.
fromNumber(int|float $number, int $digits = -1, int $roundMode = PHP_ROUND_HALF_UP): RationalExp
Creates a rational number from an integer or float.
zero(): RationalExp and one(): RationalExp
Convenience methods for common values.
Arithmetic Operations
All arithmetic operations return a new RationalExp instance:
Utility Methods
equals(RationalExp $that): bool
Checks if two rational numbers are equal.
toFloat(): float
Converts the rational number to a float (may lose precision).
jsonSerialize(): array
Serializes the rational number for JSON encoding.
Advanced Usage
Working with Fractions
Precision Control
When converting from floats, you can control precision and rounding:
Financial Calculations
Perfect for monetary calculations where precision is crucial:
Development
Setup
Clone the repository and install dependencies:
Running Tests
Code Style
This project uses PHP-CS-Fixer for code formatting:
Project Structure
Use Cases
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
Contributing
- 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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Sergey N. Kruk - [email protected]
For more examples and detailed documentation, please refer to the test files in the test/ directory.