Newbie question about function return values

John Machin sjmachin at lexicon.net
Fri Mar 14 20:25:30 EST 2003


---- Original message ----
>Date: 14 Mar 2003 16:50:41 -0800
>From: Chad Netzer <cnetzer at mail.arc.nasa.gov>  
>Subject: Re: Newbie question about function return values  
>To: John Machin <sjmachin at lexicon.net>, Brian Munroe 
<bmunroe at tribador.nu>
>Cc: python-list at python.org
>
>On Fri, 2003-03-14 at 16:19, John Machin wrote:
>> Chad Netzer <cnetzer at mail.arc.nasa.gov> wrote in message 
news:<mailman.1047603262.3191.python-list at python.org>...
>> > 
>> > def sectors(x):
>> >     if ((x % 512) != 0):
>> >         x = x + 1
>> >         return sectors(x)
>> >     else:
>> >         return x
>> > 
>> > 
>> > BTW - There are better methods of finding the next 
larger power of two.
>
>> (2) "power of 2" seems totally irrelevant.
>
>Oops, you are right.  The task was to find the next higher 
multiple of
>512 (as you stated).
>
>> Try this:
>> 
>> def round_to_higher_mult_of_512(x):
>>    return ((x + 511L) // 512L) * x
>>    # 'L' just in case rounded-up file size >= 2GB
>
>???
>
>>>> round_to_higher_mult_of_512(511)
>511L
>>>> round_to_higher_mult_of_512(513)
>1026L

OOOOOoooopppps, I stuffed up.

>I'll suggest this instead:
>
># Will automatically promote to long int in python 2.2.x
>def round_to_higher_mult_of_512(x):
>    return (x//512 + 1) * 512
>

OOOOOoooopppps, you stuffed up also.
For 512, this returns 1024.

Try this:

def round_to_higher_mult_of_512(x):
   return ((x + 511L) // 512L) * 512L
   # 'L' just in case rounded-up file size >= 2GB





More information about the Python-list mailing list