Skip to content

Commit

Permalink
added iszero
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jan 14, 2017
1 parent 6473a35 commit dd0c40d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
8 changes: 8 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dd0c40d

Please sign in to comment.