# Is there any preference between \`iszero\` and \`==0\` for simple numbers?

**URL:** <https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410>\
**Category:** New to Julia\
**Created:** [June 8, 2022, 8:19am UTC](https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410 "2022-06-08T08:19:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![numberMX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/numbermx/32/31874_2.png) [@numberMX](https://discourse.julialang.org/u/numberMX)\
**Post date:** [June 8, 2022, 8:19am UTC](https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410/1 "2022-06-08T08:19:54Z")

</div>

I noticed that there are functions like `iszero` that are meant for arrays, but also work on integers. When writing Julia code, is it preferred Julia “style” to use `iszero` for checking whether an integer is equal to zero, or is it better to just use `== 0`?

For example if I was checking if an `Int` is divisible by another `Int`, I could do:

```julia
if n % z == 0

```

or:

```julia
if iszero(n % z)

```

Is there a convention in Julia for which should be used? And, is there any performance difference between the two?

---

<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:** [June 8, 2022, 8:38am UTC](https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410/2 "2022-06-08T08:38:59Z")

</div>

I’d be surprised if there was a performance difference because:

```julia
julia> @which iszero(1)
iszero(x) in Base at number.jl:42

```

and

> <https://github.com/JuliaLang/julia/blob/master/base/number.jl#L42>

The advantage over `== 0` is of course that your code becomes type generic, as `isszero` tests against `zero(x)`, which is not always `0` (i.e. integer zero):

```julia
julia> zero(Complex)
0 + 0im

```

but you could of course do `== zero(x)` instead of `== 0` yourself.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 8, 2022, 9:00am UTC](https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410/3 "2022-06-08T09:00:34Z")

</div>

I use `iszero` to let testing for zero stand out from other comparisons.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 8, 2022, 10:02am UTC](https://discourse.julialang.org/t/is-there-any-preference-between-iszero-and-0-for-simple-numbers/82410/4 "2022-06-08T10:02:56Z")

</div>

I think using `n == 0` is fine when you know for sure that `n` is an ordinary (and perhaps real) number. You know this because it’s an internal variable, or there is a type constraing on an input variable, for example.

`iszero(x)` is better when you don’t have full control over the type of `x`, or if it’s not an ordinary number, otherwise it seems a bit like overkill to me (though a pretty gentle overkill at that.)
