14

Is it possible to get more than 100 decimal digits in C#?

If yes what is the necessary portion of code?

In Java there something call BigDecimal but it still can't reach more than 55 digits.

Abel
  • 56,041
  • 24
  • 146
  • 247
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
  • 3
    possible duplicate of [What is the equivalent of the Java BigDecimal class in C#?](http://stackoverflow.com/questions/2863388/what-is-the-equivalent-of-the-java-bigdecimal-class-in-c) (essentially: C# doesn't have one, do you need to use C#?) – Noon Silk Oct 25 '11 at 10:19
  • 1
    Why the downvote? Questions for high precision arithmetic seems a very normal one, no? – Abel Oct 25 '11 at 10:19
  • 1
    `System.Decimal` won't work since it only supports about 28 digits. `BigInteger` only supports integers. – CodesInChaos Oct 25 '11 at 10:20
  • 2
    I think you need to use some third party library, such as BigRational http://bcl.codeplex.com/releases/view/42782 – CodesInChaos Oct 25 '11 at 10:21
  • @Abel: The problem with 'normal questions' is that are asked too often and searched too little. http://stackoverflow.com/search?q=%5Bc%23%5D+biginteger . That's how they become 'normal' – sehe Oct 25 '11 at 10:22
  • @CodeInChaos: OR use the Fraction class from IronScheme. – leppie Oct 25 '11 at 10:22
  • @leppie: make that an answer and get points for creativity – sehe Oct 25 '11 at 10:23
  • 2
    @sehe: Nah, making it an answer, means I will have to support it :) – leppie Oct 25 '11 at 10:24

2 Answers2

9

Using J# libraries:

Download the J# Redistributable to obtain the J# class libraries. This article hints about how to use it to get ZIP-ability in your .NET projects, and explains a bit about BigDecimal, but as Ramhound explained, the article is relatively old.

After download, add the vsjlib to your project. It didn't work for me on .NET 4.0, but it did work on 2.0 project. It contains a BigDecimal class, among others. Not sure, however, if that gives you up to 100 decimals, but you might give it a try.

Using MPIR -- Multi Precision Integers and Rationals

You can download a wrapper for the MPIR library for .NET here (download version 0.2). Then, do the following:

  • Add the the files \wrapper.*.dll to your project, make sure that on each built they are copied to your Debug or Release directory.
  • Add \wrapper\xmpir.cs to your project
  • Add the following code to your project:

    // set the precision to 512 bits (adjust this to your needs)
    mpir.mpf_set_default_prec(512);
    ulong default_prc = mpir.mpf_get_default_prec();
    
    // init vars (important!)
    mpir.mpf_t val = mpir.mpf_init_set_d(.5);
    mpir.mpf_t result = mpir.mpf_init_set_ui(0);
    
    // calculate 0.5^200
    mpir.mpf_pow_ui(result, val, 200);
    double dresult = mpir.mpf_get_d(result);
    
    // convert result to a string, in the form 1.2345 (dot not in output) and exp holding exponent
    long exp;
    string sresult = mpir.mpf_get_string(out exp, 10, 0, result);
    
    // free vars (important!)
    mpir.mpf_clear(val);
    mpir.mpf_clear(result);
    

Note that the variable sresult will only hold the significant digits. You'll have to add the logic for printing the exponent as well, or a bunch of zeroes.

Result is 6.2230152778611417071440640537801242405902521687211671331011166147896988340353834411839448231257136169569665895551224821247160434722900390625E-60

A documentation PDF file on the full MPIR library shows how to use it in more detail.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • thanks for your help but as you know this library use the java implementation of Bigdecimal – Hussein Zawawi Oct 25 '11 at 10:48
  • The article is not current. There is a class to manipulate a Zip file, this has been the clast for at least 5 years, 2 years after the article was written. – Security Hound Oct 25 '11 at 11:01
  • @Ramhound, I updated the text with correct download locations for J#. – Abel Oct 25 '11 at 12:10
  • @HusseinX, I added code for the MPIR library wrapper, which is a bit cumbersome to use, but that's because they're from the C++ world. But the documentation is good. – Abel Oct 25 '11 at 12:11
-1

If you use .net Framework 4.0, you can refernce System.Numerics assembly. or you can download BigInteger from codeplex

Usage of BigIngeger : http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

for example:

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

try {
   posBigInt = BigInteger.Parse(positiveString);
   Console.WriteLine(posBigInt);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                     positiveString);
}

if (BigInteger.TryParse(negativeString, out negBigInt))
  Console.WriteLine(negBigInt);
else
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                      negativeString);

// The example displays the following output:
//   9.1389681247993671255432112E+31
//   -9.0315837410896312071002088037E+34

EDITED: here have a bigNum project http://sine.codeplex.com/

using W3b.Sine;

public class ExpressionSample {

    public static void Main(String[] args) {

        Dictionary<String,BigNum> symbols = new Dictionary<String,BigNum>() {
            {"x", 100} // the implicit conversion defined
        };

        Expression expression = new Expression("1 + 2 / 3 * x");
        BigNum result = expression.Evaluate( symbols );
        Console.WriteLine( expression.ExpressionString + " == " + result.ToString() );

        BigNum x = 100;
        BigNum xPow100 = x.Pow( 100 );
        Console.WriteLine("100^100 == " + xPow100.ToString() );

    }
}
Aimeast
  • 1,609
  • 14
  • 29
  • 11
    How does using a `BigInteger` class help display **decimal** values? – ChrisF Oct 25 '11 at 10:58
  • No, it only calculate ingeger – Aimeast Oct 25 '11 at 12:28
  • You edited the question a few times, but I'm still at a loss how it helps the OP with 100+ digits precision in _floating point arithmetic_ (decimals, that is). You only mention big integers. – Abel Oct 25 '11 at 18:03
  • 1
    Sine can display decimal value with 100 digits precision. see details in http://sine.codeplex.com/ – Aimeast Oct 26 '11 at 10:14