PDA

View Full Version : Decimal Math



eoasap
- 17th April 2006, 10:47
I'm having a hard time doing a calculation.

I have two ascii numbers "10" and "4715"
which represent the left and right side of "10.4715"

i need to convert this to decimal and divide this result by 60, which should give a result of 0.174525

Any ideas on how to approach this?
I can probably drop the 4th digit of precision on the decimal if need be.

keithdoxey
- 17th April 2006, 12:18
PBP only works with Integers not floating point.

what is the biggest number you would ever end up using ?

Using your example without the decimal point you would have 104717 which is too big for a word (65535).

If you could lose the least significant digit then you would have 10471 (or you could choose to make it 10472 based on the digit you are losing.

Remember that you only had 3 digits after the point.

Divide that by 60 and you get 174 which will equate to 0.174

Divide by 6 instead and you get 1745 and can now allow for 4 digits after the point as you had already reduced your divisor by a factor of 10 so that will equate to 0.1745

Is that accurate enough for your needs ?

Failing that, search the forum for "Floating Point"

Melanie will no doubt offer a far better solution shortly due to her much greater knowledge of solving problems with PBP :)

Regards

eoasap
- 17th April 2006, 12:42
PBP only works with Integers not floating point.

what is the biggest number you would ever end up using ?

Using your example without the decimal point you would have 104717 which is too big for a word (65535).

If you could lose the least significant digit then you would have 10471 (or you could choose to make it 10472 based on the digit you are losing.

Remember that you only had 3 digits after the point.

Divide that by 60 and you get 174 which will equate to 0.174

Divide by 6 instead and you get 1745 and can now allow for 4 digits after the point as you had already reduced your divisor by a factor of 10 so that will equate to 0.1745

Is that accurate enough for your needs ?

Failing that, search the forum for "Floating Point"

Melanie will no doubt offer a far better solution shortly due to her much greater knowledge of solving problems with PBP :)

Regards


Thanks Keith! I actually have it working for 5 digits, but would really like to use the 6th digit. the highest the number would be is 599,999. The problem is, in order to add the 6th digit i'd need to do something like this:


word = word
tmp = byte

for i = 0 to 5
word = word*10 + tmp[i]
next i

where i'm storing it in a word, and adding tmp[i]. Of course, this works fine up until i exceed a valid word on the last iteration.

If i didn't need to add tmp[i] (the 6th digit) i could just directly use DIV32 with no worries, but adding the tmp[i] term would not allow me to use DIV32 since the DIV32 needs to execute immediately after the multiplication, right?

paul borgmeier
- 18th April 2006, 10:08
This should keep your 4 decimal places

'**********

A VAR BYTE ' 0 to 59 (10 for your example)
B VAR WORD '1000 to 9999 (4715 for your example) must be 4 digits
X VAR BYTE
DIGIT VAR BYTE[4]

FOR X = 0 TO 3
DIGIT[X] = B DIG X
NEXT X

FOR X = 3 TO 0 STEP -1
A = A * 10 + DIGIT[X]
DIGIT[X]= 0
Here:
IF A >= 60 THEN
A = A – 60
DIGIT[X] = DIGIT[X] + 1
GOTO Here
ENDIF
NEXT X

B = DIGIT[3]*1000+Digit[2]*100+Digit[1]*10+Digit[0] ' always 4 digits
IF A > 29 THEN B = B + 1

'**********

Try
10.4715
A=10, B = 4715

B = 1745
(Answer = .1745)

Try
1.1
A = 1
B = 1000

B = 183
(Answer = .0183)

Try
59.9999
A=59
B=9999

B=10000
(Answer =1.0000)

Untested but works on paper. If you are not always dividing by 60 then change to variable and calculate (A / denominator) first in a similar manner. If your values are going to change a lot then work in binary or hex with a fixed point math approach (google "fixed point math division" for lots of algorithms).

Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA