Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-math.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.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="lib.html" title='Python Library Reference' />
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="module-cmath.html" />
13<link rel="prev" href="module-decimal.html" />
14<link rel="parent" href="misc.html" />
15<link rel="next" href="module-cmath.html" />
16<meta name='aesop' content='information' />
17<title>5.7 math -- Mathematical functions</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.6.8 Decimal FAQ"
25 href="decimal-faq.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. Miscellaneous Services"
28 href="misc.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.8 cmath "
31 href="module-cmath.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 Library Reference</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'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></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="decimal-faq.html">5.6.8 Decimal FAQ</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-cmath.html">5.8 cmath </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION007700000000000000000">
565.7 <tt class="module">math</tt> --
57 Mathematical functions</A>
58</H1>
59
60<P>
61<A NAME="module-math"></A>
62
63<P>
64This module is always available. It provides access to the
65mathematical functions defined by the C standard.
66
67<P>
68These functions cannot be used with complex numbers; use the functions
69of the same name from the <tt class="module"><a href="module-cmath.html">cmath</a></tt> module if you require
70support for complex numbers. The distinction between functions which
71support complex numbers and those which don't is made since most users
72do not want to learn quite as much mathematics as required to
73understand complex numbers. Receiving an exception instead of a
74complex result allows earlier detection of the unexpected complex
75number used as a parameter, so that the programmer can determine how
76and why it was generated in the first place.
77
78<P>
79The following functions are provided by this module. Except
80when explicitly noted otherwise, all return values are floats.
81
82<P>
83Number-theoretic and representation functions:
84
85<P>
86<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
87 <td><nobr><b><tt id='l2h-1250' xml:id='l2h-1250' class="function">ceil</tt></b>(</nobr></td>
88 <td><var>x</var>)</td></tr></table></dt>
89<dd>
90Return the ceiling of <var>x</var> as a float, the smallest integer value
91greater than or equal to <var>x</var>.
92</dl>
93
94<P>
95<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
96 <td><nobr><b><tt id='l2h-1251' xml:id='l2h-1251' class="function">fabs</tt></b>(</nobr></td>
97 <td><var>x</var>)</td></tr></table></dt>
98<dd>
99Return the absolute value of <var>x</var>.
100</dl>
101
102<P>
103<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
104 <td><nobr><b><tt id='l2h-1252' xml:id='l2h-1252' class="function">floor</tt></b>(</nobr></td>
105 <td><var>x</var>)</td></tr></table></dt>
106<dd>
107Return the floor of <var>x</var> as a float, the largest integer value
108less than or equal to <var>x</var>.
109</dl>
110
111<P>
112<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
113 <td><nobr><b><tt id='l2h-1253' xml:id='l2h-1253' class="function">fmod</tt></b>(</nobr></td>
114 <td><var>x, y</var>)</td></tr></table></dt>
115<dd>
116Return <code>fmod(<var>x</var>, <var>y</var>)</code>, as defined by the platform C library.
117Note that the Python expression <code><var>x</var> % <var>y</var></code> may not return
118the same result. The intent of the C standard is that
119<code>fmod(<var>x</var>, <var>y</var>)</code> be exactly (mathematically; to infinite
120precision) equal to <code><var>x</var> - <var>n</var>*<var>y</var></code> for some integer
121<var>n</var> such that the result has the same sign as <var>x</var> and
122magnitude less than <code>abs(<var>y</var>)</code>. Python's
123<code><var>x</var> % <var>y</var></code> returns a result with the sign of
124<var>y</var> instead, and may not be exactly computable for float arguments.
125For example, <code>fmod(-1e-100, 1e100)</code> is <code>-1e-100</code>, but the
126result of Python's <code>-1e-100 % 1e100</code> is <code>1e100-1e-100</code>, which
127cannot be represented exactly as a float, and rounds to the surprising
128<code>1e100</code>. For this reason, function <tt class="function">fmod()</tt> is generally
129preferred when working with floats, while Python's
130<code><var>x</var> % <var>y</var></code> is preferred when working with integers.
131</dl>
132
133<P>
134<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
135 <td><nobr><b><tt id='l2h-1254' xml:id='l2h-1254' class="function">frexp</tt></b>(</nobr></td>
136 <td><var>x</var>)</td></tr></table></dt>
137<dd>
138Return the mantissa and exponent of <var>x</var> as the pair
139<code>(<var>m</var>, <var>e</var>)</code>. <var>m</var> is a float and <var>e</var> is an
140integer such that <code><var>x</var> == <var>m</var> * 2**<var>e</var></code> exactly.
141If <var>x</var> is zero, returns <code>(0.0, 0)</code>, otherwise
142<code>0.5 &lt;= abs(<var>m</var>) &lt; 1</code>. This is used to "pick apart" the
143internal representation of a float in a portable way.
144</dl>
145
146<P>
147<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
148 <td><nobr><b><tt id='l2h-1255' xml:id='l2h-1255' class="function">ldexp</tt></b>(</nobr></td>
149 <td><var>x, i</var>)</td></tr></table></dt>
150<dd>
151Return <code><var>x</var> * (2**<var>i</var>)</code>. This is essentially the inverse of
152function <tt class="function">frexp()</tt>.
153</dl>
154
155<P>
156<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
157 <td><nobr><b><tt id='l2h-1256' xml:id='l2h-1256' class="function">modf</tt></b>(</nobr></td>
158 <td><var>x</var>)</td></tr></table></dt>
159<dd>
160Return the fractional and integer parts of <var>x</var>. Both results
161carry the sign of <var>x</var>, and both are floats.
162</dl>
163
164<P>
165Note that <tt class="function">frexp()</tt> and <tt class="function">modf()</tt> have a different
166call/return pattern than their C equivalents: they take a single
167argument and return a pair of values, rather than returning their
168second return value through an `output parameter' (there is no such
169thing in Python).
170
171<P>
172For the <tt class="function">ceil()</tt>, <tt class="function">floor()</tt>, and <tt class="function">modf()</tt>
173functions, note that <em>all</em> floating-point numbers of sufficiently
174large magnitude are exact integers. Python floats typically carry no more
175than 53 bits of precision (the same as the platform C double type), in
176which case any float <var>x</var> with <code>abs(<var>x</var>) &gt;= 2**52</code>
177necessarily has no fractional bits.
178
179<P>
180Power and logarithmic functions:
181
182<P>
183<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
184 <td><nobr><b><tt id='l2h-1257' xml:id='l2h-1257' class="function">exp</tt></b>(</nobr></td>
185 <td><var>x</var>)</td></tr></table></dt>
186<dd>
187Return <code>e**<var>x</var></code>.
188</dl>
189
190<P>
191<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
192 <td><nobr><b><tt id='l2h-1258' xml:id='l2h-1258' class="function">log</tt></b>(</nobr></td>
193 <td><var>x</var><big>[</big><var>, base</var><big>]</big><var></var>)</td></tr></table></dt>
194<dd>
195Return the logarithm of <var>x</var> to the given <var>base</var>.
196If the <var>base</var> is not specified, return the natural logarithm of <var>x</var>
197(that is, the logarithm to base <em>e</em>).
198
199<span class="versionnote">Changed in version 2.3:
200<var>base</var> argument added.</span>
201
202</dl>
203
204<P>
205<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
206 <td><nobr><b><tt id='l2h-1259' xml:id='l2h-1259' class="function">log10</tt></b>(</nobr></td>
207 <td><var>x</var>)</td></tr></table></dt>
208<dd>
209Return the base-10 logarithm of <var>x</var>.
210</dl>
211
212<P>
213<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
214 <td><nobr><b><tt id='l2h-1260' xml:id='l2h-1260' class="function">pow</tt></b>(</nobr></td>
215 <td><var>x, y</var>)</td></tr></table></dt>
216<dd>
217Return <code><var>x</var>**<var>y</var></code>.
218</dl>
219
220<P>
221<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
222 <td><nobr><b><tt id='l2h-1261' xml:id='l2h-1261' class="function">sqrt</tt></b>(</nobr></td>
223 <td><var>x</var>)</td></tr></table></dt>
224<dd>
225Return the square root of <var>x</var>.
226</dl>
227
228<P>
229Trigonometric functions:
230
231<P>
232<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
233 <td><nobr><b><tt id='l2h-1262' xml:id='l2h-1262' class="function">acos</tt></b>(</nobr></td>
234 <td><var>x</var>)</td></tr></table></dt>
235<dd>
236Return the arc cosine of <var>x</var>, in radians.
237</dl>
238
239<P>
240<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
241 <td><nobr><b><tt id='l2h-1263' xml:id='l2h-1263' class="function">asin</tt></b>(</nobr></td>
242 <td><var>x</var>)</td></tr></table></dt>
243<dd>
244Return the arc sine of <var>x</var>, in radians.
245</dl>
246
247<P>
248<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
249 <td><nobr><b><tt id='l2h-1264' xml:id='l2h-1264' class="function">atan</tt></b>(</nobr></td>
250 <td><var>x</var>)</td></tr></table></dt>
251<dd>
252Return the arc tangent of <var>x</var>, in radians.
253</dl>
254
255<P>
256<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
257 <td><nobr><b><tt id='l2h-1265' xml:id='l2h-1265' class="function">atan2</tt></b>(</nobr></td>
258 <td><var>y, x</var>)</td></tr></table></dt>
259<dd>
260Return <code>atan(<var>y</var> / <var>x</var>)</code>, in radians.
261The result is between <code>-pi</code> and <code>pi</code>.
262The vector in the plane from the origin to point <code>(<var>x</var>, <var>y</var>)</code>
263makes this angle with the positive X axis.
264The point of <tt class="function">atan2()</tt> is that the signs of both inputs are
265known to it, so it can compute the correct quadrant for the angle.
266For example, <code>atan(1</code>) and <code>atan2(1, 1)</code> are both <code>pi/4</code>,
267but <code>atan2(-1, -1)</code> is <code>-3*pi/4</code>.
268</dl>
269
270<P>
271<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
272 <td><nobr><b><tt id='l2h-1266' xml:id='l2h-1266' class="function">cos</tt></b>(</nobr></td>
273 <td><var>x</var>)</td></tr></table></dt>
274<dd>
275Return the cosine of <var>x</var> radians.
276</dl>
277
278<P>
279<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
280 <td><nobr><b><tt id='l2h-1267' xml:id='l2h-1267' class="function">hypot</tt></b>(</nobr></td>
281 <td><var>x, y</var>)</td></tr></table></dt>
282<dd>
283Return the Euclidean norm, <code>sqrt(<var>x</var>*<var>x</var> + <var>y</var>*<var>y</var>)</code>.
284This is the length of the vector from the origin to point
285<code>(<var>x</var>, <var>y</var>)</code>.
286</dl>
287
288<P>
289<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
290 <td><nobr><b><tt id='l2h-1268' xml:id='l2h-1268' class="function">sin</tt></b>(</nobr></td>
291 <td><var>x</var>)</td></tr></table></dt>
292<dd>
293Return the sine of <var>x</var> radians.
294</dl>
295
296<P>
297<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
298 <td><nobr><b><tt id='l2h-1269' xml:id='l2h-1269' class="function">tan</tt></b>(</nobr></td>
299 <td><var>x</var>)</td></tr></table></dt>
300<dd>
301Return the tangent of <var>x</var> radians.
302</dl>
303
304<P>
305Angular conversion:
306
307<P>
308<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
309 <td><nobr><b><tt id='l2h-1270' xml:id='l2h-1270' class="function">degrees</tt></b>(</nobr></td>
310 <td><var>x</var>)</td></tr></table></dt>
311<dd>
312Converts angle <var>x</var> from radians to degrees.
313</dl>
314
315<P>
316<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
317 <td><nobr><b><tt id='l2h-1271' xml:id='l2h-1271' class="function">radians</tt></b>(</nobr></td>
318 <td><var>x</var>)</td></tr></table></dt>
319<dd>
320Converts angle <var>x</var> from degrees to radians.
321</dl>
322
323<P>
324Hyperbolic functions:
325
326<P>
327<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
328 <td><nobr><b><tt id='l2h-1272' xml:id='l2h-1272' class="function">cosh</tt></b>(</nobr></td>
329 <td><var>x</var>)</td></tr></table></dt>
330<dd>
331Return the hyperbolic cosine of <var>x</var>.
332</dl>
333
334<P>
335<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
336 <td><nobr><b><tt id='l2h-1273' xml:id='l2h-1273' class="function">sinh</tt></b>(</nobr></td>
337 <td><var>x</var>)</td></tr></table></dt>
338<dd>
339Return the hyperbolic sine of <var>x</var>.
340</dl>
341
342<P>
343<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
344 <td><nobr><b><tt id='l2h-1274' xml:id='l2h-1274' class="function">tanh</tt></b>(</nobr></td>
345 <td><var>x</var>)</td></tr></table></dt>
346<dd>
347Return the hyperbolic tangent of <var>x</var>.
348</dl>
349
350<P>
351The module also defines two mathematical constants:
352
353<P>
354<dl><dt><b><tt id='l2h-1275' xml:id='l2h-1275'>pi</tt></b></dt>
355<dd>
356The mathematical constant <em>pi</em>.
357</dd></dl>
358
359<P>
360<dl><dt><b><tt id='l2h-1276' xml:id='l2h-1276'>e</tt></b></dt>
361<dd>
362The mathematical constant <em>e</em>.
363</dd></dl>
364
365<P>
366<div class="note"><b class="label">Note:</b>
367The <tt class="module">math</tt> module consists mostly of thin wrappers around
368 the platform C math library functions. Behavior in exceptional cases is
369 loosely specified by the C standards, and Python inherits much of its
370 math-function error-reporting behavior from the platform C
371 implementation. As a result,
372 the specific exceptions raised in error cases (and even whether some
373 arguments are considered to be exceptional at all) are not defined in any
374 useful cross-platform or cross-release way. For example, whether
375 <code>math.log(0)</code> returns <code>-Inf</code> or raises <tt class="exception">ValueError</tt> or
376 <tt class="exception">OverflowError</tt> isn't defined, and in
377 cases where <code>math.log(0)</code> raises <tt class="exception">OverflowError</tt>,
378 <code>math.log(0L)</code> may raise <tt class="exception">ValueError</tt> instead.
379</div>
380
381<P>
382<div class="seealso">
383 <p class="heading">See Also:</p>
384
385 <dl compact="compact" class="seemodule">
386 <dt>Module <b><tt class="module"><a href="module-cmath.html">cmath</a></tt>:</b>
387 <dd>Complex number versions of many of these functions.
388 </dl>
389</div>
390
391<DIV CLASS="navigation">
392<div class='online-navigation'>
393<p></p><hr />
394<table align="center" width="100%" cellpadding="0" cellspacing="2">
395<tr>
396<td class='online-navigation'><a rel="prev" title="5.6.8 Decimal FAQ"
397 href="decimal-faq.html"><img src='../icons/previous.png'
398 border='0' height='32' alt='Previous Page' width='32' /></A></td>
399<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
400 href="misc.html"><img src='../icons/up.png'
401 border='0' height='32' alt='Up One Level' width='32' /></A></td>
402<td class='online-navigation'><a rel="next" title="5.8 cmath "
403 href="module-cmath.html"><img src='../icons/next.png'
404 border='0' height='32' alt='Next Page' width='32' /></A></td>
405<td align="center" width="100%">Python Library Reference</td>
406<td class='online-navigation'><a rel="contents" title="Table of Contents"
407 href="contents.html"><img src='../icons/contents.png'
408 border='0' height='32' alt='Contents' width='32' /></A></td>
409<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
410 border='0' height='32' alt='Module Index' width='32' /></a></td>
411<td class='online-navigation'><a rel="index" title="Index"
412 href="genindex.html"><img src='../icons/index.png'
413 border='0' height='32' alt='Index' width='32' /></A></td>
414</tr></table>
415<div class='online-navigation'>
416<b class="navlabel">Previous:</b>
417<a class="sectref" rel="prev" href="decimal-faq.html">5.6.8 Decimal FAQ</A>
418<b class="navlabel">Up:</b>
419<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
420<b class="navlabel">Next:</b>
421<a class="sectref" rel="next" href="module-cmath.html">5.8 cmath </A>
422</div>
423</div>
424<hr />
425<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
426</DIV>
427<!--End of Navigation Panel-->
428<ADDRESS>
429See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
430</ADDRESS>
431</BODY>
432</HTML>