Re: print and long ints...

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Thu, 10 Mar 1994 13:06:49 -0500

On Mar 10, 9:26, lance@markv.com wrote:
>
> What am I doing wrong?
>
> >>> a
> 5L
> >>> print '%i' % a
> Traceback (innermost last):
> File "<stdin>", line 1
> TypeError: int argument required
> >>> print '%d' % a
> Traceback (innermost last):
> File "<stdin>", line 1
> TypeError: int argument required
> >>>
>
> Why can't I use the '%' modifier on a print statement AND use a LONG
> as the value passed in?
>
>

Remember that:
Python "int"s are typically machine/C longs.
Python "longs" are really "bignums" represented by multiple machine words.
'%' operator format strings bascially map into C printf formats.
+ 'C' has no multi-word integer data types.
-----------------------------------------------------------------------------
??? You can probably add it up NOW :-)

print "%d" % int(a)

will work, of course, until a grows large enough to yield:
"ValueError: long int too long to convert" from 'int(a)'.

BTW: did you know notice that '%' operator will take a tuple as the
second operand if the format string has more than one '%"
substitution?

>>> print "sqrt(%d)=%f" % ( 32, math.sqrt(32) )
sqrt(32)=5.656854

I didn't notice if this was in the manual, but the above reasoning
led me to try it, and it worked!

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics