Skip to content

Commit

Permalink
Always encode floats efficiently.
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfix committed Oct 2, 2024
1 parent 7de2e75 commit 03ccfec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion python-ecosys/cbor2/cbor2/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ def encode_float(encoder, value):
elif math.isinf(value):
encoder.write(b"\xf9\x7c\x00" if value > 0 else b"\xf9\xfc\x00")
else:
encoder.write(struct.pack(">Bd", 0xFB, value))
std = struct.pack(">f", value)
if struct.unpack(">f", std)[0] != value:
encoder.write(struct.pack(">Bd", 0xFB, value))
else:
half = struct.pack(">e", value)
if struct.unpack(">e", half)[0] != value:
encoder.write("\xfa" + std)
else:
encoder.write("\xf9" + half)


def encode_int(encoder, value):
Expand Down
2 changes: 1 addition & 1 deletion python-ecosys/cbor2/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="1.1.0", pypi="cbor2")
metadata(version="1.1.1", pypi="cbor2")

package("cbor2")

0 comments on commit 03ccfec

Please sign in to comment.