Shop OBEX P1 Docs P2 Docs Learn Events
BS2P Math Operations — Parallax Forums

BS2P Math Operations

spainespaine Posts: 51
edited 2006-08-29 15:26 in BASIC Stamp
I am trying to perform a math operation such as: 22.34564 / 500, but I'm not sure how this would be done.

Any ideas?

Thanks!

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-08-24 17:15
    Represent 22.34564 in the Stamp memory as 22346. Then 22346 / 5 = 4469, which represents 0.04469.

    That is a quick view, and it is hard to say more without knowing specifics of what you are trying to accomplish. The Stamp only does 16 bit integer math, and to get precise results you have to draw from an arsenal of ad hoc tricks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • spainespaine Posts: 51
    edited 2006-08-24 17:40
    Tracy,

    Thanks for the help. I see what you mean about the 16 bit math. I was just reading your AppNote at EME Systems. Do you mind if I contact you about this topic, if I have further questions?

    I'm actually kind of lost by your AppNote. That is, I don't think I personally have enough knowledge of the topic to understand it.

    Thanks,
    Stephen
  • spainespaine Posts: 51
    edited 2006-08-25 18:49
    After reading your Application Notes on your website, I thought I would take a step back and try to simplify it the math processing. I went ahead and purchased a uMFPU from Parallax. Hopefully, this will be somewhat easy to operate smile.gif

    Thanks for the help Tracy.
    Stephen
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-08-25 19:00
    Yes, the uFPU will let you focus on your problem at hand instead of the math details. A lot depends on what you want to do. If it is a scaling problem that can fit within the 16 bit precision, there is probably a relatively simple way to do it with the ** or */ operator or a calculation loop if the variable is in the denominator. But if you really need precision like 22.34564, or if you floating point math functions without so much concern about scaling and overflow, then the uFPU is certainly the way to go.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Kirk FraserKirk Fraser Posts: 364
    edited 2006-08-27 14:45
    If your needs are simple such as always dividing by a decimal constant, the Stamp help on */ says you can use hex like $01A3. Since I didn't exactly know how to convert or what number I needed, I just ran the program a few times in Stamp debugger changing the values until I got results in the range I wanted.

    Of course if you need to change the decimal variable frequently then you're on the right track.

    Kirk
  • KatyBriKatyBri Posts: 171
    edited 2006-08-28 03:16
    Would someone please explain what uFPU (mentioned in this thread) is? Thanks
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-28 04:30
    KatyBri -

    A uFPU is a small, math co-processor which is capable of (F)loating (P)oint calculations, among other things. Thus uFPU is "micro Floating Point Unit".

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-08-28 16:40
    You can find the uFPU (along with some documentation and source code) at the link below.· I hope this helps.· Take care.

    http://www.parallax.com/detail.asp?product_id=604-00030

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • spainespaine Posts: 51
    edited 2006-08-28 20:27
    I have tried using the uMFPU and I can add to floats together by using the following code:
    Main:
    SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA+1, ATOF, "123.123", 0, FSET]
    SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]ATOF, "100.100", 0, FADD]
    GOSUB Print_Float
    END

    I get the correct "223.223" result, however I get something like "1.4085" when I try to use the below code. That is, I take input from either an end-user or from a device such as a GPS.

    NUM1 VAR Byte (7)
    NUM2 VAR Byte (7)

    Main:
    DEBUG CR, "Please enter the first number to be added: "
    DEBUGIN Str NUM1\6

    DEBUG CR, "Please enter the second number to be added: "
    DEBUGIN Str NUM2\6

    SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA+1, ATOF, NUM1, 0, FSET]
    SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]ATOF, NUM2, 0, FADD]
    GOSUB Print_Float
    END

    If I provide "123.123" for NUM1 and "100.100" for NUM2, I get the result of "1.4085" for the above code.

    Am I using Byte wrong?

    Again help is greatly welcomed. I have to admit, I know very little about the uMFPU.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-29 00:46
    spaine -

    This may not be the whole problem, but how is SHIFTOUT supposed to know whether is should send out 1, 2, 3, 4, 5, or 6 bytes of data for NUM1 and NUM2? Perhpas a length specification would be appropriate?

    I know just about nothing about uFPU's but are you suppposed to be string or numeric data to it? Just something to consider.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • spainespaine Posts: 51
    edited 2006-08-29 15:26
    Thanks bruce. I actually found out that it was because I was sending a single Byte, and not the entire byte array. I should needed to update the code to shiftout each byte within the array:

    SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA+1, ATOF, NUM1(0), NUM1(1),..., NUM1(6), 0,
    FSET, ATOF, NUM2(0), NUM2(1),..., NUM2(6), 0, FADD]

    Again, thanks for the help. I'm having fun, but I have to admit there is something new to discover each day.
Sign In or Register to comment.