@@ -189,15 +189,16 @@ end
189
189
190
190
# isapprox: approximate equality of numbers
191
191
"""
192
- isapprox(x, y; rtol::Real=sqrt( eps) , atol::Real=0, nans::Bool=false, norm::Function)
192
+ isapprox(x, y; rtol::Real=atol>0 ? √ eps : 0 , atol::Real=0, nans::Bool=false, norm::Function)
193
193
194
- Inexact equality comparison: `true` if `norm(x-y) <= atol + rtol*max(norm(x), norm(y))`. The
194
+ Inexact equality comparison: `true` if `norm(x-y) <= max( atol, rtol*max(norm(x), norm(y) ))`. The
195
195
default `atol` is zero and the default `rtol` depends on the types of `x` and `y`. The keyword
196
196
argument `nans` determines whether or not NaN values are considered equal (defaults to false).
197
197
198
- For real or complex floating-point values, `rtol` defaults to
199
- `sqrt(eps(typeof(real(x-y))))`. This corresponds to requiring equality of about half of the
200
- significand digits. For other types, `rtol` defaults to zero.
198
+ For real or complex floating-point values, if an `atol > 0` is not specified, `rtol` defaults to
199
+ the square root of [`eps`](@ref) of the type of `x` or `y`, whichever is bigger (least precise).
200
+ This corresponds to requiring equality of about half of the significand digits. Otherwise,
201
+ e.g. for integer arguments or if an `atol > 0` is supplied, `rtol` defaults to zero.
201
202
202
203
`x` and `y` may also be arrays of numbers, in which case `norm` defaults to `vecnorm` but
203
204
may be changed by passing a `norm::Function` keyword argument. (For numbers, `norm` is the
@@ -220,8 +221,8 @@ julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0])
220
221
true
221
222
```
222
223
"""
223
- function isapprox (x:: Number , y:: Number ; rtol:: Real = rtoldefault (x,y), atol:: Real = 0 , nans:: Bool = false )
224
- x == y || (isfinite (x) && isfinite (y) && abs (x- y) <= atol + rtol* max (abs (x), abs (y))) || (nans && isnan (x) && isnan (y))
224
+ function isapprox (x:: Number , y:: Number ; atol :: Real = 0 , rtol:: Real = rtoldefault (x,y, atol) , nans:: Bool = false )
225
+ x == y || (isfinite (x) && isfinite (y) && abs (x- y) <= max ( atol, rtol* max (abs (x), abs (y) ))) || (nans && isnan (x) && isnan (y))
225
226
end
226
227
227
228
const ≈ = isapprox
@@ -230,7 +231,10 @@ const ≈ = isapprox
230
231
# default tolerance arguments
231
232
rtoldefault (:: Type{T} ) where {T<: AbstractFloat } = sqrt (eps (T))
232
233
rtoldefault (:: Type{<:Real} ) = 0
233
- rtoldefault (x:: Union{T,Type{T}} , y:: Union{S,Type{S}} ) where {T<: Number ,S<: Number } = max (rtoldefault (real (T)),rtoldefault (real (S)))
234
+ function rtoldefault (x:: Union{T,Type{T}} , y:: Union{S,Type{S}} , atol:: Real ) where {T<: Number ,S<: Number }
235
+ rtol = max (rtoldefault (real (T)),rtoldefault (real (S)))
236
+ return atol > 0 ? zero (rtol) : rtol
237
+ end
234
238
235
239
# fused multiply-add
236
240
fma_libm (x:: Float32 , y:: Float32 , z:: Float32 ) =
0 commit comments