bardoqi/bigmath

A PHP library to work with big integers and big decimals in most easy way.

0.0.2 2020-11-26 15:13 UTC

This package is auto-updated.

Last update: 2024-10-27 00:21:22 UTC


README

_______   __            __       __             __      __       
|       \ |  \          |  \     /  \           |  \    |  \      
| $$$$$$$\ \$$  ______  | $$\   /  $$  ______  _| $$_   | $$____  
| $$__/ $$|  \ /      \ | $$$\ /  $$$ |      \|   $$ \  | $$    \ 
| $$    $$| $$|  $$$$$$\| $$$$\  $$$$  \$$$$$$\\$$$$$$  | $$$$$$$\
| $$$$$$$\| $$| $$  | $$| $$\$$ $$ $$ /      $$ | $$ __ | $$  | $$
| $$__/ $$| $$| $$__| $$| $$ \$$$| $$|  $$$$$$$ | $$|  \| $$  | $$
| $$    $$| $$ \$$    $$| $$  \$ | $$ \$$    $$  \$$  $$| $$  | $$
 \$$$$$$$  \$$ _\$$$$$$$ \$$      \$$  \$$$$$$$   \$$$$  \$$   \$$
              |  \__| $$                                          
               \$$    $$                                          
                \$$$$$$                                           

BigMath

Latest Version on Packagist Software License Total Downloads

A PHP library to work with big integers. This library makes use of the GMP extension and bcmath to do its calculations.

Introduction

Maybe you ara a developer of blockchain using php. Maybe you often coding encrypt and decrypt. And you fund that there is no library easy to use for big integer or high precision number in php. Now you could coding in most easy way with the BigMath!

Install

Via Composer

$ composer require bardoqi/bigmath:dev-master

Usage

    //with pecl Operator overloading extension:
    $number = BInt('8273467836243255543265432745') + BInt('2');
    //without pecl Operator overloading extension:
    $number = BInt('8273467836243255543265432745')->add(BInt('2'));

Features

This library supports the following operations:

  • Big Intgeger and high precision decimal support ....Class BigInteger: using with GMP. ....Class BigDecminal: using with bcmath.

  • Global type converting functions; .... You need not use 'new' operator to create new object. .... You Only need use:

     //to get a new BigInteger instance of $var; 
     BInt($var); 
     //to get a new BigDecimal instance of $var; 
     BDec($var); 
  • Mothods for coding simple: max(); min(); even(); odd(); sign(); isOne(); isZero(); randomRange();
  • Mothods of Mathematics: abs(); divideRem; powMod(); squareRoot();
    factorial(); gcd();
  • Mothods for bit orpeator(Only in BigInteger) andBits(); orBits(); clearBits(); complement() invert(); setBit(); testBit(); scan0(); scan1();
  • Mothods of Math Theory(Only in BigInteger) isPrime(); jacobi(); legendre(); perfectSquare() popcount(); root();
  • Chain Operators support: You can just use:
    //with pecl Operator overloading extension:
    $x=($a+$b)*($c-$d);
    //without pecl Operator overloading extension:
    $x=BInt($a)->add(BInt($b))->multiply(BInt($c)->substract(BInt($d))); 		

Sample Code

    //with Operator overloading extension:
    //if sample:
    if(abs($big_g)>abs($big_m)){
        $big_g = $big_g % $big_m;
    }
    
    //while sample:
    While (rt_v!=1){
        $x = $rd_v/$rt_v;
        //...
    }
    
    //for sample:
    for($i=$xstart; $i<$xMax; $i += $step){
        $item = &$data[];
        $item['x']=$i;
    }
    
    //without Operator overloading extension:
    //if sample:
    //if(abs($big_g)>abs($big_m)){...}
    if ($big_g->abs()->gt($big_m->abs())){
        $big_g = $big_g->mod($big_m);
    }
    
    //while sample:
    //While (rt_v!=1){...}
    while (!($rt_v->isOne())){ //rt!=1
        //x=rd/rt
        $x = $rd_v->div($rt_v);
        //...
    }
    
    //for sample:
    //for($i=$xstart; $i<$xMax; $i += $step)
    for($i = BInt($xStart); $i->lt(BInt($xMax)); $i->plus($step)){
        $item = &$data[];
        $item['x']=$i;
    }
    
    //More samples please read the code in exanples! 

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Road Map

Add: '<<' and '>>' Operator to class BigInteger

Add: '~' Operator to class BigInteger

Add: trigonometric functions such as sin cos etc to class BigDecimal

Add: inverse trigonometric function such as asin acos etc to class BigDecimal

Add: hyperbolic trigonometric functions such as sinh cosh etc to class BigDecimal

Add: high precision math constant such as e and pi.

Add: rational number class

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please create an issue in the issue tracker.

Credits

License

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