func AddDelta
Sets z to the sum x + y, where z and x are uint256s and y is an int256.
If the y is positive, it adds y.value to x. otherwise, it subtracts y.Abs() from x.
The int256 package provides a 256-bit signed interger type for gno, supporting arithmetic operations and bitwise mani...
The int256 package provides a 256-bit signed interger type for gno, supporting arithmetic operations and bitwise manipulation.
It designed for applications that require high-precision arithmetic beyond the standard 64-bit range.
## Features
## Notes
This package provides three different division and modulus operations:
Truncated division (Div, Rem) is the most common implementation in modern processors and programming languages. It rounds quotients towards zero and the remainder always has the same sign as the dividend.
Floored division (Quo, Mod) always rounds quotients towards negative infinity. This ensures that the modulus is always non-negative for a positive divisor, which can be useful in certain algorithms.
Euclidean division (DivE, ModE) ensures that the remainder is always non-negative, regardless of the signs of the dividend and divisor. This has several mathematical advantages:
[+] Currently, ModE and Mod are shared the same implementation.
## Performance considerations:
## Usage guidelines:
Note: When working with negative numbers, be aware of the differences in behavior between these division types, especially at the boundaries of integer ranges.
## References
Daan Leijen, “Division and Modulus for Computer Scientists”: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
Sets z to the sum x + y, where z and x are uint256s and y is an int256.
If the y is positive, it adds y.value to x. otherwise, it subtracts y.Abs() from x.
Sets z to the sum x + y, where z and x are uint256s and y is an int256.
This function returns true if the addition overflows, false otherwise.
FromDecimal creates a new Int from a decimal string representation. It handles both positive and negative values.
This function is useful for parsing user input or reading numeric data from text-based formats.
MustFromDecimal is similar to FromDecimal but panics if the input string is not a valid decimal representation.
New creates and returns a new Int initialized to zero.
NewInt allocates and returns a new Int set to the value of the provided int64.
One returns a new Int initialized to one.
This function is convenient for operations that require a unit value, such as incrementing or serving as an identity element in multiplication.
Zero returns a new Int initialized to 0.
This function is useful for creating a starting point for calculations or when an explicit zero value is needed.
Abs returns the absolute value of z.
Add adds two int256 values and saves the result in z.
AddUint256 adds int256 and uint256 values and saves the result in z.
And sets z to the bitwise AND of x and y and returns z.
The bitwise AND operation results in a value that has a bit set only if both corresponding bits of the operands are set.
Clone creates a new Int identical to z
Cmp compares z and x and returns:
Div performs integer division z = x / y and returns z. If y == 0, it panics with a "division by zero" error.
This function handles signed division using two's complement representation:
Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):
Let x = -6 (11111010 in two's complement) and y = 3 (00000011)
Step 2: Determine signs
Step 3: Calculate absolute values
1|x| = 6: 11111010 -> 00000110
2 NOT: 00000101
3 +1: 00000110
4
5|y| = 3: 00000011 (already positive)
Step 4: Unsigned division
16 / 3 = 2: 00000010
Step 5: Adjust sign (x and y have different signs)
Note: This implementation rounds towards zero, as is standard in Go.
DivE performs Euclidean division of x by y, setting z to the quotient and returning z. If y == 0, it panics with a "division by zero" error.
Euclidean division satisfies the following properties:
FromUint256 sets the Int to the value of the provided Uint256.
This method allows for conversion from unsigned 256-bit integers to signed integers.
Int64 returns the lower 64-bits of z
IsNeg returns true if z < 0
IsZero returns true if z == 0
Lsh sets z to the result of left-shifting x by n bits and returns z.
Left shift operation moves all bits in the operand to the left by the specified number of positions. Bits shifted out on the left are discarded, and zeros are shifted in on the right.
Mod sets z to the modulus x%y for y != 0 and returns z. The result (z) has the same sign as the divisor y.
ModE computes the Euclidean modulus of x by y, setting z to the result and returning z. If y == 0, it panics with a "division by zero" error.
The Euclidean modulus is always non-negative and satisfies:
10 <= x mod y < |y|
Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):
Case 1: Let x = -7 (11111001 in two's complement) and y = 3 (00000011)
Step 1: Compute remainder (using Rem)
1Result of Rem: -1 (11111111 in two's complement)
Step 2: Adjust sign (result is negative, y is positive)
Final result: 2 (00000010)
Case 2: Let x = -7 (11111001 in two's complement) and y = -3 (11111101 in two's complement)
Step 1: Compute remainder (using Rem)
1Result of Rem: -1 (11111111 in two's complement)
Step 2: Adjust sign (result is negative, y is negative)
1No adjustment needed
Final result: -1 (11111111 in two's complement)
Note: This implementation ensures that the result always has the same sign as y, which is different from the Rem operation.
Mul multiplies two int256 values and saves the result in z.
It considers the signs of the operands to determine the sign of the result.
Neg sets z to -x and returns z.)
NilToZero returns the Int if it's not nil, or a new zero-valued Int otherwise.
This method is useful for safely handling potentially nil Int pointers, ensuring that operations always have a valid Int to work with.
Not sets z to the bitwise NOT of x and returns z.
The bitwise NOT operation flips each bit of the operand.
Or sets z to the bitwise OR of x and y and returns z.
The bitwise OR operation results in a value that has a bit set if at least one of the corresponding bits of the operands is set.
Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):
Let x = -7 (11111001 in two's complement) and y = 3 (00000011)
Step 2: Determine signs
Step 3: Calculate absolute values
1|x| = 7: 11111001 -> 00000111
2 NOT: 00000110
3 +1: 00000111
4
5|y| = 3: 00000011 (already positive)
Step 4: Unsigned division
17 / 3 = 2: 00000010
Step 5: Adjust sign (x and y have different signs)
Final result: -2 (11111110 in two's complement)
Note: This implementation rounds towards zero, as is standard in Go.
Rem sets z to the remainder x%y for y != 0 and returns z.
The function performs the following steps:
Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):
Let x = -7 (11111001 in two's complement) and y = 3 (00000011)
Step 2: Determine signs
Step 3: Calculate absolute values
1|x| = 7: 11111001 -> 00000111
2 NOT: 00000110
3 +1: 00000111
4
5|y| = 3: 00000011 (already positive)
Step 4: Unsigned division
Step 5: Adjust sign of remainder (x is negative)
Final result: -1 (11111111 in two's complement)
Note: The sign of the remainder is always the same as the sign of the dividend (x).
Rsh sets z to the result of right-shifting x by n bits and returns z.
Right shift operation moves all bits in the operand to the right by the specified number of positions. Bits shifted out on the right are discarded, and zeros are shifted in on the left.
Set sets z to x and returns z.
SetInt64 sets the Int to the value of the provided int64.
This method allows for easy conversion from standard Go integer types to Int, correctly handling both positive and negative values.
SetString sets the Int to the value represented by the input string. This method supports decimal string representations of integers and handles both positive and negative values.
SetFromUint256 converts a uint256.Uint to Int and sets the value to z.
SetUint64 sets the Int to the value of the provided uint64.
Sign determines the sign of the Int.
It returns -1 for negative numbers, 0 for zero, and +1 for positive numbers.
ToString returns a string representation of z in base 10. The string is prefixed with a minus sign if z is negative.
Sub subtracts two int256 values and saves the result in z.
SubUint256 subtracts uint256 and int256 values and saves the result in z.
Uint64 returns the lower 64-bits of z
Xor sets z to the bitwise XOR of x and y and returns z.
The bitwise XOR operation results in a value that has a bit set only if the corresponding bits of the operands are different.