Casting error when assigning a negative int16 variable to float32 variable

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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. 

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You ought to display your float variable as a float and not as a decimal number.

   

 

   

Bob

View solution in original post

0 Likes
13 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You ought to display your float variable as a float and not as a decimal number.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

 Bob,

   

The variable 'Vgrid' datatype is float32 in the watch window (see the image file), if you are mentioning about that.

   

There is something that I have to mention, when ever 'Vgrid' is assigned with negative integer constant (eg:Vgrid=-2;) it shows some weird value in the watch window (4294967294) but when ever I assign a negative floating constant (eg:Vgrid=-2.00001) it shows the right value in the watch window (-2.00001001). 

   

-Dorai

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You might consider filing a CASE on this to alert Cypress to the problem.

   

 

   

    

   

          

   

To create a technical case at Cypress -

   

 

   

www.cypress.com

   

“Support”

   

“Technical Support”

   

“Create a Case”

   

 

   

You have to be registered on Cypress web site first.

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your Radix is set to decimal which will convert the float to a decimal value. Set the radix to "float" or "default"

   

 

   

Bob

0 Likes
Anonymous
Not applicable

May be a case for Cypress.

   

However 4294967294 = 0xFFFFFFFE which is -2 for for a signed 16bit value.  Could be an issues with the compiler/compiler option setting.

Anonymous
Not applicable

 Bob, 

   

I think Radix has nothing to do with datatype (float)., its just number sytem base and I set it to decimal because I wish to see the values in decimal format. Default Radix in my debug setting is also Decimal. 

   

 

   

Dana, 

   

I will raise a case. 

   

Thanks,

   

Dorai

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

So, why don't you believe me? I set the radix to decimal, as you did and the value was as wrong as yours, then I set it to float and default and it was displayed as a negative floatingpoint number. All as I expected.

   

 

   

Bob

HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

The 'radix' configuration means 'interpret the number to be in this format', and not 'this how I want it to be shown to me'.

0 Likes
Anonymous
Not applicable

 Guys,

   

When the variable is declared the FLOAT, then the watch window shows the  value as expected (Thanks to Bob!) but when the variable is declared as FLOAT32 the watch window starts displaying negative integers in 2's complement form

   

Can you guys tell me whats the difference between FLOAT and FLOAT32 declaration of a variable?

   

Thanks

   

Dorai

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The difference is obviously the way it gets displayed.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

but in cytypes.h

   

typedef          float  float32;

   

So float and float32 should be the same type. ?? Could it be the creator has issue for float32 which is not the actual base type?

Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

No, it is the "typedef". So a typedefed variable is, as far as the debugger is concerned, will have the correct address and length but not the underlying type. Since you may change easily the way a variable is displayed (changing the radix) this seems not to be of much concern.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

What I mean is that the compiler should assign the value and spaces for the variable the same way, but it is the creator that failed to displayed it while debugging.

0 Likes