Shop OBEX P1 Docs P2 Docs Learn Events
BS2sx Stamp won't perform division calculations — Parallax Forums

BS2sx Stamp won't perform division calculations

I'm programming in PBasic 2.5
The stamp is a BS2sx

I can't get the stamp to perform division.
For example, the following code returns 0 value.
Debug DEC 50/3, CR
The stamp performs and outputs addition, subtraction, and multiplication perfectly.
Using actual decimal values or variables makes no difference.

Any ideas?

Comments

  • BASIC Stamps do not do floating point math, hence the result of that calculation is 16 (50 divided by 3 is 16, with a remainder of 2).

    You can increase your resolution and do something like this
      value = 500
      DEBUG DEC value / 30, ".", DEC (value // 30) / 3, CR
    
    You must define value as a Word variable. Note that we've scaled it up by 10 so that we can derive a resolution of 10ths. This will print out 16.6 -- which is closer to 16.667 than 16.
  • Thanks, I'll give that a try.
  • Regardless, DEBUG DEC 50/3, CR should print 16, not 0.

    -Phil
  • Phil is correct, of course. I tried it with a BS2sx and it printed the expected 16.

    Either the OP has a bad chip or mis-observed.
  • JonnyMac, thanks for your response above.
    In this line, DEBUG DEC value / value1, ".", DEC (value // value1) / 3, CR
    Why divide (value // value1) by 3?
    The line returns a value, but it isn't correct.
    In my actual program, value is smaller than value1, so the answer is always less than 1.
    Since the Stamp is limited in it's ability to perform mathematical calculations, would you recommend the propeller?
    Thanks
  • Why divide (value // value1) by 3?
    Because the resolution is tenths, and that section is handling the 10ths digit.

    The Propeller is also integer math, but with a lot of room to work using signed 31-bit values. And, if you want, you can use a floating point co-processor.

    The Propeller opens up a whole host of features that one cannot match with a BASIC Stamp.

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-01-23 06:21
    Am I imagining things, or have some previous posts gone missing from this thread? 'Seems like there was a lot more context for some of the later comments.

    -Phil
  • Am I imagining things, or have some previous posts gone missing from this thread? 'Seems like there was a lot more context for some of the later comments.

    -Phil
    Everything I remember seeing in this thread is still there.

  • PublisonPublison Posts: 12,366
    edited 2017-01-23 17:53
    Am I imagining things, or have some previous posts gone missing from this thread? 'Seems like there was a lot more context for some of the later comments.

    -Phil

    Phil, I went through the change log, and it don't see that anything was deleted.

  • Thanks
    I may have to move to the propeller for this project since the stamp doesn't seem to handle this simple calculation.
    I was hoping I could do this simple project with the Stamp. I've not figured out to get the propeller to cooperate with me at all.
    I think all the posts are there. At least the ones I've seen.
  • Perhaps you ought to explain the "simple project" before condemning the BASIC Stamp. Clearly, you don't have a lot of experience with it, but there are many of us here who do and would happily help you if we knew what you were attempting.
  • All of these pic controllers will only do integer math. They only really perform addition and subtraction. Routines are written to convert it to complex math functions. So if you want to run the basic Stamp 2 you can buy a math coprocessor like a Pak-I or Pak-Ii from AWC. Parallax used to sell one. Even with the Propeller you have to use the float math and simple numbers objects. First you will convert your number to a floating point and float to a string and display it as a string STR rather than a DEC.
    Cut and paste your basic code so everyone can look at it.

  • djkintexas wrote: »
    I'm programming in PBasic 2.5
    The stamp is a BS2sx

    I can't get the stamp to perform division.
    For example, the following code returns 0 value.
    Debug DEC 50/3, CR
    The stamp performs and outputs addition, subtraction, and multiplication perfectly.
    Using actual decimal values or variables makes no difference.

    Any ideas?

    You know, this entire thread is based on a false premise. Namely that
      DEBUG DEC 50/3
    
    returns the value 0. It does not. On my BS-2sx, it returns the value 16, just as expected. If it *does* return any other value, then the OP has a bad device.

  • My BS2 returns 16. However if you leave out the DEC (Debug 50/3) you will get a square character that could look like a zero.
  • JonnyMacJonnyMac Posts: 8,924
    edited 2017-01-29 04:23
    DEC is a formatting control code which converts a number to its ASCII value for viewing in terminals. Your inserting the division into the debug output. In actual practice you would do something like this
      result = 50  / 3
    
    At this point, result holds 16. To display result on a text terminal, we have to convert it to its text representation with DEC
      DEBUG DEC result, CR
    
    If you change DEC to HEX, the output will be 10. If you change that to IHEX (indicated hex) you will get $10. $ is the indicator Parallax uses for hex values. Try BIN and IBIN, too. You see, the value hasn't changed, it's how you're formatting it on output.

Sign In or Register to comment.