简体   繁体   English

C# 科学计算 - 带有许多小数的大数字

[英]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) . integer 部分: 70*(10^27)和精度,小数部分: 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).大多数时候我只做简单的简单操作(加/减/多/格),我可以忽略小数部分的大多数数字(只使用 8 位小数)——但是,在很多情况下,我必须采用“整体”十进制并以该精度进行计算(并存储用于后续计算的结果)。 An example of a number:一个数字的例子:

66898832014839425790021345548 . 66898832014839425790021345548 8499970865478385639546957014538 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; (只接受最后一位小数的四舍五入)我的项目都在 C# 和 SQL 服务器中; thank you very much in advance.非常感谢你。

There is no standard implementation in C#, but you can create your own library based on BigInteger like C# 中没有标准实现,但是您可以创建自己的基于BigInteger的库,例如

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.您还可以使用其.Net 分支/端口(如Math.Gmp.Native.NETlibgmp等)引用一些 3rd 方库(如GMP )。

There are some custom libs, as Franz Gleichmann already mentioned in his comment: BigDecimal , AngouriMath有一些自定义库,正如 Franz Gleichmann 在他的评论中已经提到的那样: BigDecimalAngouriMath

For SQL Server most of the libraries use strings to store such kind of data.对于 SQL 服务器,大多数库使用字符串来存储此类数据。 For instance, in Java there is BigDecimal and it is mapped to string via JDBC .例如,在 Java 中有BigDecimal ,它通过JDBC映射到字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM