-4

I am new to programming and learning C++ using C++ Primer Plus, 6th Ed. by Stephen Prata.

In Chapter 3, it states that

"when an operation involves two types, the smaller is converted to the larger."

The example it provides is from a previous listing where 9.0/5 (double divided by int) results in a value of 1.8 (double). It states that 5 is converted to double before doing the division. Hence, a double divided by a double will result in a value that is also double. This makes sense.

However, if this is the case, why is it that 10.0 / 5 (double divided by int) results in 2 (int). Should not the answer be 2.0 (if 5 is converted to double and then divided into 10.0)?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

2 Answers2

1

When you print a double with .0, the output is just the integer part. You can test it by compiling this code:

double num = 2.0;
std::cout << num;

Output will be: 2

Michael
  • 1,018
  • 4
  • 14
  • 30
0

"when an operation involves two types, the smaller is converted to the larger."

The book's citation doesn't refer to the values of the types used in an operation, but their size.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190