2022-05-26 01:28 PM
Hello STM32 Community.
I need your support, I am trying to do a small project where I need to use for example a var where PWM frequency is saved, as I need to calculate the period I just write a division 1/test_float and the result must be the period.... but the result is zero...
I am using a Blue pill board, STM32F103 and STM32Cube IDE.
I run this small example in order to show you my issue.
float test_float =0;
test_float = 1/10; // ====> test_float=0
test_float = 1/1000; // ====> test_float=0
test_float = 1/10000000; // ====> test_float=0
test_float = 0.0000001; // ====> test_float= 9.9999999999999995e-008 that is OK
Why first four lines are equal to zero?, last line (test_float = 0.0000001; ) it has the right value ...100 ns
any comment/advice is welcome
regards
Alfredo
Solved! Go to Solution.
2022-05-26 01:41 PM
Write as
test_float = 1.0/10.0;
or
test_float = 1.0f / 10.0f;
So it knows the constants aren't integers, which the C compiler will assume they are otherwise.
2022-05-26 01:41 PM
Write as
test_float = 1.0/10.0;
or
test_float = 1.0f / 10.0f;
So it knows the constants aren't integers, which the C compiler will assume they are otherwise.
2022-05-26 01:47 PM
2022-05-26 01:47 PM
Hi Tesla DeLorean, thanks a lot for your comment!!!!
You are right, after adding ".0" it´s works.
Thanks!!!! Again
Alfredo
2022-05-27 02:05 AM
@ERABALF - Note that this is standard C stuff - nothing specifically to do with STM32F103.