Is there a fast implementation of power(int, int) in standard clibrary?

S

Sean

of course you could use pow(double, double) , but I am concerned about
the efficiency.
For example: 5^8 = (5^2)*(5^2)(5^2), only need 3 integer
multiplications
 
B

Ben Pfaff

Sean said:
of course you could use pow(double, double) , but I am concerned about
the efficiency.
For example: 5^8 = (5^2)*(5^2)(5^2), only need 3 integer
multiplications

No, the standard C library doesn't have such a function.

You can implement one pretty easily, e.g.:

int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}

return result;
}

I took this from
http://stackoverflow.com/questions/...nt-an-integer-based-power-function-powint-int,
which was near the top of the Google search results for "integer
power function". I haven't tested it, but it looks plausible.

I would probably use unsigned integer types.
 
T

Tom St Denis

of course you could use pow(double, double) , but I am concerned about
the efficiency.
For example: 5^8 = (5^2)*(5^2)(5^2), only need 3 integer
multiplications

Except that (5^2)^3 = 5^6 not 5^8 ...

The quickest way to x^8 is by squaring x 3 times.

Tom
 
K

Kenny McCormack

Except that (5^2)^3 = 5^6 not 5^8 ...

The quickest way to x^8 is by squaring x 3 times.

Tom

<CLC-pedant-mode>
Actually, if you square X 3 times, what you get is: x^2 x^2 x^2
E.g., for x=2, you get: 4 4 4
Same result each time.
</CLC-pedant-mode>
 
T

Tom St Denis

<CLC-pedant-mode>
Actually, if you square X 3 times, what you get is: x^2 x^2 x^2
E.g., for x=2, you get: 4 4 4
Same result each time.
</CLC-pedant-mode>

<actually has a clue mode>
When someone says "square x 3 times" they mean ((x^2)^2)^2, which
you'd know if you weren't as equally clueless about mathematics as you
are about the C language or proper manners and netiquette.
</clue>

Tom
 
K

Kenny McCormack

Richard said:
Indeed.

<nerdy "full of oneself" voice on>
Basically it's rubbish in and rubbish out, and I, for one, would have
to give full marks to the OP had he implemented Tom's incorrect solution
albeit unknowingly.
</nerdy voice off>

Very good. I think you've got their mannerisms down pat.
 
K

Kenny McCormack

<CLC-pedant-mode>
Actually, if you square X 3 times, what you get is: x^2 x^2 x^2
E.g., for x=2, you get: 4 4 4
Same result each time.
</CLC-pedant-mode>

<actually has a clue mode>
When someone says "square x 3 times" they mean ((x^2)^2)^2, which
you'd know if you weren't as equally clueless about mathematics as you
are about the C language or proper manners and netiquette.
</clue>

Tom[/QUOTE]

Gotcha!
 
T

Tom St Denis

<actually has a clue mode>
When someone says "square x 3 times" they mean ((x^2)^2)^2, which
you'd know if you weren't as equally clueless about mathematics as you
are about the C language or proper manners and netiquette.
</clue>

Gotcha![/QUOTE]

Explain how? I *know* you're a troll. I just felt like replying
because it was vaguely math related and I was bored.

Oh, I get it, ahahahaha, you're a waste of a human being and you find
it funny. Awesome. Way to contribute to society.

Tom
 
A

Antoninus Twink

I would probably use unsigned integer types.

On the great clc principle that getting the wrong answer doesn't matter
as long as you don't invoke UB, presumably?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top