View previous topic :: View next topic |
Author |
Message |
SlimG
Joined: 03 Dec 2004 Posts: 6
|
Float Division with wrong Answers |
Posted: Thu Feb 24, 2005 6:48 am |
|
|
Im trying to do variable divisions with answers between 0 and 360. The problem Im having is that whenever the answer is greater than 255 I don't get the right answer.
(Im using the PIC 16f873)
e.g 7/9*360=280
instead the answer I get is 24
but it works fine with answers under 256
I need the answer to be 280 so that I can display it as a Phase Angle between two sinusoidal waveforms.
Thanks |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Thu Feb 24, 2005 7:34 am |
|
|
With:
Code: |
a = 7.0;
b = 9.0;
c = 360.0;
d = (a/b) * c;
sprintf(outbuf,"%f",d);
|
outbuf has 279.999993 in it.... this is with 3.212 and a 452.
You need to post code, including variable definitions, and state your processor and version number to get help with problems like this. Also, use parens liberally when using CCS!
|
|
|
Ttelmah Guest
|
|
Posted: Thu Feb 24, 2005 8:18 am |
|
|
The division is fine.
The _type_ of your target variable is wrong.
If you are putting this result into an integer, it can only hold 0-255. Hence it 'wraps', and the result given is 280-256 = 24...
Best Wishes |
|
|
|