-3

I am confused a little bit as when I multiply an int variable by 10 and then divide it by 10 I thought the variable value should not be changed but I get a different result am I missing something or there is something I should know here is the code

#include <iostream>
using namespace std;
int main()
{
   int intVar = 1500000000; //1,500,000,000
   intVar = (intVar * 10) / 10; //result too large
   cout << “intVar = “ << intVar << endl; //wrong answer
   return 0 ;
}

any help to explain that, please

  • Overflow... Try with a slightly smaller value. – Bo Persson Jun 22 '16 at 15:11
  • Maximum value of a integer before it overflows is about 2.1 billion. If you multiple 1.5 by 10 you get 10.5 billion, so it overflows, a few times. Whatever result you got is divided by 10. Use a `long` if you need to do math with bigger integers. – Cᴏʀʏ Jun 22 '16 at 15:12
  • Fire up the Windows Calculator app and select View|Programmer. Then try your example and watch what happens with the bits. – Dale Wilson Jun 22 '16 at 15:14
  • 1
    Note that signed integer overflow is [undefined behavior](http://en.cppreference.com/w/cpp/language/ub), and that compilers can take advantage of that status to optimize the produced program. gcc, for one, generally exploits it with something approaching glee. – jaggedSpire Jun 22 '16 at 15:17
  • @JoachimPileborg I'm not sure if that dupe target applies. It appears the OP does not understand the order of evaluation and how overflow plays into that. – NathanOliver Jun 22 '16 at 15:18
  • i think when i multiply by 10 and then divide by 10 in the same statement nothing will chang as we do in math when for example (5 * 10)/10 still be 5 am i wrong? – Galal Ramzy Jun 22 '16 at 15:22
  • @GalalRamzy: The computer doesn't recognize that the *10 and / 10 cancel each other out. It actually performs the math. First it does 5 * 10 = 50, then divides 50 by 10. The problem in your question is that 1.5B * 10 is a bigger number than can fit in the `int` data type. If you took the maximum integer value and added 1, you would get the minimum integer value, a negative number. – Cᴏʀʏ Jun 22 '16 at 15:28
  • @NathanOliver Reading the comments by the OP it's clear that the question is actually about the evaluation order. Reopened. – Some programmer dude Jun 22 '16 at 15:33
  • @Joachim Pileborg you are right my question is about evaluation order,thank you – Galal Ramzy Jun 22 '16 at 15:36
  • @Cᴏʀʏ I get it thank you – Galal Ramzy Jun 22 '16 at 15:39
  • @JoachimPileborg Thanks. – NathanOliver Jun 22 '16 at 15:43

1 Answers1

2

The range of a 32-bit int is -(1 << 32) to (1 << 32) - 1.

When 1.5 billion gets multiplied by ten, it exceeds the upper limit on int (which is about 2.1 billion) and overflows to a different number, and when that gets divided by 10, you get the result of that [new] number divided by ten.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • do you mean that the variable is multiplied by 10 then assigned to the variable which is not able to hold so the result is a different number which is divided by 10 – Galal Ramzy Jun 22 '16 at 15:16
  • @GalalRamzy Not quite. `1500000000 * 10` has to briefly get stored in a register in memory before it then gets divided by 10. When it gets stored in a register, it has to get truncated, which we usually refer to as integer overflow. – Xirema Jun 22 '16 at 15:17