Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
0
votes
1answer
48 views
Reverse of a number taking care of boundary conditions
I'm trying to write a simple method in Java that return the reverse of a number (in the mathematical way, not string-wise). I want to take care of boundary conditions since a number whose reverse is ...
4
votes
4answers
68 views
What happens when a integer overflow occurs in a C expression?
I have the following C code:
uint8_t firstValue = 111;
uint8_t secondValue = 145;
uint16_t temp = firstValue + secondValue;
if (temp > 0xFF) {
return true;
}
return false;
This is the ...
-1
votes
4answers
35 views
I want to make equation between two Edit text
I want to make equation between two edit text and put the output on Toast
I tried to use that way :
String data1 = one.getText().toString();
String data2 = two.getText().toString();
String RES = ...
2
votes
4answers
48 views
Integer range and Overflow
This is a simple program to convert positive decimal number into binary. I have to report and stop conversion of those numbers which could cause overflow or erroneous results. I found size of integer ...
1
vote
1answer
69 views
Why x - y does not overflow for TMin in this function? Why is the function wrong in this case?
I was reading about this function:
int tadd_ok ( int x, int y ) {
int sum = x + y;
int negative_overflow = x < 0 && y < 0 && sum >= 0;
int ...
1
vote
1answer
28 views
Check of overflow in signed addition and abelian groups
I was reading about why the following code is buggy:
int tadd_ok ( int x, int y ) {
int sum = x + y;
return ( sum - x == y ) && ( sum - y == x );
}
The explanation was ...
1
vote
2answers
60 views
How to know a value overflows without recurring to a runtime OverflowException
So the question is:
Given a native integral type that represents the largest possible number in a given compiler (language?), for example ulong in C#, how do you detect that an input string ...
0
votes
2answers
82 views
long long division wrong
I am working in a project and in a certain time I have this problem. I have two very large numbers and I want to divide them and get an integer/long long integer. This is what's happening:
Code
...
0
votes
1answer
29 views
PHP intval lose accuracy with big decimal
echo intval(hexdec('c0199506cb5dd57f')); // -4604485287595354112
When we did the same on the clients have another:
client on objective-c returns -4604485287595354753
client wrote on java returns ...
-2
votes
1answer
26 views
In Assembly, How do i get past the -128 to 127 range?
So I have this declaration in .bss
answer resb 1
In answer, I store the results of the sum of 2 digit integers ranging from -99 to +99. When I try adding +99 and +99, the answer becomes negative. ...
0
votes
1answer
72 views
Write a function that prints out every digit of n^100
I was asked to write a Java function that takes an integer, n, and prints out the value n^100.
I have no idea how to approach this. I know by conventional means it would overflow as n grows. Answers ...
0
votes
3answers
68 views
Is there any other way to implement “overflow safe” arithmetic operations in Java other than overriding arithmetic operators?
So here is my question. Since I learnt Java, I have been aware that arithmetic overflow and underflow can exist and java compiler will not complain it to you. Now I come up with a Operation class ...
0
votes
1answer
58 views
Qt: QML Int overflow
In my Qt app I'm trying to pass a large integer value from my C++ code to QML.
In my C++ I have a Q_PROPERTY(int ...), but apparently int sometimes isn't enough for my app and I get an overflow.
Can I ...
2
votes
3answers
85 views
Decimal to Fraction
I am working on a system that needs to accept and display complex fractions. The code for accepting fractions and turning them in to a double works, but when I want to display that value, I need to ...
1
vote
2answers
54 views
unsigned integer overflow error in gcc(TDM-GCC)?
#include <iostream>
#include <climits>
#include <cinttypes>
using namespace std;
int main()
{
uint16_t i = 0;
cout << USHRT_MAX << '\n' << i - 1 << ...
0
votes
1answer
105 views
Is a safe accumulator really this complicated?
I'm trying to write an accumulator that is well behaved given unconstrained inputs. This seems to not be trivial and requires some pretty strict planning. Is it really this hard?
int ...
4
votes
1answer
161 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
113 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
110 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
135 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
159 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
1answer
24 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
52 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?
...
15
votes
6answers
1k 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
68 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
83 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
52 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
125 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
62 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
52 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
27 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
94 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
44 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
34 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
45 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
166 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
144 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
53 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
71 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
70 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
58 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
127 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
157 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
129 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
117 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
137 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
79 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
118 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 ...
-1
votes
1answer
121 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
45 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 ...