CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

conversion from int32 to float32

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
epitalon



Joined: 11 Mar 2010
Posts: 11
Location: france

View user's profile Send private message

conversion from int32 to float32
PostPosted: Thu Nov 28, 2013 12:38 pm     Reply with quote

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: 19436

View user's profile Send private message

PostPosted: Thu Nov 28, 2013 1:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 3:13 am     Reply with quote

Hi Ttelmah,

I made the test. And you are right. It is only the printf that goes wrong.
Thanks for your help !
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group