有没有在C#中BigFloat类? [英] Is there a BigFloat class in C#?

查看:195
本文介绍了有没有在C#中BigFloat类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Numerics.BigInteger 让你乘大整数在一起,但有相同类型的浮点数的东西吗?如果没有,有没有免费的图书馆,我可以使用?

  //这个但花车
System.Numerics.BigInteger MAXINT =新的BigInteger(int.MaxValue);System.Numerics.BigInteger大= MAXINT * MAXINT * MAXINT;
的System.Console.WriteLine(大);


解决方案

也许你正在寻找的 BigRational ?微软发布它codePLEX他们BCL项目中。实际上并不知道如何或是否会满足您的需求。

它保持它作为一个理性的数字。你可以得到一个字符串的十进制值或者通过铸造或某些乘法。

 变种R =新BigRational(5000,3768);
Console.WriteLine((十进制)R);
Console.WriteLine((双)R);

或者用一个简单的(ISH)扩展这样的方式:

 公共静态类BigRationalExtensions
{
    公共静态字符串ToDecimalString(这BigRational R,INT precision)
    {
        变种分数= r.GetFractionPart();        //案例其中有理数是一个整数
        如果(fraction.Numerator == 0安培;&安培; fraction.Denominator == 1)
        {
            返回r.GetWholePart()+.0;
        }        VAR adjustedNumerator =(fraction.Numerator
                                           * BigInteger.Pow(10,precision));
        VAR小数位数= adjustedNumerator / fraction.Denominator;        //情况precision是不是足够大。
        如果(小数位数== 0)
        {
            回到0.0;
        }        //给它周边的我们应该需要什么能力
        //整数部分和总precision
        //(这是有点草率,但确实的伎俩)
        VAR SB =新的StringBuilder(precision + r.ToString()长。);        布尔noMoreTrailingZeros = FALSE;
        的for(int i = precision; I> 0;我 - )
        {
            如果(!noMoreTrailingZeros)
            {
                如果((小数位数%10)== 0)
                {
                    小数位数=小数位数/ 10;
                    继续;
                }                noMoreTrailingZeros = TRUE;
            }            //最右边的小数添加到字符串
            sb.Insert(0,小数位数%10);
            小数位数=小数位数/ 10;
        }        //将整个零​​件和小数
        sb.Insert(0,。);
        sb.Insert(0,r.GetWholePart());        返回sb.ToString();
    }
}

如果它出了小数或双的precision范围,它们将被转换为各自类型的值0.0。此外,铸造为十进制,当结果是它的范围之外,将导致发生OverflowException 抛出。

扩展方法我写的(这可能不是的的方法来计算分数的小数重presentation)将准确地将其转换为一个字符串,具有无限precision。然而,如果数目比所请求的precision较小,它将返回0.0,就像十进制或双会

System.Numerics.BigInteger lets you multiply large integers together, but is there anything of the same type for floating point numbers? If not, is there a free library I can use?

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

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

解决方案

Perhaps you're looking for BigRational? Microsoft released it under their BCL project on CodePlex. Not actually sure how or if it will fit your needs.

It keeps it as a rational number. You can get the a string with the decimal value either by casting or some multiplication.

var r = new BigRational(5000, 3768);
Console.WriteLine((decimal)r);
Console.WriteLine((double)r);

Or with a simple(ish) extension method like this:

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();
    }
}

If it's out of the precision range of a decimal or double, they will be cast to their respective types with a value of 0.0. Also, casting to decimal, when the result is outside of its range, will cause an OverflowException to be thrown.

The extension method I wrote (which may not be the best way to calculate a fraction's decimal representation) will accurately convert it to a string, with unlimited precision. However, if the number is smaller than the precision requested, it will return 0.0, just like decimal or double would.

这篇关于有没有在C#中BigFloat类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆