PDA

View Full Version : Microchip Floating Point Routines



Joe Rocci
- 12th October 2006, 18:52
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

Darrel Taylor
- 12th October 2006, 19:51
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...
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>