Big Int¶
| Spice | |
|---|---|
BigInt struct¶
Integer of arbitrary size
Constructors¶
ctor¶
| Spice | |
|---|---|
Create a BigInt with the given value
Parameters
| Name | Type | Description |
|---|---|---|
value |
long |
Value (default: 0l) |
ctor¶
| Spice | |
|---|---|
Create a BigInt with the given value
Parameters
| Name | Type | Description |
|---|---|---|
value |
int |
Value |
Methods¶
isZero¶
| Spice | |
|---|---|
Returns: bool — Whether the value is zero
isNegative¶
| Spice | |
|---|---|
Returns: bool — Whether the value is less than zero
getSignum¶
| Spice | |
|---|---|
Returns: int — -1, 0 or 1 for a negative value, zero and a positive value
isOdd¶
| Spice | |
|---|---|
Returns: bool — Whether the value is odd
getBitLength¶
| Spice | |
|---|---|
Returns: unsigned long — Number of bits of the absolute value, without leading zeros. 0 for zero
testBit¶
| Spice | |
|---|---|
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 | |
|---|---|
Returns: long — Index of the least significant set bit of the absolute value, -1 for zero
fitsInLong¶
| Spice | |
|---|---|
Returns: bool — Whether the value is in the range of long
getAbsoluteLowBits¶
| Spice | |
|---|---|
Returns: unsigned long — The lowest 64 bits of the absolute value
toLong¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Returns: BigInt — Absolute value
toString¶
| Spice | |
|---|---|
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 | |
|---|---|
Add another BigInt to this one
Parameters
| Name | Type | Description |
|---|---|---|
summand |
const BigInt& |
Summand |
add¶
| Spice | |
|---|---|
Add a long to this BigInt
Parameters
| Name | Type | Description |
|---|---|---|
summand |
long |
Summand |
subtract¶
| Spice | |
|---|---|
Subtract another BigInt from this one
Parameters
| Name | Type | Description |
|---|---|---|
subtrahend |
const BigInt& |
Subtrahend |
subtract¶
| Spice | |
|---|---|
Subtract a long from this BigInt
Parameters
| Name | Type | Description |
|---|---|---|
subtrahend |
long |
Subtrahend |
multiply¶
| Spice | |
|---|---|
Multiply this BigInt with another one
Parameters
| Name | Type | Description |
|---|---|---|
factor |
const BigInt& |
Factor |
multiply¶
| Spice | |
|---|---|
Multiply this BigInt with a long
Parameters
| Name | Type | Description |
|---|---|---|
factor |
long |
Factor |
pow¶
| Spice | |
|---|---|
Raise this BigInt to the given power
Parameters
| Name | Type | Description |
|---|---|---|
exponent |
unsigned long |
Exponent. 0 gives 1, also for zero |
divideWithRemainder¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Negate this BigInt
shiftLeft¶
| Spice | |
|---|---|
Shift this BigInt to the left, which multiplies it by 2^bits
Parameters
| Name | Type | Description |
|---|---|---|
bits |
unsigned long |
Number of bits |
shiftRight¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Add two BigInts
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BigInt& |
|
rhs |
const BigInt& |
Returns: BigInt — New BigInt with the sum
operator-¶
| Spice | |
|---|---|
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 | |
|---|---|
Multiply two BigInts
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BigInt& |
|
rhs |
const BigInt& |
Returns: BigInt — New BigInt with the product
operator/¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Add a BigInt to lhs, in place
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
BigInt& |
|
rhs |
const BigInt& |
operator-=¶
| Spice | |
|---|---|
Subtract a BigInt from lhs, in place
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
BigInt& |
|
rhs |
const BigInt& |
operator*=¶
| Spice | |
|---|---|
Multiply lhs with a BigInt, in place
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
BigInt& |
|
rhs |
const BigInt& |
operator/=¶
| Spice | |
|---|---|
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 | |
|---|---|
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BigInt& |
|
rhs |
const BigInt& |
Returns: bool — Whether both BigInts have the same value
operator!=¶
| Spice | |
|---|---|
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BigInt& |
|
rhs |
const BigInt& |
Returns: bool — Whether the BigInts have different values
operator¶
| Spice | |
|---|---|
Returns: bool — Whether lhs is less than rhs
operator¶
| Spice | |
|---|---|
Returns: bool — Whether lhs is less than or equal to rhs
operator>(const BigInt& lhs, const BigInt& rhs)¶
| Spice | |
|---|---|
Returns: bool — Whether lhs is greater than rhs
operator>=(const BigInt& lhs, const BigInt& rhs)¶
| Spice | |
|---|---|
Returns: bool — Whether lhs is greater than or equal to rhs