Is it more efficient to store floats as decimals or fractions?

I was sitting down here on my desk, coding as per usual, when I was suddenly struck by the question in the title. Usually, I store down floats in the usual way you create one (int.decimal | ex.: 1.5), but I was wondering if storing in their fractional way (int/int | ex.: 3/2) is efficient, and, maybe, even better.

I do not know how I could provoke an intensive test for this myself, but I am sure some of you technical Roblox developers might have a broader knowledge to educate me on this. It would be of great help since sometimes it is easier to write down floats as a fraction instead of int.decimal.

Thank you in advance!

PS: I do assume writing them as fractions is more resource-intensive, since that requires the computer to calculate the float to begin with, unlike the int.decimal pattern.

1 Like

I mean, yeah, it’s what I left inside of that little PS, 'cause it makes sense that it’d be ever so slightly less… optimized? Because the difference between 0.5 and 1/2 is that the computer still has to divide 1 by 2, which does make them essentially the same in the end, but the step is still there.

Unless there’s somehow some sort of “operational cache” inside of the application, I really do have to agree on the possibility of it being less optimized in the long run, even if neglectable.

No difference. Both are evaluated to the same value during compilation. There will not be any measurable performance impact from using one over another. However, it is good practice to use the one the one that is most convenient and makes the most sense.

For example, you would write:

local secondsPerFrame = 1 / 60

Instead of:

local secondsPerFrame = 0.0166666

The former has the benefit of being more concise, with the added benefit of further explaining what the variable does. 1 / 60 has more meaning than 0.0166666.

1 Like

If this is a legitimate information, then I’d actually love to read on a source about that, 'cause that does sound very interesting that they are valued exactly the same during compilation. Maybe I could even reach far off into other things I am curious of.

This does already make me a little more reassured that my practice is not at all bad, but I will still gladly listen from more mouths. Thank you for the input, though.

Luau is a semi-compiled language. In compiled languages, many optimizations take place. Here’s a Wikipedia article that explains these optimizations:

I’m fairly certain that Luau has number literal optimizations, where the literal values are calculated beforehand.

1 Like

Oh hell yeah, this is the kind of stuff I was actually thinking about. Honestly, since Luau is a somewhat fast language, I will actually trust that this is exactly what happens in the background during the compilation and execution of code.

Thank you once again for thy input, and this also helped me killing out some doubts I have about other factors.

I will still await for more responses just to be safe, though!