On Thu, Aug 24, 2006 at 10:06:25AM +0100, Andy Stark wrote:
Is it worth discussing how Lua could
support more than one sort of number?
Would it make sense to have binary coded decimal numbers as the
default
numeric type for Lua? (Ooooh, controversial!) It's obviously much,
much
slower than onboard floating point but is still fast enough for
many uses
and it has its advantages:
How much slower is it than onboard integral arithmetic? Don't some
processors support BCD instructions?
I find the "only one numeric type" stance of lua to be hard to work
around when interfacing with C code that uses unsigned long long a
lot,
and I haven't found a satisfying solution. Double is enough to contain
most integrals of a human scale, but not enough to mantain unsigned
long
long without loss of value. 64 bit processors are becoming common,
uint64s in C APIs might become more common, too.
Things we've tried:
- a Uint64 class
easy to do, hard to use (have to initialize with string literals,
have to rework C code to check for a lua_Number or Uint64 user-data
in lots of places, doesn't work with the %d formatting and in other
places where lua expects a number, feels like a hack, basically)
- Kauppi's patch - seems really nice, but only does signed long long,
and I need unsigned long long. Can keep the bit-representation, but
printing and display would be useless for me.
- long double as the lua native type - been working on this. People
seem horrified.... but I don't do any arithmetic to speak of other
than comparisons, simple counting loops, etc., I just need to move
data back and forth between C and lua accurately. I'm still working
on this.
BCD would be a nice compromise for me. Probably adequately fast for
most
things, would be a single numeric type (which fits the Lua Way, as I
understand it), but arbitrary precision. It would be quite small for
small numbers.
I'm trying to find a solution that doesn't involve creating a second
type that lua itself doesn't understand to be a number, which is what
straight-forward binding of multi-precision math libraries gives (lbc,
lmapm, etc.).
Anybody have a BCD-in-lua library? I'd be very happy to try working
with
such a beast!
Of course, we would still want the existing option of using different
numeric types with Lua but just supply BCD as standard. And there
is more
I'd be happy with BCD as an option. Actually, I'd be happy with
ANYTHING
that wasn't a native C type (gnu-mp, mapm, ...), so that
9223372036854775808 in lua code became a lua_Number, with no loss.
Cheers,
Sam