shtse8/arbimath

Arbitrary precision mathematics library

dev-master 2025-04-06 01:37 UTC

This package is auto-updated.

Last update: 2025-04-06 01:40:57 UTC


README

Latest Stable Version Total Downloads License

Tired of floating-point inaccuracies ruining your calculations? Need to handle numbers larger than your server's RAM?

Standard PHP math functions can hit limitations quickly, leading to subtle bugs and precision errors, especially in critical applications like finance, scientific computing, or cryptography.

✨ Enter ArbiMath PHP: Your ultimate weapon for high-precision mathematics in PHP! ✨

ArbiMath PHP provides a robust, intuitive, and type-safe ArbiNumber class, built upon the battle-tested BCMath extension, allowing you to perform calculations with virtually unlimited precision. Say goodbye to precision headaches and hello to reliable results!

Why Choose ArbiMath PHP? 🤔

  • 🎯 Uncompromising Precision: Perform calculations with the exactness you demand. No more rounding errors, no more approximations. Perfect for financial systems, scientific simulations, and anywhere accuracy is paramount.
  • 🐘 Handle HUGE Numbers Effortlessly: Whether it's astronomical figures or complex cryptographic keys, ArbiMath PHP manages numbers of any magnitude with ease.
  • 💻 Clean & Intuitive API: A thoughtfully designed object-oriented interface makes complex math feel simple. Chain methods for elegant and readable code.
  • ✨ Optional Operator Overloading: Want to write $a + $b instead of $a->add($b)? Install the (optional) PECL Operator extension and enjoy natural mathematical syntax!
  • ⚡ Performance Optimized: Leverages the highly optimized, low-level BCMath extension for reliable speed.
  • ** nowoczesny PHP Ready:** Built for PHP 8.0+, embracing strict types and PSR-12 standards for modern development workflows.

Quick Start ⚡

<?php

use Shtse8\ArbiMath\ArbiNumber;

require 'vendor/autoload.php';

// Create numbers with insane precision or magnitude
$pi = new ArbiNumber('3.1415926535897932384626433832795028841971');
$largeNumber = new ArbiNumber('1.8573958822565E+50'); // Handles scientific notation!
$smallIncrement = new ArbiNumber('0.0000000000000000000000000000000000000001');

// Chain operations fluently
$result = $largeNumber->mul($pi)->add($smallIncrement)->pow('3');

echo "Result: " . $result->toString() . "\n";
// Need formatting?
echo "Formatted: " . $result->format(10) . "\n"; // Format with separators and 10 decimal places

// With PECL Operator (Optional)
// $result = ($largeNumber * $pi + $smallIncrement) ** 3;
// echo $result;

?>

Installation 🛠️

  1. Require via Composer:
    composer require shtse8/arbimath
  2. Ensure BCMath is Enabled: This extension is usually enabled by default, but if not:
    # Example for PHP 8.0 on Debian/Ubuntu:
    sudo apt install php8.0-bcmath
    # Or consult your OS/PHP documentation
  3. (Optional) Install PECL Operator for Operator Overloading: Note: This extension might require manual compilation and compatibility checks.
    # General steps (adapt for your system):
    # git clone https://github.com/php/pecl-php-operator
    # cd pecl-php-operator && phpize && ./configure && make && sudo make install
    # echo "extension=operator.so" | sudo tee /etc/php/8.0/mods-available/operator.ini
    # sudo ln -s /etc/php/8.0/mods-available/operator.ini /etc/php/8.0/cli/conf.d/20-operator.ini
    # sudo service php8.0-fpm reload # If using FPM

Core Methods Overview 📚

ArbiMath PHP offers a comprehensive set of methods:

  • Arithmetic: add(), sub(), mul(), div(), mod(), pow()
  • Comparison: isEqual(), isSmaller(), isGreater(), isSmallerOrEqual(), isGreaterOrEqual()
  • Rounding/Truncating: round(), floor(), ceil(), truncate()
  • Logarithms: ln(), log()
  • Other Utilities: abs(), isNegative(), isPositive(), inc(), dec(), format(), getHumanValue(), getHumanUnit(), duplicate(), toString() / __toString()

(Refer to the source code or future API documentation for detailed usage.)

❤️ Support The Project ❤️

If ArbiMath PHP helps you tackle complex calculations and saves you time, consider showing your appreciation! Your support helps maintain and improve this library.

Buy Me A Coffee

Contributing 🤝

Found a bug or have a feature request? Contributions are highly welcome! Please feel free to open an issue or submit a Pull Request.

License 📄

ArbiMath PHP is open-source software licensed under the MIT License.