Repository navigation
OnixLabs.Numerics.NumberInfo
Represents component information about a rational number, decomposing it into its constituent parts: unscaled value, scale, significand, exponent, sign, and precision.
| Namespace | OnixLabs.Numerics |
| Assembly | OnixLabs.Numerics.dll (NuGet) |
| Inheritance |
object → ValueType → NumberInfo
|
| Implements |
IValueEquatable<NumberInfo>, IValueComparable<NumberInfo>, ISpanParsable<NumberInfo>, IFormattable
|
public readonly partial struct NumberInfo :
IValueEquatable<NumberInfo>,
IValueComparable<NumberInfo>,
ISpanParsable<NumberInfo>,
IFormattableNumberInfo captures the internal representation of a rational number as a pair of values: an unscaled value (a BigInteger that includes all significant digits and any trailing zeros) and a scale (a non-negative integer indicating how many digits from the right of the unscaled value form the fractional part). From these two values all other properties — Significand, Exponent, Precision, and Sign — are derived on demand.
NumberInfo is the underlying representation used by BigDecimal. The BigDecimal.ToNumberInfo() method exposes this representation, and every numeric type in OnixLabs.Numerics provides a ToNumberInfo() extension method via NumericsExtensions.
NumberInfo distinguishes two equality modes:
-
Strict equality — two values are equal only when their
UnscaledValueandScaleare both identical. For example,1.20and1.2are not strictly equal because their unscaled values differ (120vs12). This is the mode used byEquals(NumberInfo),Equals(object?), andGetHashCode(). -
Semantic equality — two values are equal when they represent the same mathematical quantity, regardless of trailing zeros or scale.
1.20and1.2are semantically equal. This is the mode used by the==and!=operators.
Both modes are exposed through the NumberInfoEqualityComparer type.
Note The static
Equals(NumberInfo, NumberInfo)overload and theEquals(NumberInfo, NumberInfoEqualityComparer)family allow callers to choose the comparer explicitly.
using OnixLabs.Numerics;
NumberInfo info = NumberInfo.Parse("123.450");
Console.WriteLine(info.UnscaledValue); // 123450
Console.WriteLine(info.Scale); // 3
Console.WriteLine(info.Significand); // 12345
Console.WriteLine(info.Exponent); // -2
Console.WriteLine(info.Precision); // 6
Console.WriteLine(info.Sign); // 1using OnixLabs.Numerics;
NumberInfo a = NumberInfo.Parse("1.20");
NumberInfo b = NumberInfo.Parse("1.2");
// Semantic equality: same numeric value, different scale
Console.WriteLine(a == b); // True
Console.WriteLine(NumberInfo.Equals(a, b, NumberInfoEqualityComparer.Semantic)); // True
// Strict equality: scale must also match
Console.WriteLine(a.Equals(b)); // False
Console.WriteLine(NumberInfo.Equals(a, b, NumberInfoEqualityComparer.Strict)); // False| Property | Type | Description |
|---|---|---|
Zero |
NumberInfo |
Gets a NumberInfo instance representing zero. |
One |
NumberInfo |
Gets a NumberInfo instance representing one. |
NegativeOne |
NumberInfo |
Gets a NumberInfo instance representing negative one. |
public static NumberInfo Zero { get; }Gets a NumberInfo instance representing zero (UnscaledValue = 0, Scale = 0).
public static NumberInfo One { get; }Gets a NumberInfo instance representing one (UnscaledValue = 1, Scale = 0).
public static NumberInfo NegativeOne { get; }Gets a NumberInfo instance representing negative one (UnscaledValue = -1, Scale = 0).
| Property | Type | Description |
|---|---|---|
UnscaledValue |
BigInteger |
Gets the unscaled value of the represented number, including any trailing zeros. |
Scale |
int |
Gets the scale of the represented number. |
Significand |
BigInteger |
Gets the significand of the represented number, with trailing zeros stripped. |
Exponent |
int |
Gets the exponent of the represented number. |
Precision |
int |
Gets the total number of significant digits in the represented number. |
Sign |
int |
Gets the sign of the represented number. |
public BigInteger UnscaledValue { get; }Gets the unscaled value of the represented number. The unscaled value is a signed BigInteger that includes any trailing zeros. When the represented number has a fractional component, trailing zeros in the unscaled value are considered significant — for example, 1.20 has UnscaledValue = 120 and Scale = 2.
public int Scale { get; }Gets the scale of the represented number. The scale is a non-negative integer indicating how many digits from the right-hand side of UnscaledValue form the fractional component of the number. A scale of 0 means the number is an integer. For example, 123.45 has Scale = 2.
public BigInteger Significand { get; }Gets the significand of the represented number. The significand is the signed UnscaledValue with all trailing zeros stripped. For example, 123.450 has UnscaledValue = 123450 and Significand = 12345. When UnscaledValue is zero, Significand is also zero.
public int Exponent { get; }Gets the exponent of the represented number, expressed as a positive, negative, or zero integer. The exponent reflects the power of ten required to recover the original value from the Significand. For example, 123.450 has Significand = 12345 and Exponent = -2, because 12345 × 10⁻² = 123.45. When UnscaledValue is zero, Exponent is 0.
public int Precision { get; }Gets the total number of significant digits in the represented number. If UnscaledValue or Significand contain fewer digits than Precision, trailing zeros are considered significant. For example, 1.200 has Precision = 4.
public int Sign { get; }Gets the sign of the represented number. Returns -1 for negative numbers, 1 for positive numbers, and 0 for zero.
| Method | Returns | Description |
|---|---|---|
Compare(NumberInfo, NumberInfo) |
int |
Compares two NumberInfo values and returns an integer indicating their relative order. |
Equals(NumberInfo, NumberInfo) |
bool |
Determines whether two NumberInfo values are equal using the Strict comparer. |
Equals(NumberInfo, NumberInfo, NumberInfoEqualityComparer) |
bool |
Determines whether two NumberInfo values are equal using the specified comparer. |
Parse(string, IFormatProvider?) |
NumberInfo |
Parses a string into a NumberInfo value. |
Parse(string, NumberStyles, IFormatProvider?) |
NumberInfo |
Parses a string with the specified styles into a NumberInfo value. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
NumberInfo |
Parses a ReadOnlySpan<char> into a NumberInfo value. |
Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?) |
NumberInfo |
Parses a ReadOnlySpan<char> with the specified styles into a NumberInfo value. |
TryParse(string?, out NumberInfo) |
bool |
Tries to parse a string into a NumberInfo value. |
TryParse(string?, IFormatProvider?, out NumberInfo) |
bool |
Tries to parse a string using the specified format provider. |
TryParse(string?, NumberStyles, IFormatProvider?, out NumberInfo) |
bool |
Tries to parse a string with the specified styles. |
TryParse(ReadOnlySpan<char>, out NumberInfo) |
bool |
Tries to parse a ReadOnlySpan<char> into a NumberInfo value. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out NumberInfo) |
bool |
Tries to parse a ReadOnlySpan<char> using the specified format provider. |
TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out NumberInfo) |
bool |
Tries to parse a ReadOnlySpan<char> with the specified styles. |
| Method | Returns | Description |
|---|---|---|
CompareTo(NumberInfo) |
int |
Compares this instance with another NumberInfo value. |
CompareTo(object?) |
int |
Compares this instance with another object. |
Equals(NumberInfo) |
bool |
Determines whether this instance equals another NumberInfo value using the Strict comparer. |
Equals(NumberInfo, NumberInfoEqualityComparer) |
bool |
Determines whether this instance equals another NumberInfo value using the specified comparer. |
Equals(object?) |
bool |
Determines whether this instance equals another object using the Strict comparer. |
Equals(object?, NumberInfoEqualityComparer) |
bool |
Determines whether this instance equals another object using the specified comparer. |
GetHashCode() |
int |
Returns a hash code for this instance using the Strict comparer. |
ToString() |
string |
Returns a string representation using the default "G" format and the current culture. |
ToString(string?, IFormatProvider?) |
string |
Returns a string representation using the specified format and format provider. |
ToString(ReadOnlySpan<char>, IFormatProvider?) |
string |
Returns a string representation using the specified span format and format provider. |
public static int Compare(NumberInfo left, NumberInfo right)Compares two NumberInfo values and returns an integer that indicates whether left is less than, equal to, or greater than right. Delegates to NumberInfoOrdinalityComparer.Default.
| Parameter | Type | Description |
|---|---|---|
left |
NumberInfo |
The left value to compare. |
right |
NumberInfo |
The right value to compare. |
Returns — A signed integer: negative if left precedes right, zero if they are equal, positive if left follows right.
public static bool Equals(NumberInfo left, NumberInfo right)Determines whether two NumberInfo values are equal using the Strict comparer. Both UnscaledValue and Scale must match.
| Parameter | Type | Description |
|---|---|---|
left |
NumberInfo |
The left value to compare. |
right |
NumberInfo |
The right value to compare. |
Returns — true if the two values are strictly equal; otherwise, false.
public static bool Equals(NumberInfo left, NumberInfo right, NumberInfoEqualityComparer comparer)Determines whether two NumberInfo values are equal using the specified comparer.
| Parameter | Type | Description |
|---|---|---|
left |
NumberInfo |
The left value to compare. |
right |
NumberInfo |
The right value to compare. |
comparer |
NumberInfoEqualityComparer |
The equality comparer to use. |
Returns — true if the two values are equal according to comparer; otherwise, false.
public static NumberInfo Parse(string value, IFormatProvider? provider = null)Parses the specified string into a NumberInfo value using NumberStyles.Any. When provider is null, the current culture is used.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — A new NumberInfo instance parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognised format. |
public static NumberInfo Parse(string value, NumberStyles style, IFormatProvider? provider = null)Parses the specified string into a NumberInfo value using the given NumberStyles.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
style |
NumberStyles |
A bitwise combination of styles that may appear in value. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — A new NumberInfo instance parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognised format. |
public static NumberInfo Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Parses the specified ReadOnlySpan<char> into a NumberInfo value using NumberStyles.Any.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — A new NumberInfo instance parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognised format. |
public static NumberInfo Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider = null)Parses the specified ReadOnlySpan<char> into a NumberInfo value using the given NumberStyles.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
style |
NumberStyles |
A bitwise combination of styles that may appear in value. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — A new NumberInfo instance parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognised format. |
public static bool TryParse(string? value, out NumberInfo result)Tries to parse the specified string into a NumberInfo value using NumberStyles.Any and the current culture.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The string to parse. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(string? value, IFormatProvider? provider, out NumberInfo result)Tries to parse the specified string into a NumberInfo value using the given format provider and NumberStyles.Any.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The string to parse. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(string? value, NumberStyles style, IFormatProvider? provider, out NumberInfo result)Tries to parse the specified string into a NumberInfo value using the given styles and format provider.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The string to parse. |
style |
NumberStyles |
A bitwise combination of styles that may appear in value. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, out NumberInfo result)Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using NumberStyles.Any and the current culture.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out NumberInfo result)Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using the given format provider and NumberStyles.Any.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out NumberInfo result)Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using the given styles and format provider.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
style |
NumberStyles |
A bitwise combination of styles that may appear in value. |
provider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
result |
out NumberInfo |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public int CompareTo(NumberInfo other)Compares this instance with another NumberInfo value. Delegates to NumberInfoOrdinalityComparer.Default.
| Parameter | Type | Description |
|---|---|---|
other |
NumberInfo |
The value to compare with this instance. |
Returns — A signed integer indicating the relative order of this instance and other.
public int CompareTo(object? obj)Compares this instance with another object. Delegates to NumberInfoOrdinalityComparer.Default.
| Parameter | Type | Description |
|---|---|---|
obj |
object? |
The object to compare with this instance. |
Returns — A signed integer indicating the relative order of this instance and obj.
public bool Equals(NumberInfo other)Determines whether this instance is equal to another NumberInfo value using the Strict comparer. Both UnscaledValue and Scale must match.
| Parameter | Type | Description |
|---|---|---|
other |
NumberInfo |
The value to compare with this instance. |
Returns — true if this instance is strictly equal to other; otherwise, false.
public bool Equals(NumberInfo other, NumberInfoEqualityComparer comparer)Determines whether this instance is equal to another NumberInfo value using the specified comparer.
| Parameter | Type | Description |
|---|---|---|
other |
NumberInfo |
The value to compare with this instance. |
comparer |
NumberInfoEqualityComparer |
The equality comparer to use. |
Returns — true if this instance is equal to other according to comparer; otherwise, false.
public override bool Equals(object? obj)Determines whether this instance is equal to another object using the Strict comparer.
| Parameter | Type | Description |
|---|---|---|
obj |
object? |
The object to compare with this instance. |
Returns — true if obj is a NumberInfo strictly equal to this instance; otherwise, false.
public bool Equals(object? obj, NumberInfoEqualityComparer comparer)Determines whether this instance is equal to another object using the specified comparer.
| Parameter | Type | Description |
|---|---|---|
obj |
object? |
The object to compare with this instance. |
comparer |
NumberInfoEqualityComparer |
The equality comparer to use. |
Returns — true if obj is a NumberInfo equal to this instance according to comparer; otherwise, false.
public override int GetHashCode()Returns a hash code for this instance using the Strict comparer. The hash combines UnscaledValue and Scale.
Returns — A 32-bit signed integer hash code.
public override string ToString()Returns a string representation of this instance using the default "G" format and CultureInfo.CurrentCulture.
Returns — The formatted string representation of this instance.
public string ToString(string? format, IFormatProvider? formatProvider = null)Returns a string representation of this instance using the specified format and format provider. When format is null, the default "G" format is used. When formatProvider is null, the current culture is used.
Supported format specifiers: C (currency), D (decimal), E (exponential/scientific), F (fixed-point), G (general), N (number), P (percent), R (round-trip), X (hexadecimal).
| Parameter | Type | Description |
|---|---|---|
format |
string? |
The format specifier, or null for the default "G" format. |
formatProvider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — The formatted string representation of this instance.
public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider = null)Returns a string representation of this instance using the specified span format and format provider.
| Parameter | Type | Description |
|---|---|---|
format |
ReadOnlySpan<char> |
The format specifier as a character span. |
formatProvider |
IFormatProvider? |
An object that provides culture-specific formatting information, or null to use the current culture. |
Returns — The formatted string representation of this instance.
| Operator | Description |
|---|---|
operator == |
Determines whether two NumberInfo values are semantically equal. |
operator != |
Determines whether two NumberInfo values are not semantically equal. |
operator > |
Determines whether the left value is greater than the right value. |
operator >= |
Determines whether the left value is greater than or equal to the right value. |
operator < |
Determines whether the left value is less than the right value. |
operator <= |
Determines whether the left value is less than or equal to the right value. |
public static bool operator ==(NumberInfo left, NumberInfo right)Determines whether two NumberInfo values are semantically equal. Uses the Semantic comparer; 1.20 == 1.2 evaluates to true.
public static bool operator !=(NumberInfo left, NumberInfo right)Determines whether two NumberInfo values are not semantically equal. Uses the Semantic comparer; 1.20 != 1.200 evaluates to false.
public static bool operator >(NumberInfo left, NumberInfo right)Determines whether left is greater than right. Delegates to NumberInfoOrdinalityComparer.Default.
public static bool operator >=(NumberInfo left, NumberInfo right)Determines whether left is greater than or equal to right. Delegates to NumberInfoOrdinalityComparer.Default.
public static bool operator <(NumberInfo left, NumberInfo right)Determines whether left is less than right. Delegates to NumberInfoOrdinalityComparer.Default.
public static bool operator <=(NumberInfo left, NumberInfo right)Determines whether left is less than or equal to right. Delegates to NumberInfoOrdinalityComparer.Default.
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