马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ e1 b' o, S; ^5 A1 ?* D# O
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, J; V. x6 W2 Q/ f: w$ Y5 r& ]/ h9 ]7 [ f4 F5 k: o5 m8 x
方法1:
6 p. \4 |' o, Y: S# ^4 a" ?- >>> import math* _' K; u8 k- n& ]6 i8 m
- >>> math.sqrt(9)
- \) v2 M/ r! b( k' I - 3.0
复制代码 方法2:
/ F8 N/ L; r2 z/ J- >>> from math import sqrt
+ S, \( }1 Q* r - >>> sqrt(9)4 h. ^8 D2 G# `+ N- O& o* x, Z: W4 T' F
- 3.0
复制代码 " S k+ \6 z: h1 W% [
) c8 A2 a7 R$ _, o
math.e 表示一个常量
3 `& K0 H; X, h' _" z: G- #表示一个常量
8 C8 K3 h7 w4 e0 k - >>> math.e1 A! s9 n& N6 \* R. y4 X+ a
- 2.718281828459045
复制代码
: L, ^" q) f' f5 T$ g' jmath.pi 数字常量,圆周率2 j t; _1 N# W% K& z4 I1 \ Q
- #数字常量,圆周率
- }5 }# j$ {% t: I - >>> print(math.pi)8 Q* _& Y4 x: m2 A
- 3.141592653589793
复制代码
8 j1 x" X! B" q' i/ wmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x0 g/ E4 g0 X1 z5 q' \: t
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x: v* T* k5 b( U. x6 l j/ U4 n
- ceil(x)
" V3 g* j9 d4 H- b - Return the ceiling of x as an int.
6 e( S8 L- c6 k! A& `* F% M# y/ m - This is the smallest integral value >= x.
$ G- E& T; ^2 F# |9 e
7 P- u- t. N9 x& k- >>> math.ceil(4.01)# M* L* A- r8 U' y; X2 d
- 5
# y9 L0 S: M# q4 T - >>> math.ceil(4.99)
6 `# K5 ~! r8 e2 m - 5( u- s; y& L) h6 m
- >>> math.ceil(-3.99)
) N( u: O, A9 E$ | - -3
1 ?- k; y+ \" m' K4 w4 ? - >>> math.ceil(-3.01)
6 {- K _( M: f - -3
复制代码
) K1 L! x/ L: O( D4 Amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ w1 A8 v9 l8 ?- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身! [5 N& s/ r) [3 |& Y8 r/ f* |
- floor(x)
+ n# _/ q* N: l- j/ V* T& G - Return the floor of x as an int.
; K& q; m" S$ R0 m2 z/ h7 L, r - This is the largest integral value <= x.* x( |5 r. k0 Z( P
- >>> math.floor(4.1)
+ u- l) q& ^) F% h - 42 _6 Y5 t* J0 ~+ ]* w+ s
- >>> math.floor(4.999)0 _7 |5 w8 M7 v2 d& ^
- 4
* |( t0 v# X( l5 A& Y+ ]) D - >>> math.floor(-4.999)
0 M0 \: J4 L% h4 X - -5- a) P" H5 D; m5 w: \
- >>> math.floor(-4.01)) F! h6 v! |7 t' A
- -5
复制代码 . [; U0 y. B, f
math.pow(x,y) 返回x的y次方,即x**y
1 ~2 D9 s1 |: n- #返回x的y次方,即x**y& v. ]" m4 D) A% d0 c
- pow(x, y)
4 J6 _6 a- U# Z& |0 v0 K/ [( ?7 Y# u - Return x**y (x to the power of y).$ z5 C# x0 L8 c9 C
- >>> math.pow(3,4)
5 j! }' r0 ^' z% b9 L* C - 81.0 q4 `+ {( }6 ]
- >>>
4 _& \) A- |8 d+ S0 Q, V. i - >>> math.pow(2,7)
) c0 W4 C; W7 M# K6 E; ? - 128.0
复制代码
! T/ Q8 E+ a4 e9 I! |9 N/ D* ?math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- Z$ y5 I% N/ F2 C: h. w
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* \) j8 D, k- m
- log(x[, base])0 [# o! {6 E, c* |
- Return the logarithm of x to the given base.
- q) Y. d: b+ s# n' t* W- g - If the base not specified, returns the natural logarithm (base e) of x.( C8 B) @* [1 Y P- q ^
- >>> math.log(10)
, h5 } `+ n I - 2.302585092994046
& G$ @ \# c, g! P" ] - >>> math.log(11) V8 i& s6 J5 S% }& S( _
- 2.3978952727983707 r3 j/ m* Q8 w# ?- {) t# z
- >>> math.log(20)% r8 t% t+ F6 B; u
- 2.995732273553991
复制代码 ' h% u2 |4 r4 t& [1 o$ r
math.sin(x) 求x(x为弧度)的正弦值( s6 `, ?2 Q9 ]+ m: _
- #求x(x为弧度)的正弦值
$ ~& Q4 c' [" ^3 M& e4 A7 B - sin(x)" W4 K- T/ c: {& U8 _% J2 S! y6 V
- Return the sine of x (measured in radians).; g% F6 m8 K+ A C) Q! z. u4 D
- >>> math.sin(math.pi/4)- k' i- U! |5 [. x
- 0.7071067811865475
+ A% I& L, q: e$ E! G - >>> math.sin(math.pi/2). e& @* C1 H6 z- l( i7 ?& Z
- 1.00 q* ]& {# B# U; S z8 _ t
- >>> math.sin(math.pi/3)9 E a$ e+ R, b* C+ b8 M# X* H
- 0.8660254037844386
复制代码 2 u4 ?0 E, Y& O9 X! |
math.cos(x) 求x的余弦,x必须是弧度 v, ^! Y+ G- v2 D
- #求x的余弦,x必须是弧度; W: c: G: \, M" k8 r% |
- cos(x)
8 S0 d3 k8 S. S {) j - Return the cosine of x (measured in radians).
, O. s# l( R" k - #math.pi/4表示弧度,转换成角度为45度
+ _5 \2 }* t- \! X- e - >>> math.cos(math.pi/4)
7 g# u/ ]% c9 `) v - 0.7071067811865476
+ m4 r3 U0 {7 s+ o1 S( v - math.pi/3表示弧度,转换成角度为60度
2 j& N5 y/ z& c. L' d3 A& \3 H - >>> math.cos(math.pi/3)
! [( J+ p9 K- i - 0.5000000000000001
* |+ z) c0 B1 E' \* Y - math.pi/6表示弧度,转换成角度为30度 I( d/ }/ F6 w/ ?" L
- >>> math.cos(math.pi/6)
' U! Y; i& \ z! M& w Y - 0.8660254037844387
复制代码
( E$ B! h1 h- O* X/ imath.tan(x) 返回x(x为弧度)的正切值% ]9 D- _4 P& J8 o
- #返回x(x为弧度)的正切值
. r8 p; j6 P7 d2 x+ K% I - tan(x)0 f6 ^: L4 S# v3 D
- Return the tangent of x (measured in radians).
% C/ M( U+ t" U) U6 L# U& ^ - >>> math.tan(math.pi/4)
& m9 K* t) {0 W/ k2 \' B - 0.9999999999999999
# k( @( [9 y0 F; G - >>> math.tan(math.pi/6)/ D2 v* ?: U' `; D0 R
- 0.5773502691896257
: Z& m. ?% v5 |9 K - >>> math.tan(math.pi/3)8 ?+ }7 j H8 S* @( F" h" j( I8 Y
- 1.7320508075688767
复制代码 , {5 w$ t {7 g: Q
math.degrees(x) 把x从弧度转换成角度* Z) T- G( N# e L
- #把x从弧度转换成角度
( Z) q- |9 G) m& B/ e - degrees(x)5 v8 Z/ c8 D4 |$ T- U# ^; i9 m" C
- Convert angle x from radians to degrees.
3 d, m) F2 c- p3 K - $ ~$ ~- E/ U5 z/ E q7 D; L' C3 k6 E
- >>> math.degrees(math.pi/4)# o3 F% O7 x; x. Q9 P. D
- 45.0
6 [1 `8 t4 k4 M& u- u9 J' y - >>> math.degrees(math.pi)6 I8 s9 a6 I3 m# ~; N( Z
- 180.0
( r) X: M1 o* R$ T5 g* h - >>> math.degrees(math.pi/6)
' m8 V3 S$ K% I7 t- A- B- @ - 29.999999999999996( u0 z# Z% A, N( E; I+ q
- >>> math.degrees(math.pi/3)2 i- U+ T$ B) r, M0 m6 P( M
- 59.99999999999999
复制代码
4 m; D" d* L' t7 E- k( o/ ?7 imath.radians(x) 把角度x转换成弧度
4 ?2 w! Q+ y& S, J. w- #把角度x转换成弧度
- o- d& U2 ` y8 _7 N* O& ? - radians(x); {0 ~3 o2 `' w* ^/ t0 E" [
- Convert angle x from degrees to radians.
- x7 r+ D+ e" H! C1 H4 W# U1 F8 | - >>> math.radians(45)
8 F6 C7 @! r& L5 s. ` - 0.78539816339744838 M3 q# X# {& \! n
- >>> math.radians(60)6 g4 `# Z" \! _. @
- 1.0471975511965976
复制代码 ; W6 A$ X, s* K% n/ Z& `8 U
math.copysign(x,y) 把y的正负号加到x前面,可以使用0. t* ?6 A8 j3 L- V
- #把y的正负号加到x前面,可以使用0
, c6 V) ?! t; e9 h5 m6 i/ { - copysign(x, y)
! f/ C) C# f2 j( r: a2 y; L - Return a float with the magnitude (absolute value) of x but the sign
/ {# m; Y& ?0 E% g; @+ T - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
$ z$ a q+ D& p& z' m - returns -1.0.
( g z9 c1 ]8 Q- _
) B' H1 z! _2 L' h g5 U- {1 p# d- >>> math.copysign(2,3)
, i9 H7 ~2 N- _+ h8 t1 A1 s% D - 2.0
2 r6 K) | x. ~2 c4 y0 l - >>> math.copysign(2,-3), u' k- G$ p5 t* \/ d- e
- -2.0
( O: s: G6 a" o: _/ Y - >>> math.copysign(3,8)4 T7 P7 w, p- I5 E( i4 B
- 3.04 b% a. ]" t8 s" Z
- >>> math.copysign(3,-8)
( J7 `$ v1 R- e+ Z - -3.0
复制代码 - @) e' h+ {/ y
math.exp(x) 返回math.e,也就是2.71828的x次方2 k3 m+ ^& T9 D6 n
- #返回math.e,也就是2.71828的x次方
) ^" G8 G: l5 W" }$ P/ q( y' D - exp(x)* Z% \% p$ u) {* G0 h* L: y
- Return e raised to the power of x.
) G2 p4 u7 y/ r4 W/ R# h2 X
0 W" Y5 @# ^; W/ d- ^- >>> math.exp(1): ~0 ?( T6 ^, n- _
- 2.718281828459045
" D0 j' T+ Y ]: O( k - >>> math.exp(2)
7 q% Z; z7 M& Q: M0 f4 b9 u - 7.38905609893065
" X, l" P4 v& n- d, D1 ~ - >>> math.exp(3)5 u4 O5 R e, u; p) H0 }
- 20.085536923187668
复制代码 ( T5 \" w3 v7 I. j4 o. S! D2 O
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1# I8 M, b; U& t2 M! ?% O/ p1 T0 J
- #返回math.e的x(其值为2.71828)次方的值减1
; {! S( g$ s6 x9 } - expm1(x)
3 l6 \; L" M1 c, r# P - Return exp(x)-1./ g! V: ]- H0 ]/ Q! F+ `
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 p6 ^% i @* `. E/ ]2 r - : {) o( j+ F$ B% ~
- >>> math.expm1(1)
& g% x5 J+ r$ {; g, | - 1.718281828459045% Q/ M1 n0 M8 \% e
- >>> math.expm1(2)
- g1 _ Z# S3 r- g7 m1 U9 A7 Q - 6.38905609893065
1 o/ ?: B! b3 Y) T - >>> math.expm1(3)9 ~& {. _: V0 K
- 19.085536923187668
复制代码 3 z- y! ^) y8 a6 g Z: V
math.fabs(x) 返回x的绝对值4 M5 D9 S, H* H' K0 I& u
- #返回x的绝对值, R/ E+ `- a( t. r
- fabs(x)6 E9 {' e+ `% C' c; O
- Return the absolute value of the float x.
8 `$ J" f' v& b, M z# K0 |' t
l+ h# p( E+ Q0 @3 _( s- >>> math.fabs(-0.003)
2 r/ Y( o$ u% t8 S3 D2 f5 E# l - 0.003
2 M u* E+ M! A" x+ v; t. |- N' e - >>> math.fabs(-110)
( P' ~8 M4 K+ V8 V - 110.0 y! v1 {4 E0 s8 z
- >>> math.fabs(100)
. `- E; h. T. ~# t - 100.0
复制代码 # F2 {7 h3 j' F4 ]1 w# u, x
math.factorial(x) 取x的阶乘的值
; }! R I$ S% I6 e6 @8 N9 U- #取x的阶乘的值8 |9 y1 _, ^2 T. m$ D+ o. U
- factorial(x) -> Integral4 X) g: v7 P5 L
- Find x!. Raise a ValueError if x is negative or non-integral.! R* ~; B0 k0 M' N- T9 q; t8 k7 Q7 T2 W
- >>> math.factorial(1)" ]* o) B4 }! K2 R3 c9 `3 M2 {( {7 t
- 12 V9 D- j5 O' n% w: m
- >>> math.factorial(2)$ K# x/ h/ u6 d8 U- T$ s( [
- 2
* l2 w5 X+ v7 u9 w% ^ - >>> math.factorial(3)8 T7 Y. b5 }2 i, j7 P* S: P
- 6! F- w4 ?# W+ Y0 X5 g/ ?
- >>> math.factorial(5), n; |8 f3 c5 y) [! x9 _
- 120- E$ i, [. L; x0 e( |1 g
- >>> math.factorial(10)9 j1 N b( A' W8 s8 F2 S! @' ^
- 3628800
复制代码 1 x: d& k- y. q- p' D1 c- o
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数' S7 u7 y5 |% w+ D, g) u2 j
- #得到x/y的余数,其值是一个浮点数$ J) R) G# u* L3 M
- fmod(x, y)
: d" w$ l6 s$ H3 `; ` T; [8 y - Return fmod(x, y), according to platform C. x % y may differ.
& z k* R+ l. h1 [) T - >>> math.fmod(20,3)
8 A$ i: o+ L+ e! m. u - 2.0
1 T S4 a) B% h: j+ |* ? - >>> math.fmod(20,7)
& ~8 `" d; S# }7 I. \ - 6.0
复制代码 + S3 w! U" E! X* C m0 t
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
. c; g; I8 h l' D. A: L- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
" r, l6 x& x" b$ k - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
/ F. w& a5 k% p0 x - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ d; M5 Z7 d3 e" }$ P" S5 _0 A - frexp(x)
& P6 u+ _7 X0 g6 v0 `0 z - Return the mantissa and exponent of x, as pair (m, e).! t) |2 G1 I: {4 L* c2 s; m
- m is a float and e is an int, such that x = m * 2.**e.
) x/ g: _' a9 l! k, ^ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.2 u8 [( D$ N# z8 Q
- >>> math.frexp(10)! b9 @% C% Y! n* R0 v% j
- (0.625, 4), q0 D/ t: K7 v: R- T
- >>> math.frexp(75)
( i2 h2 B5 o* H) p$ A - (0.5859375, 7)
7 h1 ~# K! a% n2 o7 T& V& ^ - >>> math.frexp(-40)4 S* i) T/ {2 R: H9 J0 v0 l
- (-0.625, 6)4 m! B/ @" J4 l+ k0 S2 [
- >>> math.frexp(-100)
, ~5 G/ Y5 S5 h$ Z1 I - (-0.78125, 7). t" |# L" C. I6 V3 j4 z5 h, S0 Z
- >>> math.frexp(100)
# k- h; X. F: a. C; q6 r3 z4 H - (0.78125, 7)
复制代码 ; v" ?8 i% p+ L) b8 W% t" i" U0 Z
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列). E4 j: g% o: k! g1 D
- #对迭代器里的每个元素进行求和操作( o8 m' d/ G, f
- fsum(iterable)
, K& k* t) ?& h9 j/ P - Return an accurate floating point sum of values in the iterable.
: N& k* L8 ?6 j% M4 k6 i - Assumes IEEE-754 floating point arithmetic.1 E5 j8 U2 i5 T5 l8 v
- >>> math.fsum([1,2,3,4])
6 ?; ?( b0 `* Q: V2 b, k - 10.0" g) a9 I6 |3 ^: a5 X6 W( \1 L v
- >>> math.fsum((1,2,3,4))
4 h* O i. P7 ^, \" E6 F' L1 e - 10.0) _& }& B, @8 t* y6 r* B
- >>> math.fsum((-1,-2,-3,-4))
8 B1 T5 F9 ?/ k2 L - -10.0
& ^& O7 ]1 x0 L# G* v- q, H; h- Z - >>> math.fsum([-1,-2,-3,-4])( Z9 \" c- r+ I, k5 u
- -10.0
复制代码 ' w9 S) g- g2 o) y: ]
math.gcd(x,y) 返回x和y的最大公约数
) v2 Q, v; G8 t1 d) |8 {- #返回x和y的最大公约数
9 q+ E- u. r' ?; G/ u/ ]& y - gcd(x, y) -> int$ ~8 k$ `$ N4 U8 ~0 _; M
- greatest common divisor of x and y
4 e1 p- g, q' s: u, h' V - >>> math.gcd(8,6)$ Y: T" Z |6 b. V U
- 2
+ P- x/ a! G) n6 O" }6 M8 D0 N4 K$ o - >>> math.gcd(40,20)* @5 |' e0 ?8 o
- 204 h* x% d: R. t1 U8 X# c( r
- >>> math.gcd(8,12)+ C4 H2 M/ |& J# T5 k
- 4
复制代码 , J; |" W0 N. {/ s
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False; _" e" R6 x6 I7 B# Y6 U# q
- #得到(x**2+y**2),平方的值
; t' s8 g9 ~0 x - hypot(x, y)
/ z y2 g0 a4 X. f - Return the Euclidean distance, sqrt(x*x + y*y)./ Q, ?) R# o- w& |
- >>> math.hypot(3,4)( M3 f/ K3 `/ z5 O/ V
- 5.0
+ @/ G' Q7 l- z7 E - >>> math.hypot(6,8)4 N* M( [8 _' f; z8 y3 y* W2 |
- 10.0
复制代码
% {" X0 h' e Umath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False2 r5 G9 d+ {* i, {) D
- #如果x是不是无穷大的数字,则返回True,否则返回False3 B) N: a; q" Q6 g+ M: a
- isfinite(x) -> bool: f3 p# C$ T7 z) c' ~7 J' I
- Return True if x is neither an infinity nor a NaN, and False otherwise.
e+ K4 M& W! I - >>> math.isfinite(100)
# A' |/ n8 @( g; V6 ?4 `6 _ - True
$ M: C r9 g& b9 b - >>> math.isfinite(0)
( _' I/ h4 H! s6 H0 ^4 V2 m0 N7 q - True
0 c+ K% w t7 U( d - >>> math.isfinite(0.1)
3 z2 v6 j- {3 X. \" w+ K5 f - True9 _7 p' Q# D( F2 q& P0 n2 Z7 P- ^! e
- >>> math.isfinite("a")" I5 {- i* y8 q0 ]0 P" b
- >>> math.isfinite(0.0001)
2 w( L/ m- T" G* v - True
复制代码
6 A; a) \; }7 Z- i0 l- `& x; i" f, W2 Emath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! ]" L) F/ _$ [! d x2 B- #如果x是正无穷大或负无穷大,则返回True,否则返回False
8 f0 n w+ ]1 E. K/ W( f# E# n - isinf(x) -> bool! i( y. J' \% l( p: T1 w9 ]
- Return True if x is a positive or negative infinity, and False otherwise.
2 q$ ?. |+ i. v/ y- M6 Y - >>> math.isinf(234)% u9 j- u' B- D6 z2 n9 _4 w3 ]7 Y
- False
( }0 G0 d7 n1 D$ ]+ ~. ?$ b9 { - >>> math.isinf(0.1)
( z! x. h* T2 i0 E - False
复制代码
* L7 ?( v* K" M, {; Smath.isnan(x) 如果x不是数字True,否则返回False+ m0 F: `$ M8 o/ h5 @' S* p
- #如果x不是数字True,否则返回False
" w- j- s+ R; S - isnan(x) -> bool
' u8 }$ x" q% Z! Q - Return True if x is a NaN (not a number), and False otherwise.0 E7 w. ?; o% S$ z) V
- >>> math.isnan(23)& R i1 E/ J' H
- False z. _1 S: Z+ v! ^
- >>> math.isnan(0.01)
' V& j' A: s$ o - False
复制代码 5 u3 ~9 M$ h$ b- ?
math.ldexp(x,i) 返回x*(2**i)的值
+ M6 M ^1 x* Y% q9 l) l9 V- #返回x*(2**i)的值 t9 {/ S# l* k u
- ldexp(x, i)
1 w1 q0 N0 q! j3 y - Return x * (2**i).
5 B* l! I: p6 m r& S- J - >>> math.ldexp(5,5)
2 P x7 _( B* ]; m7 k: h* u - 160.0
5 ?5 @# U/ f M0 Q - >>> math.ldexp(3,5), l$ O8 w7 f3 v
- 96.0
复制代码
$ _3 o C. [4 L }8 }+ i" y. L2 z/ Xmath.log10(x) 返回x的以10为底的对数
; b! j/ E1 K1 b* q- #返回x的以10为底的对数: `9 }8 o8 Z. J; y0 {* W3 T! [
- log10(x)
x2 t; m' F+ Y5 n- H# L - Return the base 10 logarithm of x.2 I! k8 K; B3 p6 J7 ]5 P
- >>> math.log10(10)
5 t1 U4 _+ |( }! n4 X+ L - 1.0
( ^7 J! P4 E/ ] - >>> math.log10(100)
9 N' `3 h( A) ]6 B* w- D - 2.0. \4 I2 c7 `. g
- #即10的1.3次方的结果为204 q8 [ n* S+ V$ ?: f' M" _* Q
- >>> math.log10(20)
% v: v7 b9 L2 _# K8 c9 Z6 { - 1.3010299956639813
复制代码
* F+ p) y2 [; V$ Nmath.log1p(x) 返回x+1的自然对数(基数为e)的值; P6 p! A8 j5 u' c4 h3 D x* }
- #返回x+1的自然对数(基数为e)的值8 W( j6 P% W7 s; [6 ]
- log1p(x)
. _8 E# l" P- i; K3 A. X - Return the natural logarithm of 1+x (base e).9 r( C7 m# ^) b& i; u" s8 `
- The result is computed in a way which is accurate for x near zero.' x4 x+ N* _4 e- E0 y
- >>> math.log(10)
. c# n6 v9 m' U - 2.302585092994046
+ W7 S) m7 @+ E8 R - >>> math.log1p(10)
. @9 {) [ k: l7 g. `: ]$ }# a - 2.3978952727983707+ @- t+ l' h' C# v Y8 V: h
- >>> math.log(11)3 o( b8 J9 y8 R7 Z C$ v
- 2.3978952727983707
复制代码
6 ~2 q6 u2 r" l2 J$ j+ ? W; C nmath.log2(x) 返回x的基2对数. }3 k1 e/ ^; S
- #返回x的基2对数& e; G, O0 R" ^. ^( E
- log2(x)9 t9 q, K0 ]6 _$ x
- Return the base 2 logarithm of x.& l+ E+ c9 b, G" D8 I' T6 {
- >>> math.log2(32)
: q; @# S1 R- ]# R - 5.03 I4 B" s# {1 A# M/ o; j
- >>> math.log2(20)
' q6 i5 _+ I1 o4 q, j8 f& P5 I. p - 4.321928094887363" ^" q# O* J( s4 @' Q# L( l
- >>> math.log2(16)7 I9 e% ^5 m- a$ w1 A8 b* y
- 4.0
复制代码 / A& e3 z1 U ~5 j; W& S7 _, M
math.modf(x) 返回由x的小数部分和整数部分组成的元组7 J- O1 B+ u- l3 h7 K# u% \( ]3 R
- #返回由x的小数部分和整数部分组成的元组' D8 Z1 X1 E) _ A& u7 k
- modf(x)
V! F6 A8 t: H! |) |8 j - Return the fractional and integer parts of x. Both results carry the sign
6 t% O+ ^. _1 z# j - of x and are floats.
7 u( |6 D. f' b1 R - >>> math.modf(math.pi)
" I1 U6 Q6 V6 i% F7 I - (0.14159265358979312, 3.0)
" g" D4 F* i. [% d - >>> math.modf(12.34)' z+ v! i. Q- y* b2 d' Y
- (0.33999999999999986, 12.0)
复制代码
8 O4 n7 D7 z1 s* i( d3 v0 |9 Rmath.sqrt(x) 求x的平方根; t6 v* C" S0 `% Y5 S4 P
- #求x的平方根2 G/ I: e# V/ O' q* P8 ]
- sqrt(x)! r. V5 ^/ Q1 N- N8 i J
- Return the square root of x.
$ P6 k6 Y: u% t) r* {9 |9 e - >>> math.sqrt(100)
* o; r+ m8 Y, N" q7 @: f$ Z - 10.0
' M1 u% ~4 G% D; Z+ e8 V; N& b8 p - >>> math.sqrt(16)
. I$ c0 }- ` A- c - 4.05 y0 J; I( H/ D! D: `# `
- >>> math.sqrt(20)6 W; _) C4 M5 ]% E0 v _ z6 c( U
- 4.47213595499958
复制代码 ' W% S. l7 q# o# m$ |5 t
math.trunc(x) 返回x的整数部分- #返回x的整数部分2 q+ N3 @$ y s$ E) x
- trunc(x:Real) -> Integral6 e! n/ @/ y' p# U5 u# A
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
. N4 o( n* D7 X, p5 O; J - >>> math.trunc(6.789)( q/ N8 q% ]. Z! e8 c
- 6+ Y) h" O! t8 `
- >>> math.trunc(math.pi)9 o/ y9 m3 R6 x" F4 K
- 3/ c3 l( e1 T; c0 w3 R
- >>> math.trunc(2.567)
( K- e0 b8 l" A% h) @7 I6 @ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|