You are not logged in. Please login at www.codechef.com to post your questions!

×

[closed] Addition and Multiplication

I happened to type this in some part of the code and noted this ambiguity:

#define I_LIMIT 100000
#define ELE_LIMIT 1000000000

int main() {
    // your code goes here
    long sum = 0;
    for(int i=1; i<=I_LIMIT; i++)
        sum += ELE_LIMIT;
    cout << sum << endl;

    sum = (I_LIMIT*ELE_LIMIT);
    cout << sum << endl;

    return 0;
}

The output of sum variable in the two cases comes different:

100000000000000
276447232

Why such?

asked 07 Feb, 22:07

damn_me's gravatar image

3★damn_me
2.6k21336
accept rate: 24%

closed 13 Feb, 14:26

The question has been closed for the following reason "The question is answered, right answer was accepted" by damn_me 13 Feb, 14:26


Search the forums. This is one of the most commonly asked questions.

I_LIMIT*ELE_LIMIT

Both of them are ints, there product is stored in int, which then overflows and the overflowed result gets stored in long. Make it (1LL *I_LIMIT*ELE_LIMIT) and see.

sum += ELE_LIMIT

It goes like sum=sum+ELE_limit , which is long+int $\implies$ long. Result stored in long type, doesnt overflow and correct answer stored in sum.

link

answered 08 Feb, 01:29

vijju123's gravatar image

5★vijju123 ♦
14.2k11349
accept rate: 18%

@vijju123's answer is correct. I just want to add one more thing.

Why should we consider that I_LIMIT or ELE_LIMIT will be stored in a 32-bit data type instead of 64-bit data type? Then I stumbled on this StackOverflow answer which explains how does compiler determines the type of Integer Literals in C++ (given there are no explicit suffix in the literal). Basically, it checks if the literal can be contained in any of the following types in the same order and stops as soon as one of the types is able to hold it.

int
long int
long long int

Generally, int is a 32-bit data type and it can contain both the literals. Thus, I_LIMIT and ELE_LIMIT is of type int.

And just for the sake of completeness, the behavior that you have observed is how Implicit Type Conversion works. If you perform an arithmetic operation on two different data types, then the smaller one will be promoted (type promotion) to match with that of the larger data type and then the operation will be performed (this is what happens when you do sum += ELE_LIMIT).

In the other case, both the data type involved in the operation is int and thus the answer will be stored in an int.

link

answered 08 Feb, 13:04

shubhambhattar's gravatar image

3★shubhambhattar
2787
accept rate: 23%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,785
×26
×6

question asked: 07 Feb, 22:07

question was seen: 191 times

last updated: 13 Feb, 14:26