[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Some thoughts on integer exponentiation (and an implementation)
- From: Sean Conner <sean@...>
- Date: Sun, 14 May 2017 17:46:51 -0400
It was thus said that the Great Stefan Ginsberg once stated:
> Something I would find more useful is if overflowed integer results were
> returned as nil (i.e. 123^456 == nil).
> Not just for '^' but for all relevant integer operations. This would also
> make unsigned numbers more manageable.
Such checks add overhead to Lua (but as usual, one would have to test to
see of the overhead is unacceptable). Unfortunately, there is no cheap way
to check for overflow in C (unlike assembly ).
Second, I'm not sure if I like returning nil.
a = 1
b = 63
c = 1
x = (1 << b) + c
stdin:1: attempt to perform arithmetic on a nil value
stdin:1: in main chunk
[C]: in ?
Okay, that doesn't happen now---what happens now is that x =
9223372036854775807 instead of -9223372036854775809 because of rollover.
I can see an argument for it, but ...
-spc (but I'm not sure if I want to see how sloppy I've been with math ... )
It's not much overhead in time, but it does increase the memory
usage of the code because of the overhead of checking for overflow.