Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > aproximate a number

Reply
Thread Tools

aproximate a number

 
 
billiejoex
Guest
Posts: n/a
 
      08-28-2005
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


 
Reply With Quote
 
 
 
 
rafi
Guest
Posts: n/a
 
      08-28-2005
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)
 
Reply With Quote
 
 
 
 
Will McGugan
Guest
Posts: n/a
 
      08-28-2005
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")
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-28-2005
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.velocityreviews.com/forums/(E-Mail Removed) && 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
 
Reply With Quote
 
Michael Sparks
Guest
Posts: n/a
 
      08-28-2005
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.
 
Reply With Quote
 
billiejoex
Guest
Posts: n/a
 
      08-28-2005
Thank you.


 
Reply With Quote
 
Mikael Olofsson
Guest
Posts: n/a
 
      08-29-2005
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: (E-Mail Removed)
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!
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      08-29-2005
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
 
Reply With Quote
 
Thomas Bartkus
Guest
Posts: n/a
 
      08-30-2005
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


 
Reply With Quote
 
Devan L
Guest
Posts: n/a
 
      08-30-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the aproximate duration of 70-271 Hisham Abraham MCDST 3 12-07-2005 08:50 PM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
real number to 16 bit signed number hari VHDL 6 05-02-2004 04:10 PM
IE 6.0 sockets number (TCP/IP channels number) for the same Site ??? taras ASP .Net 1 04-17-2004 04:51 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments