Newbie question about function return values

Chad Netzer cnetzer at mail.arc.nasa.gov
Sat Mar 15 01:50:41 CET 2003


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


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

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list