+1 vote

That's it. The following code:

print(2 / 4)

returns 0 but I believe it should return 0.5. My phone's calculator returns 0.5 as well as the default Windows calculator. So, why does Godot output 0 instead of 0.5?

in Engine by (195 points)

This standard C code returns the same than Godot:

 #include <stdio.h>

int main(void) {
    float r= 2/4;
    printf("%f", r);
    return 0;
}

Output is 0.000000

2 Answers

+3 votes
Best answer

This is because 2 and 4 are implicit integers, so you need to use 2.0 or 4.0 to tell the script you want to compute float numbers

by (58 points)
selected by
+3 votes

print(2.0/4.0)
or
print(2/4.0)
or
print(2.0/4)

by (576 points)

Also print(float(2)/4)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.