[PEP] number accuracy

Luke Kenneth Casson Leighton lkcl@samba-tng.org
Wed, 9 Oct 2002 12:28:39 +0000


number accuracy, by Luke Kenneth Casson Leighton <lkcl@samba-tng.org>

abstract
--------

python classes that support number types have an automatic
cast mechanism (hierarchical).  unfortunately, the existing
mechanism can only really cope with builtin types.
any user-defined number types cannot be type-cast to or from
any other user-defined number types because every single
other class, including the builtin number types, need to know
about every single other number type.

the proposal is therefore to provide a means by which the
accuracy of numbers may be determined independently.

number ranking
--------------

The Plan is to provide a function that returns a tuple or a
list of tuples indicating the accuracy of the numerical class's
resolution.

then, type-casting may occur by first checking the length
of the list and further by checking the tuples in the lists.

the tuple consists of:

- 0 if the representation can do positive numbers, 1 if it can
  do positive and negative
- the number of binary digits (ln2 of the max range) in the mantissa
- optionally the number of digits in the exponent

example:

class UserLong:

	def __accuracy__(self):
		
		return (1, 1000000000000000L) # presumed infinite!

class UserIEEEFloat:

	def __accuracy__(self):
		
		return (1, 32, 12) # does up to 32-bit mantissa, 12-bit exponent

when complex numbers are involved, you want to do this:

class UserComples:

	def __accuracy__(self):

		return [(1, 32, 12), (1, 32, 12)]

which indicates that both the real and imaginary components of
the 2-dimensional number space are capable of supporting floating
point e.g. "0.95829-32.59j"

usage
-----

a simple test determines whether numbers can be typecast:


	if num1.__accuracy__() > num2.__accuracy():
		return num1
	else:
		return num2


type-casting
------------

*thinks*...

i believe... also... that... the same principle may be
applied to actually performing a typecast...

def __mantissa__(self, dimension=0):

def __exponent__(self, dimension=0):

as long as these two functions return numbers of built-in
types (int and long, basically) then it will be possible
to type-cast between any user-defined numerical types.
	

-- 
----------------------------------------------------------
this message is private, confidential, and is intented for
the specified recipients only.  if you received in error,
altered, deleted, modified, destroyed or interfered with
the contents of this message, in whole or in part, please
inform the sender (that's me), immediately.

if you, the recipient, reply to this message, and do not
then receive a response, please consider your reply to have
been lost or deliberately destroyed: i *always* acknowledge
personal email received.  please therefore take appropriate
action and use appropriate protocols to ensure effective
communication.

thank you.