Int to float issue

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dscattergood
Posts: 3
Joined: 17 Jan 2012 14:30

Int to float issue

#1 Post by dscattergood » 03 Mar 2012 18:12

Hi

I tried to turn an int to a float using

Code: Select all

temp_float = temp_int;
for some numbers this worked fine, but for the majority it gives errors.

eg. 0 - 10 works fine
11 comes out as 10.99991
12 comes out as 11.99991 etc

however 15,16,20 (plus several other seemingly random numbers) work fine.

Could anyone explain why some numbers are inaccurate, and is there a better way to do it.

Regards

David

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Int to float issue

#2 Post by filip » 05 Mar 2012 11:40

Hi,

I have tried something like this :

Code: Select all

float temp_float;
int temp_int;

void main() {
  temp_int = 12;
  temp_float = temp_int;
}
and it worked fine for me.

How did you test it ?

Regards,
Filip.

dscattergood
Posts: 3
Joined: 17 Jan 2012 14:30

Re: Int to float issue

#3 Post by dscattergood » 06 Mar 2012 14:00

Hi

Here is the code that has the problem when I put it into my 18F4550...

Code: Select all

void main() {
  char temp_string[10];
  int temp_int;
  float temp_float;
  
  UART1_Init(9600);

  UART1_Write(0xFE);                                        //LCD home
  UART1_Write(0x51);
  UART1_Write(0xFE);                                        //LCD contrast
  UART1_Write(0x52);
  UART1_Write(0x1E);
 
 do {
  
  for ( temp_int = 0; temp_int < 64; temp_int++ )  {

    temp_float = (float)temp_int;                         //same problem with and without (float)

    FloatToStr(temp_float, temp_string);
    UART1_Write_Text(temp_string);

    Delay_ms(1000);
    UART1_Write(0xFE);                                      //LCD home
    UART1_Write(0x51);
    }
    
 } while (1);
}
I am very new to C, so its probably some rookie mistake I am making.

thanks

David

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Int to float issue

#4 Post by Mince-n-Tatties » 07 Mar 2012 10:23

Hi David,

did you read the help file? search floattostr... there is a min number of spaces needed for the array.

set that correctly and see if it helps.
Best Regards

Mince

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Int to float issue

#5 Post by filip » 07 Mar 2012 10:32

Hi,

I have tried your code and it works fine.
Here is the minimal code that is working OK :

Code: Select all

void main() {
  int temp_int;
  float temp_float;

    for ( temp_int = 1; temp_int < 64; temp_int++ )
      temp_float = temp_int;
}
Please, put temp_float variable in the Watch Window of the debugger and see how it changes values.
Regards,
Filip.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Int to float issue

#6 Post by janni » 07 Mar 2012 13:02

dscattergood wrote:I am very new to C, so its probably some rookie mistake I am making.
Maybe not a rookie one :) . Your mistake was to assume that it's the typecasting from integer to float that produces the numbers you've shown while it's the conversion to string that does it. The algorithm used in FloatToStr is quite smart but it may lead to small errors - hence the trailing 9's (the last '1' you've appended yourself :wink: ).

Do not ignore Mince's remark on minimal destination string length or, sooner or later, you'll see your program acting in unpredictable way.

dscattergood
Posts: 3
Joined: 17 Jan 2012 14:30

Re: Int to float issue

#7 Post by dscattergood » 11 Mar 2012 00:23

Thanks for the help guys, I sorted it by using 'sprintf' instead.
I've never used it before, it seems to be a very flexible command that I wish i'd found sooner

thanks

David

Post Reply

Return to “mikroC PRO for PIC General”