integer-overflow

Integer arithmetic: Add 1 to UINT_MAX and divide by n without overflow

百般思念 提交于 2019-12-13 02:24:41
问题 Is there a way to compute the result of ((UINT_MAX+1)/x)*x-1 in C without resorting to unsigned long (where x is unsigned int )? (respective "without resorting to unsigned long long " depending on architecture.) 回答1: It is rather simple arithmetic: ((UINT_MAX + 1) / x) * x - 1 = ((UINT_MAX - x + x + 1) / x) * x - 1 = ((UINT_MAX - x + 1) / x + 1) * x - 1 = (UINT_MAX - x + 1) / x) * x + (x - 1) 回答2: With integer divisions we have the following equivalence (y/x)*x == y - y%x So we have ((UINT

How to draw a horizontal line at the large y-axis integer?

随声附和 提交于 2019-12-12 16:33:17
问题 For the following data.dat file: 08:01:59 451206975237005878 08:04:07 451207335040839108 08:05:56 451207643872368805 08:07:49 451207961547842270 08:09:56 451208317883903787 08:10:12 451208364811411904 08:14:09 451209030026853864 08:16:19 451209395116787156 08:17:14 451209552481002386 08:20:22 451210080432357203 08:25:36 451210963309583903 08:30:23 451211772783766177 08:34:04 451212394854723707 08:35:53 451212702239472024 08:48:46 451214876715294857 08:49:56 451215072475511660 08:51:24

Will gcc skip this check for signed integer overflow?

…衆ロ難τιáo~ 提交于 2019-12-12 15:26:41
问题 For example, given the following code: int f(int n) { if (n < 0) return 0; n = n + 100; if (n < 0) return 0; return n; } Assuming you pass in a number that is very close to integer overflow (less than 100 away), will the compiler produce code that would give you a negative return? Here is an excerpt about this issue from "The Descent to C" by Simon Tatham: "The GNU C compiler (gcc) generates code for this function which can return a negative integer, if you pass in (for example) the maximum

Factorial function produces wrong result for 21! and above

时光怂恿深爱的人放手 提交于 2019-12-12 12:06:54
问题 for (int i = 0; i <= 25; i++) System.out.printf("%d! = %,d\n", i, factorial(i)); The code above initializes the factorial method below: public static long factorial(int num1) { if (num1 == 0) return 1; else return Math.abs(num1 * factorial(num1 - 1)); } As so the following output is created: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5,040 8! = 40,320 9! = 362,880 10! = 3,628,800 11! = 39,916,800 12! = 479,001,600 13! = 6,227,020,800 14! = 87,178,291,200 15! = 1,307,674,368

Is overflow defined for VHDL numeric_std signed/unsigned

♀尐吖头ヾ 提交于 2019-12-12 10:53:33
问题 If I have an unsigned(MAX downto 0) containing the value 2**MAX - 1 , do the VHDL (87|93|200X) standards define what happens when I increment it by one? (Or, similarly, when I decrement it by one from zero?) 回答1: Short answer: There is no overflow handling, the overflow carry is simply lost. Thus the result is simply the integer result of your operation modulo 2^MAX . Longer answer: The numeric_std package is a standard package but it is not is the Core the VHDL standards (87,93,200X). For

Permutation with Repetition: Avoiding Overflow

假装没事ソ 提交于 2019-12-12 10:37:16
问题 Background: Given n balls such that: 'a' balls are of colour GREEN 'b' balls are of colour BLUE 'c' balls are of colour RED ... (of course a + b + c + ... = n ) The number of permutations in which these balls can be arranged is given by: perm = n! / (a! b! c! ..) Question 1: How can I 'elegantly' calculate perm so as to avoid an integer overflow as as long as possible , and be sure that when I am done calculating, I either have the correct value of perm , or I know that the final result will

Check for int overflow in C++

↘锁芯ラ 提交于 2019-12-12 09:16:39
问题 This question was migrated from Computer Science Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . I'm running a C++ program which adds numbers in a for loop: int y = 0; for (int i=0; i<NUM; i++) { int pow = 1; for (int j=0 j<i; j++) { pow *= 10; } y+= vec[i]*pow; // where vec is a vector of digits } but I am not sure how to check if an overflow has occured. Is there a way? 回答1: Overflow of a signed integral variable results in undefined behaviour. A

C++ function to calculate factorial returns negative value

扶醉桌前 提交于 2019-12-12 04:16:33
问题 I've written a C++ function to calculate factorial and used it to calculate 22 C 11 (Combination). I have declared a variable ans and set it to 0. I tried to calculate 22C11 = fact(2*n)/(fact(n)*fact(n)) where i sent n as 11. For some reason, i'm getting a negative value stored in answer. How can i fix this? long int fact(long int n) { if(n==1||n==0) return 1; long int x=1; if(n>1) x=n*fact(n-1); return x; } The following lines are included in the main function: long int ans=0; ans=ans+(fact

JavaScript integer shift safety: (n << 1) != (n * 2)

痞子三分冷 提交于 2019-12-12 04:12:47
问题 Digging JS just discovered something new to me: n = 0xffffffff 4294967295 n 4294967295 n << 1 -2 n * 2 8589934590 (n << 1) == (n * 2) false n + 1 4294967296 This is console output of builtin FireFox (51.0.1 64-bit) debugger... What I have read so far (w3school, etc), does not allow me to suspect such a behaviour. Is it ok or have I something missed? ...To be continued... 回答1: n << b handles n and the result as int 32, whereas n * 2 handles n and the 2 as number. Note that 4294967295 << 0 will

Swift: Overriding NSObject hash Without Overflow Crash

风流意气都作罢 提交于 2019-12-12 03:56:29
问题 Using Swift 3, I have some NSObject subclasses that I am overriding the hash property and isEqual() functions for. (I want the classes to be able to be used as keys in a dictionary, and I want an array of them to be able to be sorted, but it doesn't really matter why I'm overriding them.) Harkening back to my old C++/Java days, I recalled that a "proper" hash involved prime numbers and the hashes of the object's properties. These questions talk about this style. Something like this: override