Shop OBEX P1 Docs P2 Docs Learn Events
Raising to a power — Parallax Forums

Raising to a power

ArchiverArchiver Posts: 46,084
edited 2002-11-12 21:18 in General Discussion
I need to raise a word (in the range 0:1023) to the power n, where n
will be < 1, for example:

y = x**.23


I've read Tracy's math stuff, but didn't see anything there that was
directly applicable. Other than table lookup, anyone got any good
ideas?

Thanks

Larry

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-11-12 18:48
    This will be tough to do well with integer math. The trick is to keep in
    mind that what you really want is a root. So if you say 5**.5 you are
    really asking for the square root (1/2 = .5).

    Of course, this can be done with a log table (think slide rule) or just
    a pure look up table, but for 1024 entries that's somewhat large.
    Another answer would be a PAK-II or one of its related coprocessors.
    http://www.al-williams.com/pak1.htm
    This has the extra advantage that you can get the fractional part as
    accurate as you like subject to the 32-bit floating point number (24
    bits of significant value).

    Do you need just one root, or is the exponent also variable?

    Al Williams
    AWC
    * Floating point math for the Stamp, PIC, SX, or any microcontroller
    http://www.al-williams.com/awce/pak1.htm



    >
    Original Message
    > From: ve3crx [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=x1JsMx0HzqzQTKFx-aRsqffEFBszGJxu2EzsYFfaqtDS88SckHm4fg_x-KeqGAzFeRFDXdI]ve3crx@r...[/url
    > Sent: Tuesday, November 12, 2002 12:01 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]JUNK] [noparse][[/noparse]basicstamps] Raising to a power
    >
    >
    > I need to raise a word (in the range 0:1023) to the power n, where n
    > will be < 1, for example:
    >
    > y = x**.23
    >
    >
    > I've read Tracy's math stuff, but didn't see anything there that was
    > directly applicable. Other than table lookup, anyone got any good
    > ideas?
    >
    > Thanks
    >
    > Larry
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-11-12 19:40
    >I need to raise a word (in the range 0:1023) to the power n, where n
    >will be < 1, for example:
    >
    >y = x**.23
    >
    >
    >I've read Tracy's math stuff, but didn't see anything there that was
    >directly applicable. Other than table lookup, anyone got any good
    >ideas?
    >
    >Thanks
    >
    >Larry


    If n is a variable, too, then a table lookup/interpolation is not feasible.

    I would approach it as
    lg y = n * lg x
    where lg is log base 2. There is a an iterative algorithm for that
    on my web site. Having that, you can multiply (n * lg x) using the
    ** operator, and the result will be an integer part (characteristic)
    and a fractional part (mantissa) equal to lg y. The characteristic
    brackets the possible values of y between two integer powers of two.
    Then to narrow it down it would not be hard to put lg y in a loop and
    converge quickly on the solution. By quickly, I mean 10s of
    milliseconds altogether!

    I have not done the Stamp antilog explicitly (maybe someday).


    -- best regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...
  • ArchiverArchiver Posts: 46,084
    edited 2002-11-12 20:53
    Hi again Larry,

    Another thought on this,
    y = x**.23
    computed using lg (log base 2)
    lg y = .23 * lg x
    y = 2 to the power (.23 * lg x)

    if you need more speed and not so much accuracy (??), you could use
    the bitlog and its inverse (2 to the power of..) from
    http://www.emesys.com/BS2math3.htm#bitlog
    Bitlog is a very fast interpolation scheme to find logs and antilogs.

    -- Tracy


    >>I need to raise a word (in the range 0:1023) to the power n, where n
    >>will be < 1, for example:
    >>
    >>y = x**.23
    >>
    >>
    >>I've read Tracy's math stuff, but didn't see anything there that was
    >>directly applicable. Other than table lookup, anyone got any good
    >>ideas?
    >>
    >>Thanks
    >>
    >>Larry
    >
    >
    >If n is a variable, too, then a table lookup/interpolation is not feasible.
    >
    >I would approach it as
    > lg y = n * lg x
    >where lg is log base 2. There is a an iterative algorithm for that
    >on my web site. Having that, you can multiply (n * lg x) using the
    >** operator, and the result will be an integer part (characteristic)
    >and a fractional part (mantissa) equal to lg y. The characteristic
    >brackets the possible values of y between two integer powers of two.
    >Then to narrow it down it would not be hard to put lg y in a loop
    >and converge quickly on the solution. By quickly, I mean 10s of
    >milliseconds altogether!
    >
    >I have not done the Stamp antilog explicitly (maybe someday).
    >
    >
    > -- best regards
    > Tracy Allen
    > electronically monitored ecosystems
    > http://www.emesystems.com
    > mailto:tracy@e...
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-11-12 21:18
    Tracy (and Al)

    The power is a constant within a given system (It is an amp-hour
    meter for my sailboat; the available power in a battery depends on
    the actual current drain raised to a power; the number is a constant
    for a particular brand of battery.

    A general method would of course be best; then I can just change the
    constant for different batteries.

    Actually, the real version of this is going to be built using a PIC
    16F877 rather than a STAMP. The stamp is real nice for playing around
    with concepts. The real device will probably be programmed using
    PicBasic Pro.

    I'll look at the web site again for your log stuff, Tracy.

    Thanks

    Larry
Sign In or Register to comment.