-
Notifications
You must be signed in to change notification settings - Fork 59
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
Adds support for decoding floating-point typed arrays from RFC8746 #111
base: master
Are you sure you want to change the base?
Conversation
Hi @tgockel, thanks for doing this. I had done some experiments a while back decoding typed arrays into python This is related to #32 and is maybe a simple way to handle it without needing numpy as a dependency. Let me know what you think, I'm open to suggestions. |
I have never seen The biggest issue I see is immutability -- |
If you want it to be immutable, you can wrap the bytes in a memoryview and then cast it, like this: >>> my_array = memoryview(b'\x1f\x85\xebQ\xb8\x1e\t@').cast('d')
>>> assert my_array[0] == 3.14
>>> my_array[0] = 2.16
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
myarray[0] = 2.14
TypeError: cannot modify read-only memory |
That unfortunately doesn't work because the ultimate point of making this read-only is so that it can be used as keys in a dictionary, but
|
This adds support for decoding arrays of floating point numbers of IEEE 754 formats binary16, binary32, and binary64 in both the big- and little-endian form.
I tried writing a little class to represent a float16 array instead of converting to a list of floats and posted it here: https://codereview.stackexchange.com/q/261573/243247. This lets you write an encoder that can just copy the underlying buffer into the output. This could be added to cbor2.types. |
There's an interesting question on hashing -- should the endianness of the generated source affect hashing? Let's say an x86 machine and an AArch64 machine both generate This gets even more hairy when we get into integer v float comparisons. In Python,
This extends to I'm not sure there is a good answer here. My solution of calling |
IMO: No it should not, foreign endian data should always be converted to native endian prior to hashing, and each platform should write arrays in their native format since it can always be unambiguously tagged as such.
Does that hashing behaviour hold true for numpy 1d arrays? Would it just be easier to require numpy for handling these? |
numpy arrays avoid the problem by not being hashable. |
Bump. Any movement on getting various floating point formats encoded with CBOR? |
The problem with immutability/hashability has not been solved yet. If you want this faster, participate in the process of finding solutions. |
I found a solution that works for me - casting to np.floatX, then back to float, then use value_to_encode = float( np.float16( value ) ) |
This adds support for decoding arrays of floating point numbers of IEEE 754 formats binary16, binary32, and binary64 in both the big- and little-endian form.
If this looks good, we can add unsigned and signed integers using the same general ideas...and also encoders for these special markers.