Skip to content
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

Decimal not usable as Dict key: no hash method defined #35

Open
Jasha10 opened this issue Jul 19, 2019 · 5 comments
Open

Decimal not usable as Dict key: no hash method defined #35

Jasha10 opened this issue Jul 19, 2019 · 5 comments

Comments

@Jasha10
Copy link

Jasha10 commented Jul 19, 2019

Perhaps the hash could be defined in terms of the has of the underlying fields s, c and q of the Decimal struct. For reference, here is a stacktrace:

julia> d = Dict()
Dict{Any,Any} with 0 entries

julia> d[Decimal(123)] = 456
ERROR: MethodError: no method matching decompose(::Decimal)
Closest candidates are:
  decompose(::BigFloat) at hashing2.jl:133
  decompose(::Float64) at hashing2.jl:122
  decompose(::Float32) at hashing2.jl:111
  ...
Stacktrace:
 [1] hash(::Decimal, ::UInt64) at ./hashing2.jl:32
 [2] hash(::Decimal) at ./hashing.jl:18
 [3] hashindex(::Decimal, ::Int64) at ./dict.jl:169
 [4] ht_keyindex2!(::Dict{Any,Any}, ::Decimal) at ./dict.jl:309
 [5] setindex!(::Dict{Any,Any}, ::Int64, ::Decimal) at ./dict.jl:382
 [6] top-level scope at none:0
@Jasha10

This comment has been minimized.

@Jasha10
Copy link
Author

Jasha10 commented Aug 12, 2019

As far as I can tell, implementing hash for Decimals.jl presents similar problems as it does here: JuliaMath/DecFP.jl#97

I think that implementing decompose for the Decimal type would require using BigInt, since Decimal uses BigInt internally.

@tencnivel
Copy link

This issue also makes it impossible to use the 'unique' function

@stevengj
Copy link
Member

DecFP (master) now supports hashing, though our solution involved BigInt. It's possible that we may find a faster algorithm in the future, but for now at least it works.

@jmkuhn
Copy link

jmkuhn commented Feb 9, 2021

Hashing could be supported by adding decompose() similar to the DecFP implementation.

sigexp() is a DecFP function that is documented here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants