nansencenter / DAPPER

Data Assimilation with Python: a Package for Experimental Research
https://nansencenter.github.io/DAPPER
MIT License
342 stars 119 forks source link

round2 function in math module #15

Closed yumengch closed 3 years ago

yumengch commented 3 years ago

I'm thinking about adding some unit tests and tried to do some simple tests for the round2 function and understand its behaviour.

It works with integer input for the number of significant figures.

With decimal input which is viewed as the precision, it can generate results I don't understand:

For example:

>>> round2(1234.4, 2.7)
>>> array(1234.)
>>> round2(1234.4, 2.5)
>>> array(1235.)
>>> dpr.round2(1234.45, 0.3)
>>> array(1234.5)
>>> dpr.round2(1234.45, 0.4)
>>> array(1234.4)

Such behaviour seems appear whenever the last digit of the precision is not 1. I believe the reason for such behaviour arises from the use of function: _round2prec(num, prec)

I wonder if this is done by intention, or if the precision should be limited in the form of 0.1, 0.01, etc.

patnr commented 3 years ago

Good spot. Interesting 1st issue. I believe it is has the right behavior. I have updated the docstring to explain it better.

Please check if you agree with it. Otherwise feel free to re-open the issue.

yumengch commented 3 years ago

Thanks. Interesting definition here. I didn't realise it.

patnr commented 3 years ago

Actually, on second thought. Some of the examples in the docstring are just too unexpected.

No wonder it seemed wrong to you.

Also, the following might be wrong. The idea was that an uncertainty (which will serve as the precision) of 0.53 should result in numbers with that uncertainty get rounded to multiples of it, then rounded off to 1 decimal. For example, maybe a variable has value 1.87. Then it would be rounded to 2.12 (4*0.53) and subsequently to 2.0. However, this is probably not the right way to print values with uncertainty. It should simply be 1.8.

Fixing this will also probably produce less of the unexpected behaviours. I will look into it tomorrow.

patnr commented 3 years ago

Hey, I made quite a bit more changes.

I made a (local) branch for these changes, merged them, and pushed the master. But AFAICT github doesn't provide showing my merges like a pull request. So I guess to get a good view you have to git diff across the commits of today.

As you can tell, I can be quite confused. So don't hesitate to voice your opinion!