Questions tagged [integer-overflow]

Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.

1056 questions
7
votes
2 answers

What happens in Rust programming language when an integer arithmetic operation overflows?

As far as I know, in C programming language(and many C - based languages), when an arithmetic operation overflows over an N-bit integer, this overflow shortens the result to modulo N-th power of 2, retaining only the LSB's of the resultant. What…
Hari Krishnan U
  • 166
  • 5
  • 16
7
votes
4 answers

How can I compute a * b / c when both a and b are smaller than c, but a * b overflows?

Assuming that uint is the largest integral type on my fixed-point platform, I have: uint func(uint a, uint b, uint c); Which needs to return a good approximation of a * b / c. The value of c is greater than both the value of a and the value of…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
7
votes
3 answers

How to Idiomatically Test for Overflow when Shifting Left (<<) in Rust?

For most operators that might overflow, Rust provides a checked version. For example, to test if an addition overflows one could use checked_add: match 255u8.checked_add(1) { Some(_) => println!("no overflow"), None =>…
chuck
  • 1,420
  • 4
  • 19
7
votes
1 answer

What happens if Date.now () is greater than Number.MAX_SAFE_INTEGER?

Of course it took another 200,000 years for that to happen. But will the Javascript dating system error after the value of Date.now() exceeds the value of Number.MAX_SAFE_INTEGER? What consequences will occur? Maybe this question looks strange and…
7
votes
4 answers

List sum too large, throwing overflow exception

I have a list of prime numbers up to 2 000 000. That's a list containing almost 150 000 very large integers. I want a sum of all the numbers in it. Here's a random list of large integers just for demonstration: List numbers = new…
Milan Vodák
  • 125
  • 1
  • 8
7
votes
1 answer

Why doesn't this program overflow?

Trying to study how C# reacts to overflows, I wrote this simple code : static uint diff(int a, int b) { return (uint)(b - a); } static void Main(string[] args) { Console.Out.WriteLine(int.MaxValue); uint l = diff(int.MinValue,…
Hey
  • 1,701
  • 5
  • 23
  • 43
7
votes
4 answers

3 * 1000000000 overflows as an int, but the variable is long long. Why?

I have a simple c++ app that performs the following calculations long long calcOne = 3 * 100000000; // 3e8, essentially long long calcTwo = 3 * 1000000000; // 3e9, essentially long long calcThree = 3 * 10000000000; // 3e10, essentially If I…
John Voss
  • 107
  • 1
  • 4
7
votes
2 answers

1000000000 * 3 = -1294967296?

I'm confused! Today is November 3rd DateTime DateTime = new DateTime(2010,11,3); long shazbot = 1000000000 * DateTime.Day; shazbot comes out to -1294967296 Huh???
sooprise
  • 22,657
  • 67
  • 188
  • 276
7
votes
1 answer

Can someone explain overflow in C# using binary?

I'm currently reading a book on C# programming and it has brief intro to Overflow and Underflow and the writer gives a general idea of what happens when you go over the allowed range of a certain type. Example short a = 30000; short b =…
Shabubble
  • 73
  • 5
7
votes
2 answers

Does this function for detecting integer addition overflow actually work?

While reading the comments for this question, I came across a link to the comp.lang.c FAQ that shows a "careful addition function" which purportedly detects integer overflow: int chkadd(int a, int b) { if (INT_MAX - b < a) { fputs("int…
zennehoy
  • 6,405
  • 28
  • 55
7
votes
1 answer

Chaining checked arithmetic operations in Rust

When doing integer arithmetic with checks for overflows, calculations often need to compose several arithmetic operations. A straightforward way of chaining checked arithmetic in Rust uses checked_* methods and Option chaining: fn…
mzabaluev
  • 532
  • 5
  • 13
7
votes
3 answers

Why does integer overflow cause errors with C++ iostreams?

Ok, so I have some problems with C++ iostreams that feels very odd, but it is probably defined behaviour, considering this happens with both MSVC++ and G++. Say I have this program: #include using namespace std; int main() { int a; …
Maister
  • 4,978
  • 1
  • 31
  • 34
7
votes
3 answers

arithmetic exception in C#

Why in C# is Example A valid, compilable and will just wrap while examples B will not compile? A int val = 0; val = val + Int32.MaxValue +2; or int val = Int32.MaxValue; val++; B int val = 0; val = 2147483647 + 1; or int val = 0; int val =…
Chris G
  • 469
  • 8
  • 14
7
votes
1 answer

Integer overflow with UDL (user defined literal) for __int128 @ min negative value

For clarity and simplicity I will shorten the following numbers as follows: −170,141,183,460,469,231,731,687,303,715,884,105,728 as -170…728 170,141,183,460,469,231,731,687,303,715,884,105,727 as 170…727 These numbers represent the minimum and…
bolov
  • 72,283
  • 15
  • 145
  • 224
7
votes
1 answer

Delphi: How do i use $OVERFLOWCHECKS OFF to disable overflow checks?

i have bit of code that causes an underflow: var t1, t2, delta: DWORD: begin t1 := 0xffffff00; t2 := 0x00000037; delta := (t2 - t1); The subtraction itself does generate an overflow (underflow), but i don't want Delphi to throw an…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219