How does Godot calculate?

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

It would be great, so that I can gain a better understanding, if someone could explain to me why Godot calculates as follows:

var a = 100 / (0 + 7) * 7
var b = 100 / (0 + 17) * 17
var c = 100 / (4 + 17) * 17
print(a)
print(b)
print(c)

(Incorrect) Results: a=98, b=85, c=68

var a = 100 / (float(0) + 7) * 7
var b = 100 / (float(0) + 17) * 17
var c = 100 / (float(4) + 17) * 17
print(a)
print(b)
print(c)

(Correct) Results: a=100, b=100, c=80.95238

You may have seen warnings in the console about losing precision?

SteveSmith | 2023-02-02 14:30

:bust_in_silhouette: Reply From: magicalogic

Probably because the variables are assumed to be int and not floats and narrowing conversion occurs. Its best to use types when you do not want that to happen.

var a : float = 100.0 / (0.0 + 7.0) * 7.0
var b : float = 100.0 / (0.0 + 17.0) * 17.0
var c : float = 100.0 / (4.0 + 17.0) * 17.0
print(a)
print(b)
print(c)

This is not unique to gdscript. In many programming languages, math performed on only integers always results in an integer. Really, if you elevate any of the values in the expression to a float, the result will also be a float. So, for that first example, just this would give you the expected result:

var a = 100.0 / (0 + 7) * 7  # change 100 to a float value

jgodfrey | 2023-02-02 14:42