Simple math
Archiver
Posts: 46,084
I am working with the following program:
'{$STAMP BS2}
a var byte
b var byte
x var byte
a=23
b=95
'x=b/a
x=x*100
debug "result: ", dec x
After running my debug result is "4" which looks OK.
If I change the program to:
'{$STAMP BS2}
a var byte
b var byte
x var byte
a=23
b=95
x=b/a
x=x*100
debug "result: ", dec x
My debug result is "144" ?
Why is this? I wanted to get "413".
thanks for your help,
Leroy
'{$STAMP BS2}
a var byte
b var byte
x var byte
a=23
b=95
'x=b/a
x=x*100
debug "result: ", dec x
After running my debug result is "4" which looks OK.
If I change the program to:
'{$STAMP BS2}
a var byte
b var byte
x var byte
a=23
b=95
x=b/a
x=x*100
debug "result: ", dec x
My debug result is "144" ?
Why is this? I wanted to get "413".
thanks for your help,
Leroy
Comments
Not sure why you are getting 4 on the first one since X is not set at
all before the multiply.
However, on the second one, don't forget that the Stamp does integer
math.
So x=b/a results in 4. Then 4*100 should equal 400, but since you have a
byte variable you are getting 400-256 = 144 (overflow!).
So if you change x to a word, that would at least give you 400 instead
of 144. However, a better way to compute it might be:
X=b*100/a
For the numbers supplied that would give you 413. The problem is you
must make sure that b is never >655 since the intermediate result can't
go above 65535.
If you need better math, check out our PAK-I, II, or IX.
Al Williams
AWC
* Floating point math for the Stamp, PIC, SX, or any microcontroller
http://www.al-williams.com/awce/pak1.htm
> '{$STAMP BS2}
> a var byte
> b var byte
> x var byte
> a=23
> b=95
> x=b/a
> x=x*100
> debug "result: ", dec x
>
> My debug result is "144" ?
>
> Why is this? I wanted to get "413".
>
>
>
> thanks for your help,
>
> Leroy
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed. Text in the
> Subject and Body of the message will be ignored.
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>I am working with the following program:
>
>'{$STAMP BS2}
>a var byte
>b var byte
>x var byte
>a=23
>b=95
>'x=b/a
>x=x*100
>debug "result: ", dec x
>
>After running my debug result is "4" which looks OK.
>
>If I change the program to:
>
>'{$STAMP BS2}
>a var byte
>b var byte
>x var byte
>a=23
>b=95
>x=b/a
>x=x*100
>debug "result: ", dec x
>
>My debug result is "144" ?
>
>Why is this? I wanted to get "413".
>
>
>
>thanks for your help,
>
>Leroy
>
>
Hi Leroy,
I believe that the problem is that a byte variable can only be a
value of 0-255, so the multiplied value of x cannot be correctly
displayed. Try changing your definition of x to a word variable and I
think the problem should be solved.
Tim
Timothy Medema
CrystaLite, Incorporated
3307 Cedar St. (425) 745-6000 800-666-6065
Everett, WA 98201 Fax: (425) 257-0232
www.crystaliteinc.com
<mailto:timm@c...>timm@c...
The information transmitted is intended only for the person or entity to
whom it is addressed and may contain confidential and/or privileged
material. Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited. If you received
this in error, please contact the sender and delete the material from any
computer.
>I am working with the following program:
>
>'{$STAMP BS2}
>a var byte
>b var byte
>x var byte
>a=23
>b=95
>'x=b/a
I'm going to assume you meant to comment out x=x*100 instead of the line above.
You haven't defined X to this point so you better insert
x = 0
before continuing. Maybe the Stamp initializes variables to zero, but I
think you were just lucky.
>x=x*100
>debug "result: ", dec x
>
>After running my debug result is "4" which looks OK.
>
>If I change the program to:
>
>'{$STAMP BS2}
>a var byte
>b var byte
>x var byte
>a=23
>b=95
>x=b/a
>x=x*100
>debug "result: ", dec x
>
>My debug result is "144" ?
>
>Why is this? I wanted to get "413".
Your variable X is a byte and the Stamp does integer math so you'll never
see any result over 255 decimal with this approach.
Read the sections of the manual titled "Runtime Math and Logic," "Unary
Operators," and "Binary Operators." They're listed in the Table of
Contents and start on page 231 in my hard copy Ver 1.8 manual.
Imagine all the math being done in binary integers and any extra bits
beyond 8 are tossed out at EACH step, not just at the end. Looking at it
this way, 95/23 = 4, not 4.13.
Then when you multiply the resulting 4 by 100, the expected (not the
actual) result is 400, which looks like 110010000 in binary. You're still
using an 8-bit byte, so toss away that 9th bit on the left and you're left
with 10010000 which is decimal 144.
In simpler terms that most scientific calculators should be able to handle,
the Stamp math with byte variables for 4*100 is done as (4*100) mod 256,
which is 144 - the answer you got.
You could use a word-sized variable for X since the "/" operator gives a
16-bit result, but remember the Stamp still does integer math so that first
step of 95/23 still gives 4, not 4.13. But at least your final answer of
400 is closer to 413 than 144 is.
Now, if you know the range of values for your input and know none of the
intermediate steps will overflow a word sized variable, you can rearrange
your calculation something like this:
'{$STAMP BS2}
a var byte
b var byte
x var word
a=23
b=95
x=100*b/a
debug "result: ", dec x
Now the answer is 413 as you wanted.
Read that manual.
Jim H
to word and the program seems to work. Guess I am just Lucky!!
Leroy
Jim Higgins wrote:
>
> At 09:04 04/18/02, Leroy Hall wrote:
>
> >I am working with the following program:
> >
> >'{$STAMP BS2}
> >a var byte
> >b var byte
> >x var byte
> >a=23
> >b=95
> >'x=b/a
>
> I'm going to assume you meant to comment out x=x*100 instead of the line
above.
>
> You haven't defined X to this point so you better insert
> x = 0
> before continuing. Maybe the Stamp initializes variables to zero, but I
> think you were just lucky.
>
> >x=x*100
> >debug "result: ", dec x
> >
> >After running my debug result is "4" which looks OK.
> >
> >If I change the program to:
> >
> >'{$STAMP BS2}
> >a var byte
> >b var byte
> >x var byte
> >a=23
> >b=95
> >x=b/a
> >x=x*100
> >debug "result: ", dec x
> >
> >My debug result is "144" ?
> >
> >Why is this? I wanted to get "413".
>
> Your variable X is a byte and the Stamp does integer math so you'll never
> see any result over 255 decimal with this approach.
>
> Read the sections of the manual titled "Runtime Math and Logic," "Unary
> Operators," and "Binary Operators." They're listed in the Table of
> Contents and start on page 231 in my hard copy Ver 1.8 manual.
>
> Imagine all the math being done in binary integers and any extra bits
> beyond 8 are tossed out at EACH step, not just at the end. Looking at it
> this way, 95/23 = 4, not 4.13.
>
> Then when you multiply the resulting 4 by 100, the expected (not the
> actual) result is 400, which looks like 110010000 in binary. You're still
> using an 8-bit byte, so toss away that 9th bit on the left and you're left
> with 10010000 which is decimal 144.
>
> In simpler terms that most scientific calculators should be able to handle,
> the Stamp math with byte variables for 4*100 is done as (4*100) mod 256,
> which is 144 - the answer you got.
>
> You could use a word-sized variable for X since the "/" operator gives a
> 16-bit result, but remember the Stamp still does integer math so that first
> step of 95/23 still gives 4, not 4.13. But at least your final answer of
> 400 is closer to 413 than 144 is.
>
> Now, if you know the range of values for your input and know none of the
> intermediate steps will overflow a word sized variable, you can rearrange
> your calculation something like this:
>
> '{$STAMP BS2}
> a var byte
> b var byte
> x var word
> a=23
> b=95
> x=100*b/a
> debug "result: ", dec x
>
> Now the answer is 413 as you wanted.
>
> Read that manual.
>
> Jim H
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed. Text in the Subject and Body
of the message will be ignored.
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/