Shop OBEX P1 Docs P2 Docs Learn Events
getting some strange output for subtraction... why? — Parallax Forums

getting some strange output for subtraction... why?

darshanrtdarshanrt Posts: 4
edited 2007-10-18 01:58 in BASIC Stamp
hello everyone,

first of all thanks aton for your previous replys. I have one more problem if any one of you can help me with it..!!

let me explain it with example:

I have two inputs one is in_1 = 2.10 and·in_2 =·1.90. now as I am using basic stamp i did in_1 = 210/100 and in_2 = 190/100. and I am expecting my result but some strange answer coming like "1"··smhair.gif·. can you tell me how can I get the answer as 0.2 on the screen..??

I am new to basic stamp language so pls bare some silly questions from my side. thanks once again.

regards,
Darshan

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-10-17 22:12
    Darshan,

    The BASIC Stamp only knows integers, so you have to keep your variables in whole integer form at all times. Here's how you might display your result as a decimal. It displays the integer part first, then the decimal point, then the remainder from dividing by 100 as a two-digit number:

    in_1 = 210
    in_2 = 190
    DEBUG DEC (in_1 - in_2) / 100,  ".", DEC2 (in_1 - in_2) % 100, CR
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 10/18/2007 2:24:28 AM GMT
  • darshanrtdarshanrt Posts: 4
    edited 2007-10-18 00:27
    hey phil,

    thanks.. even I was thinking the same way but you cleared my all doubts... except one... now what if I want to use them (in_1, in_2 and even the result) will they be the carried fwd as 2.10, 1.90 and 0.20...??

    again bare my stupid questions...!!

    Thanks aton,
    Regards,
    Darshan
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-18 01:58
    No. The values are really 210, 190, and 20. You can continue doing fixed point arithmetic though, carrying the implicit decimal point forward. There's a good discussion of fixed point arithmetic on the Stamp here: www.emesystems.com. This site contains a lot of excellent advice, examples, and discussion for using a Stamp and attaching it to all sorts of sensors, information on Stamp speed, etc.
Sign In or Register to comment.