Repository navigation
OnixLabs.Numerics.GenericMath
Provides generic mathematical functions that operate over any numeric type implementing the relevant generic numeric interfaces from System.Numerics.
| Namespace | OnixLabs.Numerics |
| Assembly | OnixLabs.Numerics.dll (NuGet) |
| Inheritance |
object → GenericMath
|
public static class GenericMathGenericMath collects mathematical operations that are not directly available through BCL generic math interfaces, or that need special care to produce correct results across all numeric types.
Key design points:
-
Type-precision arithmetic —
Pow<T>andPow10<T>compute the result directly inTusing exponentiation by squaring. Routing through adoubleintermediary would cap accuracy at ~15–17 decimal digits regardless of the precision ofT. -
Unsigned-safe delta —
Delta<T>always subtracts the smaller operand from the larger, so unsigned integer types such asbyteanduintdo not wrap. -
BigInteger factorial —
Factorial<T>accumulates into aBigIntegercounter rather thanTto avoid overflow when iterating nearT.MaxValue. -
Tuple convenience —
MinMax<T>returns both the minimum and the maximum in a single call.
Add a using static OnixLabs.Numerics.GenericMath; directive to call the members unqualified.
using static OnixLabs.Numerics.GenericMath;
byte a = 3;
byte b = 200;
byte diff = Delta(a, b); // 197 — safe for unsigned typesusing static OnixLabs.Numerics.GenericMath;
BigInteger result = Factorial(10); // 3628800using static OnixLabs.Numerics.GenericMath;
BigDecimal value = BigDecimal.Parse("1.5");
BigDecimal cubed = Pow(value, 3); // 3.375 — computed at BigDecimal precisionusing static OnixLabs.Numerics.GenericMath;
decimal scale = Pow10<decimal>(6); // 1000000| Method | Returns | Description |
|---|---|---|
Delta<T>(T, T) |
T |
Computes the absolute difference between two values. |
Factorial<T>(T) |
BigInteger |
Computes the factorial of a non-negative integer value. |
IntegerLength<T>(T) |
int |
Returns the number of digits in the integral component of a value. |
MinMax<T>(T, T) |
(T Min, T Max) |
Returns both the minimum and maximum of two values. |
Pow<T>(T, int) |
T |
Raises a value to a non-negative integer exponent. |
Pow10<T>(int) |
T |
Raises 10 to a non-negative integer exponent. |
public static T Delta<T>(T left, T right) where T : INumber<T>Computes the delta, or absolute difference, between the specified numbers.
| Parameter | Type | Description |
|---|---|---|
left |
T |
The left number from which to compute the delta. |
right |
T |
The right number from which to compute the delta. |
| Type Parameter | Description |
|---|---|
T |
The underlying INumber<TSelf> type. |
Returns — The absolute difference between left and right.
Note The subtraction is ordered so that the smaller operand is always subtracted from the larger. This makes the result correct for unsigned integer types, which would otherwise wrap around when
leftis less thanright.
public static BigInteger Factorial<T>(T value) where T : IBinaryInteger<T>Computes the factorial of the specified IBinaryInteger<TSelf> value.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The non-negative integer value for which the factorial will be computed. |
| Type Parameter | Description |
|---|---|
T |
The underlying IBinaryInteger<TSelf> type. |
Returns — The factorial of value as a BigInteger.
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
value is less than zero. |
Note The accumulator and loop counter are both
BigInteger, notT, to avoid overflow when iterating nearT.MaxValue(for example, abytecounter would wrap from 255 to 0 and loop forever).
public static int IntegerLength<T>(T value) where T : INumberBase<T>Obtains the number of digits in the integral component of the specified INumberBase<TSelf> value. Fractional digits and the sign are not counted.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The value from which to obtain the length of the integral component. |
| Type Parameter | Description |
|---|---|
T |
The underlying INumberBase<TSelf> type. |
Returns — The number of digits in the integral component of value.
public static (T Min, T Max) MinMax<T>(T left, T right) where T : INumber<T>Obtains the minimum and maximum values from the specified left and right values in a single call.
| Parameter | Type | Description |
|---|---|---|
left |
T |
The left INumber<TSelf> value to compare. |
right |
T |
The right INumber<TSelf> value to compare. |
| Type Parameter | Description |
|---|---|
T |
The underlying INumber<TSelf> type. |
Returns — A (T Min, T Max) tuple containing the lesser and greater of the two values.
public static T Pow<T>(T value, int exponent) where T : INumber<T>Computes value raised to the power of a non-negative integer exponent using exponentiation by squaring, performing all arithmetic in T.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The base value to raise. |
exponent |
int |
The exponent to which value is raised. Must be greater than or equal to zero. |
| Type Parameter | Description |
|---|---|
T |
The numeric type. Must implement INumber<T>. |
Returns — A value of type T equal to value raised to the power of exponent, computed at T's precision.
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
exponent is less than zero. |
Note Computing in
Tpreserves the full precision of the target type. Using adoubleintermediary (for exampleT.CreateChecked(Math.Pow(base, exp))) would cap accuracy at ~15–17 significant decimal digits regardless of how preciseTis.
public static T Pow10<T>(int exponent) where T : INumber<T>Computes 10 raised to the power of a non-negative integer exponent using exponentiation by squaring, returning the result as type T.
| Parameter | Type | Description |
|---|---|---|
exponent |
int |
The exponent to raise 10 to. Must be greater than or equal to zero. |
| Type Parameter | Description |
|---|---|
T |
The numeric type. Must implement INumber<T>. |
Returns — A value of type T equal to 10 raised to the power of exponent.
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
exponent is less than zero. |
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions