lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/16/14, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
>> I am trying to convert a floating point number to an int
>
> Do you really need to convert from one subtype to another?
> Or just remove the fractional part of a float, resulting in a float?
> Note that a float may have an integral value but might not be convertible
> to
> an int. What do you need to happen if that is the case?
>
> Like Dirk said, it all depends on what the application needs.
>
> The philosophy of integers in Lua 5.3 is that applications rarely need to
> mix the two subtypes and will benefit from using them separately, even if
> some code needs to be changed. When an application really needs to mix the
> two subtypes, then that is a point where it needs to stop and think about
> what really needs to be done.
>
>

I'm currently wrapping up my first non-trivial project with Lua 5.3. I
was planning to write feedback on this, but I'm still reflecting on
it. However, since this topic is being discussed now... the
float-integer conversion was the thing that gave me the most pause. To
preface this, I love the new integer support and this is the killer
feature for me for Lua 5.3 that I've been waiting a long time for.

My biggest stumbling block with the float-integer conversion in my
project was that I have 2 separate C libraries I'm binding to. One
library is a physics library (Chipmunk) which uses floating point. The
other is a graphics library (SDL) which uses integer. The physics is
the "model" of the object and drives the "view". So the natural
inclination is just to hand off the physics results to the graphics,
but for non-intergral values, all my values turned to 0.

(I should note that I don't remember lua_tointeger dropping
non-integral numbers to 0 in Lua 5.1. Assuming this is a change, I do
consider this surprising and might cause other legacy bindings
problems like mine.)

So in the C bindings, I now do a little dance where effectively I try
to use lua_tointeger and if that fails, I call lua_tonumber and cast
to integer.


I don't have a strong feeling about what the correct behavior is and
I'm not (yet) advocating for any specific behavior. At the end of the
day, I solved my problem and my program works fine. But I am reminded
of Douglas Crockford's criticisms of integer overflows, "What is
reasonable behavior, and what is the worst thing we could do?"
Reasonable behavior might be throw an error or cap the value. The
worst thing is what is done today...make the value a maximum negative
and indistinguishable from other valid numbers. Lua dropping my value
to 0 in this case feels a little like a worst thing we could do to me.
Though in its defense, lua_tointegerx at least gives me a return value
if it worked. Thinking out-loud, maybe the 5.1 behavior of just
casting (and dropping the fractional part) is less damaging,
particularly since we do have an error value if we really need to
know.


Thanks,
Eric
-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/