Against PEP 240

Bengt Richter bokr at accessone.com
Fri Jun 1 14:18:00 EDT 2001


On Fri, 01 Jun 2001 16:51:27 GMT, bokr at accessone.com (Bengt Richter)
wrote:
[...]
>Ok, here's a thing to encode and decode a decimal tuple (v,x10) into
>a float, where v is an integer up to 15 digits and x10 is an exponent
>dec() decodes it back (well, I should have done a long() on the first
>return element).
>
>import math
>def enc(v,x10):
>	"enc(v,x10) encode up to 15-digit integer signed v and 10-bit
>signed x10 in float"
>	if v < 0:
>		return math.ldexp(v-2L**52,x10)
>	else:
>		return math.ldexp(v+2L**52,x10)
>def dec(d):
>	" decode float that was encoded with enc() above"
>	z = math.frexp(d)	# (f,x2)
>	if d < 0:
>		return z[0]*2L**53 + 2L**52, z[1]-53
>	else:
>		return z[0]*2L**53 - 2L**52, z[1]-53
>
>Maybe someone will find this a useful take-off point...
>No warranty, this is hot OTTOMH ;-)
>

Hm, replacing "z[0]*2L**53" by "math.ldexp(z[0],53)"
would seem an improvement. Also, BTW, not just "=="
but also ">" and "<" on encoded values should return
valid results, (I think ;-)

Note the __doc__ line wrapped in the mail.

Note also, if you try to encode a v with decimals, it
will effectively round it when it adds the 2L**52, e.g.,

 >>> dec(enc(123.45,3))
 (123.0, 3)
 >>> dec(enc(123.55,3))
 (124.0, 3)
 >>>

I guess if there was a third optional argument to enc()
saying how many decimals were to the right,
that could be taken care of.



More information about the Python-list mailing list