Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big numbers with fraction support

Tags:

c#

numeric

I need a c# number something that can handle very 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?

like image 214
dutt Avatar asked Dec 29 '22 21:12

dutt


1 Answers

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.

like image 62
Tomas Petricek Avatar answered Dec 31 '22 11:12

Tomas Petricek