Skip to content

Commit 160a467

Browse files
authored
more discussion of x ≈ 0 in isapprox docs (#26029)
* more discussion of x ≈ 0 in isapprox docs * tweak and typo * clarification * update in response to comments * reduce example tolerance
1 parent 12964e8 commit 160a467

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

base/floatfuncs.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ approximately equal component-wise.
214214
The binary operator `≈` is equivalent to `isapprox` with the default arguments, and `x ≉ y`
215215
is equivalent to `!isapprox(x,y)`.
216216
217+
Note that `x ≈ 0` (i.e., comparing to zero with the default tolerances) is
218+
equivalent to `x == 0` since the default `atol` is `0`. In such cases, you should either
219+
supply an appropriate `atol` (or use `norm(x) ≤ atol`) or rearrange your code (e.g.
220+
use `x ≈ y` rather than `x - y ≈ 0`). It is not possible to pick a nonzero `atol`
221+
automatically because it depends on the overall scaling (the "units") of your problem:
222+
for example, in `x - y ≈ 0`, `atol=1e-9` is an absurdly small tolerance if `x` is the
223+
[radius of the Earth](https://en.wikipedia.org/wiki/Earth_radius) in meters,
224+
but an absurdly large tolerance if `x` is the
225+
[radius of a Hydrogen atom](https://en.wikipedia.org/wiki/Bohr_radius) in meters.
226+
227+
217228
# Examples
218229
```jldoctest
219230
julia> 0.1 ≈ (0.1 - 1e-10)
@@ -224,6 +235,12 @@ true
224235
225236
julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0])
226237
true
238+
239+
julia> 1e-10 ≈ 0
240+
false
241+
242+
julia> isapprox(1e-10, 0, atol=1e-8)
243+
true
227244
```
228245
"""
229246
function isapprox(x::Number, y::Number; atol::Real=0, rtol::Real=rtoldefault(x,y,atol), nans::Bool=false)

0 commit comments

Comments
 (0)