Floating point
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-06-09 3:53 AM
Posted on June 09, 2014 at 12:53
Hi, I can't stand why my stm32f4 does not do the floating point calculation.
Frequency = (RCC_Clocks.HCLK_Frequency)/2 / (IC2Value * (TIM4->PSC+1));Frequency is a float variable, but when i read it with ''printf'' there's an integer inside.
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-06-09 4:04 AM
Posted on June 09, 2014 at 13:04
Hi
Basic (stupid) question : Are you telling printf to print a float number? printf( tmpString, ''%f'', floatVar )Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-06-09 5:07 AM
Posted on June 09, 2014 at 14:07 I could guess all variables in the expression are integers, so the calculation is done in int and then assigned (cast) to float. It is necessary to cast the intermediate result to float before the first division.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-06-09 6:39 AM
Posted on June 09, 2014 at 15:39
The printf is right. The problem was the IC2Value, now i've declared it ''float''. Thank you!
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-06-10 2:31 AM
Posted on June 10, 2014 at 11:31
Hi,
Try this method for your old code,i.e declare IC2Value as you declared before. I think it will workFrequency =(float)((RCC_Clocks.HCLK_Frequency)/2 / (IC2Value * (TIM4->PSC+1)));