Title: Can you figure out the fuss here? (CCS compiler) Post by: OleRuffo on April 03, 2008, 12:39:24 00:39 Hi crew:
I was working with some more interesting computing and a PIC16F716 ( mid range device) so i enter this dummy code to create a sensor value fair easy but..lets see: int msb,lsb; //obvious use signed int16 word16; //you can bet what will happen here float fword; //complicating the things char result[5]; //yup thats the goal void main() { //since i have no attached sensor here lets put some dummy data msb=0xff; //in theory captured from the sensing device lsb=0x1a; //same word16=make16(msb,lsb); //now i have the whole thing joined if (msb> 128) // if the MSBit of the MSByte is 1 then we have a negative number { word16=~word16 +1 ; // I make a two's complement of the number to get the number i need same as word16=word16-0x10000 } //now the part that dont work fword=word16 * 0.0625; //this simple operation would get rid of the entire problem...but something fails here //now put this into the string sprintf(result,"\f%5.1f",fword); } This lil code sounds easy, i get the -230 that should have in word16 but the time i do that simple multiply than i get a real weird value (-4.91112136e-032) so the whole nice code goes ruined...the espected result is -14.375 (but i need to be displayed just -14.3 or -14.4, hence the 5.1 format) surely I'm doing something real bad, but i can't catch the issue here. ::) Some input will be more than appreciated, for the interest and knowledge of all ;D Cheers Title: Re: Can you figure out the fuss here? (CCS compiler) Post by: TigerX on April 03, 2008, 08:12:07 08:12 Merhaba... (Hello...)
Your code has the wrong statement as you said as follows: fword=word16 * 0.0625; //this simple operation would get rid of the entire problem...but something fails here The wrong part is "word16" because signed int16 word16; //you can bet what will happen here So, "world16" is an unsigned integer. Then if you multiply an integer by a float constant, the result is an integer not float. You must change the types or you must make type conversion like these: Solution 1: float fword,world16; //complicating the things ... ... ... fword=word16 * 0.0625; ... then your code will run truely. Solution 2: Your all code will be used only one statement will be changed like these: ... ... fword=(float)word16 * 0.0625; ... ... Then your code also run truely again. These are the tricky point of program writing in C... Have a nice codes... :D Selamlar... EyĆ¼p Title: Re: Can you figure out the fuss here? (CCS compiler) Post by: OleRuffo on April 03, 2008, 11:41:44 23:41 Great!
Both solutions gives the right answer! seems that the solution 2 is more compact in memory usage. Seems that i miss understood the ANSI C data book that i have, where FLOAT=INT *FLOAT instead than INT=INT*FLOAT :o here my confusion, and thanks to you, real solved. :D I think that is great to post this kinda doubts and clever answer like yours so averybody can learn something about C or PICS or whatever! by now i will remember this forever ;) Have a great day! Cheers |