|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
( t" J+ Z0 z7 H% v: Z. G2 @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
2 A- y2 C; b1 E
$ W/ }. ?" p# x8 L: x& z, W+ L( ]方法1:
! m# Q4 t# E2 l; |( l- >>> import math# Z- G) `# @8 I! C$ U* ^
- >>> math.sqrt(9)
2 Y6 z2 q4 P9 e6 J - 3.0
复制代码 方法2:" L: m( d' v$ w6 W
- >>> from math import sqrt
/ k0 F5 |& |) c \# X& o - >>> sqrt(9)
1 p3 v$ u2 H& C - 3.0
复制代码 ! D% F% N0 a! W+ N; h) V9 V! Y
' L8 K5 T, L% T# ]4 }2 L. K
math.e 表示一个常量
+ i" b M, a( `6 G' n+ j- #表示一个常量7 A6 K, E+ Y* p% ~5 @6 P ?( p
- >>> math.e. g* n7 k# Z4 U: o, J6 U
- 2.718281828459045
复制代码 1 M& X7 ]1 k/ m
math.pi 数字常量,圆周率, [7 ~; @& e8 H d/ E0 L3 ^
- #数字常量,圆周率1 X$ X& P' V9 \ O9 ?
- >>> print(math.pi)1 z5 U6 y$ p2 ]0 O. T' S
- 3.141592653589793
复制代码 5 J/ u7 r1 A$ V9 F8 V
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 L5 K, }' G9 V* s- #取大于等于x的最小的整数值,如果x是一个整数,则返回x% c& F( h$ p3 Z: ^- Z+ C8 N
- ceil(x)% t' @5 }' K: x2 ^- w
- Return the ceiling of x as an int.
$ n" E% Z" U4 @ - This is the smallest integral value >= x.
6 w: I5 T; N( K: L& Q( H
# f5 @9 A, `0 Y! F% h, a4 _8 P- >>> math.ceil(4.01)3 ^' q! [+ ?. F; ?3 A. r, D: {. }$ h
- 5
5 b, o) f+ M6 K - >>> math.ceil(4.99), [8 _' x8 j( Y" v( r- [8 {, f0 h4 ~- }
- 5! @& Y, J2 V: V8 v- ^. q7 ?/ `! r
- >>> math.ceil(-3.99): u8 g k8 ]" I7 z/ i
- -3
5 N( r% y! t+ t8 f/ ] L - >>> math.ceil(-3.01)
- X5 T4 n7 \# ^ - -3
复制代码 + u) B+ @- |; y6 K5 N: x
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 D+ |- G! d! a% q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) f. u: }' p$ P- o
- floor(x): ]' S2 T% k: X: o1 h
- Return the floor of x as an int.
6 J$ N% `6 a2 A - This is the largest integral value <= x.2 Q g- |/ X) D' ]* C% B8 s
- >>> math.floor(4.1)2 g2 B& c" M- x. K: P+ @5 F# }5 {
- 44 Y2 s6 r K) g9 J& P- r
- >>> math.floor(4.999) M( @ v" f( A" M" Q
- 4
8 o1 E/ \5 I7 H - >>> math.floor(-4.999)
" p! c+ u* }8 p, r; r/ U, e - -5! v! \, x+ \& v
- >>> math.floor(-4.01)
4 \5 `/ `1 g5 \1 Z( V/ V+ o) c2 i - -5
复制代码 . a5 s& q' L. `$ H( n# x
math.pow(x,y) 返回x的y次方,即x**y
6 D8 A" }1 R$ j7 o0 r6 V2 \- #返回x的y次方,即x**y% l) {6 m! p: o- p }' \
- pow(x, y)
$ L" B7 Z: B- [ - Return x**y (x to the power of y).
* b6 b, F5 u" ^( l/ o! m/ ` - >>> math.pow(3,4)
3 S' `( Z& \/ r& t+ i( w w - 81.01 M! p# ~( _/ T% }- q) }$ b) `
- >>> 3 [# V2 k7 e1 ^$ }+ A
- >>> math.pow(2,7)
: C$ [" m/ X, B- b( T0 ~, h - 128.0
复制代码 1 _ N% o4 ?3 D5 f4 f
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 Z. u Y1 D; o1 Q4 A# o
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), G1 I2 C% n/ t) \$ M! R: z
- log(x[, base])
+ W0 f' d8 s6 B' v* F: L! _ - Return the logarithm of x to the given base.
2 t! T _+ N8 B$ [" z - If the base not specified, returns the natural logarithm (base e) of x.$ h! y* c* u$ j; X8 |7 T
- >>> math.log(10)9 Z B$ l' H& d2 n6 y
- 2.3025850929940464 N# b- U! q' s/ H* u& H) Y
- >>> math.log(11)
- ^& x5 _0 l" ~( V. D6 B - 2.39789527279837070 I4 ^, b6 O' n$ s' u
- >>> math.log(20)
( }, Z8 `# j9 k. q. ]! C - 2.995732273553991
复制代码 - s2 z( H3 b- F) s" [
math.sin(x) 求x(x为弧度)的正弦值
8 a: p2 q9 I/ h% |' R- #求x(x为弧度)的正弦值
9 c; y' D) `0 G. `: g3 r - sin(x)7 o. u0 Y: ]2 F j1 k# k0 m, H: m
- Return the sine of x (measured in radians).$ j4 _' m: s: `2 Z& V6 A* k
- >>> math.sin(math.pi/4)3 s+ {- ~$ l4 o6 _2 E( ^+ M
- 0.7071067811865475! a" L4 R# W7 N1 A* b Q8 g g
- >>> math.sin(math.pi/2)3 c- d& n3 R( Y( s: I7 M6 ~. G P
- 1.0
# J" B: J5 b7 W0 _# R/ g- Z( n - >>> math.sin(math.pi/3)
1 g' A" T3 Y- ?0 I. j - 0.8660254037844386
复制代码
" X/ A1 B" w/ ymath.cos(x) 求x的余弦,x必须是弧度
& ]% I7 W6 _6 }% n- #求x的余弦,x必须是弧度) o: w( N/ T% S; g( F4 v: u
- cos(x), E% Q. E) C: j$ W1 v* g5 G
- Return the cosine of x (measured in radians).
( c- j* a$ W9 {. L - #math.pi/4表示弧度,转换成角度为45度
. g* O3 z ~% k4 M$ K - >>> math.cos(math.pi/4)
2 k6 Y: m4 |+ s$ w) @, e/ P - 0.70710678118654767 ?* U5 ], D. m# R4 X
- math.pi/3表示弧度,转换成角度为60度# F) x; f6 \! J" j; n6 q9 O' X& L
- >>> math.cos(math.pi/3)! K$ E' ~, p, }& z1 R0 g4 H2 e& ~
- 0.5000000000000001
3 M# {6 k) ]6 h& H w6 y3 T. h* D - math.pi/6表示弧度,转换成角度为30度# C7 M! @4 u3 A
- >>> math.cos(math.pi/6)
+ [* W( l P) k - 0.8660254037844387
复制代码 # y. r& n- e1 v8 D; N2 {
math.tan(x) 返回x(x为弧度)的正切值; `, i7 O% p6 i' K% h' E/ y
- #返回x(x为弧度)的正切值
2 P( S: e2 k) L4 Y' Y - tan(x)/ a7 Y/ l% `6 M4 P- n
- Return the tangent of x (measured in radians).+ Z/ @* q I/ P! d
- >>> math.tan(math.pi/4)" |. |9 N$ P# X( c4 o" |9 W& C$ i
- 0.9999999999999999$ n; k- b, B3 L# m4 j! U) S- q6 }
- >>> math.tan(math.pi/6)
2 E- R; ~3 k' t% G - 0.5773502691896257
W% i4 Q; H7 I2 Y; b. q( j5 [ - >>> math.tan(math.pi/3)
8 p1 |4 B7 c% O1 c# V' c6 O - 1.7320508075688767
复制代码
/ K+ d9 n& G$ o. w0 d# I- d% xmath.degrees(x) 把x从弧度转换成角度8 E0 r' b" B) F5 I6 u
- #把x从弧度转换成角度5 S$ Y: T& I' ?* K
- degrees(x)1 f% P5 a- s. O- l+ A, S" _
- Convert angle x from radians to degrees.0 s( x8 i0 t% J' M# v5 u8 }5 X
- ; B( |; H0 l3 f. i( G8 E! `
- >>> math.degrees(math.pi/4)
; ~* W! a8 s0 ?7 c' {! m - 45.08 N7 `/ z+ s1 _$ m) E" t) t
- >>> math.degrees(math.pi)4 Y _8 D, I" ~
- 180.0
2 z$ F# r% ~6 Q/ I3 Z - >>> math.degrees(math.pi/6)
* E1 m; C1 E$ h, [7 n- | - 29.999999999999996
$ L' R- w6 y1 O$ w' Y5 H8 N: U - >>> math.degrees(math.pi/3) S. N* }9 v U- z: Y8 n- f
- 59.99999999999999
复制代码
5 f" p) c, n1 H9 Q7 E8 bmath.radians(x) 把角度x转换成弧度
0 N- o4 H& D7 f( G- #把角度x转换成弧度
- S; b8 J1 u' U - radians(x)6 d2 z+ x) @1 {% p
- Convert angle x from degrees to radians.
& g: E( f7 I. l4 U& N - >>> math.radians(45)
# M g5 S( I9 O, Z4 ^3 W/ b - 0.7853981633974483) A# t" x% R1 ?4 d9 l7 c, ]: x
- >>> math.radians(60)
- T' k5 d) v0 L. L: s - 1.0471975511965976
复制代码
8 w! o4 [3 M% e4 \* `- |, T5 {8 ^math.copysign(x,y) 把y的正负号加到x前面,可以使用0. y! T8 \3 x7 |& t, H4 C
- #把y的正负号加到x前面,可以使用0
0 c& K% ~! R& Z; S( r - copysign(x, y)
% D+ b% Y$ E6 O2 l I6 g - Return a float with the magnitude (absolute value) of x but the sign + l. ]- ]: h7 r* h
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
3 f6 i" Z! y/ m* p8 E; {% H - returns -1.0. K# ]- k9 b( I6 c. S
- ; w4 Z! K5 x. m. n' s! ~* k
- >>> math.copysign(2,3)1 L# q2 S1 A D* t; K; `+ \
- 2.0
7 }; M8 x/ q) H4 F4 V9 z9 ~- q- P - >>> math.copysign(2,-3)
/ ` M$ g, C' }% _- G! E* y" ` - -2.0
3 ]: T9 {. a# E; h3 [8 `, A - >>> math.copysign(3,8)
( j7 {: c+ L" v8 f - 3.0
# i: Y! _1 _7 ?, c5 u& }3 u - >>> math.copysign(3,-8)
" C1 e$ M# o" h( p4 `0 `8 c+ i - -3.0
复制代码 4 P& b" S$ g. f1 o. l
math.exp(x) 返回math.e,也就是2.71828的x次方& f0 |9 \6 ^( w, A
- #返回math.e,也就是2.71828的x次方
6 U% w( y4 I! V6 n+ u3 o, [ - exp(x)$ V1 a' x# E$ Q
- Return e raised to the power of x.4 ` v) g9 V) j
2 d. b, B% [% d/ R1 _- >>> math.exp(1)9 R7 p7 A/ j# x% E. j
- 2.718281828459045& D7 x9 l- h) P* D2 k/ D& A
- >>> math.exp(2)! m) x' R5 O; B- E2 G, w, {! n1 i/ h
- 7.38905609893065
6 m- B+ |2 [0 |7 ~ - >>> math.exp(3)5 V; Z: T0 q, e- {5 \
- 20.085536923187668
复制代码 , ]( k% a3 Y5 g$ Q0 n) O5 v
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1# b! Q' p/ h" d' O& L( ^) R: E
- #返回math.e的x(其值为2.71828)次方的值减1( F4 O$ Z+ z7 q4 m* @# F8 p
- expm1(x)
9 U1 H& L# v! B" u. k4 ~0 F* o - Return exp(x)-1.
" Q) I: _- Y! f. C - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.2 O; D9 w% U, ?5 W O
- i) q1 W4 q+ |- >>> math.expm1(1)
* x% Z+ k5 m- B. j8 [4 o3 ? - 1.718281828459045" T$ V# Z9 l) @ B
- >>> math.expm1(2)
; I2 n1 A _1 R3 z - 6.38905609893065
/ t) L2 j0 g% l- |; a! W: ] - >>> math.expm1(3)
' ?) y9 G' `' o1 p& Z - 19.085536923187668
复制代码
! p- j0 w1 n" T' {( _6 Rmath.fabs(x) 返回x的绝对值
1 n/ E9 r9 E7 X: A: P+ I- #返回x的绝对值
- j+ M5 V5 h& z! q" L, } - fabs(x)$ S( d( G7 X ]* ~
- Return the absolute value of the float x.* b! \, |7 ~9 {% z! U* ~
- 7 Q. Z2 ]/ ~* r7 g
- >>> math.fabs(-0.003)( H6 T# O! o7 o! i! M9 J7 R
- 0.0036 `% n/ |4 o6 ^4 U! X
- >>> math.fabs(-110); r2 R P8 ~$ P: @; r+ L( W
- 110.0
& p4 [3 u t. C - >>> math.fabs(100); g, U( b* x N/ a1 d3 M6 P
- 100.0
复制代码
- V& K/ G! ^. o+ t7 H4 Zmath.factorial(x) 取x的阶乘的值
: E7 O- z: W) h4 B( U! V- #取x的阶乘的值
# q! P% m: m$ ^; L7 } - factorial(x) -> Integral. A9 o& `9 o( {" d2 d, g
- Find x!. Raise a ValueError if x is negative or non-integral.
, d" z% Q" r9 J6 k - >>> math.factorial(1)8 [8 u+ `/ I# k
- 1
0 W9 E; v, ]4 l9 \/ I P8 _ - >>> math.factorial(2)5 q; c: F$ v* G" B
- 2
7 @% S& s9 _% B. I: \* ? - >>> math.factorial(3)7 s8 h, G: g U+ h4 f
- 6! F/ Y+ w3 m2 q* V2 C6 b: \- y& \; {
- >>> math.factorial(5)
- k- A6 Z2 K. m$ T5 B - 120) a `+ `) O5 i6 I8 h
- >>> math.factorial(10)
6 v5 S" x* N' ?0 q& k+ z3 Q1 i - 3628800
复制代码 1 C3 z3 Z. v) c4 q+ L
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数- ~- q! f; H! |$ B& R
- #得到x/y的余数,其值是一个浮点数& O& s/ P; e1 |/ R! g/ \0 o
- fmod(x, y)
( o5 {- S2 a3 T: E7 b& _% \7 k& j - Return fmod(x, y), according to platform C. x % y may differ.& @/ p E$ {2 o
- >>> math.fmod(20,3); s0 P# `0 ?, H
- 2.0
& E$ I1 v, j9 A - >>> math.fmod(20,7)
9 P0 L }+ n. N - 6.0
复制代码
/ j; y, p( O5 `: U) Emath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 d6 r9 I! v( ]5 S7 T% Y5 W! ~
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 a" T( f$ s4 R
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值% ?, a5 x1 ]/ s5 V; E N
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1+ _# Z' s0 d) A7 ~' {/ U
- frexp(x), @2 t, Z6 `) u; b4 T
- Return the mantissa and exponent of x, as pair (m, e)., b6 }1 q* R- A" U1 ^3 @2 T
- m is a float and e is an int, such that x = m * 2.**e.
( s/ r7 f9 }! z" d" b9 y: y - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
- r; b" m X( m' @ - >>> math.frexp(10)
1 o% k( w, f8 D0 @ - (0.625, 4)# q# l! [! M( u, F& x
- >>> math.frexp(75)
7 }5 W: `( B$ s; a# k - (0.5859375, 7)
* f& ]( D9 p( O - >>> math.frexp(-40)
. V) H# d2 |- r2 } A' F/ Z - (-0.625, 6); i* z3 J) v7 j' \* L
- >>> math.frexp(-100)7 G. z4 f1 W' n1 ?9 k, Q
- (-0.78125, 7)
- E: k6 L* V/ ~8 ?! a2 q8 a - >>> math.frexp(100)8 L3 z9 j8 j7 J5 ~
- (0.78125, 7)
复制代码 1 _; I8 H, _: ~0 u7 X; s5 j
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
4 _) f* ?1 e& M9 |& z! d8 _+ d! m- #对迭代器里的每个元素进行求和操作
. Z+ ? }) r! A( {. m* c6 {* X - fsum(iterable). n3 f; L. m w! A# f0 }( m0 C
- Return an accurate floating point sum of values in the iterable.
/ g5 C) h! [! w& W - Assumes IEEE-754 floating point arithmetic.
' i& X- {: V: e' s( ? [7 y$ j - >>> math.fsum([1,2,3,4])
% q5 g' A7 t5 Q( B% F5 S - 10.0. K* W. }+ s3 N( m1 ?7 [
- >>> math.fsum((1,2,3,4))( N. ^8 V: {8 t+ U9 Q- s
- 10.0, o* s% b* j- N
- >>> math.fsum((-1,-2,-3,-4))
$ @6 b& T# `8 ^7 u$ _( } - -10.0# O6 j+ n; }6 I/ f
- >>> math.fsum([-1,-2,-3,-4])* E" M0 L* q! h3 \" f' Q
- -10.0
复制代码
4 T) T$ e k! V# z& B$ Rmath.gcd(x,y) 返回x和y的最大公约数
+ G! B$ U: E0 L5 D- #返回x和y的最大公约数
5 N' C$ k; I, D4 f7 O) s - gcd(x, y) -> int
% V l; w/ @% a - greatest common divisor of x and y
0 d6 O! i i1 M& F# c- Z& l. Z; ~ - >>> math.gcd(8,6)
( N: p: |; w% M# s - 2
3 `/ a! s3 j) j* o - >>> math.gcd(40,20)9 L6 I9 m, F( ~( P2 i3 h
- 206 w3 U. f5 t2 [4 ~2 u
- >>> math.gcd(8,12)
0 H1 ~9 S* W. x, _, F. l - 4
复制代码
' A( t, A7 _9 I" kmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
: Z+ G0 w! I" c1 M# S. ~- #得到(x**2+y**2),平方的值2 {5 P9 E' h8 T7 Q+ L
- hypot(x, y)
3 [: B% L. c6 I, h: g" J7 U - Return the Euclidean distance, sqrt(x*x + y*y).
0 L; H! I9 U0 i" r" A - >>> math.hypot(3,4): S. q5 ^$ V: A) p1 C
- 5.0
9 U/ `" f, h% Y; d - >>> math.hypot(6,8)+ P; M7 w/ t, I* [! S4 Z
- 10.0
复制代码
( F2 B0 j, a( P1 H: m% y' @/ o& ymath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False; _: x6 o; g# B) d* {" l! @
- #如果x是不是无穷大的数字,则返回True,否则返回False
: v6 C0 p6 f1 K" e1 z2 G: S - isfinite(x) -> bool" {2 [, G& B* x
- Return True if x is neither an infinity nor a NaN, and False otherwise.
3 A- l( p% r/ p, I' M9 J - >>> math.isfinite(100)& \0 e" G8 \2 n( u) ~# z
- True1 W0 d* N( D0 m$ H/ J
- >>> math.isfinite(0)* t$ Z/ z z; }9 t
- True
# v. e' [3 e# o% s/ O4 w - >>> math.isfinite(0.1)9 \+ e0 x0 E5 b3 }# x- s
- True/ H! l7 Y8 V- J* M7 _* K4 G4 ^
- >>> math.isfinite("a")
) g* ]! T5 }( ]; O - >>> math.isfinite(0.0001)+ m+ l' l- L% H5 J
- True
复制代码
* s. H8 h. W/ @, Umath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ F; `+ j' A1 B- #如果x是正无穷大或负无穷大,则返回True,否则返回False7 A: H: N4 }( r# ?* o) u8 Q
- isinf(x) -> bool+ V, L. v4 R9 j) F$ U! b3 T
- Return True if x is a positive or negative infinity, and False otherwise.
3 n2 A; _* S* C9 U' e1 v8 i* H - >>> math.isinf(234)
6 @; ?$ Z1 |5 ] - False
# n) V: }" l$ z - >>> math.isinf(0.1)* L' y$ u8 u( Z2 L% A
- False
复制代码
T% y3 s/ t; d% A/ C/ smath.isnan(x) 如果x不是数字True,否则返回False
5 K' f/ t, }: W2 {' \- #如果x不是数字True,否则返回False6 {# ?, E" C7 g6 T* p% _
- isnan(x) -> bool
3 M9 J# L8 z8 q - Return True if x is a NaN (not a number), and False otherwise.1 Y8 C0 @ N8 _3 |$ p! }
- >>> math.isnan(23); u6 F; m0 w* J0 Y
- False0 V8 m6 W% B8 d- D
- >>> math.isnan(0.01): Z' F; A" ~1 C% H1 A4 J
- False
复制代码 7 U, U* G; X( L1 b; ]$ B3 s/ J5 z. w
math.ldexp(x,i) 返回x*(2**i)的值6 |* {& e; ]8 y- ^2 k
- #返回x*(2**i)的值2 V; Z+ J$ `5 s) d# h
- ldexp(x, i)2 Y6 P; ~6 @( R! L/ ?& D) z N: c) ]/ o
- Return x * (2**i).
U- |1 O$ Q1 [/ o - >>> math.ldexp(5,5)
/ `3 L$ r* P% y; Z - 160.0* b2 O) C4 g# ]
- >>> math.ldexp(3,5)
' b q0 Z* F2 [9 F( E/ C( c. y - 96.0
复制代码
j ?3 V" B! n2 l% C0 M* omath.log10(x) 返回x的以10为底的对数
9 Z; g; w/ L4 o6 r# w! Y- #返回x的以10为底的对数- W7 ], Y. F8 J' o- `
- log10(x)
5 e: b" h& M! C' Z: I, p - Return the base 10 logarithm of x.! x8 f9 a: Q- D
- >>> math.log10(10)
8 ~* y9 d9 m% x% W( ^ - 1.0 M& l5 ?( s: R% a4 m
- >>> math.log10(100)
6 ^3 M" s/ d2 l0 ^* a5 Y$ S7 Y - 2.07 k0 ^6 y0 I+ m/ w0 s5 E0 a+ ]+ }
- #即10的1.3次方的结果为20
$ s2 l E" {+ A; {1 o: ~; F) Q - >>> math.log10(20)
7 K. b; F- f! ?3 w: H4 H1 o' _ - 1.3010299956639813
复制代码 / {5 a( u q. P: q6 T
math.log1p(x) 返回x+1的自然对数(基数为e)的值3 i8 _& P, P m+ X# g4 s
- #返回x+1的自然对数(基数为e)的值
5 `; Q5 t2 z# J+ s& `; g# L: r) \ - log1p(x)/ d- X3 R* l8 q+ @$ n- `4 A
- Return the natural logarithm of 1+x (base e).
' B, X' _! R" ?8 r2 @0 A - The result is computed in a way which is accurate for x near zero.4 v- G2 Y/ ]- {( T7 s
- >>> math.log(10)4 U- ~ y% J/ l+ t
- 2.302585092994046' Z. d# P3 o) p2 a' F
- >>> math.log1p(10)! ~" }* d$ B3 C3 e7 U3 V& j
- 2.3978952727983707
$ i! A8 Q3 B: w- e - >>> math.log(11)0 \, e7 q( T: Y# ]
- 2.3978952727983707
复制代码 0 c0 z8 f u! A1 W+ |, U. z% a3 u+ U
math.log2(x) 返回x的基2对数0 d* d9 _! }- Q" t
- #返回x的基2对数
6 C' m% T# Y! S2 C- f2 _5 | - log2(x)7 o$ o; a, d' R- L5 U& Q
- Return the base 2 logarithm of x.
1 S& z5 O- B! L0 w* W5 O - >>> math.log2(32), `% q% d$ }5 H# P f( l' P- Q, Q
- 5.0
% e- o: ]; c# c! ? - >>> math.log2(20); k- o1 M F: t1 {7 O! u
- 4.321928094887363/ H/ I8 g. q) w
- >>> math.log2(16); j0 S M! x+ ]; F7 s+ w
- 4.0
复制代码
0 m) e" o2 M3 ?9 O, V, Fmath.modf(x) 返回由x的小数部分和整数部分组成的元组
4 f* s; a7 h: x5 ?3 l- #返回由x的小数部分和整数部分组成的元组
$ a6 V* H% v5 y0 Z5 S - modf(x)
! G% H$ z. B' d/ v4 P - Return the fractional and integer parts of x. Both results carry the sign: r2 P- ?* {8 m# C2 |4 @
- of x and are floats.
; z7 C" s7 K' Z( h! U; n5 ] - >>> math.modf(math.pi)8 V- N O1 x) i$ W7 L }( D& W! ^) X
- (0.14159265358979312, 3.0)% I1 Q2 i( s% J% B0 g
- >>> math.modf(12.34)
4 ?( O! g6 C- I( y2 }) \ - (0.33999999999999986, 12.0)
复制代码 6 B4 Y0 q$ B' ?- y2 D; ^9 G( J
math.sqrt(x) 求x的平方根+ q3 Y+ C5 c0 r5 G
- #求x的平方根& y' O9 P3 K9 h6 ]. m7 G4 U
- sqrt(x)
$ {1 G3 ]: g" r. c - Return the square root of x." \8 p7 X1 [+ a3 {+ Q, Y$ Y
- >>> math.sqrt(100), O* z- p5 S$ l1 i) L
- 10.0
; N" g4 U: a- s - >>> math.sqrt(16)
4 \5 W- s9 {! A9 b- T' E - 4.0
. c6 J* s, U( x+ d. x9 ] - >>> math.sqrt(20)' c1 }4 i. k$ I3 j% N
- 4.47213595499958
复制代码
/ x& V9 U' |6 D, x# emath.trunc(x) 返回x的整数部分- #返回x的整数部分# n8 i3 d2 f9 |
- trunc(x:Real) -> Integral5 `. d0 N1 E* M, U( c3 T
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' T! a ?8 o& a7 V
- >>> math.trunc(6.789)
* Y+ A* _3 ~5 K7 e! z" g - 6
5 d) R! s- h3 W A0 k- ] - >>> math.trunc(math.pi)
3 t% w# C5 m% a( L - 3+ j' I0 R0 N: k0 N
- >>> math.trunc(2.567)
% i$ D6 L, ]- z, r; C c# r - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|