GLCD display wrong calculation result

General discussion on mikroC.
Post Reply
Author
Message
saharul
Posts: 489
Joined: 08 Jul 2010 08:11

GLCD display wrong calculation result

#1 Post by saharul » 02 Jun 2019 00:07

hi,

i have problem to display a floating point with single decimal point

.........................
........................
int x=300;
float mf_err_small;
mf_err_small= (x-250)/250);

.................................

..................................
floatToStr(mf_err_small,s_temp);
Glcd_Write_Text( "small" , 5, 5, 2);
Glcd_Write_Text(s_temp, 45, 5, 2);
....................................................

the result that i got from glcd was 81.999
it supposed to display 0.2.

what went wrong?

Many thanks
:D :D :D

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: GLCD display wrong calculation result

#2 Post by jovana.medakovic » 03 Jun 2019 13:42

Hello,

Floating-point operations are not 100% accurate because some numbers cannot be represented in the IEEE format without the loss of precision. Therefore, the results of the division can end with .999999.

So, in your code, you can write:

Code: Select all

unsigned char s_temp[20] = {0};
int x = 300;
float mf_err_small;

mf_err_small = (((float)x-250)/250);
mf_err_small += 0.0000001;
  
FloatToStr(mf_err_small, s_temp);
Glcd_Write_Text( "small" , 5, 5, 2);
Glcd_Write_Text(s_temp, 45, 5, 2);
Kind regards,
Jovana

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: GLCD display wrong calculation result

#3 Post by saharul » 28 Jun 2019 08:54

Thanks jovana

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: GLCD display wrong calculation result

#4 Post by jovana.medakovic » 28 Jun 2019 13:39

You're welcome!

Kind regards,
Jovana

Post Reply

Return to “mikroC General”