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

countmap weird behaviour for -0.0 #809

Closed
jhbdiehl opened this issue Jul 7, 2022 · 1 comment · Fixed by #831
Closed

countmap weird behaviour for -0.0 #809

jhbdiehl opened this issue Jul 7, 2022 · 1 comment · Fixed by #831

Comments

@jhbdiehl
Copy link

jhbdiehl commented Jul 7, 2022

t = [0., -0.]
ct = countmap(t, [1 for e in t])

will output:

Dict{Float64, Int64} with 2 entries:
  0.0  => 1
  -0.0 => 1

Changing t to Integers

t = [0, -0]
ct = countmap(t, [1 for e in t])

or removing the weights

t = [0., -0.]
ct = countmap(t)

leads to the expected behaviour:

Dict{Float64, Int64} with 1 entry:
  0.0 => 2

Now all of this does not seem problematic until you start changing the keys, e.g.

Dict(keys(ct) .+ 1 .=> values(ct))

leads to

Dict{Float64, Int64} with 1 entry:
  1.0 => 1

in the first case, instead of

Dict{Float64, Int64} with 1 entry:
  1.0 => 2

Therefore I think this is not the intended behaviour.
To be fair this could also be interpreted as a bug in the general Dict routine.

@nalimilan
Copy link
Member

Good catch. See #831. This may end up going in the opposite direction you expected though.

Note that the different behavior of t = [0, -0] isn't a bug in StatsBase, it's just that -0 it exactly the same as 0, as the equivalent of -0.0 doesn't exist for Int. In other words, the information is lost even before countmap has the chance to see it.

Likewise, Dict(keys(ct) .+ 1 .=> values(ct)) simply retains the value for the last time the key was seen in (keys(ct) .+ 1 .=> values(ct)) == [1.0 => 1, 1.0 => 1], it doesn't sum counts (as the Dict constructor isn't made to work only with counts but with any kind of data).

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

Successfully merging a pull request may close this issue.

2 participants