[Tutor] puzzled by Python 3's print()
Richard D. Moores
rdmoores at gmail.com
Fri Jul 2 05:34:43 CEST 2010
On Thu, Jul 1, 2010 at 16:18, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, 2 Jul 2010 05:18:00 am Eike Welk wrote:
>
>> As you are using long integers (and you were previously writing about
>> prime numbers) the precision of floating point numbers might not be
>> enough for your purposes.
>
> It certainly won't be once you get to large enough primes!
>
>> Therefore you should probably use the integer division operator: "//"
>
> And the reminder (or modulo) operator %, together with the combination
> function divmod(a, b) which returns (a//b, a%b). The advantage of
> divmod is that it is faster than calling a//b followed by a%b.
Thanks to you and Eike, Steven, I was able to write this little
function that does the job for me, and more:
>>> def divide_large_ints(n, div):
x = divmod(n, div)
return str(x[0]) + str(x[1]/div).lstrip('0')
>>> n = 2000000000000000000000000000000033
>>> div = 2
>>> divide_large_ints(n, div)
'1000000000000000000000000000000016.5'
>>>
Dick
More information about the Tutor
mailing list