FloatingPointExtensionMethods

public static class FloatingPointExtensionMethods
Extension methods for System.Double

Methods

ArrayTo

#Example
Create an array from this number to another.
public static double[] ArrayTo(
this double
number0,
First number in array.
number1,
Inclusive last number in array.
steps
Number of steps. Array will contain steps+1 elements.
)
Array from a to b.

ArrayTo

#Example
Create an array of doubles from a value to a limit.
public static double[] ArrayTo(
this double
start,
First value in array.
limit,
Inclusive limit in array.
stepSize
Distance between elements.
)
Array.

ArrayTo

#Example
Create an array from this number to another.
public static float[] ArrayTo(
this float
number0,
First number in array.
number1,
Inclusive last number in array.
steps
Number of steps. Array will contain steps+1 elements.
)
Array from a to b.

AsColour

#Example
Create a greyscale colour.
public static Color AsColour(
this float
luminance
)

Average

#Example
Compute the mean (average) of a collection of numbers. This method is designed to work with many numbers as well as very large numbers. Essentially, if you're certain that the sum of all values doesn't exceed 1e128, there are faster ways to compute the average.
public static double Average(
numbers
Number collection.
)
Average of all numbers.

Average

#Example
Compute the mean (average) of a collection of numbers. This method is designed to work with many numbers as well as very large numbers. Essentially, if you're certain that the sum of all values doesn't exceed 1e128, there are faster ways to compute the average.
public static float Average(
numbers
Number collection.
)
Average of all numbers.
Limit a number to a range.
public static decimal Clip(
this decimal
number,
Integer.
lowerLimit,
Inclusive lower limit.
upperLimit
Inclusive upper limit.
)
Clipped number.
Limit a number to a range.
public static decimal Clip(
this decimal
number,
Integer.
upperLimit
Inclusive upper limit.
)
Clipped number.
Limit a number to a range.
public static double Clip(
this double
number,
Integer.
lowerLimit,
Inclusive lower limit.
upperLimit
Inclusive upper limit.
)
Clipped number.
Limit a number to a range.
public static double Clip(
this double
number,
Integer.
upperLimit
Inclusive upper limit.
)
Clipped number.
Limit a number to a range.
public static float Clip(
this float
number,
Integer.
lowerLimit,
Inclusive lower limit.
upperLimit
Inclusive upper limit.
)
Clipped number.
Limit a number to a range.
public static float Clip(
this float
number,
Integer.
upperLimit
Inclusive upper limit.
)
Clipped number.

DecimalPrecision

#Example
Gets the number of significant decimal places of a decimal number.
public static int DecimalPrecision(
this decimal
value
Value to test.
)
Number of decimal places.

EnumerateTo

#Example
Create an enumeration from one value to another.
public static IEnumerable<double> EnumerateTo(
this double
number0,
First number in enumeration.
number1,
Inclusive last number in enumeration.
steps
Number of steps. Enumeration will contain steps+1 elements.
)
Enumeration from a to b.

FormatHonest

#Example
Format a double using a limited number of decimal places, appending an ellipsis whenever the displayed digits do not capture the exact value. The displayed digits are rounded, so 5.99999999993 formats as "6.0…" rather than "5.99999…".
public static string FormatHonest(
this double
value,
Number to format.
minimumDigits,
Minimum decimal places.
maximumDigits
Maximum decimal places, including the ellipsis if it's needed.
)

FormatScientific

#Example
Format a bigInteger value using scientific base-10 notation.
public static string FormatScientific(
value,
Value to format.
minimumDigits,
Minimum number of digits to use in the decimal.
maximumDigits,
Maximum number of digits to use in the decimal.
superscript
True if superscript exponents ought to be used.
)
Scientific format.

FormatScientific

#Example
Format a double value using scientific base-10 notation.
public static string FormatScientific(
this double
value,
Value to format.
minimumDigits,
Minimum number of digits to use in the decimal.
maximumDigits,
Maximum number of digits to use in the decimal.
superscript
True if superscript exponents ought to be used.
)
Scientific format.

FractionalPart

#Example
Return the fractional portion of a double. This method does not suffer from the same rounding issues as f = x % 1 or f = x - floor(x). For example try taking an arithmetical approach to finding the fractional part of 100000000000000.1 and you'll get 0.09375 instead of 0.1
public static double FractionalPart(
this double
value
Value to strip.
)
Fractional part between -1 and +1. Negative input values yield negative fractional parts.

GetMagnitude

#Example
Gets both the magnitude of a number and the scaled value.
public static void GetMagnitude(
this double
number,
Number to measure.
out int
magnitude,
Magnitude of number (power of ten).
out double
scaledValue
Scaled value (a number in between -10 and +10).
)

GetMagnitude

#Example
Gets both the magnitude of a number.
public static void GetMagnitude(
this double
number,
Number to measure.
out int
magnitude
Magnitude of number (power of ten).
)

Highest

#Example
Gets the highest number in an array.
public static double Highest(
numbers
Collection of numbers.
)
Highest number in the set.

Highest

#Example
Gets the highest number in an array.
public static float Highest(
numbers
Collection of numbers.
)
Highest number in the set.

IntegralPart

#Example
Return the integral portion of a double. This is equivalent to Math.Truncate().
public static double IntegralPart(
this double
value
Value to strip.
)
Integral part. Negative input values yield negative parts that are either equal to or larger than the input.

IsBetween

#Example
Gets whether the number is in between both limits. This method returns true even if the number is coincident with the limits.
public static bool IsBetween(
this double
number,
Number to test.
limitA,
First limit.
limitB
Second limit.
)

IsBetween

#Example
Gets whether the number is in between both limits. This method returns true even if the number is coincident with the limits.
public static bool IsBetween(
this int
number,
Number to test.
limitA,
First limit.
limitB
Second limit.
)

IsInteger32

#Example
Gets whether a double precision value is an exact integer in the 32-bit range.
public static bool IsInteger32(
this double
number
Number to test.
)
True if number is expressible as a 32-bit integer without loss of information.

IsInteger64

#Example
Gets whether a double precision value is an exact integer in the 64-bit range.
public static bool IsInteger64(
this double
number
Number to test.
)
True if number is expressible as a 64-bit integer without loss of information.

Lowest

#Example
Gets the lowest number in an array.
public static double Lowest(
numbers
Collection of numbers.
)
Lowest number in the set.

Lowest

#Example
Gets the lowest number in an array.
public static float Lowest(
numbers
Collection of numbers.
)
Lowest number in the set.
Map a value from one numeric domain to another.
public static double Map(
this double
value,
Value to map.
sourceMinimum,
Start of source domain.
sourceMaximum,
End of source domain.
targetMinimum,
Start of target domain.
targetMaximum
End of target domain.
)
Mapped value.
Map a normalised value to a new numeric domain.
public static double Map(
this double
value,
Value to map.
targetMinimum,
Start of target domain.
targetMaximum
End of target domain.
)
Mapped value.
Map a value from one numeric domain to another.
public static float Map(
this float
value,
Value to map.
sourceMinimum,
Start of source domain.
sourceMaximum,
End of source domain.
targetMinimum,
Start of target domain.
targetMaximum
End of target domain.
)
Mapped value.
Map a normalised value to a new numeric domain.
public static float Map(
this float
value,
Value to map.
targetMinimum,
Start of target domain.
targetMaximum
End of target domain.
)
Mapped value.

Median

#Example
Compute the median of a collection of numbers. This method uses the QuickMedian algorithm.
public static double Median(
values
Value to take into consideration.
)
Median of values.

Median

#Example
Compute the median of a collection of numbers. This method uses the QuickMedian algorithm.
public static float Median(
values
Value to take into consideration.
)
Median of values.
Gets the first double value that is larger than the current one.
public static double Next(
this double
number,
Number to increment.
count
Number of steps to take.
)
Next double value.
Gets the first double value that is larger than the current one.
public static double Next(
this double
number
Number to increment.
)
Next double value.

NextEpsilon

#Example
Gets the distance from this number to the next possible number.
public static double NextEpsilon(
this double
number
Number to measure from.
)
Epsilon to next value.

NextOrderOfMagnitude

#Example
Gets the order of magnitude bigger than or equal to the given number. The value returned is equal to or bigger than number, where "bigger" means further away from zero. Ie. negative values will result in larger negative values.
public static BigInteger NextOrderOfMagnitude(
number
Number.
)
Order of magnitude.

NextOrderOfMagnitude

#Example
Gets the order of magnitude bigger than or equal to the given number. The value returned is equal to or bigger than number, where "bigger" means further away from zero. Ie. negative values will result in larger negative values.
public static decimal NextOrderOfMagnitude(
this decimal
number
Number.
)
Order of magnitude.

NextOrderOfMagnitude

#Example
Gets the order of magnitude bigger than or equal to the given number. The value returned is equal to or bigger than number, where "bigger" means further away from zero. Ie. negative values will result in larger negative values.
public static double NextOrderOfMagnitude(
this double
number
Number.
)
Order of magnitude.

NextOrderOfMagnitude

#Example
Gets the order of magnitude bigger than or equal to the given number. The value returned is equal to or bigger than number, where "bigger" means further away from zero. Ie. negative values will result in larger negative values.
public static int NextOrderOfMagnitude(
this int
number
Number.
)
Order of magnitude.

NumbersBetween

#Example
Gets the number of steps between two values. This is one more than the amount of unique numbers in between a and b.
public static ulong NumbersBetween(
this double
number,
First number.
other
Second number.
)
Number of steps between values.
Gets the first double value that is smaller than the current one.
public static double Prev(
this double
number,
Number to decrement.
count
Number of steps to take.
)
Previous double value.
Gets the first double value that is smaller than the current one.
public static double Prev(
this double
number
Number to decrement.
)
PRevious double value.

PrevEpsilon

#Example
Gets the distance from this number to the previous possible number.
public static double PrevEpsilon(
this double
number
Number to measure from.
)
Epsilon to prev value.

PrevOrderOfMagnitude

#Example
Gets the order of magnitude smaller than or equal to the given number. The value returned is equal to or smaller than number, where "smaller" means closer to zero. Ie. negative values will result in smaller negative values.
public static BigInteger PrevOrderOfMagnitude(
number
Number.
)
Order of magnitude.

PrevOrderOfMagnitude

#Example
Gets the order of magnitude smaller than or equal to the given number. The value returned is equal to or smaller than number, where "smaller" means closer to zero. Ie. negative values will result in smaller negative values.
public static decimal PrevOrderOfMagnitude(
this decimal
number
Number.
)
Order of magnitude.

PrevOrderOfMagnitude

#Example
Gets the order of magnitude smaller than or equal to the given number. The value returned is equal to or smaller than number, where "smaller" means closer to zero. Ie. negative values will result in smaller negative values.
public static double PrevOrderOfMagnitude(
this double
number
Number.
)
Order of magnitude.

PrevOrderOfMagnitude

#Example
Gets the order of magnitude smaller than or equal to the given number. The value returned is equal to or smaller than number, where "smaller" means closer to zero. Ie. negative values will result in smaller negative values.
public static int PrevOrderOfMagnitude(
this int
number
Number.
)
Order of magnitude.

UniqueValuesBetween

#Example
Gets the total number of unique doubles between two values.
public static ulong UniqueValuesBetween(
this double
number,
Initial value.
other
Other value.
)
Total number of unique doubles that span the given range.