Against PEP 240

Bengt Richter bokr at accessone.com
Fri Jun 1 18:51:27 CEST 2001


On 31 May 2001 16:49:34 -0700, aahz at panix.com (Aahz Maruch) wrote:

>In article <3b16a154.9545695 at wa.news.verio.net>,
>Bengt Richter <bokr at accessone.com> wrote:
>>On 31 May 2001 10:28:32 -0700, aahz at panix.com (Aahz Maruch) wrote:
>>>
>>>One can have floating-point BCD, which is what I'm currently working on.
>>>It's a real pleasure to write
>>>
>>>    1.2e2346257231235019862340987162305961293865 * 2.0
>>>
>>>and get
>>>
>>>    2.40e2346257231235019862340987162305961293865
>>>
>>>Note carefully the mantissa with its trailing zero.
>>
>>Just curious, is that based on something like [ <long>, <long> ]
>>with the second being a decimal exponent, analogous to binary fp?
>>
>>Or, are you maintaining a decimal digit sequence representation
>>with some decimal "digit" radix like 10^4 or 10^9 (with each "digit"
>>represented in binary)? Or?
>
>The exponent is a long, but the digits are a tuple.  See
>http://starship.python.net/crew/aahz/

Interesting. I note that Cowlishaw's proposed single precision format
fits exactly into the bit fields of a double. I'd bet you could get
mileage out of that, but I don't have time to play with it now...
At least sign tests, negate, absolute value, equality tests (ok, since
all bits are accounted for).

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 ;-)




More information about the Python-list mailing list