Vector4 Rounding Errors with Colors

Howdy,

I want to have a conditional where I check to see if a node is a certain color.

local green = vmath.vector4(0.1, 1, 0.1, 1)
print("=== node color info: "..tostring(gui.get_color(node)))
if gui.get_color(node) == green then
	stuff()
end

This works occasionally, but frequently fails because of rounding issues.

My output often looks like:

=== node color info: vmath.vector4(0.10000002384186, 1, 0.10000000149012, 1)

Any ideas on how to solve this?

Thanks!

This is due to floating point precision.

You can read more about it here

In short, unless you are certain that the number can be accurately represented (e.g. whole numbers), you are best advised to not do “==” comparisons with floating point numbers.

An alternative is to see if the number “is close” to the number you desired: if abs(a - b) < 0.001 then .... Although I’d look into other ways of detecting these scenarios instead.

2 Likes