-
February 13th, 2014, 02:17 PM
#1
Return int
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.
-
February 13th, 2014, 02:24 PM
#2
Re: Return int
result will be 7 as it is of type int.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 13th, 2014, 02:45 PM
#3
Re: Return int
That would be easy enough to test wouldn't it?
-
February 16th, 2014, 05:52 PM
#4
Re: Return int
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.
-
February 16th, 2014, 07:46 PM
#5
Re: Return int
Originally Posted by Jose M
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.
No.
Integer division of integers will produce an integer (according to the rules of integer division).
There's no implicit conversion taking place at all.
Last edited by razzle; February 16th, 2014 at 08:08 PM.
-
February 17th, 2014, 02:22 AM
#6
Re: Return 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???
-
February 17th, 2014, 02:59 AM
#7
Re: Return int
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.
-
February 17th, 2014, 03:09 AM
#8
Re: Return int
Originally Posted by Jose M
Then the cast would be this:
int result=15.0f/2;
Obviously here there's cast ...true????????
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|