HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

BigDecimal class with arbitrary precision

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bigdecimalwitharbitraryprecisionclass

Problem

I recently came across a need to display very large decimal numbers and realized that I could use a BigDecimal class. After some tinkering I decided to leverage the BigInteger class to do the heavy lifting.

```
using System;
using System.Numerics;
using System.Text;

namespace Numerics
{
public class BigDecimal : IComparable
{
//The number represents all the digits in the displayed value.
//The precision is the number of decimal places.
//maxPrecision is the maximum number of decimal places allowed.
//This is adjustable to any arbitrary number with the function provided.
BigInteger number = 0;
BigInteger precision = 0;
static BigInteger maxPrecision = 20;
public static BigInteger MAXPRECISION
{
get { return maxPrecision; }
}
#region Constructors
//Constructors that accept limited numeric types
public BigDecimal()
{
BigInteger test = new BigInteger();
BigInteger test2 = test + 1;
}
public BigDecimal(BigInteger num)
{
number = num;
}
public BigDecimal(int num)
{
number = num;
}
public BigDecimal(double num)
{
BigDecimal temp = new BigDecimal((decimal)num);
number = temp.number;
precision = temp.precision;
}
public BigDecimal(decimal num)
{
number = (BigInteger)num;
if (num == 0)
{
return;
}
if (number == 0)
{
string tempNum = num.ToString();
if (num (BigDecimal a, BigDecimal b)
{
BigInteger maxPrecision = BigInteger.Max(a.precision, b.precision);
if (a.precision b.number;
}
if (b.precision (b.number * BIPow(10, maxPrecision - b.precision));
}
return a.number >

Solution

I don't understand the "max precision" logic at all.

  • If 20 is the max, then why is the precision a big integer? Just make it an integer if it can't get bigger than 20. But...



  • ...if 20 is the max, then how is it possible to get a decimal with precision of 40? (Multiply two precision-20 numbers together and that's what you get.) Or 29? (A Decimal can have 29 digits after the decimal.) Maximums should be maximums. We should have an iron clad guarantee that the precision is never observed to exceed the maximum.



  • The type doesn't do what it says on the tin. You said you want an arbitrary precision decimal, and you then put a tiny little cap on the precision! The built in decimal type already has a billion times more precision than 20 places; what's the point of an "arbitrary precision" decimal type that is less precise than the built in one?



Scrap this whole idea. There is no maximum precision in an arbitrary precision numeric type; that's what "arbitrary" means.

Moreover: consider the merits of implementing not BigDecimal but rather BigFraction. A BigDecimal is simply a BigFraction where the denominator is a power of ten. The logic for adding, subtracting, multiplying and dividing arbitrary big fractions is actually easier than getting all this stuff right for the case restricted to decimal.

Context

StackExchange Code Review Q#141861, answer score: 6

Revisions (0)

No revisions yet.