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

Little math problem, I'm sure the answer is simple...

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



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

Little math problem, I'm sure the answer is simple...
PostPosted: Fri Feb 13, 2009 4:16 pm     Reply with quote

Hi there, I'm fairly new to this, so can anyone tell me why the 1st code works, but the 2nd one doesn't? I need it to work as in the 2nd code, but it doesn't seem to work for reason I cannot fathom. Any help greatly received! Thank you!

Code:

signed int16 average = 250;
int16 closed = 100, open = 400;
average -= closed;
average *= (float) 100 / (400 - 100);
// average = 50


Code:

signed int16 average = 250;
int16 closed = 100, open = 400;
average -= closed;
average *= (float) 100 / (open - closed);
// average != 50 :(
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 4:31 pm     Reply with quote

Code:
average = (float)average * (100.0 /(open - closed));
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 4:43 pm     Reply with quote

To my opinion, the second code reveals a case of CCS C ignoring well defined rules of C syntax and required implicite type conversions. Please correct me, if I'm wrong.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 4:46 pm     Reply with quote

Ttelmah's comments on this:
http://www.ccsinfo.com/forum/viewtopic.php?t=29749
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 5:13 pm     Reply with quote

Yes. The observed PCH calculation result of 6188 can't be explained by a wrong operator precedence or missing type conversion, it's rather a totally wrong calculation.

PCD V4.084 , in contrast, is basically providing a correct result. It's 49 instead of 50 due to a apparent rounding error, which is O.K. so far.
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Fri Feb 13, 2009 5:33 pm     Reply with quote

Thanks for the fast reply, its working Smile
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