10000 Support floating point hex representation in loadtxt (Trac #1924) · Issue #2517 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Support floating point hex representation in loadtxt (Trac #1924) #2517

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

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 1 comment · Fixed by #5504
Closed

Support floating point hex representation in loadtxt (Trac #1924) #2517

numpy-gitbot opened this issue Oct 19, 2012 · 1 comment · Fixed by #5504

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1924 on 2011-08-03 by trac user claumann, assigned to unknown.

Python supports a hexadecimal representation for floating numbers in float.hex() and float.fromhex():

a = 3.14

a.hex()
'0x1.91eb851eb851fp+1'

float.fromhex(a.hex())
3.14

Numpy's loadtxt does not support this hex text format but it seems like it would be a useful enhancement. For example, a simple implementation might come from changing the default converter returned by _getconv for columns of type float:

def _floatconv(x):
try:
return float(x)
except ValueError:
pass
return float.fromhex(x)

And then change the return float in _getconv to return _floatconv. This converter obeys essentially the same semantics (and exceptions) as float() except that it falls back on fromhex before giving up.

A slightly less permissive change would be to check that the string x begins with '0x' before calling fromhex and throwing a ValueError if not. The code above would accept 'bad' and convert it to 2989, which might be unnecessarily permissive.

Best, Chris

@numpy-gitbot
Copy link
Author

@rgommers wrote on 2011-08-10

see #133

charris pushed a commit to charris/numpy that referenced this issue Feb 13, 2015
Add _floatconv to npyio.py as a default floating point converter. This
uses float() as a type conversion with a fallback on (ValueError) to
float.fromhex().

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

Successfully merging a pull request may close this issue.

1 participant
0