Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Raising numbers to a power

0 views
Skip to first unread message

bogus6...@mailinator.com

unread,
Dec 10, 2006, 2:24:20 AM12/10/06
to
If you have to compute y^x, where y is a non-negative real and x is a
(not necessarily integer-valued) real, do you _all_ use exp(x*ln(y))
(or some slight variation thereof), or do any of you use a method not
involving logarithms?

Marcin 'Qrczak' Kowalczyk

unread,
Dec 10, 2006, 3:44:26 AM12/10/06
to
bogus6...@mailinator.com writes:

If either argument is a floating-point number, and x is non-negative
(i.e. the result is real), I use the C function pow() to implement
exponentiation in my language. I don't care whether C pow() implements
it as exp(x*ln(y)), or as a machine instruction for exponentiation,
or as something else.

For two integers with a positive base my implementation uses
mpz_pow_ui() and mpz_ui_pow_ui() from the GMP library. Again I don't
care how it computes it, perhaps by repeated multiplication and
squaring according to bits of the exponent, but I'm sure it doesn't
use logarithms.

These two are the only "core" implementations I use. They are wrapped
by appropriate formulas when the arguments or the result are exact
non-integer rational numbers or non-real complex numbers.

--
__("< Marcin Kowalczyk
\__/ qrc...@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

cr88192

unread,
Dec 10, 2006, 6:14:59 AM12/10/06
to

<bogus6...@mailinator.com> wrote in message
news:1165735460.7...@n67g2000cwd.googlegroups.com...

int^int:
I use a loop (ideal would be to hard-code some of these cases, but I will
argue that, in general, exponents aren't that common, so it is not really
needed imo).

then again, such an optimization could always be added as a minor compiler
hack, and I could utilize pre-existing bytecodes, eg:

int x;
...
x^3

load x
dup_f
dup_f
mul_fn
mul_fn

which is probably better than nothing...


float^float:
I use pow (except when the output will be complex, eg, with a negative base
and non-integer exponent, ...).

complex^float
float^complex
complex^complex
I use more "involved" calculations...

0 new messages