Shop OBEX P1 Docs P2 Docs Learn Events
Math Problem — Parallax Forums

Math Problem

geneshultsgeneshults Posts: 22
edited 2011-03-03 09:44 in BASIC Stamp
I'm having a problem with math in the BS2.
I have a value I want to multiply by a value with a decimal point in it.
Value times 2.55. BS2 editor doesn’t like the decimal point. What do I have to do to make this work??

Thanks
Gene

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-02 21:16
    If the number you're trying to multiply has a value less than 256, you can multiply it by 255 then divide by 100. If it can be bigger than 256, things get a little more complicated, and you will have to call in the big guns -- i.e. Tracy Allen. :)

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-02 21:45
    The Stamps do all their arithmetic in 16-bit integers, so you really can't have decimal points. What's done (as Phil implied) is scaling. If you want to multiply by 2.55, what you really do is multiply by 255/100 and do the two operations separately. The result is still limited to an integer. You also have to be careful that the result and all intermediate results will fit into 16 bits, hence the limitation of 255.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-03-03 09:44
    The Stamp can approximate the fraction 0.55 as either 0.55 = 141 / 255 or for better accuracy as 0.55 = 36045 / 65536. That makes use of the "big gun" */ or ** operators you can read about in the manual.
    By the same logic, 2.55 = 653 / 256.
    y = x */ 653     ' same as multiplying x * 2.55 or x * 653/256.
    
    or for more accuracy,
    
    y = (x * 2)  +  (x ** 36045)
    
Sign In or Register to comment.