From 512339ba4b41a8f06134123a309405ea5af716de Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Tue, 10 Jan 2017 14:05:51 -0800 Subject: [PATCH] RFC: Export iszero (#19950) * Export and test Base.iszero --- NEWS.md | 2 ++ base/exports.jl | 1 + doc/src/stdlib/numbers.md | 1 + test/numbers.jl | 16 ++++++++++++++++ 4 files changed, 20 insertions(+) diff --git a/NEWS.md b/NEWS.md index 81db6bd8a8609..4debf14c80e7a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -159,6 +159,8 @@ Library improvements * `logging` can be used to redirect `info`, `warn`, and `error` messages either universally or on a per-module/function basis ([#16213]). + * New `iszero(x)` function to quickly check whether `x` is zero (or is all zeros, for an array) ([#19950]). + Compiler/Runtime improvements ----------------------------- diff --git a/base/exports.jl b/base/exports.jl index a15267a329604..0020e1120482e 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -360,6 +360,7 @@ export isreal, isimag, issubnormal, + iszero, lcm, ldexp, leading_ones, diff --git a/doc/src/stdlib/numbers.md b/doc/src/stdlib/numbers.md index 715a8319f9b18..8a6508d0ae326 100644 --- a/doc/src/stdlib/numbers.md +++ b/doc/src/stdlib/numbers.md @@ -67,6 +67,7 @@ Base.issubnormal Base.isfinite Base.isinf Base.isnan +Base.iszero Base.nextfloat Base.prevfloat Base.isinteger diff --git a/test/numbers.jl b/test/numbers.jl index 75e950d2511f7..f260609b25be3 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2864,3 +2864,19 @@ let types = (Base.BitInteger_types..., BigInt, Bool) end @test !isempty(complex(1,2)) + +@testset "iszero" begin + # Numeric scalars + for T in Iterators.flatten(subtypes.([AbstractFloat, Signed, Unsigned])) + @test iszero(T(0)) + @test iszero(Complex{T}(0)) + end + @test iszero(BigFloat(0)) + @test !iszero(nextfloat(BigFloat(0))) + @test iszero(BigInt(0)) + @test iszero(0//1) + + # Array reduction + @test !iszero([0, 1, 2, 3]) + @test iszero(zeros(Int, 5)) +end