MathHelper
Provides various mathematical helper methods and extension methods for numeric types.
Methods
public static decimal Abs(decimal value)
result = MathHelper.Abs(value)
Returns the absolute value of the decimal .
- value
- The decimal value.
Returns: The absolute value.
public static short Abs(short value)
result = MathHelper.Abs(value)
Returns the absolute value of the short .
- value
- The short integer.
Returns: The absolute value.
public static int Abs(int value)
result = MathHelper.Abs(value)
Returns the absolute value of the integer .
- value
- The integer value.
Returns: The absolute value.
public static long Abs(long value)
result = MathHelper.Abs(value)
Returns the absolute value of the long integer .
- value
- The long integer.
Returns: The absolute value.
public static sbyte Abs(sbyte value)
result = MathHelper.Abs(value)
Returns the absolute value of the sbyte .
- value
- The sbyte value.
Returns: The absolute value.
public static float Abs(float value)
result = MathHelper.Abs(value)
Returns the absolute value of the float .
- value
- The float value.
Returns: The absolute value.
public static double Abs(double value)
result = MathHelper.Abs(value)
Returns the absolute value of the double .
- value
- The double value.
Returns: The absolute value.
public static TimeSpan Abs(TimeSpan value)
result = MathHelper.Abs(value)
Returns a TimeSpan whose ticks are the absolute value of the original TimeSpan's ticks.
- value
- The TimeSpan value.
Returns: The TimeSpan with absolute ticks.
public static decimal Acos(decimal value)
result = MathHelper.Acos(value)
Returns the angle whose cosine is the specified decimal value.
- value
- A decimal value representing a cosine.
Returns: The angle, in radians, whose cosine is the specified value.
public static double Acos(double value)
result = MathHelper.Acos(value)
Returns the angle whose cosine is the specified value.
- value
- A double value representing a cosine.
Returns: The angle, in radians, whose cosine is the specified value.
public static decimal Asin(decimal value)
result = MathHelper.Asin(value)
Returns the angle whose sine is the specified decimal value.
- value
- A decimal value representing a sine.
Returns: The angle, in radians, whose sine is the specified value.
public static double Asin(double value)
result = MathHelper.Asin(value)
Returns the angle whose sine is the specified value.
- value
- A double value representing a sine.
Returns: The angle, in radians, whose sine is the specified value.
public static long AsRaw(double value)
result = MathHelper.AsRaw(value)
Gets the raw 64-bit integer representation of the double value.
- value
- The double value.
Returns: The 64-bit integer representation.
public static double AsRaw(long value)
result = MathHelper.AsRaw(value)
Converts a 64-bit integer representation to a double value.
- value
- The 64-bit integer representation.
Returns: The double value.
public static float AsRaw(int value)
result = MathHelper.AsRaw(value)
Converts an integer value to a single-precision float via a byte array conversion.
- value
- The integer value.
Returns: The float value.
public static int AsRaw(float value)
result = MathHelper.AsRaw(value)
Converts a float value to its raw 32-bit integer representation.
- value
- The float value.
Returns: The 32-bit integer representation.
public static decimal Atan(decimal value)
result = MathHelper.Atan(value)
Returns the angle whose tangent is the specified decimal value.
- value
- A decimal value representing a tangent.
Returns: The angle, in radians, whose tangent is the specified value.
public static double Atan(double value)
result = MathHelper.Atan(value)
Returns the angle whose tangent is the specified value.
- value
- A double value representing a tangent.
Returns: The angle, in radians, whose tangent is the specified value.
public static decimal Atan2(decimal y, decimal x)
result = MathHelper.Atan2(y, x)
Returns the angle whose tangent is the quotient of two specified decimal numbers.
- y
- The y coordinate.
- x
- The x coordinate.
Returns: The angle, in radians, whose tangent is the quotient of y and x.
public static double Atan2(double y, double x)
result = MathHelper.Atan2(y, x)
Returns the angle whose tangent is the quotient of two specified double numbers.
- y
- The y coordinate.
- x
- The x coordinate.
Returns: The angle, in radians, whose tangent is the quotient of y and x.
public static long BigMul(int x, int y)
result = MathHelper.BigMul(x, y)
Returns the full product of two integers.
- x
- The first integer.
- y
- The second integer.
Returns: The product as a long.
public static int Ceiling(float value)
result = MathHelper.Ceiling(value)
Returns the smallest integer greater than or equal to the float value.
- value
- The float value.
Returns: The ceiling integer.
public static decimal Ceiling(decimal value)
result = MathHelper.Ceiling(value)
Returns the smallest integer greater than or equal to the specified .
- value
- The decimal value.
Returns: The ceiled value.
public static decimal Ceiling(decimal value, decimal step)
result = MathHelper.Ceiling(value, step)
Returns the smallest multiple of greater than or equal to the specified .
- value
- The decimal value.
- step
- The step size.
Returns: The ceiled value.
public static long Ceiling(double value)
result = MathHelper.Ceiling(value)
Returns the smallest integer greater than or equal to the specified double as a long.
- value
- The double value.
Returns: The ceiled value as long.
public static double Cos(double value)
result = MathHelper.Cos(value)
Returns the cosine of the specified double angle.
- value
- An angle, measured in radians.
Returns: The cosine of the angle.
public static decimal Cos(decimal value)
result = MathHelper.Cos(value)
Returns the cosine of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The cosine of the angle as a decimal.
public static double Cosh(double value)
result = MathHelper.Cosh(value)
Returns the hyperbolic cosine of the specified double angle.
- value
- An angle, measured in radians.
Returns: The hyperbolic cosine of the angle.
public static decimal Cosh(decimal value)
result = MathHelper.Cosh(value)
Returns the hyperbolic cosine of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The hyperbolic cosine of the angle as a decimal.
public static long DivRem(long a, long b, long result)
result = MathHelper.DivRem(a, b, result)
Computes the quotient and remainder of the division of two long integers.
- a
- The dividend.
- b
- The divisor.
- result
- When the method returns, contains the remainder.
Returns: The quotient.
public static int DivRem(int a, int b, int result)
result = MathHelper.DivRem(a, b, result)
Computes the quotient and remainder of the division of two integers.
- a
- The dividend.
- b
- The divisor.
- result
- When the method returns, contains the remainder.
Returns: The quotient.
public static double Exp(double value)
result = MathHelper.Exp(value)
Returns e raised to the specified double power.
- value
- A double exponent.
Returns: The value of e raised to the specified power.
public static decimal Exp(decimal value)
result = MathHelper.Exp(value)
Returns e raised to the specified decimal power.
- value
- A decimal exponent.
Returns: The value of e raised to the specified power as a decimal.
public static void ExtractMantissaExponent(double value, long mantissa, int exponent)
MathHelper.ExtractMantissaExponent(value, mantissa, exponent)
Extracts the normalized mantissa and exponent from the double value.
- value
- The double value.
- mantissa
- The extracted mantissa (negative for negative values).
- exponent
- The extracted exponent.
public static ValueTuple<long, int> ExtractMantissaExponent(decimal value)
result = MathHelper.ExtractMantissaExponent(value)
Extracts the mantissa and exponent from the decimal value.
- value
- The decimal value.
Returns: The extracted mantissa and exponent.
public static int Floor(float value)
result = MathHelper.Floor(value)
Returns the largest integer less than or equal to the float value.
- value
- The float value.
Returns: The floor integer.
public static decimal Floor(decimal value, decimal step)
result = MathHelper.Floor(value, step)
Returns the largest multiple of less than or equal to the specified .
- value
- The decimal value.
- step
- The step size.
Returns: The floored value.
public static double Floor(double value, double step)
result = MathHelper.Floor(value, step)
Returns the largest multiple of less than or equal to the double .
- value
- The double value.
- step
- The step size.
Returns: The floored value.
public static float Floor(float value, float step)
result = MathHelper.Floor(value, step)
Returns the largest multiple of less than or equal to the float .
- value
- The float value.
- step
- The step size.
Returns: The floored value.
public static long Floor(long value, long step)
result = MathHelper.Floor(value, step)
Returns the largest multiple of less than or equal to the long integer .
- value
- The long integer value.
- step
- The step size.
Returns: The floored value.
public static int Floor(int value, int step)
result = MathHelper.Floor(value, step)
Returns the largest multiple of less than or equal to the integer .
- value
- The integer value.
- step
- The step size.
Returns: The floored value.
public static long Floor(double value)
result = MathHelper.Floor(value)
Returns the largest integer less than or equal to the specified double as a long.
- value
- The double value.
Returns: The floored value as long.
public static decimal Floor(decimal value)
result = MathHelper.Floor(value)
Returns the largest integer less than or equal to the specified .
- value
- The decimal value.
Returns: The floored value.
public static bool GetBit(int value, int index)
result = MathHelper.GetBit(value, index)
Gets the state of the bit at the specified index in an integer.
- value
- The integer value.
- index
- The bit index (0-based).
Returns: True if the bit is set; otherwise, false.
public static bool GetBit(long value, int index)
result = MathHelper.GetBit(value, index)
Gets the state of the bit at the specified index in a long value.
- value
- The long value.
- index
- The bit index (0-based).
Returns: True if the bit is set; otherwise, false.
public static bool GetBit(byte value, int index)
result = MathHelper.GetBit(value, index)
Determines whether the bit at the specified zero-based index in the byte is set.
- value
- The byte value.
- index
- The zero-based index of the bit to test.
Returns: true if the specified bit is set; otherwise, false.
public static int GetCachedDecimals(decimal value)
result = MathHelper.GetCachedDecimals(value)
Gets the effective scale (number of significant decimal places) of the decimal value, using caching for performance.
- value
- The decimal value.
Returns: The effective scale of the decimal.
public static DecimalInfo GetDecimalInfo(decimal value)
result = MathHelper.GetDecimalInfo(value)
Retrieves detailed information about the decimal value, including mantissa, precision, scale, and trailing zeros.
- value
- The decimal value.
Returns: A DecimalInfo structure containing detailed decimal information.
public static int GetDigitCount(long x)
result = MathHelper.GetDigitCount(x)
Returns the number of digits in a positive long integer.
- x
- The positive long integer value.
Returns: The digit count.
public static int GetDigitCount(int x)
result = MathHelper.GetDigitCount(x)
Returns the number of digits in a positive integer.
- x
- The positive integer value.
Returns: The digit count.
public static decimal GetMiddle(decimal from, decimal to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two decimal values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static decimal GetMiddle(double from, double to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two double values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static decimal GetMiddle(float from, float to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two float values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static decimal GetMiddle(long from, long to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two long integer values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static decimal GetMiddle(int from, int to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two integer values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static decimal GetMiddle(short from, short to)
result = MathHelper.GetMiddle(from, to)
Calculates the middle (average) value between two short values.
- from
- The first value.
- to
- The second value.
Returns: The middle value as a decimal.
public static float[] GetParts(float value)
result = MathHelper.GetParts(value)
Splits a float value into its integer and fractional parts.
- value
- The float value.
Returns: An array with the integer part and the fractional remainder.
public static double[] GetParts(double value)
result = MathHelper.GetParts(value)
Splits a double value into its integer and fractional parts.
- value
- The double value.
Returns: An array with the integer part and the fractional remainder.
public static int[] GetParts(long value)
result = MathHelper.GetParts(value)
Extracts the high and low parts of a long value.
- value
- The long value.
Returns: An array with two integers: the low and high parts.
public static double[] GetRoots(double a, double b, double c)
result = MathHelper.GetRoots(a, b, c)
Calculates the real roots of a quadratic equation given coefficients a, b, and c.
- a
- Coefficient a.
- b
- Coefficient b.
- c
- Coefficient c.
Returns: An array containing the two roots if the discriminant is non-negative; otherwise, an empty array.
public static bool HasBits(long value, long part)
result = MathHelper.HasBits(value, part)
Determines whether the specified bits are set in the long integer value.
- value
- The long integer value.
- part
- The bits to test.
Returns: true if all specified bits are set; otherwise, false.
public static bool HasBits(int value, int part)
result = MathHelper.HasBits(value, part)
Determines whether the specified bits are set in the integer value.
- value
- The integer value.
- part
- The bits to test.
Returns: true if all specified bits are set; otherwise, false.
public static bool IsInfinity(double value)
result = MathHelper.IsInfinity(value)
Determines if the double value represents infinity.
- value
- The double value.
Returns: true if the value is infinity; otherwise, false.
public static bool IsInfinity(float value)
result = MathHelper.IsInfinity(value)
Determines if the float value represents infinity.
- value
- The float value.
Returns: true if the value is infinity; otherwise, false.
public static bool IsNaN(double value)
result = MathHelper.IsNaN(value)
Determines if the double value is NaN (Not a Number).
- value
- The double value.
Returns: true if the value is NaN; otherwise, false.
public static bool IsNaN(float value)
result = MathHelper.IsNaN(value)
Determines if the float value is NaN (Not a Number).
- value
- The float value.
Returns: true if the value is NaN; otherwise, false.
public static bool IsNegativeInfinity(float value)
result = MathHelper.IsNegativeInfinity(value)
Determines if the float value represents negative infinity.
- value
- The float value.
Returns: true if the value is negative infinity; otherwise, false.
public static bool IsNegativeInfinity(double value)
result = MathHelper.IsNegativeInfinity(value)
Determines if the double value represents negative infinity.
- value
- The double value.
Returns: true if the value is negative infinity; otherwise, false.
public static bool IsPositiveInfinity(float value)
result = MathHelper.IsPositiveInfinity(value)
Determines if the float value represents positive infinity.
- value
- The float value.
Returns: true if the value is positive infinity; otherwise, false.
public static bool IsPositiveInfinity(double value)
result = MathHelper.IsPositiveInfinity(value)
Determines if the double value represents positive infinity.
- value
- The double value.
Returns: true if the value is positive infinity; otherwise, false.
public static decimal Log(decimal value, decimal newBase)
result = MathHelper.Log(value, newBase)
Returns the logarithm of a decimal value in the specified base.
- value
- The decimal value.
- newBase
- The base of the logarithm.
Returns: The logarithm of the value in the specified base as a decimal.
public static double Log(double value)
result = MathHelper.Log(value)
Returns the natural logarithm of a double value.
- value
- The double value.
Returns: The natural logarithm of the value.
public static decimal Log(decimal value)
result = MathHelper.Log(value)
Returns the natural logarithm of a decimal value.
- value
- The decimal value.
Returns: The natural logarithm of the value as a decimal.
public static double Log(double value, double newBase)
result = MathHelper.Log(value, newBase)
Returns the logarithm of a double value in the specified base.
- value
- The double value.
- newBase
- The base of the logarithm.
Returns: The logarithm of value in the specified base.
public static decimal Log10(decimal value)
result = MathHelper.Log10(value)
Returns the base 10 logarithm of a decimal value.
- value
- The decimal value.
Returns: The base 10 logarithm of the value as a decimal.
public static double Log10(double value)
result = MathHelper.Log10(value)
Returns the base 10 logarithm of a double value.
- value
- The double value.
Returns: The base 10 logarithm of the value.
public static decimal Max(decimal v1, decimal v2, decimal v3, decimal v4)
result = MathHelper.Max(v1, v2, v3, v4)
Returns the largest of four decimal values.
public static decimal Max(decimal v1, decimal v2, decimal v3, decimal v4, decimal v5)
result = MathHelper.Max(v1, v2, v3, v4, v5)
Returns the largest of five decimal values.
public static DateTimeOffset Max(DateTimeOffset value1, DateTimeOffset value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two DateTimeOffset values.
- value1
- The first DateTimeOffset value.
- value2
- The second DateTimeOffset value.
Returns: The later DateTimeOffset.
public static decimal Max(decimal v1, decimal v2, decimal v3)
result = MathHelper.Max(v1, v2, v3)
Returns the largest of three decimal values.
public static decimal Max(decimal value1, decimal value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two decimal values.
- value1
- The first decimal value.
- value2
- The second decimal value.
Returns: The larger decimal value.
public static double Max(double value1, double value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two double values.
- value1
- The first double value.
- value2
- The second double value.
Returns: The larger double value.
public static float Max(float value1, float value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two float values.
- value1
- The first float value.
- value2
- The second float value.
Returns: The larger float value.
public static byte Max(byte value1, byte value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two byte values.
- value1
- The first byte value.
- value2
- The second byte value.
Returns: The larger byte value.
public static sbyte Max(sbyte value1, sbyte value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two sbyte values.
- value1
- The first sbyte value.
- value2
- The second sbyte value.
Returns: The larger sbyte value.
public static ulong Max(ulong value1, ulong value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two ulong values.
- value1
- The first ulong value.
- value2
- The second ulong value.
Returns: The larger ulong value.
public static long Max(long value1, long value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two long values.
- value1
- The first long value.
- value2
- The second long value.
Returns: The larger long value.
public static TimeSpan Max(TimeSpan value1, TimeSpan value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two TimeSpan values.
- value1
- The first TimeSpan value.
- value2
- The second TimeSpan value.
Returns: The longer TimeSpan.
public static uint Max(uint value1, uint value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two uint values.
- value1
- The first uint value.
- value2
- The second uint value.
Returns: The larger uint value.
public static ushort Max(ushort value1, ushort value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two ushort values.
- value1
- The first ushort value.
- value2
- The second ushort value.
Returns: The larger ushort value.
public static short Max(short value1, short value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two short values.
- value1
- The first short value.
- value2
- The second short value.
Returns: The larger short value.
public static int Max(int value1, int value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two int values.
- value1
- The first int value.
- value2
- The second int value.
Returns: The larger int value.
public static DateTime Max(DateTime value1, DateTime value2)
result = MathHelper.Max(value1, value2)
Returns the maximum of two DateTime values.
- value1
- The first DateTime value.
- value2
- The second DateTime value.
Returns: The later DateTime.
public static DateTimeOffset Min(DateTimeOffset value1, DateTimeOffset value2)
result = MathHelper.Min(value1, value2)
Returns the minimum of two DateTimeOffset values.
- value1
- The first DateTimeOffset value.
- value2
- The second DateTimeOffset value.
Returns: The smaller DateTimeOffset value.
public static DateTime Min(DateTime value1, DateTime value2)
result = MathHelper.Min(value1, value2)
Returns the earlier DateTime.
- value1
- The first DateTime.
- value2
- The second DateTime.
Returns: The minimum DateTime.
public static TimeSpan Min(TimeSpan value1, TimeSpan value2)
result = MathHelper.Min(value1, value2)
Returns the earlier TimeSpan.
- value1
- The first TimeSpan.
- value2
- The second TimeSpan.
Returns: The minimum TimeSpan.
public static decimal Min(decimal v1, decimal v2, decimal v3, decimal v4, decimal v5)
result = MathHelper.Min(v1, v2, v3, v4, v5)
Returns the smallest of five decimal values.
public static decimal Min(decimal v1, decimal v2, decimal v3, decimal v4)
result = MathHelper.Min(v1, v2, v3, v4)
Returns the smallest of four decimal values.
public static decimal Min(decimal v1, decimal v2, decimal v3)
result = MathHelper.Min(v1, v2, v3)
Returns the smallest of three decimal values.
public static decimal Min(decimal value1, decimal value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two decimal values.
- value1
- The first decimal value.
- value2
- The second decimal value.
Returns: The minimum value.
public static uint Min(uint value1, uint value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two uint values.
- value1
- The first uint value.
- value2
- The second uint value.
Returns: The minimum value.
public static double Min(double value1, double value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two double values.
- value1
- The first double value.
- value2
- The second double value.
Returns: The minimum value.
public static float Min(float value1, float value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two float values.
- value1
- The first float value.
- value2
- The second float value.
Returns: The minimum value.
public static byte Min(byte value1, byte value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two byte values.
- value1
- The first byte value.
- value2
- The second byte value.
Returns: The minimum value.
public static int Min(int value1, int value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two integer values.
- value1
- The first integer value.
- value2
- The second integer value.
Returns: The minimum value.
public static sbyte Min(sbyte value1, sbyte value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two sbyte values.
- value1
- The first sbyte value.
- value2
- The second sbyte value.
Returns: The minimum value.
public static ushort Min(ushort value1, ushort value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two ushort values.
- value1
- The first ushort value.
- value2
- The second ushort value.
Returns: The minimum value.
public static short Min(short value1, short value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two short values.
- value1
- The first short value.
- value2
- The second short value.
Returns: The minimum value.
public static long Min(long value1, long value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two long values.
- value1
- The first long value.
- value2
- The second long value.
Returns: The minimum value.
public static ulong Min(ulong value1, ulong value2)
result = MathHelper.Min(value1, value2)
Returns the smaller of two ulong values.
- value1
- The first ulong value.
- value2
- The second ulong value.
Returns: The minimum value.
public static double Pow(double x, double y)
result = MathHelper.Pow(x, y)
Raises a double number to the power of a double exponent.
- x
- The base double value.
- y
- The exponent double value.
Returns: The resulting double value.
public static int Pow(int x, int y)
result = MathHelper.Pow(x, y)
Raises an int number to the power of an int exponent.
- x
- The base int value.
- y
- The exponent int value.
Returns: The resulting int value.
public static decimal Pow(decimal x, decimal y)
result = MathHelper.Pow(x, y)
Raises a decimal number to the power of another decimal number.
- x
- The base decimal value.
- y
- The exponent decimal value.
Returns: The resulting decimal value.
public static double Remainder(double x, double y)
result = MathHelper.Remainder(x, y)
Returns the IEEE remainder of two double values.
- x
- The dividend.
- y
- The divisor.
Returns: The remainder after division.
public static decimal Remainder(decimal x, decimal y)
result = MathHelper.Remainder(x, y)
Returns the IEEE remainder of two decimal values.
- x
- The dividend.
- y
- The divisor.
Returns: The remainder after division as a decimal.
public static decimal RemoveTrailingZeros(decimal value)
result = MathHelper.RemoveTrailingZeros(value)
Removes insignificant trailing zeros from the decimal value.
- value
- The decimal value.
Returns: The decimal value without trailing zeros.
public static decimal Round(decimal value, int digits)
result = MathHelper.Round(value, digits)
Rounds the specified to a specified number of fractional digits.
- value
- The decimal value.
- digits
- The number of fractional digits.
Returns: The rounded value.
public static double Round(double value, MidpointRounding rounding)
result = MathHelper.Round(value, rounding)
Rounds the double using the specified rounding mode.
- value
- The double value.
- rounding
- The rounding mode.
Returns: The rounded value.
public static double Round(double value, int digits, MidpointRounding rounding)
result = MathHelper.Round(value, digits, rounding)
Rounds the double to a specified number of digits using the specified rounding mode.
- value
- The double value.
- digits
- The number of fractional digits.
- rounding
- The rounding mode.
Returns: The rounded value.
public static double Round(double value)
result = MathHelper.Round(value)
Rounds the double value to the nearest integer.
- value
- The double value to round.
Returns: The rounded double value.
public static decimal Round(decimal value, decimal step, int? digits, MidpointRounding rounding)
result = MathHelper.Round(value, step, digits, rounding)
Rounds the specified to the nearest multiple of , and optionally rounds to the specified number of digits.
- value
- The decimal value.
- step
- The step size.
- digits
- Optional number of fractional digits.
- rounding
- The rounding mode (defaults to ToEven).
Returns: The rounded value.
public static decimal Round(decimal value, MidpointRounding rounding)
result = MathHelper.Round(value, rounding)
Rounds the specified using the specified rounding mode.
- value
- The decimal value.
- rounding
- The rounding mode.
Returns: The rounded value.
public static decimal Round(decimal value, int digits, MidpointRounding rounding)
result = MathHelper.Round(value, digits, rounding)
Rounds the specified to the specified number of fractional digits using the specified rounding mode.
- value
- The decimal value.
- digits
- The number of fractional digits.
- rounding
- The rounding mode.
Returns: The rounded value.
public static double Round(double value, int digits)
result = MathHelper.Round(value, digits)
Rounds the double value to the specified number of fractional digits.
- value
- The double value to round.
- digits
- The number of decimal places to round to.
Returns: The rounded double value.
public static decimal Round(decimal value)
result = MathHelper.Round(value)
Rounds the specified to the nearest integer.
- value
- The decimal value.
Returns: The rounded value.
public static decimal Round4Expression(decimal value, decimal digits)
result = MathHelper.Round4Expression(value, digits)
Rounds the specified to a specified number of fractional digits.
- value
- The decimal value.
- digits
- The number of fractional digits (as decimal, will be converted to int).
Returns: The rounded value.
public static double RoundToNearest(double value)
result = MathHelper.RoundToNearest(value)
Rounds the double value to the nearest significant digit.
- value
- The double value.
Returns: The rounded value.
public static byte SetBit(byte value, int index, bool bit)
result = MathHelper.SetBit(value, index, bit)
Sets or clears the bit at the specified index in the byte.
- value
- The byte value.
- index
- The zero-based index of the bit to modify.
- bit
- If set to true, the bit is set; otherwise, it is cleared.
Returns: The modified byte value.
public static int SetBit(int value, int index, bool bit)
result = MathHelper.SetBit(value, index, bit)
Sets or clears the bit at the specified index in an integer.
- value
- The integer value.
- index
- The bit index (0-based).
- bit
- True to set the bit; false to clear it.
Returns: The resulting integer value after modification.
public static long SetBit(long value, int index, bool bit)
result = MathHelper.SetBit(value, index, bit)
Sets or clears the bit at the specified index in a long value.
- value
- The long value.
- index
- The bit index (0-based).
- bit
- True to set the bit; false to clear it.
Returns: The resulting long value after modification.
public static int Sign(float value)
result = MathHelper.Sign(value)
Returns the sign of a float value.
- value
- The float value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(sbyte value)
result = MathHelper.Sign(value)
Returns the sign of a sbyte value.
- value
- The sbyte value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(double value)
result = MathHelper.Sign(value)
Returns the sign of a double value.
- value
- The double value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(decimal value)
result = MathHelper.Sign(value)
Returns the sign of a decimal value.
- value
- The decimal value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(TimeSpan value)
result = MathHelper.Sign(value)
Returns the sign of a TimeSpan value.
- value
- The TimeSpan value.
Returns: -1, 0, or 1 depending on the sign of the Ticks.
public static int Sign(long value)
result = MathHelper.Sign(value)
Returns the sign of a long value.
- value
- The long value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(int value)
result = MathHelper.Sign(value)
Returns the sign of an int value.
- value
- The int value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static int Sign(short value)
result = MathHelper.Sign(value)
Returns the sign of a short value.
- value
- The short value.
Returns: -1, 0, or 1 depending on the sign of the value.
public static decimal Sin(decimal value)
result = MathHelper.Sin(value)
Returns the sine of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The sine of the angle as a decimal.
public static double Sin(double value)
result = MathHelper.Sin(value)
Returns the sine of the specified double angle.
- value
- An angle, measured in radians.
Returns: The sine of the angle.
public static decimal Sinh(decimal value)
result = MathHelper.Sinh(value)
Returns the hyperbolic sine of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The hyperbolic sine of the angle as a decimal.
public static double Sinh(double value)
result = MathHelper.Sinh(value)
Returns the hyperbolic sine of the specified double angle.
- value
- An angle, measured in radians.
Returns: The hyperbolic sine of the angle.
public static decimal Sqrt(decimal value)
result = MathHelper.Sqrt(value)
Returns the square root of the decimal value.
- value
- The decimal value.
Returns: The square root of the value.
public static double Sqrt(double value)
result = MathHelper.Sqrt(value)
Returns the square root of the double value.
- value
- The double value.
Returns: The square root of the value.
public static double Tan(double value)
result = MathHelper.Tan(value)
Returns the tangent of the specified double angle.
- value
- An angle, measured in radians.
Returns: The tangent of the angle.
public static decimal Tan(decimal value)
result = MathHelper.Tan(value)
Returns the tangent of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The tangent of the angle as a decimal.
public static double Tanh(double value)
result = MathHelper.Tanh(value)
Returns the hyperbolic tangent of the specified double angle.
- value
- An angle, measured in radians.
Returns: The hyperbolic tangent of the angle.
public static decimal Tanh(decimal value)
result = MathHelper.Tanh(value)
Returns the hyperbolic tangent of the specified decimal angle.
- value
- An angle, measured in radians (as decimal).
Returns: The hyperbolic tangent of the angle as a decimal.
public static double ToAngles(double radian)
result = MathHelper.ToAngles(radian)
Converts an angle in radians to degrees.
- radian
- The angle in radians.
Returns: The angle in degrees.
public static decimal ToDecimal(long mantissa, int exponent)
result = MathHelper.ToDecimal(mantissa, exponent)
Creates a decimal value from a given mantissa and exponent by applying power-of-ten adjustments.
- mantissa
- The mantissa.
- exponent
- The exponent, where positive values scale up and negative values scale down.
Returns: The resulting decimal value.
public static decimal? ToDecimal(double value)
result = MathHelper.ToDecimal(value)
Converts a double to a decimal if within the allowed range; otherwise, returns null.
- value
- The double value.
Returns: The converted decimal value or null when conversion is not possible.
public static decimal? ToDecimal(float value)
result = MathHelper.ToDecimal(value)
Converts a float to a decimal if within the allowed range; otherwise, returns null.
- value
- The float value.
Returns: The converted decimal value or null when conversion is not possible.
public static double ToRadians(double angle)
result = MathHelper.ToRadians(angle)
Converts an angle in degrees to radians.
- angle
- The angle in degrees.
Returns: The angle in radians.
public static double Truncate(double value)
result = MathHelper.Truncate(value)
Truncates the double by discarding the fractional part.
- value
- The double value.
Returns: The truncated value.
public static decimal Truncate(decimal value)
result = MathHelper.Truncate(value)
Truncates the decimal by discarding the fractional part.
- value
- The decimal value.
Returns: The truncated value.
Fields
public const int MaxExponent
value = MathHelper.MaxExponent
Maximum exponent value for decimal mantissa scaling.
public const int MinExponent
value = MathHelper.MinExponent
Minimum exponent value for decimal mantissa scaling.