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

float problem

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



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

float problem
PostPosted: Tue Dec 28, 2004 2:47 pm     Reply with quote

Sometimes after devide this:

x unsigned int32;
y float;
z unsigned int32;

x/y=z;
100000/10=9999; --- ? Why not 10000; were 1

But sometimes = 10000 - That correct !
How correct this if I don't need use printf or sprintf;
and tell me please what mean letter ' L ' after some digits?
Guest








Re: float problem
PostPosted: Wed Dec 29, 2004 12:09 am     Reply with quote

Userrr wrote:
Sometimes after devide this:

x unsigned int32;
y float;
z unsigned int32;

x/y=z;
100000/10=9999; --- ? Why not 10000; were 1

But sometimes = 10000 - That correct !
How correct this if I don't need use printf or sprintf;
and tell me please what mean letter ' L ' after some digits?


try this

z = x / y *(1+1e-7);

then you should have 10000 instead of 9999.999
Ttelmah
Guest







PostPosted: Wed Dec 29, 2004 6:07 am     Reply with quote

'L', is a signal to the compiler, that this value is to be treated as a 'Long'. Though generally, the compiler will convert types, the 'casting' (automatic conversion), in the CCS C, is sometimes a little'lacking', and it can therefore be worthwhile 'forcing' the type.

On the first question about division, what you show, should work. However there is a major 'caveat', if you are dealing with real numbers, rather than the fixed values shown. If (for instance), the result of some aritmetic, is 99999.9921875 (the next value 'below' 100000, that is actually representable by the floating format), and you then calculate this /10, and convert this to an integer (which is what is happening in your z=x/y calculation - the sum as you show it is 'reversed' - you cannot put a value 'into' a calculation), this will be rounded down in the conversion, and will result in 9999. Also 'beware' that CCS, does not reliably cast forward (converting parts of a sum to the higher arithmetic type), but can end up doing the arithmetic in the 'lower' type (int32).
Hence if you want to convert types like this, it is better to explicitly cast, and also to force 4/5 rounding if this is what is wanted.
So a line like:

z=x/(int32)(y+0.49999);

Will force 'y' to be converted to an integer before the aritmetic, and be rounded 4/5, when this happens, and may give what you expect.

Best Wishes
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