马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 m% |- T: h1 X2 W2 u2 _' _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& R6 P0 r4 i2 r$ S4 v
+ D# ~# S0 L( \* F方法1:- k( T6 f2 e9 l3 ?' B) a3 i
- >>> import math
: s4 W, W9 ]+ p9 \: S2 `+ }; L - >>> math.sqrt(9)$ Y4 C/ E/ Y5 z) [4 e
- 3.0
复制代码 方法2:" u/ l2 J; y" W5 L1 B- G; d8 x. C
- >>> from math import sqrt
: c; }/ E5 N; I6 m) _- f6 U - >>> sqrt(9)! w6 P8 D7 o/ `2 E1 F
- 3.0
复制代码 , b! q$ A, n$ F( K n2 L
0 C2 K: A) K6 }" Q( H3 y
math.e 表示一个常量 R6 A2 A7 S x
- #表示一个常量6 z5 `1 I( o; [( H; B# X$ M8 e& Y
- >>> math.e
6 Y+ g. r( k1 S0 ` - 2.718281828459045
复制代码
! ]* j9 W* L6 l; A- J4 ~& B: a! K$ }math.pi 数字常量,圆周率4 `1 _0 K5 ?. m
- #数字常量,圆周率# S. ?/ E i) U3 n
- >>> print(math.pi)
3 R$ m' D( X3 J K2 O( j8 ~ - 3.141592653589793
复制代码 " ]( y8 o, Y4 i$ ^9 k0 `' H
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x( z* d+ H5 |- M1 Z
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 f1 c, V& r) s4 u
- ceil(x); c$ A0 `; B A4 I
- Return the ceiling of x as an int.4 H" R, Z4 k& @- b! D- }) D6 i
- This is the smallest integral value >= x.
1 ^5 P \/ ~$ ?- r9 v, e
. C _! @( W k. K+ ]- >>> math.ceil(4.01)7 v* n5 L# O! k6 v
- 5
* j0 Y" y$ `! B8 J - >>> math.ceil(4.99)9 N# `4 R. m* v
- 5
6 p- \) R2 Q) L( s5 N0 ` - >>> math.ceil(-3.99): ~2 D+ J& e, x! c9 ~
- -3
- x6 Y& A; i+ B% K5 a - >>> math.ceil(-3.01)) _( ]; Q+ T0 V
- -3
复制代码
, o) d" T$ h& i% j* B% H; h- xmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 p" k4 \; I( o3 J4 T- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* z; W2 M% ?$ R! g
- floor(x)/ Z0 A% x9 P. [4 g, S5 l8 t0 h
- Return the floor of x as an int.( N! f7 |* t1 n2 {) Z' J, m
- This is the largest integral value <= x.
5 F N3 D: H$ }1 ? - >>> math.floor(4.1), E5 E6 l, m- y9 W% v7 p* ?0 F. a
- 4
( W, ]0 F) O* Q% T2 M8 Z - >>> math.floor(4.999)" w& M8 G1 f8 G2 _9 t
- 4
, O# M. X, _9 i - >>> math.floor(-4.999)
# x8 ?0 d$ q! _5 ^2 H5 i - -50 V$ E6 u6 P3 r
- >>> math.floor(-4.01)
7 L0 L3 S2 m8 I+ t7 l - -5
复制代码 * X, s1 J. F! q) N- v
math.pow(x,y) 返回x的y次方,即x**y
) x% C5 h7 N: P% i& E a3 I- #返回x的y次方,即x**y5 c# U6 D& M' }+ m# L* M
- pow(x, y)' O2 M; p7 X( K9 D. V3 {
- Return x**y (x to the power of y).
9 t' f" E- j% k" `) B - >>> math.pow(3,4)
2 F U" E. G; T7 Q" A4 i. K - 81.0
2 o* d- _8 U' v0 K5 u( T. `- u - >>> 3 Z( w1 h# W- {2 c) |- q6 E
- >>> math.pow(2,7)/ j! S" ?( b5 f$ a- A
- 128.0
复制代码
1 R6 N: L# ]! g* E1 s1 Hmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ X. t7 a8 h- c) D- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! H& b) e5 x0 A. c9 _ - log(x[, base])
/ Z1 ^6 U/ m( ?- c/ Y: Z - Return the logarithm of x to the given base.) [/ g0 K2 H; ]$ M |: ]* l) b
- If the base not specified, returns the natural logarithm (base e) of x.0 V% z4 M8 e# m- H, W @
- >>> math.log(10)
e+ O- A" c. a9 b* e - 2.3025850929940466 u' b. |& x9 a+ z" k0 b
- >>> math.log(11), n" b8 L/ B1 I7 p/ {9 ^( C* v
- 2.39789527279837073 J1 N* w4 n# p: Q: M& Y
- >>> math.log(20)
+ k, I0 j3 R% A7 ]( l7 \" s" c B - 2.995732273553991
复制代码 + i+ y) A+ _) s, R: {, k
math.sin(x) 求x(x为弧度)的正弦值
W6 z+ l" W [1 P5 f- #求x(x为弧度)的正弦值
) G+ f+ e, q- ~6 F- Q, c. L - sin(x)
) @& d5 v1 j. F! V - Return the sine of x (measured in radians).6 b6 ^# e+ n2 r$ N- A
- >>> math.sin(math.pi/4)( M4 B7 y: h6 Y7 ]
- 0.7071067811865475
) ]# F! ^- N1 `, Z( k - >>> math.sin(math.pi/2)
/ r' K& d3 @0 ?" u7 X \7 h - 1.0" z. c7 _$ k$ `# L
- >>> math.sin(math.pi/3)& s1 }* N) H) x0 b, @, H
- 0.8660254037844386
复制代码 ) U2 ~5 \/ r( ~+ X8 D) D
math.cos(x) 求x的余弦,x必须是弧度) m7 D |) y: b: q/ I; v
- #求x的余弦,x必须是弧度
4 o' t- p# ^( S, M6 R* _ - cos(x)
3 B2 B& i1 \1 w: g; u# @ - Return the cosine of x (measured in radians).2 {' J) M& g& _( a* M! [
- #math.pi/4表示弧度,转换成角度为45度9 I+ r8 a H3 v/ C4 L+ Y. f/ F
- >>> math.cos(math.pi/4)
1 ?+ b# E0 Z7 ?" I$ F - 0.70710678118654768 n, N3 w' z: f4 k+ p
- math.pi/3表示弧度,转换成角度为60度
5 E9 P% O5 E8 h1 T# Z; z( J5 Y - >>> math.cos(math.pi/3)* ^0 A3 b" J% k# J- M3 f0 x- g
- 0.50000000000000015 N. H1 |1 \ f+ D" a9 R
- math.pi/6表示弧度,转换成角度为30度# Q0 a; D$ D3 m6 H" t
- >>> math.cos(math.pi/6) w$ l A4 |) L9 C3 v$ h3 }( y
- 0.8660254037844387
复制代码 ! K5 ]2 h/ u% J5 i" L5 G
math.tan(x) 返回x(x为弧度)的正切值% O; k% t4 R# K8 z% h5 u' x! o
- #返回x(x为弧度)的正切值
3 A) w) b2 Q) N - tan(x)
7 n" o; v! g) V - Return the tangent of x (measured in radians).
+ S$ t) C) _5 O - >>> math.tan(math.pi/4)4 c( N: a" |0 N8 V8 x
- 0.9999999999999999
! F# W2 D- L8 C0 I& T$ ` - >>> math.tan(math.pi/6)& o' d9 v, e# ]* Z2 C7 b
- 0.5773502691896257" N3 Z7 m7 u6 w F& Q; J9 j% p
- >>> math.tan(math.pi/3)
, ~2 S; G$ g8 M* ]" N - 1.7320508075688767
复制代码 3 O! A4 P2 ?6 E5 H* E
math.degrees(x) 把x从弧度转换成角度/ e; j( D' L$ Z$ r" m7 q/ Q& [
- #把x从弧度转换成角度) m" K% x. i3 ^; N& A3 W
- degrees(x)
3 k1 U0 j6 b" @: y' t v( I - Convert angle x from radians to degrees.8 T8 P. ~' s( b
) J5 e3 K" p" ~2 E- >>> math.degrees(math.pi/4)
, i5 ?- E6 X: Y3 @. ~! u - 45.0; H( {6 E' i: E, Q* N; Z' G. g
- >>> math.degrees(math.pi)
0 c: y$ ?& g$ N - 180.0
* \9 A* K* W( x f/ ~ - >>> math.degrees(math.pi/6)
0 d6 a, c e3 P/ H+ t - 29.999999999999996; o$ {) |, Y, Y+ U
- >>> math.degrees(math.pi/3)4 G3 J: S. E. H3 K0 L
- 59.99999999999999
复制代码 - u6 S/ m* K. [7 R
math.radians(x) 把角度x转换成弧度
% [' w D9 s9 [7 @& Z2 m5 \4 _- #把角度x转换成弧度
& b" L6 T# M6 Y3 m, s; c& O) e6 n5 X - radians(x)
& h, {, x$ J& D - Convert angle x from degrees to radians.6 X5 N# p7 c/ V ]& C1 J
- >>> math.radians(45)" Z# A& n9 Y; _0 E4 \ S. R, c
- 0.7853981633974483
% J* T' }2 x" k5 X' | - >>> math.radians(60)) r+ x5 `) ?3 R9 U: n7 M! i
- 1.0471975511965976
复制代码 - H7 ^* H6 ]# Q$ f7 L; \
math.copysign(x,y) 把y的正负号加到x前面,可以使用0+ ^2 W1 Z% w2 U* z* J; m! g3 ]
- #把y的正负号加到x前面,可以使用0
" G% Y+ M0 _* j - copysign(x, y)) Z1 N- o# J* e- D5 r7 }5 w" h
- Return a float with the magnitude (absolute value) of x but the sign
1 b- J: o3 ]' r. |2 S' U, w1 p( i - of y. On platforms that support signed zeros, copysign(1.0, -0.0) # c: w' a9 n% J5 ~0 ?! u4 C
- returns -1.0.5 m9 W- X/ @! S
" I8 r) u4 a. O6 d& P4 C: B; v- >>> math.copysign(2,3)
6 i) w# l' e& a2 z) s; p4 Z - 2.0 _9 a R! F) N
- >>> math.copysign(2,-3)0 G+ r: |2 I7 H+ R
- -2.0
3 l: {: [; K6 a5 X5 {9 }! p - >>> math.copysign(3,8)
$ U$ n p* m/ M7 z3 ?6 x - 3.0
1 ]6 e2 [8 k9 S/ G7 n5 X - >>> math.copysign(3,-8)& @' X8 p8 w0 V0 Z7 }2 k
- -3.0
复制代码
" }7 B& \- e9 dmath.exp(x) 返回math.e,也就是2.71828的x次方8 u( ~' {- r. _+ T y3 w7 x( W5 y
- #返回math.e,也就是2.71828的x次方
% T2 S! ?( [, f9 o& Q9 _) X - exp(x)5 O3 J/ I( v5 L$ I z% |; B
- Return e raised to the power of x.9 c% W r& \- N9 i0 X
- : z0 o! k+ r+ P" C& L! C
- >>> math.exp(1)
' Q, a: @! ]* T7 @8 M8 c - 2.718281828459045# n! _" r% t% y/ [7 [& ]
- >>> math.exp(2)
! M- V9 z& [6 _* h - 7.38905609893065
+ E$ S8 X, P; N; O$ Q& d6 M4 u - >>> math.exp(3): n2 i$ N5 Z( R
- 20.085536923187668
复制代码 / D* B0 {" |- G6 x: w
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
; ]5 _& E/ f! m; ]$ x5 `4 k8 e- #返回math.e的x(其值为2.71828)次方的值减1
+ E2 U6 y8 @) J+ B# d9 \ - expm1(x)4 t# ]" m7 r$ i# |9 C3 g
- Return exp(x)-1.4 p/ G, L) |1 R9 v0 ~
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
% t* D5 p" V; O1 O
# `8 [1 P' I. m7 O- >>> math.expm1(1)
! Z* h# B" \( ]' F - 1.7182818284590455 Y! u( C: t4 e# |
- >>> math.expm1(2)
+ o# E6 D' x9 F7 o7 W: c - 6.38905609893065, \- p! V' i2 Z- E3 a
- >>> math.expm1(3)
3 S3 o2 ^! b ] - 19.085536923187668
复制代码 0 Z1 V7 _! ]5 U' j4 Y
math.fabs(x) 返回x的绝对值
; u6 e1 i9 }' |" F; Y$ e- #返回x的绝对值
2 X3 T8 [5 A5 H J" v/ X - fabs(x)2 v1 c0 H8 S5 A
- Return the absolute value of the float x.: j7 a) r& A6 }' F
- & i/ M: B8 C( h1 I" M" c
- >>> math.fabs(-0.003)
4 l$ A3 Q5 x3 N% y# E* t - 0.003; d0 [+ `- \1 H% J, e
- >>> math.fabs(-110)' C7 o+ L6 r, B$ [4 l- V1 ~
- 110.0
' j9 ]' Z/ L; ~3 z, `- ~ - >>> math.fabs(100)0 q$ K- r7 {7 k( I1 {/ z
- 100.0
复制代码 $ u7 h, |8 T5 O6 p, I' A
math.factorial(x) 取x的阶乘的值
+ R1 u4 A) `# X: u% D- #取x的阶乘的值! ~3 F& S. |& L0 b( E }
- factorial(x) -> Integral
+ f$ t3 l$ d* d3 N( i7 _+ K - Find x!. Raise a ValueError if x is negative or non-integral.2 o$ A6 f; [) K
- >>> math.factorial(1)
* n! e6 U0 o: i: a& k - 19 Y) W1 w' B3 g1 K% R, {3 T J
- >>> math.factorial(2)" y! E# R1 ^; a( k E' ]' u
- 2/ p3 E+ G$ |* B9 T w( K$ d6 j
- >>> math.factorial(3)
, _9 g4 Z& M* f3 w$ q. Z6 c - 6
, p' |9 y$ ]: @* p0 c4 J2 W) M( i6 z - >>> math.factorial(5)5 o& Z7 [- B& P& F( ?
- 120
. p" J/ Q# H" G1 E - >>> math.factorial(10)# w4 p a; ^9 M8 J- |# Q/ H' ?4 K/ T5 f
- 3628800
复制代码 % @0 h w- A/ @. A S. A: O
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
7 E3 D4 Y ?2 i& O, j- #得到x/y的余数,其值是一个浮点数
) X( A4 `1 O% d6 @/ F" Q - fmod(x, y)3 n6 J$ s0 X1 d" d, @$ ?8 `9 `/ X5 t
- Return fmod(x, y), according to platform C. x % y may differ.
. k: o$ v! j' u$ M - >>> math.fmod(20,3)
) N. Q. ~! G4 y# i& c% u0 \ - 2.0- V7 [3 o- Q+ D3 p/ H
- >>> math.fmod(20,7). _" A3 e, i. t5 |
- 6.0
复制代码
7 m/ f4 A; Z4 N' b3 x- K0 Lmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 }8 _1 s' h5 X1 O- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; a! a0 n7 z, s( y1 @
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
* l9 D: z/ N) k$ p1 c - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1# C% B; I1 @2 [8 v
- frexp(x), x0 A: m3 S9 f/ d/ i# ^
- Return the mantissa and exponent of x, as pair (m, e).
0 J' @- j" X: }0 A - m is a float and e is an int, such that x = m * 2.**e.8 E1 o, n1 U$ m& h' O4 D
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- l$ n, y; M/ d
- >>> math.frexp(10)
' \2 }5 z. j& ]4 j. F - (0.625, 4)$ C1 M, b: M( f# C4 i& i9 ~& @3 f
- >>> math.frexp(75)& t6 m B2 s$ d. T8 S
- (0.5859375, 7)
/ Z1 n+ W. ]2 l' B - >>> math.frexp(-40)
& y, S# a; ~( \; @ - (-0.625, 6)1 W7 P, d: ^* j: q9 u* R, {4 c
- >>> math.frexp(-100): U! W' s7 d' s5 R1 J' j$ \, t
- (-0.78125, 7)
- Q; _2 c( w& n5 \5 O4 ]. z - >>> math.frexp(100)
0 I8 `0 N* M& Y- Y - (0.78125, 7)
复制代码
8 X1 F- |8 {% A' g9 @math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)0 _5 I ]8 W9 P5 T5 q
- #对迭代器里的每个元素进行求和操作; }7 H2 j0 [* m, E+ O
- fsum(iterable)- x7 c. H; W1 d) N
- Return an accurate floating point sum of values in the iterable.9 j8 p; b: o! H9 R8 B. M
- Assumes IEEE-754 floating point arithmetic.
' }& z! U8 o4 i - >>> math.fsum([1,2,3,4])* M3 ^5 H2 A9 M0 z6 j) q8 w0 A( V
- 10.0, W# n, R! _9 r# k4 K
- >>> math.fsum((1,2,3,4))/ R* Y _1 M6 z9 I
- 10.06 T/ {6 x+ Y: h( [ Q) v" Q5 r
- >>> math.fsum((-1,-2,-3,-4))# m0 A' u/ Y7 S7 d; C7 n
- -10.0: `( V' P0 T8 T8 @9 @% l+ Z/ |' V+ ]
- >>> math.fsum([-1,-2,-3,-4])
- ^% c1 D S# s" q! N3 E% ^7 u* } - -10.0
复制代码
/ r* P5 B$ I. m& I- F' }3 kmath.gcd(x,y) 返回x和y的最大公约数
5 U4 L h% d3 T- D7 b0 x! J- #返回x和y的最大公约数
$ l4 L4 }, I, B2 \( o* @" [3 w& J - gcd(x, y) -> int
. C9 V1 P d8 A; e2 W - greatest common divisor of x and y
2 B+ V( s* k! w% ] - >>> math.gcd(8,6)' x% T$ ~: \1 T
- 2% B5 ?, J. Y+ b& _
- >>> math.gcd(40,20)0 u) i0 q0 |0 a# C1 t
- 20
5 T! L/ @- ?1 h5 a - >>> math.gcd(8,12)
# w1 W9 p, O, J# M4 A" L - 4
复制代码
$ {$ H- W$ ~5 ]% t! Lmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False" ^4 L* t' B: J& a- S% q3 H6 Y
- #得到(x**2+y**2),平方的值
; y+ z6 J: I' |4 U. n - hypot(x, y)
" o$ {. }1 n4 t/ L+ P/ z - Return the Euclidean distance, sqrt(x*x + y*y).8 ]: G7 H" j6 s" K6 ?* y! p Z
- >>> math.hypot(3,4). e+ i6 }* I9 ?, }0 G6 }
- 5.0
# b/ j1 x* U4 Y9 C5 l; X - >>> math.hypot(6,8)
4 d4 S3 L! v4 E' i# P - 10.0
复制代码
) C; N$ J7 g+ X( Omath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
: A" u2 r! p' j- #如果x是不是无穷大的数字,则返回True,否则返回False
$ N3 I* g) d* N5 r% M& ^ - isfinite(x) -> bool% _5 x8 V6 o9 l0 y, A+ I
- Return True if x is neither an infinity nor a NaN, and False otherwise.
/ Y8 V& e/ `- U/ C6 n - >>> math.isfinite(100)
8 r! ?- T7 m/ w& d2 s! D - True9 ~1 k) R1 A" k3 @- d4 u
- >>> math.isfinite(0)5 u- O7 @7 c5 A" p$ z8 T
- True
) {' H6 r4 g7 |: ~& h2 L7 ~ - >>> math.isfinite(0.1)
" \: y: R: t! G3 m - True
! U% G% U- O( {% p6 d - >>> math.isfinite("a")5 ^; m% V6 G% g
- >>> math.isfinite(0.0001)$ U4 b0 x3 |# Z
- True
复制代码
, p+ {5 y, p/ w5 ]+ Z& N2 `* umath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
. M- V! s1 o H5 Z7 O7 R3 h$ v- #如果x是正无穷大或负无穷大,则返回True,否则返回False
; c- P0 J2 `* M7 @4 c* ] - isinf(x) -> bool# T9 Q9 E. v1 W$ F. c2 o
- Return True if x is a positive or negative infinity, and False otherwise.) |8 `. {, R9 k
- >>> math.isinf(234): L) r2 Y" U' i2 \
- False
. M R; g' o$ @" |0 q% u2 b - >>> math.isinf(0.1)7 n1 |4 j5 i: v, c) V/ s- C6 A
- False
复制代码
* l' o ]* V% w- Pmath.isnan(x) 如果x不是数字True,否则返回False" D6 y" \5 _9 G
- #如果x不是数字True,否则返回False
& J/ m! ?, ?7 U. w D N - isnan(x) -> bool' Z0 F1 ~7 F6 `: t
- Return True if x is a NaN (not a number), and False otherwise.
, t- {. m6 r* A; G - >>> math.isnan(23)
0 `5 o) C9 d% r9 P- p$ Z - False% O/ C& n& `; B% j" Y3 L& l
- >>> math.isnan(0.01)' L/ W' `8 _+ B w) [
- False
复制代码
' }# \% P% o# K0 _& Q3 _math.ldexp(x,i) 返回x*(2**i)的值
0 n7 d# Y$ d/ y& X0 [8 Z- #返回x*(2**i)的值5 D" ], e+ G. ?$ H. j8 y
- ldexp(x, i)
2 B4 I5 o* J) P3 S - Return x * (2**i).7 b2 ?: ?6 p) U8 d& @
- >>> math.ldexp(5,5)
e4 S2 U0 D; t$ G6 b4 b - 160.0
2 a0 b# ^' l( Y9 j1 r - >>> math.ldexp(3,5)
. m& y4 x# J% f# B# B' S5 ] - 96.0
复制代码 : x% y5 V+ m9 k. E1 w: S
math.log10(x) 返回x的以10为底的对数- ?9 q- P9 b0 }
- #返回x的以10为底的对数
- d I4 A8 D P' W6 B& `: V - log10(x)
9 @ k5 c" ?. r" Q - Return the base 10 logarithm of x.
' H9 @1 c% k1 Q* P" A - >>> math.log10(10)
2 n6 M3 |" L9 x; o ? - 1.02 ? b: }" h$ f- T# K
- >>> math.log10(100)
- `5 N3 k% Z: e' { - 2.0- a: G# G8 ]4 s# t' N' L7 F
- #即10的1.3次方的结果为20# W& Y. V9 l& ^2 t' Y9 f! P
- >>> math.log10(20)- o! @9 U S0 t2 E. f x9 h
- 1.3010299956639813
复制代码
& ?) D, T/ H7 h! m1 ]math.log1p(x) 返回x+1的自然对数(基数为e)的值) a# R; W8 }5 ^
- #返回x+1的自然对数(基数为e)的值! h& V1 `8 D4 U) a
- log1p(x)$ e% i1 k! d5 m
- Return the natural logarithm of 1+x (base e).
6 s# K$ E6 R m - The result is computed in a way which is accurate for x near zero.- P, `, s5 Q: {1 s9 L* K
- >>> math.log(10)
/ h: S3 l. `7 u2 Q; @8 G% U - 2.302585092994046. r% {- d* [% l: S8 _1 c- U& V
- >>> math.log1p(10)4 Z3 }2 x8 }, d
- 2.3978952727983707! K# Q0 \( q" E8 o5 x8 x8 s2 o
- >>> math.log(11)
. a' Q+ M: j9 s - 2.3978952727983707
复制代码
/ _. D1 A9 R, L( X7 \; T8 z: xmath.log2(x) 返回x的基2对数
1 E+ k: Y" H; g$ P- ~- #返回x的基2对数6 H2 C# \8 Z2 h9 F5 k' s: m
- log2(x), K) _6 V n& q; M
- Return the base 2 logarithm of x." ^. o* |9 I5 J* _! n: x
- >>> math.log2(32)
' V1 ^6 f. q' v) m9 S - 5.0! s0 P, q$ R0 g! b) K- N
- >>> math.log2(20); x/ V0 _( a+ r7 j/ L1 S# p3 N
- 4.321928094887363
, `3 d" ^' A! X* x7 S R - >>> math.log2(16)
% ^+ I0 w: B" q9 ? - 4.0
复制代码
+ }( K) ^, C9 I# l7 ?: ^math.modf(x) 返回由x的小数部分和整数部分组成的元组( B8 ]* K' A: s! J' u# t Z, s+ ^
- #返回由x的小数部分和整数部分组成的元组
5 ^; c3 t Y8 h$ u# S; Y - modf(x)
0 _+ E- U* |0 t. m( } - Return the fractional and integer parts of x. Both results carry the sign
7 r, k/ |2 [1 m - of x and are floats. G+ v* W, C/ u6 c$ y
- >>> math.modf(math.pi)- j2 [% I2 P. f4 G, \4 h; W& L
- (0.14159265358979312, 3.0)% n6 i5 K* b1 ^
- >>> math.modf(12.34)
0 G5 g5 ]- l1 P: ` - (0.33999999999999986, 12.0)
复制代码 # m% `& m$ H. A* } K
math.sqrt(x) 求x的平方根
" |% B4 X- p5 ]- #求x的平方根& p2 F9 F6 h- D) e* z S
- sqrt(x)
5 w: |" N7 l" F0 e& W: e - Return the square root of x.9 I- @. D- t% T, M
- >>> math.sqrt(100)
1 W7 {7 R' W( ?1 x+ n; O1 L, j a - 10.0+ q1 m* o# K; G7 g1 P5 v( R: w
- >>> math.sqrt(16)
% |: z1 u' G8 D* z- S9 ]6 J9 w# V+ D& L - 4.0
1 y$ n. n7 `! Y3 X J - >>> math.sqrt(20)
& q s/ a1 `& N8 s+ i/ o+ N - 4.47213595499958
复制代码
" V8 [* J" { q2 l; V# ^math.trunc(x) 返回x的整数部分- #返回x的整数部分
0 D( _5 W# M' v/ Q - trunc(x:Real) -> Integral
; g4 Z* S- v+ F2 a - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
% L% y- Z: F! D+ q4 q( h$ A - >>> math.trunc(6.789)
+ z- S7 X# |! h( W# O$ K. O5 R+ ~ - 6
" T3 E% _8 N6 n" o* R/ R - >>> math.trunc(math.pi)2 l% Z* r% m2 i; ~7 u( H3 m: a; l* A
- 3
& ?: r- {: ]6 G- J! u9 r6 U% [ - >>> math.trunc(2.567)
# {) z! H( s' {5 o+ H# ]# @ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|