Mathematical functions in Python

Source: Internet
Author: User
Tags mathematical constants mathematical functions natural logarithm

5.7 Math-- Mathematical functions

This module is always available. It provides access to the mathematical functions defined by the C standard.

These functions cannot be used with complex numbers; Use the functions of the same name fromCmathModule 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. processing 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.

The following funwing are provided by this module. When t when explicitly noted otherwise, all return values are floats.

Number-theoretic and representation functions:

 

Ceil( X)
Return the ceiling XAs a float, the smallest integer value greater than or equal X.

 

FABS( X)
Return the absolute value X.

 

Floor( X)
Return the floor XAs a float, the largest integer value less than or equal X.

 

Fmod( X, Y)
Return fmod(x, y), As defined by the platform C library. Note that the python expression x % yMay not return the same result. The intent of the C standard is that fmod(x, y)Be exactly (mathematically; to infinite precision) equal x - n*yFor some integer NSuch that the result has the same sign XAnd magn0000less abs(y). Python's x % yReturns a result with the sign YInstead, and may not be exactly computable for float arguments. For example, fmod(-1e-100, 1e100)Is -1e-100, But the result of Python's -1e-100 % 1e100Is 1e100-1e-100, Which cannot be represented exactly as a float, and rounds to the surprising 1e100. For this reason, Function Fmod ()Is generally preferred when working with floats, while Python's x % yIs preferred when working with integers.

 

Frexp( X)
Return the mantissa and exponent XAs the pair (m, e). MIs a float and EIs an integer such that x == m * 2**eExactly. If XIs zero, returns (0.0, 0), Otherwise 0.5 <= abs(m) < 1. This is used to "pick apart" the internal representation of a float in a portable way.

 

Ldexp( X, I)
Return x * (2**i). This is essential, the inverse of Function Frexp ().

 

MODF( X)
Return the fractional and integer parts X. Both Results carry the sign X, And both are floats.

Note thatFrexp ()AndMODF ()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 ).

ForCeil (),Floor (), AndMODF ()Functions, note thatAllFloating-point numbers of sufficiently large magnnames 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 floatXWithabs(x) >= 2**52Necessarily has no fractional bits.

Power and logarithmic functions:

 

Exp( X)
Return e**x.

 

Log( X[, Base])
Return the logarithm XTo the given Base. If BaseIs not specified, return the natural logarithm X(That is, the logarithm to base E). Changed in Version 2.3: BaseArgument added.

 

Log10( X)
Return the base-10 logarithm X.

 

POW( X, Y)
Return x**y.

 

SQRT( X)
Return the square root X.

Trigonometric functions:

 

ACOs( X)
Return the arc cosine X, In radians.

 

Asin( X)
Return the arc sine X, In radians.

 

Atan( X)
Return the arc tangent X, In radians.

 

Atan2( Y, X)
Return atan(y / x), In radians. The result is -piAnd pi. The vector in the plane from the origin to Point (x, y)Makes this angle with the positive X axis. The point Atan2 ()Is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan(1) And atan2(1, 1)Are both pi/4, atan2(-1, -1)Is -3*pi/4.

 

Cos( X)
Return the cosine XRadians.

 

Hypot( X, Y)
Return the euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to Point (x, y).

 

Sin( X)
Return the sine XRadians.

 

Tan( X)
Return the tangent XRadians.

Angular conversion:

 

Degrees( X)
Converts Angle XFrom radians to degrees.

 

Radians( X)
Converts Angle XFrom degrees to radians.

Hyperbolic functions:

 

Cosh( X)
Return the hyperbolic cosine X.

 

Sinh( X)
Return the hyperbolic sine X.

 

Tanh( X)
Return the hyperbolic tangent X.

The module also defines two mathematical constants:

 

Pi
The mathematical constant Pi.

 

E
The mathematical constant E.

 

Note:The MathModule 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 math.log(0)Returns -InfOr raises ValueerrorOr OverflowerrorIsn' t defined, and in cases where math.log(0)Raises Overflowerror, math.log(0L)May raise ValueerrorInstead.

 

See also:

Module Cmath:
Complex Number versions of these functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.