【发布时间】:2016-02-23 00:15:52
【问题描述】:
这是一道数学题,但要尝试解决 C# 编程问题,所以我不确定这里是否是最好的地方。
在下面的代码中,我可以得到导数:
var x = 13.399E+153;
var d = ((1 + x) * (1 - x));
Console.WriteLine("d = {0:0}", d);
Console.ReadLine();
其中 13.399E+153; 9 重复出现。但是如果 x 变成:
var x = 14.00E+153;
我得到-Infinity。我已经做了一些研究,但不了解任何可能的解决方案。
网址:Mathematical function differentiation with C#? 还有:Limit of the derivative of a function as x goes to infinity 我确实理解为什么会发生这种情况:
If the limit of f(x) f(x) exists, there is a horizontal asymptote.
Therefore as the function approaches infinity it becomes more linear and
...thus the derivative approaches zero.
.
我的问题是,例如,如果我将 Derivative 作为双精度返回,有什么解决方案可以防止它作为 Infinity 返回?我应该返回 1 还是零?
if (double.IsInfinity(Derivative))
{
return ?;
}
【问题讨论】:
-
我应该返回 1 还是返回 0?它可以是 1、0 或 -1 或其他任何值。由您来处理返回值。您也可以从方法调用中返回
double.IsInfinity(Derivative)和out的导数。 -
也许使用
BigRational而不仅仅是double。但是有人想知道您愿意采用哪个指数? -
42 是答案...我不知道您实际上希望通过返回一些有限值来实现什么。如果你真的需要,你必须在计算中明确支持“无穷大”的概念。