promotion image of download ymail app
Promoted
Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 2 months ago

c++ output difference?

In c++, why does 3 + 3/2 and 3 + 3.0/2 have different results?? The first one returns 4 and the second one returns 4.5 so what exactly does the decimal 3.0 change? Thanks in advance!

8 Answers

Relevance
  • Marvin
    Lv 7
    2 months ago
    Favorite Answer

    You are confusing the data types.  Int and double behave differently.  In your first example, you are doing integer math (3 + 3/2 is the same as 3 + 1 or 4). In your second example, you are doing integer, and floating point math 3 + 3.0/2 is the same as 3 + 1.5, or 4.5).

    Keep playing with it, and it will come to you.

    Good luck

    • Commenter avatarLogin to reply the answers
  • 2 months ago

    C++ is strongly typed and this must be enforced to get the desired results - int or float, as the case may be 🙄

    • Commenter avatarLogin to reply the answers
  • 2 months ago

    In computing there is an order of processing precedence.  In your sum 3 + 3/2 the division is done first.  There is also a requirement for the processing to create a temporary variable to store the result of part of your sum.  The system will use what it knows to create the largest temporary variable.  You have an int and an int.  Therefore the temporary variable will be an int.

    If you have an int and a float then the temporary variable will be a float.  For int and double then the temporary variable will be a double.

    You need to be aware that temporary variables are being created as this will increase the size of your program.  You also need to know how to create constants of the correct data type.  So if your variables are float then your constants should be float not double.

    Also as a general rule, though it is not necessary, both constants in your division should contain a decimal point

    • Commenter avatarLogin to reply the answers
  • 2 months ago

    If it's not set as type double, you lose data.  The compiler doesn't know what to do with the rest of the numbers, so it drops them.  How many times does 2 go into 3?.   Oh 1, but what is this other stuff?  Idk guess I dont need it.

    • Commenter avatarLogin to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 2 months ago

    You literally are attempting to program something in c++ without knowing how basic operations work there?! :O

    • Commenter avatarLogin to reply the answers
  • 2 months ago

    in C++, division of two ints results in an int.  Any remainders created from getting the quotient is lost.

    so:

    3 + 3/2

    works out to be 4.

    3/2 = 1

    then 3 + 1 = 4

    In your other example, since you have a double, the result is a double:

    3 + 3.0 / 2

    3 + 1.5

    4.5

    And the final answer is a double.

    • Commenter avatarLogin to reply the answers
  • 2 months ago

    In just about any programming language, lower precision types are promoted up to the higher type to make a calculation.

    In calculating 3 / 2, both operands are integers (or perhaps unsigned, short integers, of something like that).  The result is an integer, 1.  There is no integer value of 1.5.

    In calculating 3.0 / 2, the dividend is a float (or double, or similar type).  The divisor, 2, is an integer, and is promoted up to a float to match the other operand.  The quotient is 1.5, a float.

    Note that if you assign the results to an integer, you'll still get 4 as the result in both cases in your question.

    Or if you cast (force) one of the numbers into another type, you'll get the same answer.

    3 + (double) 3 / 2

    will return 4.5

    • Commenter avatarLogin to reply the answers
  • EddieJ
    Lv 7
    2 months ago

    3/2 uses integer division so the result is an integer.

    This occurs when both operands are integers.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.