Shop OBEX P1 Docs P2 Docs Learn Events
exp function — Parallax Forums

exp function

TinkersALotTinkersALot Posts: 535
edited 2013-04-28 10:44 in Propeller 1
Is there an exp function (preferably in spin) ?

Comments

  • tperkinstperkins Posts: 98
    edited 2013-04-27 05:27
    There isn't a defined word for it. How close an approximation do you need? and for what range of x and y?

    I am thinking you are looking for x^y generally, or e^x? e^x for a narrow range of values could I think best be a lookup table with interpolation.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-04-27 08:59
    There is an exp table in the hub at $D000, 12 bits in, 16 bits out. I'm attaching a little demo program to show how it works.

    For most problems it gets you only part of the way to your answer and the question about the range of x and y is very important. Look at it this way. The hub exp table gives you 2x for values of x between 0 and 1. So 2x is between 1 and 2. For powers outside that range you have to scale. Add or subtract integer values from the exponent to make the fractional part fall in range for the table. The integer powers you subsequently handle with right or left shifts of the answer that comes out of the table lookup. For other bases, for example ex, you have to dust of high school algebra and recall that ex = 2lg2(e)*x = 21.442695 * x. After multiplying out the exponent, then do the thing with powers of 2 and the table lookup.
  • TinkersALotTinkersALot Posts: 535
    edited 2013-04-27 15:35
    man this site just rocks! thanks guys!
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-04-28 10:44
    When you say "in Spin" are you adverse to loading a floating point cog? Both Float32 and F32 do have exp base e, and base 10, and both internally are interpolating in the base 2 hub tables.

    Floating point is at its heart represents a number as a binary fraction between 0 and 1, along with an exponent, so it is preconditioned to use the tables. Here is the start of the Float32 pasm exp function:
    [SIZE=1][FONT=courier new]_Exp                    call    #_FMulI                 ' e ** fnum
                            long    1.442695041
                            jmp     #_Exp2
    
    _Exp10                  call    #_FMulI                 ' 10 ** fnum
                            long    3.321928095
    
    _Exp2                   call    #_Unpack                ' unpack variable                    
    [/FONT][/SIZE]
    

    You see right there the conversion factor 1.442695041 that will return a base e result, and 3.321928095 for base 10, and then it jumps to the base 2 routine that does the table lookup, adjusts the exponent, and manages the details.
Sign In or Register to comment.