Arbitrary-Precision Decimals in C#

Arbitrary-Precision Decimals in C#

Big Decimal:

  • Install the J# runtime (it's free): http://www.microsoft.com/downloads/en/details.aspx?familyid=f72c74b3-ed0e-4af8-ae63-2f0e42501be1&displaylang=en

Big Int (If you like J.D.'s solution, or want to come up with a rational number/fraction type class. That, and I somehow missed that you were looking for decimals, not ints):

  • Get C# 4.0: Big integers in C#
  • Get the IntX class: http://intx.codeplex.com/

Need Arbitrary-Precision-Arithmetic in C#

MPIR, a fork of the GMP project, has C# bindings. Personally, I've found them easier to deal with when raising problems, to the point that I no longer worry about GMP (primarily due to its tendency to exit violently when running out of memory).

There are others listed here but I have no direct experience with them so can't comment on their usefulness.

Arbitrary/Infinite precision variables - C#

I don't want to use any external libraries--would I have to write a class myself?

I don't really get why would you implement a new library instead of using an existing one. But I would then suggest the BigRational library of Microsoft. Source codes are available as well. It uses the BigInteger type internally.

Arbitrary Precision for decimals in C# help?

Can you not convert your int's to BigDecimal's before comparing them?

I assume you understand the problem here is that there is no operator overload for the greater, less than, etc. signs on the BigDecimal class that accepts an int.

Set the precision for Decimal numbers in C#

This isn't exactly what you're asking, but you could initialize a NumberFormatInfo object within the global scope and use it to format decimals. Here is an example:

using System.Globalization;

NumberFormatInfo setPrecision = new NumberFormatInfo();
setPrecision.NumberDecimalDigits = 2;
decimal test = 1.22223;

Console.Write(test.ToString("N", setPrecision)); //Should write 1.23

setPrecision.NumberDecimalDigits = 3;
test = 5m/3m;

Console.Write(test.ToString("N", setPrecision)); //Should write 1.667

MSDN Link: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo(v=vs.110).aspx

NumberDecimalDigits usage example: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimaldigits(v=vs.110).aspx

How can i get precision up-to 128 decimal places in C#?

The easiest way to achieve arbitrary precision numbers is to combine the BigInteger class from System.Numerics with an int exponent. You could use BigInteger for your exponent, but this is likely overkill as the numbers would be well beyong meaningful in scale.

So if you create a class along these lines:

public class ArbDecimal
{
BigInteger value;
int exponent;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
int place;
foreach (char digit in value.ToString())
{
if (place++ == value.ToString().Length - exponent)
{
sb.Append('.');
}
sb.Append(digit);
}
return sb.ToString();
}
}

You should then be able to define your mathematical operations using the laws of indices with the value and exponent fields.

For instance, to achieve addition, you would scale the larger value to have the same exponent as the smaller one by multiplying it by 10^(largerExp-smallerExp) then adding the two values and rescaling.


In your class, the number 0.01 would be represented like:

value = 1

exponent = -2

Due to the fact that 1*10^-2 = 0.01.


Utilising this method, you can store arbitrarily precise (and large) numbers limited only by the available ram and the .NET framework's object size limit.

String format double with arbitrary precision, fixed decimal position

This should give you what you want:

public static string PrintRow(string token, string unit, double value, string desc)
{
// convert the number to a string and separate digits
// before and after the decimal separator
string[] tokens = value
.ToString()
.Split(new string[] { CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator }, StringSplitOptions.RemoveEmptyEntries);

// calculate padding based on number of digits
int lpad = 11 - tokens[0].Length;
int rpad = 19 - tokens[1].Length;

// format the number only
string number = String.Format("{0}{1}.{2}{3}",
new String(' ', lpad),
tokens[0],
tokens[1],
new string(' ', rpad));

// construct the whole string
return string.Format(" {0,-4}.{1,-4}{2} : {3,-23}\n",
token, unit, number, desc);
}


Related Topics



Leave a reply



Submit