马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 ]0 Q7 w: J n9 ?1 k
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. R# L3 S9 Q" R& j% u
) w. }6 G7 |5 S7 g! @" s K1 B, F6 Z方法1:7 ?! A. E( h; h; M* a5 Z
- >>> import math) B" @ U5 U+ X8 Q8 n# T& W% O# A
- >>> math.sqrt(9) {% z1 i0 W) |, \( [
- 3.0
复制代码 方法2:; K! l3 _- T8 |' ^: i2 r" o: ?
- >>> from math import sqrt; s2 n9 r0 g# j+ M5 v
- >>> sqrt(9)$ p( D3 c4 T4 J# s# G1 L8 X. U
- 3.0
复制代码
$ H! n( X8 H4 E5 [* ?) E! J
" a. y. n; H) A0 W) f& Fmath.e 表示一个常量' R- Y9 S$ ?* u' i0 H
- #表示一个常量) G' y: ~7 A8 a. e" G/ ]3 d6 a
- >>> math.e0 ?0 `& Z3 F8 A% J
- 2.718281828459045
复制代码 % X' r6 T4 T! W3 t4 D8 R8 ]
math.pi 数字常量,圆周率- C$ d' ]+ q5 ^
- #数字常量,圆周率! O4 @0 Z2 a' K% F7 O2 t9 d5 ?
- >>> print(math.pi)- e4 V$ S3 P& ^2 Y ?3 R0 \" X ^
- 3.141592653589793
复制代码
; j( D; O: ~; `/ xmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
- R( f$ B9 i$ k# x" N- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
) ]. Y3 i: `% p5 x: R - ceil(x)
" f5 W$ }/ i) ^$ x: e* p" y8 m - Return the ceiling of x as an int.: Y1 p( ]4 z* ?% B
- This is the smallest integral value >= x.$ q/ J3 N1 L! x) q* A$ b+ j
9 Q/ D6 P( m! v6 c$ V- >>> math.ceil(4.01)9 A' E7 v$ j0 m' } m) u) ^* w
- 5
7 l9 k4 V) Y k. z - >>> math.ceil(4.99)
& v4 `' o$ p9 |/ I! [ - 5
9 m d8 A4 z; |3 }2 F" s: K& m" R - >>> math.ceil(-3.99)
}' t) n& F, T H; Z. i# g - -3
4 m! e7 M( C% i: ^! O - >>> math.ceil(-3.01)
# G& ~, X& o% R) v W - -3
复制代码 - T) {- m$ q4 d: @3 s
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 p7 u0 Z |3 A1 b9 f7 w$ t- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* c; [5 O# Y Q! G; ]9 l4 D
- floor(x)
( j0 ?- T- q/ d I* p - Return the floor of x as an int.
4 M6 N! O0 T6 S - This is the largest integral value <= x.
- {0 a" o& v# j5 J2 v% a3 K# F' m - >>> math.floor(4.1)+ c0 c0 Z. ^1 V% G
- 4) } {3 ~* B+ S/ [% {: m$ h- C
- >>> math.floor(4.999)
$ _, T* C' F) t - 4
( {5 Z& N+ f' E+ L - >>> math.floor(-4.999); V7 A0 v: F! Z
- -5- R) z4 \1 Y; y" t: k
- >>> math.floor(-4.01)
0 ], q t% T6 S( e* e4 F - -5
复制代码 $ x* s$ l& }8 V( X* k
math.pow(x,y) 返回x的y次方,即x**y0 T! X6 ?2 `" R. r U( B* i. h5 p- @$ R+ i
- #返回x的y次方,即x**y! L* Z) D* X9 n" ?' J5 _, b! @6 y
- pow(x, y)
0 V5 l$ I, ]) f! h( D: ^6 L0 f - Return x**y (x to the power of y).
; O/ g. i1 h8 \( k; ? - >>> math.pow(3,4)+ m. r4 d; C" Y0 ~% s
- 81.0
: D0 Q( a! L( y+ H& b - >>>
9 o: y8 F: ^- q; w - >>> math.pow(2,7)' T$ U% t! H* B, f7 W% K1 \. P+ N: R
- 128.0
复制代码 ' K( q+ R! h3 ~! n. X
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) w% i- g, _# ^% C7 h* D
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, n+ I) H3 P( `( W+ t! l3 X - log(x[, base])' a+ n- i9 \1 s9 ?9 @$ Y- T# K
- Return the logarithm of x to the given base.3 C) g: ]2 |+ b
- If the base not specified, returns the natural logarithm (base e) of x.
- c+ \5 |2 U- i - >>> math.log(10)
/ e0 r, X: b1 m- T" x& J* J - 2.302585092994046
6 U$ z) d9 b2 E - >>> math.log(11)3 V/ D" M. y+ Z/ r; ?! m0 h& }
- 2.3978952727983707! |. c4 Z; U5 L: d1 X, n( H6 q- E
- >>> math.log(20)* s8 q: `0 A9 s. J# T8 @
- 2.995732273553991
复制代码
3 Z6 p4 w" r) a9 N9 N& p' X8 t. Wmath.sin(x) 求x(x为弧度)的正弦值
h' x- T4 s3 ]8 F! I8 W! }- #求x(x为弧度)的正弦值% O4 t. A! a( |- a- A. B
- sin(x); F! d) J, I- v1 T3 N, S" j
- Return the sine of x (measured in radians).
* P5 r& W1 P# m. T+ ] - >>> math.sin(math.pi/4)' \( W5 o8 p* I l2 C' q$ ^
- 0.7071067811865475
; ^) k+ p4 p% Q - >>> math.sin(math.pi/2)
* @% |# v( b; G) Z5 j8 ?6 S) r - 1.0
/ S& }- ]' {0 p4 j( a - >>> math.sin(math.pi/3)' L/ q0 X% I; n- y. p; ?& @! w1 a
- 0.8660254037844386
复制代码 2 H% Y" Y. Q" @
math.cos(x) 求x的余弦,x必须是弧度
; Q/ y% B. v5 b, t- b- #求x的余弦,x必须是弧度
, y3 N9 s+ `/ D - cos(x) s! P2 d: I) x" |5 T# V) V6 s& T
- Return the cosine of x (measured in radians).! @( Q6 V9 k$ f1 G5 ~) P8 C
- #math.pi/4表示弧度,转换成角度为45度
6 @) ?# d/ L4 m; N3 ^0 d - >>> math.cos(math.pi/4)
% F& Y" }' Z; o+ q- ?2 ` - 0.7071067811865476
% Q# p O: U, t/ S - math.pi/3表示弧度,转换成角度为60度: M0 O9 k2 E0 q+ _6 F6 s2 m
- >>> math.cos(math.pi/3)
3 R" E& Q+ G+ t5 S - 0.50000000000000019 F6 p, e W" Q |1 W' k
- math.pi/6表示弧度,转换成角度为30度
; N, I8 J3 m+ ]7 O4 ^ - >>> math.cos(math.pi/6)4 b% X( M; w8 P& q, d
- 0.8660254037844387
复制代码
( |- W* Z6 @7 N0 U. ~" L2 m( Jmath.tan(x) 返回x(x为弧度)的正切值
% z" B" b6 V- a+ V- #返回x(x为弧度)的正切值
, v% l4 A$ l9 }3 @ - tan(x)! f% \' L0 d: A9 S+ h& M8 A
- Return the tangent of x (measured in radians).4 M0 y1 ]7 F* \% q, x: V
- >>> math.tan(math.pi/4)7 n9 y: D5 L$ Z# Y
- 0.9999999999999999
+ {$ m) _. w9 Z. b% [& ]5 u - >>> math.tan(math.pi/6)
. D2 ]; x. U- s4 S$ w2 a3 ^ - 0.5773502691896257! N1 m6 h% ]# I( Z6 H
- >>> math.tan(math.pi/3)
' H9 p2 \# X% a: ` - 1.7320508075688767
复制代码
( \' D& g( p+ P$ x' e6 Xmath.degrees(x) 把x从弧度转换成角度# G3 b% M! ?; J" m1 c% O) T! k
- #把x从弧度转换成角度4 H. h! q2 w( m0 l8 |
- degrees(x)
/ s$ ~ n! @+ ?" n" u4 h - Convert angle x from radians to degrees.5 v" D! D! j+ o" }8 V& r% {2 Q
- # _ t1 q5 i F2 \1 J+ N) C
- >>> math.degrees(math.pi/4)6 J1 ^2 X3 a' X2 L" @. K# k
- 45.0
+ y" q5 V% C9 V, u: ?) a - >>> math.degrees(math.pi)
w( }' u/ O, \. U7 M' u4 B. x - 180.0
# I! h# y. r$ i0 n5 D - >>> math.degrees(math.pi/6)
. ? k( k7 P4 i4 Y - 29.9999999999999968 P3 w# g6 S. F/ g4 ?6 G$ r
- >>> math.degrees(math.pi/3)
1 n: b V5 [7 X3 \# I% q& F. v1 g* y+ ^ - 59.99999999999999
复制代码
* n. i5 u) N/ a" @$ d& Gmath.radians(x) 把角度x转换成弧度: e4 f' C# P/ G/ u" k; T' a$ I5 I/ _
- #把角度x转换成弧度$ B2 p6 ]1 H* @* ?* S4 v, |; y; T. P
- radians(x)
8 u- a$ [+ o# u6 m - Convert angle x from degrees to radians.
; |8 R/ o9 s0 b0 D0 t - >>> math.radians(45)$ n9 n# b9 X8 m' W* ]% z
- 0.7853981633974483
" F. ^/ {& N9 w* W - >>> math.radians(60)7 b+ k$ Y2 U; d2 b2 c6 \' m. j+ k# y% |
- 1.0471975511965976
复制代码
: Z8 b8 ^4 _; p% kmath.copysign(x,y) 把y的正负号加到x前面,可以使用0: F9 v0 k) C0 j
- #把y的正负号加到x前面,可以使用0# q- b" J3 I8 X8 @
- copysign(x, y)
! q7 P3 t5 l& ~% e - Return a float with the magnitude (absolute value) of x but the sign 8 B- ?, @- h& E6 Z& X
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) - O, d9 o6 J( h5 _) a. }
- returns -1.0.3 Z" R. o! ]) h: D* ? j7 g+ d
- 5 R, [9 K# U2 R7 _$ P2 u8 ]% f
- >>> math.copysign(2,3)
6 ?1 V; R' h D - 2.0
2 r& C- \5 E# O- Y2 d5 k* \" l - >>> math.copysign(2,-3)
. ]8 }3 A' A: D5 M - -2.06 n& i7 u" U8 j- V# G0 }
- >>> math.copysign(3,8)) ~: a i% r: a+ M; K2 F
- 3.09 o$ M+ O6 G# k' u8 b
- >>> math.copysign(3,-8)- t+ E- {+ t5 T# q( _ E P
- -3.0
复制代码
2 R g, W/ A- a' ~math.exp(x) 返回math.e,也就是2.71828的x次方
0 o' \# D2 ?6 x1 _/ O9 M- #返回math.e,也就是2.71828的x次方; `8 Z9 B# o; ]/ s6 |
- exp(x)
: K1 P% L1 }& @, e2 q - Return e raised to the power of x.
0 n/ F! C9 G; U4 V* v1 W( C+ a$ o - 0 F# B4 x4 t. g% z
- >>> math.exp(1)2 K: l& f! u7 a2 a# `# a: t: q3 b
- 2.7182818284590450 |; [' w4 m" C6 s( e9 {; T, D2 A, d
- >>> math.exp(2)
$ [( c" _8 ?/ `. N - 7.38905609893065$ ]# c2 ^% ~& X6 ]& a
- >>> math.exp(3)+ r( O% k* B& w2 S+ E
- 20.085536923187668
复制代码 & A2 Y: a `1 Q2 E2 u* \4 W0 q$ s
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
4 S1 n( ?( o x; X! [- #返回math.e的x(其值为2.71828)次方的值减1: `; Y+ V7 L0 P, l$ _, d
- expm1(x)1 S* ]$ j9 L5 ]- P
- Return exp(x)-1.( f9 U$ k# ^8 U; O2 u) F2 H
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.3 d, ]% I' b) t; V6 f8 ?
, d# N0 Q8 s7 f- N, Q- >>> math.expm1(1)
0 ]6 I+ o6 ~5 ^+ r7 c" k2 r - 1.718281828459045# v. p+ ^0 f6 K9 T0 R4 D
- >>> math.expm1(2)
( Z) x m L5 _8 a1 g# I/ ?7 q - 6.38905609893065
@9 ^6 ~" p* f - >>> math.expm1(3)
! G5 [) l5 ]: p - 19.085536923187668
复制代码
8 i4 _; [4 s- O9 I( \7 |math.fabs(x) 返回x的绝对值) B6 ] }2 @' l* ~: p7 i8 ?
- #返回x的绝对值
& m& `3 ^1 ]" L3 w, p - fabs(x)" {: D& N, _+ }! M% K# |
- Return the absolute value of the float x.8 v/ |4 ~4 O$ ?* s' s. t
- 0 U* p+ g% |# K# h- h9 V
- >>> math.fabs(-0.003): L0 p% K" N' y8 s* Y
- 0.003, j$ k# U1 \: F$ o4 u! Z, q
- >>> math.fabs(-110)
' b" `, X4 H: O0 W% k# o8 B+ f! F - 110.0
s$ c5 s, }; N% M9 b5 ~: k - >>> math.fabs(100)- R5 I" |/ \) q; d7 w
- 100.0
复制代码
7 N0 l/ {7 D+ m6 z) `math.factorial(x) 取x的阶乘的值, x# n/ s7 f- ^8 H" j- y' }
- #取x的阶乘的值4 b$ |4 y! V& W" ^3 l1 p
- factorial(x) -> Integral
) h7 N) k. U" i( m; d; _, t - Find x!. Raise a ValueError if x is negative or non-integral.3 u7 I- U G1 Z/ a% v4 p* T
- >>> math.factorial(1)$ C; U @( L8 [& c. _! }
- 17 p$ A% C: i/ Y$ B( c
- >>> math.factorial(2)
; U3 k4 e& ~1 l% l# o4 }0 X - 2
8 L0 A4 V0 L: y( x4 `- j: l8 } - >>> math.factorial(3)# g) J; I8 C: ?# O, `. s c" T5 |; B
- 6, T3 R" |- u2 a; T
- >>> math.factorial(5)2 `. j8 |, C3 r' L) D: f" `1 y
- 120$ M# w4 a4 d. i& D) U/ c2 f
- >>> math.factorial(10)
: ^+ B* ?% }0 O/ A1 z1 n0 E- i - 3628800
复制代码 ) b8 y7 W8 D- j. F2 g% b2 q" `
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数1 h2 T) F$ o) _
- #得到x/y的余数,其值是一个浮点数0 |. i5 O/ B, H
- fmod(x, y)
% g% M, z9 ^ s2 D - Return fmod(x, y), according to platform C. x % y may differ.
; W: C; }& \& ^! A4 o8 T" R7 @9 x - >>> math.fmod(20,3)
# i& W( u& p: V7 f" [+ B( m7 h - 2.0
5 d+ k- F+ k d( ] - >>> math.fmod(20,7)! l( |' W' m- O% D! F
- 6.0
复制代码 # J, Z1 x% F' r! t& M4 E- X
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: \, B( M$ ? {0 V- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 R! u ]# ^' k - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 E0 @ r B" C+ N$ x3 z; G' L
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
- e- P) v; c" ?% X2 b3 r1 e; j - frexp(x)! e& D' b, y+ B* R- X2 `
- Return the mantissa and exponent of x, as pair (m, e).
7 _, O; X1 h% a1 r' T* }% c, X - m is a float and e is an int, such that x = m * 2.**e.
4 U; G, D0 [4 ]& t - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
+ Y6 z: P( J7 x- p5 t1 N4 y9 I - >>> math.frexp(10)
8 W& l# o2 b7 L, |1 j! C - (0.625, 4)* x8 u+ g9 X% P8 ^- P7 [1 Z
- >>> math.frexp(75)$ @* F4 v5 b) w7 I
- (0.5859375, 7); I/ D, [: {3 @
- >>> math.frexp(-40)
1 L! ~: {1 I! }% E; I3 q, @! D& W7 G - (-0.625, 6)
, n' x/ T- p( \! O2 } - >>> math.frexp(-100)
; O9 o. e7 _8 I" q$ H$ w - (-0.78125, 7)
7 E) I0 N; |, I H4 q1 ^+ B9 `0 q - >>> math.frexp(100)
6 B$ t! q, Y9 u- [4 _ - (0.78125, 7)
复制代码 7 {( Z/ [1 \ o4 D, E* g: Z
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)3 d1 s& p% X+ j; u
- #对迭代器里的每个元素进行求和操作
' C7 p1 Q) w) S; c1 i; @! T - fsum(iterable)6 ^3 \; Y i! b1 K5 [# \; q
- Return an accurate floating point sum of values in the iterable.
5 t+ h: S: }# g3 f& }) ~0 N - Assumes IEEE-754 floating point arithmetic.
2 F7 o* g5 r& l) U# e - >>> math.fsum([1,2,3,4])' S0 ]2 |5 A- ]7 E4 t
- 10.0
% O* G( s+ ?* Q z - >>> math.fsum((1,2,3,4))* d0 t5 p7 i3 }% n, y1 t1 f- Y
- 10.0& H& M) W* E5 p
- >>> math.fsum((-1,-2,-3,-4))' d2 z* B; g. ^- K/ K3 _
- -10.01 ]1 I' J6 o& y5 p, D' {1 ^1 ]2 A
- >>> math.fsum([-1,-2,-3,-4])* S' ?& }( p7 ?% J% B
- -10.0
复制代码
' y' w, Y( _+ d; `4 T4 A; B! b dmath.gcd(x,y) 返回x和y的最大公约数
3 a9 I7 `% Q/ A, T) f- #返回x和y的最大公约数; ]3 @: g" S) `, K9 [9 Q) z
- gcd(x, y) -> int
7 S# A1 s; i& R6 p6 }) p9 F - greatest common divisor of x and y5 p; q; v9 b. m; I) _' \' C: e
- >>> math.gcd(8,6)
# m7 \; Q8 ^* \4 G+ _; E- H) b - 25 s) K4 F) l) U. J
- >>> math.gcd(40,20)- R# q& w2 }- e! G7 `8 R
- 20
2 j$ l, ?5 M7 X X# M" P - >>> math.gcd(8,12)
" c; i/ Q, F, `; a - 4
复制代码 3 Y* g4 b5 v% u* V1 @% X( _
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False: l4 [; P- w5 [. e$ v O
- #得到(x**2+y**2),平方的值
2 r5 Z7 e& m- `8 [( v - hypot(x, y)
, a8 i4 B* X3 K8 e5 ? - Return the Euclidean distance, sqrt(x*x + y*y).' R+ B6 A8 U% ~# \0 W% H: K
- >>> math.hypot(3,4)
; w$ L" v3 W% j - 5.0
$ n8 J: T3 I7 d U0 J8 N - >>> math.hypot(6,8)% i- N- J) c# l7 p6 |4 D2 q
- 10.0
复制代码 [2 t9 ?! \8 J9 ^* J7 }7 _
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
* n; X3 p$ a- [+ U& Y3 ?' E- #如果x是不是无穷大的数字,则返回True,否则返回False
- ~& V5 b. E# b7 j) t' V - isfinite(x) -> bool
' c: x5 L" a4 j r1 |: D$ P4 { - Return True if x is neither an infinity nor a NaN, and False otherwise.
: \* h4 O7 g; Z( U2 [/ j/ A - >>> math.isfinite(100)( F# T0 y O# h
- True- O Z% W/ _7 x+ H: E3 ?4 V2 L
- >>> math.isfinite(0)" _" O1 K" |. p3 _/ d& t( m
- True
1 s b& E' ?- x q$ d! o; s1 y - >>> math.isfinite(0.1)
9 B- E7 V2 J) T1 y) n5 S# E - True. T/ I- p7 u& D% q# s y. D* M
- >>> math.isfinite("a")
- B5 x9 Q. N+ y+ G( w& ? - >>> math.isfinite(0.0001)
( g( y: q/ ?3 r, A - True
复制代码 % n; N, T$ b2 h; k) q- G4 r0 `0 _6 L
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
F1 L. \9 L" H* G6 A' m- #如果x是正无穷大或负无穷大,则返回True,否则返回False, p' b1 U+ B2 P* J
- isinf(x) -> bool( I# E: [: C! s& Q
- Return True if x is a positive or negative infinity, and False otherwise.. B0 Z" `2 L( n! I% H7 W# ]" o
- >>> math.isinf(234)
4 R+ K k" R# G/ B - False* z) v, d" O5 E3 B% W$ L
- >>> math.isinf(0.1)
: [: P( u7 F" u# K/ v: v - False
复制代码
5 n; y/ n; k2 d6 S+ G: Q$ G1 h4 Vmath.isnan(x) 如果x不是数字True,否则返回False' z, F+ v" r* Y9 `( f
- #如果x不是数字True,否则返回False* u5 t5 c5 {. s/ I0 a3 r
- isnan(x) -> bool
( L' D) J. q, M8 T4 l) b - Return True if x is a NaN (not a number), and False otherwise.
8 L; G. H3 B% i5 S! y9 k - >>> math.isnan(23)
9 K/ h/ y/ v3 O0 U - False' c* S5 N7 O/ R$ Q) m8 _( `
- >>> math.isnan(0.01)' q# x1 E g- p: T; z5 t
- False
复制代码 # r; z- { A+ A: L$ v" u
math.ldexp(x,i) 返回x*(2**i)的值
6 U- }# j! o9 [1 w+ |$ M- #返回x*(2**i)的值* ?- v' J9 E/ X0 i' Q
- ldexp(x, i)
2 d# n6 a& I6 e) K2 b - Return x * (2**i).
. K/ k |8 M. ]9 i# N/ j - >>> math.ldexp(5,5)
- j2 G: f5 D0 U! K" P - 160.0. B$ O+ d8 L, ~
- >>> math.ldexp(3,5)6 U6 @0 K8 ?" H1 } g
- 96.0
复制代码
' [: q) k$ l9 t5 L' s- N: rmath.log10(x) 返回x的以10为底的对数% A2 n4 B' ]) s6 D! I X
- #返回x的以10为底的对数
4 E+ t& _% t8 S; V - log10(x)
2 @6 T& I8 B p0 P0 O/ z: U - Return the base 10 logarithm of x.! v9 O$ Y, x4 x5 }) c+ N6 ~
- >>> math.log10(10)
% G6 `8 X2 F1 K$ ~ - 1.0
. T. n& L7 n9 Y1 q - >>> math.log10(100)
) \' A& c4 b" a _9 C* s X - 2.0
: r8 X8 T; |; ]2 H( f, [- z8 X - #即10的1.3次方的结果为209 @+ [& p# m7 O0 ^3 E
- >>> math.log10(20)- m1 W0 n- g2 L# Q
- 1.3010299956639813
复制代码
E# W& D) N- C" Nmath.log1p(x) 返回x+1的自然对数(基数为e)的值4 V; a' X& p% f
- #返回x+1的自然对数(基数为e)的值
, M* G* }, O, L - log1p(x)+ X5 [# t" _/ _! _2 i8 B4 k1 m
- Return the natural logarithm of 1+x (base e).
! ~8 j! A$ F7 {$ j! \8 e7 H - The result is computed in a way which is accurate for x near zero.
) ~& L8 I! ?7 H$ A- g M - >>> math.log(10)( Y& w2 ]; b: w0 y6 r& w
- 2.302585092994046
$ z2 A* I: B* e5 F7 Z5 d; @$ u: D - >>> math.log1p(10)
7 b* X, f. Y+ Y | - 2.3978952727983707- k5 ^& i5 m; x& t
- >>> math.log(11)
# p! F) p) w% } - 2.3978952727983707
复制代码
9 M8 Q$ }* c# hmath.log2(x) 返回x的基2对数
5 ]/ P4 B& x3 T6 f& Y- #返回x的基2对数
$ t [# A! }1 Q& j. o - log2(x)
0 K1 d9 }4 P# O - Return the base 2 logarithm of x.
; a0 f% R7 j; `7 \1 | - >>> math.log2(32)
1 Y0 I" r6 B2 Q2 E5 p# R. b) D - 5.0
: L' ^: A/ @: s& |) B4 X - >>> math.log2(20)+ \6 k3 y. a- u1 v3 h0 p5 D5 F
- 4.321928094887363/ V: G1 S4 T3 x5 W" a0 S+ n
- >>> math.log2(16)$ I: C/ m/ v$ R+ R" W y
- 4.0
复制代码 ' f- M2 M* ^2 [' D r- \, u" b% `
math.modf(x) 返回由x的小数部分和整数部分组成的元组5 Z i! |6 u. i8 q3 }, {+ \6 i
- #返回由x的小数部分和整数部分组成的元组% j5 t5 @0 [& {3 F) _
- modf(x)
6 P4 U6 L8 k5 U1 @7 ` - Return the fractional and integer parts of x. Both results carry the sign
: G Q. l% m! a# l9 d3 Y3 z - of x and are floats.; r) g/ m) i1 Y, s$ x3 q+ n
- >>> math.modf(math.pi)9 l8 x! Q5 c1 C- p/ J
- (0.14159265358979312, 3.0)2 T5 M7 Z( d$ y( k5 v
- >>> math.modf(12.34): M3 |. E P& T$ w l6 }# S& U4 r
- (0.33999999999999986, 12.0)
复制代码 ! _9 f. h3 ^, b, X, O6 C) `
math.sqrt(x) 求x的平方根
5 U* n) j/ a, T( b' J- #求x的平方根
+ e% R2 h7 I9 [ - sqrt(x)- b/ ^- ^+ p6 \! c! A& S. E* j
- Return the square root of x.0 M- p9 g: Y4 x' u
- >>> math.sqrt(100)/ |9 q f: Y7 V3 g1 V; c; U
- 10.0
6 }8 s( Z7 r$ }$ E, X - >>> math.sqrt(16)9 _$ O2 T( a6 s5 F O& p
- 4.0* J1 U3 y, r( p, w" O% c7 r6 G* P
- >>> math.sqrt(20)
. O& p# b. Z' f4 e - 4.47213595499958
复制代码 1 d" g" h) {/ g1 l2 |7 I) ]9 a
math.trunc(x) 返回x的整数部分- #返回x的整数部分
* u( v& i Y, F! q% f. H* ?* K! r - trunc(x:Real) -> Integral
& K. X# C n( I - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# i8 H: y( E$ I7 \% a3 i: | - >>> math.trunc(6.789)* K3 U2 z7 a; p3 {' ?- K+ y/ N
- 6
0 z+ T* C: T* E# ?5 c! M% c# _ - >>> math.trunc(math.pi)
/ D1 t8 K9 |$ }$ ^0 m4 x - 31 x R2 n' e5 Z2 ]% r9 {
- >>> math.trunc(2.567)# j% Y! |3 E O1 N& u' E
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|