1,122 questions
4votes
3answers
168views
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; ...
2votes
3answers
111views
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 ...
0votes
0answers
137views
overflow encountered in matmul, what can I do?
I am currently using an open source Python package called snompy (https://github.com/TomVincentUK/snompy). While implementing the package, a function that defines the transfer matrix returns an ...
0votes
0answers
58views
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 ...
-3votes
2answers
102views
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 ...
0votes
5answers
160views
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? ...
0votes
1answer
114views
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: ...
19votes
2answers
182views
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 ...
1vote
2answers
104views
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 ...
11votes
2answers
1kviews
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 ...
1vote
1answer
116views
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 ...
2votes
2answers
167views
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{ ...
2votes
1answer
230views
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 ...
4votes
2answers
154views
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 ...
0votes
0answers
162views
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 ...