Hello all,
I have two integers and I want to divide one by another, and want to
get an integer result which is the higher side whenever the result is
a fraction.
3/2 =1 # Usual behavior
some_func(3, 2) =2 # Wanted
Any easier solution other than int(math.ceil(float(3)/2))
-
Suresh 9 7239 jm*******@no.spam.gmail.com wrote:
Hello all,
I have two integers and I want to divide one by another, and want to
get an integer result which is the higher side whenever the result is
a fraction.
3/2 =1 # Usual behavior
some_func(3, 2) =2 # Wanted
Any easier solution other than int(math.ceil(float(3)/2))
The normal solution is to add (divisor-1) to the dividend before division.
Ie ceil(3/2) = floor((3+(2-1))/2) = 2. Correct.
But ceil(4/2) = floor((4+(2-1))/2) = 2 also. Correct.
Hamish
On Jun 7, 2:15 pm, Hamish Moffatt <ham...@cloud.net.auwrote:
jm.sur...@no.spam.gmail.com wrote:
Hello all,
I have two integers and I want to divide one by another, and want to
get an integer result which is the higher side whenever the result is
a fraction.
3/2 =1 # Usual behavior
some_func(3, 2) =2 # Wanted
Any easier solution other than int(math.ceil(float(3)/2))
The normal solution is to add (divisor-1) to the dividend before division.
Ie ceil(3/2) = floor((3+(2-1))/2) = 2. Correct.
But ceil(4/2) = floor((4+(2-1))/2) = 2 also. Correct.
Hamish
What about this?
>>def div_ceil(a, b):
.... if a%b:
.... return ((a/b)+1)
.... else:
.... return (a/b)
....
>>div_ceil(3,2)
2
>>div_ceil(-3,2)
-1
>>-3%2
1
>>-(3%2)
-1
>>div_ceil(-5,2)
-2
>>div_ceil(5,2)
3 jm*******@no.spam.gmail.com <jm*******@gmail.comwrote:
3/2 =1 # Usual behavior
some_func(3, 2) =2 # Wanted
def some_func(a, b):
return -(-a/b)
And people complain about Python's behaviour regarding division of
negative integers.
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump jm*******@no.spam.gmail.com wrote:
On Jun 7, 2:15 pm, Hamish Moffatt <ham...@cloud.net.auwrote:
>jm.sur...@no.spam.gmail.com wrote:
>>Hello all, I have two integers and I want to divide one by another, and want to get an integer result which is the higher side whenever the result is a fraction. 3/2 =1 # Usual behavior some_func(3, 2) =2 # Wanted Any easier solution other than int(math.ceil(float(3)/2))
The normal solution is to add (divisor-1) to the dividend before division.
Ie ceil(3/2) = floor((3+(2-1))/2) = 2. Correct. But ceil(4/2) = floor((4+(2-1))/2) = 2 also. Correct.
What about this?
>>>def div_ceil(a, b):
... if a%b:
... return ((a/b)+1)
... else:
... return (a/b)
Yes, although it's not as short or as fast (probably as my version):
def div_ceil(a, b):
return ((a+(b-1))/b)
Hamish
Hamish Moffatt <ha****@cloud.net.auwrote:
>jm*******@no.spam.gmail.com wrote:
>>>>def div_ceil(a, b):
... if a%b: ... return ((a/b)+1) ... else: ... return (a/b)
Yes, although it's not as short or as fast (probably as my version):
def div_ceil(a, b):
return ((a+(b-1))/b)
If that's what you care about :
$ python -mtimeit -s 'def divc1(a,b): return (a+b-1)/b' 'divc1(3,2)'
1000000 loops, best of 3: 0.443 usec per loop
$ python -mtimeit -s 'def divc2(a,b): return -(-a/b)' 'divc2(3,2)'
1000000 loops, best of 3: 0.331 usec per loop
Also, note:
>>divc2(sys.maxint, 2)
1073741824
>>divc1(sys.maxint, 2)
1073741824L
which is going to cause problems with sys.version_info < (2, 3) .
(Or do I mean (2, 2)? I don't have a 2.2 to hand.)
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
"Sion Arrowsmith" <si***@chiark.greenend.org.ukwrote in message
news:Y9*******@news.chiark.greenend.org.uk...
| jm*******@no.spam.gmail.com <jm*******@gmail.comwrote:
| 3/2 =1 # Usual behavior
| some_func(3, 2) =2 # Wanted
|
| def some_func(a, b):
| return -(-a/b)
|
| And people complain about Python's behaviour regarding division of
| negative integers.
Nice. This goes on my 'why didn't I think of that' list;-)
I was thinking of the rather pedestrian
d,r = divmod(a,b)
if r: d+=1
tjr
On Jun 7, 8:30 pm, Some Other Guy <bga...@microsoft.comwrote:
jm.sur...@no.spam.gmail.com wrote:
Hello all,
I have two integers and I want to divide one by another, and want to
get an integer result which is the higher side whenever the result is
a fraction.
3/2 =1 # Usual behavior
some_func(3, 2) =2 # Wanted
Are you trying to accomplish int((a/b) + 0.5), but cheaply?
If so, consider that that is the same as (a/b) + (b/2b),
which is (2a/2b) + (b/2b), which is (2a+b)/2b.
Since this just involves doubling you can avoid multiplying altogether
and just use this:
def rounddiv(a,b):
return int((a+a+b)/(b+b))
That's 3 integer adds and 1 integer divide. The int() cast is just
there is case somebody passes in floats for a or b; if you know you're
only going to have integer arguments you don't need it.
The // operator was added to the language for a reason. I'm surprised
at how few Pythonistas are using it six years later.
"Some Other Guy" <bg****@microsoft.comwrote in message
news:bb***************************@TEKSAVVY.COM...
| jm*******@no.spam.gmail.com wrote:
|
| Hello all,
| I have two integers and I want to divide one by another, and want to
| get an integer result which is the higher side whenever the result is
| a fraction.
| 3/2 =1 # Usual behavior
| some_func(3, 2) =2 # Wanted
|
| Are you trying to accomplish int((a/b) + 0.5), but cheaply?
No, the OP wanted 'ceiling' division rather than 'floor' division.
In other words, always round up instead of down.
| If so, consider that that is the same as (a/b) + (b/2b),
| which is (2a/2b) + (b/2b), which is (2a+b)/2b.
This is nice for rounded division.
tjr
In <11*********************@q69g2000hsb.googlegroups. com>, Dan Bishop
wrote:
On Jun 7, 8:30 pm, Some Other Guy <bga...@microsoft.comwrote:
>Since this just involves doubling you can avoid multiplying altogether and just use this:
def rounddiv(a,b): return int((a+a+b)/(b+b))
That's 3 integer adds and 1 integer divide. The int() cast is just there is case somebody passes in floats for a or b; if you know you're only going to have integer arguments you don't need it.
The // operator was added to the language for a reason. I'm surprised
at how few Pythonistas are using it six years later.
It would just document intent here because it still gives `float` results
if used with at least one `float` as operands:
In [1]: 10.0 // 2
Out[1]: 5.0
Ciao,
Marc 'BlackJack' Rintsch This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sebastian Haase |
last post by:
Hi,
I'm interested in having more people in our lab using numarray/NumPy
instead of MatLab. For that I have put together a couple useful
modules and written many myself. But then I got reminded of...
|
by: Imbaud Pierre |
last post by:
integer division and modulo gives different results in c and python,
when negative numbers
are involved. take gdb as a widely available c interpreter
print -2 /3
0 for c, -1 for python.
more...
|
by: Teis Draiby |
last post by:
In .NET, can I be sure that the result of a division between two integers
always is truncated rather that rounded to nearest?
Example:
99 / 50 = 1
regards, Teis
|
by: Janice |
last post by:
I got this question from my textbook and I cannot understand the theory.
When a signed positive integer X divided by pow(2,k), the result is shifting
k bits to right and putting w-k bits of 0 from...
|
by: Mike S |
last post by:
Does anyone know the logic behind why in VB.NET the result of a
floating-point division ('/') is -rounded- on being converted to an
integer type, such as with statements like
Dim x As Integer =...
|
by: Candace |
last post by:
I am using the following code to pick off each digit of a number, from right
to left. The number I am working with is 84357. So for the first iteration it
should return the number 7 and for the...
|
by: PengYu.UT |
last post by:
Hi,
The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it...
|
by: Alasdair |
last post by:
I need to apply the ceiling function to arbitrary sized (long) integers.
However, division automatically returns the type of its operands, so that,
for example: math.ceil(7/4) returns 1. I can use...
|
by: bdsatish |
last post by:
How does (a/b) work when both 'a' and 'b' are pure integers ?
4
-5
Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.
What should I do...
|
by: Default User |
last post by:
Hi,
If I have three 64 bit integers and I want to do this operation on them:
x*y/z
Lets say that what we are multiplying by (y) is offset by what we are
dividing by (z) so that the final...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |