diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 2bca2989bab0b..1b3a088524c8e 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -225,9 +225,11 @@ end """ isapprox(x, y; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps, nans::Bool=false[, norm::Function]) -Inexact equality comparison: `true` if `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`. The -default `atol` is zero and the default `rtol` depends on the types of `x` and `y`. The keyword -argument `nans` determines whether or not NaN values are considered equal (defaults to false). +Inexact equality comparison. Two numbers compare equal if their relative distance *or* their +absolute distance is within tolerance bounds: `isapprox` returns `true` if +`norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`. The default `atol` is zero and the +default `rtol` depends on the types of `x` and `y`. The keyword argument `nans` determines +whether or not NaN values are considered equal (defaults to false). For real or complex floating-point values, if an `atol > 0` is not specified, `rtol` defaults to the square root of [`eps`](@ref) of the type of `x` or `y`, whichever is bigger (least precise). @@ -259,13 +261,16 @@ but an absurdly large tolerance if `x` is the # Examples ```jldoctest -julia> 0.1 ≈ (0.1 - 1e-10) +julia> isapprox(0.1, 0.15; atol=0.05) true -julia> isapprox(10, 11; atol = 2) +julia> isapprox(0.1, 0.15; rtol=0.34) true -julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0]) +julia> isapprox(0.1, 0.15; rtol=0.33) +false + +julia> 0.1 + 1e-10 ≈ 0.1 true julia> 1e-10 ≈ 0 @@ -273,6 +278,9 @@ false julia> isapprox(1e-10, 0, atol=1e-8) true + +julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0]) # using `norm` +true ``` """ function isapprox(x::Number, y::Number;