cancel
Showing results for 
Search instead for 
Did you mean: 

data values ​​of float type variables appear differently when debugging?

bupark007
Associate

스크린샷 2025-09-05 115915.png스크린샷 2025-09-05 120031.png

I'm trying to figure out why the value 4.2 is returned as -2 when passed as a function argument. Could you please also provide a solution? The MCU is an STM32F302VET.

8 REPLIES 8
TDK
Super User

Is optimization turned off?

If you feel a post has answered your question, please click "Accept as Solution".
Ghofrane GSOURI
ST Employee

Hello @bupark007 

As mentioned by @TDK  using high optimization levels can cause variables to be kept in CPU registers or even optimized away, making them difficult to inspect during debugging. To address this, try setting the optimization level to -O0 (no optimization) when debugging your code.

GhofraneGSOURI_0-1757081751846.png

 

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I'd probably instrument, and call test cases with terminal output to confirm the functionality.

I think there are reported issues with the debugger's memory/expression view.

Check you have pulled libraries with sscanf / printf support of floating point.

Inspect code generated by the compiler for consistency, double check the casting of the math at each stage.

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

Thank you for your answer. However, it is set in optimization mode.

I created a new project and confirmed that it would be output when I copied the above functions and debugged it. The current project keeps confirmed differently ~ I don't understand if STMCUBEIDE's own error ~

 스크린샷 2025-09-08 113231.png

TDK
Super User

Does the if fdata < 1.5 line trigger? Maybe you're on a different statement.

Perhaps make a complete compile-able program that shows the problem.

The compiler is robust--unlikely to be any issues there.

If you feel a post has answered your question, please click "Accept as Solution".
AlfieJames
Associate

Looks like a type mismatch. You’re probably passing a float to a function expecting an int, so 4.2 gets converted weirdly to -2. Make the function parameter a float and pass 4.2f instead.

Yes~ ifdata <1.5 line is executed and 0 is returned.
By the way, if the factor receiving the function is changed from float type to double type, the real value is transmitted even if there is a slight deviation?

화면 캡처 2025-09-09 093250.png

I've been doing Windows program for 20 years and I'm studying firmware program these days. There are a lot of difficult parts. Thank you for your sincere answer.

I'm sorry, but I don't understand. ㅠ.ㅠ Which part is the type mismatch???