-(1/30) returns 0 in GDscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fuck

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

:bust_in_silhouette: Reply From: kidscancode

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

answered at the same time… you wrote faster!

807 | 2020-08-05 21:47

:bust_in_silhouette: Reply From: 807

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.