开发者

Big numbers with fraction support

开发者 https://www.devze.com 2022-12-24 05:01 出处:网络
I need a c# number something that can handle ver开发者_StackOverflowy large numbers but also fraction support, I looked at System.Numerics.BigInteger coming in .NET 4.0 but I can\'t get it to work wit

I need a c# number something that can handle ver开发者_StackOverflowy large numbers but also fraction support, I looked at System.Numerics.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?


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.


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);


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.


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).


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;
    }
}


An experimental BigRational in BCL Codeplex based on BigInteger


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号