!isnan.() doesn't work

Dear Julia user,

Why does isnan.([0 0/0]) return a 1×2 BitArray{2}: false true, but !isnan.([0 0/0]) return an error message: no method matching !(::BitArray{2})?

Thanks a lot!

Order of operations: broadcasting takes precedence over negation. To enforce the correct order of operations, use (!isnan).([0 0/0]), or just broadcast over the whole expression: @. !isnan([0 0/0])

3 Likes

Perfectly solved the problem. Thanks a lot!

Alternatively, .!isnan.([1, NaN])

3 Likes

That seems pretty unreadable to me!

2 Likes

That’s a matter of taste, but it can also be written as

@. !isnan([1, NaN])
1 Like

I don’t disagree… I like the @. Syntax, but I feel like that would bite me somehow. I usually just stick to map :wink:

1 Like

Note that map will return Boolean arrays instead of BitArrays.

julia> map(isodd, 0:10)'
1×11 LinearAlgebra.Adjoint{Bool,Array{Bool,1}}:
 0  1  0  1  0  1  0  1  0  1  0

julia> isodd.(0:10)'
1×11 LinearAlgebra.Adjoint{Bool,BitArray{1}}:
 0  1  0  1  0  1  0  1  0  1  0
2 Likes

To be honest, I don’t know how they’re different…

1 Like

Bools are stored using one byte per value, while BitArrays use, like the name implies, one bit per value.

julia> Base.summarysize(Array{Bool}(undef, 1024))
1064

julia> Base.summarysize(BitArray(undef, 1024))
192

julia> Base.summarysize(BitArray(undef, 0))
64

julia> 1028 ÷ (192 - 64)
8

2 Likes