Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] MikroC > Float to string routine …

Eric_O

Advanced Member level 4
Joined
May 31, 2020
Messages
104
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
996
Here under the beginning of my routine that runs …

void float2ascii_v3_2 (float f, unsigned char *buf, char decimal) // Converts a floating point number to an ASCII string.
{ //
unsigned int remainder;
unsigned char *s, length; // unsigned char *s is the pointer to the string
long i;
char j;

length = 0;
j = 0;

i = (long)((float)f * pow(10, decimal)); // float type is from 3,4 x 10 -38 to 3,4 x 10 38



But …

When I run the routine step by step, with MikroC Debbuger in order to monitor variables and checking loops conditions … when decimal = 3, f = 1234,567, the Debbuger shows f = 1,234567EXP003 (ok) and i = 1234565, instead of 1234567.

Why ?

Merci.

Éric
 
Hi

Most probably what you see is the resolution (problem) of a float.
You say your
float type is from 3,4 x 10 -38 to 3,4 x 10 38
Is this according IEEE standard?
--> Read the wikipedia article about float numbers.

i = 1234565, instead of 1234567.
1234567 is a 21 bit binary integer number. Missing 2 LSB values let's me guess your float value uses just 19 significand bits.

Klaus

Added: Maybe the missing 2 LSB values are (additionally) caused by calculation errors.
 
Did you check against sprintf(), the standard method for float to string conversion?

Float function pow() might cause the loss of accuracy, did you check?
 
Did you check against sprintf(), the standard method for float to string conversion?

Float function pow() might cause the loss of accuracy, did you check?
Thanks for your advice. Writing a float to string routine is for my understanding and practicing pointers for a LCD voltmeter, thermometer project or something else …
Presently my 16F877 and 887 can not use sprintf. I will soon switch to a 18F …
Probably pow() function is responsible.
Because before, in the previous version of my routine I didn’t use « decimal » parameter and I wrote 100 instead of pow(10, decimal), and fixed at 2 decimals. I will test with 1000.
 
Hi,

generally I avoid float with small microcontrollers at all. (whenever possible. And it was possible almost every time)

16 bit integer (fixed point) should be suitable for 4 digits.

Klaus
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top