mbed math

07 Feb 2010

Why does the compiler hate this statement?

accel = sqrt(pow(Ax,2)+pow(Ay,2)+pow(Az,2))/1333.0;

In particular the 'pow'...  Am I missing somethign in the manual? The compiler won't do it..  I ran into this once before, about a week ago..  I just looked at http://www.keil.com/support/docs/2518.htm

and http://www.cplusplus.com/reference/clibrary/cmath/pow/

I mean... I know it's almost 4am, but..  can someone enlighten me?  I'm mostly curious, and probably totally missing something obvious....

07 Feb 2010

I copied and paste your line in one of my program and it compiled with no error.

07 Feb 2010

Thanks for the reply!  Interesting...  Perhaps I will revisit after some sleep then... ;)

 

07 Feb 2010

BTW, using pow() for small integer powers is really inefficient... Ax*Ax will likely be much faster.

07 Feb 2010

Igor Skochinsky wrote:

BTW, using pow() for small integer powers is really inefficient... Ax*Ax will likely be much faster.

What he said, pow is only of benefit if the exponent is a variable or floating point.

08 Feb 2010

Thanks for the input!  That was actually a grab from some code I found on the web that I'm trying to get to work on the mbed for th SCA3000-D01 accelerometer...  Not with much luck as of yet I might add... =/ 

I do appreciate the help though!  I will certainly keep that in mind.. =) 

22 Nov 2010

why doesnt this work:

float temp;

float ppmm;

temp = (5 - voltage)/voltage;

ppmm = 103*(pow(temp, -1.495));

I thought pow works with floating point....

Errors

"More than one instance of overloaded function "pow" matches the argument list: (E308)" in file "/main.cpp"

"            function "std::pow(float, int)" (E0)" in file "/main.cpp"

22 Nov 2010

Hi Gowtham,

Try -1.495f (add an f); that'll tell the compiler to use the pow(float, float).

Currently, it doesn't know whether is should use pow(float, float) or pow(int, int).

Simon

12 Feb 2011

A note on the pow() function:

It's really, really terrible for small integer bases. Even if the exponent is a variable, it's probably better just to multiply it out. I just found a case where I was (stupidly) using it to create a bitmask - it took 31 uSec to execute (and that was with the {float, float} version - the {long double, int} version took 61 uSec!). By comparison, generating the mask manually with a for loop took 450ns.

Sure, this is a simple example, since I'm only doing some shift operations. But I figured someone else might also benefit from realizing just how incredibly inefficient pow() can be, since this is of special concern in the world of embedded development.

26 Mar 2011

Also, as far a I know the cmath function pow, will not work with int, int arguments.