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

Typecasting int->float

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







Typecasting int->float
PostPosted: Tue Dec 11, 2001 3:42 pm     Reply with quote

Consider the following (on a PIC16F877):

float f;
int i = 23;
f = (float) i;
printf("Int is \%D, float is \%3.3f",i,f);

The output is:
Int is 23, float is 22.9

Basically, whatever int I cast to a float, the float is always 0.1 less than the int - this isn't due to the lack of accuracy in the floating point number implementation, so why could this be happening?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1576
Dale Botkin
Guest







Re: Typecasting int->float
PostPosted: Tue Dec 11, 2001 8:42 pm     Reply with quote

:= float f;
:= int i = 23;
:= f = (float) i;
:= printf("Int is \%D, float is \%3.3f",i,f);
:=
:=The output is:
:= Int is 23, float is 22.9

Is this on a PIC or a Pentium?

8-) Sorry, but /somebody/ had to say it...
___________________________
This message was ported from CCS's old forum
Original Post ID: 1583
Kevin Mayer
Guest







Re: Typecasting int->float
PostPosted: Tue Dec 11, 2001 9:18 pm     Reply with quote

Actually - no-one really had to say it....the first line of my post is:

"Consider the following (on a PIC16F877):"

so it's on a PIC, not a Pentium...
___________________________
This message was ported from CCS's old forum
Original Post ID: 1584
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Typecasting int->float
PostPosted: Wed Dec 12, 2001 8:21 am     Reply with quote

Their are rounding errors in the floating number format but they are not causing the 0.1 error. You can use the "Pconvert.exe" program distributed with PCW to look at this. The error you are seeing is probably in the printf function. \%3.3f should have displayed something like 22.999
___________________________
This message was ported from CCS's old forum
Original Post ID: 1591
Kevin Mayer
Guest







Re: Typecasting int->float
PostPosted: Wed Dec 12, 2001 3:40 pm     Reply with quote

I only have the PCM compiler, not the whole PCW suite and hence I don't have pconvert.exe :(

Sorry - it was actually a printf \%3.1f - but shouldn't printf round it? (or is that asking too much?)
___________________________
This message was ported from CCS's old forum
Original Post ID: 1602
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Typecasting int->float
PostPosted: Wed Dec 12, 2001 4:18 pm     Reply with quote

Try adding .0000001 to the float prior to printing the number. It should look right to at least 5 decimal places (\%3.5f).


:=I only have the PCM compiler, not the whole PCW suite and hence I don't have pconvert.exe <img src="http://www.ccsinfo.com/pix/forum/sad.gif" border="0">
:=
:=Sorry - it was actually a printf \%3.1f - but shouldn't printf round it? (or is that asking too much?)
___________________________
This message was ported from CCS's old forum
Original Post ID: 1605
Guest








PostPosted: Thu Aug 18, 2005 1:42 pm     Reply with quote

I have the same problem, but is not a casting problem, the proble is in the printf function.
Anybody have a solution to this problem???

Thnaks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 18, 2005 1:49 pm     Reply with quote

To fix the printf rounding problem, see this thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=23290

So, if your precision is ".1", then you add .05 to the value.

if the precision is ".2", then you add .005

if it's ".3", you add .0005

This should fix the CCS round-off problem.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Aug 18, 2005 4:26 pm     Reply with quote

There is no reason why this should happen from a math point of view.
23 is an integer clearly within the range of the mantissa.
Assuming the 4 byte in float representation is accurate of the decimal value 23 then printf and its possible divide by ten logic to tease out the digits for printing is causing an error.
A necessary for accuracy move on a float before CCS attempts a printf conversion would be to first see if the value can be expressed within the mantissa with a zero exponent. This is certainly true of all integers less than 2^23. If the exponent can be balanced down to zero ( it would actually be 127 due to the bias needed to allow for negative exponents)and at the same time adjusting the mantissa for the exponent changes then printf should treat the float now as a 23bit integer. Printf can print integers correctly so it should also print float correctly. Again this is a bug probably caused by starting with the floats normalized 0.1...... binary mantissa representation rather than up shifting when they see a zero in the right most (23 bit of the mantissa) and balancing with a decrement in the exponent.
It's a bug from my point of view.
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