godot icon indicating copy to clipboard operation
godot copied to clipboard

[Core] Add `is_same` to types that have float components

Open AThousandShips opened this issue 1 year ago • 8 comments
trafficstars

Compares NaN as equal.

Added to:

  • AABB
  • Basis
  • Color
  • Quaternion
  • Plane
  • Projection
  • Rect2
  • Transform2D/3D
  • Vector2
  • Vector3
  • Vector4

And a helper method in Math

Based on the code in Variant::hash_compare

Replaces the code in HashMapComparatorDefault and Variant::hash_compare

Can expose these as well as I think they are useful for script

Depends on (see for context):

  • https://github.com/godotengine/godot/pull/97550

AThousandShips avatar Sep 27 '24 17:09 AThousandShips

The name is entirely up for debate, picked it based on the naming in hash_compare but makes sense IMO

AThousandShips avatar Sep 27 '24 17:09 AThousandShips

Will move the (a == b) || (Math::is_nan(a) && Math::is_nan(b)) to Math to avoid the duplication here

AThousandShips avatar Sep 27 '24 18:09 AThousandShips

Why is nan comparison equal? Curious.

fire avatar Sep 28 '24 11:09 fire

For the special uses, otherwise you can't remove Vector2(NAN, NAN) from a HashMap for example, hence the hash_compare and the default comparisons

We already technically have this for float in is_same, so could use that naming instead, it uses Variant::identity_compare which calles hash_compare for these types

AThousandShips avatar Sep 28 '24 11:09 AThousandShips

Similar to #97550, it's probably worth adding these to the other Variant math structs for consistency. There's a fair bit of inconsistency in implementation across those scripts (structurally & functionally) that should be ironed out at some point, but that's not an immediate concern.

Repiteo avatar Sep 28 '24 15:09 Repiteo

Will add the remaining cases and rebase on top of an updated https://github.com/godotengine/godot/pull/97550

Will leave any exposing of these to the future and will change to is_same unless there's any opposition to that, more clear IMO

AThousandShips avatar Sep 28 '24 15:09 AThousandShips

Can you briefly explain what benefit this PR provides? Is there a bug this fixes or something that this improves?

clayjohn avatar Sep 30 '24 05:09 clayjohn

This is to unify uses of nan-handling equality checks used in hash comparisons, code that's duplicated between Variant and HashMapComparatorDefault, and possibly exposing these in the future (helping with the confusing cases with the == and != operators) to avoid hard to handle cases there

The primary reason is the noisy code added in https://github.com/godotengine/godot/pull/97550 to cover all relevant cases there

AThousandShips avatar Sep 30 '24 06:09 AThousandShips

Thanks!

Repiteo avatar Mar 10 '25 15:03 Repiteo

Thank you!

AThousandShips avatar Mar 10 '25 15:03 AThousandShips