Shop OBEX P1 Docs P2 Docs Learn Events
Decimal Math Help — Parallax Forums

Decimal Math Help

Dan CroninDan Cronin Posts: 5
edited 2005-12-06 13:41 in BASIC Stamp
I'm new to this forum and PBASIC.· I'm a little stumped on how to do some decimal math and the best way to do it in PBASIC with a BS2.· Can anyone give me a tip on how to do the following equation and/or point me in the direction of some decimal math tutorials?· Thanks in advance.

Dan

a = (n / 2048) * 5

For example I have a value of 1224 for n so the answer I need is 2.988

2.988 = (1224 /2048) * 5

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-12-05 20:54
    Other BS math gurus will probably be more help, but you question isn't easily answered. The closest operator to what you are looking for is */ which multiplies an 8 bit integer by an 8 bit fraction. The 2048 takes 11 bits to store, so */ won't work. I would do it by first doing n * 5 in a word sized variable, then make a copy of the value and shift it right by 11, this is the integer portion. Make another copy and shift it left by 5, this is the frational portion where the msb is 1/2 and lsb is 1/(2^16). I may be off by one on the 11 and 5 shift by values, doing some quick calculations should verfiy if it is correct or not, but the sum of the two should be 16.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • NewzedNewzed Posts: 2,503
    edited 2005-12-05 20:58
    a = (n / 2048) * 5

    For example I have a value of 1224 for n so the answer I need is 2.988

    2.988 = (1224 /2048) * 5

    Try this:

    n*10 = 12240/(2048/10) = 12240/204 = 60
    (60/10) = 6 * 5 = 3
    3000/2988 = .4% error

    Is that close enough?


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-12-05 21:11
    If you will download the division program I just posted under "Division with a Stamp", ·that program will give you an answer to 1224/2048 of .60.· Multiply that answer by 5 and you get 3.00, which is what I just posted for you.

    Sid
  • Dan CroninDan Cronin Posts: 5
    edited 2005-12-05 21:25
    Thanks Sid.· That was VERY helpful and got me over the hump for now.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-12-05 22:01
    Just one note, Sid's solution provides an approximate answer, the one I outlined is an exact solution (it provides an answer of 2.98828125), but at a greater complexity of computation. It depends on your application on whether an approximate answer is sufficient, if it isn't and you want further explanation of the exact solution, ask.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Dan CroninDan Cronin Posts: 5
    edited 2005-12-05 22:22
    Thanks for your reply also Paul.· I think for now an approximate answer is sufficient.· The sample formula I posted was only the first part of multiple steps and I'm trying to make it as simple as possible on my little brain! tongue.gif
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-12-06 01:41
    >> a = (n / 2048) * 5
    >> e.g. 2.988 = (1224 /2048) * 5

    Hi Dan,

    Here is another way to do it. Paul mentioned the */ operator, and also there is the ** operator, both of which are the Stampese way of doing fractions. Since the denominator here is 2048, an exact power of two, either one will work with good accuracy. Here it is using */.

     n var word  ' NOTE 0<=n<=13107  due to *5 in formula
      a var word
      DO
      debugin dec n   ' type in a value from 0 to 13107
      a = n * 5 */ 1250
      debug dec a/10000,".",dec4 a
      LOOP
    



    Print "2.9882", when n=1224. The value of a is 29882.

    Fractional multiply implicitly divides by 256, so on a normal calculator you can look at it like this:

    a = n * 5 * 1250/256 = n * 5 * (8*1250)/(8*256) = n * 5 * 10000 / 2048

    If you want a tutorial, you might peruse my math pages at
    www.emesys.com/BS2index.htm#math

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Dan CroninDan Cronin Posts: 5
    edited 2005-12-06 04:46
    Thanks for all the tips.· You guys are good.· If you want a challenge here is the rest of the formula I’m working with.· Using my sample input value of 1224 and a temperature variable of 61 degrees the result I should come up with in the end is a humidity percentage of 73%.· My code comes in at 71% due to some rounding errors.· This is probably close enough for my project but if you experts can hit 73% I would love to see the code and get educated.
    ·
    n = 1224···· ‘my sample input
    t = 61········ ·‘sample temperature
    h = 73%····· ‘ the end result humidity value using the input values above
    ·
    a = (n/2048) * 5········ ··········‘ a=(1224/2048)*5 = 2.988
    b = (a-.8)/.31···· ·················‘ b= (2.988-.8)/.031 = 70.6% Humidity
    h = b/(1.093-0.0021*t)··· ·····‘ h= 70.6% / (1.093-0.0021*61) = 73% Humidity adjusted for temperature
    ·
    I just broke it up so you can see what I’m doing.· I don’t need to hold on to the “a” and “b” values.· I just need to solve for h.· I suppose the one line formula would be:
    ·
    h = ((((n/2048) * 5)-.8)/.31)/(1.093-0.0021*t)
    ·
    Somewhat of a daunting task for a Stamp rookie.
    ·
    Dan
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2005-12-06 05:36
    Dan,

    I would suggest going through Tracy's pages. It's probably the single best resource for learning how to correctly use these operators to accomplish your goals...

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-12-06 06:46
    That looks an awful lot like the formula for the Honeywell HIH36XX!? I can give you an "earful" about that, concerning the math and the temperature correction formula itself.

    www.emesys.com/OL2rhtd.htm

    BTW, I think there is a typo in the formula in HIH3610 data sheet, because the formula there disagrees with the formula in the HIH3602 data sheet (but the two Celsius formulae _do_ agree.) It should be (1.093-0.0012*t). It is supposed to be a linearization around 25 Celsius (77 Fahrenheit). {Note that (1.093-0.0012*t)=1.000 when t=77.5 Fahrenheit.}

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Dan CroninDan Cronin Posts: 5
    edited 2005-12-06 13:41
    Hi Tracy,

    I believe you are correct and it is a Honeywell HIH-3610 Series. I'll check out your link, looks like you have lots of info and have been done this road already.

    Thanks,
    Dan
Sign In or Register to comment.