View previous topic :: View next topic |
Author |
Message |
Sydney
Joined: 13 Feb 2009 Posts: 71
|
Little math problem, I'm sure the answer is simple... |
Posted: Fri Feb 13, 2009 4:16 pm |
|
|
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
|
|
Posted: Fri Feb 13, 2009 4:31 pm |
|
|
Code: | average = (float)average * (100.0 /(open - closed)); |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Feb 13, 2009 4:43 pm |
|
|
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
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Feb 13, 2009 5:13 pm |
|
|
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
|
|
Posted: Fri Feb 13, 2009 5:33 pm |
|
|
Thanks for the fast reply, its working |
|
|
|