Problem with float number

General discussion on mikroC.
Post Reply
Author
Message
ietcyberwolf
Posts: 1
Joined: 18 Feb 2011 18:16

Problem with float number

#1 Post by ietcyberwolf » 18 Feb 2011 18:48

Hi for all members:
I am new on PIC16F877 with MikroC i used to programming in IAR of Texas Instruments well the problem is with a float number i made a simulate with PROTEUS and i have all the digits on LCD of my variable, i only need 1 digit after decimal point

do u have any solution?
thanks

Code: Select all

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
unsigned long valorin, valorex;
unsigned char ui,ue,di,de;


void main()
{
float valore;
float ina=0, exa;
char *txt="              ";
char *txtina="";
TRISD.F0 = 0;
TRISD.F1 = 1;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
while(1)
{
    if(PORTD.F1 == 0)
    {
    ina = ina + 0.10000;
    Delay_ms(50);
    }
lcd_out(3,18,"FR");
lcd_out(3,1,"TI");
valore = 60/1.5;
FloatToStr(valore,txt);
lcd_out(4,17,txt);
FloatToStr(ina,txtina);
lcd_out(4,1,txtina);
}
}
:(
Attachments
I only need 1 digit after decimal point :'(
I only need 1 digit after decimal point :'(
problema.JPG (74.17 KiB) Viewed 1105 times

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Problem with float number

#2 Post by tihomir.losic » 21 Feb 2011 17:27

Hello,

You need to know that float values are stored using Microchip 32bit float representation, and there are certain limitations and boundaries with this approach.
So if we see the representation of your float values:

Code: Select all

bX = 10;          // 0x82200000h = 1.000000E+001 = 10
aX = (1/bX);      // 0x7B4CCCCCh = 9.999999E-002 = 0.099
And this approach actually keeps better precision during division. And the result difference is just because of the limitation of the float representation.
If you want you can use manual single-bit rounding with following code:

Code: Select all

aX.B0 = 1;        // 0x7B4CCCCDh = 1.000000E-001 = 0.1
Best regards,

Losic Tihomir
mikroElektronika [Support team]

Post Reply

Return to “mikroC General”