2025-09-04 9:09 PM - last edited on 2025-09-09 6:33 AM by Andrew Neil
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.
2025-09-05 5:56 AM
Is optimization turned off?
2025-09-05 7:14 AM - edited 2025-09-05 7:16 AM
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.
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.
2025-09-05 10:45 AM
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.
2025-09-07 7:40 PM
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 5:17 AM
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.
2025-09-08 5:50 AM
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.
2025-09-08 5:38 PM
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?
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.
2025-09-08 5:43 PM
I'm sorry, but I don't understand. ㅠ.ㅠ Which part is the type mismatch???