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