integer-overflow

Catch integer exceptions in Fortran

▼魔方 西西 提交于 2019-12-19 22:25:11
问题 Is there a way to catch integer exceptions with gfortran or ifort like there is for catching floating point exceptions? Consider this simple program to calculate the factorial: program factorial use, intrinsic :: iso_fortran_env implicit none integer(8) :: fac real(REAL64) :: facR integer,parameter :: maxOrder = 30 integer :: i fac = 1 ; facR = 1.e0_REAL64 do i=2,maxOrder fac=fac*i ; facR=facR*real(i,REAL64) write(*,*) i, fac, facR enddo ! i end program At some point there will be an overflow

Catch integer exceptions in Fortran

巧了我就是萌 提交于 2019-12-19 22:24:50
问题 Is there a way to catch integer exceptions with gfortran or ifort like there is for catching floating point exceptions? Consider this simple program to calculate the factorial: program factorial use, intrinsic :: iso_fortran_env implicit none integer(8) :: fac real(REAL64) :: facR integer,parameter :: maxOrder = 30 integer :: i fac = 1 ; facR = 1.e0_REAL64 do i=2,maxOrder fac=fac*i ; facR=facR*real(i,REAL64) write(*,*) i, fac, facR enddo ! i end program At some point there will be an overflow

Can a non-empty string have a hashcode of zero?

自古美人都是妖i 提交于 2019-12-18 13:59:20
问题 By "non-empty", I mean in this question a string which contains at least one non-zero character. For reference, here's the hashCode implementation : 1493 public int hashCode() { 1494 int h = hash; 1495 if (h == 0) { 1496 int off = offset; 1497 char val[] = value; 1498 int len = count; 1499 1500 for (int i = 0; i < len; i++) { 1501 h = 31*h + val[off++]; 1502 } 1503 hash = h; 1504 } 1505 return h; 1506 } and the algorithm is specified in the documentation. Before an integer overflow occurs,

Can a non-empty string have a hashcode of zero?

这一生的挚爱 提交于 2019-12-18 13:59:04
问题 By "non-empty", I mean in this question a string which contains at least one non-zero character. For reference, here's the hashCode implementation : 1493 public int hashCode() { 1494 int h = hash; 1495 if (h == 0) { 1496 int off = offset; 1497 char val[] = value; 1498 int len = count; 1499 1500 for (int i = 0; i < len; i++) { 1501 h = 31*h + val[off++]; 1502 } 1503 hash = h; 1504 } 1505 return h; 1506 } and the algorithm is specified in the documentation. Before an integer overflow occurs,

Integer division overflows

不想你离开。 提交于 2019-12-18 13:05:38
问题 The Problem I have been thinking about integer (type int) overflows, and it occurs to me that division could overflow. Example : On my current platform, I have INT_MIN == -INT_MAX - 1 and thus INT_MIN < -INT_MAX and thus INT_MIN / -1 > -INT_MAX / -1 and thus INT_MIN / -1 > INT_MAX. Hence, the division ( INT_MIN / -1 ) does overflow. The Questions So, I have two questions: What (cross-platform) C code could one write in order to prevent division overflows (for type (signed) int)? What

Can XOR of two integers go out of bounds?

北慕城南 提交于 2019-12-18 11:38:23
问题 I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The result is 5 . My question is - supposedly the integers (getting generated by the XOR operation) are too large due to this operation: LonelyInteger ^ arr[i] Which leads to a potentially large integer which cannot be represented by the datatype say int in

On-purpose int overflow

╄→гoц情女王★ 提交于 2019-12-18 09:05:06
问题 I'm using the hash function murmur2 which returns me an uint64 . I want then to store it in PostgreSQL, which only support BIGINT (signed 64 bits). As I'm not interested in the number itself, but just the binary value (as I use it as an id for detecting uniqueness (my set of values being of ~1000 values, a 64bit hash is enough for me) I would like to convert it into int64 by "just" changing the type. How does one do that in a way that pleases the compiler? 回答1: You can simply use a type

On-purpose int overflow

◇◆丶佛笑我妖孽 提交于 2019-12-18 09:04:09
问题 I'm using the hash function murmur2 which returns me an uint64 . I want then to store it in PostgreSQL, which only support BIGINT (signed 64 bits). As I'm not interested in the number itself, but just the binary value (as I use it as an id for detecting uniqueness (my set of values being of ~1000 values, a 64bit hash is enough for me) I would like to convert it into int64 by "just" changing the type. How does one do that in a way that pleases the compiler? 回答1: You can simply use a type

Overflow issues when implementing math formulas

戏子无情 提交于 2019-12-18 08:39:19
问题 I heard that, when computing mean value, start+(end-start)/2 differs from (start+end)/2 because the latter can cause overflow. I do not quite understand why this second one can cause overflow while the first one does not. What are the generic rule to implement a math formula that can avoid overflow. 回答1: Suppose you are using a computer where the maximum integer value is 10 and you want to compute the average of 5 and 7. The first method (begin + (end-begin)/2) gives 5 + (7-5)/2 == 5 + 2/2 ==

Clojure - Calculate with big numbers

我与影子孤独终老i 提交于 2019-12-18 05:43:56
问题 I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception? My factorial code is right now: (reduce * (range 1 1001)) . 回答1: You could use the *' operator which supports arbitrary precision by automatically promoting the result to BigInt in case it would overflow: (reduce *' (range 1 1001)) 回答2: Put N at the end of the number which makes it a bigint, (reduce * (range 1N 1001N)) 回答3: Coerce the parameters to clojure.lang.BigInt (reduce * (range (bigint