IntegerExtensionMethods

public static class IntegerExtensionMethods
Extension methods for various integer types.

Methods

ArrayTo

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

ArrayTo

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

ArrayTo

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

ArrayTo

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

ArrayTo

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

Average

#Example
public static double Average(this IEnumerable<int> integers)
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.
integers (IEnumerable<int>)Integer collection.
Average of all integers.
public static int Clip(this int integer, int lowerLimit, int upperLimit)
Limit an integer to a range.
integer (int)Integer.
lowerLimit (int)Inclusive lower limit.
upperLimit (int)Inclusive upper limit.
Clipped integer.
public static int Clip(this int integer, int upperLimit)
Limit an integer to a range.
integer (int)Integer.
upperLimit (int)Inclusive upper limit.
Clipped integer.
public static long Clip(this long integer, long lowerLimit, long upperLimit)
Limit an integer to a range.
integer (long)Integer.
lowerLimit (long)Inclusive lower limit.
upperLimit (long)Inclusive upper limit.
Clipped integer.
public static long Clip(this long integer, long upperLimit)
Limit an integer to a range.
integer (long)Integer.
upperLimit (long)Inclusive upper limit.
Clipped integer.

DigitCount

#Example
public static int DigitCount(this BigInteger integer)
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.

integer (BigInteger)

Digits

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

DivideAsDecimal

#Example
public static double DivideAsDecimal(this BigInteger numerator, BigInteger denominator)
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.
numerator (BigInteger)
denominator (BigInteger)

ExtendSequence

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

FindCycle

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

FindCycle

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

Highest

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

Interpolate

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

Lowest

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

MarchTo

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

MarchTo

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

MarchTo

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

MarchTo

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

OrdinalSuffix

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

Simplify

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

Simplify

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

ToDifferenceArray

#Example
public static int[] ToDifferenceArray(this int[] array)
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.
array (int[])Array to operate on.
Difference array.

ToEnglishNumeral

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

ToEnglishOrdinal

#Example
public static string ToEnglishOrdinal(this int integer)
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.
integer (int)Integer to convert.
Ordinal numeral.

ToGrasshopperString

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

ToRomanNumeral

#Example
public static string ToRomanNumeral(this int integer)
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.
integer (int)Number to convert.
String representing the number as a roman numeral if possible.

ToShortString

#Example
public static string ToShortString(this BigInteger value, int fullDigits = 20, int headDigits = 10)
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.
value (BigInteger)Value to format.
fullDigits (int = 20)Length up to which the value is written out in full.
headDigits (int = 10)Leading digits to keep once it is not.

ToStringCancellable

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

With<T>

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

Without<T>

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