-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement Base.hash(x::DecFP.DecimalFloatingPoint) #97
Comments
For example, we do have |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
For hashing we want
So for -0.0/0.0 a simple implementation for
In this case |
This is tricky because it should ideally work for arguments of different numeric types. For example, Base gives: julia> hash(3.0) == hash(3.0f0) == hash(3) == hash(3+0im)
true Accomplishing this in a reasonably efficient way was only possible after a lot of effort by @StefanKarpinski (in JuliaLang/julia#6624). Doing |
Right now the only thing I can think of is something like: function Base.hash(x::Dec64, h::UInt)
xf = Float64(x)
isequal(xf, x) && return(xf, h)
# ...convert to Rational{BigInt} and call hash(::Real, h) ...
end Converting to |
Here is what you need to implement: https://github.com/JuliaLang/julia/blob/442d1599e62b1a3a453bd/base/hashing2.jl#L28-L94 If you implement
I don't really understand the details of the DecFP type, but I presume that it represents rational values of the form p ≥ 0 ? (n*5^p, p, 1) : (n, p, 5^-p) You need to be careful that the return values can't overflow, of course. If It may be possible to get even greater efficiency by implementing a more specialized version of |
Oh, you also may need to move powers of 5 from |
Thanks so much for your input, Stefan. After some thinking, I have come up with the following routine for extracting integer numbers
The Regarding the possibility of integer overflow: we are guaranteed that
So, for example, if |
That's the basic problem here — |
All may not be lost. This might be avoidable with some additional cleverness. See https://github.com/JuliaLang/julia/blob/442d1599e62b1a3a453bd/base/hashing2.jl#L5-L26 This implements hashing of the integer values |
In particular, There is always the "nuclear option" of changing the way Base hashing works. I doubt many packages are hooking into it at this point and we can easily figure out the ones that do so. |
Did this get fixed? |
Yes, although the solution involved |
Ok, glad it could be fixed, that is a shame about the necessity for bigints, but hopefully it can become more optimized in the future. |
We could use
Base.hash(x::DecFP.DecimalFloatingPoint, h::UInt) = hash(x.x, h)
. But I have thought of a problem:If
x
andy
are of typeDec64
, and ifx == y
, then we must be able to guaranteehash(x) == hash(y)
. Since the definition ofBase.:(==)(x::Dec64, y::Dec64)
makes accall
to the Intel Decimal Floating-Point Math Library, I doubt thatx == y
will implyhash(x.x) == hash(y.x)
.Any ideas about how we can provide a good
hash
function for use in Julia'sDict
,Set
, etc?The text was updated successfully, but these errors were encountered: