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

18f45k22 union problem: union not working exactly !!!

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



Joined: 31 Jan 2015
Posts: 6

View user's profile Send private message

18f45k22 union problem: union not working exactly !!!
PostPosted: Tue Nov 17, 2015 2:46 pm     Reply with quote

Hi, I need a help about union usage for converting hex to floating point with the communication of GPRS modem.
I think it is simple but my code didn't show correct output.
In Visual Studio 2010 the code worked fine but in CCS the float value is always 0.0. What do you think about the problem.

Code:

   char p[4]={0x42,0xed,0x40,0x00};
   int value, k1, k2, k3, k4, m1, m2, m3, m4 ;
   
   value=(int)(p[0]);
   fprintf(gsm,"%d\r\n", p[0]);
   k1=value/16;
   m1=value%16;
   value=(int)(p[1]);
   k2=value/16;
   m2=value%16;
   value=(int)(p[2]);
   k3=value/16;
   m3=value%16;
   value=(int)(p[3]);
   k4=value/16;
   m4=value%16;
   
   union myUnion
   {
      float32 f;
      uint32_t res;
     
   }fl;
   
   fl.res=(k1*pow(2.0,28.0))+(m1*pow(2.0,24.0))+(k2*pow(2.0,20.0))+(m2*pow(2.0,16.0))+(k3*pow(2.0,12.0))+(m3*pow(2.0,8.0))+(k4*pow(2.0,4.0))+(m4*pow(2.0,0.0));
   fprintf(gsm,"%ld\r\n", fl.res);
   fprintf(gsm,"%f", fl.f);



output:
1122844672 (int value is correct)
0.00 (float value is false. it must be 118.625)
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Nov 17, 2015 3:02 pm     Reply with quote

One big difference between CCS and Visual Studio is how a float is represented internally. Just a simple search on the internet found me the following link: https://www.ccsinfo.com/forum/viewtopic.php?p=52346
Ttelmah



Joined: 11 Mar 2010
Posts: 19484

View user's profile Send private message

PostPosted: Wed Nov 18, 2015 2:07 am     Reply with quote

and, as an 'add on' to this, since when that post was made, CCS have added similar functions, to those supplied with the compiler. Look at the file 'ieefloat.c'.

The 'reason' relates to the hardware/history. Before IEEE float came out, there were several different float formats. Soon after the PIC was launched, Microchip published an arithmetic library 'optimised' for the instructions available on the PIC's as they then existed (earliest PIC14/16's). These laid the data out this way to save a few instructions on several of the operations.

When CCS launched, they used the work that had already been done.

Then IEEE float came along.

Now the optimisations were still worthwhile, so the format was retained. The 'origins' in Microchip's work, is why the format is sometimes also called Microchip float32 format. With the latest DsPIC chips, extra instructions, mean that there is no advantage, so PCD, uses IEEE float format.
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