Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-math.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-cmath.html" />
<link rel="prev" href="module-decimal.html" />
<link rel="parent" href="misc.html" />
<link rel="next" href="module-cmath.html" />
<meta name='aesop' content='information' />
<title>5.7 math -- Mathematical functions</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.6.8 Decimal FAQ"
href="decimal-faq.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.8 cmath "
href="module-cmath.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="decimal-faq.html">5.6.8 Decimal FAQ</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cmath.html">5.8 cmath </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION007700000000000000000">
5.7 <tt class="module">math</tt> --
Mathematical functions</A>
</H1>
<P>
<A NAME="module-math"></A>
<P>
This module is always available. It provides access to the
mathematical functions defined by the C standard.
<P>
These functions cannot be used with complex numbers; use the functions
of the same name from the <tt class="module"><a href="module-cmath.html">cmath</a></tt> module if you require
support for complex numbers. The distinction between functions which
support complex numbers and those which don't is made since most users
do not want to learn quite as much mathematics as required to
understand complex numbers. Receiving an exception instead of a
complex result allows earlier detection of the unexpected complex
number used as a parameter, so that the programmer can determine how
and why it was generated in the first place.
<P>
The following functions are provided by this module. Except
when explicitly noted otherwise, all return values are floats.
<P>
Number-theoretic and representation functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1250' xml:id='l2h-1250' class="function">ceil</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the ceiling of <var>x</var> as a float, the smallest integer value
greater than or equal to <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1251' xml:id='l2h-1251' class="function">fabs</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the absolute value of <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1252' xml:id='l2h-1252' class="function">floor</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the floor of <var>x</var> as a float, the largest integer value
less than or equal to <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1253' xml:id='l2h-1253' class="function">fmod</tt></b>(</nobr></td>
<td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return <code>fmod(<var>x</var>, <var>y</var>)</code>, as defined by the platform C library.
Note that the Python expression <code><var>x</var> % <var>y</var></code> may not return
the same result. The intent of the C standard is that
<code>fmod(<var>x</var>, <var>y</var>)</code> be exactly (mathematically; to infinite
precision) equal to <code><var>x</var> - <var>n</var>*<var>y</var></code> for some integer
<var>n</var> such that the result has the same sign as <var>x</var> and
magnitude less than <code>abs(<var>y</var>)</code>. Python's
<code><var>x</var> % <var>y</var></code> returns a result with the sign of
<var>y</var> instead, and may not be exactly computable for float arguments.
For example, <code>fmod(-1e-100, 1e100)</code> is <code>-1e-100</code>, but the
result of Python's <code>-1e-100 % 1e100</code> is <code>1e100-1e-100</code>, which
cannot be represented exactly as a float, and rounds to the surprising
<code>1e100</code>. For this reason, function <tt class="function">fmod()</tt> is generally
preferred when working with floats, while Python's
<code><var>x</var> % <var>y</var></code> is preferred when working with integers.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1254' xml:id='l2h-1254' class="function">frexp</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the mantissa and exponent of <var>x</var> as the pair
<code>(<var>m</var>, <var>e</var>)</code>. <var>m</var> is a float and <var>e</var> is an
integer such that <code><var>x</var> == <var>m</var> * 2**<var>e</var></code> exactly.
If <var>x</var> is zero, returns <code>(0.0, 0)</code>, otherwise
<code>0.5 &lt;= abs(<var>m</var>) &lt; 1</code>. This is used to "pick apart" the
internal representation of a float in a portable way.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1255' xml:id='l2h-1255' class="function">ldexp</tt></b>(</nobr></td>
<td><var>x, i</var>)</td></tr></table></dt>
<dd>
Return <code><var>x</var> * (2**<var>i</var>)</code>. This is essentially the inverse of
function <tt class="function">frexp()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1256' xml:id='l2h-1256' class="function">modf</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the fractional and integer parts of <var>x</var>. Both results
carry the sign of <var>x</var>, and both are floats.
</dl>
<P>
Note that <tt class="function">frexp()</tt> and <tt class="function">modf()</tt> have a different
call/return pattern than their C equivalents: they take a single
argument and return a pair of values, rather than returning their
second return value through an `output parameter' (there is no such
thing in Python).
<P>
For the <tt class="function">ceil()</tt>, <tt class="function">floor()</tt>, and <tt class="function">modf()</tt>
functions, note that <em>all</em> floating-point numbers of sufficiently
large magnitude are exact integers. Python floats typically carry no more
than 53 bits of precision (the same as the platform C double type), in
which case any float <var>x</var> with <code>abs(<var>x</var>) &gt;= 2**52</code>
necessarily has no fractional bits.
<P>
Power and logarithmic functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1257' xml:id='l2h-1257' class="function">exp</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return <code>e**<var>x</var></code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1258' xml:id='l2h-1258' class="function">log</tt></b>(</nobr></td>
<td><var>x</var><big>[</big><var>, base</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the logarithm of <var>x</var> to the given <var>base</var>.
If the <var>base</var> is not specified, return the natural logarithm of <var>x</var>
(that is, the logarithm to base <em>e</em>).
<span class="versionnote">Changed in version 2.3:
<var>base</var> argument added.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1259' xml:id='l2h-1259' class="function">log10</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the base-10 logarithm of <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1260' xml:id='l2h-1260' class="function">pow</tt></b>(</nobr></td>
<td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return <code><var>x</var>**<var>y</var></code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1261' xml:id='l2h-1261' class="function">sqrt</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the square root of <var>x</var>.
</dl>
<P>
Trigonometric functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1262' xml:id='l2h-1262' class="function">acos</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc cosine of <var>x</var>, in radians.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1263' xml:id='l2h-1263' class="function">asin</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc sine of <var>x</var>, in radians.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1264' xml:id='l2h-1264' class="function">atan</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc tangent of <var>x</var>, in radians.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1265' xml:id='l2h-1265' class="function">atan2</tt></b>(</nobr></td>
<td><var>y, x</var>)</td></tr></table></dt>
<dd>
Return <code>atan(<var>y</var> / <var>x</var>)</code>, in radians.
The result is between <code>-pi</code> and <code>pi</code>.
The vector in the plane from the origin to point <code>(<var>x</var>, <var>y</var>)</code>
makes this angle with the positive X axis.
The point of <tt class="function">atan2()</tt> is that the signs of both inputs are
known to it, so it can compute the correct quadrant for the angle.
For example, <code>atan(1</code>) and <code>atan2(1, 1)</code> are both <code>pi/4</code>,
but <code>atan2(-1, -1)</code> is <code>-3*pi/4</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1266' xml:id='l2h-1266' class="function">cos</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the cosine of <var>x</var> radians.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1267' xml:id='l2h-1267' class="function">hypot</tt></b>(</nobr></td>
<td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return the Euclidean norm, <code>sqrt(<var>x</var>*<var>x</var> + <var>y</var>*<var>y</var>)</code>.
This is the length of the vector from the origin to point
<code>(<var>x</var>, <var>y</var>)</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1268' xml:id='l2h-1268' class="function">sin</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the sine of <var>x</var> radians.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1269' xml:id='l2h-1269' class="function">tan</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the tangent of <var>x</var> radians.
</dl>
<P>
Angular conversion:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1270' xml:id='l2h-1270' class="function">degrees</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Converts angle <var>x</var> from radians to degrees.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1271' xml:id='l2h-1271' class="function">radians</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Converts angle <var>x</var> from degrees to radians.
</dl>
<P>
Hyperbolic functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1272' xml:id='l2h-1272' class="function">cosh</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic cosine of <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1273' xml:id='l2h-1273' class="function">sinh</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic sine of <var>x</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1274' xml:id='l2h-1274' class="function">tanh</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic tangent of <var>x</var>.
</dl>
<P>
The module also defines two mathematical constants:
<P>
<dl><dt><b><tt id='l2h-1275' xml:id='l2h-1275'>pi</tt></b></dt>
<dd>
The mathematical constant <em>pi</em>.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-1276' xml:id='l2h-1276'>e</tt></b></dt>
<dd>
The mathematical constant <em>e</em>.
</dd></dl>
<P>
<div class="note"><b class="label">Note:</b>
The <tt class="module">math</tt> module consists mostly of thin wrappers around
the platform C math library functions. Behavior in exceptional cases is
loosely specified by the C standards, and Python inherits much of its
math-function error-reporting behavior from the platform C
implementation. As a result,
the specific exceptions raised in error cases (and even whether some
arguments are considered to be exceptional at all) are not defined in any
useful cross-platform or cross-release way. For example, whether
<code>math.log(0)</code> returns <code>-Inf</code> or raises <tt class="exception">ValueError</tt> or
<tt class="exception">OverflowError</tt> isn't defined, and in
cases where <code>math.log(0)</code> raises <tt class="exception">OverflowError</tt>,
<code>math.log(0L)</code> may raise <tt class="exception">ValueError</tt> instead.
</div>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-cmath.html">cmath</a></tt>:</b>
<dd>Complex number versions of many of these functions.
</dl>
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.6.8 Decimal FAQ"
href="decimal-faq.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.8 cmath "
href="module-cmath.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="decimal-faq.html">5.6.8 Decimal FAQ</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cmath.html">5.8 cmath </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>