integer-overflow

Fast method to multiply integer by proper fraction without floats or overflow

痞子三分冷 提交于 2021-02-07 05:14:38
问题 My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D (i.e. 10 * 2 / 3 = 7) Obviously I could just use r=x*n/d directly but I will often get overflow from the x*n . If I instead do r=x*(n/d) then I only get 0 or x due to integer division dropping the fractional component. And then there's r=x*(float(n)/d)

How to avoid overflow in expr. A * B - C * D

女生的网名这么多〃 提交于 2021-02-05 12:36:34
问题 I need to compute an expression which looks like: A*B - C*D , where their types are: signed long long int A, B, C, D; Each number can be really big (not overflowing its type). While A*B could cause overflow, at same time expression A*B - C*D can be really small. How can I compute it correctly? For example: MAX * MAX - (MAX - 1) * (MAX + 1) == 1 , where MAX = LLONG_MAX - n and n - some natural number. 回答1: This seems too trivial I guess. But A*B is the one that could overflow. You could do the

How to avoid overflow in expr. A * B - C * D

偶尔善良 提交于 2021-02-05 12:35:24
问题 I need to compute an expression which looks like: A*B - C*D , where their types are: signed long long int A, B, C, D; Each number can be really big (not overflowing its type). While A*B could cause overflow, at same time expression A*B - C*D can be really small. How can I compute it correctly? For example: MAX * MAX - (MAX - 1) * (MAX + 1) == 1 , where MAX = LLONG_MAX - n and n - some natural number. 回答1: This seems too trivial I guess. But A*B is the one that could overflow. You could do the

GCC optimizations based on integer overflow

别等时光非礼了梦想. 提交于 2021-02-05 06:54:13
问题 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 signed integer overflow in context of C/C++. (Which I believe fully inherits this part of standard from C). What kinds of check that need signed integer overflow will be optimized away by current-ish GCC and which won't? Since the original text wasn't all that well formulated and apparently

GCC optimizations based on integer overflow

怎甘沉沦 提交于 2021-02-05 06:53:24
问题 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 signed integer overflow in context of C/C++. (Which I believe fully inherits this part of standard from C). What kinds of check that need signed integer overflow will be optimized away by current-ish GCC and which won't? Since the original text wasn't all that well formulated and apparently

dafny modeling integer overflow

瘦欲@ 提交于 2021-01-28 10:24:34
问题 Can Dafny model integer overflow ? I was surprised when Dafny proved the following: method overflow(c : int) { if (c > 0) { assert((c+1) > 0); } } What am I missing? 回答1: The type int in Dafny means "mathematical integer". So there is no overflow. If you want to model machine arithmetic, there are a few ways to do it. One way is to define something like: type uint64 = x:int | 0 <= x < 0x10000000000000000 and then when you try to store the result in a uint64 you will get an error: method

dafny modeling integer overflow

匆匆过客 提交于 2021-01-28 10:17:39
问题 Can Dafny model integer overflow ? I was surprised when Dafny proved the following: method overflow(c : int) { if (c > 0) { assert((c+1) > 0); } } What am I missing? 回答1: The type int in Dafny means "mathematical integer". So there is no overflow. If you want to model machine arithmetic, there are a few ways to do it. One way is to define something like: type uint64 = x:int | 0 <= x < 0x10000000000000000 and then when you try to store the result in a uint64 you will get an error: method

Unexpected 32-bit integer overflow in pandas/numpy int64 (python 3.6)

谁说胖子不能爱 提交于 2021-01-27 02:41:36
问题 Let me start with the example code: import numpy from pandas import DataFrame a = DataFrame({"nums": [2233, -23160, -43608]}) a.nums = numpy.int64(a.nums) print(a.nums ** 2) print((a.nums ** 2).sum()) On my local machine, and other devs' machines, this works as expected and prints out: 0 4986289 1 536385600 2 1901657664 Name: nums, dtype: int64 2443029553 However, on our production server, we get: 0 4986289 1 536385600 2 1901657664 Name: nums, dtype: int64 -1851937743 Which is 32-bit integer

Unexpected 32-bit integer overflow in pandas/numpy int64 (python 3.6)

非 Y 不嫁゛ 提交于 2021-01-27 02:41:09
问题 Let me start with the example code: import numpy from pandas import DataFrame a = DataFrame({"nums": [2233, -23160, -43608]}) a.nums = numpy.int64(a.nums) print(a.nums ** 2) print((a.nums ** 2).sum()) On my local machine, and other devs' machines, this works as expected and prints out: 0 4986289 1 536385600 2 1901657664 Name: nums, dtype: int64 2443029553 However, on our production server, we get: 0 4986289 1 536385600 2 1901657664 Name: nums, dtype: int64 -1851937743 Which is 32-bit integer

How can I compute a * b / c when both a and b are smaller than c, but a * b overflows?

不想你离开。 提交于 2021-01-26 11:48:27
问题 Assuming that uint is the largest integral type on my fixed-point platform, I have: uint func(uint a, uint b, uint c); Which needs to return a good approximation of a * b / c . The value of c is greater than both the value of a and the value of b . So we know for sure that the value of a * b / c would fit in a uint . However, the value of a * b itself overflows the size of a uint . So one way to compute the value of a * b / c would be: return a / c * b; Or even: if (a > b) return a / c * b;