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 CCS Technical Support

Simple division math error using float number???

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



Joined: 25 Oct 2012
Posts: 8

View user's profile Send private message

Simple division math error using float number???
PostPosted: Thu Nov 08, 2012 10:01 am     Reply with quote

hi
plz check this simple code
Code:

float ff1;
int8 open=60;
ff1=open/100;

The result sould be 0.6 but it is 0.000 Why ??
pcm version is 4.13
plz help me
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Nov 08, 2012 10:23 am     Reply with quote

Because you are not using float....

In _C_ nothing to do with CCS, if you perform a maths operation between two numbers, the compiler looks at the types of both numbers and uses the maths for the highest type. You have two 8bit integers in the source, so integer maths is used. If you change one number to be float, then float maths will be used, so:

ffl=open/100.0;

or

ffl=(float)open/100;

In the first case the constant divisor is made a float (adding a decimal does this). In the second, 'open' is 'cast' to a float before it is used.

This is standard C and any book on the subject should explain this.

Best Wishes
omid_juve



Joined: 25 Oct 2012
Posts: 8

View user's profile Send private message

PostPosted: Thu Nov 08, 2012 10:35 am     Reply with quote

Thanks it`s ok now
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