Algorithm assistance
Archiver
Posts: 46,084
Yes I know, this is a Basic Stamp forum......but I know some of you may be
able to help....I have also posted to the SX forum...many heads are better than
a few....
I could use some help with an algorithm. I already know how to get the end
result I want, but am looking for a more elegant (smarter programming)
algorithm.
My SX will end up with a 2 byte number (max value $270F / 9,999). I want to
strip the 3 most significant digits so I can output to 3 seven segment
displays a value of 9.99, based on 9,999, or 2.35 based on $936 / 2,350. (The
BS2sx
is not fast enough for my application,,,,hence the SX)
My current idea (and likely a bad one at that) is to do the following:
1. Test each bit of the low and high byte. If the bit is set add that value
(1, 2, 4, 8, 16 etc) to two separate variables (var1 & var2), in which case I
might end up with 9,999.
2. Divide var2 by 1000 which will give me my most significant digit
able to help....I have also posted to the SX forum...many heads are better than
a few....
I could use some help with an algorithm. I already know how to get the end
result I want, but am looking for a more elegant (smarter programming)
algorithm.
My SX will end up with a 2 byte number (max value $270F / 9,999). I want to
strip the 3 most significant digits so I can output to 3 seven segment
displays a value of 9.99, based on 9,999, or 2.35 based on $936 / 2,350. (The
BS2sx
is not fast enough for my application,,,,hence the SX)
My current idea (and likely a bad one at that) is to do the following:
1. Test each bit of the low and high byte. If the bit is set add that value
(1, 2, 4, 8, 16 etc) to two separate variables (var1 & var2), in which case I
might end up with 9,999.
2. Divide var2 by 1000 which will give me my most significant digit
Comments
One way to do that would require only subtraction of your two-byte
operands. On the SX you have a carry/borrow bit that you can check to
extend the subtraction to two bytes and to indicate if the result is
negative or positive.
Count the number of times you can subtract 1000 ($3E8) _before_ it
goes negative. That count is the most significant decimal digit.
Add back $3e8 depending on how you did it so the remainder is
positive and less than 1000. Now repeat but subtracting 100 ($64)
each time. That is the 100s digit. And then subtract 10s for your
last digit.
There are other methods,too. For example, divideby 10, four times
around, and each time the remainder is one of your decimal digits,
working from the least significant up. If you look on the Ubicom or
on the Microchip web sites, you will find some efficient division
algorithms. E.g.,
http://www.sxlist.com/techref/ubicom/lib/index.htm
-- best regards
Tracy Allen
electronically monitored ecosystems
http://www.emesystems.com
mailto:tracy@e...
>Yes I know, this is a Basic Stamp forum......but I know some of you may be
>able to help....I have also posted to the SX forum...many heads are
>better than
>a few....
>
>I could use some help with an algorithm. I already know how to get the end
>result I want, but am looking for a more elegant (smarter programming)
>algorithm.
>
>My SX will end up with a 2 byte number (max value $270F / 9,999). I want to
>strip the 3 most significant digits so I can output to 3 seven segment
>displays a value of 9.99, based on 9,999, or 2.35 based on $936 /
>2,350. (The BS2sx
>is not fast enough for my application,,,,hence the SX)
>
>My current idea (and likely a bad one at that) is to do the following:
>
>1. Test each bit of the low and high byte. If the bit is set add that value
>(1, 2, 4, 8, 16 etc) to two separate variables (var1 & var2), in which case I
>might end up with 9,999.
>2. Divide var2 by 1000 which will give me my most significant digit’Ķ.9
>3. Multiply the most significant digit (9) by 1000, and subtract this value
>from var1. var1 will now hold 999.
>4. Move var1 into var2, (both var1 & var2 now hold 999).
>5. Divide var2 by 100 which will give me my next signifcant digit 9.
>6. Multipy this next significant digit (9) by 100 and subtract this value
>from var1. var1 will now hold 99
>7. Continue until I have each desired digit.
>8. Sending the three most significant digits out to my 7 seg displays I can
>easily handle.
>
>There must be a more elegant way to do this??? No such thing as a "modulus"
>operator, right’Ķ..or the algorithm is doing just that???
>
>Any thoughts appreciated.
>
>Not looking for code....just the algorithm.
>
>Ken
>
tracy@e... writes:
Thank you very much.....
Ken
> Hi Ken,
>
> One way to do that would require only subtraction of your two-byte
> operands. On the SX you have a carry/borrow bit that you can check to
> extend the subtraction to two bytes and to indicate if the result is
> negative or positive.
>
> Count the number of times you can subtract 1000 ($3E8) _before_ it
> goes negative. That count is the most significant decimal digit.
> Add back $3e8 depending on how you did it so the remainder is
> positive and less than 1000. Now repeat but subtracting 100 ($64)
> each time. That is the 100s digit. And then subtract 10s for your
> last digit.
>
> There are other methods,too. For example, divideby 10, four times
> around, and each time the remainder is one of your decimal digits,
> working from the least significant up. If you look on the Ubicom or
> on the Microchip web sites, you will find some efficient division
> algorithms. E.g.,
> http://www.sxlist.com/techref/ubicom/lib/index.htm
[noparse][[/noparse]Non-text portions of this message have been removed]