Re: Taking -1 to integer powers <INT_MIN or >INT_MAX yields NaN

  • From: Mike Pall <mike-1311@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 19 Nov 2013 13:56:50 +0100

Philipp Kutin wrote:
> Arguably, exponentiating by such large numbers is pretty cornerly and
> -1 is about the only interesting LHS... I'm wondering if there are use
> cases for that.

Well, the current implementation on x86/x64 is a trade-off. One
has to decide between the iterative multiply+add algorithm for
integral exponents and the log/exp-based algorithm. The former
gets slow for big exponents and the latter cannot handle the
sign-twiddling for negative bases. And neither algorithm is
perfect. A perfectly precise pow() is quite involved.

Currently the transition point between the two algorithms is at
INT_MIN/INT_MAX. I might change that or just call libc pow() for
all architectures. Alas, the quality and speed of that one depends
a lot on the underlying C library.

[Exposing the underlying algorithm to the compiler offers some
interesting optimization opportunities, e.g. with a constant base
or a linear progression of exponents. That would be lost when
calling an opaque pow() function. But maybe these optimizations
are irrelevant in practice, except for silly microbenchmarks.]

--Mike

Other related posts: