Change hashing rules for float-like types to make 0 == -0

This commit is contained in:
gingerBill
2025-04-16 10:52:35 +01:00
parent 990cc56974
commit 3dcc22fa6d
5 changed files with 132 additions and 4 deletions
+40 -3
View File
@@ -1439,6 +1439,11 @@ as_f64 :: proc(a: any) -> (value: f64, valid: bool) {
case Type_Info_Complex:
switch v in a {
case complex32:
if imag(v) == 0 {
value = f64(real(v))
valid = true
}
case complex64:
if imag(v) == 0 {
value = f64(real(v))
@@ -1453,6 +1458,11 @@ as_f64 :: proc(a: any) -> (value: f64, valid: bool) {
case Type_Info_Quaternion:
switch v in a {
case quaternion64:
if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
value = f64(real(v))
valid = true
}
case quaternion128:
if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
value = f64(real(v))
@@ -1646,13 +1656,40 @@ equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_
return equal(va, vb, including_indirect_array_recursion, recursion_level+1)
case Type_Info_Map:
return false
case Type_Info_Float:
x, _ := as_f64(a)
y, _ := as_f64(b)
return x == y
case Type_Info_Complex:
switch x in a {
case complex32:
#no_type_assert y := b.(complex32)
return x == y
case complex64:
#no_type_assert y := b.(complex64)
return x == y
case complex128:
#no_type_assert y := b.(complex128)
return x == y
}
return false
case Type_Info_Quaternion:
switch x in a {
case quaternion64:
#no_type_assert y := b.(quaternion64)
return x == y
case quaternion128:
#no_type_assert y := b.(quaternion128)
return x == y
case quaternion256:
#no_type_assert y := b.(quaternion256)
return x == y
}
return false
case
Type_Info_Boolean,
Type_Info_Integer,
Type_Info_Rune,
Type_Info_Float,
Type_Info_Complex,
Type_Info_Quaternion,
Type_Info_Type_Id,
Type_Info_Pointer,
Type_Info_Multi_Pointer,