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

how to get float when int/int

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



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

how to get float when int/int
PostPosted: Thu Feb 04, 2016 2:36 am     Reply with quote

Code:

float A;
int32 B,C;

A=B/C*3.6;


how to be sure that result A will be float value?
Code:
A= (float)(B/C)*3.6 ?

Or
Code:
A=(float)B/(float)C*3.6

or how?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Feb 04, 2016 4:42 am     Reply with quote

Just:

Code:

float A;
int32 B,C;

A=(float)B/C*3.6;


In C arithmetic evaluates based on mathematical precedence, and then (generally) progresses left to right. The precedence order, is generically for normal arithmetic operators:

casts
exponents and roots.
multiplication and division.
addition and subtraction.

So, B/C, will evaluate before the *, on the grounds of being leftmost.
However:

5.1+B/C

Would evaluate B/C first, then the addition (since division has a higher precedence than addition).

Now the 'generally' above, is because just a few C operators have right to left associativity (& for 'address of', * to dereference a pointer, unary negation etc.). These are fairly obvious, and unlikely to occur in normal maths.

Now for each individual operation, 'casts' have a higher precedence than any arithmetic operator, so are performed before the actual operation. Casts can be explicit (float), or 'implicit'. Implicit casting occurs when either variable is of a 'higher' type than the other. In some languages it is only done when the left variable is higher than the other. Hence generally if you want to perform an operation using a particular type, explicitly cast the left variable to the higher type, and this will force this operation to use this higher type.
'Higher' and 'lower' here are determined by the 'conversion rank'. This is:

Floats (based on width 'double' - PCD only - above 'float' etc.)
Then integer based on width. So int32 has higher rank than int16 etc..

Signed and unsigned have the same rank, but sign will be preserved if possible. So if you have a 'signed int8', of only '-1' and multiply this by an unsigned int8 '2', the result will be -2. However multiply by an unsigned int16, and unsigned int16 arithmetic will be used.

Phew.

So B (converted to float) /C, evaluates using float arithmetic. The result is then a float. This *3.6, then uses float arithmetic again.

Smile

For your sum as posted:
Code:

3.6*B/C;


Would also be just as good.
Here 3.6 is a float, so B will be implicitly cast to float and a float multiplication will result. Then the float result will automatically cast C to float for the division.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Thu Feb 04, 2016 5:37 am     Reply with quote

Thanks, Ttelmah!
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