Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
4
votes
1answer
157 views
Array auto-filling itself in C++
I'm learning C++, doing some easy examples, and found this odd behavior.
When filling the elements of an array of integers, if any of the elements is set to something greater than 2147483647 (which I ...
2
votes
3answers
88 views
How to multiply a 64 bit integer by a fraction in C++ while minimizing error? [duplicate]
Given a 64 bit (signed) long long or __int64, how would you go about multiplying it by an arbitrary fraction while at the same time minimizing error?
Three simple sketches:
int64_t numerator = ...;
...
2
votes
1answer
93 views
Will gcc skip this check for signed integer overflow?
For example, given the following code:
int f(int n)
{
if (n < 0)
return 0;
n = n + 100;
if (n < 0)
return 0;
return n;
}
Assuming you pass in a number that is ...
6
votes
1answer
113 views
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 ...
-1
votes
2answers
89 views
unsigned long int giving integer overflow
I am trying to create a program in c to solve sudoku puzzles,in which i am trying to store a number equal to 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 in an unsigned long int variable.
on ...
1
vote
0answers
10 views
Overflow-aware implementation of a kalman filter
I'm trying to implement a kalman filter to obtain the orientation of an object, using an 3 axis accelerometer and a 3 axis gyroscope as sensors.
Choosing the dynamic model for the predict phase of ...
-4
votes
1answer
50 views
Result of overflowed-multiplication of two numbers [closed]
Let a be any unsigned integer type a=16*8=128
Now I am wondering: If a can only contain values from 0 to 127 (both inclusive) then
is this statement correct cout<< a or it will print 0?
...
13
votes
6answers
991 views
What is the right way to find the average of two values?
I recently learned that integer overflow is an undefined behavior in C (side question - is it also UB in C++?)
Often in C programming you need to find the average of two values a and b. However doing ...
2
votes
2answers
52 views
Telephone number changed to 2147483647? How to represent telephone numbers?
So, I've created a datatable that asks for a users Mobile Provider (varchar), phonenumber, and pin. The table seems to work well except that when a user inputs a phone number the original number ...
3
votes
1answer
78 views
What are some common strategies different compilers use to deal with overflow in numeric conversions?
I understand that, in C++, when I convert a float/double into an int, whereby the floating-point number is beyond the range that the int can hold, the result is not defined as part of the C++ ...
0
votes
1answer
39 views
Getting number of rows larger than MAX_INT from cursor in Python's psycopg2 with Amazon Redshift
I have started using the Python module psycopg2 recently to work with a Redshift database.
I have a certain query which inserts a lot of rows (about 100 Billions), and the results of the cursor do ...
2
votes
1answer
124 views
Why does << 32 not result in 0 in javascript?
This is false:
(0xffffffff << 31 << 1) === (0xffffffff << 32)
It seems like it should be true. Adding >>> 0 anywhere does not change this.
Why is this and how can I ...
0
votes
2answers
56 views
php multiply strange integer overflow behavior
In scala(java)
scala> 8218553819005469347L * 31
res75: Long = -3479248642764172867
But in php (5.5 / 64bit linux system)
<?php
echo (int)(8218553819005469347 * 31);
it prints ...
0
votes
2answers
50 views
Everytime I read in through ifstream for a specific input, the input changes, possible int overflow [closed]
Every time I use ifstream for input to my program for large inputs, I get something weird. I have a feeling this has to do with integer overflow, but my program still doesn't work with unsigned long ...
0
votes
1answer
20 views
integer overflow with rollmean in zoo
I have an Integer overflow error with the rollmean function. It's working well when I am doing it on 5 numbers section but not working on a 20 (and 260) number sections.
Numbers are trading volumes ...
1
vote
1answer
78 views
Unsigned long long overflow error?
I have been having some strange issues with unsigned long long.
It happens when I set an unsigned long long (I used size_t, however the problem is repeatable with u-l-l). I have set it to 2^31, ...
1
vote
2answers
43 views
different overflow policy for Double and Integer. why?
this code
System.out.println(Double.MAX_VALUE+12345 == Double.MAX_VALUE);
System.out.println(Integer.MAX_VALUE+12345 == Integer.MAX_VALUE);
returns
true
false
Please clarify this difference.
0
votes
1answer
31 views
PHP Counter overwraps/overflows only 1 byte of data, counter resets (race condition)
I know this is a simple question but I downloaded a PHP Counter script from http://www.stevedawson.com/scripts/text-counter.php
which is the first result on google for PHP counter scripts and it ...
0
votes
1answer
38 views
How is uint overflow defined in OpenCL?
What happens when the result of a multiplication or sum in OpenCL overflows? Does it wrap?
In particular I'd like to know if I can catch an overflow in
uint4 x = ( get_global_id( 0 ) * 4 + ...
3
votes
3answers
148 views
C++ while loop optimization not working properly
I have this code segment:
#include <stdio.h>
int main(int argc, const char** argv)
{
int a = argv[0][0];
int b = argv[0][1];
while ((a >= 0) &&
(a < b))
...
0
votes
2answers
136 views
GCC optimizations based on integer overflow
Recently I had a discussion about someone who wanted to check for signed int overflow like this if (A + B < 2 * max(A, B)). Lets ignore for a second that the logic itself is wrong and discuss ...
1
vote
2answers
52 views
Overflowing ints in python
I'm converting a piece of Ian Bell's Elite from C to Python. The C (ab)uses 16-bit integers, which overflow. Python uses magical Python integers, which do not. This is a problem.
I could just define ...
0
votes
3answers
62 views
How to avoid overflow in python when numerator and denominator are both large
from operator import mul
from fractions import Fraction
import math
n = 5000
def nCk(n,k):
return int( reduce(mul, (Fraction(n-i, i+1) for i in range(k)), 1) )
p = 2.884e-5
totP = 0
sgn = 1
...
2
votes
3answers
62 views
How to use biginteger in Clojure?
I am trying to calculate the 500th Fibonacci number in Clojure:
(defn fib-pair [[a b]] [b (+ a b)])
(nth (map first (iterate fib-pair [1 1])) 500)
ArithmeticException integer overflow ...
-1
votes
1answer
57 views
unsigned long long overflow with numbers in the range [10^5,10^10]
I've implemented Repeated square-and-multiply algorithm for exponentiation from book "A Handbook of Applied Cryptography (http:// cacr.uwaterloo.ca/hac/about/chap2. pdf - Algorithm 2.143), that ...
3
votes
1answer
124 views
How does subtracting pointers p - (p - 1) create integer overflow?
Here it is in code:
#include <stdio.h>
int main()
{
int i = 3;
int *p = &i;
p - (p - 1);
return 0;
}
The compiler (gcc) warns of integer overflow at the outer subtraction:
...
6
votes
2answers
156 views
Why is checked arithmetic in .NET sometimes faster than unckecked?
Why is it when I turn on "check for arithmetic underflow/overflow" under C# Project Properties > Build > Advanced, that the following code runs faster (138 ms) than if I turn the option off (141 ms)?
...
1
vote
4answers
99 views
Java Hashcode gives integer overflow
Background information:
In my project I'm applying Reinforcement Learning (RL) to the Mario domain. For my state representation I chose to use a hashtable with custom objects as keys. My custom ...
0
votes
4answers
83 views
Python prevent overflow errors while handling large floating point numbers and integers
I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code:
import math
def F(n):
return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5))
def ...
8
votes
2answers
108 views
“Simulate” a 32-bit integer overflow in JavaScript
JavaScript can handle the following Math just fine:
var result = (20000000 * 48271) % 0x7FFFFFFF;
But in some programming languages, that first int*int multiplication results in a value too large ...
1
vote
3answers
76 views
Calculate Java Int Overflow
Is there a formula to calculate what the overflow of a Java int would be?
Example: if I add 1 to Integer.MAX_VALUE; the answer is not 2147483648, but rather -2147483648.
Question: if I wanted to ...
6
votes
1answer
97 views
Convert INT_MAX to float and then back to integer.
In C programming, I find a weird problem, which counters my intuition. When I declare a integer as the INT_MAX (2147483647, defined in the limits.h) and implicitly convert it to a float value, it ...
0
votes
1answer
44 views
Arithmetic Overflow in mips
I am just started learning exception handler of MIPS instruction.
I need to make my program to have Arithmetic overflow exception so that i can test my exception handler.
I have two array A and ...
0
votes
1answer
42 views
Reducing huge user input to limits? (C++)
I have a function in my C++ program that collects one number from the terminal.
I want it to accept any number input from the user, reduce numbers outside the signed long long int range to the exact ...
2
votes
3answers
56 views
Signed integers' undefined behavior and Apple Secure Coding Guide
Apple Secure Coding Guide says the following (page 27):
Also, any bits that overflow past the length of an integer variable (whether signed or unsigned) are dropped.
However, regards to signed ...
0
votes
3answers
52 views
Number overflow in Java
I am tring to implement C/C++ atoi function in Java, below is the code snippet
for (int j = 0; j < s.length(); j++) {
int digit = Character.digit(s.charAt(j), 10);
if (sum ...
0
votes
2answers
69 views
g++ strict overflow, optimization, and warnings
When compiling the following with the strict overflow flag, it tells me, on the 2nd test that r may not be what I think it could be:
int32_t r(my_rand());
if(r < 0) {
r = -r;
...
6
votes
3answers
180 views
How can both (i + 1) < ii and (i + 1) > ii both be true?
I'm currently learning C program But I came across some weird behavior
I expected one result but two results is printed like this
$ ./a.out
yes1 0x80000000
yes3 0x80000000
How could possible that?
...
2
votes
3answers
72 views
Check for arithmetic overflow and get overflow count?
What would be the most appropriate way to detect an arithmetic overflow (or underflow for that matter) and get the overflow count?
For easier understanding I'll will be using byte, but this is the ...
1
vote
3answers
158 views
Delphi 2007 - programmatic manipulation of the “exceptions to ignore” list
I have an application which occasionally returns an integer overflow when FormatDateTime is called. I have no idea what scenario triggers this, although I have found mention of the problem here and ...
1
vote
2answers
201 views
VB6 how to get C-like integer overflow
I want to port this simple hash algorithm to VB6.
I have come up with:
Public Function simpleHash(hashString As String) As Long
Dim hash As Long
Dim c As Long
Dim i As Integer
On Local Error ...
4
votes
2answers
295 views
How to calculate (n!)%1000000009
I need to find n!%1000000009.
n is of type 2^k for k in range 1 to 20.
The function I'm using is:
#define llu unsigned long long
#define MOD 1000000009
llu mulmod(llu a,llu b) // This function ...
0
votes
3answers
97 views
Unsigned arithmetic and integer overflow
I am trying to understand arithmetic overflow. Suppose I have the following,
unsigned long long x;
unsigned int y, z;
x = y*z;
y*z can lead to an integer overflow. Does casting one of the operands ...
2
votes
6answers
399 views
Absolute value of INT_MIN [duplicate]
How could I extract the absolute value of INT_MIN without overflowing? See this code for the problem:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
...
2
votes
3answers
53 views
Does the C standard make an guarantees about the relationship between INT_MIN and INT_MAX?
For example, in both 2's and 1's complement, INT_MAX*-1 is a valid number. Is this guaranteed? Are there any other guarantees? Could INT_MIN be -1 or 0 on any architecture, for example? Is there ...
3
votes
4answers
153 views
Is value of variable always negative in case of overflow
It tried to solve this problem, given N,K amd M, find maximum integer T such that N*(K^T) <= M. N,K and M can be the values to 10^18. So long long is sufficient.
I tried to solve it using iteration ...
0
votes
5answers
67 views
Type that can hold the product of two size_t
I have two size_t integers and need to take their product. In what type should I store the result?
#include <limits>
#include <vector>
#include <iostream>
int main() {
typedef ...
1
vote
4answers
92 views
How to determine if number represented by char array will overflow an int?
Suppose that I have a char array of arbitrary length that only holds ASCII representations of numerical digits used to form a number in each entry of the array. For example
char my_array[10] = { '5', ...
2
votes
1answer
116 views
What happens when a char is assigned a value too large to fit in a byte?
Let's say I have a char pointer:
char * cp;
int val2 = 2;
cp = &val2;
*cp = 1234;
What will happen since the value 1234 is too large to fit in 1 byte? Will it just overflow and ...
1
vote
1answer
109 views
Understanding integer overflow with `int` and `long long` variables
Why do these programs produce different outputs?
Program 1:
#include <stdio.h>
#include <limits.h>
int main()
{
long long bigger = 0;
int smaller = INT_MAX;
bigger = ...