Skip to content

Commit

Permalink
Add single argument version of isapprox (#32305)
Browse files Browse the repository at this point in the history
Adds a single argument version of isapprox or ≈, in the same vein as the single argument version of `==`.
  • Loading branch information
asinghvi17 authored Jan 31, 2020
1 parent 51f1710 commit ab5b40a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ New library features
--------------------
* Function composition now works also on one argument `∘(f) = f` (#34251)

* `isapprox` (or ``) now has a one-argument "curried" method `isapprox(x)` which returns a function, like `isequal` (or `==`)` ([#32305]).
* `Ref{NTuple{N,T}}` can be passed to `Ptr{T}`/`Ref{T}` `ccall` signatures ([#34199])


Expand Down
9 changes: 9 additions & 0 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ function isapprox(x::Number, y::Number; atol::Real=0, rtol::Real=rtoldefault(x,y
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= max(atol, rtol*max(abs(x), abs(y)))) || (nans && isnan(x) && isnan(y))
end

"""
isapprox(x; kwargs...) / ≈(x; kwargs...)
Create a function that compares its argument to `x` using `≈`, i.e. a function equivalent to `y -> y ≈ x`.
The keyword arguments supported here are the same as those in the 2-argument `isapprox`.
"""
isapprox(y; kwargs...) = x -> isapprox(x, y; kwargs...)

const = isapprox
"""
x ≉ y
Expand Down
6 changes: 6 additions & 0 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ end
@test round(Float16(0.6), sigdigits=2) === Float16(0.6)
@test round(Float16(1.1), sigdigits=70) === Float16(1.1)
end

@testset "curried approximation" begin

@test (1.0; atol=1).(1.0:3.0) == [true, true, false]

end

0 comments on commit ab5b40a

Please sign in to comment.