# Size of long array

**URL:** <https://fortran-lang.discourse.group/t/size-of-long-array/3676>\
**Category:** Help\
**Created:** [June 6, 2022, 9:05pm UTC](https://fortran-lang.discourse.group/t/size-of-long-array/3676 "2022-06-06T21:05:08Z")\
**Posts on this page:** 1\
**Showing post:** 46

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [June 7, 2022, 8:39pm UTC](https://fortran-lang.discourse.group/t/size-of-long-array/3676/46 "2022-06-07T20:39:03Z")

</div>

[https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=52053](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:

```txt
>>> 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:

```txt
>>> 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:

```txt
$ 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     

```

```txt
$ 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.

---

_[View the full topic](https://fortran-lang.discourse.group/t/size-of-long-array/3676)._
