Tuesday, March 10, 2015

Quick and Dirty .Net BigDecimal Implementation

[UPDATE 8] Added Conversion, Constructors, Comparisons, Operations for types Short, UShort, Byte, SByte.

[UPDATE 7] Added Ceiling, Floor, Truncate functions as well as fixed the Mod function. Added Ctype to/from byte arrays.
[TODO 3] Implement a BigDecimal rounding function that can round to a specified decimal place.

[UPDATE 6] Added a Random function which returns a random BigDecimal number with the specified number of significant digits.
[TODO 2] Fix modulus of negative numbers.

[UPDATE 5] Fixed constructors for Decimal, Double, and Single as arguments.

[UPDATE 4] Providing the new source code below.

[UPDATE 3] I rewrote the entire thing and improved the speed by ~5x to 10x. Removed all string manipulation hacks for calculations and added several other things for ease of use in code. I compiled it to a dll for ease of use.
Download: http://www.mediafire.com/download/yj56gd65616cu7v/BigDecimal.dll
Reference the BigDecimal.Dll,
     "Import TizzyT"
     Optionally you can also "Import TizzyT.BigDecimal"
     Dim bigNumber as BigDecimal = "51316561513854163416165416163163165613.1473415424496255785965457458965348754854856"
     Dim bigNumber as New BigDecimal("51316561513854163416165416163163165613.1473415424496255785965457458965348754854856")
The constructors accepts these types as the parameter:
String, BigInteger, Decimal, Double, Integer, Long, Single, UInteger, ULong.
Supported Operators: /, *, -, +, ^, Mod, =, <, >, <=, >=, <>
There are no BitWise operators.

[TODO 1] Add e constant calculation function and Phi calculation function. Add pre-calculated values to the first 65535 digits for Pi and e (and other constants in the future) for fast lookup and only calculating when needed. Fix potential rounding issue in the divide function. Support parsing and simple calculations with imaginary numbers. Raising to decimal powers.

[UPDATE 2] Added Pi function which (calculates Pi to the specified decimal place).

[UPDATE 1] Added square root function (calculates the square root to a specified decimal place).

    Now many of us I'm sure have gone to Wolfram Alpha and have done some calculations and get very precise answers. I thought why cant I just make a calculator that runs on the computer instead of relying on a website. The problem here is that I typically write in .Net and as far as I know in .Net there isn't yet a BigDecimal implementation though I hear something called Rational Number will be in the future (correct me if I'm wrong). .Net as of 4.5 does include BigInteger though and I figured "hey I can surely manipulate that to do decimals". So by playing with powers of 10 and some string manipulation I threw together this quick and dirty BigDecimal implementation. It does most of the basic arithmetic operations (add, subtract, multiply, divide) and also includes modulus and a limited power operator (only decimals to integer powers allowed).
It also has most of the basic comparison operators (=,<>,>,<,>=,<=).

     I didn't implement a way to do powers (exponents) of decimal numbers to decimal numbers as I don't know of any way to do arithmetic that are to the power of a decimal number. This is one of many things Wolfram Alpha and other tools are still useful for. I did however add a power operator where you can raise a decimal number to an integer power. Again this was a quick and dirty implementation by use of an already existing BigInteger and some string manipulation so this is more of a workaround then a full and proper implementation. With all that said this works as it is intended and I just thought I'd share this for those who want to have such capabilities and/or not want to make their own or use one that others have made (for what ever reason). Below is the source in VB.net, feel free to critique it constructively or even suggest improvements/additions/corrections/other changes (they are always welcome) in the comments, even link to other/better implementations for others to learn from (including myself lol).

     Here is a brief and simplified explanation of how the operators work (take note of the number of digits after the decimal places, trailing zeros after the decimal are omitted).
How Addition is done:
     ex: 2.5 + 3.56
     250 + 356 = 606
     606 * 10^-2 = 6.06
How Subtraction is done:
     ex: 3.56 - 2.5
     356 - 250 = 106
     106 * 10^-2 = 1.06
How Mulitplication is done:
     ex: 1.23 * 2.34
     123 * 234 = 28782
     28782 * 10^-4 = 2.8782
How Division is done:
     ex: 2.75 / 1.23
     27500 / 123 = 223.5772...
     223.5772... * 10^-2 = 2.235772...
How Modulus is done:
     ex: 81.913 mod 1.89
     81913 mod 1890 = 643
     643 * 10^-3 = 0.643
How Powers is done:
     ex: 3.86 ^ 3
     386 ^ 3 = 57512456
     57512456 * 10^(-2 * 3) = 57.512456

Here is an example of what this BigDecimal can do:

Wolfram Alpha would show:

This will give:

New Source:

Old Source: http://pastebin.com/embed_js.php?i=jcNaUhuH

Free for non commercial use. All changes to source must be provide here.

No comments: