Decimal Math


Closed Thread
Results 1 to 4 of 4

Thread: Decimal Math

  1. #1
    eoasap's Avatar
    eoasap Guest

    Default Decimal Math

    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.

  2. #2
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    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
    Keith

    www.diyha.co.uk
    www.kat5.tv

  3. #3
    eoasap's Avatar
    eoasap Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by keithdoxey
    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?

  4. #4
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    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

Similar Threads

  1. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  2. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  3. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  4. 16bit variable and degrees conversion
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd May 2005, 17:27
  5. Doing Simple Math - getting the wrong answer
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2005, 14:27

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts