Skip to content

BigDecimal API Documenation

Adam White edited this page Oct 7, 2024 · 5 revisions

BigDecimal

BigDecimal is an arbitrary precision floating point number type/class.
It stores a Mantissa and an Exponent. The Mantissa is of type BigInteger, enabling BigDecimal to have arbitrary precision. BigDecimal is a base-10 floating point number.

Based on code by Jan Christoph Bernack (http://stackoverflow.com/a/4524254 or jc.bernack at gmail.com)
Modified, extended and maintained by Adam White (https://github.com/AdamWhiteHat - AdamWhiteHat at outlook.com)
Further modified by Rick Harker, Rick.Rick.Harker at gmail.com

public readonly struct BigDecimal :
System.IComparable,
System.IComparable<ExtendedNumerics.BigDecimal>,
System.IComparable<int>,
System.IComparable<System.Nullable<int>>,
System.IComparable<decimal>,
System.IComparable<double>,
System.IComparable<float>,
System.IEquatable<ExtendedNumerics.BigDecimal>

Implements System.IComparable, System.IComparable<BigDecimal>, System.IComparable<System.Int32>, System.IComparable<System.Nullable<System.Int32>>, System.IComparable<System.Decimal>, System.IComparable<System.Double>, System.IComparable<System.Single>, System.IEquatable<BigDecimal>

Constructors
BigDecimal(decimal) Initializes a new instance of BigDecimal from a Decimal.
BigDecimal(double) Initializes a new instance of BigDecimal from a Double.
BigDecimal(float) Initializes a new instance of BigDecimal from a Single.
BigDecimal(int) Initializes a new instance of BigDecimal from a Int32.
BigDecimal(BigInteger) Initializes a new instance of BigDecimal from a BigInteger.
BigDecimal(BigInteger, int) Initializes a new instance of BigDecimal from a mantissa and an exponent.
BigDecimal(BigInteger, BigInteger) Initializes a new instance of BigDecimal from a fraction, specified as a numerator and a denominator.
Fields
Exponent The exponent of the internal floating point number representation of this BigDecimal.
Mantissa The mantissa of the internal floating point number representation of this BigDecimal.
Properties
AlwaysNormalize Specifies whether a call to Normalize is made after every operation and during constructor invocation. The default value is true.
AlwaysTruncate Specifies whether the significant digits should be truncated to the given precision after each operation.
Setting this to true will tend to accumulate errors at the precision boundary after several arithmetic operations.
Therefore, you should prefer using Round(BigDecimal, int) explicitly when you need it instead,
such st at the end of a series of operations, especially if you are expecting the result to be truncated at the precision length.
This should generally be left disabled by default.
This setting may be useful if you are running into memory or performance issues, as could conceivably be brought on by many operations on irrational numbers.
DecimalPlaces Returns the number of digits to the right of the decimal point. Same thing as the output of ExtendedNumerics.BigDecimal.PlacesRightOfDecimal(ExtendedNumerics.BigDecimal)
E Gets a value that represents the number e, also called Euler's number.
Length The length of the BigDecimal value (Equivalent to SignifigantDigits).
MinusOne Gets a value that represents the number -1 (negative one).
One Gets a value that represents the number 1 (one).
OneHalf Gets a value that represents the number 0.5 (one half).
Pi Gets a value that represents the number Pi.
Precision Sets the desired precision of all BigDecimal instances, in terms of the number of digits to the right of the decimal.
If AlwaysTruncate is set to true all operations are affected.
Sign Gets a number that indicates the sign (negative, positive, or zero) of the current BigDecimal object.
SignifigantDigits Gets the number of significant digits in BigDecimal.
Essentially tells you the number of digits in the mantissa.
Ten Gets a value that represents the number 10 (ten).
WholeValue Gets the whole-number integer (positive or negative) value of this BigDecimal, so everything to the left of the decimal place.
Equivalent to the Truncate function for a float.
Zero Gets a value that represents the number 0 (zero).
π Gets a value that represents the number Pi.
Methods
Abs(BigDecimal) Returns the absolute value of the BigDecimal
Add(BigDecimal, BigDecimal) Adds two BigDecimal values.
Arccos(BigDecimal) Arbitrary precision inverse cosine function.
Arccos(BigDecimal, int) Arbitrary precision inverse cosine function.
Arccot(BigDecimal) Arbitrary precision inverse cotangent function.
Arccot(BigDecimal, int) Arbitrary precision inverse cotangent function.
Arccsc(BigDecimal) Arbitrary precision inverse cosecant function.
Arccsc(BigDecimal, int) Arbitrary precision inverse cosecant function.
Arcsec(BigDecimal) Arbitrary precision inverse secant function.
Arcsec(BigDecimal, int) Arbitrary precision inverse secant function.
Arcsin(BigDecimal) Arbitrary precision inverse sine function.
Arcsin(BigDecimal, int) Arbitrary precision inverse sine function.
Arctan(BigDecimal) Arbitrary precision inverse tangent function.
Arctan(BigDecimal, int) Arbitrary precision inverse tangent function.
Ceiling(BigDecimal) Rounds a BigDecimal up to the next largest integer value, even if the fractional part is less than one half. Equivalent to obtaining the floor and then adding one.
CompareTo(decimal) Compares the current instance to a System.Decimal floating point value and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the System.Decimal value.
CompareTo(double) Compares the current instance to a double-precision floatinig-point value and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the double-precision floatinig-point value.
CompareTo(BigDecimal) Compares the current instance to a second BigDecimal and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the other value.
CompareTo(float) Compares the current instance to a single-precision floatinig-point value and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the single-precision floatinig-point value.
CompareTo(int) Compares the current instance to a 32-bit signed integer and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the 32-bit signed integer.
CompareTo(Nullable<int>) Compares the current instance to a nullable System.Int32 value and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the nullable System.Int32 value.
Cos(BigDecimal) Arbitrary precision cosine function.
Cos(BigDecimal, int) Arbitrary precision cosine function.
Cosh(BigDecimal) Arbitrary precision hyperbolic cosine function.
Cosh(BigDecimal, int) Arbitrary precision hyperbolic cosine function.
Cot(BigDecimal) Arbitrary precision cotangent function.
The input must not be zero, as the cotangent is undefined at that value.
Cot(BigDecimal, int) Arbitrary precision cotangent function.
The input must not be zero, as the cotangent is undefined at that value.
Coth(BigDecimal) Arbitrary precision hyperbolic cotangent function.
Coth(BigDecimal, int) Arbitrary precision hyperbolic cotangent function.
The input must not be zero.
Csc(BigDecimal) Arbitrary precision cosecant function.
The input must not be zero or π, as the cosecant is undefined at that value.
Csc(BigDecimal, int) Arbitrary precision cosecant function.
The input must not be zero or π, as the cosecant is undefined at that value.
Csch(BigDecimal) Arbitrary precision hyperbolic cosecant function.
The input must not be zero.
Csch(BigDecimal, int) Arbitrary precision hyperbolic cosecant function.
The input must not be zero.
Divide(BigDecimal, BigDecimal) Divides two BigDecimal values.
Divide(BigDecimal, BigDecimal, int) Divides two BigDecimal values.
Equals(Nullable<BigDecimal>, Nullable<BigDecimal>) Determines if two instances of BigDecimal are equal.
The precise behavior of this method depends on the values of the static members AlwaysTruncate and AlwaysNormalize.
If AlwaysTruncate or AlwaysNormalize is true, then both values are rounded at Precision
or normalized with Normalize(BigDecimal), respectively.
Then their mantissas and exponents are compared.
If AlwaysTruncate is false, then their mantissas and exponents are compared exactly as they are, to as many digits as is tracked by their instances
(which may well exceed Precision and will be unlikely to have the same number of digits,
unless the same number of multiplications and divisionsn were performed on them).
If AlwaysTruncate is false, it is recommended that you use the overload of Equals(Nullable<BigDecimal>, Nullable<BigDecimal>, int) that takes a precision parameter,
or you round both values off to some the level of precision that you care about first before calling this method.
Equals(Nullable<BigDecimal>, Nullable<BigDecimal>, int) Determines if two instances of BigDecimal are equal, up to precision digits.
The precise behavior of this method depends on the value of AlwaysTruncate.
If AlwaysTruncate is true, then both arguments will first be rounded to precision or Precision decimal places,
which ever is smaller. Then they will be checked for equivalency.
If AlwaysTruncate is false, then this method will behave as expected.
Exp(BigDecimal) Calculates e^x to arbitrary precision.
Exp(BigDecimal, int) Calculates e^x to arbitrary precision.
Floor(BigDecimal) Rounds a BigDecimal down to the next smallest integer value, even if the fractional part is greater than one half. Equivalent to discarding everything right of the decimal point.
GetDecimalIndex() Returns the zero-based index of the decimal point, if the BigDecimal were rendered as a string.
GetFractionalPart() Gets the fractional part of the BigDecimal, setting everything left of the decimal point to zero.
GetHashCode() Returns the hash code for the current BigDecimal object.
GetWholePart() Returns the whole number integer part of the BigDecimal, dropping anything right of the decimal point. Essentially behaves like Math.Truncate(). For
example, GetWholePart() would return 3 for Math.PI.
IsNegative() This method returns true if the BigDecimal is less than zero, false otherwise.
IsPositive() This method returns true if the BigDecimal is greater than zero, false otherwise.
IsZero() This method returns true if the BigDecimal is equal to zero, false otherwise.
Ln(BigDecimal) Returns the natural logarithm of the input.
Ln(BigDecimal, int) Returns the natural logarithm of the input to a specified precision.
Log(BigDecimal, int) Returns the logarithm of an argument in an arbitrary base to the number of digits specified in Precision.
Log(BigDecimal, int, int) Returns the logarithm of an argument in an arbitrary base.
Log10(BigDecimal) Returns the base-10 logarithm of an argument.
Log10(BigDecimal, int) Returns the base-10 logarithm of an argument.
Log2(BigDecimal) Returns the base-2 logarithm of an argument to the number of digits specified in Precision.
Log2(BigDecimal, int) Returns the base-2 logarithm of an argument.
LogN(int, BigDecimal) Returns the logarithm of an argument in an arbitrary base to the number of digits specified in Precision.
LogN(int, BigDecimal, int) Returns the logarithm of an argument in an arbitrary base.
Max(BigDecimal, BigDecimal) Returns the larger of two BigDecimal values.
Min(BigDecimal, BigDecimal) Returns the smaller of two BigDecimal values.
Mod(BigDecimal, BigDecimal) Divides two BigDecimal values, returning the remainder and discarding the quotient.
Multiply(BigDecimal, BigDecimal) Multiplies two BigDecimal values.
Negate(BigDecimal) Returns the result of multiplying a BigDecimal by negative one.
Normalize(BigDecimal) Removes any trailing zeros on the mantissa, adjusts the exponent, and returns a new BigDecimal.
NthRoot(BigDecimal, int, int) Returns the Nth root of the supplied input decimal to the given number of places.
NumberOfDigits(BigInteger) Returns the number of digits to the right of the decimal point.
Parse(decimal) Converts the string representation of a decimal to the BigDecimal equivalent.
Parse(double) Converts the string representation of a double to the BigDecimal equivalent.
Parse(float) Converts the string representation of a float to the BigDecimal equivalent.
Parse(string) Converts the string representation of a decimal to the BigDecimal equivalent.
Parse(string, IFormatProvider) Converts the string representation of a decimal in a specified culture-specific format to its BigDecimal equivalent.
Pow(double, double) Returns a specified number raised to the specified power.
Pow(BigDecimal, BigDecimal, int) Returns a specified number raised to the specified power.
Note: This method may be a bit slower than the other Pow overloads. Prefer the other overloads if possible.
To improve execution speed, set AlwaysTruncate to true
and set Precision to only the precision that you need.
Pow(BigDecimal, BigInteger) Returns a specified number raised to the specified power.
Round(BigDecimal) Rounds a BigDecimal value to the nearest integral value.
Round(BigDecimal, int) Rounds a BigDecimal to the given number of digits to the right of the decimal point.
Pass a negative precision value to round (zero) digits to the left of the decimal point in a manner that mimics Excel's ROUNDDOWN function.
Round(BigDecimal, int, RoundingStrategy) Rounds a BigDecimal value off at the specified level of precision. A parameter specifies how to round the value if it is midway between two values.
Round(BigDecimal, MidpointRounding) Rounds a BigDecimal value to the nearest integral value. A parameter specifies how to round the value if it is midway between two numbers.
Sec(BigDecimal) Arbitrary precision secant function.
The input must not be (2*n + 1)*π/2 (an odd multiple of π/2), as the secant is undefined at that value.
Sec(BigDecimal, int) Arbitrary precision secant function.
The input must not be (2*n + 1)*π/2 (an odd multiple of π/2), as the secant is undefined at that value.
Sech(BigDecimal) Arbitrary precision hyperbolic secant function.
Sech(BigDecimal, int) Arbitrary precision hyperbolic secant function.
Sin(BigDecimal) Arbitrary precision sine function.
Sin(BigDecimal, int) Arbitrary precision sine function.
Sinh(BigDecimal) Arbitrary precision hyperbolic sine function.
Sinh(BigDecimal, int) Arbitrary precision hyperbolic sine function.
SquareRoot(BigDecimal, int) Returns the square root of the supplied BigDecimal to the given number of places..
Subtract(BigDecimal, BigDecimal) Subtracts two BigDecimal values.
Tan(BigDecimal) Arbitrary precision tangent function.
The input must not be π/2 or 3π/2, as the tangent is undefined at that value.
Tan(BigDecimal, int) Arbitrary precision tangent function.
The input must not be π/2 or 3π/2, as the tangent is undefined at that value.
Tanh(BigDecimal) Arbitrary precision hyperbolic tangent function.
Tanh(BigDecimal, int) Arbitrary precision hyperbolic tangent function.
ToScientificENotation(BigDecimal) Converts the numeric value of the specified BigDecimal object to its equivalent string representation in scientific E notation.
ToString() Converts the numeric value of the current BigDecimal object to its equivalent string representation.
ToString(IFormatProvider) Converts the numeric value of the current BigDecimal object to its equivalent string representation by using the specified culture-specific formatting information.
Truncate(BigDecimal) Truncates the BigDecimal at the decimal point. Equivalent to using Floor.
TryParse(string, BigDecimal) Tries to convert the string representation of a number to its BigDecimal equivalent, and returns a value that indicates whether the conversion succeeded.
TryParse(string, IFormatProvider, BigDecimal) Tries to convert the string representation of a number in a specified style and culture-specific format
to its BigDecimal equivalent and passing the result to the out parameter if successfull.
Returns a boolean value that indicates whether the conversion was successful.
Operators
operator +(BigDecimal, BigDecimal) Adds two specified BigDecimal values.
operator --(BigDecimal) Decrements a BigDecimal value by 1.
operator /(BigDecimal, BigDecimal) Divides two specified BigDecimal values.
explicit operator decimal(BigDecimal) Converts value to an System.Decimal if possible, otherwise throws System.OverflowException .
explicit operator double(BigDecimal) Converts value to an System.Double if possible, otherwise throws System.OverflowException .
explicit operator float(BigDecimal) Converts value to an System.Single if possible, otherwise throws System.OverflowException .
explicit operator int(BigDecimal) Converts value to an System.Int32 if possible, otherwise throws System.OverflowException .
explicit operator BigInteger(BigDecimal) Performs an explicit conversion of a BigDecimal value to a System.Numerics.BigInteger value.
explicit operator uint(BigDecimal) Converts value to an System.UInt32 if possible, otherwise throws System.OverflowException .
operator >(BigDecimal, BigDecimal) Returns a value that indicates whether a BigDecimal value is greater than another BigDecimal value.
operator >=(BigDecimal, BigDecimal) Returns a value that indicates whether a BigDecimal value is greater than or equal to another BigDecimal value.
implicit operator BigDecimal(byte) Performs an explicit conversion of a System.Byte value to a BigDecimal value.
implicit operator BigDecimal(decimal) Performs an explicit conversion of a System.Decimal value to a BigDecimal value.
implicit operator BigDecimal(double) Performs an explicit conversion of a System.Double value to a BigDecimal value.
implicit operator BigDecimal(float) Performs an explicit conversion of a System.Single value to a BigDecimal value.
implicit operator BigDecimal(int) Performs an explicit conversion of a System.Int32 value to a BigDecimal value.
implicit operator BigDecimal(long) Performs an explicit conversion of a System.Int64 value to a BigDecimal value.
implicit operator BigDecimal(sbyte) Performs an explicit conversion of a System.SByte value to a BigDecimal value.
implicit operator BigDecimal(short) Performs an explicit conversion of a System.Int16 value to a BigDecimal value.
implicit operator BigDecimal(BigInteger) Performs an explicit conversion of a value to a BigDecimal value.
implicit operator BigDecimal(uint) Performs an explicit conversion of a System.UInt32 value to a BigDecimal value.
implicit operator BigDecimal(ulong) Performs an explicit conversion of a System.UInt64 value to a BigDecimal value.
implicit operator BigDecimal(ushort) Performs an explicit conversion of a System.UInt16 value to a BigDecimal value.
operator ++(BigDecimal) Increments a BigDecimal value by 1.
operator <(BigDecimal, BigDecimal) Returns a value that indicates whether a BigDecimal value is less than another BigDecimal value.
operator <=(BigDecimal, BigDecimal) Returns a value that indicates whether a BigDecimal value is less than or equal to another BigDecimal value.
operator *(BigDecimal, BigDecimal) Multiplies two specified BigDecimal values.
operator -(BigDecimal, BigDecimal) Subtracts two specified BigDecimal values.
operator -(BigDecimal) Negates a specified BigDecimal value.
operator +(BigDecimal) Returns the value of the BigDecimal operand. (The sign of the operand is unchanged.)
Explicit Interface Implementations
System.IComparable.CompareTo(object) Compares the current instance with another object of the same type and returns
an integer that indicates whether the current instance precedes, follows, or
occurs in the same position in the sort order as the other object.
Clone this wiki locally