Skip to content

strange behavior of mpc(-1) when used as a key in a set or dict #985

Description

@donhatch

I make a set {1, -1}, and then test membership of the elements as ints, floats, mpf's and mpc's. I expect all of these membership tests to return True.

>>> import mpmath
>>> s = {1, -1}
>>> 1 in s
True
>>> -1 in s
True
>>> 1.0 in s
True
>>> -1.0 in s
True
>>> mpmath.mpf(1) in s
True
>>> mpmath.mpf(-1) in s
True
>>> mpmath.mpc(1) in s
True
>>> mpmath.mpc(-1) in s
False                      # <---- what?

I didn't expect that last False; it seems inconsistent.
The same thing happens if a dict is used instead of a set.

Alternatively, we can look at the hash values:

>>> hash(1), hash(1.0), hash(mpmath.mpf(1)), hash(mpmath.mpc(1))
(1, 1, 1, 1)
>>> hash(-1), hash(-1.0), hash(mpmath.mpf(-1)), hash(mpmath.mpc(-1))
(-2, -2, -2, 6)

That last 6 looks like an anomaly. Shouldn't it be -2 like the others next to it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions