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

Optionally set data type #115

Closed
aidanheerdegen opened this issue May 14, 2020 · 8 comments
Closed

Optionally set data type #115

aidanheerdegen opened this issue May 14, 2020 · 8 comments

Comments

@aidanheerdegen
Copy link
Contributor

When reading in namelists I would like to be able to preserve the precision of the values being read in using the python decimal library. If numerical values are converted from string to a numerical type makes it more difficult to do this.

Options to support this might be to specify only strings to be returned when read in, or specify decimal.Decimal as one of the types to cast to.

@marshallward
Copy link
Owner

Reads ought to be in double precision, is that not happening?

@aidanheerdegen
Copy link
Contributor Author

Yeah, but you can't represent some numbers perfectly with floating point, so you get weird results in some cases. See the linked payu issues.

@marshallward
Copy link
Owner

Oh, I see, you do not want to use base2 floating point...
That is a tall order, we might be better off doing some in-person discussion.

@aidanheerdegen
Copy link
Contributor Author

Not possible to make adding to/modifying this array user settable through API?

https://github.com/marshallward/f90nml/blob/master/f90nml/parser.py#L725

@marshallward
Copy link
Owner

marshallward commented May 14, 2020

It may be possible, but a bit against the spirit of the module. For one thing, there's no way to indicate that the number is base10 in the namelist.

(Theoretically one could assign a base10 "kind"; however, kind cannot be assigned in a namelist, nor have I ever seen a compiler actually do this, so let's ignore that possibility).

So we would have to introduce new infrastructure which lets us pre-assign a type to this data, presumably with some sort of metadata during initialization of the Parser. Even then, we'd be storing this number in some format which Fortran does not understand, and going back to base2 would probably just create another problem.

Before considering this, would something like this work for you?

  • read in the value at full precision: e.g. 0.10000000000000001

  • Write it to a string in some truncated precision which represents your decimal accuracy (say 10 digits)

  • pass this string to decimal.Decimal, do your processing, and generate whatever integer you are trying to get out of this thing.

Would something like that work? If not, then let's keep talking.

@marshallward
Copy link
Owner

BTW this has come up before, and I resisted it back then, but perhaps I should be more open to allowing users to add new types to that list, and providing some infrastructure to access them.

In my opinion, such an entity would no longer be a namelist, but I can see how it could be useful.

@aidanheerdegen
Copy link
Contributor Author

Clearly you've thought this through before, and I can see what you're saying, this is input into a program which is using base2 floating point.

I'd say leave it and just point me back to this issue when I forget I've raised this before and whinge again!

@marshallward
Copy link
Owner

BTW it was not you who had suggested it before!

I am coming around to the idea of letting users add their own types, I will keep that one in mind.

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

2 participants