Rounding up to the nearest exact logarithmic decade

D

Derek Basch

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:

10**3 = 1000
x = 4978
10**4 = 10000

x = 10000

Thanks Everyone!
Derek Basch
 
F

Fredrik Lundh

Derek said:
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:

10**3 = 1000
x = 4978
10**4 = 10000
x = 10000

how about
... return 10**math.ceil(math.log10(x))
... 10000.0

?

</F>
 
J

jao

Quoting Derek Basch said:
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))))

Jack Orenstein
 
F

Felipe Almeida Lessa

Em Ter, 2006-02-28 às 17:47 -0500, (e-mail address removed) escreveu:
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"
 
D

Derek Basch

Thanks effbot. I knew their had to be something buried in the math
module that could help. ceil() it is!

</dTb>
 
G

Grant Edwards

How about this:

def roundup(x):
if x < 1:
return 1
else:
return '1' + ('0' * len(str(int(x))))

Interesting approach. You're converting to string at the same
time. I would guess the OP wanted a float.
 
J

johnzenger

I like Fredrik's solution. If for some reason you are afraid of
logarithms, you could also do:
x = 4978
decades = [10 ** n for n in xrange(-1,8)]
import itertools
itertools.ifilter(lambda decade: x < decade, decades).next()
10000

BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?
 
G

Grant Edwards

I like Fredrik's solution. If for some reason you are afraid of
logarithms, you could also do:
x = 4978
decades = [10 ** n for n in xrange(-1,8)]
import itertools
itertools.ifilter(lambda decade: x < decade, decades).next()
10000

BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?

You're joking, right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top