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!
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])
Perfectly solved the problem. Thanks a lot!
Alternatively, .!isnan.([1, NaN])
That seems pretty unreadable to me!
That’s a matter of taste, but it can also be written as
@. !isnan([1, NaN])
I don’t disagree… I like the @. Syntax, but I feel like that would bite me somehow. I usually just stick to map
Note that map
will return Bool
ean arrays instead of BitArray
s.
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
To be honest, I don’t know how they’re different…
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