isapprox
isapprox(x, y; rtol::Real=sqrt(eps), atol::Real=0)
Inexact equality comparison: true if norm(x-y) <= atol + rtol*max(norm(x), norm(y)). The default atol is zero and the default rtol depends on the types of x and y.
For real or complex floating-point values, rtol defaults to sqrt(eps(typeof(real(x-y)))). This corresponds to requiring equality of about half of the significand digits. For other types, rtol defaults to zero.
x and y may also be arrays of numbers, in which case norm defaults to vecnorm but may be changed by passing a norm::Function keyword argument. (For numbers, norm is the same thing as abs.)
The binary operator ≈ is equivalent to isapprox with the default arguments, and x ≉ y is equivalent to !isapprox(x,y).
Examples
jldoctest
julia> isapprox(0.1 + 0.2, 0.3)
true
In this example, isapprox is used to compare the sum of two floating-point numbers with the value 0.3. It returns true because the difference between the sum and 0.3 is within the default tolerance.
jldoctest
julia> isapprox(0.1 + 0.2, 0.3, rtol=1e-2, atol=1e-3)
false
Here, isapprox is used with custom relative and absolute tolerances. Since the difference between the sum and 0.3 exceeds the specified tolerance, it returns false.
jldoctest
julia> isapprox([1.0, 2.0, 3.0], [1.0, 2.01, 2.99])
true
In this case, isapprox is used to compare arrays of numbers. It returns true because the element-wise differences between the arrays are within the default tolerance.
jldoctest
julia> isapprox([1.0, 2.0, 3.0], [1.0, 2.01, 2.99], atol=1e-2)
false
Here, the absolute tolerance is customized to 1e-2. Since one of the element-wise differences exceeds this tolerance, isapprox returns false.
The binary operator ≈ can also be used as a shorthand for isapprox with the default arguments.
jldoctest
julia> 0.1 + 0.2 ≈ 0.3
true
Similarly, ≉ can be used as a shorthand for !isapprox.
jldoctest
julia> 0.1 + 0.2 ≉ 0.3
falseSee Also
cmp, float, get_bigfloat_precision, get_rounding, get_zero_subnormals, isapprox, maxintfloat, mod2pi, nextfloat, precision, prevfloat, rationalize, round, set_bigfloat_precision, set_rounding, set_zero_subnormals, significand, with_bigfloat_precision, with_rounding,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.