Shop OBEX P1 Docs P2 Docs Learn Events
BS2 and Math — Parallax Forums

BS2 and Math

MichelBMichelB Posts: 154
edited 2009-06-23 15:23 in BASIC Stamp
Hi, I found in Tracy Allen's app-notes two interesting things, i) how to multiply X times PI and ii) how to display decimals, but my code (attached) does not run as expected. Where is my error?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-22 16:15
    Are you expecting to see x*pi to two decimal places? It doesn't work that way. The calculation is done using integer arithmetic and there are no decimal places.

    Take out the "y = y * 100", then try x = 100. You should see "3.14" on the DEBUG window.
  • MichelBMichelB Posts: 154
    edited 2009-06-23 07:01
    Yes Mike, but 100 x PI = 314.16 not 3.14, how to do to have the correct answer?

    Post Edited (MichelB) : 6/23/2009 8:00:07 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-23 14:57
    The formula that Tracy gave is correct (Y = (3 * X) + (9280 ** X)). As I mentioned, it gives an integer result.

    Your program then attempts to get two decimal places by multiplying by 100 (Y = Y * 100). That can't work the way you want.

    When you display the result using two "decimal places", all you get is zeroes.

    So, the simple answer to "how to have the correct answer?" is "you can't".

    For you, what's "the correct answer"? If you just want two decimal places, you can scale X as I suggested so that X is actually in units of 0.01.
    To get Pi * 1, you use X = 100. To get Pi * 2, you use X = 200. You can't go much beyond that because of the 16 bit word size.
  • MichelBMichelB Posts: 154
    edited 2009-06-23 15:23
    OK Mike, well understood, thank you.
    P.S.: modified code attached.

    Post Edited (MichelB) : 6/23/2009 3:36:38 PM GMT
Sign In or Register to comment.