C# BigInt, BigFloat library - a free arbitrary precision library for C#

In the course of writing a calculator app, I needed an arbitrary precision maths library for C#. To my surprise, I could not find one online, that was available for free, so I wrote one. I am making this code available, free of charge, for any use, provided that all credits remain in the source files (however edited) and that all substantial changes are sent back to me at adam_a_brown^@hotmail.com (remove the ^).

Important: Please link to this page from your site if you make use of the library

Download the source code (windows project) from here
View the online documentation (generated from the XML comments) here

Update: Version 1.2 (20-03-2011)

  • Fixed return values of static trig functions.

Update: Version 1.1 (12-12-2010)

  • Fixed bug in static logic functions in BigInt
  • Fixed unintended side-effects of static function operations in BigFloat
  • Fixed NAN, Inf etc... handling for Arithmetic comparison operations in BigFloat
  • Improved thread-safety of library.

Important notes:

  • This code is not thouroughly tested. In particular, most things seem to work very well up to precisions of 1024 bits, but have not in general been tested beyond that.
  • This is entirely managed C# code, without recourse to native assembler. This makes it portable, but creates severe performance penalties for all operations.
  • As far as possible, I have tried to implement operations and mathematical functions in fast ways (both asymptotically fast and generally fast). This however, does not extend to multiplication, which is implemented using the standard "schoolbook" algorithm. If you desire fast multiplication at high precisions, I would suggest extending the code to implement more advanced multiplication and division algorithms.
  • The elementary mathematical functions are calculated without guard bits. This means that the last few bits of the results (e.g. of Sin(x)) will be incorrect.
  • The library has some limited support for mixed precision calculations, but it is largely untested. In other words, try to make sure all your numbers are the same precision. It will certainly run faster if they are.
  • If you spot any bugs / infinite loops, please tell me and I'll fix them

High-precision windows calculator

This is a high-precision windows calculator, which uses the BigInt and BigFloat library from above. The source code demonstrates some of the use of the BigInt library. In particular, this calculator maintains 250 decimal digits of precision in addition to what is displayed. This, combined with correct rounding, allows the calculator to do effectively exact calculations in many cases.

Note: To run this program, you will need the .net framework 3.5 installed. It is provided as is, without any warranty express or implied etc... You are free to use it, but I accept no liability for its use.

Download the source code (windows project) from here
Download the zipped .exe here

XSLT style sheet for visual studio documentation

In order to document the source code, I wrote a very basic XSLT style sheet to display the XML data from the XML comment feature in a hierarchical manner. This scores over other free style sheets in three important ways:

  • It modifies the default XML hierarchy, so that subclasses appear under their parent classes etc...
  • It correctly displays the names of all functions and operators
  • It provides a collapsible hierarchy, with [+] and [-] buttons for each topic

Download the XSLT style sheet here

Note that this is my first time experimenting with XSLT, so there are some things it doesn't do, and what it does do is not done in an ideal way. You may therefore find it difficult to modify.

20000 decimal digits of pi (plus the 3.)

These can be viewed here

20000 decimal digits of e (plus the 2.)

These can be viewed here