Merge pull request #1123 from corona10/fix_complex_hash

complex: Fix hash function to work correctly
This commit is contained in:
Windel Bouwman
2019-07-10 19:27:59 +02:00
committed by GitHub
2 changed files with 7 additions and 2 deletions

View File

@@ -94,6 +94,10 @@ assert hash(complex(3.14)) == hash(float(3.14))
assert hash(complex(-float('inf'))) == hash(-float('inf'))
assert hash(1j) != hash(1)
# TODO: Find a way to test platform dependent values
assert hash(3.1 - 4.2j) == hash(3.1 - 4.2j)
assert hash(3.1 + 4.2j) == hash(3.1 + 4.2j)
# numbers.Complex
a = complex(3, 4)

View File

@@ -1,5 +1,6 @@
use num_complex::Complex64;
use num_traits::Zero;
use std::num::Wrapping;
use crate::function::OptionalArg;
use crate::pyhash;
@@ -254,7 +255,7 @@ impl PyComplex {
fn hash(&self, _vm: &VirtualMachine) -> pyhash::PyHash {
let re_hash = pyhash::hash_float(self.value.re);
let im_hash = pyhash::hash_float(self.value.im);
re_hash + im_hash * pyhash::IMAG
let ret = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(pyhash::IMAG);
ret.0
}
}