Rounding up to the nearest exact logarithmic decade

Discussion in 'Python' started by Derek Basch, Feb 28, 2006.

  1. Derek Basch

    Derek Basch Guest

    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
     
    Derek Basch, Feb 28, 2006
    #1
    1. Advertising

  2. Derek Basch wrote:

    > 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

    >>> import math
    >>> def roundup(x):

    ... return 10**math.ceil(math.log10(x))
    ...
    >>> roundup(4978)

    10000.0

    ?

    </F>
     
    Fredrik Lundh, Feb 28, 2006
    #2
    1. Advertising

  3. Derek Basch

    Guest

    Quoting 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:


    How about this:

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

    Jack Orenstein
     
    , Feb 28, 2006
    #3
  4. Em Ter, 2006-02-28 às 17:47 -0500, escreveu:
    > Quoting 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:

    >
    > 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"
     
    Felipe Almeida Lessa, Feb 28, 2006
    #4
  5. Derek Basch

    Derek Basch Guest

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

    </dTb>
     
    Derek Basch, Feb 28, 2006
    #5
  6. On 2006-02-28, <> wrote:
    > Quoting 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:

    >
    > 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.

    --
    Grant Edwards grante Yow! It's OKAY -- I'm an
    at INTELLECTUAL, too.
    visi.com
     
    Grant Edwards, Feb 28, 2006
    #6
  7. Derek Basch

    Guest

    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 ?
     
    , Feb 28, 2006
    #7
  8. On 2006-02-28, <> wrote:
    > 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?

    --
    Grant Edwards grante Yow! HOORAY, Ronald!! Now
    at YOU can marry LINDA
    visi.com RONSTADT too!!
     
    Grant Edwards, Feb 28, 2006
    #8
  9. Fredrik Lundh, Feb 28, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Young [MVP]

    Re: rounding values to the nearest 5 or 10

    Greg Young [MVP], Apr 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    487
    Greg Young [MVP]
    Apr 27, 2006
  2. chaz
    Replies:
    0
    Views:
    402
  3. =?ISO-8859-1?Q?G=F6ran_Andersson?=

    Re: rounding values to the nearest 5 or 10

    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Apr 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,305
    Milsnips
    Apr 28, 2006
  4. Shea Martin
    Replies:
    7
    Views:
    3,600
    P.J. Plauger
    Oct 30, 2003
  5. Mr. Ken
    Replies:
    2
    Views:
    664
    Tomás
    May 25, 2006
Loading...

Share This Page