Tagged Questions
5
votes
2answers
303 views
Panicked at 'attempt to subtract with overflow' when cycling backwards though a list
I am writing a cycle method for a list that moves an index either forwards or backwards. The following code is used to cycle backwards:
(i-1)%list_length
In this case, i is of the type usize, ...
-3
votes
1answer
43 views
Implementing / enforcing wraparound arithmetic in C [closed]
The C standard says that overflow in arithmetic is undefined.
I would like to know how to implement wraparound arithmetic in a performance-friendly way. This means that overflow checking solutions ...
4
votes
1answer
106 views
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_* ...
0
votes
2answers
315 views
How to avoid integer overflow in case of pow function in division with a number?
I have the following statement.
d = (pow(a,2*l+1)+1)/(val+1);
Here,
val, a and l are variables which are of no relation to the question.
the numerator can exceed long long int range.
...
1
vote
1answer
109 views
Overflow and carry flag
The context
I read in a textbook that...
An addition and subtraction cannot cause overflow. To quote,
"An overflow cannot occur after an addition if one number is positive and the other negative,...
0
votes
1answer
83 views
Get max value on arithmethic overflow
Is there a built-in way to get max value on arithmetic overflow?
Here's what I need:
var val = byte.MaxValue + 1;
//should be rounded down to byte.MaxValue
MyByteProperty = val;
P.S. I know I can ...
1
vote
1answer
541 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
142 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 ...
0
votes
3answers
302 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 ...
2
votes
1answer
261 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 ...
2
votes
3answers
251 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
733 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 ...
189
votes
36answers
27k views
Unexpected results when working with very big integers on interpreted languages
I am trying to get the sum of 1 + 2 + ... + 1000000000, but I'm getting funny results in PHP and Node.js.
PHP
$sum = 0;
for($i = 0; $i <= 1000000000 ; $i++) {
$sum += $i;
}
printf("%s", ...
3
votes
1answer
148 views
Need help understanding integer arithmetic function
Would someone please be kind enough to explain this function for me thank you!
int overflow(int x, int y)
{
int result, non_overflow, overflow, result_sign, mask;
result = x + y;
...