int result = 15/2;
is it that result will now be 7 or 7.5. based on my understanding it will be 7. since it is returning an integer. am I correct to say that the .5 will be discarded.
Printable View
int result = 15/2;
is it that result will now be 7 or 7.5. based on my understanding it will be 7. since it is returning an integer. am I correct to say that the .5 will be discarded.
result will be 7 as it is of type int.
That would be easy enough to test wouldn't it?
Yes, you are right.
What you are doing is an implicit cast wich is a conversion type from float to int. Your are casting the type float (or double) to int.
Yes it's true. I didn't know it.
Then the cast would be this:
int result=15.0f/2;
Obviously here there's cast ...true????????
because the result of the division would be 7.5 but finally the value of the variable 'result' will be 7.
Do you agree???
Actually casting is done by adding the data type in paranthesis before the variable you want to type cast such as (float)var.
What you are doing (15.0f/2) is again a float but according to the division rules which say float and integer division will always be float.
We would normally say conversion rather than "cast" since it is implicit rather than explicit. Note that a compiler might complain about possible loss of data in such a conversion, so an explicit cast might be used anyway.Quote:
Originally Posted by Jose M