Page 1 of 1

Int to float issue

Posted: 03 Mar 2012 18:12
by dscattergood
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

Re: Int to float issue

Posted: 05 Mar 2012 11:40
by filip
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.

Re: Int to float issue

Posted: 06 Mar 2012 14:00
by dscattergood
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

Re: Int to float issue

Posted: 07 Mar 2012 10:23
by Mince-n-Tatties
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.

Re: Int to float issue

Posted: 07 Mar 2012 10:32
by filip
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.

Re: Int to float issue

Posted: 07 Mar 2012 13:02
by janni
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.

Re: Int to float issue

Posted: 11 Mar 2012 00:23
by dscattergood
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