lsp = (lrt + lft) / tcw
Archiver
Posts: 46,084
In the program below, why does 'lsp' print nothing with the debug
statement? Thanks for your help.
Leroy
'{$STAMP BS2}
lft var word
rft var word
rrt var word
lrt var word
tcw var word
lsp var word
fwp var word
rwp var word
cwp var word
lft=121
rft=132
rrt=120
lrt=118
tcw=lft+rft+rrt+lrt
lsp = (lrt + lft) / tcw
debug "percent: ", dec lsp
debug cr
statement? Thanks for your help.
Leroy
'{$STAMP BS2}
lft var word
rft var word
rrt var word
lrt var word
tcw var word
lsp var word
fwp var word
rwp var word
cwp var word
lft=121
rft=132
rrt=120
lrt=118
tcw=lft+rft+rrt+lrt
lsp = (lrt + lft) / tcw
debug "percent: ", dec lsp
debug cr
Comments
than 1, hence you get zero. A simple way to solve your dilemma is to
multiply the multiplicand (top value) by 100 before the division, then you'll
get what you're looking for.
You just need to be careful that your multiplicand never exceeds 655
otherwise the multiplying by 100 will cause a 16-bit rollover error.
-- Jon Williams
-- Parallax
In a message dated 4/18/02 11:30:17 AM Central Daylight Time, leroy@f...
writes:
> In the program below, why does 'lsp' print nothing with the debug
> statement? Thanks for your help.
>
> Leroy
>
>
> '{$STAMP BS2}
> lft var word
> rft var word
> rrt var word
> lrt var word
> tcw var word
>
> lsp var word
> fwp var word
> rwp var word
> cwp var word
>
> lft=121
> rft=132
> rrt=120
> lrt=118
>
> tcw=lft+rft+rrt+lrt
>
> lsp = (lrt + lft) / tcw
> debug "percent: ", dec lsp
> debug cr
>
[noparse][[/noparse]Non-text portions of this message have been removed]
>In the program below, why does 'lsp' print nothing with the debug
>statement? Thanks for your help.
>
>Leroy
>
>'{$STAMP BS2}
>lft var word
>rft var word
>rrt var word
>lrt var word
>tcw var word
>
>lsp var word
>fwp var word
>rwp var word
>cwp var word
>
>lft=121
>rft=132
>rrt=120
>lrt=118
>
>tcw=lft+rft+rrt+lrt
>
>lsp = (lrt + lft) / tcw
>debug "percent: ", dec lsp
>debug cr
Does it print nothing, or does it print zero. They aren't the same
thing. The result is zero in Stamp math.
If you want a percentage, then the last few lines should be:
lsp = lrt + lft*100/tcw
debug "percent:", dec lsp
debug cr
Be careful with this if your starting values get much larger since one of
the intermediate values was 25,300. Your 16-bit limit is 65,535.
Also remember, the Stamp does math from left to right so the above is
equivalent to the algebraically correct expression:
lsp = ((lrt+lft)*100)/tcw
A trick you might try. If you get answers of zero unexpectedly, toss in a
10X or 100X multiplier in the first term of one of your calculations before
the last one and see what you get then. It will help you find integer
underflow (value < 1) problems.
Jim H
> > rft=132
> > rrt=120
> > lrt=118
> >
> > tcw=lft+rft+rrt+lrt
> >
> > lsp = (lrt + lft) / tcw
> > debug "percent: ", dec lsp
Hi Leroy
For most accurate results in any percent problem (where the
denominator is greater than the numerator), use the bitwise inversion
formula. In this case...
N var word ' word for calculation
F var word ' result of calculation
J var nib ' index for calculation
tcw=lft+rft+rrt+lrt ' denominator
N = lrt+lft ' numerator
for J=15 to 0 ' index, nib
N=N//tcw<<1 ' remainder*2
F.bit0(J)=N/tcw ' next bit of word F
next
' done, fraction is F/65536
F=F**10000 ' normalize to decimal
debug dec F/100,".",dec2 F,cr
The percent is calculated to 4 significant figures, for any denominator<32768.
-- regards,
Tracy Allen
electronically monitored ecosystems
mailto:tracy@e...
http://www.emesystems.com