基本数学函数

ceil()

The method ceil() returns ceiling value of x - the smallest integer not less than x. we need to call this function using math static object.

Syntax&P

import math #导入math模块
math.ceil( x )
#x -- This is a numeric expression.

eg

>>> import math
>>> math.ceil(1.3)
2.0
>>> math.ceil(1)
1.0

exp()

The method exp() returns returns exponential of x: ex. Syntax&P

import math
math.exp(x)
#x -- This is a numeric expression.

eg

>>> math.exp(2)
7.38905609893065

fabs()

The method fabs() returns the absolute value of x.

Syntax&P

import math
math.fabs(x)
#x -- This is a numeric value.

eg

>>> math.fabs(1.3)
1.3
###abs() VS fabs()
>>> abs(12)
12
>>> math.fabs(12)
12.0

floor()

The method floor() returns floor of x - the largest integer not greater than x.

Syntax&P

import math
math.floor(x)
#x -- This is a numeric expression.

eg

>>> math.floor(12)
12.0
>>> math.floor(12.3)
12.0

log()

The method log() returns natural logarithm of x, for x > 0.

Syntax&P

import math
math.log(x)
#x -- This is a numeric expression.

eg

>>> math.log(10)
2.302585092994046

log10()

The method log10() returns base-10 logarithm of x for x > 0.

Syntax&P

import math
math.log10(x)
#x -- This is a numeric expression.

eg

>>> math.log10(10)
1.0

modf()

Returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.

Syntax&P

import math
math.modf(x)
#x -- This is a numeric expression.

eg

>>> math.modf(12)
(0.0, 12.0)
>>> math.modf(math.e)
(0.7182818284590451, 2.0)

pow()

This method pow() returns the value of x**y

Syntax&P

import math
math.pow(x , y)
#x -- This is a numeric expression
#y  -- This is also a numeric expression

eg

>>> math.pow(2 , 4)
16.0

sqrt()

The method sqrt() returns the square root of x for x > 0.

import math
math.sqrt(x)
#x -- This is a numeric expression.

This method returns square root of x for x > 0.

>>> math.sqrt(3)
1.7320508075688772

三角函数

acos()

Returns the arc cosine of x, in radians.

Syntax&P

import math
math.acos(x)
#x must be a numeric value in the range -1 to 1. If x is greater than 1 then it will generate an error.

eg

>>> math.acos(math.sqrt(3)/2)
0.5235987755982989

asin()

returns the arc sine of x, in radians.

Syntax&P

import math
math.asin(x)
#x must be a numeric value in the range -1 to 1. If x is greater than 1 then it will generate an error.

eg

>>> math.asin(math.sqrt(3)/2)
1.0471975511965976

atan()

Return the arc tangent of x, in radians.

Syntax&P

import math
math.atan(x)
#x is a numeric value

eg

>>> math.atan(1)
0.7853981633974483

atan2()

Return atan(y / x), in radians.

Syntax&P

import math
math.atan2(y,x)

eg

>>> math.atan2(1,2)
0.4636476090008061

cos()

Return the cosine of x radians.

Syntax&P

import math
math,cos(x)

eg

>>> math.cos(math.pi)
-1.0

hypot()

Return the Euclidean norm, sqrt(xx + yy).

Syntax&P

import math
math.hypot(x , y)

eg

>>> math.hypot(3,4)
5.0

sin()

Return the sine of x radians.

Syntax&P

import math
math.sin(x)

eg

>>> math.sin(math.pi)
1.2246467991473532e-16

tan()

Return the tangent of x radians.

Syntax&P

import math
math.tan(x)

eg

>>> math.tan(math.pi/4)
0.9999999999999999

degrees()

Converts angle x from radians to degrees.

Syntax&P

import math
math.degrees(x)

eg

>>> math.degrees(math.pi)
180.0

radians()

Converts angle x from degrees to radians.

Syntax&P

import math
math.radians()

eg

>>> math.radians(180)
3.141592653589793