Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix negative score order (backward incompatible!)
Before this commit, sorted set negative scores were stored after all positive scores because of their binary representation. For example: >>> struct.pack('>d', -1) b'\xbf\xf0\x00\x00\x00\x00\x00\x00' >>> struct.pack('>d', +1) b'?\xf0\x00\x00\x00\x00\x00\x00' >>> struct.pack('>d', -1) < struct.pack('>d', +1) False This commit introduces the FloatCodec class to encode/decode floats into bytes and vice-versa, preserving their numerical order. For example, the following list is encoded to bytes while keeping and preserves its numerical order: >>> floats = [1.5, -2.5, 3.2, 2.0] >>> sorted(floats, key=FloatCodec().encode) [-2.5, 1.5, 2.0, 3.2] References: * https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754_design_rationale * https://stackoverflow.com/a/43305015/565999 * https://stackoverflow.com/a/12933766/565999 * https://ananthakumaran.in/2018/08/17/order-preserving-serialization.html * https://ananthakumaran.in/2018/08/17/order-preserving-serialization.html#float WARNING: This is a backward incompatible change and requires a major release!
- Loading branch information