| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="shifting.html" /> |
| 13 | <link rel="prev" href="unary.html" /> |
| 14 | <link rel="parent" href="expressions.html" /> |
| 15 | <link rel="next" href="shifting.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.6 Binary arithmetic operations</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="5.5 Unary arithmetic operations" |
| 25 | href="unary.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="5. Expressions" |
| 28 | href="expressions.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="5.7 Shifting operations" |
| 31 | href="shifting.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="unary.html">5.5 Unary arithmetic operations</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="shifting.html">5.7 Shifting operations</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION007600000000000000000"></A><A NAME="binary"></A> |
| 56 | <BR> |
| 57 | 5.6 Binary arithmetic operations |
| 58 | </H1> |
| 59 | <a id='l2h-414' xml:id='l2h-414'></a> |
| 60 | <P> |
| 61 | The binary arithmetic operations have the conventional priority |
| 62 | levels. Note that some of these operations also apply to certain |
| 63 | non-numeric types. Apart from the power operator, there are only two |
| 64 | levels, one for multiplicative operators and one for additive |
| 65 | operators: |
| 66 | |
| 67 | <P> |
| 68 | <dl><dd class="grammar"> |
| 69 | <div class="productions"> |
| 70 | <table> |
| 71 | <tr> |
| 72 | <td><a id='tok-m_expr' xml:id='tok-m_expr'>m_expr</a></td> |
| 73 | <td>::=</td> |
| 74 | <td><a class='grammartoken' href="unary.html#tok-u_expr">u_expr</a> | <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> "*" <a class='grammartoken' href="unary.html#tok-u_expr">u_expr</a> |
| 75 | | <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> "//" <a class='grammartoken' href="unary.html#tok-u_expr">u_expr</a> |
| 76 | | <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> "/" <a class='grammartoken' href="unary.html#tok-u_expr">u_expr</a></td></tr> |
| 77 | <tr> |
| 78 | <td></td> |
| 79 | <td></td> |
| 80 | <td><code>| <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> "%" <a class='grammartoken' href="unary.html#tok-u_expr">u_expr</a></code></td></tr> |
| 81 | <tr> |
| 82 | <td><a id='tok-a_expr' xml:id='tok-a_expr'>a_expr</a></td> |
| 83 | <td>::=</td> |
| 84 | <td><a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> | <a class='grammartoken' href="binary.html#tok-a_expr">a_expr</a> "+" <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a> |
| 85 | | <a class='grammartoken' href="binary.html#tok-a_expr">a_expr</a> "-" <a class='grammartoken' href="binary.html#tok-m_expr">m_expr</a></td></tr> |
| 86 | </table> |
| 87 | </div> |
| 88 | <a class="grammar-footer" |
| 89 | href="grammar.txt" type="text/plain" |
| 90 | >Download entire grammar as text.</a> |
| 91 | </dd></dl> |
| 92 | |
| 93 | <P> |
| 94 | The <code>*</code> (multiplication) operator yields the product of its |
| 95 | arguments. The arguments must either both be numbers, or one argument |
| 96 | must be an integer (plain or long) and the other must be a sequence. |
| 97 | In the former case, the numbers are converted to a common type and |
| 98 | then multiplied together. In the latter case, sequence repetition is |
| 99 | performed; a negative repetition factor yields an empty sequence. |
| 100 | <a id='l2h-416' xml:id='l2h-416'></a> |
| 101 | |
| 102 | <P> |
| 103 | The <code>/</code> (division) and <code>//</code> (floor division) operators yield |
| 104 | the quotient of their arguments. The numeric arguments are first |
| 105 | converted to a common type. Plain or long integer division yields an |
| 106 | integer of the same type; the result is that of mathematical division |
| 107 | with the `floor' function applied to the result. Division by zero |
| 108 | raises the |
| 109 | <tt class="exception">ZeroDivisionError</tt> exception. |
| 110 | <a id='l2h-415' xml:id='l2h-415'></a><a id='l2h-417' xml:id='l2h-417'></a> |
| 111 | |
| 112 | <P> |
| 113 | The <code>%</code> (modulo) operator yields the remainder from the |
| 114 | division of the first argument by the second. The numeric arguments |
| 115 | are first converted to a common type. A zero right argument raises |
| 116 | the <tt class="exception">ZeroDivisionError</tt> exception. The arguments may be floating |
| 117 | point numbers, e.g., <code>3.14%0.7</code> equals <code>0.34</code> (since |
| 118 | <code>3.14</code> equals <code>4*0.7 + 0.34</code>.) The modulo operator always |
| 119 | yields a result with the same sign as its second operand (or zero); |
| 120 | the absolute value of the result is strictly smaller than the absolute |
| 121 | value of the second operand<A NAME="tex2html5" |
| 122 | HREF="#foot4570"><SUP>5.2</SUP></A>. |
| 123 | <a id='l2h-418' xml:id='l2h-418'></a> |
| 124 | |
| 125 | <P> |
| 126 | The integer division and modulo operators are connected by the |
| 127 | following identity: <code>x == (x/y)*y + (x%y)</code>. Integer division and |
| 128 | modulo are also connected with the built-in function <tt class="function">divmod()</tt>: |
| 129 | <code>divmod(x, y) == (x/y, x%y)</code>. These identities don't hold for |
| 130 | floating point numbers; there similar identities hold |
| 131 | approximately where <code>x/y</code> is replaced by <code>floor(x/y)</code> or |
| 132 | <code>floor(x/y) - 1</code><A NAME="tex2html6" |
| 133 | HREF="#foot4636"><SUP>5.3</SUP></A>. |
| 134 | |
| 135 | <P> |
| 136 | <div class="versionnote"><b>Deprecated since release 2.3.</b> |
| 137 | The floor division operator, the modulo operator, |
| 138 | and the <tt class="function">divmod()</tt> function are no longer defined for complex |
| 139 | numbers. Instead, convert to a floating point number using the |
| 140 | <tt class="function">abs()</tt> function if appropriate.</div><p></p> |
| 141 | |
| 142 | <P> |
| 143 | The <code>+</code> (addition) operator yields the sum of its arguments. |
| 144 | The arguments must either both be numbers or both sequences of the |
| 145 | same type. In the former case, the numbers are converted to a common |
| 146 | type and then added together. In the latter case, the sequences are |
| 147 | concatenated. |
| 148 | <a id='l2h-419' xml:id='l2h-419'></a> |
| 149 | |
| 150 | <P> |
| 151 | The <code>-</code> (subtraction) operator yields the difference of its |
| 152 | arguments. The numeric arguments are first converted to a common |
| 153 | type. |
| 154 | <a id='l2h-420' xml:id='l2h-420'></a> |
| 155 | |
| 156 | <P> |
| 157 | <BR><HR><H4>Footnotes</H4> |
| 158 | <DL> |
| 159 | <DT><A NAME="foot4570">... operand</A><A |
| 160 | href="binary.html#tex2html5"><SUP>5.2</SUP></A></DT> |
| 161 | <DD> |
| 162 | While <code>abs(x%y) < abs(y)</code> is true mathematically, for |
| 163 | floats it may not be true numerically due to roundoff. For |
| 164 | example, and assuming a platform on which a Python float is an |
| 165 | IEEE 754 double-precision number, in order that <code>-1e-100 % 1e100</code> |
| 166 | have the same sign as <code>1e100</code>, the computed result is |
| 167 | <code>-1e-100 + 1e100</code>, which is numerically exactly equal |
| 168 | to <code>1e100</code>. Function <tt class="function">fmod()</tt> in the <tt class="module">math</tt> |
| 169 | module returns a result whose sign matches the sign of the |
| 170 | first argument instead, and so returns <code>-1e-100</code> in this case. |
| 171 | Which approach is more appropriate depends on the application. |
| 172 | |
| 173 | |
| 174 | </DD> |
| 175 | <DT><A NAME="foot4636">... 1</A><A |
| 176 | href="binary.html#tex2html6"><SUP>5.3</SUP></A></DT> |
| 177 | <DD> |
| 178 | If x is very close to an exact integer multiple of y, it's |
| 179 | possible for <code>floor(x/y)</code> to be one larger than |
| 180 | <code>(x-x%y)/y</code> due to rounding. In such cases, Python returns |
| 181 | the latter result, in order to preserve that <code>divmod(x,y)[0] |
| 182 | * y + x % y</code> be very close to <code>x</code>. |
| 183 | |
| 184 | |
| 185 | </DD> |
| 186 | </DL> |
| 187 | <DIV CLASS="navigation"> |
| 188 | <div class='online-navigation'> |
| 189 | <p></p><hr /> |
| 190 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 191 | <tr> |
| 192 | <td class='online-navigation'><a rel="prev" title="5.5 Unary arithmetic operations" |
| 193 | href="unary.html"><img src='../icons/previous.png' |
| 194 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 195 | <td class='online-navigation'><a rel="parent" title="5. Expressions" |
| 196 | href="expressions.html"><img src='../icons/up.png' |
| 197 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 198 | <td class='online-navigation'><a rel="next" title="5.7 Shifting operations" |
| 199 | href="shifting.html"><img src='../icons/next.png' |
| 200 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 201 | <td align="center" width="100%">Python Reference Manual</td> |
| 202 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 203 | href="contents.html"><img src='../icons/contents.png' |
| 204 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 205 | <td class='online-navigation'><img src='../icons/blank.png' |
| 206 | border='0' height='32' alt='' width='32' /></td> |
| 207 | <td class='online-navigation'><a rel="index" title="Index" |
| 208 | href="genindex.html"><img src='../icons/index.png' |
| 209 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 210 | </tr></table> |
| 211 | <div class='online-navigation'> |
| 212 | <b class="navlabel">Previous:</b> |
| 213 | <a class="sectref" rel="prev" href="unary.html">5.5 Unary arithmetic operations</A> |
| 214 | <b class="navlabel">Up:</b> |
| 215 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> |
| 216 | <b class="navlabel">Next:</b> |
| 217 | <a class="sectref" rel="next" href="shifting.html">5.7 Shifting operations</A> |
| 218 | </div> |
| 219 | </div> |
| 220 | <hr /> |
| 221 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 222 | </DIV> |
| 223 | <!--End of Navigation Panel--> |
| 224 | <ADDRESS> |
| 225 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 226 | </ADDRESS> |
| 227 | </BODY> |
| 228 | </HTML> |