0

I'm trying to do calculations with huge numbers. for example:

22352794900029489130063309832192 / 1000000000000000000000000000000

I want the outcome to be 22.35279490 (preferred decimal), but if I use BigInteger the result gets rounded to 22.

Does .NET have something similar to an uint128?

What´s the best practise to do so?

MrAskAlot
  • 11
  • 5
  • 1
    [BigInteger.Remainder](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.remainder?view=net-5.0) –  Apr 18 '21 at 16:25
  • Also see [Biginteger.DivRem](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.divrem?view=net-5.0) –  Apr 18 '21 at 16:26
  • What happens if you just convert each to a double then divide? – Wyck Apr 18 '21 at 16:33

1 Answers1

1

To preserve precision, consider converting the BigInteger values to doubles first, then dividing after.

BigInteger a = BigInteger.Parse("22352794900029489130063309832192");
BigInteger b = BigInteger.Parse("1000000000000000000000000000000");
double da = (double)a;
double db = (double)b;
double result = da / db;
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • ok thanks. i think you won. easiest solutions for my needs. do you know a good solution for reversing that process? like now i have 22.35279490 but i need * 1000000000000000000000000000000. result should be: 2235279490000000000000000000000 but result is: 2235279494058010475075268376185 (adds up random numbers) – MrAskAlot Apr 18 '21 at 16:54
  • 1
    Division is not closed over the integers. You may want to reconsider your precision requirements for general purpose division operations. Or consider the use of a Rational class where the numerator and denominator are BigInteger values. Change of base with fixed precision (as in the decimal expansions of rational numbers) may lead to repeating decimals, typically handled by truncation as is the case of IEEE 754 floats. – Wyck Apr 18 '21 at 17:43