floor() function and mathematical integers

Bengt Richter bokr at accessone.com
Fri May 25 03:45:53 EDT 2001


On Thu, 24 May 2001 20:57:02 -0400, "Tim Peters" <tim.one at home.com>
wrote:

>[Bengt Richter]
>> >>> import math
>> >>> M=5L; B=3L; E=33L
>> >>> a = M*B**E
>> >>> a,float(a),math.floor(a)
>> (27795302832777615L, 27795302832777616.0, 27795302832777616.0)
>> >>> prb(a)
>> 1100010101111111010111000111111110001011010100110001111
>
>[Aahz]
>> What's this prb() function?
>
>Not part of Python; from context it looks like a function Bengt wrote to
>PRint Bits, i.e. to print its argument in binary.
>
"Bingo." A quick kludge (no warranty):

def prb(n):
	"prb prints long decimal in binary"
	if n<0: s = "-"; n=abs(n)
	else: s = ""
	k=1L
	while k<=n: k = k*2
	k = k/2
	while k:
		if k<=n: s = s +'1' ; n = n - k
		else: s = s +'0';
		k = k/2
	print s

Kind of a binary long division by one ;-)



More information about the Python-list mailing list