View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
floating point calculation |
Posted: Wed Feb 23, 2011 11:58 am |
|
|
Hi There,
I'm having troubles doing a seemingly simple floating point calculation.
I want to get the period time from a frequence and thus need to have a value 1/freqency.
This code is only giving me 0(assuming store[0] is 50):
Code: |
float period=0;
period=1/store[0];
fprintf(DEBUG,"period %fs\r\n",period);
|
Why is that? how do I get the correct value out of there?
Thanks for help! |
|
![](templates/subSilver/images/spacer.gif) |
pebbert9
Joined: 31 Dec 2010 Posts: 39
|
|
Posted: Wed Feb 23, 2011 12:05 pm |
|
|
if store[0] is an integer then you need to convert it to float like this, otherwise the result will be an integer.
period = 1 / (float) store[0];
Look for type conversions in the manual |
|
![](templates/subSilver/images/spacer.gif) |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Wed Feb 23, 2011 12:12 pm |
|
|
pebbert9 wrote: | if store[0] is an integer then you need to convert it to float like this, otherwise the result will be an integer.
period = 1 / (float) store[0];
Look for type conversions in the manual |
Yep, that did it thanks! ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|