14,608,158 members

A BigNumber Class Done in C#

Rate this:
3 Sep 2009CPOL
A simple C# library for doing math calculations in any desired precision

Introduction

I wanted to write a simple library for C# that allows to do math calculations at any desired precision level.

Background

During the last few months, I have been playing around with math libraries, and started to implement my own precision math library. I stumbled upon the MAPM library (http://www.tc.umn.edu/~ringx004/mapm-main.html) and decided to create something similar for .NET. This library is the result of my work. Basically. it is a rewrite of the MAPM library for .NET.

My goal is to use this library inside a Mandelbrot fractal renderer I have posted here on CodeProject. Theoretically, this should provide endless zooming into the fractal (at the expense of speed). We will soon see what happens ;-)

Using the Code

The header of the Big Num class looks like this:

```public partial class BigNumber
{
sbyte signum=0;
int exponent=0;
byte[] mantissa= {0};
int dataLength=1;

...
}```

A big number is represented in the form of S * A * 10 ^ B where:

• S ... is the signum
• A ... is the mantissa
• B ... is the exponent

The mantissa is represented as an array of packed bytes.
Each nibble of a byte represents 2 digits of the number.
The mantissa can have an arbitrary length. It is limited to 1 < |a| < 10

Open the project, compile, and run. The included test program will call the function `DoTest()` of the library.

Have a look at the `DoTest()` function to see how to work with this library. You will find examples of how to:

• assign values to `BigNumber`s
• do basic math operations using the overloaded operators +, -, *, and /
• use math functions like rounding sqrt, pow, log10, log2, and more
• calculate 1000! using this library
• calculate the number PI to a desired amount of digits
```public static void DoTest()
{
BigNumber A = 0, B = 0, C = 0;
BigNumber PI= new BigNumber();

// assignment by string
A = "12345";
Console.WriteLine("assigned value was: " + A.ToFullString() +
"(" + A.ToString() + ")");

// assignment by double
A = 123.45;
Console.WriteLine("assigned value was: " + A.ToFullString() +
"(" + A.ToString() + ")");

// assignment by string in exponential form x = a*10^y. 10E3 = 10*10^^3 = 10000
A = "10E3";     // 10E3 = 10*10^3 = 10000
B = "1E4";      // 1E4  =  1*10^4 = 10000
C = 10000;
Console.WriteLine("assigned value was: " + A.ToFullString() +
"(" + A.ToString()+")");
Console.WriteLine("10000 = " + A.ToFullString() + " = " +
B.ToFullString() + " = " + C.ToFullString());

A = 1; B = 2; C = 0;
C = A + B;
Console.WriteLine("the result of " + A.ToFullString() + "+" +
B.ToFullString() + "=" + C.ToFullString());
// addition of BigNumber + double
C = A + 3.2;
// addition of double + BigNumber
C = 3.1 + B;
A = "5.141592"; B = "2.91827";
C = A - B;
Console.WriteLine("the result of " + A.ToFullString() +
"-" + B.ToFullString() + "=" + C.ToFullString());

C = A * B;
Console.WriteLine("the result of " + A.ToFullString() +
"*" + B.ToFullString() + "=" + C.ToFullString());
A = 5.0; B = 3.0;
C = A * B;
Console.WriteLine("the result of " + A.ToFullString() +
"*" + B.ToFullString() + "=" + C.ToFullString());

A = 4; B = 0.5;
C = A.Pow(B);
Console.WriteLine("the result of " + A.ToFullString() +
" pow " + B.ToFullString() + "=" + C.ToFullString());

A = 0.5; B = "5E-1";
C = A.Pow(B,16);
Console.WriteLine("the result of " + A.ToFullString() +
" pow " + B.ToFullString() + "=" + C.ToFullString());

A = "1e3"; // "10E2"; //   "1E3 = 1000";
C = A.Log10();
Console.WriteLine("the result of " + A.ToFullString() +
" Log10 =" + C.ToFullString());

A = "10E3"; B = "1E4"; C=10000 ;

A = BigNumber.BN_E;
C = A.Log();
Console.WriteLine("the result of " + A.ToString() +
" Log =" + C.ToFullString());

A = 3.0;
C = A.Rez();
Console.WriteLine("the result of " + A.ToString() +
" Rez =" + C.ToFullString());

int NumPlaces = 4;
A = 1.53456;
C = A.Round(NumPlaces);
Console.WriteLine("the result of " + A.ToString() +
" Round(" + NumPlaces + ") =" +
C.ToFullString());

NumPlaces = 2;
C = A.Round(NumPlaces);
Console.WriteLine("the result of " + A.ToString() +
" Round(" + NumPlaces + ") =" +
C.ToFullString());

NumPlaces = 0;
C = A.Round(NumPlaces);
Console.WriteLine("the result of " + A.ToString() +
" Round(" + NumPlaces + ") =" +
C.ToFullString());

NumPlaces = 16;
A = 2.0;
C = A.Sqrt(NumPlaces);
Console.WriteLine("the result of " + A.ToString() +
" Sqrt(" + NumPlaces + ") =" +
C.ToFullString());

A = 1.0; B = 0;
try
{
C = A / B;
}
catch (BigNumberException ex)
{
Console.WriteLine("Exception in operation: " + ex.Message);
}

A = 1.0;
for (int i = 1; i <= 1000; i++)
{
A = A * i;
}

Console.WriteLine("the result of 1000!=" + A.ToFullString());
A = A.Round(numDefaultPlaces);
Console.WriteLine("the result of 1000!=" + A.ToString());

DateTime before = DateTime.Now;

NumPlaces = 5000;
CalculatePiAGM(PI, NumPlaces);

TimeSpan ts = DateTime.Now - before;
Console.WriteLine("time for "+NumPlaces+" digits of PI: " +
ts.TotalMilliseconds + "[ms]");
Console.WriteLine(PI.ToFullString());

Console.WriteLine("Press 'x' key to quit test");
while (true)
{
if (i.KeyChar == 'x') break;
}
}```

History

• 31.08.2009
• Initial release
• 03.09.2009
• Implemented setting values from binary and hex strings
• Fixed a bug in the add function

Share

 Software Developer (Senior) Austria
I have started programming at the age of 13 on the commodore 64.

Ever since then I have been programming on many systems in many languages.

During the last 12 years I have been working as professional programmer in different companies and different areas.

Now I am working as freelancer programmer / consultant

 First Prev Next
 Where are the Trogonometric functions ? Member 159101614-May-13 4:18 Member 1591016 14-May-13 4:18
 Parabéns Member 85420094-Apr-12 23:22 Member 8542009 4-Apr-12 23:22
 Small problem found bizarre23-Jun-10 8:57 bizarre2 3-Jun-10 8:57
 Pow KatrienDerijckere3-Apr-10 1:59 KatrienDerijckere 3-Apr-10 1:59
 using ArbitraryMath.dll [modified] Member 466007225-Nov-09 3:19 Member 4660072 25-Nov-09 3:19
 Re: using ArbitraryMath.dll [modified] Dave Kerr2-Dec-11 2:57 Dave Kerr 2-Dec-11 2:57
 using the ArbitraryMath.dll Member 466007220-Nov-09 5:29 Member 4660072 20-Nov-09 5:29
 the sign of the number doesn't display when using ToFullString longstrange1-Nov-09 9:14 longstrange 1-Nov-09 9:14
 Test agains BigInt AndyHo7-Sep-09 10:05 AndyHo 7-Sep-09 10:05
 Fractint thbernt6-Sep-09 23:39 thbernt 6-Sep-09 23:39
 Re: Fractint zinken7-Sep-09 1:26 zinken 7-Sep-09 1:26
 Some comments Jaime Olivares6-Sep-09 11:49 Jaime Olivares 6-Sep-09 11:49
 Re: Some comments Zimmermann Stephan6-Sep-09 23:22 Zimmermann Stephan 6-Sep-09 23:22
 Very Nice eran201831-Aug-09 10:54 eran2018 31-Aug-09 10:54
 :) Good Anthony Daly31-Aug-09 9:28 Anthony Daly 31-Aug-09 9:28
 Looks useful... Rev1.031-Aug-09 5:11 Rev1.0 31-Aug-09 5:11
 Re: Looks useful... Zimmermann Stephan31-Aug-09 5:24 Zimmermann Stephan 31-Aug-09 5:24
 Few Queries programmersmind31-Aug-09 1:09 programmersmind 31-Aug-09 1:09
 Re: Few Queries Zimmermann Stephan31-Aug-09 5:26 Zimmermann Stephan 31-Aug-09 5:26
 Re: Few Queries programmersmind2-Sep-09 1:26 programmersmind 2-Sep-09 1:26
 Last Visit: 14-Aug-20 22:55     Last Update: 14-Aug-20 22:55 Refresh 1