Skip to content

Float & signed int slot encoding: guarantee lexicographic ordering #2

@tv42

Description

@tv42

Floats can be interpreted as sign-and-magnitude integers, while keeping the same ordering. This is close to lexicographic ordering, but not quite there (every ellipsis is all zeroes):

1…11  -3
1…10  -2
1…01  -1
1…00  -0
0…00  +0
0…01  +1

The IEEE float and double formats were designed so that the numbers are “lexicographically ordered”, which – in the words of IEEE architect William Kahan means “if two floating-point numbers in the same format are ordered ( say x < y ), then they are ordered the same way when their bits are reinterpreted as Sign-Magnitude integers.” http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

This can be made to match the usual ordering with something like the following:

  • if highest bit is set (= negative), flip all bits of the float.
  • if highest bit is not set (= positive), flip the sign bit

Having input with someone who deals with floats more would be useful. I mostly play with just integers.

Resources

Not directly related but interesting:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions