cancel
Showing results for 
Search instead for 
Did you mean: 

Variable of type float not showing negative float values

Kolab
Senior

I am reading data from a magnetic field sensor and storing it in a variable of type float. But what concerns is that I can't see the negative measurements on the debug window. When I store it in a int16_t variable the negative measurements show up. How could I watch the negative float values as well ?

1 ACCEPTED SOLUTION

Accepted Solutions

Can you see the right sign of a float you directly assign with a constant?

I​f so perhaps look at how you're converting or casting.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

11 REPLIES 11

Can you see the right sign of a float you directly assign with a constant?

I​f so perhaps look at how you're converting or casting.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Wijeden RHIMI
ST Employee

Hello BKola.2241,

I would like to thank you for proposing this issue.

Can you please inform me about the exact type of debug window if it was expression view, live expression or something else...?

Also, can you check if you have chosen somewhere casting in your code or the option <Cast To Type> inside your view as shown the figure bellow.

0693W00000WJ5foQAD.png 

Finally, to know the right solution of this stuff: I would like to see a screenshot of the different values displayed on your debug window! "You can't see the negative measurements on the debug window", so what do you see exactly: a positive value or some other errors?

Wijeden;

Kolab
Senior

It is from the live expression window.At this moment they should go under 0. But they get stuck somewhere around(above) 0.

0693W00000WJ8wYQAT.pngJust to clarify, each cell of mag_fi_meas is a combination of two 8-bits words, build up through (raw_data[1]<<8 | raw_data[0]); (raw_data[2]<<8 | raw_data[3]); and (raw_data[4]<<8 | raw_data[5]); where the MSB of raw_data[1], raw_data[3] and raw_data[5] is the sign bit (data are stored in the registers using 2's complement)

I type casted my variable to int16_t before I store it into a float variable and I could see the negative values!

But I'm still trying to figure out wether the data I read is correct or not. I'm trying to store a 16-bit data in 2's complement in a float variable. I'm looking for the right type cast that will consider correctly the 2's complement.

I thought I posed a rather simple question.

What does this look like when inspected?

float foo = -3.0f;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Community member​ -3 shows up on debug window. Thank you for the support

Kolab
Senior

@Wijeden RHIMI​ the issue was solved. Thank you for your effort

Piranha
Chief II

What's the problem of just reading the int16_t data and converting (assigning) it normally to float?

int16_t raw_data[3];
float mag_fi_meas[3];
 
SensorRead(&raw_data, sizeof(raw_data));
 
for (size_t i = 0; i < 3; ++i) {
	mag_fi_meas[i] = raw_data[i];
}