double x=5/2 = 2 but not 2.0?

hi, my question is the following:
1
2
3
  double numb_1;
  numb_1=5/2;
  cout<<"num_1="<<numb_1<<endl;


the result I get is 2, but I thought that the result would evaluate according to the data type of the two numbers (int) and then according to the variable type (double) meaning 5/2=2.5(removing the {.5}because both numbers are integers)=2=2.0 because the variable type is double, sorry for any typo or other mistake regarding my English.....thanks in advance.
It doesn't matter what the calculation was, if the value of the double is a whole number, by default there will be no decimal point.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double num = 2.0;
    
    cout << "num = " << num <<  '\n';
    cout << showpoint;
    cout << "num = " << num <<  '\n';
    cout << fixed << setprecision(1);
    cout << "num = " << num <<  '\n';        
    
}
num = 2
num = 2.00000
num = 2.0
When you divide two integers the result will also be an integer. If you want the quotient to be a double you must make sure that at least one of the operands is a double before the division is carried out.
Last edited on
Unnamed temporary object.

The a=b/c is not a single step expression. It contains two subexpressions.

The first, b/c, performs a division and produces a result object. We do not write any name for it and it will not live beyond this one statement, but the compiler treats it as object. The division expression has to be evaluated before the second step.

The second expression, a=temporary, is an assignment where the created temporary value is assigned to variable. The type of temporary in your case is int, so during this expression the code performs implicit conversion to the type of the variable: double.
@Chervil so there is no difference between 2 and 2.0 in this case?...thanks a lot :) , I'm a first year student and the lecturer explained it in a way that I thought every variable with the floating point data type had to have a decimal point in it..
@Peter87 i know that if I made it this way (5.0/2=5.0/2.0=2.5 also because the variable is double this would be the end result if I'm not mistaken) but this wasn't what I meant. I believed that every variable that was a float was supposed to have a decimal point and was afraid that I was doing something wrong because if it was just 2 ,without the (.0) you couldn't really show that it was a float,, thank you for your comment!
@keskiverto,, the sub-expressions are quite a bother :( but in this case I'm getting the first step(both 5 and 2 are integers) therefore the result of this step should be 2.
but for the second step we look at the type of variable which was double so I thought that the final touch would be to add the .0 to be 2.0 it was confusing because I believed every float type variable has to have a decimal point (and here it had to be the last step were the 2 turns into 2.0 as thought it would change from int to double),,, thanks for taking the time to comment :)
"2.0" is just a way to display the value to humans. It has very little to do with how it's stored on the computer. By default it only outputs decimals if it affects the value. In this case the decimals were all zeroes so it could leave out all decimals (2.0 = 2).
Ah, the question is about what is shown to user (by default) by the ostream operator<<.

http://www.cplusplus.com/reference/ios/ios_base/precision/
"Notice that in default notation the precision is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision."

http://www.cplusplus.com/reference/ios/showpoint/
"For standard streams, the showpoint flag is not set on initialization."

In other words, the trailing zero's and the point are controlled separately and by default both can be omitted.
Topic archived. No new replies allowed.