Stream: helpdesk (published)

Topic: Bitwise and


view this post on Zulip Dale Black (Jun 17 2021 at 20:47):

Say I have two arrays of Floats, is there a way to compare them using the bitwise and operator .& or some other operator?

 .& y

The above works for Int and Bool but not Floats

view this post on Zulip Dale Black (Jun 17 2021 at 20:50):

Or maybe a better question, is there a better way to convert Floats to integers?

Here is the full code and I am getting an inexact error when I try to run this

function dice_metric(, y)
    , y = Int64.(), Int64.(y)
    dice = 2 * sum(( .& y)) / (sum() + sum(y))
    return dice
end

view this post on Zulip Dale Black (Jun 17 2021 at 20:51):

InexactError: Int64(-5.5319665e-6)

Stacktrace:
  [1] Int64
    @ ./float.jl:723 [inlined]
  [2] _broadcast_getindex_evalf
    @ ./broadcast.jl:648 [inlined]
  [3] _broadcast_getindex
    @ ./broadcast.jl:621 [inlined]
  [4] getindex
    @ ./broadcast.jl:575 [inlined]
  [5] macro expansion
    @ ./broadcast.jl:984 [inlined]
  [6] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [7] copyto!
    @ ./broadcast.jl:983 [inlined]
  [8] copyto!
    @ ./broadcast.jl:936 [inlined]
  [9] copy
    @ ./broadcast.jl:908 [inlined]
 [10] materialize
    @ ./broadcast.jl:883 [inlined]
 [11] dice_metric(ŷ::Array{Float32, 4}, y::Array{Float32, 4})
    @ Main ./In[28]:2
 [12] top-level scope
    @ In[37]:39
 [13] eval
    @ ./boot.jl:360 [inlined]
 [14] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1094

view this post on Zulip Matt Bauman (Jun 17 2021 at 20:51):

What's a true floating point value to you?

view this post on Zulip Matt Bauman (Jun 17 2021 at 20:51):

all nonzeros?

view this post on Zulip Dale Black (Jun 17 2021 at 20:52):

Yes I am working with binary arrays currently so that should be correct

view this post on Zulip Matt Bauman (Jun 17 2021 at 20:53):

what's a binary array?

view this post on Zulip Matt Bauman (Jun 17 2021 at 20:53):

In any case, I think I'd do @. !iszero(ŷ) & !iszero(y)

view this post on Zulip Mason Protter (Jun 17 2021 at 20:55):

To me, this is what I think of when I think of bitwise and between floats:

julia> x = rand(5)
5-element Vector{Float64}:
 0.4136385097187676
 0.8561170860323639
 0.5673269657861939
 0.3364253326881379
 0.2945387286498147

julia> y = rand(5)
5-element Vector{Float64}:
 0.6559688998496613
 0.033461184123736
 0.6446603481441768
 0.9123783704466826
 0.7033529950179171

julia> reinterpret(UInt64, x) .& reinterpret(UInt64, y)
5-element Vector{UInt64}:
 0x3fc0790025808400
 0x3fa1214300c04100
 0x3fe0210aa1001082
 0x3fc502341e540000
 0x3fc2819823a42004

julia> reinterpret(Float64, ans)
5-element reinterpret(Float64, ::Vector{UInt64}):
 0.1286926444163612
 0.03345689186255818
 0.5040333885704202
 0.16412974816194037
 0.14457990398682352

view this post on Zulip Matt Bauman (Jun 17 2021 at 20:56):

Yeah, I think you're after logical &&, not bitwise &

view this post on Zulip Dale Black (Jun 17 2021 at 21:04):

Okay I will do some investigating because I realize now I don't really understand bitwise and

view this post on Zulip chriselrod (Jun 21 2021 at 15:47):

julia> bitstring(0x55)
"01010101"

julia> bitstring(0x0f)
"00001111"

julia> bitstring(0x55 & 0x0f)
"00000101"

Bitwise & does & between the individual bits of the number.

view this post on Zulip Dale Black (Jun 22 2021 at 02:16):

Okay, it looks like bitwise & is actually what I am looking for then. I just need to figure out the best way to turn an array of numbers into a binary array of zeros and ones after they go through a training loop

view this post on Zulip Dale Black (Jun 22 2021 at 02:16):

I'm simply trying to use this dice_metric

function dice_metric(, y)
    dice = 2 * sum( .& y) / (sum() + sum(y))
    return dice
end

view this post on Zulip Dale Black (Jun 22 2021 at 02:31):

I will go with this for now since it seems to work fine

function as_discrete(array, logit_threshold)
    array = array .>= logit_threshold
    return array
end

Last updated: Nov 06 2024 at 04:40 UTC