cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103, Why some division are equal to zero when the result must be a float value, but not zero.

ERABALF
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions

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.

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

4 REPLIES 4

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.

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

0693W00000NqjNaQAJ.png

ERABALF
Associate III

Hi Tesla DeLorean, thanks a lot for your comment!!!!

You are right, after adding ".0" it´s works.

Thanks!!!! Again

Alfredo

@ERABALF​ - Note that this is standard C stuff - nothing specifically to do with STM32F103.