The Godfather talking
You can run, but you can't hide.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 11:43:58 11:43


Login with username, password and session length


Pages: [1]
Print
Author Topic: Can you figure out the fuss here? (CCS compiler)  (Read 3122 times)
0 Members and 1 Guest are viewing this topic.
OleRuffo
Junior Member
**
Offline Offline

Posts: 40

Thank You
-Given: 36
-Receive: 9


« 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.  Roll Eyes
Some input will be more than appreciated, for the interest and knowledge of all   Grin
Cheers


Logged
TigerX
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 4
-Receive: 1


« Reply #1 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... Cheesy

Selamlar...
EyĆ¼p
Logged
OleRuffo
Junior Member
**
Offline Offline

Posts: 40

Thank You
-Given: 36
-Receive: 9


« Reply #2 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    Shocked
here my confusion, and thanks to you, real solved.  Cheesy
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  Wink
Have a great day!
Cheers
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC