Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中是否有一个大float类?_C#_Numeric_Bigfloat - Fatal编程技术网

C#中是否有一个大float类?

C#中是否有一个大float类?,c#,numeric,bigfloat,C#,Numeric,Bigfloat,System.Numerics.BigInteger允许将大整数相乘,但浮点数有相同类型的吗?如果没有,我可以使用免费的图书馆吗 //this but with floats System.Numerics.BigInteger maxint = new BigInteger(int.MaxValue); System.Numerics.BigInteger big = maxint * maxint * maxint; System.Console.WriteLine(big); 也许你在

System.Numerics.BigInteger
允许将大整数相乘,但浮点数有相同类型的吗?如果没有,我可以使用免费的图书馆吗

//this but with floats
System.Numerics.BigInteger maxint = new BigInteger(int.MaxValue);

System.Numerics.BigInteger big = maxint * maxint * maxint;
System.Console.WriteLine(big);

也许你在找什么?微软在CodePlex的BCL项目下发布了它。实际上不确定它如何或是否能满足您的需求

它保持它为有理数。您可以通过强制转换或一些乘法来获得带十进制值的字符串

var r = new BigRational(5000, 3768);
Console.WriteLine((decimal)r);
Console.WriteLine((double)r);
或者使用以下简单(ish)扩展方法:

public static class BigRationalExtensions
{
    public static string ToDecimalString(this BigRational r, int precision)
    {
        var fraction = r.GetFractionPart();

        // Case where the rational number is a whole number
        if(fraction.Numerator == 0 && fraction.Denominator == 1)
        {
            return r.GetWholePart() + ".0";
        }

        var adjustedNumerator = (fraction.Numerator
                                           * BigInteger.Pow(10, precision));
        var decimalPlaces = adjustedNumerator / fraction.Denominator;

        // Case where precision wasn't large enough.
        if(decimalPlaces == 0)
        {
            return "0.0";
        }

        // Give it the capacity for around what we should need for 
        // the whole part and total precision
        // (this is kinda sloppy, but does the trick)
        var sb = new StringBuilder(precision + r.ToString().Length);

        bool noMoreTrailingZeros = false;
        for (int i = precision; i > 0; i--)
        {
            if(!noMoreTrailingZeros)
            {
                if ((decimalPlaces%10) == 0)
                {
                    decimalPlaces = decimalPlaces/10;
                    continue;
                }

                noMoreTrailingZeros = true;
            }

            // Add the right most decimal to the string
            sb.Insert(0, decimalPlaces%10);
            decimalPlaces = decimalPlaces/10;
        }

        // Insert the whole part and decimal
        sb.Insert(0, ".");
        sb.Insert(0, r.GetWholePart());

        return sb.ToString();
    }
}
如果它超出了十进制或双精度范围,它们将被转换为各自的类型,值为0.0。此外,当结果超出其范围时,强制转换为十进制将导致抛出
OverflowException


我编写的扩展方法(这可能不是计算分数十进制表示的最佳方法)将精确地将其转换为字符串,精度不受限制。但是,如果该数字小于要求的精度,它将返回0.0,就像十进制或双精度一样。

应该考虑如果存在
BigFloat
类型,会产生什么影响

BigFloat x = 1.0;
BigFloat y = 3.0;
BigFloat z = x / y;
答案是
0.333333333333333333333
recurtive。永远。无限的。内存不足错误

构造无限大的
BigFloat
很容易

但是,如果您愿意坚持使用有理数,则可以通过将一个整数除以另一个整数来表示,而不是使用
biginger
来构建
bigonal
类型,该类型可以为表示任何实数提供任意精度

BigRational x = 1;
BigRational y = 3;
BigRational z = x / y;
这项工作为我们提供了以下类型:


您只需NuGet
BigRational
就可以找到许多实现,包括一次来自Microsoft的实现。

您确定您没有寻找一个
BigRat
?不,也没有标准的
BigDecimal
:((但是他们被
复杂的
覆盖了,耶!)@RandolphWest Double与
int
有相同的限制,尽管是一个更高的数字。@Cameron什么是BigRat?有链接吗?可能是重复的,答案保持在有理形式?你能从中得到一个小数吗?这可能行得通。感谢链接我使用了一些非常大的数字,但你转换它的两种方法都给了我零's@jb-对不起,我花了这么长时间,但我终于弄明白了。我的新扩展方法将把任何大有理数正确地转换为十进制字符串。它比我希望的要复杂一点,我确信有一种更快或更简单的方法可以做到这一点。但是,我不是数学家,所以我认为这样做是安全的假设我不知道我在做什么。希望这有帮助。为了避免可能的OOM,似乎可以“懒散计算”结果,这样它只会计算所需的足够数字。