CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Float Division with wrong Answers

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SlimG



Joined: 03 Dec 2004
Posts: 6

View user's profile Send private message

Float Division with wrong Answers
PostPosted: Thu Feb 24, 2005 6:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 24, 2005 7:34 am     Reply with quote

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!

Idea
Ttelmah
Guest







PostPosted: Thu Feb 24, 2005 8:18 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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