473,241 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

fast power function

hello group,

is there some reference implementation of a fast power function? how
fast is the power function in <cmath>?

thanks & hand, chris
Jan 21 '08 #1
11 21115
Chris Forone:
hello group,

is there some reference implementation of a fast power function? how
fast is the power function in <cmath>?

thanks & hand, chris

Very probably the fastest method of calculating powers for that platform.
--
Tomás Ó hÉilidhe
Jan 21 '08 #2
Tomás Ó hÉilidhe wrote:
Very probably the fastest method of calculating powers for that platform.
Not necessarily in all cases.

For example in intel architectures pow() is rather slow because
there's no such FPU opcode in 387. We are probably talking about many
hundreds, if not even over a thousand clock cycles even on a Pentium.
While compilers may try to optimize the pow() call away if they can,
they often can't.

In some cases it may be faster to "open up" a calculation than
calling pow(). For example, in many cases it may be slower to perform a
"pow(x, 1.5)" than a "x*sqrt(x)" (many compilers are unable to optimize
the former into the latter).

But of course this is more related to architectures and compilers than
to C++, and thus slightly off-topic.
Jan 21 '08 #3

Would it not make sense for the standard library to have a pow function
that deals only with integer exponents?

unsigned pow_int(unsigned const x,unsigned exp)
{
unsigned retval = 1;

while ( ; exp; --exp) retval *= x;

return retval;
}

--
Tomás Ó hÉilidhe
Jan 21 '08 #4
On 2008-01-21 11:25:39 -0500, "Tomás Ó hÉilidhe" <to*@lavabit.comsaid:
>
Would it not make sense for the standard library to have a pow function
that deals only with integer exponents?
It has three: pow(float, int), pow(double, int), and pow(long double, int).

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 21 '08 #5
Tomás Ó hÉilidhe wrote:
Would it not make sense for the standard library to have a pow function
that deals only with integer exponents?
Compilers do have specialized pow() functions when the exponent is
integer.
unsigned pow_int(unsigned const x,unsigned exp)
{
unsigned retval = 1;

while ( ; exp; --exp) retval *= x;

return retval;
}
That's *not* the fastest way of calculating pow(x, integer).
Jan 21 '08 #6
In article <47***********************@news.tdc.fi>,
no****@thanks.invalid says...

[ ... ]
For example in intel architectures pow() is rather slow because
there's no such FPU opcode in 387. We are probably talking about many
hundreds, if not even over a thousand clock cycles even on a Pentium.
While compilers may try to optimize the pow() call away if they can,
they often can't.
I don't know of any processor that would let you implement pow in a
single instruction -- but Intel floating point includes instructions for
logarithm and inverse logarithm, which make pow pretty easy to
implement.

As far as speed goes, you should be looking at about 150-190 CPU cycles
on a reasonably modern CPU (depending somewhat on data). I haven't found
any data for which it takes 200 cycles on my machine, and it's a few
years old -- I believe current CPUs are typically at least 20-30% faster
at the same clock speed.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 22 '08 #7
In article <fn**********@newsreader1.xoc.utanet.at>, 4o**@gmx.at says...
hello group,

is there some reference implementation of a fast power function? how
fast is the power function in <cmath>?
The implementation in the library is likely to be oriented more toward
being general purpose than the fastest for a given situation. You can
probably do substantially better if (and only if) you know a fair amount
about the data you're working with, so you can write something more
specialized.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 22 '08 #8
On Jan 21, 11:09 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Tomás Ó hÉilidhe wrote:
Would it not make sense for the standard library to have a pow function
that deals only with integer exponents?
Compilers do have specialized pow() functions when the exponent is
integer.
unsigned pow_int(unsigned const x,unsigned exp)
{
unsigned retval = 1;
while ( ; exp; --exp) retval *= x;
return retval;
}
That's *not* the fastest way of calculating pow(x, integer).
Nor the most accurate.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 22 '08 #9
On Tue, 22 Jan 2008 05:50:47 -0800, James Kanze wrote:
On Jan 21, 11:09 pm, Juha Nieminen <nos...@thanks.invalidwrote:
>Tomás Ó hÉilidhe wrote:
Would it not make sense for the standard library to have a pow
function
that deals only with integer exponents?
> Compilers do have specialized pow() functions when the exponent is
integer.
unsigned pow_int(unsigned const x,unsigned exp) {
unsigned retval = 1;
while ( ; exp; --exp) retval *= x;
return retval;
}
> That's *not* the fastest way of calculating pow(x, integer).
It may or may not be...
Nor the most accurate.
Surely it's guaranteed 100% accurate* - it's integer arithmetic.

*provided retval doesn't overflow.

--
Lionel B
Jan 22 '08 #10
Thaks a lot!

yours sincerely, chris
Jan 23 '08 #11
On Jan 22, 3:49 pm, Lionel B <m...@privacy.netwrote:
On Tue, 22 Jan 2008 05:50:47 -0800, James Kanze wrote:
On Jan 21, 11:09 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Tomás Ó hÉilidhe wrote:
Would it not make sense for the standard library to have a pow
function
that deals only with integer exponents?
Compilers do have specialized pow() functions when the exponent is
integer.
unsigned pow_int(unsigned const x,unsigned exp) {
unsigned retval = 1;
while ( ; exp; --exp) retval *= x;
return retval;
}
That's *not* the fastest way of calculating pow(x, integer).
It may or may not be...
It's not, by far.
Nor the most accurate.
Surely it's guaranteed 100% accurate* - it's integer arithmetic.
Oops. I missed that; I read the line "Compilers do have
specialized pow() functions when the exponent is integer", and
didn't look at the actual function parameters.

Using this function if the base is a floating point type, of
course, will result in very poor accuracy.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 23 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
32
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now...
11
by: anibio06 | last post by:
hi guys, i want to find 2^n (pow(2,n)). I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double. ...
5
by: Gus007 | last post by:
Hi all, Need the community great support once more. :) I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a...
40
by: logicode | last post by:
Hi, I usualy try to solve class problems on my own but I am hitting a wall. I am supposed to write a power function and call it in main but having problems like void power(int base,int exponent)...
9
by: suppamax | last post by:
Hi everybody! I'm writing a C program for a PIC18F microcontroller. I need to calculate a power function, in which both base and exponent are fixed point numbers (ex: 3.15^1.13). Using...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.