views:

167

answers:

7

I have an application that I want to be able to use large numbers and very precise numbers. For this, I needed a precision interpretation and IntX only works for integers.

Is there a class in .net framework or even third party(preferably free) that would do this?

Is there another way to do this?

+6  A: 

Maybe the Decimal type would work for you?

Tim Pietzcker
Thanks Guys, I was wondering about performance but with the amount of precision I get, this shouldn't be a problem. Can decimal handle large numbers though?
AKRamkumar
According to http://stackoverflow.com/questions/230105/net-decimal-what-in-sql, Decimal can handle up to 29 digits to the left of the decimal point (and 28 to the right). Is that large enough?
Tim Pietzcker
+1  A: 

Use decimal for this if possible.

Femaref
A: 

Decimal is 128 bits if that would work.

ho1
+2  A: 

The F# library has some really big number types as well if you're okay with using that...

Justin
+1  A: 

If Decimal doesn't work for you, try implementing (or grabbing code from somewhere) Rational arithmetic using large integers. That will provide the precision you need.

High Performance Mark
+1  A: 

Decimal is a 128-bit (16 byte) value type that is used for highly precise calculations. It is a floating point type that is represented internally as base 10 instead of base 2 (i.e. binary). If you need to be highly precise, you should use Decimal - but the drawback is that Decimal is about 20 times slower than using floats.

Odhran
+2  A: 

You can use the freely available, arbitrary precision, BigDecimal from java.math, which is part of the J# redistributable package from Microsoft and is a managed .NET library.

Place a reference to vjslib in your project and you can something like this:

using java.math;

public void main() 
{
    BigDecimal big = new BigDecimal("1234567890123456789011223344556677889900.0000009876543210000987654321");
    big.add(new BigDecimal(1.0));
    Debug.Print(big);
}

Will print the following to the debug console:

1234567890123456789011223344556677889901.0000009876543210000987654321

Note that, as already mentioned, .NET 2010 contains a BigInteger class which, as a matter of fact, was already available in earlier versions, but only as internal class (i.e., you'd need some reflection to get it to work).

Abel