# How can I redefine the operator "≈" to my desired tolerance

**URL:** https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072
**Category:** New to Julia
**Tags:** approx
**Created:** [April 27, 2021, 2:13am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072 "2021-04-27T02:13:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [April 27, 2021, 2:13am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/1 "2021-04-27T02:13:20Z")

</div>

[Currently](https://docs.julialang.org/en/v1/base/math/), “The binary operator ≈ is equivalent to isapprox with the default arguments”  
Hence: `x ≈ y` is equivalent to `isapprox(x,y)`

How can I redefine the operator w/ my own custom tolerance:  
such that: `x ≈ y` is equivalent to `isapprox(x,y,atol=.01)`

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [April 27, 2021, 2:35am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/2 "2021-04-27T02:35:38Z")

</div>

```julia
ε = .1; 
≊(a,b;ε=ε) = isapprox(a,b;atol=ε)

julia> 4.9 ≊ 5.0
true

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 27, 2021, 6:26am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/3 "2021-04-27T06:26:31Z")

</div>

`≈` is just a function in Base, so you can do

```julia
julia> import Base.≈

julia> ≈(a::Float64, b::Float64) = isapprox(a, b, atol = 0.1)
isapprox (generic function with 11 methods)

julia> 4.9 ≈ 5.0
true

```

but I would be careful about redefining how basic functionality like this works, at a minimum it makes it harder for others to understand your code. Choosing your own alternative Unicode identifier as you do above is preferable in my opinion, in these situations where I modify a defined infix operator I often add suffixes to indicate what the modification is doing, e.g. in this case maybe `≈₀₁`

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 27, 2021, 6:34am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/4 "2021-04-27T06:34:59Z")

</div>

This looks like a decent application for a macro:

```julia
function _approx!(ex::Expr, kwargs)
    _approx!.(ex.args, Ref(kwargs))
    if ex.head == :call && ex.args[1] == :≈
        append!(ex.args, kwargs)
    end
end

_approx!(x, kwargs) = nothing

macro approx(args...)
    kwargs = [Expr(:kw, arg.args...) for arg in args[1:end-1]]
    ex = args[end]
    _approx!(ex, kwargs)
    return ex
end

```

With this you can do:

```julia
julia> @approx atol=0.1 rtol=0.01 begin
           @show 1 ≈ 1.05
           @show 1 ≈ 2
       end
≈(1, 1.05, atol = 0.1, rtol = 0.01) = true
≈(1, 2, atol = 0.1, rtol = 0.01) = false

```

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [April 27, 2021, 8:22am UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/5 "2021-04-27T08:22:48Z")

</div>

This is nice because it is consistent with the behavior in Test.jl:

```julia
julia> using Test

julia> @test 0.5 ≈ 0.6 atol=0.1
Test Passed

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 28, 2021, 12:51pm UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/6 "2021-04-28T12:51:04Z")

</div>

Note that there are tons of unassigned operators, so you can do something like

```julia
≅(a, b) = isapprox(a, b; atol = 0.1)

```

and not have to worry about type piracy.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 28, 2021, 1:16pm UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/7 "2021-04-28T13:16:34Z")

</div>

You can always just redefine `≈` within your module:

```julia
≈(x,y) = isapprox(x,y,atol=0.01)

```

This doesn’t affect `Base.≈` and is _not_ type-piracy… it is defining a new module-local function of the same name. Of course, this could be confusing for someone reading your code, so it might be clearer to give it a different name like ` ≈₂` (which is a valid operator name in Julia).

But if you just want to change the tolerance in some `@test` calls, you can also use `@test x ≈ y atol=0.01` to pass keyword arguments to `≈`.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [April 28, 2021, 1:26pm UTC](https://discourse.julialang.org/t/how-can-i-redefine-the-operator-to-my-desired-tolerance/60072/8 "2021-04-28T13:26:35Z")

</div>

Another option if you really want to use `≈`, is to define it in a local scope just around where you are using it.

```julia
julia> let ≈(a,b;ε=0.1) = isapprox(a,b;atol=ε)
           4.9 ≈ 5.0
       end
true

```

This may be clearer than defining it at the module level.
