Incorrect vector value calculations !!!!

Godot Version

4.2.1

Question

Incorrect vector value calculations !!!

extends Node


# Called when the node enters the scene tree for the first time.
func _ready():
	print(2963665 * 20000)
	print(Vector3(-1915, 0, 2963665 * 20000))


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

this code returns the values:
59273300000
(-1915, 0, 59273302016)
Why 59273302016???
Here my test project Van-Fim/godoonesimpleproject: GodotOneSimpleProject (github.com)

Vector3 values are floating-point, which leads to precision errors when doing math. Use Vector3i for integer vectors, but keep in mind each component is limited to 32-bit signed integer (64-bit if using a self-compiled precision=double engine build), whereas int is always a 64-bit signed integer.

The same applies to Vector2 and Vector4.

3 Likes

Vector3I is always 32 bits, regardless of precision=double, it only affects real_t

1 Like

And if floating point precision errors are a surprise, get ready for integer overflow errors. A 32-bit signed integer has a maximum value of about 2 billion (2,147,483,647) before it overflows to become a negative number.

extends Node

func _ready():
	print(2963665 * 20000)
	print(Vector3(-1915, 0, 2963665 * 20000))
	print(Vector3i(-1915, 0, 2963665 * 20000))

Output:
59273300000
(-1915, 0, 59273302016)
(-1915, 0, -856242144)

So, be careful multiplying big numbers together and passing the result to places that are going to store the number in a data type that might not be able to contain that number correctly.

1 Like