Hello everyone!
I am working project which involves float point values. PWM duty is needed to set at a particular value where to float values (calculated from ADC) are equal.
Now the problem is that PWM duty doesn't stop any where it just scrolls 0-100-0, I think it is because it is difficult for float values to match exactly so I want to reduce them up to 3 or 4 decimal places (e.g. 2.9478578348.. to 2.948).
I have googled for "round off float values in mikroc" etc. but all the results I found were for displaying values not for using in other calculations and want I have to use the values for further calculation so I'm posting here a new thread.
Thanks to all in advance for any suggestions.
Round off float values to fewr decimal places
Re: Round off float values to fewr decimal places
It's not possible to 'round-off' floating-point numbers internally stored in binary format, like that used in mC, with decimal places precision. Note that some decimal numbers cannot even be precisely stored in this format.
Added: As for the solution of your problem, why don't you convert the f-p numbers to integers multiplied by required power of 10? Integers equality can be checked without precision problems.
Added: As for the solution of your problem, why don't you convert the f-p numbers to integers multiplied by required power of 10? Integers equality can be checked without precision problems.
-
- Posts: 1557
- Joined: 24 Jun 2007 19:27
- Location: 01800 St Maurice de Gourdans France
- Contact:
Re: Round off float values to fewr decimal places
hello,
a bit "heavy" as solution , but if you have enough RAM,ROM ressources..
F2= rounded value of F1 after 3 decimal digits
result:
a bit "heavy" as solution , but if you have enough RAM,ROM ressources..
Code: Select all
unsigned char TEXTE[32];
unsigned char *txt;
txt=TEXTE;
F1= 2.9478578348;
Float2Ascii (F1,txt,3);
F2=atof(txt);
F2= rounded value of F1 after 3 decimal digits
result: