Nick
Nick

Reputation: 533

C# Scientific calculation - big number with many decimals

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.

Upvotes: 3

Views: 409

Answers (1)

Boris Sokolov
Boris Sokolov

Reputation: 1793

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.

Upvotes: 4

Related Questions