0 votes

-(1/30) returns 0.
why? how can i fix it?

I am in version 3.2.2

-0.0333333 works, but I shouldn't have to do that

in Engine by (96 points)

2 Answers

+2 votes
Best answer

This is normal. It is called "integer division". When you divide two integers, the result will also be an integer.

1 / 3 == 0
1.0 / 3.0 == 0.33333

Note that as long as one operand is a float, the result will be a float:

1.0 / 3 == 0.333333
float(1) / 3 == 0.33333
by (21,979 points)
selected by

answered at the same time... you wrote faster!

0 votes

use -(1.0/30.0)

if that numbers comes from a float variable, should not be problem:

    var number1 : float = 1
    var number2 : float = 30
    var calc : float = -(number1/number2)
    print (str(calc))
    # print -> 0.033333333

Problem is the implicit declaration of integers of that operation: " -(int1/int30)"
You can use too: -( float(1) / float(30) )

An operation like this directly with numbers is not common in game code, but you can cast all the numbers or use "dot zero" to allow the interpreter to know the type.

by (341 points)
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.