danomatika / ofxLua

(maintained) a lua scripting interface & bindings for openFrameworks
Other
164 stars 36 forks source link

New types int and double #36

Closed alexp-nl closed 7 years ago

alexp-nl commented 7 years ago

Hi Dan,

thanks for ofxLua, it is making my Lua/openframeworks live a lot easier. And it would be even better if you could include get/set/is int and double functions. So getInt, setInt, isInt, etc.

Thanks in advance, and best regards, AlexP

danomatika commented 7 years ago

The basic numeric type in lua is a floating point number: http://lua-users.org/wiki/NumbersTutorial

I can see the need for a double type getter/setter for more precision, but you can simply cast for integers:

int a = 10;
lua.setFloat((float)a); 
alexp-nl commented 7 years ago

In Lua 5.3 the integer was introduced, but you are right that in most cases the float version of the functions can be used. But only up to 2^23. For numbers higher than this you will lose the precision that int has. So natural integer versions of the ofxLua functions are still better in my opinion.

The double versions are really crucial to me however, and since the internal representation of floats in Lua is the double, it would be logical to include them in ofxLua.

danomatika commented 7 years ago

The best solution, really, is to change the float getter/setter functions to use lua_Number instead, which is usually a double by default, but can be changed with a compile flag.

danomatika commented 7 years ago

Also, I try to keep ofxLua compatible with luajit for Raspberry PI, so adding lua_Integer and lua_Unsigned functions would be wrapped in ifdef blocks based on the Lua version.

alexp-nl commented 7 years ago

That's all fine with me, as long as I can use the full integer and double range in exchanging values between C++ and Lua using ofxLua. By the way, I work with Windows.

danomatika commented 7 years ago

Ok, try the number branch which uses lua_Number instead of float. I probably should have used this to begin with and now the api types better match the Lua types aka LUA_TNUMBER.

EDIT: If it's working fine, I can merge with master later.

alexp-nl commented 7 years ago

Number branch works like a charm, thanks!

danomatika commented 7 years ago

Ok, easy fix and something I had thought about doing before.

danomatika commented 7 years ago

Merged