+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 (198 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

+4 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 (60 points)
selected by
+3 votes

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

by (584 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 Frequently asked questions and 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 [email protected] with your username.