Shop OBEX P1 Docs P2 Docs Learn Events
Dang Math.....And Yes I tried Tracy's Site — Parallax Forums

Dang Math.....And Yes I tried Tracy's Site

GuidoGuido Posts: 195
edited 2008-01-06 21:12 in BASIC Stamp
As Usual I have a problem. I am trying to save variable space and trying to divide a Variable By 254 (1 little small Byte)
The problem I have is I need to get accuracy to then tenth.

Example:
Average·= Sample / 254

or

Average = 6799/254

I do not wish to use two word Variables, if possible for high and low Bytes

Any Help I would APPPPPPPREEEEEECIATE
JUST ME
GUIDO

Comments

  • Tracy AllenTracy Allen Posts: 6,667
    edited 2007-12-18 01:55
    Hi Guido,

    The divisor is a constant, 254.

    average = (Sample / 254 * 100) + (sample // 254 ** 25802)
    DEBUG DEC average/100,".",DEC2 average

    That gives average to 2 decimal places. You can then better round off to tenths.

    example: 6799/254=26.767716535433
    snippet above gives 26.76.

    good luck!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • GuidoGuido Posts: 195
    edited 2007-12-19 02:51
    Tracy!!
    Your the Man!!!
    Thanks
    Guido
    PS: Never did get the other problem solved....I think it is just toooooo MUch for one serout!
  • GuidoGuido Posts: 195
    edited 2008-01-01 00:10
    Tracy,

    I finally got around to trying your formula. I am still having problems.

    Sample would be a word Variable anywhere from 0 to maybe as much as 10,000 or more.

    When I plugged your formula in my Sample total was 3075 with a constant sample rate of 254.

    So:

    Example: 3076/254=12.110235



    Using your Example:

    average = (Sample / 254 * 100) + (sample // 254 ** 25802)

    My Debug showed 1.91

    I ahve one other question, when you insert 25802 in the end of you equation, What is this a Seed?

    Just Goofy Guido
  • wellman.ronwellman.ron Posts: 16
    edited 2008-01-01 17:19
    Guido said...
    Tracy,

    I finally got around to trying your formula. I am still having problems.

    Sample would be a word Variable anywhere from 0 to maybe as much as 10,000 or more.

    When I plugged your formula in my Sample total was 3075 with a constant sample rate of 254.

    So:

    Example: 3076/254=12.110235



    Using your Example:

    average = (Sample / 254 * 100) + (sample // 254 ** 25802)

    My Debug showed 1.91

    I ahve one other question, when you insert 25802 in the end of you equation, What is this a Seed?

    Just Goofy Guido





    Multply sample by 100 then divide by the constant. This works around the inability of the stamp to do floating point math.
  • Tracy AllenTracy Allen Posts: 6,667
    edited 2008-01-02 01:23
    Guido,

    Are you sure you have the parentheses around the two terms? I get an answer of 12.11 when I plug sample=3076 into
    average = (sample / 254 * 100) + (sample // 254 ** 25802)

    Actually, that is more than you need. I don't recall what I was thinking. All you need is,

    average = sample ** 25802
    DEBUG DEC average/100,".",DEC2 average
    



    That goes directly from sample=3076 to average=1211, which displays as 12.11. The formula works fine for all sample values from 0 to 10000, in fact, all the way up to 65535.

    Where does the 25802 come from? You need to multiply samples by 100 and divide by 254. You could do that directly so long as samples is less than 655, but it causes integer overflow for sample>655. To get around that, us the ** operator. Note that 100/254 = 25802/65536 (very close!). That is where the 25802 comes from. The ** operator multiplies times 25802 and then internally divides by 65536, but there is no problem with integer overflow. It works for all sample values even up to 65535.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • GuidoGuido Posts: 195
    edited 2008-01-02 21:58
    Tracy and Ron,

    ThankYou for your Help. I think I have finally figured this out. Just to make sure if I changed my sample rate from 254 to lets say 50, I would use 13107·instead of·25802?

    Using Ron's adivce I did come up with this and it seems to work

    Average·= (Sample * 100 /·254 ) +·(Sample //·254 ** 25802)

    Thanks

    Guido
  • Tracy AllenTracy Allen Posts: 6,667
    edited 2008-01-04 04:31
    The trouble with (Sample * 100 / 254) is that it won't work correctly if Sample > 655. That is because (Sample * 100) will overflow one word. In an earlier message you said that Sample might be as large as 10000.

    Did you try (average = sample ** 25802)? That should work for all values of Sample from 0 to 65535.

    Why are you taking 254 samples? Or why 50?

    You asked, "Just to make sure if I changed my sample rate from 254 to lets say 50, I would use 13107 instead of 25802?"
    No, that is not correct. If you take 50 samples, then
    average = sample * 2   '  <- times 2
    DEBUG DEC average/100,".",DEC2 average
    


    If you take 100 samples, then
    average = sample   ' Already exactly 100 times!
    DEBUG DEC average/100, ".", DEC2 average
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • GuidoGuido Posts: 195
    edited 2008-01-06 12:53
    Tracy,

    Everything that you have mentioned is 100% correct. I was thinking about a sample rate of 254, mainly for a Timing sequence (About 10 Minute Sample Rate). I have thought that maybe trying a smaller timing sequence may be better.

    Mostly looking for your wisdom, as I am trying to figure out how to do math using a basic stamp...I thought I had it, But NO Guido did not!

    Have patience for this poor Dumb Itialian!

    Thanks Again

    Guido

    I will try your example when I get a chance and thank You again!!
  • Tracy AllenTracy Allen Posts: 6,667
    edited 2008-01-06 21:12
    Guido, Di niente--Io non penso che lei
Sign In or Register to comment.