为什么
0.1 * 3 ≈ 0.3
的结果是true,而
1e-300 ≈ 0.0
的结果是false?
作为对比,以下结果为true:
isapprox(1e-9, 0.0; atol = 1e-8)
那么,≈ 所默认的机器零精度为多少?如何设置?
为什么
0.1 * 3 ≈ 0.3
的结果是true,而
1e-300 ≈ 0.0
的结果是false?
作为对比,以下结果为true:
isapprox(1e-9, 0.0; atol = 1e-8)
那么,≈ 所默认的机器零精度为多少?如何设置?
既然你知道了 ≈
等价于isapprox
,那么问题的答案就在这个函数的文档里(?≈
):
Note that x ≈ 0 (i.e., comparing to zero with the default tolerances) is equivalent to x == 0 since the default atol
is 0. In such cases, you should either supply an appropriate atol (or use norm(x) ≤ atol) or rearrange your code
(e.g. use x ≈ y rather than x - y ≈ 0). It is not possible to pick a nonzero atol automatically because it depends
on the overall scaling (the "units") of your problem: for example, in x - y ≈ 0, atol=1e-9 is an absurdly small
tolerance if x is the radius of the Earth (https://en.wikipedia.org/wiki/Earth_radius) in meters, but an absurdly
large tolerance if x is the radius of a Hydrogen atom (https://en.wikipedia.org/wiki/Bohr_radius) in meters.