Rounding up to the nearest exact logarithmic decade

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Feb 28 18:07:00 EST 2006


Em Ter, 2006-02-28 às 17:47 -0500, jao at geophile.com escreveu:
> Quoting Derek Basch <dbasch at yahoo.com>:
> 
> > Given a value (x) that is within the range (1e-1, 1e7) how do I round
> > (x) up to the closest exact logarithmic decade? For instance:
> 
> How about this:
> 
> def roundup(x):
>     if x < 1:
>         return 1
>     else:
>         return '1' + ('0' * len(str(int(x))))

No dice. First, it returns an int for some cases and a string for the
others. Second, casting from str to int and vice-versa and concatenating
strings won't perform any good. I wouldn't like this hack on my code.

My 2¢,
Felipe.

-- 
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

  -- Sun Tzu, em "A arte da guerra"




More information about the Python-list mailing list