integer-overflow

Why don't languages raise errors on integer overflow by default?

﹥>﹥吖頭↗ 提交于 2019-11-26 15:26:41
In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow. (For brevity, the method also doesn't handle the case where the specified list is a null reference.) //Returns the sum of the values in the specified list. private static int sumList(List<int> list) { int sum = 0; foreach (int listItem in list) { sum += listItem; } return sum; } If this method is called as

Why does Java think that the product of all numbers from 10 to 99 is 0?

余生长醉 提交于 2019-11-26 15:09:17
问题 The following block of codes gives the output as 0. public class HelloWorld{ public static void main(String []args){ int product = 1; for (int i = 10; i <= 99; i++) { product *= i; } System.out.println(product); } } Please can somebody explain why this happens? 回答1: Here is what the program does at each step: 1 * 10 = 10 10 * 11 = 110 110 * 12 = 1320 1320 * 13 = 17160 17160 * 14 = 240240 240240 * 15 = 3603600 3603600 * 16 = 57657600 57657600 * 17 = 980179200 980179200 * 18 = 463356416

Modular Exponentiation for high numbers in C++

孤人 提交于 2019-11-26 14:22:52
问题 So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to familiarize myself with c++, and I don't want to have to work with anything 64-bits for awhile. An added bonus is that the algorithm is deterministic for all 32-bit numbers, so I can significantly increase efficiency because I know exactly what witnesses to test for. So for low numbers, the algorithm

How to handle arbitrarily large integers

随声附和 提交于 2019-11-26 13:59:56
问题 I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is factorial(12). What are some techniques for handling integers of an arbitrary maximum size. The language currently works by translating code to C++. 回答1: If you need larger than 32-bits you could consider using 64-bit integers (long long), or use or write an arbitrary precision math library, e.g. GNU MP

Program behaving strangely on online IDEs

元气小坏坏 提交于 2019-11-26 12:06:15
问题 I have come across the below C++ program (source): #include <iostream> int main() { for (int i = 0; i < 300; i++) std::cout << i << \" \" << i * 12345678 << std::endl; } It looks like a simple program and gives the correct output on my local machine i.e. something like: 0 0 1 12345678 2 24691356 ... 297 -628300930 298 -615955252 299 -603609574 But, on online IDEs like codechef, it gives the following output: 0 0 1 12345678 2 24691356 ... 4167 -95167326 4168 -82821648 4169 -7047597 Why doesn\

Checking for underflow/overflow in C++?

江枫思渺然 提交于 2019-11-26 11:34:59
问题 Is there a general way to check for an overflow or an underflow of a given data type (uint32, int etc.)? I am doing something like this: uint32 a,b,c; ... //initialize a,b,c if(b < c) { a -= (c - b) } When I print a after some iterations, it displays a large number like: 4294963846. 回答1: To check for over/underflow in arithmetic check the result compared to the original values. uint32 a,b; //assign values uint32 result = a + b; if (result < a) { //Overflow } For your specific the check would

Allowing signed integer overflows in C/C++

天涯浪子 提交于 2019-11-26 09:04:10
问题 I want signed integers to overflow when they become too big. How do I achieve that without using the next biggest datatype (or when I am already at int128_t)? For example using 8bit integers 19*12 is commonly 260, but I want the result 1 11 10 01 00 with the 9th bit cut off, thus -27. 回答1: Signed overflow is undefined in C, and that's for real. One solution follows: signed_result = (unsigned int)one_argument + (unsigned int)other_argument; The above solution involves implementation-defined

How do I get real integer overflows in MATLAB/Octave?

狂风中的少年 提交于 2019-11-26 08:29:29
问题 I\'m working on a verification-tool for some VHDL-Code in MATLAB/Octave. Therefore I need data types which generate \"real\" overflows: intmax(\'int32\') + 1 ans = -2147483648 Later on, it would be helpful if I can define the bit width of a variable, but that is not so important right now. When I build a C-like example, where a variable gets increased until it\'s smaller than zero, it spins forever and ever: test = int32(2^30); while (test > 0) test = test + int32(1); end Another approach I

Overflowing of Unsigned Int

时光总嘲笑我的痴心妄想 提交于 2019-11-26 08:25:58
问题 What will the unsigned int contain when I overflow it? To be specific, I want to do a multiplication with two unsigned int s: what will be in the unsigned int after the multiplication is finished? unsigned int someint = 253473829*13482018273; 回答1: unsigned numbers can't overflow, but instead wrap around using the properties of modulo. For instance, when unsigned int is 32 bits, the result would be: (a * b) mod 2^32 . As CharlesBailey pointed out, 253473829*13482018273 may use signed

Efficient unsigned-to-signed cast avoiding implementation-defined behavior

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:56:05
问题 I want to define a function that takes an unsigned int as argument and returns an int congruent modulo UINT_MAX+1 to the argument. A first attempt might look like this: int unsigned_to_signed(unsigned n) { return static_cast<int>(n); } But as any language lawyer knows, casting from unsigned to signed for values larger than INT_MAX is implementation-defined. I want to implement this such that (a) it only relies on behavior mandated by the spec; and (b) it compiles into a no-op on any modern