Decimal can be Binary Too (was decimal or rational)

Mikael Olofsson mikael at isy.liu.se
Fri Jun 8 02:39:30 EDT 2001


On 02-Jun-2001 Don O'Donnell wrote:
 >  Scaling would require a multiply or divide by 10 every time the
 >  exponent is incremented/decremented by one, not as fast as a
 >  left or right bit shift; but it can be done pretty fast considering
 >  that a multiply by 10 is equivalent to a left shift by 3 (*8)
 >  followed by two adds (10*x == (8+2)*x == 8*x + 2*x == 8*x + x + x):
 >  
 > >>> x = 42
 > >>> (x<<3) + x + x    # multiply by 10 without using * op
 >  420

Why not make that one left shift by 3, one left shift by 1, and one add?

>>> x = 42
>>> (x<<3) + (x<<1)
420

What's more expensive here, add or shift? I'm more into hardware, and
hardwired shifts are costless (disregarding overflow checks), while 
hardwired adds are not.

 >  I'm not sure that this is any faster than x*10; in Python, probably
 >  not, but in C it may be. (Haven't looked at any CPU timing charts 
 >  lately, so I may be all wet here; it used to be that mult & div were 
 >  very expensive.)  (A quick timing check shows that 
 >  the above shift and add calculation takes about 3 times longer in
 >  Python than a straight *10, as expected.)

Neither am I. There's too much unknown here for me.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    08-Jun-2001
Time:    08:28:40

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------




More information about the Python-list mailing list