  lua-l archive

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

• Subject: Re: Some thoughts on integer exponentiation (and an implementation)
• From: Stefan Ginsberg <stefanginsberg@...>
• Date: Sun, 14 May 2017 12:02:32 +0200

On Sun, May 14, 2017 at 7:02 AM, Dirk Laurie wrote:

Well, I raised a question much like this less than a year ago .
The usual sort of list discussion followed and the matter was dropped.

What I wanted was that if M and N are integers, and M^N can be
represented exactly, the exponentiation operator should do that.

You're going a step further and say: if it overflows, let it overflow.

I'm trying to think of a plausible use case and can't. If we had unsigned
integers, I could.

 http://lua-users.org/lists/lua-l/2016-06/msg00077.html

I believe you made a good case for integer exponentiation in that post.

The implementation does not check for overflow simply to be consistent with the other mathematical operators.

I see a problem with rounding instead of wrapping around in that it makes '^' behave differently from '*'.
That is, 'b ^ 3' and 'b * b * b' resulting in both different types and values for different integer 'b'.
If integer '^' checks for overflow then shouldn't '*' do the same? As you point out, '*' is also prone to overflow.

If a rounded float result is desired then why not just use float operands instead of integers?
Me I would rather have a wrapped around integer than a rounded float, if I am using integer operands.
I don't find much use for wrap-around either (in a high-level language) but that's how Lua does it.

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.

• Follow-Ups:
• References: