Hi all I was wondering is there a class in .Net that is used to calculate precise numbers with no rounding errors?
For example, this is not what I want:
decimal dividend = Decimal.One;
decimal divisor = 3;
dividend/divisor * divisor // gives us 0.9999999999999999999999999999 instead of 1
I was thinking if there is a number class that buffers operation until when we need to display it, in other words it would look like this:
Num n = new Num(1);
n.DivideBy(3);
n.MultiplyBy(3);
n.toString(); // gives us "1"
Num n2 = new Num(n);
n2.DivideBy(3);
int decimal_places = 8;
n2.RoundHalfUp(decimal_places);
n2.toString(); // gives us "0.33333333"
Of course this is just an example implementation. Basically the main point here is I'm searching for a class that does not have any rounding errors (usually by delaying calculations to the last moment).
I understand that performance would be slower than Double
or Decimal
. But it doesn't have to do calculations blindly fast, as long as it is within acceptable time.