Shop OBEX P1 Docs P2 Docs Learn Events
Help with a formula — Parallax Forums

Help with a formula

ArchiverArchiver Posts: 46,084
edited 2001-07-23 20:32 in General Discussion
Hello Group:

I have a big problem, I need to read a value and apply
the following formula to this value:

a=4.427157*SQR(x/3.14)

How can I do that Maybe I need a math cooprocesor or
something like that.

Please Help me.

=====
Alberto Zamora Estrada.
Phone: (506) 220-6736.
Cell: (506) 374-3846.
Fax: (506) 220-8684
Investigaci

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-07-23 18:36
    Hi Alberto,

    If you use a calculator to get rid of the SQR(1/3.14) you find

    a = 2.498389 * SQR(x)
    This is almost 2.5 * SQR(x) (-0.064% deviation)
    If you can live with that use a = 5 * SQR(x) / 2 and see if that is accurate
    enough.
    Otherwise you must use floating point math or a copressor.
    Take a look http://www.emesystems.com/BS2math6.htm for double precision
    math.

    Greetings peter



    Oorspronkelijk bericht
    Van: Alberto Zamora [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=syR8AVvn8IbKCIist6EaUC1JsBacvzmINN9G1dSk6AS_aczmByc6yjlzP4aKmKss6f-VrJw2ZTI5kTQxyg]azestrada@y...[/url
    Verzonden: maandag 23 juli 2001 18:08
    Aan: Grupo Basic Stamp
    Onderwerp: [noparse][[/noparse]basicstamps] Help with a formula

    Hello Group:

    I have a big problem, I need to read a value and apply
    the following formula to this value:

    a=4.427157*SQR(x/3.14)

    How can I do that Maybe I need a math cooprocesor or
    something like that.

    Please Help me.

    =====
    Alberto Zamora Estrada.
    Phone: (506) 220-6736.
    Cell: (506) 374-3846.
    Fax: (506) 220-8684
    Investigaci
  • ArchiverArchiver Posts: 46,084
    edited 2001-07-23 19:23
    Alberto -

    It is important to know the range of x, as well as how
    you will be using 'a' later. Since the Stamp deals
    only in integers, errors can accumulate quickly.

    Remember that sqrt(x) * sqrt(y) == sqrt(xy). So
    instead of figuring out the square root of x/3.14 and
    multiplying by 4.427157, we can consolidate the right
    side as sqrt(4.427157*4.427157*x/3.14). This
    simplifies to about 6.24195 * x.

    The ** operator, which Tracy at emesystems.com
    explains very thoroughly, works well for fractions.
    Removing the whole part of 6.24195 and multiplying the
    fraction by 65535 gives a product of 15856, which we
    can use with the ** operator. Some additional math
    and trial-and-error tells us that the value of x can
    be 0 to 10499 with this argument of **.

    The code looks like this:

    '{$STAMP BS2} ' change for BS2e/sx/p
    x VAR WORD
    a VAR WORD
    FOR x = 0 TO 99
    a = (x * 6) + (x ** 15856)
    a = SQR a
    DEBUG DEC x, " ", DEC a, CR
    NEXT

    This works well if you only need the integer, but if
    that were the case 2.5 * sqrt(x) would have been fine.
    You may want a value to be used with **, for instance,
    and in that case the range of x becomes more
    important. Check out what Tracy has on square roots
    and come back if we can help more.

    By the way, if the 3.14 in your formula should really
    be pi, use 15649 where I used 15656.

    Bob Pence

    --- Alberto Zamora <azestrada@y...> wrote:
    > Hello Group:
    >
    > I have a big problem, I need to read a value and
    > apply
    > the following formula to this value:
    >
    > a=4.427157*SQR(x/3.14)
    >
    > How can I do that Maybe I need a math cooprocesor or
    > something like that.
    >
    > Please Help me.
    >
    > =====
    > Alberto Zamora Estrada.
    > Phone: (506) 220-6736.
    > Cell: (506) 374-3846.
    > Fax: (506) 220-8684
    > Investigaci
  • ArchiverArchiver Posts: 46,084
    edited 2001-07-23 20:32
    As several have pointed out, you can probably simplify this and get close if
    your X value is well-bounded and you don't mind the integer result.

    However, you can very easily do this with a PAK-II or a PAK-IX (the PAK-IX
    is just like a PAK-II but it also does 5 channels of 10-bit A/D conversion).
    The PAK-I could do this, but it doesn't have a built-in square root (the
    example code shows how to do square roots "manually" though). The PAK-II
    also adds 16 I/O pins. The PAK-IX (which is on sale right now) adds 8
    digital I/O along with the 5 analog inputs.

    To realize your formula on the PAKs you'd use the FCONVERT program supplied
    (or downloaded from our site) to convert 3.14 and 4.427157 into 4 byte hex
    numbers. Then you'd load Y with the 3.14, and load X (an integer) into the
    PAK's X register. You'd make a call to convert the integer into floating
    point and then another call do do the divide. On the PAKII/IX you'd make yet
    another call to do the square root. Finally, you'd load the 4.427157 into
    the Y register and call multiply.

    This would leave a floating point result in X. You could convert it to an
    integer. Pick it apart digit by digit (good for display), convert it to IEEE
    format, or just leave it in floating point format (not much good for the
    Stamp). Also, you could scale it (say multiply by 10 or 100) and then
    convert to an integer if you wanted to see some of the digits after the
    decimal point in the Stamp.

    You can find several examples at http://www.al-williams.com/awce/doclib.htm
    and the main page at http://www.al-williams.com/awce/pak1.htm.

    Good luck!

    Al Williams
    AWC
    * Read 8 pulses at once
    http://www.al-williams.com/awce/pak7.htm


    >
    Original Message
    > From: Alberto Zamora [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=z7CMYDX5e8CM2HDKIKP0syEQhzNJdIZG9rigU3aIpCRaFZ_EgkcdpJERTjX5AwOdwS7UXMLZTNr3FgA]azestrada@y...[/url
    > Sent: Monday, July 23, 2001 12:08 PM
    > To: Grupo Basic Stamp
    > Subject: [noparse][[/noparse]basicstamps] Help with a formula
    >
    >
    > Hello Group:
    >
    > I have a big problem, I need to read a value and apply
    > the following formula to this value:
    >
    > a=4.427157*SQR(x/3.14)
    >
    > How can I do that Maybe I need a math cooprocesor or
    > something like that.
    >
    > Please Help me.
    >
    > =====
    > Alberto Zamora Estrada.
    > Phone: (506) 220-6736.
    > Cell: (506) 374-3846.
    > Fax: (506) 220-8684
    > Investigaci
Sign In or Register to comment.