How to convert float to sortable integer in Python

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 16 13:05:16 CET 2007

robert <no-spam at no-spam-no-spam.invalid> wrote:

> shellon wrote:
>> Hi all:
>>     I want to convert the float number to sortable integer, like the
>> function float2rawInt() in java, but I don't know the internal
>> expression of float, appreciate your help!
> float comparision works well enough for sorting in Python. What is 
> the actual requirement?

Maybe this is the problem?

>>> sorted([-0.0, 0.0, -0.0, 0.0, -0.0])
[-0.0, 0.0, -0.0, 0.0, -0.0]

Java sorting imposes an artificial total ordering on float or double 

> The < relation does not provide a total order on all floating-point
> values; although they are distinct numbers -0.0 == 0.0 is true and a
> NaN value compares neither less than, greater than, nor equal to any
> floating-point value, even itself. To allow the sort to proceed,
> instead of using the < relation to determine ascending numerical
> order, this method uses the total order imposed by
> Double.compareTo(java.lang.Double). This ordering differs from the <
> relation in that -0.0 is treated as less than 0.0 and NaN is
> considered greater than any other floating-point value. For the
> purposes of sorting, all NaN values are considered equivalent and
> equal. 

More information about the Python-list mailing list