Class IntExtensions
Inherited Members
Namespace: MathLib
Assembly: MathLib.dll
Syntax
public static class IntExtensions
Methods
| Edit this page View SourceAbs(int)
Returns the absolute value of the specified integer
.
Declaration
public static int Abs(this int integer)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The 32-bit signed integer to check. |
Returns
Type | Description |
---|---|
int | The absolute value of |
IsEven(int)
Indicates whether the specified integer
is even.
Declaration
public static bool IsEven(this int integer)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The 32-bit signed integer to check. |
Returns
Type | Description |
---|---|
bool |
IsOdd(int)
Indicates whether the specified integer
is odd.
Declaration
public static bool IsOdd(this int integer)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The 32-bit signed integer to check. |
Returns
Type | Description |
---|---|
bool |
IsPowerOfTwo(int)
Indicates whether the specified integer
is a power of two.
Declaration
public static bool IsPowerOfTwo(this int integer)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The integer to test. |
Returns
Type | Description |
---|---|
bool | true
|
Remarks
If the integer
is negative the result is
Mod(int, int)
Computes the modulus of an int in the same way as in languages like Python, Haskell, Julia and Matlab. The result follows the mathematical definition of modulus where the remainder always has the same sign as the divisor, ensuring predictable results for both positive and negative values.
Declaration
public static int Mod(this int integer, int modulus)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The int value to compute the modulus for. |
int | modulus | The modulus value. Must be non-zero. |
Returns
Type | Description |
---|---|
int | The remainder when |
Remarks
The result of the modulus operation is adjusted based on the sign of modulus
:
When modulus
is positive, the result is in the range [0, modulus)
When modulus
is negative, the result is in the range (modulus, 0].
This method provides consistent behavior for modulus operations, similar to how it is implemented in Python, Haskell, Julia, and Matlab.
Examples
Examples:
int result1 = 10.Mod(3); // 1
int result2 = (-10).Mod(3); // 2
int result3 = 10.Mod(-3); // -2
int result4 = (-10).Mod(-3); // -1
Exceptions
Type | Condition |
---|---|
DivideByZeroException | Thrown when |
ModMinAbs(int, int)
Modulo operation yielding the remainder (positive or negative) with the smallest absolute value, preserving congruence under the specified modulus.
Declaration
public static int ModMinAbs(this int integer, int modulus)
Parameters
Type | Name | Description |
---|---|---|
int | integer | Dividend, an integer. |
int | modulus | Modulus, a positive integer. |
Returns
Type | Description |
---|---|
int | The integer |
Remarks
For both positive and negative integer
, the result minimizes |r|
, providing a "balanced" or symmetric result.
This is useful in contexts where closeness to zero is desired, such as balanced systems or modular
arithmetic with minimal magnitude deviation.
For example:
4 Mod 7
yields-3
instead of4
, as-3
is closer to zero.9 Mod 7
yields2
, as2
is already closest to zero.
Sign(int)
Returns a value indicating the sign of the specified integer
.
Declaration
public static int Sign(this int integer)
Parameters
Type | Name | Description |
---|---|---|
int | integer | The 32-bit signed integer to check. |
Returns
Type | Description |
---|---|
int | A number that indicates the sign of |