MathLib Docs MathLib Docs
MathLib Docs MathLib Docs

Search Results for

    Edit this page

    Class IntExtensions

    Inheritance
    object
    IntExtensions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: MathLib
    Assembly: MathLib.dll
    Syntax
    public static class IntExtensions

    Methods

    | Edit this page View Source

    Abs(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 integer.

    | Edit this page View Source

    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

    true iff integer is even; otherwise, false.

    | Edit this page View Source

    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

    true iff integer is odd; otherwise, false.

    | Edit this page View Source

    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 iff integer is a power of two.

    Remarks

    If the integer is negative the result is

    | Edit this page View Source

    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 integer is divided by modulus, adjusted to follow the same sign as modulus.

    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 modulus is zero.

    | Edit this page View Source

    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 r closest to zero for which there exists an integer k such that n = k × mod + r, and -mod/2 ≤ r ≤ mod/2.

    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 of 4, as -3 is closer to zero.
    • 9 Mod 7 yields 2, as 2 is already closest to zero.
    | Edit this page View Source

    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 integer.