Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Math Laravel Package

zendframework/zend-math

Zend Math provides math utilities for PHP, including big integer/decimal support, random number generation, and statistical helpers. Part of the Zend Framework ecosystem, it offers consistent, tested components for numeric operations.

View on GitHub
Deep Wiki
Context7

Big Integers

Zend\Math\BigInteger\BigInteger offers a class to manage arbitrary length integers. PHP supports integer numbers with a maximum value of PHP_INT_MAX, a value defined by your processor architecture and available memory. If you need to manage integers bigger than PHP_INT_MAX, you need to use external libraries or PHP extensions such as GMP or BC Math.

Zend\Math\BigInteger\BigInteger is able to manage big integers using either the GMP or the BC Math extensions as adapters.

Methods available

The mathematical functions implemented in Zend\Math\BigInteger\BigInteger are:

  • add($leftOperand, $rightOperand): add two big integers.
  • sub($leftOperand, $rightOperand): subtract two big integers.
  • mul($leftOperand, $rightOperand): multiply two big integers.
  • div($leftOperand, $rightOperand): divide two big integers (this method returns only the integer part of result).
  • pow($operand, $exp): raise one big integer using the other big integer as the exponent.
  • sqrt($operand): get the square root of a big integer.
  • abs($operand): get the absolute value of a big integer.
  • mod($leftOperand, $modulus): get the modulus of dividing one big integer by another.
  • powmod($leftOperand, $rightOperand, $modulus): raise a big integer using another big integer as the exponent, and reduce by the specified modulus.
  • comp($leftOperand, $rightOperand): compare two big integers. Returns < 0 if $leftOperand is less than $rightOperand; > 0 if $leftOperand is greater than $rightOperand; and 0 if they are equal.
  • intToBin($int, $twoc = false): convert a big integer into its binary number representation;
  • binToInt($bytes, $twoc = false): convert a binary number into a big integer.
  • baseConvert($operand, $fromBase, $toBase = 10): convert a big integer between arbitrary bases.

Examples

The example below illustrates using the BC Math adapter to calculate the sum of two random integers with 100 digits each.

use Zend\Math\BigInteger\BigInteger;
use Zend\Math\Rand;

$bigInt = BigInteger::factory('bcmath');

$x = Rand::getString(100, '0123456789');
$y = Rand::getString(100, '0123456789');

$sum = $bigInt->add($x, $y);
$len = strlen($sum);

printf("%{$len}s +\n%{$len}s =\n%s\n%s\n", $x, $y, str_repeat('-', $len), $sum);

Note that the big integers are managed using strings; even the result of the sum is represented as a string.

Next is an example using the BC Math adapter to generate the binary representation of a negative big integer containing 100 digits.

use Zend\Math\BigInteger\BigInteger;
use Zend\Math\Rand;

$bigInt = BigInteger::factory('bcmath');

$digits = 100;
$x = '-' . Rand::getString($digits, '0123456789');

$byte = $bigInt->intToBin($x);

printf(
    "The binary representation of a big integer with %d digits:\n%s\nis (in Base64 format): %s\n",
    $digits
    $x,
    base64_encode($byte)
);
printf("Length in bytes: %d\n", strlen($byte));

$byte = $bigInt->intToBin($x, true);

printf(
    "The two's complement binary representation of the big integer with %d digits:\n"
    . "%s\nis (in Base64 format): %s\n",
    $digits,
    $x,
    base64_encode($byte)
);
printf("Length in bytes: %d\n", strlen($byte));

The above generates the binary representation of the big integer number using the default binary format, and the two's complement representation (specified with the true parameter in the intToBin function).

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4