Skip to content

Commit ab497a3

Browse files
authored
added iszero (#305)
1 parent 9e518ff commit ab497a3

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ Currently, the `@compat` macro supports the following syntaxes:
9898
* `transcode` converts between UTF-xx string encodings in Julia 0.5 (as a lightweight
9999
alternative to the LegacyStrings package) ([#17323])
100100

101-
* `` (typically used infix as `f ∘ g`) for function composition can be used in 0.5 and earlier. [#17155](https://github.com/JuliaLang/julia/pull/17155)
101+
* `` (typically used infix as `f ∘ g`) for function composition can be used in 0.5 and earlier ([#17155])
102102

103-
* The method of `!` to negate functions (typically used as a unary operator, as in `!isinteger`) can be used in 0.5 and earlier. [#17155](https://github.com/JuliaLang/julia/pull/17155)
103+
* The method of `!` to negate functions (typically used as a unary operator, as in `!isinteger`) can be used in 0.5 and earlier ([#17155]).
104+
105+
* `iszero(x)` efficiently checks whether `x == zero(x)` (including arrays) can be used in 0.5 and earlier ([#19950]).
104106

105107
## Renamed functions
106108

@@ -266,6 +268,7 @@ includes this fix. Find the minimum version from there.
266268
[#16563]: https://github.com/JuliaLang/julia/issues/16563
267269
[#16603]: https://github.com/JuliaLang/julia/issues/16603
268270
[#16972]: https://github.com/JuliaLang/julia/issues/16972
271+
[#17155]: https://github.com/JuliaLang/julia/issues/17155
269272
[#17302]: https://github.com/JuliaLang/julia/issues/17302
270273
[#17323]: https://github.com/JuliaLang/julia/issues/17323
271274
[#17510]: https://github.com/JuliaLang/julia/issues/17510
@@ -275,3 +278,4 @@ includes this fix. Find the minimum version from there.
275278
[#18977]: https://github.com/JuliaLang/julia/issues/18977
276279
[#19088]: https://github.com/JuliaLang/julia/issues/19088
277280
[#19246]: https://github.com/JuliaLang/julia/issues/19246
281+
[#19950]: https://github.com/JuliaLang/julia/issues/19950

src/Compat.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,14 @@ if !isdefined(Base, :numerator)
17081708
export numerator, denominator
17091709
end
17101710

1711+
# julia #19950
1712+
if !isdefined(Base, :iszero)
1713+
iszero(x) = x == zero(x)
1714+
iszero(x::Number) = x == 0
1715+
iszero(x::AbstractArray) = all(iszero, x)
1716+
export iszero
1717+
end
1718+
17111719
# julia#19088
17121720
if VERSION < v"0.6.0-dev.1256"
17131721
Base.take!(io::Base.AbstractIOBuffer) = takebuf_array(io)

test/runtests.jl

+16
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,22 @@ let str = randstring(20)
15701570
@test filter(!islower, str) == replace(str, r"[a-z]", "")
15711571
end
15721572

1573+
# julia#19950, tests from Base (#20028)
1574+
for T in (Float16, Float32, Float64, BigFloat, Int8, Int16, Int32, Int64, Int128,
1575+
BigInt, UInt8, UInt16, UInt32, UInt64, UInt128)
1576+
@test iszero(T(0))
1577+
@test iszero(Complex{T}(0))
1578+
if T<:Integer
1579+
@test iszero(Rational{T}(0))
1580+
end
1581+
if T<:AbstractFloat
1582+
@test iszero(T(-0.0))
1583+
@test iszero(Complex{T}(-0.0))
1584+
end
1585+
end
1586+
@test !iszero([0, 1, 2, 3])
1587+
@test iszero([0, 0, 0, 0])
1588+
15731589
x = view(1:10, 2:4)
15741590
D = Diagonal(x)
15751591
@test D[1,1] == 2

0 commit comments

Comments
 (0)