Calculation Problem - value goes to zero after 65


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi TOM,

    I think it's overflowing in this statement

    Dec_speed = (Dec_speed*115)/100

    This shoud work better

    Dec_speed = Dec_speed*115
    Dec_speed = DIV32 100

    HTH,
      Darrel

  2. #2
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    If you want to speed it up and add some accuracy you could use....

    Dec_speed = Dec_speed + (Dec_speed ** 9882)

    ..... this will give you 1kn = 1.1507873mph which is darn close to the 1.15078030303 it should be.

    /Ingvar

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That's good Ingvar, got me thinking.

    But, since we're going for accuracy too.

    1 Nautical Mile = 6076.11549 Feet
    1 Mile = 5280 Feet

    So the conversion is 1.150779449 (6076.11549/5280), and the number we need is [0.150779449 * 65536] or 9881.482. It's closer to 9881 than 9882

    But the biggest problem is with the integer nature of the calculation. Most of the added accuracy is lost in the Low Word of the ** multiplication..

    For instance, at 45.0 knots, the MPH is 51.78 MPH (45 * 1.150779449) which is closest to 51.8 MPH.
    However, either of our previous formula's will only show 51.7 MPH.

    Here's another way that does the same thing as the **, but also allows you to round the result according to the low Word of the multiplication.
    Code:
    Dummy = Dec_speed * 9881
    Dummy = R0                               ; Get High Word of Multiply result
    IF R2 > 32768 then Dummy = Dummy + 1     ; Round to nearest .1 MPH
    Dec_speed = Dec_speed + Dummy
    This will hold the result to +/- 1/2 Least Significant Digit.

    Best regards,
       Darrel
    Last edited by Darrel Taylor; - 28th June 2005 at 18:23.

  4. #4
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Talking

    Hi Darrel,

    Isn't it wonderful when we try to improve the accuracy with about half a ppm .... and the world has hardly descided how long a nautical mile really is. I got "1 knot = 1.15078030303 mph" from http://www.geocities.com/TheTropics/...311/ktmph.html , but after doing a little more digging around, i now agree with you. It shows that you can't really trust anything you find on the net.

    BTW, i really like the way you do the rounding, i can imagine that beeing speedy in assembler ....... i just might steal it sometime ;-)

    Just to be a pain, i'll show how i'd do this calculation with rounding(and adding that extra little accuracy). Perhaps someone will find it useful.

    Dec_speed = ((Dec_speed */ 2946) + 5) / 10

    It's probably a little slower than your method since it involves a division.

    /Ingvar

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ingvar,

    OK, now we're getting silly, but I just can't resist.

    Same thing as before, With Rounding, less code.
    Code:
    Dummy = Dec_speed * 9881
    Dec_speed = R0 + R2.15 + Dec_speed
    Saved 30 Words in the process.

    OK, I'll quit now,
       Darrel

  6. #6
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Talking Reducing even further .....

    While were on the task of reducing and optimizing Toms code, i feel a strong urge to reduce .......
    SSD4= (ssmax[18]>>4)
    SSD3= (ssmax[18] & $f)
    SSD2= (ssmax[17]>>4)
    SSD1= (ssmax[17] & $f)

    Then convert to Decimal:
    Dec_speed=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1 ' convert to dec
    ..... into .....
    Code:
    Dec_speed.HighByte = ssmax[18]
    Dec_speed.LowByte = ssmax[17]
    ...... thus saving time, codespace and ram ... you name it.

    How about it Darrel, want a shot at it ;-)

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Good eye Ingvar,

    I didn't even notice that he was Ripping them apart, just to re-assemble them back exactly like they were. That's a pretty big code savings.

    And, here's my "Shot at it"
    Code:
    @Dec_Speed = _ssmax + 17
    Dec_Speed   VAR  WORD  EXT
    Zero Code.

    Think we can reduce it even farther??

    Darrel

Similar Threads

  1. Problem to compile my program
    By wagner in forum Off Topic
    Replies: 5
    Last Post: - 7th July 2008, 20:17
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 21:04
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 0

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