1,132 questions
3
votes
2
answers
155
views
How can I take an integer string modulo 4 without overflowing u128?
I am writing a function that takes a string, converts it to an integer and returns the remainder of that value divided by 4. For the purposes of this function, I have to make it able to process very ...
Best practices
1
vote
7
replies
133
views
How reliably and safely detect hardware timer overflow causing logic error
I'm trying to find a best-practice method to handle an edge case when a hardware clock timer wraps around and the overflown value generates a false positive logic match.
For example, on a 16 MHz chip, ...
13
votes
3
answers
3k
views
Why don't C overflow checks use CPU flags?
Both amd64 and arm64 architecture processors have an overflow flag. However, in C, the most common method to detect whether an operation causes overflow/underflow is to make functions like these:
int ...
Advice
1
vote
5
replies
108
views
Integer arithmetic in R, allowing integer overflow
By default, when a result of integer operation would not fit into R's 32bit integer type, we get a warning and the result is NA. E.g.:
1920912463L
# 1920912463 (valid integer)
1920912463L + ...
0
votes
0
answers
102
views
Detecting Unsigned Addition Overflow with Signed Integers in 2's complement
I'm trying to detect if there is unsigned overflow when adding the 2's complement representation of 2 signed integers (using a custom SignedInteger class that wraps modulo 2**bits if the value leaves ...
5
votes
2
answers
185
views
Why (-1 - int.MinValue) does not cause integer overflow?
Why C# checked keyword does not treat -1 - int.MinValue as overflow?
int x = int.MinValue;
int y;
checked {
// Overflow. 2^31 cannot be represented in signed integer.
try { y = -x; } catch (...
4
votes
2
answers
206
views
integer comparision; difference in behavior between Clang and GCC 12
I'm seeing a strange (to me) difference in behavior between Clang and GCC when comparing an integer with its negation. Also, pre-v12 GCC behaves like Clang.
Code is below, but also here's a live link ...
4
votes
2
answers
116
views
What to do when the pandas error position overflows?
So, I'm experimenting with pandas with the IMDB files, especially title.basic.tsv. When trying to parse the runtimeMinutes column to "Int64", I get an error
ValueError: Unable to parse ...
6
votes
4
answers
372
views
How do you figure out how large an integer you will need for an operation in embedded systems?
For example, if I want to add two unsigned 8-bit integers together, I know I will need to store the result in a 16-bit integer. Otherwise, I run the risk of overflowing.
This problem gets more ...
3
votes
4
answers
436
views
How to clip to max an integer overflow using numpy or opencv
I have an array of the form a = np.array([1], dtype='uint8').
Now if I add 255 to a it will overflow and be np.array([0]).
Is there a built-in way to "clip" to value to 255?
NumPy has the ...
1
vote
1
answer
71
views
Numpy array from the image is not squaring right
I have this program that is supposed to select one color channel from an image, and square each element elementwise. However, it is not returning any results greater than the values in the first array?...
0
votes
1
answer
79
views
InferSharp does not detect buffer overrun in C# array access beyond bounds
I'm using InferSharp to analyze a compiled .NET 6.0 C# project for potential issues. However, it does not detect a buffer overrun in the following code:
namespace Buffer_overflow
{
public class ...
4
votes
3
answers
200
views
Overflow arithmetic in C programming language
My platform is x86_64, and assume there are 3 variables whose types are all uint16_t:
uint16_t a, b, c;
And for the following two code snippets:
(1)
uint16_t tmp = b - a;
uint16_t result1 = c - tmp;
...
2
votes
3
answers
124
views
Confused by difference between expression inside if and expression outside if
Context:
I want to verify the fact that under 32-bits, Ox8000 0000 - 1 = Ox7FFF FFFF, so if both of them are interpreted as signed integers, the sign will change from negative to positive.
Here goes ...
0
votes
0
answers
63
views
Is integer overflow defined behavior in Fortran? [duplicate]
According to the C standard, signed integer overflow is undefined behavior. Fortran integers are signed integers. Does the Fortran standard mandate how overflow should be treated? In practice it seems ...
-3
votes
2
answers
144
views
integer overflow in expression of type ‘int’ results in ‘1’ [-Woverflow]
I want to understand more about int overflow in C++ on a 64-bit system. So I would like to stick to int for my understanding without casting / extending the type to unsigned or 64-bit and I am on ...
0
votes
5
answers
182
views
If I can't use subtraction to test whether addition has overflowed, then how can you use division to test whether multiplication has overflowed?
int sum = x+y;
sum-x == y;
If sum overflows, then sum-x will not be equal to y and overflow is detected. But if sum has not overflowed, then sum-x==y is equal to y. Then why is this logic not used?
...
0
votes
1
answer
146
views
Intentional Overflow [duplicate]
I'm wondering if it is allowed in C++ to use overflow on purpose.
In specific I want to increase a counter every cycle and when uint32_max is reached start from 0.
In my mind the easy way would be:
...
21
votes
2
answers
302
views
Signed int overflow-underflow cause undefined behaviour but how does the compiler anticipate this?
Signed int arithmetic operations can overflow and underflow, and when that happens, that is undefined behavior as per the C++ standard (and C standard). At which point the program can be expected to ...
1
vote
2
answers
111
views
Initialize a integral variable to +/- infty for a running min/max in Haskell
In running minimum/maximum problems, the e.g. minimum-so-far is often initialized to \infty in order to guarantee we "capture" every minimum, no matter where it is located.
In Haskell, we ...
11
votes
2
answers
1k
views
Calculating the allocation needs for malloc(): should code use row*col*sizeof or sizeof*row*col?
Is multiplication order important when allocating?
Even if multiplication may affect things in general, what about common ordering concerns with allocation sizing?
data = malloc(row * col * sizeof ...
1
vote
1
answer
194
views
How to convert float to int in C and then back after performing operations while avoiding overflow?
I am working on a project where I need to implement a Neural Network on a microcontroller in C, and execution time is critical. I am trying to try techniques to speed up the running of the code, and ...
2
votes
2
answers
210
views
Overflow while attempting to calculate the Hamming weight in C++
I'm trying to write a short code that calculates the Hamming weight of integers:
class Solution {
public:
int hammingWeight(int n) {
if(n==0){
return 0;
}else{
...
2
votes
1
answer
628
views
It is possible to define that a variable should always saturate on arithmetic operations?
I have a variable on which I do a lot of arithmetic operations.
My code was something like this:
let a: u32 = 3;
let res = (a*2) - 42
I had an overflow when a is too low, so I had to use ...
4
votes
2
answers
198
views
Avoiding unsigned overflow during multiplications using a max limit - how to define the limit value?
I need to compute recursively an integer number represented by an unsigned int down a tree.
Each node is associated with an integer value calculated recursively by multiplying the numbers of its ...
0
votes
0
answers
238
views
How can I avoid Python overflow in pytorch tensors? EDIT: A tensor insiede softmax function suddely become nan after the second iteration
I'm working with really big tensors from pytorch, and as a result of a certain operation I need my tensor to maintain really big values(which represent some indexes), but obviously overflow makes all ...
0
votes
1
answer
539
views
Is the rule for unsigned integer overflow, the carry in matches the carry out?
I'm reading a textbook on systems programming and it states that an overflow occurs for unsigned integers if and only if the carry-in bit is mismatched with the carry-out, in the left-most bit.
So if ...
5
votes
1
answer
104
views
How do I work around what seems to be integer overflow despite the type being large enough [duplicate]
I am performing the following calculation:
unsigned long long int interval = (ui->spinBox_2->value() * 60 * 60 * 1000) + (ui->spinBox_3->value() * 60 * 1000) + (ui->spinBox_4->value()...
0
votes
1
answer
106
views
C function to detect integer overflow in (CSAPP 2.30)
The following integer overflow detection function is given in Computer Systems: A Programmer's Perspective, 3e.
/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y)...
-1
votes
1
answer
242
views
Attempt to multiply with overflow
Rust panicked: attempt to multiply with overflow
I'm trying to make a polynomial for the "AES" encryption algorithm.
The fn new() is meant to construct the bit vector representation of a ...
1
vote
1
answer
94
views
Different result in JS & PHP because of different integer range
<script>
function hs(e, n) {
return e * n % e;
}
alert(hs(1752865668, 1716717484170));
//result: 1752622684
</script>
<?php
function hs($e, $n) {
return $e * $n % $e;
}
echo ...
2
votes
1
answer
117
views
char * vs unsigned char *
So I was playing around with char* and unsigned char* pointers. And I came across this issue:
Code:
#include <stdio.h>
void func(unsigned int max) {
unsigned int* intptr = &max;
...
1
vote
1
answer
94
views
Unexpected results working on Array of Integers
While going through and re-factoring some Java code I wrote a while back for Project-Euler.
I ran into an issue with integer overflow where the answer was too large to contain in type int. This was ...
0
votes
1
answer
155
views
truncate does not support creating files with sizes greater than 2^63-1
Apparently, gnu truncate on my x86_64 system does not support creating files with sizes of size >= 8EiB (= 2^63 Bytes = 9223372036854775808 Bytes).
/usr/bin/truncate: ELF 64-bit LSB pie executable, ...
0
votes
1
answer
56
views
Hidden integer counter in Excel chart?
I animate 2d point moving in excel chart by a very simple cycle:
do while true
.ChartObjects(1).Chart.SeriesCollection(1).XValues = x
.ChartObjects(1).Chart.SeriesCollection(1).Values = y
...
0
votes
1
answer
629
views
How can I implement the overflow flag in Logisim without having access to the second last carry?
In the pic of the ALU, I've implemented the logic to calculate the zero, negative and carry flags. But I can't figure out how to implement the overflow flag without using the second last carry (carry-...
0
votes
2
answers
641
views
How to avoid "RuntimeWarning: divide by zero encountered in log10" in a for loop with over 20.000 elements?
I have a list of data called data. This list contains 21073 nested numpy arrays. Each numpy array, in turn, contains 512 values. Paradigmatially, one nested numpy array looks as follows:
[534.42623424,...
1
vote
1
answer
65
views
Is there any chance that simplified forms of certain mathematical expressions throw overflow errors while complex ones dont?
I was attempting the #69 problem of leetcode, which involves finding square root of given number.
I proceeded via a binary search approach.
int mySqrt(int x) {
if (x==0 || x==1){
return x;
...
1
vote
1
answer
290
views
RiscV checking if overflow has occurred during multiplication
using this algorithm to calculate a multiplication of signed integers (we cant use mul, mulh etc so I implemented the mul with shift and add) how can you check if the result has overflown. The ...
0
votes
0
answers
52
views
What a reason for C2148 or similar errors on another compilers?
I found interesting problem, and can't find the proper answer:
The error c2148 happens, when you try to create array with 2^31 + 1 elements. Same errors I found in another compilers: Clang, GCC, etc. (...
3
votes
1
answer
165
views
Why does bit shifting with a large amount work in C?
I am a beginner in C and mess with its quirks. Why does this code even work?
#include <stdio.h>
int main() {
unsigned char i = 126;
i = (i << 1000000000000000000000000000000000) % ...
0
votes
2
answers
172
views
What determines the data type of a variable- the declarative keyword (short int) or the format specifier (%hd)?
I have been trying to understand the integer overflow in C-programming. I am confused about whether the final value output depends on the initial datatype given to the variable during declaration or ...
1
vote
2
answers
4k
views
Making widening integer conversions safely
I need to make a few safe widening integer conversions – uint32 to uint and uint to uint64, for example. I know it’s possible to achieve this with an explicit type conversion:
x := uint32(1) // ...
1
vote
1
answer
204
views
Calculation of Cliffs delta with very large groups causes integer overflow
In R, I need to calculate Cliffs delta. Here is the formula:
Where xi is an observation in group A, and xj is an observation in group B, and [xi > xj] is 1 if xi > xj is true.
Here is Cliff's (...
0
votes
1
answer
188
views
Custom 2D Convolution not sharpening Image
I have written my own 2D Convolution function as follows
def TwoD_convolution(img, kernel):
# Flipping the kernel by 180 degrees
kernel = kernel[::-1]
for i in range(len(kernel)):
...
2
votes
2
answers
270
views
best way to recognize and handle integer overflow in c?
I am so new in C and so far I didn't comprehend how to prevent from integer overflow, I read many articles but still I am not 100% sure!
in this case
int ft_sqrt(int nb)
{
long int sqrt;
...
1
vote
2
answers
205
views
Signed integer comparison without comparison operators or widening
I am looking for a signed integer comparison function cmp(x: Int, y: Int) -> Int which does not use any comparison operators (<, <=, >, >=, <=>, etc.), does not use widening to a ...
1
vote
2
answers
268
views
Why negative values appear sporadically in Fibonacci Series' calculation? [duplicate]
I have coded a program which calculates Fibonacci Series in C++. Here's the code below:
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout &...
0
votes
4
answers
269
views
How does addition of negative integers work in C?
Given this simple code snippet:
#include <stdio.h>
#include <stdint.h>
int main() {
int8_t a = -1;
printf("Dec: %d, Hex: %hhx\n", a, a);
int8_t b = a + 1;
printf(...
2
votes
1
answer
171
views
Why does pandas sum() give wrong answers for Sparse dataframe?
In a Sparse dataframe, the sum() method applied on the whole dataframe gives wrong results, while sum() applied to specific column or to a dataframe subset works.
It looks like an overflow issue for ...