Interesting problem comparing strings with integer values...

Chris Spencer clspence at one.net
Wed Jan 15 22:55:38 EST 2003


On Wed, 15 Jan 2003 17:56:04 -0800, Chad Netzer <cnetzer at mail.arc.nasa.gov>
wrote:
>Again, I know nothing of SQL, but if you are doing a straightforward 
>lexicographical comparison here, this doesn't make sense.  These 
>values, if they are thought of as binary strings, are NOT ordered by 
>numerical value.
>
>For example, in Python (or any other common language with string 
>compare, that I know of), the first value is considered to be GREATER 
>than the second, ven though numerically it is clearly smaller, ie.:
>
>Python 2.2.2 (#1, Jan  3 2003, 12:42:27) 
>>>> a = "1111100111"
>>>> b = "110101101000"
>>>> a > b
>1
Ahhh..except the number of bits would be the same, so it'd be more like:
>>> a="001111100111"
>>> b="110101101000"
>>> a>b
>0
Which, as you can see, does evaluate correctly.

>
>Now, if things are different in SQL, then that is that.  But I could 
>only suggest that this is not the right newsgroup to discuss the issue 
>(despite most of the posters being nice, friendly people; of which I 
>must right now seem the exception)

	This isn't necessarily an SQL problem.  It's a problem of "how do I
compare two strings that have arbitrary integer values (positive and/or
negative) without the possibility of casting them as ints?"  And I think that's
a valid programming question.  And since I'm implementing a solution in Python,
I think it's a valid Python question.
	The only invariant in my problem is that the numbers MUST be represented
in strings.  I don't really care which base they are in, or if I have to pad
them out, or muck with them in weird ways.  The reason they MUST be strings is
because of limitations in SQL and the fact that this solution must be the same
across different SQL syntaxes.  That's the "problem" I'm trying to "solve" with
this Python programming question.
	Programming is about solving problems.  I asked a programming question
with a certain number of constraints as to what the solution could be.  I do NOT
think it's off topic for this newsgroup just because you don't like the
constraints.  If you can't help me, that's okay.  But please don't flame me.

Chris.

PS: Here's my take on conversion of ints and floats to binary.
However, this doesn't solve for things like: 5>-2 due to the "twos-complement"
nature of the binary representation of negative numbers.  So I'm kinda back to
square 1 on this...

def int2binary(i_IntVal):
	result=""
	blah=i_IntVal
	for i in range(64):
		foo=blah & 1
		result=`foo`+result
		blah=blah>>1
	return result

def float2binary(f_FloatVal):
	result=""
	blah=float(f_FloatVal)
	blah=`blah`
	neg=0
	if blah[0]=="-": neg=1
	parts=blah.split(".")
# cap the precision to 9 decimal places
# fill any remaining spaces with 0s
	parts[1]=parts[1][:9].ljust(9)
	parts[1]=parts[1].replace(" ","0")
	parts[0]=int(parts[0])
# turn the fraction into a negative value
# if the whole number is a negative value
	if neg: parts[1]=-1*int(parts[1])
	else: parts[1]=int(parts[1])
	return int2binary(parts[0])+"."+int2binary(parts[1])






More information about the Python-list mailing list