# Checking if all the array's elements are positive

**URL:** <https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206>\
**Category:** General Usage\
**Created:** [February 5, 2020, 12:59pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206 "2020-02-05T12:59:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)\
**Post date:** [February 5, 2020, 12:59pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/1 "2020-02-05T12:59:02Z")

</div>

in the usual case i can make sure if all the array’s elements are positive using the `nonneg(x)`now it’s not working for me everytime i try to use it, an error raises with `nonneg is not defined` is it deprecated or what?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 5, 2020, 1:13pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/2 "2020-02-05T13:13:55Z")

</div>

`all(>=(0), x) `, requires Julia 1.2 or 1.3

---

<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:** [February 5, 2020, 1:29pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/3 "2020-02-05T13:29:48Z")

</div>

^ is right, if you prefer `nonneg`

```julia
nonneg(x::Real} = x >= zero(T)
v = [1, 2, 3]
all(nonneg, v)

```

(edited to reflect the comments that follow)

---

<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:** [February 5, 2020, 1:44pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/4 "2020-02-05T13:44:26Z")

</div>

Or just

```julia
nonneg(x) = x >= zero(x)

```

---

<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:** [February 5, 2020, 1:50pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/5 "2020-02-05T13:50:58Z")

</div>

(I just prefer the more specific error message for nonreal types (say, String))

---

<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:** [February 5, 2020, 1:52pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/6 "2020-02-05T13:52:48Z")

</div>

I missed the `<:Real` and thought they were identical. So then

```julia
nonneg(x::Real) = x >= zero(x)

```

I prefer to avoid type parameters unless they make the code simpler or more readable.

---

<div class="post-metadata">

**Author:** ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)\
**Post date:** [February 5, 2020, 2:10pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/7 "2020-02-05T14:10:49Z")

</div>

me too i try as much as i can to stay away from type parameters.

---

<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:** [February 5, 2020, 2:18pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/8 "2020-02-05T14:18:33Z")

</div>

_well, you all are right to reserve type parameters for where they should be_  
I have edited my original response accordingly – with attribution.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 5, 2020, 3:08pm UTC](https://discourse.julialang.org/t/checking-if-all-the-arrays-elements-are-positive/34206/9 "2020-02-05T15:08:53Z")

</div>

ah, I got an surprise (or not really since the second one is making a Boolean array first

```julia
julia> @btime all(>=(0), $x)
  458.635 ns (0 allocations: 0 bytes)
true

julia> @btime all($x .> 0)
  995.900 ns (3 allocations: 4.42 KiB)
true

```
