How to make a Set of real values based on rtol?



Lets say you wanted to make a Set of real numbers:

Set( [ 
] )

Where for all practical purposes, this should reduce to Set([1.0,2.0])

Is there an existing data structure that allows something like: Set(..., rtol=1e-8) to provide this functionality?

(as well as maintain all the features of a Set)

edit: I guess what I’m asking is:

  • is it possible to change the equality condition for Set checking?


Set is based on hashing, and there won’t be a hash function for your notion of equality, because it’s not transitive.

julia> a,b,c = [1.0 .+ i * 5e7 * eps() for i in 0:2];

julia> isapprox(a,b)

julia> isapprox(b,c)

julia> isapprox(a,c)

Because you want to do this for real numbers, which are ordered, you should be able to keep a sorted list of members. That way, it will take log(n) time to insert an element or decide it’s already inserted, instead of n time. You should be able to use searchsorted for this.