Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: why is there not a ipow version of pow?
Date: Sun, 16 Aug 2026 07:33:42 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <86y0e62kzd.fsf@linuxsc.com>
References: <115eks4$3pn1s$1@dont-email.me> <115f01a$3ugcg$1@raubtier-asyl.eternal-september.org> <115f20j$3un9e$1@dont-email.me> <115f64m$ml3$1@raubtier-asyl.eternal-september.org> <115f7h5$1659$2@dont-email.me> <115f7v8$1b5d$1@raubtier-asyl.eternal-september.org> <115facm$1659$3@dont-email.me> <115fchf$2e17$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sun, 16 Aug 2026 14:33:43 +0000 (UTC)
Injection-Info: dont-email.me; logging-data="262108"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8biEPVU/IccG74omL/DCQnFy8BeQjlgU="; posting-host="d60e9cb3bc0ca954ed7c56d99ae2d4fd"
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Xiew5HbevoRQPV67/m4UESjZJvU= sha1:nHM1t0936L56lfYgMkHHt4XIGNk= sha256:U4gtCjXXsdunWJ6JxlF7uf3PH/EJ+lWygLhzDgN3Wag= sha1:V6ublxnchW18fBhz/qMY/qE/KsY= sha256:AKrgMaJ7JzQE5GpGjxomR2q2vxhvIMxWBBiBJkoSA1A=
Xref: csiph.com comp.lang.c:401229
bart writes:
> [a C version of someone's ipow() function]
>
> long long int ipow(long long a, int n) {
> long long int res;
>
> res = 1;
> if (n < 0) {
> res = 0;
>
> } else if (n == 0) {
> res = 1;
>
> } else if (n == 1) {
> res = a;
>
> } else if ((n & 1) == 0) { // n is even
> res = ipow(a*a, n/2);
>
> } else { // n is odd
> res = ipow(a*a, (n-1)/2)*a;
> }
>
> return res;
> }
Two observations:
One: one of the recursive calls is not properly tail recursive so
the recursion isn't always optimized out.
Two: it gets wrong answers for in some cases with negative
exponents.