简体   繁体   English

如何在C#中将精度提高到128位小数?

[英]How can i get precision up-to 128 decimal places in C#?

I have tried BigInteger, decimal, float and long but no luck. 我试过BigInteger,十进制,浮点数和长但没有运气。 Screenshot of required output example 所需输出示例的屏幕截图

It is a fairly easy task to write your own rational class; 编写自己的理性类是一项相当容易的任务; remember, rationals are just pairs of integers, and you already have BigInteger. 记住,有理数只是整数对,你已经有了BigInteger。

In this series of articles I show how to devise your own big integer and big rational classes starting from absolutely nothing, not even integers . 在这一系列文章中,我展示了如何设计自己的大整数和大理性类,从绝对没有, 甚至整数 Note that this is not fast and not intended to be fast; 请注意,这并不快,而且不是很快; it is intended to be educational. 它旨在教育。 You can use the techniques I describe in this series to help you when designing your arithmetic class. 您可以使用我在本系列中描述的技术来帮助您设计算术类。

https://ericlippert.com/2013/09/16/math-from-scratch-part-one/ https://ericlippert.com/2013/09/16/math-from-scratch-part-one/

Or, if you don't want to write it yourself, you can always use the one from Microsoft: 或者,如果您不想自己编写,可以使用Microsoft提供的那个:

http://bcl.codeplex.com/wikipage?title=BigRational&referringTitle=Home http://bcl.codeplex.com/wikipage?title=BigRational&referringTitle=Home

But that said... 但那说......

I need a minimum of 128 decimal places to calculate precise probabilities of events between different time steps 我需要至少128个小数位来计算不同时间步之间事件的精确概率

Do you need 128 decimal places to represent 128 digits of precision , or of magnitude ? 您是否需要128个小数位来表示128位精度数量 Because if it is just magnitude, then simply do a transformation of your probability math into logarithms and do the math in doubles. 因为如果它只是幅度,那么只需将概率数学转换为对数并在双精度数学中进行。

The easiest way to achieve arbitrary precision numbers is to combine the BigInteger class from System.Numerics with an int exponent. 实现任意精度数的最简单方法是将System.NumericsBigInteger类与int指数组合。 You could use BigInteger for your exponent, but this is likely overkill as the numbers would be well beyong meaningful in scale. 你可以使用BigInteger作为你的指数,但这可能是过度的,因为这些数字在规模上有很大的意义。

So if you create a class along 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();
    }
}

You should then be able to define your mathematical operations using the laws of indices with the value and exponent fields. 然后,您应该能够使用带有valueexponent字段的索引定律来定义数学运算。

For instance, to achieve addition, you would scale the larger value to have the same exponent as the smaller one by multiplying it by 10^(largerExp-smallerExp) then adding the two values and rescaling. 例如,要实现加法,您可以将较大的值缩放为具有与较小值相同的指数,方法是将其乘以10^(largerExp-smallerExp)然后将两个值相加并重新缩放。


In your class, the number 0.01 would be represented like: 在您的班级中,数字0.01将表示为:

value = 1

exponent = -2

Due to the fact that 1*10^-2 = 0.01 . 由于1*10^-2 = 0.01的事实。


Utilising this method, you can store arbitrarily precise (and large) numbers limited only by the available ram and the .NET framework's object size limit. 利用这种方法,您可以存储任意精确(和大)数字,仅限于可用的ram和.NET框架的对象大小限制。

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

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