2d数组中所有元素的总和,不同的方法 - 不同的结果

时间:2021-11-09 21:29:57

I have a 32x32 matrix of float numbers. I want to get a sum of all the elements in it. But when I use loop:

我有一个32x32的浮点数矩阵。我想得到它中所有元素的总和。但是当我使用循环时:

s = 0
for i in range(len(Temp)):
    for j in range(len(Temp)):
        s += Temp[i][j]

I get 2.795541576006144e-13

我得到2.795541576006144e-13

And when I use NumPy .sum(..) method:

当我使用NumPy .sum(..)方法时:

np.sum(Temp)

I get 3.197442310920451e-13

我得到3.197442310920451e-13

The results are pretty different, so which is right?

结果差异很大,所以哪个是对的?

P.S. can't show the array I use for an example above, because it says my post is mostly code

附:无法显示我用于上面示例的数组,因为它说我的帖子主要是代码

[  1.46992605  -4.38174259   7.11556386  -9.63811941  11.89561903
 -13.57161385  14.72605916 -15.41213544  15.31459028 -14.72605916
  13.65805725 -12.19869213  10.13557074  -7.67002338   4.8085357
  -1.64285853  -1.66207325   4.89389432  -7.94725314  10.81956631
 -13.33521624  15.38692526 -16.97717649  18.43603543 -17.16794834
  16.69578682 -16.16491586  14.16866725 -11.81446896   8.6865325
  -5.51985755   1.90225724]
[ -4.35328971  12.97683985 -21.07324444  28.54397071 -35.22971513
  40.19329204 -43.61226329  45.6441266  -45.35523972  43.61226329
 -40.44930027  36.12728748 -30.01720791  22.71531543 -14.24081776
   4.86544144   4.92234719 -14.49361334  23.53635093 -32.04290906
  39.49318383 -45.56946487  50.27910609 -54.59961979  50.84409039
 -49.44575074  47.87353893 -41.96150782  34.98938345 -25.72577893
  16.34744761  -5.63366904]
[  7.0693589  -21.07324444  34.22109206 -46.35289322  57.20995302
 -65.27036457  70.8224726  -74.12203957  73.65291273 -70.8224726
  65.68609937 -58.66753144  48.74530062 -36.88767066  23.12583318
  -7.90104818  -7.9934581   23.53635093 -38.22095996  52.03486078
 -64.13345052  74.00079549 -81.64883784  88.66497138 -82.56632255
  80.29554219 -77.74240875  68.14179118 -56.81967556  41.7763981
 -26.54681442   9.1485821 ]

[  -9.51375658   28.35981606  -46.0538423    62.38050004  -76.99164451
   87.83913358  -95.31101401   99.75148412  -99.12014562   95.31101401
  -88.39861851   78.95321507  -65.60013876   49.64245339  -31.12213581
   10.63302206   10.75738489  -31.67459976   51.43675893  -70.02714198
   86.30910468  -99.58831705  109.88085054 -119.32297784  111.11557725
 -108.05962098  104.6236814   -91.70342372   76.46641941  -56.22157372
   35.72600205  -12.31192028]
[  11.59254593  -34.55653586   56.11676921  -76.01085923   93.81459028
 -107.03229395  116.13680659 -121.54753509  120.77824689 -116.13680659
  107.71402831  -96.20477092   79.93400035  -60.48950447   37.9224322
  -12.95637487  -13.10791142   38.59561147  -62.6758721    85.3283194
 -105.16794834  121.34871544 -133.89020378  145.39546918 -135.39472263
  131.67102913 -127.48432464  111.74094511  -93.17460164   68.50618579
  -43.53225945   15.00211827]
[ -13.22584025   39.42526736  -64.02316015   86.72016373 -107.03229395
  122.11226329 -132.49952683  138.67258244 -137.79490787  132.49952683
 -122.89004841  109.75923138  -91.19604314   69.01197782  -43.2653908
   14.78182146   14.95470826  -44.03341549   71.50638666  -97.35037734
  119.98524672 -138.44575074  152.75423157 -165.88049418  154.47072474
 -150.22239348  145.44581679 -127.48432464  106.30213618  -78.15814356
   49.66559654  -17.11579326]
[  14.35087294  -42.77890794   69.46917695  -94.09686096  116.13680659
 -132.49952683  143.77036457 -150.46852022  149.51618781 -143.77036457
  133.34347287 -119.09570613   98.95347314  -74.88235957   46.94568469
  -16.03921093  -16.22680405   47.77904004  -77.58895088  105.63131489
 -130.19157937  150.22239348 -165.74799991  179.99082482 -167.61050354
  163.00079549 -157.81790776  138.32855308 -115.34453924   84.8065277
  -53.8903126    18.57171792]
[ -14.92440963   44.48857909  -72.24553223   97.8574686  -120.77824689
  137.79490787 -149.51618781  156.48203715 -155.49164451  149.51618781
 -138.67258244  123.85539968 -102.90817666   77.87505423  -48.82188225
   16.68022253   16.87531285  -49.68854288   80.68981522 -109.85290024
  135.39472263 -156.22607389  172.37216557 -187.184209    174.30910468
 -169.51516835  164.12514504 -143.85689279  119.95431635  -88.19584455
   56.04405417  -19.31394188]
[  14.92440963  -44.48857909   72.24553223  -97.8574686   120.77824689
 -137.79490787  149.51618781 -156.48203715  155.49164451 -149.51618781
  138.67258244 -123.85539968  102.90817666  -77.87505423   48.82188225
  -16.68022253  -16.87531285   49.68854288  -80.68981522  109.85290024
 -135.39472263  156.22607389 -172.37216557  187.184209   -174.30910468
  169.51516835 -164.12514504  143.85689279 -119.95431635   88.19584455
  -56.04405417   19.31394188]
[ -14.35087294   42.77890794  -69.46917695   94.09686096 -116.13680659
  132.49952683 -143.77036457  150.46852022 -149.51618781  143.77036457
 -133.34347287  119.09570613  -98.95347314   74.88235957  -46.94568469
   16.03921093   16.22680405  -47.77904004   77.58895088 -105.63131489
  130.19157937 -150.22239348  165.74799991 -179.99082482  167.61050354
 -163.00079549  157.81790776 -138.32855308  115.34453924  -84.8065277
   53.8903126   -18.57171792]
[  13.22584025  -39.42526736   64.02316015  -86.72016373  107.03229395
 -122.11226329  132.49952683 -138.67258244  137.79490787 -132.49952683
  122.89004841 -109.75923138   91.19604314  -69.01197782   43.2653908
  -14.78182146  -14.95470826   44.03341549  -71.50638666   97.35037734
 -119.98524672  138.44575074 -152.75423157  165.88049418 -154.47072474
  150.22239348 -145.44581679  127.48432464 -106.30213618   78.15814356
  -49.66559654   17.11579326]
[ -11.59254593   34.55653586  -56.11676921   76.01085923  -93.81459028
  107.03229395 -116.13680659  121.54753509 -120.77824689  116.13680659
 -107.71402831   96.20477092  -79.93400035   60.48950447  -37.9224322
   12.95637487   13.10791142  -38.59561147   62.6758721   -85.3283194
  105.16794834 -121.34871544  133.89020378 -145.39546918  135.39472263
 -131.67102913  127.48432464 -111.74094511   93.17460164  -68.50618579
   43.53225945  -15.00211827]
[   9.51375658  -28.35981606   46.0538423   -62.38050004   76.99164451
  -87.83913358   95.31101401  -99.75148412   99.12014562  -95.31101401
   88.39861851  -78.95321507   65.60013876  -49.64245339   31.12213581
  -10.63302206  -10.75738489   31.67459976  -51.43675893   70.02714198
  -86.30910468   99.58831705 -109.88085054  119.32297784 -111.11557725
  108.05962098 -104.6236814    91.70342372  -76.46641941   56.22157372
  -35.72600205   12.31192028]
[ -7.0693589   21.07324444 -34.22109206  46.35289322 -57.20995302
  65.27036457 -70.8224726   74.12203957 -73.65291273  70.8224726
 -65.68609937  58.66753144 -48.74530062  36.88767066 -23.12583318
   7.90104818   7.9934581  -23.53635093  38.22095996 -52.03486078
  64.13345052 -74.00079549  81.64883784 -88.66497138  82.56632255
 -80.29554219  77.74240875 -68.14179118  56.81967556 -41.7763981
  26.54681442  -9.1485821 ]
[  4.35328971 -12.97683985  21.07324444 -28.54397071  35.22971513
 -40.19329204  43.61226329 -45.6441266   45.35523972 -43.61226329
  40.44930027 -36.12728748  30.01720791 -22.71531543  14.24081776
  -4.86544144  -4.92234719  14.49361334 -23.53635093  32.04290906
 -39.49318383  45.56946487 -50.27910609  54.59961979 -50.84409039
  49.44575074 -47.87353893  41.96150782 -34.98938345  25.72577893
 -16.34744761   5.63366904]
[ -1.46992605   4.38174259  -7.11556386   9.63811941 -11.89561903
  13.57161385 -14.72605916  15.41213544 -15.31459028  14.72605916
 -13.65805725  12.19869213 -10.13557074   7.67002338  -4.8085357
   1.64285853   1.66207325  -4.89389432   7.94725314 -10.81956631
  13.33521624 -15.38692526  16.97717649 -18.43603543  17.16794834
 -16.69578682  16.16491586 -14.16866725  11.81446896  -8.6865325
   5.51985755  -1.90225724]
[ -1.46992605   4.38174259  -7.11556386   9.63811941 -11.89561903
  13.57161385 -14.72605916  15.41213544 -15.31459028  14.72605916
 -13.65805725  12.19869213 -10.13557074   7.67002338  -4.8085357
   1.64285853   1.66207325  -4.89389432   7.94725314 -10.81956631
  13.33521624 -15.38692526  16.97717649 -18.43603543  17.16794834
 -16.69578682  16.16491586 -14.16866725  11.81446896  -8.6865325
   5.51985755  -1.90225724]
[  4.35328971 -12.97683985  21.07324444 -28.54397071  35.22971513
 -40.19329204  43.61226329 -45.6441266   45.35523972 -43.61226329
  40.44930027 -36.12728748  30.01720791 -22.71531543  14.24081776
  -4.86544144  -4.92234719  14.49361334 -23.53635093  32.04290906
 -39.49318383  45.56946487 -50.27910609  54.59961979 -50.84409039
  49.44575074 -47.87353893  41.96150782 -34.98938345  25.72577893
 -16.34744761   5.63366904]
[ -7.0693589   21.07324444 -34.22109206  46.35289322 -57.20995302
  65.27036457 -70.8224726   74.12203957 -73.65291273  70.8224726
 -65.68609937  58.66753144 -48.74530062  36.88767066 -23.12583318
   7.90104818   7.9934581  -23.53635093  38.22095996 -52.03486078
  64.13345052 -74.00079549  81.64883784 -88.66497138  82.56632255
 -80.29554219  77.74240875 -68.14179118  56.81967556 -41.7763981
  26.54681442  -9.1485821 ]
[   9.51375658  -28.35981606   46.0538423   -62.38050004   76.99164451
  -87.83913358   95.31101401  -99.75148412   99.12014562  -95.31101401
   88.39861851  -78.95321507   65.60013876  -49.64245339   31.12213581
  -10.63302206  -10.75738489   31.67459976  -51.43675893   70.02714198
  -86.30910468   99.58831705 -109.88085054  119.32297784 -111.11557725
  108.05962098 -104.6236814    91.70342372  -76.46641941   56.22157372
  -35.72600205   12.31192028]
[ -11.59254593   34.55653586  -56.11676921   76.01085923  -93.81459028
  107.03229395 -116.13680659  121.54753509 -120.77824689  116.13680659
 -107.71402831   96.20477092  -79.93400035   60.48950447  -37.9224322
   12.95637487   13.10791142  -38.59561147   62.6758721   -85.3283194
  105.16794834 -121.34871544  133.89020378 -145.39546918  135.39472263
 -131.67102913  127.48432464 -111.74094511   93.17460164  -68.50618579
   43.53225945  -15.00211827]
[  13.22584025  -39.42526736   64.02316015  -86.72016373  107.03229395
 -122.11226329  132.49952683 -138.67258244  137.79490787 -132.49952683
  122.89004841 -109.75923138   91.19604314  -69.01197782   43.2653908
  -14.78182146  -14.95470826   44.03341549  -71.50638666   97.35037734
 -119.98524672  138.44575074 -152.75423157  165.88049418 -154.47072474
  150.22239348 -145.44581679  127.48432464 -106.30213618   78.15814356
  -49.66559654   17.11579326]
[ -14.35087294   42.77890794  -69.46917695   94.09686096 -116.13680659
  132.49952683 -143.77036457  150.46852022 -149.51618781  143.77036457
 -133.34347287  119.09570613  -98.95347314   74.88235957  -46.94568469
   16.03921093   16.22680405  -47.77904004   77.58895088 -105.63131489
  130.19157937 -150.22239348  165.74799991 -179.99082482  167.61050354
 -163.00079549  157.81790776 -138.32855308  115.34453924  -84.8065277
   53.8903126   -18.57171792]
[  14.92440963  -44.48857909   72.24553223  -97.8574686   120.77824689
 -137.79490787  149.51618781 -156.48203715  155.49164451 -149.51618781
  138.67258244 -123.85539968  102.90817666  -77.87505423   48.82188225
  -16.68022253  -16.87531285   49.68854288  -80.68981522  109.85290024
 -135.39472263  156.22607389 -172.37216557  187.184209   -174.30910468
  169.51516835 -164.12514504  143.85689279 -119.95431635   88.19584455
  -56.04405417   19.31394188]
[ -14.92440963   44.48857909  -72.24553223   97.8574686  -120.77824689
  137.79490787 -149.51618781  156.48203715 -155.49164451  149.51618781
 -138.67258244  123.85539968 -102.90817666   77.87505423  -48.82188225
   16.68022253   16.87531285  -49.68854288   80.68981522 -109.85290024
  135.39472263 -156.22607389  172.37216557 -187.184209    174.30910468
 -169.51516835  164.12514504 -143.85689279  119.95431635  -88.19584455
   56.04405417  -19.31394188]
[  14.35087294  -42.77890794   69.46917695  -94.09686096  116.13680659
 -132.49952683  143.77036457 -150.46852022  149.51618781 -143.77036457
  133.34347287 -119.09570613   98.95347314  -74.88235957   46.94568469
  -16.03921093  -16.22680405   47.77904004  -77.58895088  105.63131489
 -130.19157937  150.22239348 -165.74799991  179.99082482 -167.61050354
  163.00079549 -157.81790776  138.32855308 -115.34453924   84.8065277
  -53.8903126    18.57171792]
[ -13.22584025   39.42526736  -64.02316015   86.72016373 -107.03229395
  122.11226329 -132.49952683  138.67258244 -137.79490787  132.49952683
 -122.89004841  109.75923138  -91.19604314   69.01197782  -43.2653908
   14.78182146   14.95470826  -44.03341549   71.50638666  -97.35037734
  119.98524672 -138.44575074  152.75423157 -165.88049418  154.47072474
 -150.22239348  145.44581679 -127.48432464  106.30213618  -78.15814356
   49.66559654  -17.11579326]
[  11.59254593  -34.55653586   56.11676921  -76.01085923   93.81459028
 -107.03229395  116.13680659 -121.54753509  120.77824689 -116.13680659
  107.71402831  -96.20477092   79.93400035  -60.48950447   37.9224322
  -12.95637487  -13.10791142   38.59561147  -62.6758721    85.3283194
 -105.16794834  121.34871544 -133.89020378  145.39546918 -135.39472263
  131.67102913 -127.48432464  111.74094511  -93.17460164   68.50618579
  -43.53225945   15.00211827]
[  -9.51375658   28.35981606  -46.0538423    62.38050004  -76.99164451
   87.83913358  -95.31101401   99.75148412  -99.12014562   95.31101401
  -88.39861851   78.95321507  -65.60013876   49.64245339  -31.12213581
   10.63302206   10.75738489  -31.67459976   51.43675893  -70.02714198
   86.30910468  -99.58831705  109.88085054 -119.32297784  111.11557725
 -108.05962098  104.6236814   -91.70342372   76.46641941  -56.22157372
   35.72600205  -12.31192028]
[  7.0693589  -21.07324444  34.22109206 -46.35289322  57.20995302
 -65.27036457  70.8224726  -74.12203957  73.65291273 -70.8224726
  65.68609937 -58.66753144  48.74530062 -36.88767066  23.12583318
  -7.90104818  -7.9934581   23.53635093 -38.22095996  52.03486078
 -64.13345052  74.00079549 -81.64883784  88.66497138 -82.56632255
  80.29554219 -77.74240875  68.14179118 -56.81967556  41.7763981
 -26.54681442   9.1485821 ]
[ -4.35328971  12.97683985 -21.07324444  28.54397071 -35.22971513
  40.19329204 -43.61226329  45.6441266  -45.35523972  43.61226329
 -40.44930027  36.12728748 -30.01720791  22.71531543 -14.24081776
   4.86544144   4.92234719 -14.49361334  23.53635093 -32.04290906
  39.49318383 -45.56946487  50.27910609 -54.59961979  50.84409039
 -49.44575074  47.87353893 -41.96150782  34.98938345 -25.72577893
  16.34744761  -5.63366904]
[  1.46992605  -4.38174259   7.11556386  -9.63811941  11.89561903
 -13.57161385  14.72605916 -15.41213544  15.31459028 -14.72605916
  13.65805725 -12.19869213  10.13557074  -7.67002338   4.8085357
  -1.64285853  -1.66207325   4.89389432  -7.94725314  10.81956631
 -13.33521624  15.38692526 -16.97717649  18.43603543 -17.16794834
  16.69578682 -16.16491586  14.16866725 -11.81446896   8.6865325
  -5.51985755   1.90225724]

1 个解决方案

#1


3  

They are both correct. Please see that it is such small number that in both cases it is almost zero (because of e-13 part).

他们都是正确的。请注意它是如此之小的数字,在两种情况下它几乎为零(因为e-13部分)。

Please see this post about floating point arithmetic issues and limitations: https://docs.python.org/3/tutorial/floatingpoint.html

请参阅此帖有关浮点运算问题和限制:https://docs.python.org/3/tutorial/floatingpoint.html

#1


3  

They are both correct. Please see that it is such small number that in both cases it is almost zero (because of e-13 part).

他们都是正确的。请注意它是如此之小的数字,在两种情况下它几乎为零(因为e-13部分)。

Please see this post about floating point arithmetic issues and limitations: https://docs.python.org/3/tutorial/floatingpoint.html

请参阅此帖有关浮点运算问题和限制:https://docs.python.org/3/tutorial/floatingpoint.html