Size of long array

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52053

There is no integer overflow in this example. The largest array (1000000000) is still within the range of default integer (2147483647).

Edit: As Dominique d’Humieres indicated in the GCC bugzilla, it’s simply due to summing values of different magnitude. You can see it in NumPy:too:

>>> import numpy as np
>>> a = np.array([1.6777216e7,1.],np.float32)
>>> np.sum(a)
16777216.0

NumPy however provides

dtype : dtype, optional
The type of the returned array and of the accumulator in which the
elements are summed. The dtype of a is used by default unless a
has an integer dtype of less precision than the default platform
integer. In that case, if a is signed then the platform integer
is used while if a is unsigned then an unsigned integer of the
same precision as the platform integer is used.

This gives the expected output:

>>> np.sum(a,dtype=np.float64)
16777217.0

I imagine what @msz59 was suggesting, was to offer a “higher” precision accumulator, similar to NumPy. You could consider it syntactic sugar for the existing approach with an explicit conversion using the real intrinsic.


Edit2: before anyone begins to question the quality of gfortran for something which is an issue of finite-precision arithmetic, ifx and ifort suffer from the same problem:

$ ifx -warn all -O3 sumkind.f90 
$ ./a.out
      100000   50095.35       50095.5339293266        50000.0000000000     
     1000000   499784.1       499801.279849011        500000.000000000     
    10000000   5000416.       5000626.31909301        5000000.00000000     
   100000000  1.6777216E+07   49997782.4513367        50000000.0000000     
  1000000000  1.6777216E+07   499995740.004367        500000000.000000     
$ ifort -warn all -O3 sumkind.f90 
$ ./a.out
      100000   50095.56       50095.5339293266        50000.0000000000     
     1000000   499800.8       499801.279849011        500000.000000000     
    10000000   5000632.       5000626.31909298        5000000.00000000     
   100000000  4.9997556E+07   49997782.4513326        50000000.0000000     
  1000000000  1.3421773E+08   499995740.003895        500000000.000000  

Curiously, ifort appears to use a different summation algorithm, while the random number generators appear to be the same.