Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Julia (as in the language) Set

Tags:

julia

I've got an application where Set semantics would be useful for me, but I'm dealing with floating point numbers. I'd like to have a criterion for inclusion in the set that isn't exact equality - that is, if my Set already contains 0.5, I'd like it to ignore 0.50000000000000000018 if there's an attempt to add it. Is there existing machinery that allows this?

like image 854
user888379 Avatar asked Oct 26 '25 02:10

user888379


1 Answers

As long as you don't need to keep the precision up in the Set, you could use a Set{Float16} or Set{Float32}:

julia> set = Set{Float16}()
Set{Float16}()

julia> push!(set, Float16(2.3))
Set{Float16} with 1 element:
  Float16(2.3)

julia> push!(set, Float16(2.300000001))
Set{Float16} with 1 element:
  Float16(2.3)
like image 109
Bill Avatar answered Oct 28 '25 02:10

Bill