Fixed point math?

Hey,

I’ve been following Defold and played around with it a bit, and evaluating it for potential future projects.

This leads to one question, can you do fixed point math in Defold. In case of doing a multiplayer RTS, using float points is a bit scary as they could differ from device to device (and server). In Unity using integers would work well just interpreting them as 16.16, but not sure this can be done in Defold?

If anyone can shed some light on this, I’d appreciate it! Thanks!

2 Likes

We use Lua 5.1/LuaJIT which both have a single number type which is used to represent real (double-precision floating-point) numbers. (https://www.lua.org/pil/2.3.html)

The next few sentences from the link above: “Lua has no integer type, as it does not need it. There is a widespread misconception about floating-point arithmetic errors and some people fear that even a simple increment can go weird with floating-point numbers. The fact is that, when you use a double to represent an integer, there is no rounding error at all (unless the number is greater than 100,000,000,000,000). Specifically, a Lua number can represent any long integer without rounding problems.”

Now I understand the reason for your question and the documentation doesn’t hold true since Lua 5.3 introduced integers as a sub-type to number with automatic conversion between integers and floats (https://www.lua.org/manual/5.3/manual.html#3.4.3). You could write a Lua module or native extension that deals with the critical parts of the code that requires fixed point arithmetic if that really is what you need.

3 Likes

Ok, that makes sense. Thanks for the reply!

1 Like