2

I know not all compilers do that but does lua do it?

I have this written 15 * 1.75 * 2 I can just leave a comment line and leave the calculation there, in order make it still understandable but leaving it like the way it is better and I am curious.

Semih
  • 23
  • 3
  • 3
    Depends on the version, here you can inspect the bytecode: https://www.luac.nl/ Modern Lua optimizes that. In any case, use the more readable version unless absolutely required. – Luke100000 Jun 14 '23 at 12:17
  • You can always chose to write the more readable value as comment – Ivo Jun 14 '23 at 12:23

1 Answers1

3

Math expressions are calculated at compile time in Lua 5.1+ (thanks to Luke100000 for the link in the comments).
The only exceptions are infs and NaN. A Lua bytecode contains only finite numeric constants; "dangerous" math operations resulting in inf/NaN are deferred to runtime.

ESkri
  • 1,461
  • 1
  • 1
  • 8