Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Scientific calculation - big number with many decimals

Tags:

c#

math

decimal

I have a problem that I need advice: I have to make calculations with big numbers, in the range of (plus/minus, signed); integer part: 70*(10^27) and accuracy, decimal part: 9*(10^-31). Most of the times I only simple simple operations (add/subtr/mult/div) where I could ignore most digits (use only 8 decimals) of the decimal part - however, in many cases, I would have to take the 'whole' decimal and do calculations with that precision (and store the result which is used in subsequent calculations). An example of a number:

66898832014839425790021345548 . 8499970865478385639546957014538

I saw the articles on decimal vs long etc. Should I use a decimal or should a custom type be made? If yes on the later, how may I do simple arithmetic operations? (Roundation of the last decimal only is acceptable) My projects are all in C# and SQL Server; thank you very much in advance.

like image 449
Nick Avatar asked Nov 30 '25 20:11

Nick


1 Answers

There is no standard implementation in C#, but you can create your own library based on BigInteger like

public BigDecimal(BigInteger integer, BigInteger scale)

You can also reference some 3rd-party libraries like GMP with its .Net forks/ports like Math.Gmp.Native.NET, libgmp, etc.

There are some custom libs, as Franz Gleichmann already mentioned in his comment: BigDecimal, AngouriMath

For SQL Server most of the libraries use strings to store such kind of data. For instance, in Java there is BigDecimal and it is mapped to string via JDBC.

like image 88
Boris Sokolov Avatar answered Dec 03 '25 09:12

Boris Sokolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!