# Set default tolerance for isapprox()

**URL:** <https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337>\
**Category:** General Usage\
**Tags:** multidispatch\
**Created:** [January 10, 2022, 1:47pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337 "2022-01-10T13:47:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 10, 2022, 1:47pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/1 "2022-01-10T13:47:49Z")

</div>

How can I define `isapprox()` for a type such that I can set a default tolerance but the user can override it if required?

```julia
struct MyType
	 v::Float64
end

Base.isapprox(x::MyType, y::MyType) = isapprox(x.v, y.v, atol=10e-6)

x = MyType(10.0 / 3)
y = MyType(3.33)

@show isapprox(x, y) # usually false with 10e-6

@show isapprox(x, y, atol = 10e-12) # can be true if required

```

I think it’s something with `kwargs` ? but I forget how it’s done.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 10, 2022, 1:50pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/2 "2022-01-10T13:50:39Z")

</div>

Your implementation can pass along all keywords like this:

```julia
Base.isapprox(x::MyType, y::MyType; kwargs...) = isapprox(x.v, y.v; kwargs...)

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 10, 2022, 1:52pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/3 "2022-01-10T13:52:33Z")

</div>

Thanks - where do I put the default `10e-6` though?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 10, 2022, 1:58pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/4 "2022-01-10T13:58:17Z")

</div>

Ah, I misunderstood. You can capture a specific kwarg like this for example:

```julia
function Base.isapprox(x::MyType, y::MyType; atol=nothing, kwargs...)
    if atol === nothing
        atol = 1e-6
    end
    return isapprox(x.v, y.v; atol=atol, kwargs...)
end

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 10, 2022, 2:00pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/5 "2022-01-10T14:00:04Z")

</div>

Ah, great, thanks! I get confused with the `kwargs` stuff.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 10, 2022, 2:32pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/6 "2022-01-10T14:32:35Z")

</div>

> [@fredrikekre](#):
>
> ```julia
> function Base.isapprox(x::MyType, y::MyType; atol=nothing, kwargs...)
> if atol === nothing
> atol = 1e-6
> end
> return isapprox(x.v, y.v; atol=atol, kwargs...)
> end
> 
> ```

Anything wrong with the following?

```julia
function Base.isapprox(x::MyType, y::MyType; atol=1e-6, kwargs...)
    return isapprox(x.v, y.v; atol=atol, kwargs...)
end

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 10, 2022, 2:37pm UTC](https://discourse.julialang.org/t/set-default-tolerance-for-isapprox/74337/7 "2022-01-10T14:37:16Z")

</div>

No not in this simple case. I was thinking of the case when you need to differentiate between “user has passed `atol`” and “user has not passed `atol`”.
