Class XDouble
Extension methods and other bonus stuff for double.
Inheritance
Namespace: Galaxon.Core.Numbers
Assembly: Galaxon.Core.dll
Syntax
public static class XDouble : object
Fields
Delta
The default maximum difference between 2 double values being compared for equality.
Declaration
public const double Delta = null
Field Value
| Type | Description |
|---|---|
| System.Double |
MaxExp
The maximum binary exponent supported by the type.
Declaration
public const short MaxExp = null
Field Value
| Type | Description |
|---|---|
| System.Int16 |
MinExp
The minimum binary exponent supported by the type.
Declaration
public const short MinExp = null
Field Value
| Type | Description |
|---|---|
| System.Int16 |
NumExpBits
The number of bits in the exponent.
Declaration
public const byte NumExpBits = null
Field Value
| Type | Description |
|---|---|
| System.Byte |
NumFracBits
The number of bits in the fraction.
Declaration
public const byte NumFracBits = null
Field Value
| Type | Description |
|---|---|
| System.Byte |
Properties
MaxPosSubnormalValue
The maximum positive subnormal value.
Declaration
public static double MaxPosSubnormalValue { get; }
Property Value
| Type | Description |
|---|---|
| System.Double |
MinPosNormalValue
The minimum positive normal value.
Declaration
public static double MinPosNormalValue { get; }
Property Value
| Type | Description |
|---|---|
| System.Double |
Methods
Assemble(Byte, UInt16, UInt64)
Assemble a new double from parts.
Declaration
public static double Assemble(byte signBit, ushort expBits, ulong fracBits)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte | signBit | The sign bit (1 or 0). |
| System.UInt16 | expBits | The exponent bits. |
| System.UInt64 | fracBits | The fraction bits. |
Returns
| Type | Description |
|---|---|
| System.Double | The new double. |
Disassemble(Double)
Disassemble the double into its bitwise components.
Declaration
public static (byte signBit, ushort expBits, ulong fracBits) Disassemble(this double x)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | x |
Returns
| Type | Description |
|---|---|
| System.ValueTuple<System.Byte, System.UInt16, System.UInt64> |
FuzzyEquals(Double, Double, Double)
Check if 2 double values are equal for practical purposes.
If two double values differ only by the least significant bit, this is more likely due to inaccuracies in floating point representations than actual inequality.
This code is copied/adapted from Google Guava DoubleMath.fuzzyEquals(). https://github.com/google/guava/blob/master/guava/src/com/google/common/math/DoubleMath.java#L360
I initially tried the algorithm from the Microsoft documentation, it didn't work in all cases. https://learn.microsoft.com/en-us/dotnet/api/system.double.equals?view=net-7.0#system-double-equals(system-double)
Declaration
public static bool FuzzyEquals(this double a, double b, double tolerance = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | a | First number. |
| System.Double | b | Second number. |
| System.Double | tolerance | The maximum allowable difference between them. |
Returns
| Type | Description |
|---|---|
| System.Boolean | If close enough to equal. |
FuzzyEquals(Nullable<Double>, Nullable<Double>, Double)
Compare two nullable doubles for fuzzy equality.
Declaration
public static bool FuzzyEquals(this double? a, double? b, double tolerance = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Nullable<System.Double> | a | |
| System.Nullable<System.Double> | b | |
| System.Double | tolerance |
Returns
| Type | Description |
|---|---|
| System.Boolean |
FuzzyIsInteger(Double, Double)
IsInteger() can be a bit strict. This method allows for some fuzziness.
Declaration
public static bool FuzzyIsInteger(double d, double tolerance = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d | |
| System.Double | tolerance |
Returns
| Type | Description |
|---|---|
| System.Boolean |
FuzzyIsNegativeInteger(Double, Double)
Check if a double is a negative integer, with some fuzziness.
Declaration
public static bool FuzzyIsNegativeInteger(double d, double tolerance = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d | |
| System.Double | tolerance |
Returns
| Type | Description |
|---|---|
| System.Boolean |
FuzzyIsPositiveInteger(Double, Double)
Check if a double is a positive integer, with some fuzziness.
Declaration
public static bool FuzzyIsPositiveInteger(double d, double tolerance = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d | |
| System.Double | tolerance |
Returns
| Type | Description |
|---|---|
| System.Boolean |
GetRandom()
Get a random double.
Declaration
public static double GetRandom()
Returns
| Type | Description |
|---|---|
| System.Double |
IsNegativeInteger(Double)
Check if a double is a negative integer.
Declaration
public static bool IsNegativeInteger(double d)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d |
Returns
| Type | Description |
|---|---|
| System.Boolean |
IsPerfectSquare(Double)
Check if a value is a perfect square.
Declaration
public static bool IsPerfectSquare(double d)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d |
Returns
| Type | Description |
|---|---|
| System.Boolean |
IsPositiveInteger(Double)
Check if a double is a positive integer.
Declaration
public static bool IsPositiveInteger(double d)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d |
Returns
| Type | Description |
|---|---|
| System.Boolean |
RoundSigFigs(Double, Int32)
Round off a value to a given number of significant figures.
Declaration
public static double RoundSigFigs(double d, int nSigFigs)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | d | The number to round. |
| System.Int32 | nSigFigs | The number of significant figures. |
Returns
| Type | Description |
|---|---|
| System.Double | The rounded number. |