Skip to content
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

Fix negative score order (backward incompatible!) #35

Merged
merged 1 commit into from
Jun 27, 2019

Commits on Jun 26, 2019

  1. 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!
    hltbra committed Jun 26, 2019
    Configuration menu
    Copy the full SHA
    76a0b36 View commit details
    Browse the repository at this point in the history