Hi All, I am experiencing a basic problem in the arithametic of the instrucation A = ((B-C)*100)/204 during execution of the instruction, if the result of (B-C)*100 is greater than 16 bits what will happen? for avoiding data loss do I have to declare it as 32 bit variable or the proceesor will handle it without changing the size? Variable A is declared as 16 bit. do I have to use both B and C 32 bit if i conditionally change the instrucation as A = ((C-B)*100)/204 during execution ? my code will look like this if (B>C) A = ((B-C)*100)/204; else if (C>B) A = ((C-B)*100)/204 ; my processor is C8051F020 compiler is C51 best regards Deepak
if all declared as int the final result will be wrong right? Yep. but which one should it be ? That is entirely up to you. You could do all the calculations with a local long int variable (if you know that the end result will always fit in 16 bits), or you could make A a long int, for example.
"You could do all the calculations with a local long int variable (if you know that the end result will always fit in 16 bits), or you could make A a long int, for example." Or you could just let the compiler take care of it - think about the hint from Hans-Bernhard Broeker
Hi, Thanks for the help. But I think my problem doesn't solved by making long A variable. This was what i done previously also, thinking that the intermediate result of the calculation " (B-C)/100" , which will be longer than int will be stored in long A. if i am going to calculate the intermediate result separately it will need one more variable and code size will also increase. Hope I could implement it without adding one more variable, if it is unnecessary. rgds Deepak
But I think my problem doesn't solved by making long A variable. That alone will not do the trick. But you can make A a long int and then do the calculations with A:
A = B; A = A - C; A = A * 100; A = A / 204;
"if i am going to calculate the intermediate result separately it will need one more variable and code size will also increase" With a decent, modern, optimising compiler like C51, that is most unlikely! If you need to accomodate long values, then that's the code you need - there's no getting around it. Have you followed Hans-Bernhard Broeker's advice yet and gone back to your 'C' textbook to see how these things work? Again, It's standard 'C' - nothing specifically to do with Keil. With some suitable casting, you can force the compiler to use long arithmetic to do the calculation without having to introduce intermediate variables...
Hi, Please bear with me .. I know this is not where I have to discuss about standard 'C'. But sorry for that because here no body is near me to help me out with C coding and am very new to 'C' just like a kid learning to Walk !! I used to do it in assembly only. I tried Hans-Bernhard Broeker's advice and it worked. Thanks Mr. Hans-Bernhard Broeker. Thank you all for taking ur valuable time for me. If you don't mind give one more help. I have defined 100 in that code with a name HUNDRED to avoide usage of 'magic number' in the code. but .. how should i use Mr. Hans-Bernhard Broeker's advice with the define??? Really sorry for bothering you all thanks Deepak
I have defined 100 in that code with a name HUNDRED to avoide usage of 'magic number'. That makes no sense. Naming 100 'HUNDRED' serves no purpose. If you were to change it, you would have to change the name as well. I have not studied your app, but names like CORRECTION_FACTOR, CONVERSION_CONSTANT comes to mind. If it under ALL circumstances has to be 100, then leave it as that, if it may change, give it 'functional name' not 'value name' Erik
does "A = (long)((C-B)*100L)/204" do the expected thing?