将空数组传递给sum()和product()

roy*_*vib 2 python arrays fortran

在下面的代码中,我将一个空数组传递给内部函数sum()product()函数.

program test
    implicit none
    integer, allocatable :: A(:)

    allocate( A( 0 ) )

    print *, "sum     = ", sum( A )
    print *, "product = ", product( A )
end
Run Code Online (Sandbox Code Playgroud)

然后,我尝试过的所有编译器给出了相同的结果:

 sum     =  0
 product =  1
Run Code Online (Sandbox Code Playgroud)

所以我想知道(1)是否允许将空数组传递给那些函数,以及(2)如果是这样,结果保证为0和1(根据Fortran标准).为了比较,一些其他语言(例如Python3)也给出0和1,其中(我猜)可能与极限sum( [1,2,...,n] )product( [1,2,...,n] )to有关n -> 0.

>>> import numpy as np
>>> np.sum( [] )
0.0
>>> np.prod( [] )
1.0
Run Code Online (Sandbox Code Playgroud)

fra*_*lus 5

是的,允许将零大小的数组传递给那些内在函数(以及许多其他数组),是的,这些结果是Fortran标准明确要求的.

对于product(F2008,13.7.133):

PRODUCT(ARRAY)的结果的值等于ARRAY所有元素乘积的处理器相关近似值,或者如果ARRAY的大小为零,则值为1.

对于sum(F2008,13.7.161):

SUM(ARRAY)的结果的值等于ARRAY所有元素之和的处理器相关近似值,或者如果ARRAY的大小为零,则值为零.