Skip to main content
ERABALF
Associate III
May 26, 2022
Solved

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

  • May 26, 2022
  • 2 replies
  • 2232 views

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

This topic has been closed for replies.
Best answer by Tesla DeLorean

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.

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
May 26, 2022

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 VenmoUp vote any posts that you find helpful, it shows what's working..
ERABALF
ERABALFAuthor
Associate III
May 26, 2022

0693W00000NqjNaQAJ.png

ERABALF
ERABALFAuthor
Associate III
May 26, 2022

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

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

Thanks!!!! Again

Alfredo

Andrew Neil
Super User
May 27, 2022

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.