aproximate a number

Discussion in 'Python' started by billiejoex, Aug 28, 2005.

  1. billiejoex

    billiejoex Guest

    Hi all. I'd need to aproximate a given float number into the next (int)
    bigger one. Because of my bad english I try to explain it with some example:

    5.7 --> 6
    52.987 --> 53
    3.34 --> 4
    2.1 --> 3

    Regards
     
    billiejoex, Aug 28, 2005
    #1
    1. Advertising

  2. billiejoex

    rafi Guest

    billiejoex wrote:
    > Hi all. I'd need to aproximate a given float number into the next (int)
    > bigger one. Because of my bad english I try to explain it with some example:
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3
    >
    > Regards
    >
    >


    math.ceil returns what you need but as a float, then create an int

    >>> import math
    >>> math.ceil (12.3)

    13.0
    >>> int (math.ceil (12.3))

    13

    hth

    --
    rafi

    "Imagination is more important than knowledge."
    (Albert Einstein)
     
    rafi, Aug 28, 2005
    #2
    1. Advertising

  3. billiejoex

    Will McGugan Guest

    billiejoex wrote:
    > Hi all. I'd need to aproximate a given float number into the next (int)
    > bigger one. Because of my bad english I try to explain it with some example:
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3
    >


    Have a look at math.ceil

    >>> import math
    >>> math.ceil(5.7)

    6.0


    Will McGugan
    --
    http://www.willmcgugan.com
    "".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
    "jvyy*jvyyzpthtna^pbz")
     
    Will McGugan, Aug 28, 2005
    #3
  4. billiejoex wrote:

    > Hi all. I'd need to aproximate a given float number into the next (int)
    > bigger one. Because of my bad english I try to explain it with some example:
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3


    Probably something like int(number + 0.99999999), depending on the
    boundary cases you want (which you haven't mentioned here. Technically,
    it should be int(number + 1.0 - epsilon).

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    If you are afraid of loneliness, do not marry.
    -- Anton Chekhov
     
    Erik Max Francis, Aug 28, 2005
    #4
  5. billiejoex wrote:

    > Hi all. I'd need to aproximate a given float number into the next (int)
    > bigger one. Because of my bad english I try to explain it with some
    > example:
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3


    What about 2.0? By your spec that should be rounded to 3 - is that what you
    intend?

    If you do, you can simply do this:

    def approx(x):
    return int(x+1.0)

    Regards,


    Michael.
     
    Michael Sparks, Aug 28, 2005
    #5
  6. billiejoex

    billiejoex Guest

    Thank you. :)
     
    billiejoex, Aug 28, 2005
    #6
  7. Michael Sparks wrote:
    > def approx(x):
    > return int(x+1.0)


    I doubt this is what the OP is looking for.

    >>> approx(3.2)

    4
    >>> approx(3.0)

    4

    Others have pointed to math.ceil, which is most likely what the OP wants.

    /Mikael Olofsson
    Universitetslektor (Senior Lecturer [BrE], Associate Professor [AmE])
    Linköpings universitet

    -----------------------------------------------------------------------
    E-Mail:
    WWW: http://www.dtr.isy.liu.se/en/staff/mikael
    Phone: +46 - (0)13 - 28 1343
    Telefax: +46 - (0)13 - 28 1339
    -----------------------------------------------------------------------
    Linköpings kammarkör: www.kammarkoren.com Vi söker tenorer och basar!
     
    Mikael Olofsson, Aug 29, 2005
    #7
  8. billiejoex

    Peter Hansen Guest

    Mikael Olofsson wrote:
    > Michael Sparks wrote:
    >
    >> def approx(x):
    >> return int(x+1.0)

    >
    > I doubt this is what the OP is looking for.

    ....
    > Others have pointed to math.ceil, which is most likely what the OP wants.


    I agree that's "likely" but, as Michael pointed out in the text you
    removed, his version does do what the OP's spec states, when interpreted
    literally. Very likely there's a language issue involved, and Michael
    was aware of that as well, I'm sure.

    Still, others had already posted on math.ceil(), so Michael was just
    trying to make sure that the OP realized his specification was
    inadequate and -- just in case he wanted something other than math.ceil
    -- he provided a valid alternative.

    -Peter
     
    Peter Hansen, Aug 29, 2005
    #8
  9. On Sun, 28 Aug 2005 23:11:09 +0200, billiejoex wrote:

    > Hi all. I'd need to aproximate a given float number into the next (int)
    > bigger one. Because of my bad english I try to explain it with some example:
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3
    >


    The standard way to do this is thus:

    def RoundToInt(x):
    """ Round the float x to the nearest integer """
    return int(round(x+0.5))

    x = 5.7
    print x, '-->', RoundToInt(x)
    x = 52.987
    print x, '-->', RoundToInt(x)
    x = 3.34
    print x, '-->', RoundToInt(x)
    x = 2.1
    print x, '-->', RoundToInt(x)

    5.7 --> 6
    52.987 --> 53
    3.34 --> 4
    2.1 --> 3
     
    Thomas Bartkus, Aug 30, 2005
    #9
  10. billiejoex

    Devan L Guest

    Thomas Bartkus wrote:
    > On Sun, 28 Aug 2005 23:11:09 +0200, billiejoex wrote:
    >
    > > Hi all. I'd need to aproximate a given float number into the next (int)
    > > bigger one. Because of my bad english I try to explain it with some example:
    > >
    > > 5.7 --> 6
    > > 52.987 --> 53
    > > 3.34 --> 4
    > > 2.1 --> 3
    > >

    >
    > The standard way to do this is thus:
    >
    > def RoundToInt(x):
    > """ Round the float x to the nearest integer """
    > return int(round(x+0.5))
    >
    > x = 5.7
    > print x, '-->', RoundToInt(x)
    > x = 52.987
    > print x, '-->', RoundToInt(x)
    > x = 3.34
    > print x, '-->', RoundToInt(x)
    > x = 2.1
    > print x, '-->', RoundToInt(x)
    >
    > 5.7 --> 6
    > 52.987 --> 53
    > 3.34 --> 4
    > 2.1 --> 3


    RoundToInt(2.0) will give you 3.
     
    Devan L, Aug 30, 2005
    #10
  11. On 2005-08-30, Devan L <> wrote:

    >>> Hi all. I'd need to aproximate a given float number into the
    >>> next (int) bigger one. Because of my bad english I try to
    >>> explain it with some example:
    >>>
    >>> 5.7 --> 6
    >>> 52.987 --> 53
    >>> 3.34 --> 4
    >>> 2.1 --> 3

    >>
    >> The standard way to do this is thus:
    >>
    >> def RoundToInt(x):
    >> """ Round the float x to the nearest integer """
    >> return int(round(x+0.5))
    >>
    >> x = 5.7
    >> print x, '-->', RoundToInt(x)
    >> x = 52.987
    >> print x, '-->', RoundToInt(x)
    >> x = 3.34
    >> print x, '-->', RoundToInt(x)
    >> x = 2.1
    >> print x, '-->', RoundToInt(x)
    >>
    >> 5.7 --> 6
    >> 52.987 --> 53
    >> 3.34 --> 4
    >> 2.1 --> 3

    >
    > RoundToInt(2.0) will give you 3.


    That's what the OP said he wanted. The next bigger integer
    after 2.0 is 3.

    --
    Grant Edwards grante Yow! I'd like TRAINED
    at SEALS and a CONVERTIBLE on
    visi.com my doorstep by NOON!!
     
    Grant Edwards, Aug 30, 2005
    #11
  12. billiejoex

    Devan L Guest

    Grant Edwards wrote:
    > On 2005-08-30, Devan L <> wrote:
    > >
    > > RoundToInt(2.0) will give you 3.

    >
    > That's what the OP said he wanted. The next bigger integer
    > after 2.0 is 3.
    >
    > --
    > Grant Edwards grante Yow! I'd like TRAINED
    > at SEALS and a CONVERTIBLE on
    > visi.com my doorstep by NOON!!


    It's not really clear whether he wanted it to round up or to go to the
    next biggest integer because he says he has bad english. I can't think
    of a particular use of returning the next bigger integer.
     
    Devan L, Aug 30, 2005
    #12
  13. On 2005-08-30, Devan L <> wrote:
    > Grant Edwards wrote:
    >> On 2005-08-30, Devan L <> wrote:
    >> >
    >> > RoundToInt(2.0) will give you 3.

    >>
    >> That's what the OP said he wanted. The next bigger integer
    >> after 2.0 is 3.

    >
    > It's not really clear whether he wanted it to round up or to go to the
    > next biggest integer because he says he has bad english. I can't think
    > of a particular use of returning the next bigger integer.


    You're probably right. I suspect what he really wants is

    i = int(math.ceil(x))

    --
    Grant Edwards grante Yow! Is it FUN to be
    at a MIDGET?
    visi.com
     
    Grant Edwards, Aug 30, 2005
    #13
  14. billiejoex

    billiejoex Guest

    I wanted the round up the number (5.0 = 5.0, not 6.0.). The ceil funciotn is
    the right one for me.
    Thanks to all.


    >> Grant Edwards wrote:
    >>> On 2005-08-30, Devan L <> wrote:
    >>> >
    >>> > RoundToInt(2.0) will give you 3.
    >>>
    >>> That's what the OP said he wanted. The next bigger integer
    >>> after 2.0 is 3.

    >>
    >> It's not really clear whether he wanted it to round up or to go to the
    >> next biggest integer because he says he has bad english. I can't think
    >> of a particular use of returning the next bigger integer.

    >
    > You're probably right. I suspect what he really wants is
    >
    > i = int(math.ceil(x))
    >
    > --
    > Grant Edwards grante Yow! Is it FUN to be
    > at a MIDGET?
    > visi.com
     
    billiejoex, Aug 30, 2005
    #14
    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. makok
    Replies:
    1
    Views:
    6,786
    Anders Hellerup Madsen
    Feb 23, 2004
  2. hari
    Replies:
    6
    Views:
    6,956
    hamilton
    May 2, 2004
  3. mayur
    Replies:
    2
    Views:
    1,017
    Natty Gur
    Jul 2, 2004
  4. Replies:
    5
    Views:
    525
    Pete Becker
    May 21, 2005
  5. Hako
    Replies:
    13
    Views:
    38,859
    Ron Adam
    Nov 10, 2005
Loading...

Share This Page