Skip to content

Commit ab5b40a

Browse files
authored
Add single argument version of isapprox (#32305)
Adds a single argument version of isapprox or ≈, in the same vein as the single argument version of `==`.
1 parent 51f1710 commit ab5b40a

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ New library features
5353
--------------------
5454
* Function composition now works also on one argument `∘(f) = f` (#34251)
5555

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

5859

base/floatfuncs.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ function isapprox(x::Number, y::Number; atol::Real=0, rtol::Real=rtoldefault(x,y
274274
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= max(atol, rtol*max(abs(x), abs(y)))) || (nans && isnan(x) && isnan(y))
275275
end
276276

277+
"""
278+
isapprox(x; kwargs...) / ≈(x; kwargs...)
279+
280+
Create a function that compares its argument to `x` using `≈`, i.e. a function equivalent to `y -> y ≈ x`.
281+
282+
The keyword arguments supported here are the same as those in the 2-argument `isapprox`.
283+
"""
284+
isapprox(y; kwargs...) = x -> isapprox(x, y; kwargs...)
285+
277286
const = isapprox
278287
"""
279288
x ≉ y

test/floatfuncs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,9 @@ end
100100
@test round(Float16(0.6), sigdigits=2) === Float16(0.6)
101101
@test round(Float16(1.1), sigdigits=70) === Float16(1.1)
102102
end
103+
104+
@testset "curried approximation" begin
105+
106+
@test (1.0; atol=1).(1.0:3.0) == [true, true, false]
107+
108+
end

0 commit comments

Comments
 (0)