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
8
votes
10 answers

Java multiply operation behavior

I wrote a method to convert a given number from days to milliseconds: private long expireTimeInMilliseconds; ... public void setExpireTimeInDays(int expireTimeInDays) { expireTimeInMilliseconds = expireTimeInDays * 24 * 60 * 60 * 1000; } I had a…
Gilberto Olimpio
  • 681
  • 14
  • 23
8
votes
1 answer

How do reference-counting smart pointer's avoid or handle reference-counter overflows?

In a naive reference-counting smart pointer implementation, the reference-counter could overflow. How is this overflow avoided or handled in C++ standard library implementations?
8
votes
1 answer

Go print large number

I am currently doing the Go Lang tutorial, "Numeric Constants" to be precise. The example code starts with the following statement: const ( // Create a huge number by shifting a 1 bit left 100 places. // In other words, the binary number…
arik
  • 28,170
  • 36
  • 100
  • 156
8
votes
1 answer

c# uint to ushort overflow like in native C

I have the following piece of example code: UInt16 a = 0x3A; UInt16 b = 0xFFDF; UInt16 result = Convert.ToUInt16(a - b); line 3 errors with an Overflow exception. However i want to achieve the same result like i would subtract 2 unsigned shorts in…
eKKiM
  • 421
  • 5
  • 18
8
votes
1 answer

Why does this multiplication integer overflow result in zero?

After answering this question, I was confused about why the overflowing integer in this code resulted in 0 instead of a negative number. It's strange, why such an exact number? Why 0? public class IntegerOverflow { public static void main(String[]…
durron597
  • 31,968
  • 17
  • 99
  • 158
8
votes
6 answers

Force PHP integer overflow

We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages. Since we last upgraded PHP the behavior for overflowing integers has changed. Basically we are using following…
Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
8
votes
1 answer

Do numerical programming languages distinguish between a "largest finite number" and "infinity"?

Question motivation: In standard numerical languages of which I am aware (e.g. Matlab, Python numpy, etc.), if, for example, you take the exponential of a modestly large number, the output is infinity as the result of numerical overflow. If this is…
Josh
  • 83
  • 5
8
votes
1 answer

Metafunction to compute x^n and return the integer limit without overflow if not possible?

Consider the following code: template struct integer_power_bounded { static_assert(Exponent >= 0, "Error in 'integer_power_bounded': 'Exponent >= 0' is false"); static…
Vincent
  • 57,703
  • 61
  • 205
  • 388
8
votes
1 answer

R: simple multiplication causes integer overflow

In a longer script I have to multiply the length of a vector A (2614) with the numbers of rows of a dataframe B (1456000). If I do that directly with length(A) * nrow(B) I get the message NAs produced by integer overflow although there's no problem…
user7417
  • 1,531
  • 3
  • 15
  • 20
8
votes
2 answers

Integer overflow not consistent

Pardon me if this question has been posed before. I looked for answers to similar questions, but I'm still puzzled with my problem. So I will shoot the question anyway. I'm using a C library called libexif for image data. I run my application (which…
Spottsworth
  • 395
  • 5
  • 12
8
votes
2 answers

Integer overflow using lazy sequences in Clojure

I'm just learning to use lazy sequences in Clojure, and I'm not sure what I'm doing wrong in the following code: (defn sum [seqn] (reduce + seqn)) (defn fib ([] (concat [0 1] (fib 0 1))) ([a b] (lazy-seq (cons (+ a b) (fib b (+ a…
Chetan
  • 46,743
  • 31
  • 106
  • 145
7
votes
5 answers

Permutation with Repetition: Avoiding Overflow

Background: Given n balls such that: 'a' balls are of colour GREEN 'b' balls are of colour BLUE 'c' balls are of colour RED ... (of course a + b + c + ... = n) The number of permutations in which these balls can be arranged is given by: perm = n! /…
ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
7
votes
2 answers

Why do my .net Int64's behaves as if they were Int32's?

I'm witnessing a strange behavior in a .net program : Console.WriteLine(Int64.MaxValue.ToString()); // displays 9223372036854775807, which is 2^63-1, as expected Int64 a = 256*256*256*127; // ok Int64 a = 256*256*256*128; // compile time error :…
Brann
  • 31,689
  • 32
  • 113
  • 162
7
votes
4 answers

C integer overflow behaviour when assigning to larger-width integers

If I execute the following code in C: #include uint16_t a = 4000; uint16_t b = 8000; int32_t c = a - b; printf("%d", c); It correctly prints '-4000' as the result. However, I'm a little confused: shouldn't there be an arithmetic…
Davorian
  • 145
  • 1
  • 4
7
votes
2 answers

Why does Rust perform integer overflow checks in --release?

I have this piece of simple code: let val: u8 = 255 + 1; println!("{}", val); It is said here that such a code will compile normally if run with the --release flag. I am running this code via cargo run --release, and I still see the checks: error:…
Oleksandr Novik
  • 489
  • 9
  • 24