integer-overflow

R: simple multiplication causes integer overflow

穿精又带淫゛_ 提交于 2019-12-03 13:10:51
In a longer script I have to multiply the length of a vector A (2614) with the numbers of rows of a dataframe B (1456000). If I do that directly with length(A) * nrow(B) I get the message NAs produced by integer overflow although there's no problem when I multiply the same numbers: 2614 * 1456000 [1] 3805984000 The only way to get the multiplication to work is round(length(A)) * nrow(B) or length(A) * round(nrow(B)) . But the numbers produced by length and nrow must be integers anyhow! Moreover, I tested this with the following function suggested on the help page for the function is.integer...

Explanation of the safe average of two numbers

梦想的初衷 提交于 2019-12-03 06:36:42
Whenever I need to average two numbers for an algorithm like binary search, I always do something like this: int mid = low + ((high - low) / 2); I recently saw another way to do it in this post , but I don't understand it. It says you can do this in Java: int mid = (low + high) >>> 1; or this in C++: int mid = ((unsigned int)low + (unsigned int)high)) >> 1; The C++ version essentially makes both operands unsigned, so doing a shift results in an arithmetic shift instead of a signed shift. I understand what both these pieces of code are doing, but how does this solve the overflow issue? I

-1 * int.MinValue == int.MinValue?? Is this a bug?

北慕城南 提交于 2019-12-03 06:28:34
问题 In C# I see that -1 * int.MinValue == int.MinValue Is this a bug? It really screwed me up when I was trying to implement a search tree. I ended up using (int.MinValue + 1) so that I could properly negate it. 回答1: This is not a bug. int.MinValue * -1 is 1 greater than int.MaxValue can hold. Thus, the number wraps around back to int.MinValue . This is basically caused by an integer overflow. Int32.MinValue: The value of this constant is -2,147,483,648 Int32.MaxValue: The value of this constant

Should I use unsigned integers for counting members?

自作多情 提交于 2019-12-03 00:08:30
Should I use unsigned integers for my count class members? Answer For example, assume a class TList <T> = class private FCount : Cardinal; public property Count : Cardinal read FCount; end; That does make sense, doesn't it? The number of items stored in a list can't be negative, so why not use an unsigned integer type for it? I think it's in general a good principle to always use the least general (ergo the most special) type possible. Now, iterating over a list looks like this: for I := 0 to List.Count - 1 do Writeln (List [I]); When the number of items stored in the list is zero, the

-1 * int.MinValue == int.MinValue?? Is this a bug?

一世执手 提交于 2019-12-02 19:58:34
In C# I see that -1 * int.MinValue == int.MinValue Is this a bug? It really screwed me up when I was trying to implement a search tree. I ended up using (int.MinValue + 1) so that I could properly negate it. This is not a bug. int.MinValue * -1 is 1 greater than int.MaxValue can hold. Thus, the number wraps around back to int.MinValue . This is basically caused by an integer overflow. Int32.MinValue : The value of this constant is -2,147,483,648 Int32.MaxValue : The value of this constant is 2,147,483,647 So, -2,147,483,648 * -1 = 2,147,483,648 which is 1 greater than Int32.MaxValue . It's not

Check of overflow in signed addition and abelian groups

这一生的挚爱 提交于 2019-12-02 13:33:49
问题 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 that two's complement addition forms an abelian group and so the expression (x + y) - x with evaluate to y regardless if whether or not the addition overflows. (Same for (x + y) - y) which will evaluate to x ). I don't understand this explanation or the abelian group reference. Two's complement addition is basically unsigned

Why does << 32 not result in 0 in javascript?

情到浓时终转凉″ 提交于 2019-12-02 06:32:48
问题 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 correctly write code that handles << 32 ? 回答1: The shift operators always effectively has a right operand in the range 0-31. From the Mozilla docs: Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if

Check of overflow in signed addition and abelian groups

▼魔方 西西 提交于 2019-12-02 05:40:51
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 that two's complement addition forms an abelian group and so the expression (x + y) - x with evaluate to y regardless if whether or not the addition overflows. (Same for (x + y) - y) which will evaluate to x ). I don't understand this explanation or the abelian group reference. Two's complement addition is basically unsigned modulo arithmetic that is "converted" to two's complement, right? So for example if we have 4 bits we

Why does << 32 not result in 0 in javascript?

牧云@^-^@ 提交于 2019-12-02 02:25:44
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 correctly write code that handles << 32 ? The shift operators always effectively has a right operand in the range 0-31. From the Mozilla docs : Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if not only the low five bits will be used . Or from the ECMAscript 5 standard : The production

Unsigned integer in C++ [duplicate]

强颜欢笑 提交于 2019-12-01 23:24:55
问题 This question already has answers here : Overflowing of Unsigned Int (3 answers) Closed 2 years ago . I write the following code: #include <iostream> using namespace std; int main() { unsigned int i=1; i=i-3; cout<<i; return 0; } The output is a garbage value, which is understandable. Now I write the following code: #include <iostream> using namespace std; int main() { unsigned int i=1; i=i-3; i=i+5; cout<<i; return 0; } Now the output is 3. What's happening here? How is the garbage value