Skip to main content
Fede Rico
Associate III
September 26, 2017
Question

How to optimise the power computation

  • September 26, 2017
  • 1 reply
  • 4764 views
Posted on September 26, 2017 at 11:58

Hi there,

I'm working on STM32F103 MCU and i need to compute the powerof a value.

I use the math.h library but I'm looking for a more optimised library, if it's exists.

Is there a better way to compute the power?

Best Regards,

F.

#math.h

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    1 reply

    Danish1
    Lead III
    September 26, 2017
    Posted on September 26, 2017 at 15:11

    Am I right in thinking that you are trying to compute x^y but you find it takes too long on stm32f103?

    If it is taking too long then that often means you want to do it many times in quick succession, perhaps with either x or y not changing between calculations.

    In C, pow(x, y) does things in double-precision. If x and y are not double-precision (e.g. float - ) then it first has to convert them to double-precision, and if you only need the result to be float then it has to convert back to float. All 3 steps waste time. powf(x, y) is faster if you only need float.

    And if x or y are integers (e.g you only want to calculate x^2) then it is much faster to explicitly do x*x

    How else might things be speeded up?

    pow calculates exp(y*log(x))

    So if x doesn't change from call to call, then you can pre-calculate the log.

    I'm sure there are other things to do to improve the speed of calculation, but only because you know something about the calculation or series-of-calculations that the compiler didn't guess.

    I'd expect the math library to be pretty-well optimised and fast for double-precision numbers in general.

    Hope this helps,

    Danish

    AvaTar
    Senior III
    September 26, 2017
    Posted on September 26, 2017 at 15:25

    In C, pow(x, y) does things in double-precision.

    As usual, there is a single-precision variant in C lib too, here called

    powf()

    .

    Or, you are searching for some integer based algorithm. This function is not architecture-dependent, so you can try to port any algorithm.

    Or, depending on your requirements, even a table-based approach might do, possibly with interpolation.

    A lot depends on your ideas of performance, precision, and parameter range.

    Fede Rico
    Fede RicoAuthor
    Associate III
    September 26, 2017
    Posted on September 26, 2017 at 15:27

    Basically I want use the powf to read the distance from this distance sensor: GP2Y0A41SK0F

    I found a libray (for arduino ) that uses the power