Core\BigInt::of
Core\BigInt::of(int $value): Core\BigIntCore\BigInt::of( int $value, ): Core\BigInt
Builds a Core\BigInt from a plain int.
A Core\BigInt is a whole number of any size. An int is a whole number between -9223372036854775808 and 9223372036854775807. of converts an int to a Core\BigInt. Every int, including both ends of that range, converts exactly.
This is where most Core\BigInt values start. You build one from a number your program already has, then add, multiply or shift it. The result grows as large as it needs to, and an int would throw an error there instead.
For a number your program has as text, use Core\BigInt::parse. For one that fits a uint but not an int, use Core\BigInt::ofUint.
Parameters
$valueintTheintto convert. Everyintconverts exactly, including the smallest and the largest.
Return value
Returns Core\BigInt. A BigInt equal to $value.
Examples
Building one from an int
<?nvs// Shows how a program turns a plain `int` into a `Core\BigInt`.
// `of` takes any `int`. This prints 4500.echo Core\BigInt::of(4500), "\n";
// Negative numbers and zero work the same way. This prints -4500 and 0.echo Core\BigInt::of(-4500), "\n";echo Core\BigInt::of(0), "\n";
// Both ends of the `int` range convert exactly. This prints// 9223372036854775807.echo Core\BigInt::of(9223372036854775807), "\n";
// The smallest `int` is written as `-9223372036854775807 - 1`. The number on// its own does not fit an `int`. This prints -9223372036854775808.echo Core\BigInt::of(-9223372036854775807 - 1), "\n";4500-450009223372036854775807-9223372036854775808Growing past what an int holds
<?nvs// Shows why a program moves a number to `Core\BigInt`. An `int` stops at its// largest value, and a `Core\BigInt` keeps going.
int $largest = 9223372036854775807;
// Doubling the largest `int` throws an error, because the result does not fit// an `int`. This prints "too large for an int".try { echo $largest * 2, "\n";} catch (ArithmeticError $overflow) { echo "too large for an int\n";}
// The same number as a `Core\BigInt` doubles with no limit. This prints// 18446744073709551614.echo Core\BigInt::of($largest)->multipliedBy(Core\BigInt::of(2)), "\n";
// It keeps going. This prints a number of 38 digits.echo Core\BigInt::of($largest)->multipliedBy(Core\BigInt::of($largest)), "\n";too large for an int1844674407370955161485070591730234615847396907784232501249A total that cannot overflow
<?nvs// Shows a running total a program can trust. Each amount fits an `int`, and// the total of them does not.
class Totals{ // Returns the sum of $amounts, which may be larger than any `int`. public static function sum(array<int> $amounts): Core\BigInt { var $total = Core\BigInt::of(0); foreach ($amounts as int $amount) { $total = $total->plus(Core\BigInt::of($amount)); } return $total; }}
array<int> $amounts = [ 9000000000000000000, 9000000000000000000, 500000000000000000,];
// Adding these three as `int` would throw an error at the second one. As a// `Core\BigInt` the total is exact, and this prints 18500000000000000000.echo Totals::sum($amounts), "\n";18500000000000000000