Kirin 2bd919e148 commit | %!s(int64=2) %!d(string=hai) anos | |
---|---|---|
.. | ||
lib | %!s(int64=2) %!d(string=hai) anos | |
test | %!s(int64=2) %!d(string=hai) anos | |
LICENSE.md | %!s(int64=2) %!d(string=hai) anos | |
README.md | %!s(int64=2) %!d(string=hai) anos | |
composer.json | %!s(int64=2) %!d(string=hai) anos |
This library is a common interface for php_gmp and php_bcmath modules. It automatically detects supported modules and uses the best of them (gmp>bcmath). Gmp is a lot faster, but is also missing on many hosting services -- that is why this wrapper has been created. It is used for example in encryption functions of the PrivMX WebMail software.
You can install this library via Composer:
composer require simplito/bigint-wrapper-php
If you want to force using a specific implementation, then define constant S_MATH_BIGINTEGER_MODE - set it to "gmp" or "bcmath". If you do not do this, mode of operation and the constant will be set automatically.
If there are no gmp and bcmath modules, an exception will be thrown. If you want to prevent this, then simply define S_MATH_BIGINTEGER_QUIET constant.
All functions of this library are implemented as members of class BigInteger, which is located under BI namespace. Instances of BigInteger are immutable - member functions usually return new instances of the BigInteger class.
To make the below documentation more readable we use the "ConvertibleToBi" type symbol, which in reality can be one of the following types:
If you have a non-decimal string and want to use it -- first you have to convert it to BigInteger class using:
new BigInteger($myNonDecimalString, $baseOfMyNonDecimalString)
Creates a new instance of BigInteger. If you pass an invalid value, an exception will be thrown. If $base === true then passed $value will be used without any check and conversion. Supported bases: 2, 10, 16, 256.
Creates a new BigInteger instance in the same way as constructor, but if there is an error, false will be returned instead of throwing an exception.
Adds numbers
Subtracts numbers
Multiplies numbers
Divides numbers
Returns a remainder of the division of numbers. The remainder has the sign of the divided number.
Divides numbers and returns quotient and remainder. Returns an array(), with the first element being quotient, and the second being remainder.
The "division modulo" operation. The result is always non-negative, the sign of divider is ignored.
Calculates greatest common divisor
Inverses by modulo, returns false if inversion does not exist.
The power function.
The modular power function.
Returns absolute value.
Negates the number
Bitwise AND.
Bitwise OR
Bitwise XOR
Sets bit at given index
Tests if a bit at given index is set
Scans for 0, and returns index of first found bit
Scans for 1, and returns index of first found bit
Compares numbers, returns <0, 0, >0
Checks if numbers are equal
Sign of number, returns -1, 0, 1
Converts to number (use only with small 32/64bit numbers)
Converts to decimal string
Converts to hex string
Converts to binary string
Converts to bits string (0 and 1 characters)
Converts to string using given base (supported bases 2-62, 256)