1

I was playing around with C++ today and this is what I found after doing some tests:

#include <iostream>

using namespace std;

int main()
{
    int myInt = 100;

    int myInt2 = myInt + (0.5 * 17); // the math inside the parentheses should go first

    int difference = myInt2 - myInt; // get the difference of the two numbers

    cout << difference << endl; // difference is 8
}

the output is 8

#include <iostream>

using namespace std;

int main()
{
    int myInt = 100;

    int myInt2 = myInt - (0.5 * 17); // the math inside the parentheses should still go first

    int difference = myInt - myInt2; // get the difference of the two numbers

    cout << difference << endl; // difference is 9?
}

The output is 9?

So according to my first code sample, 0.5 * 17 = 8, but according to my second code sample, 0.5 * 17 = 9. I am aware that I would get the same results without the parentheses, but I am using them to help illustrate what I am doing in my code.

To help narrow down the problem, I tried this:

#include <iostream>

using namespace std;

int main()
{
    int myInt = 100;

    int myInt2 = 0.5 * 17; // use a variable instead of parentheses

    int myInt3 = myInt + myInt2;

    int difference = myInt3 - myInt; // get the difference of the two numbers

    cout << difference << endl; // difference is 8
}

the output is 8

#include <iostream>

using namespace std;

int main()
{
    int myInt = 100;

    int myInt2 = 0.5 * 17; // continue using a variable

    int myInt3 = myInt - myInt2;

    int difference = myInt - myInt3; // get the difference of the two numbers

    cout << difference << endl; // difference is 8 again!
}

the output is 8 again!

So my question is, if the math in the parentheses always comes first, then why am I getting different results? Shouldn't the first two trials have the same results as the second two trials? Another thing I should mention is that I have had these same results with other decimal numbers like 0.1 and 0.3.

Mashpoe
  • 504
  • 1
  • 9
  • 27
  • Possible duplicate of [Does one double promote every int in the equation to double?](http://stackoverflow.com/questions/13663545/does-one-double-promote-every-int-in-the-equation-to-double) – Ken Y-N Jun 29 '16 at 02:00
  • Do you know what `int` means here? – user253751 Jun 29 '16 at 02:05

3 Answers3

3

The expression 0.5 * 17 results in a floating point value by virtue of the fact that one of its components, 0.5, is floating point.

However, when you assign a floating point number like 8.5 to an integer, it's truncated to 8. And, to be clear about this, it's truncated at the point of assignment.

So the first code segment calculates the value 100 + 8.5 = 108.5 and truncates that to 108 when you assign it to the integer(a). The subtraction of 100 from that then gives you 8.

In the second code segment, you calculate the value 100 - 8.5 = 91.5 and truncate that to 91 when assigning to the integer. The subtraction of that from 100 gives you 9.

The reason the final two code segments work is because the 8.5 is being truncated earlier in int myInt2 = 0.5 * 17, before being added to or subtracted from 100. In that case, 100 + 8 = 108 and 100 - 8 = 92, both which are different from 100 by exactly 8 (sign notwithstanding).


(a) It may help to think of it graphically:

int myInt2 = myInt + (0.5    * 17);
       ^     int   +  double * int
       |     \        \__________/
       |      \            |
       |       \         double
       |        \_____________/
       |                |
       +- truncate <- double
  • The 0.5 * 17 is calculated first to give the floating point 8.5.
  • This is then added to the integer myInt = 100 to give the floating point 108.5.
  • This is then truncated to 108 when being assigned to the integer myInt2.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

.5 * 17 is not 8, it is 8.5

int myInt = 100;

int myInt2 = myInt + (0.5 * 17);

This computes 100 + (0.5 * 17) or 108.5, which gets truncated to 108.

int difference = myInt2 - myInt;

This computes 108 - 100, or 8, which is the result you see.

In your second example:

int myInt = 100;

int myInt2 = myInt - (0.5 * 17);

This calculates 100 - 8.5, or 91.5, which gets truncated to 91.

int difference = myInt - myInt2;

This calculates 100 - 91, or 9.

In the same fashion you can work through the rest of your examples.

There's a very useful tool on your computer. It's called a "debugger". Using this tool you can step through any of your programs, one line at a time, and see for yourself the values of all variables at each step of the way.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

When doing maths with integers any decimal parts are lost AT EACH STEP.

So .5 * 17 gives 8.5, but the result stored in an integer variable is 8 which is used in subsequent steps, including output.

Arif Burhan
  • 507
  • 4
  • 12