5

The Lua documentation says that Lua represents values of type number using a double, which allows all long integer values to be correctly represented using a floating point number.

However I see in the code that lua_Number is actually a float. Which is giving me the following warning :

warning C4244: 'argument' : conversion from 'double' to 'lua_Number', possible loss of data

For :

double fVarVal = 0.0;
lua_pushnumber( L, fVarVal );

So how are the values of type number represented in Lua ? float or double ? If they are using floats, can't this create problems when integer values, like array indexes, are being used ?

I'm using Lua 5.3.2 for Windows CE.

unwind
  • 391,730
  • 64
  • 469
  • 606
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • It is [configurable](http://pgl.yoyo.org/luai/i/lua_Number) – Eugene Sh. Mar 09 '16 at 14:46
  • Thanks. Is it worth using doubles instead of floats ? I'm creating very basic things that will mostly use integers, are occasionnally floats without requiring much precision like a physics engine would require. – Virus721 Mar 09 '16 at 14:48
  • I think it depends on your requirements. But I don't think you will notice any performance degradation using `double` instead of `float` most of the times. – Eugene Sh. Mar 09 '16 at 14:51
  • @EugeneSh. I'm more concerned about unexpected behaviors with floats rather than performance, though that will run on rather weak embedded devices using Windows CE and Android. BUt I guess if the guys who ported Lua to WIndows CE made float a defaultn it should be used as is preferably. – Virus721 Mar 09 '16 at 14:53
  • What unexpected behavior can you get out of using `double` which you won't from `float`? – Eugene Sh. Mar 09 '16 at 14:54
  • That's what they say about lua_Number that made me suspicious about floats : http://www.lua.org/pil/2.3.html Though I'm not sure about what kind of problem to expect, if any. – Virus721 Mar 09 '16 at 14:56
  • Floats (instead of doubles) are usually used on devices without FPU. – Egor Skriptunoff Mar 09 '16 at 14:59
  • @EgorSkriptunoff I've encounterd the exact opposite cases, where FPUs were working for floats but not doubles. – Eugene Sh. Mar 09 '16 at 15:01
  • @Virus721 BTW, why are you working with `double` at all and not with `lua_Number` in the first place? – Eugene Sh. Mar 09 '16 at 15:05
  • A `double` cannot represent all bit of a `long int` with 64 bits (POSIX64). – too honest for this site Mar 09 '16 at 15:08
  • @EgorSkriptunoff Well I'm storing some Lua values (primitives only) in my C code to make them available across multipe scripts. Using lua_Number is an option that hasn't come to my mind yet. I may consider doing this. But the values that i'm storing in my C code are likely to be got/set by other things than Lua scripts so I don't know if I should use lua_Numbers. – Virus721 Mar 09 '16 at 15:10

1 Answers1

2

I'm using Lua 5.3.2 for Windows.

Well that rather changes things. Lua 5.3 added the ability to use actual integers in Lua, directly. Lua always had the lua_pushinteger function, but in 5.3, it doesn't convert it to a double. It is instead a 64-bit integer.

From Lua of course, integers and floats look the same. And most of the Lua C API doesn't make much distinction. But it can if you want it to.

In any case, lua_Number defaults to double, so even on Lua 5.1/2, integers-as-floats was not much of a problem. Of course, lua_Integer and lua_Number can be adjusted as you see fit. Just be aware that external DLLs you use have to be recompiled with those settings too.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982