How can I get accuracy up to 128 decimal places in C #?

I tried BigInteger, decimal, floating and long, but no luck. Screenshot with required output example

+4
source share
2 answers

The easiest way to achieve arbitrary precision numbers is to combine a class BigIntegerfrom System.Numericswith a measure int. You could use it BigIntegerfor your exhibitor, but that would probably be redundant, as the numbers would be clearly visible in scale.

So, if you create a class on these lines:

public class ArbDecimal
{
    BigInteger value;
    int exponent;
    public override string ToString()
    {
         StringBuilder sb = new StringBuilder();
         int place;
         foreach (char digit in value.ToString())
         {
             if (place++ == value.ToString().Length - exponent)
             {
                 sb.Append('.');
             }
             sb.Append(digit);
         }
         return sb.ToString();
    }
}

Then you must define your mathematical operations using the laws of indexes with valueand exponent.

, , , , 10^(largerExp-smallerExp), .


0.01 :

value = 1

exponent = -2

, 1*10^-2 = 0.01.


, ( ) , ram .NET.

+1

Source: https://habr.com/ru/post/1675985/


All Articles