1

an API that I cannot influence is saving certain floating point numbers as contiguousarray with 64 bit, so that a value of -0.30273306 is saved as -4624246862885421056.

Now how can I retrieve the original value from this large integer?

1 Answer 1

2

You can use struct module. Firstly we pack i as long long (8 bytes signed integer). Then we unpack it as double (8-byte floating point number).

import struct

i = -4624246862885421056
f = struct.unpack("d", struct.pack("q", i))
Sign up to request clarification or add additional context in comments.

5 Comments

that looks cumbersome but thanks a lot =) does this also work for numpy arrays or just for single numbers?
@CarstenD, that's best option in my opinion. If you're reading this values from file, you can read them as double. About numpy arrays I don't clearly understand.
how would I read the data as double? several of those numbers are stored as meta data in an hdf5 file and I need to access it by a key that returns an np.array which contains multiple numbers
@CarstenD, it's much more complicated question. I'm not familiar with this file format, so I can't help you using my own experience. If in codec you use to read this file there's some option to override reader, you can use it.
@CarstenD, about numpy array, I think there's an option how to apply same changes to all elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.