简体   繁体   中英

Why It is giving me Integer Overflow

Its giving me Integer Overflow both in java and C but when I tried it in Python it gave me right answer. Any reasons?

long long int a = 1000000011/2 * 5 ;
printf("%lld",a);

There are two reasons:

  1. Most computer languages use fixed-size integers. Today, that's often 32 bits. The correct result of your calculation, 2500000025, is a 32-bit number, meaning it's too big for a signed 32-bit type. (It comes out as -1794967271 in two's complement .) Python, on the other hand, is different: it uses arbitrary-precision arithmetic .
  2. In C, as in many computer languages, expressions are evaluated "inside out". If you say float f = 1 / 2; you get 0, because the compiler performs integer division, without looking to see that you wanted to get a floating-point result. If you say long long int a = 1000000011/2 * 5; you get an overflow, because the compiler performs integer arithmetic, without looking to see that you wanted to get a long long result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM