diff --git a/README.md b/README.md index ce9ddfd16..019bf326c 100644 --- a/README.md +++ b/README.md @@ -98,9 +98,11 @@ Currently, the `@compat` macro supports the following syntaxes: * `transcode` converts between UTF-xx string encodings in Julia 0.5 (as a lightweight alternative to the LegacyStrings package) ([#17323]) -* `∘` (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) +* `∘` (typically used infix as `f ∘ g`) for function composition can be used in 0.5 and earlier ([#17155]) -* 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) +* The method of `!` to negate functions (typically used as a unary operator, as in `!isinteger`) can be used in 0.5 and earlier ([#17155]). + +* `iszero(x)` efficiently checks whether `x == zero(x)` (including arrays) ([#19950]). ## Renamed functions @@ -266,6 +268,7 @@ includes this fix. Find the minimum version from there. [#16563]: https://github.com/JuliaLang/julia/issues/16563 [#16603]: https://github.com/JuliaLang/julia/issues/16603 [#16972]: https://github.com/JuliaLang/julia/issues/16972 +[#17155]: https://github.com/JuliaLang/julia/issues/17155 [#17302]: https://github.com/JuliaLang/julia/issues/17302 [#17323]: https://github.com/JuliaLang/julia/issues/17323 [#17510]: https://github.com/JuliaLang/julia/issues/17510 @@ -275,3 +278,4 @@ includes this fix. Find the minimum version from there. [#18977]: https://github.com/JuliaLang/julia/issues/18977 [#19088]: https://github.com/JuliaLang/julia/issues/19088 [#19246]: https://github.com/JuliaLang/julia/issues/19246 +[#19950]: https://github.com/JuliaLang/julia/issues/19950 diff --git a/src/Compat.jl b/src/Compat.jl index a7b6dd820..5bbca44c0 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1708,6 +1708,14 @@ if !isdefined(Base, :numerator) export numerator, denominator end +# julia #19950 +if !isdefined(Base, :iszero) + iszero(x) = x == iszero(x) + iszero(x::Number) = x == 0 + iszero(x::AbstractArray) = all(iszero, x) + export iszero +end + # julia#19088 if VERSION < v"0.6.0-dev.1256" Base.take!(io::Base.AbstractIOBuffer) = takebuf_array(io) diff --git a/test/runtests.jl b/test/runtests.jl index 118651efd..63daf0e6f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1560,6 +1560,22 @@ let str = randstring(20) @test filter(!islower, str) == replace(str, r"[a-z]", "") end +# julia#19950, tests from Base (#20028) +for T in (Float16, Float32, Float64, BigFloat, Int8, Int16, Int32, Int64, Int128, + BigInt, UInt8, UInt16, UInt32, UInt64, UInt128) + @test iszero(T(0)) + @test iszero(Complex{T}(0)) + if T<:Integer + @test iszero(Rational{T}(0)) + end + if T<:AbstractFloat + @test iszero(T(-0.0)) + @test iszero(Complex{T}(-0.0)) + end +end +@test !iszero([0, 1, 2, 3]) +@test iszero([0, 0, 0, 0]) + x = view(1:10, 2:4) D = Diagonal(x) @test D[1,1] == 2