Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dim_sum and sum returning a different result #126

Closed
mattiarighi opened this issue Feb 12, 2020 · 4 comments
Closed

dim_sum and sum returning a different result #126

mattiarighi opened this issue Feb 12, 2020 · 4 comments

Comments

@mattiarighi
Copy link

Describe the bug
I'm encountering a strange behaviour of the sum function, which returns a different result than dim_sum when applied to a 4 dimensional variable.

Provide the following:
The 4 D variable tmp which I'm processing looks like this:

Variable: tmp
Type: float
Total Size: 2388787200 bytes
            597196800 values
Number of Dimensions: 4
Dimensions and sizes:   [12] x [48] x [720] x [1440]
Coordinates: 
Number Of Attributes: 1
  _FillValue :  9.96921e+36

The two sum statements I'm comparing are as follows:

print(sum(tmp))
print(dim_sum(dim_sum(dim_sum(dim_sum(tmp)))))

and they return different values:

(0)     1.674379e+12
(0)     1.829703e+12

Note that the same operation with avg / dim_avg gives identical results.

Computing environment

@mattiarighi
Copy link
Author

This problem can be reproduced using this simple script:

begin

  var = new((/12, 48, 720, 1440/), float)
  var = 0.5

  total1 = sum(var)
  total2 = dim_sum(dim_sum(dim_sum(dim_sum(var))))
  
  print("total1 = " + total1)
  print("total2 = " + total2)

end

which returns:

(0)     total1 = 8.38861e+06
(0)     total2 = 2.98598e+08

The correct result is total2, since 12x48x720x1440 x 0.5 = 2.98598e+08.

@rbrownrigg
Copy link
Contributor

rbrownrigg commented Feb 12, 2020 via email

@mattiarighi
Copy link
Author

Hi @rbrownrigg, thanks for your answer.
Indeed, declaring var as double solves the problem.

However, the problem occurs also for relatively small variables, e.g.:

var = new((/5000, 5000/), float)

which is surprising, also given that the corresponding avg operation works fine.

@pilotchute
Copy link

Looks like the issue was floating point precision, and declaring var to be double solved the problem. So, I'm going to close this ticket.

If there is a more fundamental problem, please open another ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants