|
|
View previous topic :: View next topic |
Author |
Message |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
float from two int16's |
Posted: Sun Jan 30, 2005 10:03 am |
|
|
I've got to fix something blind without the hardware (always a pain!). Anyway it comes down to needing a float instead of a int32.
rough code:
int16 a;
int16 b;
float c;
c = a / b;
Code: | .................... R = pforce / sforce;
1DB4: MOVFF 3D,41B
1DB8: MOVFF 3C,41A
1DBC: MOVFF 3F,41D
1DC0: MOVFF 3E,41C
1DC4: BRA 192E
1DC6: MOVFF 02,41B
1DCA: MOVFF 01,41A
1DCE: MOVLB 0
1DD0: BRA 1978
1DD2: MOVFF 03,419
1DD6: MOVFF 02,418
1DDA: MOVFF 01,417
1DDE: MOVFF 00,416
|
will this work OK or should i use
c = (float)a / (float)b; for a belts and braces approach? code:
Code: | .................... R = (float)pforce / (float)sforce;
1ECC: MOVFF 3D,41F
1ED0: MOVFF 3C,41E
1ED4: RCALL 192E
1ED6: MOVFF 00,41A
1EDA: MOVFF 01,41B
1EDE: MOVFF 02,41C
1EE2: MOVFF 03,41D
1EE6: MOVFF 3F,41F
1EEA: MOVFF 3E,41E
1EEE: RCALL 192E
1EF0: MOVFF 41D,421
1EF4: MOVFF 41C,420
1EF8: MOVFF 41B,41F
1EFC: MOVFF 41A,41E
1F00: MOVFF 03,425
1F04: MOVFF 02,424
1F08: MOVFF 01,423
1F0C: MOVFF 00,422
1F10: BRA 1966
1F12: MOVFF 03,419
1F16: MOVFF 02,418
1F1A: MOVFF 01,417
1F1E: MOVFF 00,416
|
I would hope both of these would give me exactly the same result, is the compiler doing some good optimisation for me which I am overriding with the cast?
Keep well all,
Will |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Sun Jan 30, 2005 10:57 am |
|
|
Will,
You could try your tests with a quick and dirty program on the simulator in MPLAB to confirm your results.
If you can't do that, I would definitely cast the variables before the divide. Look at one example: if a < b then the divide using ints should yield 0, while the divide with floats should yield a number between 1 and 0 in floating point notation. |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Sun Jan 30, 2005 11:31 am |
|
|
I wondered if some automatic casting would happen? I know CCS has some strange rules with casts. I haven't used a float for several years! |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Sun Jan 30, 2005 3:45 pm |
|
|
Don't get your hopes up. I have learned the hard way that I should always make an explicit cast when I want thing to work out correctly. Don't rely on automatic/assumed casting.
In the case that you have mentioned, I just ran it on the simulator, and without the cast, the division is performed as int16 first, then the result is cast to a float. So, 2/4 = 0.00. If you cast both a and b to float, then divide, the result is 0.500. I tried this with a few values and the result is the same, int16 division then cast.
So, in your case, since you need to make the change "blind" and need the highest probability of success, cast first, then divide. |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Mon Jan 31, 2005 6:25 am |
|
|
Thanks. I really should install MPLAB again. When I switched to the CCS IDE I never went back. MPLAB never got installed on my new PC. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
Re: float from two int16's |
Posted: Mon Jan 31, 2005 8:43 am |
|
|
Will Reeve wrote: |
int16 a;
int16 b;
float c;
c = a / b; |
As I read K&R this should do a 16 bit integer divide producing an integer result which is then cast to a float. I assume that is NOT what you want.
If you want the compiler to do something else you must tell it what you want! _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|