13 Replies Latest reply on Aug 26, 2014 9:52 PM by ki.leung

    Casting error when assigning a negative int16 variable to float32 variable

    dorai.yelaverthi

      Hi guys,

         

      I am  working with int16 variables (which are actually 12-bit data read from SAR ADC register) and assigning the difference of two of the these variables to a float32 variable. 

         

      I defined the datatypes  and initialization of the variables as below:

         

      volatile int16 Vgrid_adc=0;

         

      volatile int16 Igrid_adc=0;

         

      float32 Vgrid=0;

         

      int16 V; 

         

      code which has casting error is below: 

         

      V=(Vgrid_adc-Igrid_adc);

         

      Vgrid=(float32)V;

         

      Problem that I am facing:

         

      When variable V is positive it is assigned as expected to variable Vgrid but when 'V' is negative this doesnot happen(Refer the .PNG file that contains the watch window for both the cases ). Please tell me whats wrong in the code.