Optimize power function for fixed point numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • suppamax

    Optimize power function for fixed point numbers

    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 pow() function is too expensive...

    Is there another way to do that?


    Thanks,



    Max
  • Keith Thompson

    #2
    Re: Optimize power function for fixed point numbers

    suppamax <max.giacometti @gmail.comwrite s:
    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 pow() function is too expensive...
    >
    Is there another way to do that?
    How are these fixed point numbers represented? Does your compiler
    have special support for them? Standard C's only arithmetic types are
    integer and floating-point.

    If the exponent were always an integer, you could do it with repeated
    multiplication; you could save a few multiplications with judicious
    use of squaring. But with a non-integral exponent, you're going to
    have to do something very similar to what the pow() function does.

    I don't think you've given us enough information to help you. We need
    a better idea of how the operands are represented, what values they
    can have, how precise you need the result to be, and so forth.

    It's possible that comp.programmin g might be a better place to ask;
    the solution you're looking for is likely to be an algorithm rather
    that something specific to C.

    --
    Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • user923005

      #3
      Re: Optimize power function for fixed point numbers

      On Mar 12, 9:16 am, suppamax <max.giacome... @gmail.comwrote :
      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 pow() function is too expensive...
      >
      Is there another way to do that?
      Maybe this can help:


      You might look at the float implementation on the Cephes site:


      Comment

      • user923005

        #4
        Re: Optimize power function for fixed point numbers

        On Mar 12, 9:16 am, suppamax <max.giacome... @gmail.comwrote :
        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 pow() function is too expensive...
        >
        Is there another way to do that?
        Can you tell us why you need the power function?
        There may be a work-around (e.g. using Horner's rule to evaluate
        polynomials instead of pow()).

        Comment

        • suppamax

          #5
          Re: Optimize power function for fixed point numbers

          How are these fixed point numbers represented? Does your compiler
          have special support for them? Standard C's only arithmetic types are
          integer and floating-point.
          >
          If the exponent were always an integer, you could do it with repeated
          multiplication; you could save a few multiplications with judicious
          use of squaring. But with a non-integral exponent, you're going to
          have to do something very similar to what the pow() function does.
          >
          I don't think you've given us enough information to help you. We need
          a better idea of how the operands are represented, what values they
          can have, how precise you need the result to be, and so forth.
          >
          It's possible that comp.programmin g might be a better place to ask;
          the solution you're looking for is likely to be an algorithm rather
          that something specific to C.
          >
          --
          Keith Thompson (The_Other_Keit h) <ks...@mib.or g>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Numbers always have 2 digits, and are represented as integers.
          For example, if the correct value is 3.15, it will be represented as
          315.



          Max

          Comment

          • Richard Heathfield

            #6
            Re: Optimize power function for fixed point numbers

            suppamax said:

            <snip>
            Numbers always have 2 digits, and are represented as integers.
            For example, if the correct value is 3.15, it will be represented as
            315.
            That's a little confusing. If numbers always have 2 digits, 315 is not a
            number! Did you mean 3 digits?

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • suppamax

              #7
              Re: Optimize power function for fixed point numbers

              Can you tell us why you need the power function?
              There may be a work-around (e.g. using Horner's rule to evaluate
              polynomials instead of pow()).
              The function I need to realize is something like

              exp = 1.15;
              result = 0;
              while (...) {
              [evaluate base: it will be, for example, 4.77]
              result += pow(base, exp);
              }



              Max

              Comment

              • suppamax

                #8
                Re: Optimize power function for fixed point numbers

                That's a little confusing. If numbers always have 2 digits, 315 is not a
                number! Did you mean 3 digits?
                >
                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Sorry...

                2 decimal digits.

                so 3.15 -315




                Max

                Comment

                • user923005

                  #9
                  Re: Optimize power function for fixed point numbers

                  On Mar 13, 1:24 am, suppamax <max.giacome... @gmail.comwrote :
                  That's a little confusing. If numbers always have 2 digits, 315 is not a
                  number! Did you mean 3 digits?
                  >
                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999
                  >
                  Sorry...
                  >
                  2 decimal digits.
                  >
                  so 3.15 -315
                  What is the largest possible value in your system?
                  What is the smallest possible value in your system?
                  How much memory space do you have available?

                  Comment

                  • Paul Hsieh

                    #10
                    Re: Optimize power function for fixed point numbers

                    On Mar 12, 9:16 am, suppamax <max.giacome... @gmail.comwrote :
                    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 pow() function is too expensive...
                    >
                    Is there another way to do that?
                    It doesn't seem obvious to me. I guess you would want a break down
                    like:

                    two_pow_fromIM ( y * two_log_toIM ( x ) );

                    The idea would be that two_log_toIM and two_pow_fromIM could be
                    implemented as a scaling (normalize to the range 1 <= x < 2) then
                    either a post or pre-shift along with a table look up if the
                    resolution was small enough (and possibly perform interpolations) . To
                    _fromIM and _toIM reflect the fact you might like to convert it to a
                    temporarily higher resolution intermediate value, or range corrected
                    for the particular input values.

                    I am not aware of any really good approximations to log() or 2exp()
                    except for taylor series or rational function approximations, which
                    will end up doing no better than using pow() directly. This table
                    based stuff would obviously compromise accuracy/resolution.

                    --
                    Paul Hsieh
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                    Comment

                    Working...