Godot: constant `PI` <> 3.141593

Created on 14 Mar 2019  路  7Comments  路  Source: godotengine/godot

Godot version:
3.1

OS/device including version:

Issue description:
Look this:
print(PI)

shows 3.141593

print (3.141593 == PI)

shows False

print (PI - 3.141593)

shows -0

What's the deal?
If PI is a float constant and float is the maximum precision in Godot, so PI might have just 6 decimals as shown, therefore PI might be exactly = 3.141593 .

archived

All 7 comments

I think is not showing all the other decimals. If you do a print(PI*100) you will see a 3141.592654...

Instead of doing a (3.141593 == PI) I would do a (3141593 == round(PI*1000000)). And if you want more presition, multiply both sides by 10000000, 100000000, etc.

Instead of doing a (3.141593 == PI) I would do a (3141593 == round(PI*1000000)). And if you want more presition, multiply both sides by 10000000, 100000000, etc.

Great. But is there any variable type in Godot with more and 6 decimal places?

Godot already supports more than 6 decimal places, it just doesn't print all the decimal places.

var num = 1.0000001
print(num) # prints "1"
print(num == 1) # prints "false"

It's usually bad practice to compare floats with equals. You should compare them with less than and greater than. If you're looking to see if they are equivalent you can also use a proximity value such as:

if (myValue + proximity) > floatToCompare && (myValue - proximity) < floatToCompare then print('They are basically equal')

I got it.
The float type in Godot has actually many decimal places.
The problem was indeed with print

If I do:

var f : float = 0.12345678901234567890123456789
print (f*1000000)

I get:

`123456.789012'

I'll use round to compare it.
Thanks.

Instead of doing a (3.141593 == PI) I would do a (3141593 == round(PI*1000000)).

abs(value - expected) < tolerance is much more flexible.

Was this page helpful?
0 / 5 - 0 ratings