IntegerExtensionMethods

public static class IntegerExtensionMethods
Extension methods for various integer types.

Methods

ArrayTo

#Example
Create an array from this integer to another.
public static BigInteger[] ArrayTo(
index0,
Inclusive starting number.
index1,
Exclusive ending number.
stepSize
Step size between entries.
)
Array from a to b, as the stepsize allows.

ArrayTo

#Example
Create an array from this integer to another.
public static int[] ArrayTo(
this int
index0,
Inclusive starting number.
index1,
Exclusive ending number.
stepSize
Step size between entries.
)
Array from a to b, as the stepsize allows.

ArrayTo

#Example
Create an array from this integer to another.
public static int[] ArrayTo(
this int
index0,
Inclusive starting number.
index1
Exclusive ending number.
)
Array from a to b.

ArrayTo

#Example
Create an array from this integer to another.
public static long[] ArrayTo(
this long
index0,
Inclusive starting number.
index1,
Exclusive ending number.
stepSize
Step size between entries.
)
Array from a to b, as the stepsize allows.

ArrayTo

#Example
Create an array from this integer to another.
public static long[] ArrayTo(
this long
index0,
Inclusive starting number.
index1
Exclusive ending number.
)
Array from a to (but no including) b.

Average

#Example
Compute the mean (average) of a collection of integers. This method is designed to work with a large number of integers or very large integers. Essentially, if you're certain that the sum of all values doesn't exceed Int32.MaxValue, there are faster ways to compute the average.
public static double Average(
integers
Integer collection.
)
Average of all integers.
Limit an integer to a range.
public static int Clip(
this int
integer,
Integer.
lowerLimit,
Inclusive lower limit.
upperLimit
Inclusive upper limit.
)
Clipped integer.
Limit an integer to a range.
public static int Clip(
this int
integer,
Integer.
upperLimit
Inclusive upper limit.
)
Clipped integer.
Limit an integer to a range.
public static long Clip(
this long
integer,
Integer.
lowerLimit,
Inclusive lower limit.
upperLimit
Inclusive upper limit.
)
Clipped integer.
Limit an integer to a range.
public static long Clip(
this long
integer,
Integer.
upperLimit
Inclusive upper limit.
)
Clipped integer.

DigitCount

#Example
Returns the number of digits of a BigInteger value, ignoring any sign.

Read off the logarithm rather than from an expansion, so it costs a few microseconds no matter how large the value is. Only a logarithm which lands within rounding distance of a whole number has to be settled exactly, which is to say only a value within a hair of a power of ten.

public static int DigitCount(
integer
)

Digits

#Example
Count the number of base-10 digits required to represent this number. Note that this does not include the sign.
public static int Digits(
this int
integer
)

DivideAsDecimal

#Example
Divide two big integers to yield a decimal floating point result. This method is slow but generally speaking more accurate than converting the big integer into doubles prior to division.
public static double DivideAsDecimal(
numerator,
denominator
)

ExtendSequence

#Example
Attempt to continue an array of integers.
public static int ExtendSequence(
this int[]
array
Sequence to extend.
)
Best guess for next number in sequence.

FindCycle

#Example
Determine whether an array of integers contains repeating elements. the last iteration of the cycle may be incomplete.
public static int FindCycle(
this int[]
array,
Array to examine.
start
Index for cycle search.
)
Length of cycle or 0 if no cycle could be found.

FindCycle

#Example
Determine whether an array of integers contains repeating elements. the last iteration of the cycle may be incomplete.
public static int FindCycle(
this int[]
array
Array to examine.
)
Length of cycle or 0 if no cycle could be found.
Halves the value and rounds to the nearest whole number.
public static int Half(
this int
integer
Integer.
)
Half the integer.

Highest

#Example
Gets the highest number in an array.
public static int Highest(
integers
Collection of integers.
)
Highest integer in the set.

Interpolate

#Example
Interpolate between two BigInteger instances.
public static BigInteger Interpolate(
from,
to,
factor
)

Lowest

#Example
Gets the lowest number in an array.
public static int Lowest(
integers
Collection of integers.
)
Lowest integer in the set.
Apply an indexing map.
public static int Map(
map,
Map to query.
index,
Index to modify.
length
Length of collection.
)
Mapped index, or original index if a mapping is not possible.

MarchTo

#Example
Enumerate from the current integer to a target integer.
public static IEnumerable<int> MarchTo(
this int
index0,
Inclusive start integer.
index1,
Exclusive end integer.
stepSize
Size of each marching step.
)
Enumerator between two integers.

MarchTo

#Example
Enumerate from the current integer to a target integer.
public static IEnumerable<int> MarchTo(
this int
index0,
Inclusive start integer.
index1
Exclusive end integer.
)
Enumerator from a to (but not including) b.

MarchTo

#Example
Enumerate from the current integer to a target integer.
public static IEnumerable<long> MarchTo(
this long
index0,
Inclusive start integer.
index1,
Inclusive end integer.
stepSize
Size of each marching step.
)
Enumerator between two integers.

MarchTo

#Example
Enumerate from the current integer to a target integer.
public static IEnumerable<long> MarchTo(
this long
index0,
Inclusive start integer.
index1
Inclusive end integer.
)
Enumerator from a to b.

OrdinalSuffix

#Example
Get the ordinal suffix ("st", "nd", "rd", or "th") for an integer.
public static string OrdinalSuffix(
this int
integer
)

Simplify

#Example
Same as Simplify(BigInteger), except it returns success and output the 32-bit integer.
public static bool Simplify(
integer,
out int
thirtytwobit
)

Simplify

#Example
Either returns the integer itself, or an System.Int32 instance if the conversion is possible. This method is handy for storing BigIntegers into NumberLikeParameter.
public static object Simplify(
integer
)

ToDifferenceArray

#Example
Create a difference array. This array contains all the values that need to be added to the elements in the original array in order to yield the next element. The difference array will be one element shorter.
public static int[] ToDifferenceArray(
this int[]
array
Array to operate on.
)
Difference array.

ToEnglishNumeral

#Example
Convert this integer to english words.
public static string ToEnglishNumeral(
this int
integer,
separator = " "
)

ToEnglishOrdinal

#Example
Convert an integer into an ordinal (first, second, third etc). Integers up to and including 12 are given full words, integers beyond that magnitude are kept as digits with trailing 'st', 'nd', 'rd', 'th' suffixes.
public static string ToEnglishOrdinal(
this int
integer
Integer to convert.
)
Ordinal numeral.

ToGrasshopperString

#Example
Format a 128-bit Guid using Grasshopper code replacements.
public static string ToGrasshopperString(
this Guid
id
)

ToRomanNumeral

#Example
Convert an integer to a roman numeral. Only integers between 1 and 3999 are supported, integers outside of that range are returned as arabic numerals.
public static string ToRomanNumeral(
this int
integer
Number to convert.
)
String representing the number as a roman numeral if possible.

ToShortString

#Example
Format a big integer for reading rather than for round tripping. A value of no more than fullDigits digits is written out in full; a longer one is cut to its headDigits leading digits, followed by a count of the digits left out.
public static string ToShortString(
value,
Value to format.
fullDigits = 20,
Length up to which the value is written out in full.
headDigits = 10
Leading digits to keep once it is not.
)

ToStringCancellable

#Example
Converts a BigInteger to its decimal string representation, checking cancellationToken between chunks so very large values can be aborted without waiting for completion.
public static string ToStringCancellable(
value,
cancellationToken
)

With<T>

#Example
Add an enum flag.
public static T With<T>(
this T
flags,
T
flag
)

Without<T>

#Example
Remove an enum flag.
public static T Without<T>(
this T
flags,
T
flag
)