How to convert float to sortable integer in Python

Gabriel Genellina gagsl-py at yahoo.com.ar
Wed Jan 17 12:20:01 CET 2007


At Wednesday 17/1/2007 03:36, shellon wrote:

>I'm sorry I mistake the function name, the function is
>floatToRawIntBits(), it convert ieee 754 floating point number to
>integer,  e.g. if f1>f2 then floatToRawBits(f1) > floatToRawBits(f1)
>I want convert floating point number to sortable string to index in
>Lucene, so I want first to conver the floating point number to integer
>first, and than convert the integer to sortable string?

The following two functions may be useful; they do more-or-less the 
same thing, but returning a string instead of an integer.
Note that this property: f1>f2 => floatToRawBits(f1) > 
floatToRawBits(f2), only holds for  nonnegative numbers; this code 
has the same restriction.

The code assumes a few things: float(repr(x))==x for 0.5<=x<1 (that 
is, repr(x) is exact in that range); the maximum double value has an 
exponent (as given by frexp) less than 5000; the minimum positive 
value has an exponent greater than -5000; 0 is the only number having 
mantissa==0; and surely I'm assuming many more but I'm not aware of 
them. I think it's not dependent on a particular FP hardware or representation.
Beasts like denormals, INFs and NaNs are not considered at all. It 
appears to work OK for "normal" numbers, but I've not checked all 
corner cases. It was only tested on Windows XP.
After such long disclaimer... I hope it's useful :)

--- begin fpsrepr.py ---
import math

def float2srepr(f):
     """Convert a floating point number into a string representation
     which can be sorted lexicographically maintaining the original
     numerical order (only for numbers >= 0).
     That is, given f1,f2 >= 0, f1<f2 <=> float2srepr(f1)<float2srepr(f2)
     Denormals, INFs, NaNs are not handled at all!

     format: Seeeem*
       S   : sign, "0" for +, else "-"
       eeee: exponent + 5000 (never negative)
       m*  : mantissa, one or more chars as needed,
             0.5<=mantissa<1, without the leading "0."
     """
     try: m, e = math.frexp(f)
     except ValueError,E:
         E.args = list(E.args) + [repr(f)]
         raise
     if m < 0:
         sign = '-'
         m = -m
     else:
         sign = '0'
     e = e+5000
     if m==0.0: e = 0
     return "%s%04.4d%s" % (sign, e, repr(m)[2:])

def srepr2float(s):
     """Convert a string in the format used by float2srepr
     into a floating point number.
     srepr2float(float2srepr(x)) == x
     """
     sign, e, m = s[0], s[1:5], s[5:]
     neg = sign == '-'
     e = int(e)
     e = e-5000
     m = float('0.'+m)
     if m==0: f = 0
     else: f = math.ldexp(m, e)
     if neg: f = -f
     return f

# test

def find_eps(): # smallest x such 1+x!=1
     prev, curr = 1.0, 0.5
     while 1.0+curr > 1.0 and curr < prev:
         prev = curr
         curr /= 2.0
     return prev

def find_minf(): # smallest x such x>0
     prev, curr = 1.0, 0.5
     while curr > 0.0 and curr < prev:
         prev = curr
         curr /= 2
     return prev

eps = find_eps()
print "eps=%r %r" % (eps, 1.0+eps)
minf = find_minf()
print "minf=%r %r" % (minf, -minf)

values = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,
     1.0,1.0+eps,1.0+2*eps,1.0-eps,1.0-2*eps,
     2,3,10,10.0000001,10.00000000001,
     123.456,1234.567,12345.678,123456.789,1.234e10,
     123412341234,123412341234123412341234.0,1.234e20,
     1e100,1e200,1e300,math.pi,math.e]
values += [1.0/x for x in values if x>0] + [eps, minf]
values += [-x for x in values]
values = sorted(values)
for x in values:
    s = float2srepr(x)
    xr = srepr2float(s)
    print '%-30r %s' % (x, s)
    assert x==xr, '%r!=%r' % (x, xr)

f2svalues = [float2srepr(x) for x in values if x>=0]
s2fvalues = [srepr2float(x) for x in sorted(f2svalues)]
assert s2fvalues == sorted(s2fvalues), s2fvalues
--- end fpsrepr.py ---


-- 
Gabriel Genellina
Softlab SRL 


	

	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list