godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.44k stars 20.25k forks source link

Comparison Operator: Values of types int and float with same numerical value are equal #80162

Open nanoquack opened 1 year ago

nanoquack commented 1 year ago

Godot version

v4.1.stable.official [970459615]

System information

Windows 11 Pro, Version 22H2, Build 22621.1778 - v4.1.stable.official [9704596] - Vulkan (Forward +)

Issue description

It's not really a bug, but it is somehow counterintuitive: While two variables can have different types int and float, they are treated as equal by the comparison operator:

var val1 = 42
var val2 = 42.0
print(val1 == val2) #true
print(typeof(val1) == typeof(val2)) #false

This gets especially interesting, if you add an int and a float to an array or a dictionary and compare them:

var val1 = [42]
var val2 = [42.0]
print(val1 == val2) #false
print(val1[0] == val2[0]) #true

As you can see, the two arrays are not considered equal, and rightly so, but the two values are considered equal. Wouldn't it be much more logical if comparison of an int and a float always returns false?

Steps to reproduce

See above

Minimal reproduction project

N/A

AThousandShips commented 1 year ago

The first behavior is not going to change, pretty certainly, it's pretty universal across different languages to allow this, as it is a major source of confusion and errors

The second part is because when checking equality of Array it checks the type as well, this should be documented because it can be confusing

dalexeev commented 1 year ago

See:

GDScript reference - Operators:

nanoquack commented 1 year ago

I agree that many languages do it this way (especially more dynamically typed ones), and I didn't mean to imply the behaviour has to be changed, but I think as @AThousandShips proposed the behaviour for Array and Dictionary should be documented