Table of Contents

Class MathUtils

Namespace
LfrlAnvil.Numerics
Assembly
LfrlAnvil.Core.dll

Contains helper math-related methods.

public static class MathUtils
Inheritance
MathUtils
Inherited Members

Methods

BigDivU128(ulong, ulong, uint)

Divides a 128-bit unsigned value by uint value.

[Pure]
public static (ulong QuotientHigh, ulong QuotientLow, ulong Remainder) BigDivU128(ulong leftHigh, ulong leftLow, uint right)

Parameters

leftHigh ulong

High 64 bits of the dividend.

leftLow ulong

Low 64 bits of the dividend.

right uint

32-bit divisor.

Returns

(ulong QuotientHigh, ulong QuotientLow, ulong Remainder)

Tuple representing division result as quotient and remainder.

Exceptions

DivideByZeroException

When the divisor is equal to 0.

BigDivU128(ulong, ulong, ulong)

Divides a 128-bit unsigned value by ulong value.

[Pure]
public static (ulong QuotientHigh, ulong QuotientLow, ulong Remainder) BigDivU128(ulong leftHigh, ulong leftLow, ulong right)

Parameters

leftHigh ulong

High 64 bits of the dividend.

leftLow ulong

Low 64 bits of the dividend.

right ulong

64-bit divisor.

Returns

(ulong QuotientHigh, ulong QuotientLow, ulong Remainder)

Tuple representing division result as quotient and remainder.

Exceptions

DivideByZeroException

When the divisor is equal to 0.

BigMulU128(ulong, ulong)

Multiples two ulong values.

[Pure]
public static (ulong High, ulong Low) BigMulU128(ulong left, ulong right)

Parameters

left ulong

First operand.

right ulong

Second operand.

Returns

(ulong High, ulong Low)

Tuple representing multiplication result.

Remarks

See BigMul(ulong, ulong, out ulong) for more information.

ConvertToFractions(IEnumerable<Percent>, Fraction)

Converts the provided range of percentages into an equivalent range of fractions, whose sum is equal to the specified targetSum and whose denominators are all equal to the targetSumDenominator.

[Pure]
public static Fraction[] ConvertToFractions(IEnumerable<Percent> percentages, Fraction targetSum)

Parameters

percentages IEnumerable<Percent>

Range of percentages to convert into fractions.

targetSum Fraction

Expected sum of resulting fractions and their Denominator definition.

Returns

Fraction[]

Range of fractions whose number is equal to the number of the provided percentages and whose sum is equal to the targetSum.

Exceptions

ArgumentOutOfRangeException

When targetSum is less than 0 or any of the provided percentages is less than or equal to 0.

Gcd(ulong, ulong)

Calculates the GCD (greatest common divisor) of two ulong values.

[Pure]
public static ulong Gcd(ulong a, ulong b)

Parameters

a ulong

First value.

b ulong

Second value.

Returns

ulong

Greatest common divisor.

Remarks

When the second value is equal to 0, then this method will return the first value.

Lcm(ulong, ulong)

Calculates the LCM (least common multiple) of two ulong values.

[Pure]
public static ulong Lcm(ulong a, ulong b)

Parameters

a ulong

First value.

b ulong

Second value.

Returns

ulong

Least common multiple.

ToSigned(ulong, int)

Converts the provided ulongvalue to long by using the sign stored in a separate variable.

[Pure]
public static long ToSigned(ulong value, int sign)

Parameters

value ulong

Value to convert.

sign int

Expected result's sign.

Returns

long

Provided value converted to long. Result will be negated when the provided sign is less than 0.

Exceptions

OverflowException

When the result causes an arithmetic overflow.

ToUnsigned(long, ref int)

Converts the provided longvalue to ulong and stores its sign in a separate variable.

public static ulong ToUnsigned(long value, ref int sign)

Parameters

value long

Value to convert.

sign int

ref parameter that gets negated when the provided value is negative.

Returns

ulong

Absolute value of the provided parameter as ulong.

UnsignedAbs(long)

Calculates an absolute value from the given parameter and converts it to ulong.

[Pure]
public static ulong UnsignedAbs(long value)

Parameters

value long

Value to calculate an absolute value from.

Returns

ulong

Absolute value as ulong.