Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28684 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2012-09-07 23:06 +1000 |
| Last post | 2012-09-08 10:09 +0200 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Division help in python Chris Angelico <rosuav@gmail.com> - 2012-09-07 23:06 +1000
Re: Division help in python garabik-news-2005-05@kassiopeia.juls.savba.sk - 2012-09-08 07:03 +0000
Re: Division help in python Hans Mulder <hansmu@xs4all.nl> - 2012-09-08 10:09 +0200
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-07 23:06 +1000 |
| Subject | Re: Division help in python |
| Message-ID | <mailman.354.1347023611.27098.python-list@python.org> |
On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <ramyasri20@gmail.com> wrote: > I am brand new to python. checking over basic stuff. I came across the > problem while doing so. If any body aware of the problem, kindly respond me. > >>>> 5/10 > 0 >>>> - 5/10 > -1 > > The second case also should yield a 'zero' but it is giving a -1 You're clearly using Python 2, because in Python 3, the / operator will return a float instead (so these would return 0.5 and -0.5 respectively). But it's helpful to mention what Python version you're using when you ask for help :) The reason for this is that / (or in Python 3, //) rounds toward negative infinity, not toward zero. This allows the modulo operator (%) to return a positive number, while still maintaining the normal expectation that: (x//y)*y + (x%y) == x for any two integers x and y. Hope that helps! ChrisA
[toc] | [next] | [standalone]
| From | garabik-news-2005-05@kassiopeia.juls.savba.sk |
|---|---|
| Date | 2012-09-08 07:03 +0000 |
| Message-ID | <k2eqjg$csd$1@speranza.aioe.org> |
| In reply to | #28684 |
Chris Angelico <rosuav@gmail.com> wrote: > On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <ramyasri20@gmail.com> wrote: >> I am brand new to python. checking over basic stuff. I came across the >> problem while doing so. If any body aware of the problem, kindly respond me. >> >>>>> 5/10 >> 0 >>>>> - 5/10 >> -1 >> >> The second case also should yield a 'zero' but it is giving a -1 > > ... > The reason for this is that / (or in Python 3, //) rounds toward > negative infinity, not toward zero. This allows the modulo operator I think he means the non-obvious unary minus precedence. -- ----------------------------------------------------------- | Radovan GarabĂk http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread!
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-09-08 10:09 +0200 |
| Message-ID | <504afd33$0$6846$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #28715 |
On 8/09/12 09:03:12, garabik-news-2005-05@kassiopeia.juls.savba.sk wrote: > Chris Angelico <rosuav@gmail.com> wrote: >> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <ramyasri20@gmail.com> wrote: >>> I am brand new to python. checking over basic stuff. I came across the >>> problem while doing so. If any body aware of the problem, kindly respond me. >>> >>>>>> 5/10 >>> 0 >>>>>> - 5/10 >>> -1 >>> >>> The second case also should yield a 'zero' but it is giving a -1 >> > ... > >> The reason for this is that / (or in Python 3, //) rounds toward >> negative infinity, not toward zero. This allows the modulo operator > > I think he means the non-obvious unary minus precedence. That seems unlikely. Unary minus has lower precedence in Python than in most other programming languages, but its precedence is higher than division, so this example doesn't show the difference. For example, in C unary opeators have the highest precedence. Yet -5/10 returns 0, not because of precedence, but because C rounds towards zero. Hope this helps, -- HansM
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web