473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

aproximate a number

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
Aug 28 '05 #1
13 1281
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)
Aug 28 '05 #2
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")
Aug 28 '05 #3
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 && ma*@alcyone.com && 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
Aug 28 '05 #4
Thank you. :-)
Aug 28 '05 #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.
Aug 28 '05 #6
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: mi****@isy.liu.se
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!
Aug 29 '05 #7
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
Aug 29 '05 #8
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
Aug 30 '05 #9
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.

Aug 30 '05 #10
On 2005-08-30, Devan L <de****@gmail.com> 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!!
Aug 30 '05 #11
Grant Edwards wrote:
On 2005-08-30, Devan L <de****@gmail.com> 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.

Aug 30 '05 #12
On 2005-08-30, Devan L <de****@gmail.com> wrote:
Grant Edwards wrote:
On 2005-08-30, Devan L <de****@gmail.com> 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
Aug 30 '05 #13
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 <de****@gmail.com> 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

Aug 30 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shay Hurley | last post by:
this is probably a stupid question so apologies in advance. I am trying to format a number to look like a phone number with "-"'s between the numbers etc e.g. 15554256987 should be formatted as...
8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
3
by: td0g03 | last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.