tags:

views:

191

answers:

8

I need a c# number something that can handle very large numbers but also fraction support, I looked at System.Numberics.BigInteger coming in .NET 4.0 but I can't get it to work with fractions.

something i = 2;
something j = 5;
something k = i/j; //should be 0.4

when i tried

 BigInteger i = 2;
 BigInteger j = 5;
 double d = (double)(i/j); //d is 0.0

Does anybody know such a library?

+1  A: 

Check out http://www.extremeoptimization.com/Documentation/Mathematics/Arbitrary_Precision_Arithmetic/Arbitrary_Precision_Rationals.aspx

Although maybe you just want to do this: double d = ((double)i) / ((double)j);

Gabe
A: 

BigInteger supports just what it says on the tin - integral numbers, which fractions are not.

The double datatype supports binary floating-point numbers from ±5.0 × 10−324 to ±1.7 × 10308, and decimal support decimal floating-point numbers from ±1.0 × 10−28 to ±7.9 × 1028. Gabe's answer mentions classes you can use to model fractions, but stick with the primitive datatypes if the ranges are sufficient.

Michael Petrotta
+1  A: 

I once used this library: W3b.Sine. It's:

An arbitrary-precision decimal number library developed in C#.

You could also try http://www.fractal-landscapes.co.uk/bigint.html. However, I have no experience using it.

empi
A: 

This should give a good approximation converting a rational number of two BigIntegers to a double:

struct BigRational
{
    BigInteger numerator;
    BigInteger denominator;

    public double ToDouble()
    {
        BigInteger quotient;
        BigInteger remainder;

        quotient = BigInteger.DivRem(numerator, denominator, out remainder);

        return (double)quotient + (double)remainder / (double)denominator;
    }
}
dtb
Not bad, now just extend it to support all the other operations.
Gabe
A: 

Consider if the Decimal class is big enough:

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors.

If you want to go really big, then look for a "bignum" library such as GMP (and google for bindings).

Will
A: 

Would i/j even mean anything? They're instances of a class, you need to be using BigInteger.Divide(i, j)

Though it's designed to work with integers, so I should imagine it'll throw away any remainder. Which, if it does support straight division without the function call, is why you're getting 0.

AaronM
BigInteger defines an overload for the division operator (http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.op_division(v=VS.100).aspx) so `i/j` is the same as `BigInteger.Divide(i, j)`
Gabe
+3  A: 

F# PowerPack contains a numeric type BigRational. It is implemented in F# and designed for F#, but the type should be perfectly usable from C# as well (including overloaded operators and stuff like that). PowerPack is an additional library with extra F# features, so it isn't a part of the .NET framework, but it's a supported product from Microsoft.

The BigRational type supports all basic operators (+, /, -, *, >, <, >=, <=, ==, !=) and I believe that it automatically keeps a normal form of the number. If you represented the number as two BigInteger values, you'd have to implement comparison such that 1/2 equals 2/4.

Tomas Petricek
A: 

What is the range for your 'very large numbers'?

System.Double.MaxValue = 1.7976931348623157E+308, which seems to be a pretty large number for me, though (MSDN Double.MaxValue)

Chansik Im