Tuesday, June 10, 2014

Handle Big Floating Point Numbers Using BigRational

After I showed in the previous post, how to represent arbitrary big integers with the class BigInteger, today I want to show how to use arbitraty big and precise floating point numbers.
For this we use the class BigRational, which did not make it in the .Net Framework 4, therefore we have to download it, e.g. from here.
To represent an arbitrary big floating point number, BigRational saves numerator and demoninator as a BigInteger and simplifies it, if possible using the Euclidean Algorithm. Therefore we also have to include the class BigInteger, like described in the previous post.
Of course we also have to include BigRational, for this we select Project - Add Reference and select in the dialog Browse the file "BigRationalLibrary.dll", which is located in the download folder of the BigRational project, and there in the folder BigRationalLibrary/bin/Debug.
BigRational is then available in the namespace Numerics, the usage is similiar to that of BigInteger, the following code for example divides A by B:

Numerics.BigRational A = 10000;
Numerics.BigRational B = 20000;
Numerics.BigRational Result = A / B;

No comments:

Post a Comment