-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When dividing 2 integers, an integer is returned instead of a float #43711
Comments
This is expected behavior. Int/Int=Int, Float/Float=Float. do print(float(x)/float(y)) to get 2.5 from those 2 ints. |
That's always been the case in Godot indeed, or in C/C++ and many similar languages. If you want a float as return, at least one of the operands has to be a float. Otherwise you get an integer division (faster, truncates). |
This worked fine in 3.2 without any type hinting. Has this changed for 4.0 or recently in a 3.2.4 build? |
To clarify, you don't have to use type hints, but you do have to use a decimal point to let GDScript know that your Variant is a float and not an integer. Most likely your code had this bug unnoticed all along, or you had one term somewhere in the equation with type float. |
I just went back and checked this code in 3.2.4 and 4.0, as this was the source of the bug that I was trying to replicate. In 3.2.4 I had:
and this would give 1.777778 (when my res is 1280 x 720) whereas if I tried that in 4.0 it only gives a result of 1. I got around it by type hinting, but that code did give me a float in return, not an int as in 4.0. So something has changed between 3.2.4 and 4.0 with that particular bit of code. Also if I do this in 4.0:
it does return 1.777778 |
Vector2 components are floats: https://docs.godotengine.org/en/latest/classes/class_vector2.html Godot 4.0 introduces Vector2i, which as integer coordinates: https://docs.godotengine.org/en/latest/classes/class_vector2i.html And indeed It's important to understand that In |
In your specific case, the only issue is thus the introduction of an integer version of var size : Vector2 = get_viewport().get_size() # Note: this is type coercion too, from Vector2i to Vector2.
var aspect_ratio = size.x / size.y It can be debated whether changing |
Ah, thanks for the explanation! At least I know I wasn't going crazy, as I had no idea about Vector2i in Godot 4 and that Viewport.size was returning it. All makes sense now. This is what I get for trying to do some gamedev on a prealpha build :) |
Godot version:
current master 4.0
Issue description:
When dividing 2 integers, an integer is returned instead of a float. The only way around it is to add type hinting or decimal points.
Steps to reproduce:
for example:
returns a 2
correctly returns a 2.5
also correctly returns a 2.5
The text was updated successfully, but these errors were encountered: