Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

c# - Big numbers with fraction support

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...