lua-users home
lua-l archive

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


On Mon, Apr 14, 2008 at 9:53 PM, Ralph Hempel
<rhempel@hempeldesigngroup.com> wrote:
>  <long winded response>
>
>  In my pbLua application, I had the opposite problem. I had compiled
>  Lua to be strictly 32 bit integers and had a strange hybrid mix
>  of single precision float and integers that occasionally gave
>  strange results if the programmer was not careful.
>
>  In the end, I made a userdata library that implemented float as
>  its own data type and then added a metatable with the built-in
>  operations for +-/* , the comparisons, as well as a tostring()
>  and concat() capability.
>
>  It was not that hard and once again made me appreciate the care
>  and thought that has gone into making the Lua C API a thing
>  of frightening simplicity and beauty.
>
>  </long winded response>
>
>  I would suggest having a look at creating your own userata for
>  high precision monetary values, and implementing a metatable
>  with overrides for the basic Lua operations.
>
>  You may also end up having to add functions to create instances
>  of the type etc. See the C API and userdata secions in the Blue PiL
>  for details.

I recently discovered one disadvantage of such an approach: relational
operators are only possible on operands of the same type. In other
words, you can't compare such a userdata object with a regular number
since it is by definition false. This might or might not be a problem,
depending on your specific needs but it's certainly something to keep
in mind.

-- 
Dirk