[Tutor] Number representation

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Jun 20 18:42:01 2003


On Thu, 19 Jun 2003, kettil wrote:

> > "base 10" is ambiguous. We usually interpret that to mean the decimal
> > system, but an 8 fingered alien who counts in octal would say "base
> > 10" and it would mean octal.
>
> How could that be? Isn't base "10" named after those ten fingers?(or at
> least have something to do with 10)  Or have I missed the point? (have a
> strong feeling I have)


We're going WAY WAY off list topic now.  *grin*

I think the intention was to say that saying "base 10" is as ambiguous and
relative as an alien saying the word "Earth", or to an American saying the
word "Homeland".


I personally disagree about base 10 though. I think "base 10" has a
specific meaning that isn't relative to the person speaking the term:  I
feelt that it's a statement about the kind of positional number system
we're using to represent our numbers.



We better somehow drag this conversation back into something related to
Python.  *grin* With all this talk about representation of numbers, it
might be neat to see that we can represent numbers in ways that aren't
quite obvious:


###
>>> zero = ()
>>> def succ(number):
...     "Returns the successor of a given number"
...     return (number,)
...
>>> def pred(number):
...     "Returns the predecessor of a given number"
...     return number[0]
...
>>> zero
()
>>> succ(zero)
((),)
>>> succ(succ(zero))
(((),),)
###


That is, we can represent the concept of number as the nesting level of a
tuple.  This isn't really an efficient representation in a computer, but
it does work!


###
>>> def add(n1, n2):
...     "Defines addition between two 'numbers'."
...     if n1 == zero:
...         return n2
...     return add(pred(n1), succ(n2))
...
>>> two = succ(succ(zero))
>>> add(two, two) == succ(succ(succ(succ(zero))))
1
###


In such an odd system of arithmetic, we can still have a sense of number:
we can still do some useful calculations.  In fact, we can even define
multiplication, because multiplication can be seen as a bunch of additions
based on two rules:

    0*b == 0                   ## Rule 1
    a*b == b + (a-1) * b       ## Rule 2

###
>>> def mul(a, b):
...     "Defines multiplication between two 'numbers'"
...     if a == zero:
...         return zero
...     return add(b, mul(pred(a), b))
...
>>> four = add(two, two)
>>> four
(((((),),),),)
>>> mul(four, four)
(((((((((((((((((),),),),),),),),),),),),),),),),)
###


In all of this, it's a good thing to keep in mind that the question about
what "base" this tuple arithmetic is in is a meaningless one:  The concept
of 'base' can only apply when we're dealing with representing a number
with a particular positional number system.  Positional number systems
deal with concepts like digits and positions.


And in this tuple representation, we're not using position to mean
anything.  *grin* Number is not tied down to the number of digits on our
hands, although we might find it convenient to do so for calculation
reasons.


If you're interested in this stuff about representations of ideas, you may
find Douglas Hofstadter's book, "Godel, Escher, Bach: An Eternal Golden
Braid", an awesome book to read: he has several chapters on stuff like
this.


Hope this helps!