Microchip Floating Point Routines


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    65

    Default Microchip Floating Point Routines

    I'm attempting to use the Microchip 32 bit floating point routines per the application note on the Melabs web site:

    aint = 200
    Gosub itofa ' Convert int to float
    bint = 3
    Gosub itofb ' Convert int to float
    Gosub fpmul '
    bint = 100
    Gosub itofb ' Convert int to float
    Gosub fpmul ' Multiply by 100 to move remainder up 2 places
    Gosub ftoia ' Convert float to int
    aint = aint // 100 ' Get to the remainder
    hserout [".", dec2 aint]

    When I put in 200 and 3 for aint and bint respectively, the routine returns a result of 600 and a remainder of 0 as expected.

    However, if I put in 200 and 30 respectively, the routine returns 6000 and a remainder of 76. I'm having similar problems with division.

    Can anyone help me with this?

    Joe

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


    Did you find this post helpful? Yes | No

    Default

    Hi Joe,

    After multiplying 200 * 30 you get 6000, then you multiply times 100, and you end up with 600,000.

    When you convert that to an integer (9:27C0h), you only get the lower 16-bit result (27C0h) which is 10,176 decimal. Here you can see why the modulas of //100 ends up as 76.

    Try this...
    Code:
    bexp = aexp        ' Store the FP value of aarg to the barg variables
    bargb0 = aargb0
    bargb1 = aargb1
    
    Gosub ftoia        ' Convert aarg to integer
    Swap aexp,bexp     ' Swap A and B values
    Swap aargb0,bargb0
    Swap aargb1,bargb1
    
    Gosub fpsub        ' Subtract the integer portion from the full number
    
    bint = 100         ' for 2 decimal places
    Gosub itofb 
    Gosub fpmul 
    Gosub ftoia
    aint should now have the 2 decimal places you were looking for.

    This is the same technique used in the fpdisplay routine in the 4FUNCTN.BAS example.
    <br>
    DT

Similar Threads

  1. Strugling without floating point
    By pjsmith in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 27th March 2011, 06:29
  2. floating point numbers
    By n qwerty in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th April 2008, 04:18
  3. Floating Point Display Problem (serial string out)
    By Cash Olsen in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th December 2007, 02:03
  4. ADCin and floating point values
    By niknaksbarbeque in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th May 2005, 11:38
  5. DIV32 instead of floating point routines?
    By Tomasm in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd April 2004, 07:50

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