0

表达式趋于 0 时(exp(t) - 1)/t收敛到 1。t但是,当进行数值计算时,我们得到了不同的结果:

In [19]: (exp(10**(-12)) - 1) * (10**12)                                        
Out[19]: 1.000088900582341

In [20]: (exp(10**(-13)) - 1) * (10**13)                                        
Out[20]: 0.9992007221626409

In [21]: (exp(10**(-14)) - 1) * (10**14)                                        
Out[21]: 0.9992007221626409

In [22]: (exp(10**(-15)) - 1) * (10**15)                                        
Out[22]: 1.1102230246251565

In [23]: (exp(10**(-16)) - 1) * (10**16)                                        
Out[23]: 0.0

有什么方法可以计算这个表达式而不会遇到这些问题?我曾考虑过使用幂级数,但我对自己实施这一点持谨慎态度,因为我不确定实施细节,例如要使用多少个术语。

如果相关,我将 Python 与 scipy 和 numpy 一起使用。

4

1 回答 1

2

The discussion in the comments about tiny values seems pointless. If t is so tiny that it causes underflow, then the expression is 1 "since a long time". Indeed the Taylor development is

1 + t/2 + t²/6 + t³/24...

and as soon as t < 1 ulp, the floating-point representation is exactly 1.

Above that, expm1(t)/t will do a good job.

于 2021-03-18T10:06:50.490 回答