floor() function and mathematical integers

Bengt Richter bokr at accessone.com
Fri May 25 10:36:47 EDT 2001


On Fri, 25 May 2001 07:45:53 GMT, bokr at accessone.com (Bengt Richter)
wrote:
[... previous prb kludge ...]

Decided to add fractional float bit printing. Just started playing
with Python, as you can probably tell. No warranty.
______________________________________________

def prb(n):
	"prb prints numbers in binary"
	if type(n).__name__ not in ['int', 'float', 'long int']:
		print "prb only prints int, float, and long int."
		return
	if n<0: s = "-"; n=abs(n)
	else: s = ""
	k=1L
	while k<=n: k *= 2
	if n: k /= 2
	while k:
		if k<=n: s += '1' ; n -= k
		else: s += '0';
		k /= 2
	if n: s += "."
	while n:
		n *= 2
		if n >=1:  s += '1' ; n -= 1
		else: s += "0"
	print s
______________________________________________




More information about the Python-list mailing list