数值计算 的bug:(理论)数学上等价,实际运行未必等价

1. 计算表达式的值(lambda 表达式)

fun1 和 fun2 理论上是等价的;同样的输入情形下,两种输出结果不一致。

# fun1 定义
fun1=lambda x:sqrt(x+1)-sqrt(x)   

# fun2 定义
fun2= lambda x:1/(sqrt(x+1)+sqrt(x))

2. 运行示例


>>> fun1=lambda x:sqrt(x+1)-sqrt(x)           # lambda 表达式定义

>>> fun2= lambda x:1/(sqrt(x+1)+sqrt(x))

## 测试
>>> x=1e16              ## >>> x     --》 1e+16

>>> fun1(x)
0.0

>>> fun2(x)
5.0000000000000001e-09

posted on 2018-09-09 20:51  小林觉  阅读(410)  评论(0编辑  收藏  举报

导航