Skip to content

Big Int

Spice
import "std/math/big-int";

BigInt struct

Integer of arbitrary size

Constructors

ctor

Spice
public p BigInt.ctor(long value = 0l)

Create a BigInt with the given value

Parameters

Name Type Description
value long Value (default: 0l)

ctor

Spice
public p BigInt.ctor(int value)

Create a BigInt with the given value

Parameters

Name Type Description
value int Value

Methods

isZero

Spice
public inline f<bool> BigInt.isZero()

Returns: bool — Whether the value is zero

isNegative

Spice
public inline f<bool> BigInt.isNegative()

Returns: bool — Whether the value is less than zero

getSignum

Spice
public inline f<int> BigInt.getSignum()

Returns: int — -1, 0 or 1 for a negative value, zero and a positive value

isOdd

Spice
public inline f<bool> BigInt.isOdd()

Returns: bool — Whether the value is odd

getBitLength

Spice
public f<unsigned long> BigInt.getBitLength()

Returns: unsigned long — Number of bits of the absolute value, without leading zeros. 0 for zero

testBit

Spice
public f<bool> BigInt.testBit(unsigned long index)

Check a bit of the absolute value

Parameters

Name Type Description
index unsigned long Index of the bit, 0 is the least significant one

Returns: bool — Whether the bit is set

getLowestSetBit

Spice
public f<long> BigInt.getLowestSetBit()

Returns: long — Index of the least significant set bit of the absolute value, -1 for zero

fitsInLong

Spice
public f<bool> BigInt.fitsInLong()

Returns: bool — Whether the value is in the range of long

getAbsoluteLowBits

Spice
public f<unsigned long> BigInt.getAbsoluteLowBits()

Returns: unsigned long — The lowest 64 bits of the absolute value

toLong

Spice
public f<long> BigInt.toLong()

Convert to long. Values outside of the range of long wrap around, like in a cast: the result has the lowest 64 bits of the two's complement of the value

Returns: long — Value as long

compare

Spice
public f<int> BigInt.compare(const BigInt& other)

Compare with another BigInt

Parameters

Name Type Description
other const BigInt& Other BigInt

Returns: int — -1, 0 or 1, if this value is less than, equal to or greater than the other one

getAbsolute

Spice
public inline f<BigInt> BigInt.getAbsolute()

Returns: BigInt — Absolute value

toString

Spice
public f<String> BigInt.toString(int base = 10)

Format in the given base, with lower case letters for the digits from 10 on and a leading '-' for negative values

Parameters

Name Type Description
base int Base from 2 to 36 (default: 10)

Returns: String — Text of the value

add

Spice
public inline p BigInt.add(const BigInt& summand)

Add another BigInt to this one

Parameters

Name Type Description
summand const BigInt& Summand

add

Spice
public p BigInt.add(long summand)

Add a long to this BigInt

Parameters

Name Type Description
summand long Summand

subtract

Spice
public inline p BigInt.subtract(const BigInt& subtrahend)

Subtract another BigInt from this one

Parameters

Name Type Description
subtrahend const BigInt& Subtrahend

subtract

Spice
public p BigInt.subtract(long subtrahend)

Subtract a long from this BigInt

Parameters

Name Type Description
subtrahend long Subtrahend

multiply

Spice
public p BigInt.multiply(const BigInt& factor)

Multiply this BigInt with another one

Parameters

Name Type Description
factor const BigInt& Factor

multiply

Spice
public p BigInt.multiply(long factor)

Multiply this BigInt with a long

Parameters

Name Type Description
factor long Factor

pow

Spice
public p BigInt.pow(unsigned long exponent)

Raise this BigInt to the given power

Parameters

Name Type Description
exponent unsigned long Exponent. 0 gives 1, also for zero

divideWithRemainder

Spice
public p BigInt.divideWithRemainder(const BigInt& divisor, BigInt& remainder)

Divide this BigInt by another one, rounding towards zero like the division of long, and get the remainder, which has the sign of the dividend. Panics, if the divisor is zero or the remainder is to be stored in this BigInt, which gets the quotient.

Parameters

Name Type Description
divisor const BigInt& Divisor
remainder BigInt& Gets the remainder. Has to be another BigInt than this one

divide

Spice
public p BigInt.divide(const BigInt& divisor)

Divide this BigInt by another one, rounding towards zero like the division of long. Panics, if the divisor is zero.

Parameters

Name Type Description
divisor const BigInt& Divisor

divide

Spice
public f<long> BigInt.divide(long divisor)

Divide this BigInt by a long, rounding towards zero like the division of long. Panics, if the divisor is zero.

Parameters

Name Type Description
divisor long Divisor

Returns: long — Remainder, which has the sign of the dividend

remainder

Spice
public p BigInt.remainder(const BigInt& divisor)

Replace this BigInt by the remainder of its division by another one. The remainder has the sign of the dividend, like the % operator of long. Panics, if the divisor is zero.

Parameters

Name Type Description
divisor const BigInt& Divisor

negate

Spice
public inline p BigInt.negate()

Negate this BigInt

shiftLeft

Spice
public inline p BigInt.shiftLeft(unsigned long bits)

Shift this BigInt to the left, which multiplies it by 2^bits

Parameters

Name Type Description
bits unsigned long Number of bits

shiftRight

Spice
public f<bool> BigInt.shiftRight(unsigned long bits)

Shift this BigInt to the right, which divides it by 2^bits, rounding towards negative infinity like the shift of long

Parameters

Name Type Description
bits unsigned long Number of bits

Returns: bool — Whether any of the shifted out bits of the absolute value was set

Functions

getBigIntFromUnsigned

Spice
public f<BigInt> getBigIntFromUnsigned(unsigned long value)

Create a BigInt with the value of an unsigned long. A constructor would be ambiguous with the one for long

Parameters

Name Type Description
value unsigned long Value

Returns: BigInt — BigInt with the value

parseBigInt

Spice
public f<Result<BigInt>> parseBigInt(const String& text, int base = 10)

Parse a BigInt from text: an optional sign followed by at least one digit in the given base. Letters are digits from 10 on, ignoring the case. Other chars, also whitespace, are not allowed.

Parameters

Name Type Description
text const String& Text
base int Base from 2 to 36 (default: 10)

Returns: Result<BigInt> — Parsed BigInt or an error, if the text is no integer in the base

Operators

operator+

Spice
public f<BigInt> operator+(const BigInt& lhs, const BigInt& rhs)

Add two BigInts

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: BigInt — New BigInt with the sum

operator-

Spice
public f<BigInt> operator-(const BigInt& lhs, const BigInt& rhs)

Subtract a BigInt from another one

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: BigInt — New BigInt with the difference

operator*

Spice
public f<BigInt> operator*(const BigInt& lhs, const BigInt& rhs)

Multiply two BigInts

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: BigInt — New BigInt with the product

operator/

Spice
public f<BigInt> operator/(const BigInt& lhs, const BigInt& rhs)

Divide a BigInt by another one, rounding towards zero like the division of long. Panics, if the divisor is zero.

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: BigInt — New BigInt with the quotient

operator

Spice
public f<BigInt> operator<<(const BigInt& lhs, unsigned long bits)

Shift a BigInt to the left, which multiplies it by 2^bits

Returns: BigInt — New, shifted BigInt

operator>>(const BigInt& lhs, unsigned long bits)

Spice
public f<BigInt> operator>>(const BigInt& lhs, unsigned long bits)

Shift a BigInt to the right, which divides it by 2^bits, rounding towards negative infinity like the shift of long

Returns: BigInt — New, shifted BigInt

operator+=

Spice
public p operator+=(BigInt& lhs, const BigInt& rhs)

Add a BigInt to lhs, in place

Parameters

Name Type Description
lhs BigInt&
rhs const BigInt&

operator-=

Spice
public p operator-=(BigInt& lhs, const BigInt& rhs)

Subtract a BigInt from lhs, in place

Parameters

Name Type Description
lhs BigInt&
rhs const BigInt&

operator*=

Spice
public p operator*=(BigInt& lhs, const BigInt& rhs)

Multiply lhs with a BigInt, in place

Parameters

Name Type Description
lhs BigInt&
rhs const BigInt&

operator/=

Spice
public p operator/=(BigInt& lhs, const BigInt& rhs)

Divide lhs by a BigInt, in place, rounding towards zero like the division of long. Panics, if the divisor is zero.

Parameters

Name Type Description
lhs BigInt&
rhs const BigInt&

operator==

Spice
public f<bool> operator==(const BigInt& lhs, const BigInt& rhs)

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: bool — Whether both BigInts have the same value

operator!=

Spice
public f<bool> operator!=(const BigInt& lhs, const BigInt& rhs)

Parameters

Name Type Description
lhs const BigInt&
rhs const BigInt&

Returns: bool — Whether the BigInts have different values

operator

Spice
public f<bool> operator<(const BigInt& lhs, const BigInt& rhs)

Returns: bool — Whether lhs is less than rhs

operator

Spice
public f<bool> operator<=(const BigInt& lhs, const BigInt& rhs)

Returns: bool — Whether lhs is less than or equal to rhs

operator>(const BigInt& lhs, const BigInt& rhs)

Spice
public f<bool> operator>(const BigInt& lhs, const BigInt& rhs)

Returns: bool — Whether lhs is greater than rhs

operator>=(const BigInt& lhs, const BigInt& rhs)

Spice
public f<bool> operator>=(const BigInt& lhs, const BigInt& rhs)

Returns: bool — Whether lhs is greater than or equal to rhs