PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Doc/PythonDocs/library/math.rst

https://github.com/rpattabi/ironruby
ReStructuredText | 393 lines | 221 code | 172 blank | 0 comment | 0 complexity | e8a18d149d47ec56ee7c3d25bd94c4a9 MD5 | raw file
  1. :mod:`math` --- Mathematical functions
  2. ======================================
  3. .. module:: math
  4. :synopsis: Mathematical functions (sin() etc.).
  5. This module is always available. It provides access to the mathematical
  6. functions defined by the C standard.
  7. These functions cannot be used with complex numbers; use the functions of the
  8. same name from the :mod:`cmath` module if you require support for complex
  9. numbers. The distinction between functions which support complex numbers and
  10. those which don't is made since most users do not want to learn quite as much
  11. mathematics as required to understand complex numbers. Receiving an exception
  12. instead of a complex result allows earlier detection of the unexpected complex
  13. number used as a parameter, so that the programmer can determine how and why it
  14. was generated in the first place.
  15. The following functions are provided by this module. Except when explicitly
  16. noted otherwise, all return values are floats.
  17. Number-theoretic and representation functions
  18. ---------------------------------------------
  19. .. function:: ceil(x)
  20. Return the ceiling of *x* as a float, the smallest integer value greater than or
  21. equal to *x*.
  22. .. function:: copysign(x, y)
  23. Return *x* with the sign of *y*. On a platform that supports
  24. signed zeros, ``copysign(1.0, -0.0)`` returns *-1.0*.
  25. .. versionadded:: 2.6
  26. .. function:: fabs(x)
  27. Return the absolute value of *x*.
  28. .. function:: factorial(x)
  29. Return *x* factorial. Raises :exc:`ValueError` if *x* is not integral or
  30. is negative.
  31. .. versionadded:: 2.6
  32. .. function:: floor(x)
  33. Return the floor of *x* as a float, the largest integer value less than or equal
  34. to *x*.
  35. .. function:: fmod(x, y)
  36. Return ``fmod(x, y)``, as defined by the platform C library. Note that the
  37. Python expression ``x % y`` may not return the same result. The intent of the C
  38. standard is that ``fmod(x, y)`` be exactly (mathematically; to infinite
  39. precision) equal to ``x - n*y`` for some integer *n* such that the result has
  40. the same sign as *x* and magnitude less than ``abs(y)``. Python's ``x % y``
  41. returns a result with the sign of *y* instead, and may not be exactly computable
  42. for float arguments. For example, ``fmod(-1e-100, 1e100)`` is ``-1e-100``, but
  43. the result of Python's ``-1e-100 % 1e100`` is ``1e100-1e-100``, which cannot be
  44. represented exactly as a float, and rounds to the surprising ``1e100``. For
  45. this reason, function :func:`fmod` is generally preferred when working with
  46. floats, while Python's ``x % y`` is preferred when working with integers.
  47. .. function:: frexp(x)
  48. Return the mantissa and exponent of *x* as the pair ``(m, e)``. *m* is a float
  49. and *e* is an integer such that ``x == m * 2**e`` exactly. If *x* is zero,
  50. returns ``(0.0, 0)``, otherwise ``0.5 <= abs(m) < 1``. This is used to "pick
  51. apart" the internal representation of a float in a portable way.
  52. .. function:: fsum(iterable)
  53. Return an accurate floating point sum of values in the iterable. Avoids
  54. loss of precision by tracking multiple intermediate partial sums::
  55. >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
  56. 0.9999999999999999
  57. >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
  58. 1.0
  59. The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the
  60. typical case where the rounding mode is half-even. On some non-Windows
  61. builds, the underlying C library uses extended precision addition and may
  62. occasionally double-round an intermediate sum causing it to be off in its
  63. least significant bit.
  64. For further discussion and two alternative approaches, see the `ASPN cookbook
  65. recipes for accurate floating point summation
  66. <http://code.activestate.com/recipes/393090/>`_\.
  67. .. versionadded:: 2.6
  68. .. function:: isinf(x)
  69. Check if the float *x* is positive or negative infinity.
  70. .. versionadded:: 2.6
  71. .. function:: isnan(x)
  72. Check if the float *x* is a NaN (not a number). For more information
  73. on NaNs, see the IEEE 754 standards.
  74. .. versionadded:: 2.6
  75. .. function:: ldexp(x, i)
  76. Return ``x * (2**i)``. This is essentially the inverse of function
  77. :func:`frexp`.
  78. .. function:: modf(x)
  79. Return the fractional and integer parts of *x*. Both results carry the sign
  80. of *x* and are floats.
  81. .. function:: trunc(x)
  82. Return the :class:`Real` value *x* truncated to an :class:`Integral` (usually
  83. a long integer). Uses the ``__trunc__`` method.
  84. .. versionadded:: 2.6
  85. Note that :func:`frexp` and :func:`modf` have a different call/return pattern
  86. than their C equivalents: they take a single argument and return a pair of
  87. values, rather than returning their second return value through an 'output
  88. parameter' (there is no such thing in Python).
  89. For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all*
  90. floating-point numbers of sufficiently large magnitude are exact integers.
  91. Python floats typically carry no more than 53 bits of precision (the same as the
  92. platform C double type), in which case any float *x* with ``abs(x) >= 2**52``
  93. necessarily has no fractional bits.
  94. Power and logarithmic functions
  95. -------------------------------
  96. .. function:: exp(x)
  97. Return ``e**x``.
  98. .. function:: expm1(x)
  99. Return ``e**x - 1``. For small floats *x*, the subtraction in
  100. ``exp(x) - 1`` can result in a significant loss of precision; the
  101. :func:`expm1` function provides a way to compute this quantity to
  102. full precision::
  103. >>> from math import exp, expm1
  104. >>> exp(1e-5) - 1 # gives result accurate to 11 places
  105. 1.0000050000069649e-05
  106. >>> expm1(1e-5) # result accurate to full precision
  107. 1.0000050000166668e-05
  108. .. versionadded:: 2.7
  109. .. function:: log(x[, base])
  110. With one argument, return the natural logarithm of *x* (to base *e*).
  111. With two arguments, return the logarithm of *x* to the given *base*,
  112. calculated as ``log(x)/log(base)``.
  113. .. versionchanged:: 2.3
  114. *base* argument added.
  115. .. function:: log1p(x)
  116. Return the natural logarithm of *1+x* (base *e*). The
  117. result is calculated in a way which is accurate for *x* near zero.
  118. .. versionadded:: 2.6
  119. .. function:: log10(x)
  120. Return the base-10 logarithm of *x*. This is usually more accurate
  121. than ``log(x, 10)``.
  122. .. function:: pow(x, y)
  123. Return ``x`` raised to the power ``y``. Exceptional cases follow
  124. Annex 'F' of the C99 standard as far as possible. In particular,
  125. ``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even
  126. when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite,
  127. ``x`` is negative, and ``y`` is not an integer then ``pow(x, y)``
  128. is undefined, and raises :exc:`ValueError`.
  129. .. versionchanged:: 2.6
  130. The outcome of ``1**nan`` and ``nan**0`` was undefined.
  131. .. function:: sqrt(x)
  132. Return the square root of *x*.
  133. Trigonometric functions
  134. -----------------------
  135. .. function:: acos(x)
  136. Return the arc cosine of *x*, in radians.
  137. .. function:: asin(x)
  138. Return the arc sine of *x*, in radians.
  139. .. function:: atan(x)
  140. Return the arc tangent of *x*, in radians.
  141. .. function:: atan2(y, x)
  142. Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``.
  143. The vector in the plane from the origin to point ``(x, y)`` makes this angle
  144. with the positive X axis. The point of :func:`atan2` is that the signs of both
  145. inputs are known to it, so it can compute the correct quadrant for the angle.
  146. For example, ``atan(1)`` and ``atan2(1, 1)`` are both ``pi/4``, but ``atan2(-1,
  147. -1)`` is ``-3*pi/4``.
  148. .. function:: cos(x)
  149. Return the cosine of *x* radians.
  150. .. function:: hypot(x, y)
  151. Return the Euclidean norm, ``sqrt(x*x + y*y)``. This is the length of the vector
  152. from the origin to point ``(x, y)``.
  153. .. function:: sin(x)
  154. Return the sine of *x* radians.
  155. .. function:: tan(x)
  156. Return the tangent of *x* radians.
  157. Angular conversion
  158. ------------------
  159. .. function:: degrees(x)
  160. Converts angle *x* from radians to degrees.
  161. .. function:: radians(x)
  162. Converts angle *x* from degrees to radians.
  163. Hyperbolic functions
  164. --------------------
  165. .. function:: acosh(x)
  166. Return the inverse hyperbolic cosine of *x*.
  167. .. versionadded:: 2.6
  168. .. function:: asinh(x)
  169. Return the inverse hyperbolic sine of *x*.
  170. .. versionadded:: 2.6
  171. .. function:: atanh(x)
  172. Return the inverse hyperbolic tangent of *x*.
  173. .. versionadded:: 2.6
  174. .. function:: cosh(x)
  175. Return the hyperbolic cosine of *x*.
  176. .. function:: sinh(x)
  177. Return the hyperbolic sine of *x*.
  178. .. function:: tanh(x)
  179. Return the hyperbolic tangent of *x*.
  180. Special functions
  181. -----------------
  182. .. function:: erf(x)
  183. Return the error function at *x*.
  184. .. versionadded:: 2.7
  185. .. function:: erfc(x)
  186. Return the complementary error function at *x*.
  187. .. versionadded:: 2.7
  188. .. function:: gamma(x)
  189. Return the Gamma function at *x*.
  190. .. versionadded:: 2.7
  191. .. function:: lgamma(x)
  192. Return the natural logarithm of the absolute value of the Gamma
  193. function at *x*.
  194. .. versionadded:: 2.7
  195. Constants
  196. ---------
  197. .. data:: pi
  198. The mathematical constant π = 3.141592..., to available precision.
  199. .. data:: e
  200. The mathematical constant e = 2.718281..., to available precision.
  201. .. impl-detail::
  202. The :mod:`math` module consists mostly of thin wrappers around the platform C
  203. math library functions. Behavior in exceptional cases follows Annex F of
  204. the C99 standard where appropriate. The current implementation will raise
  205. :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)``
  206. (where C99 Annex F recommends signaling invalid operation or divide-by-zero),
  207. and :exc:`OverflowError` for results that overflow (for example,
  208. ``exp(1000.0)``). A NaN will not be returned from any of the functions
  209. above unless one or more of the input arguments was a NaN; in that case,
  210. most functions will return a NaN, but (again following C99 Annex F) there
  211. are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or
  212. ``hypot(float('nan'), float('inf'))``.
  213. Note that Python makes no effort to distinguish signaling NaNs from
  214. quiet NaNs, and behavior for signaling NaNs remains unspecified.
  215. Typical behavior is to treat all NaNs as though they were quiet.
  216. .. versionchanged:: 2.6
  217. Behavior in special cases now aims to follow C99 Annex F. In earlier
  218. versions of Python the behavior in special cases was loosely specified.
  219. .. seealso::
  220. Module :mod:`cmath`
  221. Complex number versions of many of these functions.