View previous topic :: View next topic |
Author |
Message |
epitalon
Joined: 11 Mar 2010 Posts: 11 Location: france
|
conversion from int32 to float32 |
Posted: Thu Nov 28, 2013 12:38 pm |
|
|
Hi all,
I use a PCH/PCW compiler V4.101 for a PIC 18F86J50.
I experience problems when converting some large integers to float.
The following simple test :
Code: |
signed int32 j;
for (j = 14800000; j < 29800000 ; j += 150000)
{
printf ("j: %ld\r\n", j);
printf ("f_j: %f\r\n", (float32) j);
}
|
gives the following results
when j <= 21'400'000, output of j as float is OK
beyond that, output of j is the following:
j: 21550000
f_j: -21399672.32
j: 21700000
f_j: -21249671.68
j: 21850000
f_j: -21099673.60
j: 22000000
f_j: -20949672.96
j: 22150000
does anyone know about a fix in the latest version of the compiler ?
Thanks in advance |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Nov 28, 2013 1:37 pm |
|
|
The problem may be printf, not the conversion.
Print out the bytes held in the four bytes of the float, feed these into PConvert, and see what they actually represent.
There were some oddities with printf, when it was used without a 'size' around this compiler area. So you could also try limiting the size and see what happens. So %nf.
Have just had a play, and can confirm it is not the number itself, but the printf, that goes wrong. It works (and correctly expands as needed for the number), provided you don't use a size larger than '7'.
So if you use:
printf ("f_j: %7.0f\r\n", val);
It will work fine.
Best Wishes |
|
|
epitalon
Joined: 11 Mar 2010 Posts: 11 Location: france
|
|
Posted: Fri Nov 29, 2013 3:13 am |
|
|
Hi Ttelmah,
I made the test. And you are right. It is only the printf that goes wrong.
Thanks for your help ! |
|
|
|