对于 1.0/3 != 1/3 ,我使用 Rational 可以准确表示 Microsoft.SolverFoundation.Common 中的 1/3 。请参阅:https ://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.common .rational%28v=vs.93%29.aspx?f=255&MSPPError=-2147217396
而且我可以捕获 1/3 的奇数根链接,因为我可以获得分母。
如果我得到一个根是斧头,我使用代码获取分子和分母。
var at = (double)ax.Numerator;
var down = (double)ax.Denominator;
有理可以使2/6=1/3。
但是 Rational.Pow 不能计算 powerBase 不是正数。
我发现 powerBase 不是正数,分母是偶数,分子是奇数。
if (at % 2 == 1 && down % 2 == 0)
{
return Double.NaN;
}
如果分母是奇数,我使用x = x * -1
if (at % 2 == 1 && down % 2 == 1)
{
x = Math.Pow(x, (int)at);
x = x * -1;
return -1 * Math.Pow(x, 1.0 / (int)down);
}
如果 Numerator 是偶数,则 Numerator 的 pow 使得 powerBase 为正。
与 pow(x,2/3) 一样,如果 x 不是正数,则使用 pow(x,2) 时为正数
x = Math.Pow(x, (int)at);
return Math.Pow(x, 1.0 / (int)down);
您可以使用的代码
if (x < 0)
{
var at = (double)ax.Numerator;
var down = (double)ax.Denominator;
if (at % 2 == 1 && down % 2 == 0)
{
return Double.NaN;
}
if (at % 2 == 1 && down % 2 == 1)
{
x = Math.Pow(x, (int)at);
x = x * -1;
return -1 * Math.Pow(x, 1.0 / (int)down);
}
x = Math.Pow(x, (int)at);
return Math.Pow(x, 1.0 / (int)down);
}
else
{
return Math.Pow(x, a);
}