马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
x5 w1 m7 ~' O! Y" U【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
4 [8 S) @2 ^0 p. K+ L
( i! S& [/ A) ^9 y" @' H3 o9 X. g方法1:
4 l& [6 r Q. u) x- c- >>> import math
0 B/ G7 Q( A! n7 T - >>> math.sqrt(9)
; `! d. Z E& E- X; ^' ?' a9 f - 3.0
复制代码 方法2:' X; a0 Y4 r. U( [' K( d( k9 v3 w, S
- >>> from math import sqrt! F. d! k2 t5 J- G; N- W0 S
- >>> sqrt(9)
1 K: i% R Y, v - 3.0
复制代码 ) ~ f4 t3 [8 r5 n s
2 x* _$ I8 s* B. u) ~# ^# T0 Zmath.e 表示一个常量: g7 D- Z5 h0 \0 o9 J* N* d
- #表示一个常量6 D1 i$ ]+ _9 j9 o8 \$ t
- >>> math.e/ d( X1 O5 |+ _3 ]( U
- 2.718281828459045
复制代码 o+ \* T5 e# z, L' l+ A* C
math.pi 数字常量,圆周率
, Z* | N) U. g- #数字常量,圆周率
# Y/ X" n! c* B/ S+ d4 T' X: l - >>> print(math.pi)
0 O* r+ X8 k5 t* u5 o - 3.141592653589793
复制代码 : t3 e. e Z3 |" F a2 B& f$ [
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
% X$ E8 A; F2 w% `! u- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, b0 k V9 W1 i# z4 d0 l( r2 I
- ceil(x)$ X3 c. l8 f' i# A' p* [
- Return the ceiling of x as an int.
$ l8 ^ o2 D% _& t - This is the smallest integral value >= x.
# {+ G% g* m+ W1 ~ - 4 `' _. A" r" |# Z4 A: [
- >>> math.ceil(4.01)
6 f- l* p o7 f; w J* P5 c" |9 n - 5: s# X- x# n$ j
- >>> math.ceil(4.99)
4 G2 {8 ]8 {* a% |0 \5 l - 5, C/ T# h" e+ {, v/ i1 z
- >>> math.ceil(-3.99) A' c$ R& P' _6 @- Z/ ~2 V. m
- -3
( V; h$ g% x- i - >>> math.ceil(-3.01)9 ~* ^% \! A' t/ f. Q- t
- -3
复制代码
5 f( t9 @0 j2 P, dmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ N: P) x2 N6 e, ]
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% M/ [. y- `3 A7 m! I( k6 Z - floor(x)
1 P8 @& G* I. H - Return the floor of x as an int.
8 X* ?, T0 ~7 a0 A6 ? - This is the largest integral value <= x.
) q- a2 r( p/ v- O4 k8 ~% t- h( u - >>> math.floor(4.1)
, e' y! }) t& O5 k+ e - 4
4 Q" q2 W! O+ @: w g+ a - >>> math.floor(4.999)
& J( f Z: b! c7 E# \- v0 z - 4 C/ O9 ?# [! O
- >>> math.floor(-4.999), X7 ]0 U+ { @
- -5' p1 w8 c' j( Q
- >>> math.floor(-4.01)) |! n! ~; a9 m6 k' V2 i7 s
- -5
复制代码 4 z3 x& i7 v7 ?# E( P
math.pow(x,y) 返回x的y次方,即x**y) x" H$ j! c4 p: [8 Z" B. g1 l
- #返回x的y次方,即x**y
' i! O0 b& a0 N. H8 l - pow(x, y)5 S0 ~3 e* N1 j/ }
- Return x**y (x to the power of y).0 x/ b. w! N8 m. H$ o
- >>> math.pow(3,4)
z) @& i; ^! r' l7 ]( {: t - 81.0
4 C1 ? U/ x T1 n' r* I - >>> ) X+ z7 e. L' @' x
- >>> math.pow(2,7)' I% y }2 B$ i/ s' u% f7 b
- 128.0
复制代码
3 Q. X3 d$ V" F: B% ]6 g! Nmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: e& Y/ ~" ?8 }# ~/ G7 K- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! V3 Q# N y) e! Q. H) d
- log(x[, base]): a- \9 M# A1 M
- Return the logarithm of x to the given base.
: s7 f$ [+ D" C2 `* k% Q+ }* C( e" Q. J) n - If the base not specified, returns the natural logarithm (base e) of x.
3 R. F* y. a/ m) m" w! G - >>> math.log(10): f) W4 o) C0 z" i. `7 X
- 2.302585092994046
7 Q3 d6 k. B/ K! S - >>> math.log(11), R/ y4 R$ p# C- S
- 2.3978952727983707- {, R/ L! r, \
- >>> math.log(20)
0 ^, O* [% W8 a9 G - 2.995732273553991
复制代码
1 e8 i7 ^( U* q' ]math.sin(x) 求x(x为弧度)的正弦值
+ I/ a9 z& A; Y, d1 v- v; ?- #求x(x为弧度)的正弦值
8 A" p: g7 V" W, D8 s; B, r - sin(x)
' c; D/ _3 y" F5 g. T$ R+ x6 p4 D - Return the sine of x (measured in radians).( w; D4 K/ l9 S2 a& @2 M( ~
- >>> math.sin(math.pi/4)
* L5 }+ u1 a: O5 ~, z: \% O8 L - 0.7071067811865475
* A' d8 Y% }3 d& Z0 a% q2 { - >>> math.sin(math.pi/2)
k: m7 T) l0 z* R7 Y - 1.0
}+ I7 \0 ~9 o& A - >>> math.sin(math.pi/3)* ~0 W x! I7 e! [/ {
- 0.8660254037844386
复制代码
' E4 A5 M: c) vmath.cos(x) 求x的余弦,x必须是弧度1 E" b. t4 Q/ T: @
- #求x的余弦,x必须是弧度
7 l: p) }$ T3 D4 g6 x# @ - cos(x)* N, g$ K' W1 l0 r+ T5 ]# d
- Return the cosine of x (measured in radians).
6 E3 Q/ B/ C1 Y# h5 i - #math.pi/4表示弧度,转换成角度为45度' I: O/ h- Y, Y
- >>> math.cos(math.pi/4)% ^7 h' y+ {8 Q# h% l" T
- 0.7071067811865476
! F" W6 \9 o& m: @) P; H: n$ F& C - math.pi/3表示弧度,转换成角度为60度+ k7 ~7 y6 n# b' c
- >>> math.cos(math.pi/3)" _4 r8 d( ~9 x
- 0.5000000000000001$ w% X4 V, T8 t3 k% J
- math.pi/6表示弧度,转换成角度为30度4 t c9 A- \$ S- ^/ L
- >>> math.cos(math.pi/6)# c/ J2 ~% c' z5 L! a- a$ W
- 0.8660254037844387
复制代码 3 P" D- H; H [4 N. |. g
math.tan(x) 返回x(x为弧度)的正切值
4 `1 @, @! \4 F8 h' ]- #返回x(x为弧度)的正切值
- M* B" L5 g3 `- [& Q* E; L; F - tan(x)
3 j7 G( n8 `/ b - Return the tangent of x (measured in radians).. c2 @0 m; L" K7 L" `
- >>> math.tan(math.pi/4)
' ~! r$ z/ i) T8 U% n7 |& N: D - 0.9999999999999999
+ @5 r& n' \1 }$ O8 X/ r - >>> math.tan(math.pi/6)& _& M% m; h/ a7 k% w
- 0.5773502691896257& }. `! ?3 G E. M$ E# E( S
- >>> math.tan(math.pi/3)
& e/ q4 b, Q! p4 s( T4 _) y/ ?8 p - 1.7320508075688767
复制代码 0 U% _8 L) D0 Q$ i/ P' }$ p
math.degrees(x) 把x从弧度转换成角度! ~& v! G+ Q2 s6 r5 f; G
- #把x从弧度转换成角度# l* I, N% ~# x5 a- D+ z
- degrees(x)! r/ d# v6 z7 P# J" G p
- Convert angle x from radians to degrees.
! ?) G/ L, G: K- s1 [6 R - 1 o- z3 F% a! [' `2 Y) r
- >>> math.degrees(math.pi/4)/ K1 z* v6 K0 E X
- 45.0
) V8 }5 e7 b. h6 l: ?; L3 a6 | - >>> math.degrees(math.pi)
8 d( @0 u" [; ]5 z' Q" Z: @ - 180.0
" {: c6 w# f6 x" k. e - >>> math.degrees(math.pi/6)6 B6 s& x) `0 k0 ~
- 29.999999999999996- _2 `- W1 v) ~6 [; Q
- >>> math.degrees(math.pi/3)
# v6 R, T% r( B8 a9 } - 59.99999999999999
复制代码
8 k8 ~2 w# d6 jmath.radians(x) 把角度x转换成弧度
" ]( ?7 ^' v4 I' G) X- #把角度x转换成弧度/ s8 W- e- E8 v0 a! P$ i' A6 N
- radians(x)- a, b* n. H* a3 M) |6 B
- Convert angle x from degrees to radians.6 T: ?* M' t! q7 P. M; Z) P4 h4 P
- >>> math.radians(45)
9 m" Y9 @; ^) r! j! P - 0.78539816339744834 R F" S" V9 g) n
- >>> math.radians(60)" g. L! h1 o; H# o, }+ P
- 1.0471975511965976
复制代码 9 ?) s3 {$ Q2 ~+ c- S$ i0 Y
math.copysign(x,y) 把y的正负号加到x前面,可以使用08 ] B) c' H \* @ w7 ~
- #把y的正负号加到x前面,可以使用0
5 Z3 ^0 X0 K" T0 N. ] - copysign(x, y)/ @6 X" I2 |! p! ]
- Return a float with the magnitude (absolute value) of x but the sign + a/ P# s# B- N: M
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ; Y% h- |. J2 l$ l6 B3 B
- returns -1.0.
3 J* J7 Q+ W( f& k- o$ ]
) b, ~/ l3 v. e6 z( Y- >>> math.copysign(2,3)
9 Y K8 L6 r1 _/ i - 2.06 U3 m$ S3 g: K: S6 E( D/ o
- >>> math.copysign(2,-3). f/ v$ x; i& l% Q, W
- -2.0/ v- R/ T; U: I
- >>> math.copysign(3,8)6 ?" x6 o7 _7 E0 ]4 h3 q1 R
- 3.0* n; p! o! e+ W! d, d: b
- >>> math.copysign(3,-8)/ L& m2 I; H* l& Q4 i
- -3.0
复制代码 & [* w/ ~- `2 Q
math.exp(x) 返回math.e,也就是2.71828的x次方" u# {4 c" T) j1 D5 M8 a4 E
- #返回math.e,也就是2.71828的x次方: v8 g7 Y5 @9 B5 d) L n
- exp(x)7 ?0 N$ A! Z- f- q4 @; H
- Return e raised to the power of x.& o: L0 T$ h3 |, R1 \) H
: z/ Y! T8 y- K' o2 g7 ^8 g- >>> math.exp(1)& B1 ^. w# d& {6 q8 P
- 2.718281828459045
* r6 n; j7 S- p5 z2 X& \ - >>> math.exp(2)# J. r4 H9 h/ }
- 7.389056098930650 l# q# c- k+ }6 r5 B" Z
- >>> math.exp(3)
# r0 @7 H" C* F& S& C7 b3 Y9 k" C - 20.085536923187668
复制代码
0 {' K! ?" O: |% t, tmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
% q }: M; D: Y# Q& @2 y. }- #返回math.e的x(其值为2.71828)次方的值减15 c: r" `( N9 t: ?! Z1 E
- expm1(x)
, S1 b3 G) A; ~" l - Return exp(x)-1.
6 |. u% ?* i! Q6 y - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
; D2 i) @: ~8 r' V, q1 L - 9 {3 U0 u* K7 _
- >>> math.expm1(1)
2 s& a8 z% ]8 u# W! r9 Y3 u7 R - 1.718281828459045% t6 R4 j2 l% c9 H$ Q& K" C
- >>> math.expm1(2)
: Y( L! D& ]5 _- O" {% G - 6.38905609893065
, l4 x' k2 y$ ]" {! ?$ a: ^ - >>> math.expm1(3)
% A6 i8 ]- ~5 d - 19.085536923187668
复制代码
H" g. U3 L1 U! @math.fabs(x) 返回x的绝对值
; R9 M% n- m8 K! U$ b& Y4 w- #返回x的绝对值
- F3 c X3 q1 b - fabs(x)3 s t. A3 r5 u; J! J9 Q
- Return the absolute value of the float x.: o7 A1 Y) g+ [0 D) A
- 8 H/ ^. z! J( l
- >>> math.fabs(-0.003)6 O9 ?3 u5 b+ p- r+ W* w, }1 E
- 0.003
+ R3 H+ l' Y8 u3 A6 v& r, g - >>> math.fabs(-110)
9 E t3 g: P$ P, \9 z6 z; c: B - 110.0$ d2 z3 h) d# z
- >>> math.fabs(100)
6 b- b- j$ x6 L; t - 100.0
复制代码
1 o. b& v4 F0 N" h& Omath.factorial(x) 取x的阶乘的值5 I5 I) A! p: C' d( V
- #取x的阶乘的值/ h2 ~2 t" T* z, l
- factorial(x) -> Integral6 S" i6 ?7 I# }* r* ^# d: q: |
- Find x!. Raise a ValueError if x is negative or non-integral.
\3 X; V; @1 z2 H9 d D! l2 ?4 f - >>> math.factorial(1)- p) q8 ?+ W4 ?; a
- 1
* @, ?# u% Z1 ^( w" z0 M. z - >>> math.factorial(2)
5 `0 i' i& X2 r - 2
# D+ H4 A( n+ Q5 z5 j6 W$ _- S - >>> math.factorial(3)
+ A) {/ u! w: f0 t6 K. ^3 ?9 J2 V - 6( ~, d4 {2 X4 q" [/ ~
- >>> math.factorial(5)
% O9 S/ Z. @# q$ |' _ O6 v - 1205 B! i/ g Q5 b2 r% V8 d7 j
- >>> math.factorial(10)
9 Y u: W, x7 u8 O - 3628800
复制代码 ; {5 {6 Q6 o6 V* r' D. o9 ?' c
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
4 Q" P0 o' O% w) {0 x- #得到x/y的余数,其值是一个浮点数7 g- R" V+ [. \
- fmod(x, y)2 g9 z) i. v3 T1 e3 i' w
- Return fmod(x, y), according to platform C. x % y may differ.
( j( o& V' Y3 _ a) D S, E - >>> math.fmod(20,3)
) \1 S! o$ J- N5 j8 Y. @ - 2.0
+ R F5 x: @- c; s3 M, G2 K+ H - >>> math.fmod(20,7)
4 T: c" h/ Q) r; O8 k - 6.0
复制代码 " ~% E+ ^5 x2 C4 n
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 x, L3 t1 }5 p- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
2 q7 Z5 r3 g: P; E( ?- ?" r: e - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值 S2 w# @5 N3 |: K- M8 C7 n# X- a
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 n& q) N& G9 j& G. n+ q - frexp(x)
% H) B9 w* I1 p. E - Return the mantissa and exponent of x, as pair (m, e).! J: q' y4 W0 j* g. J. w- O3 o( h
- m is a float and e is an int, such that x = m * 2.**e.
0 S0 E( s, @) }* ] ` - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" H$ T A( n( H4 E - >>> math.frexp(10)
4 D- r# R& K$ W1 _! |6 D; Z - (0.625, 4). t \$ z! |" w3 d0 E( P
- >>> math.frexp(75)
8 V$ h% A8 Q0 T" l& G8 K- K8 u - (0.5859375, 7)
, t! d5 s( l6 D5 h - >>> math.frexp(-40)( a: j3 |, h3 a: s; p; D- w! p
- (-0.625, 6)
; |. M2 \- l& a' l - >>> math.frexp(-100)
: p- j! z8 t. S6 ]# {5 _9 N - (-0.78125, 7)
& u* g4 |9 ?9 r- i1 @2 L/ H - >>> math.frexp(100)3 [8 e& D1 N% ^' z
- (0.78125, 7)
复制代码 ; n; q* {# ~/ K2 N. ]
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)0 W& F+ c, e7 ^; P
- #对迭代器里的每个元素进行求和操作
$ X f8 p Z( R/ M - fsum(iterable)
* @& W# x. o' J$ X- P - Return an accurate floating point sum of values in the iterable.
3 S( q9 E, J# B( [- E% ` - Assumes IEEE-754 floating point arithmetic.
! ~2 q. @0 f( ^ - >>> math.fsum([1,2,3,4])
2 n( e2 G1 R" N - 10.0* M" }5 W/ z( ]
- >>> math.fsum((1,2,3,4))
; `, O, Y$ c7 _+ z, y2 k. w - 10.0
3 ]5 j) i) W8 r3 C C: o- X' S - >>> math.fsum((-1,-2,-3,-4))# \/ F: p8 L0 M* e8 _- E! B
- -10.0/ }4 v7 ^0 o3 K5 ?) S4 y4 f. X
- >>> math.fsum([-1,-2,-3,-4])& q# J4 m7 X4 U f1 Q9 V% f
- -10.0
复制代码 9 _- ?! d: ~& ?$ G5 S) h
math.gcd(x,y) 返回x和y的最大公约数
9 Z6 P: J: ^8 r. |8 N. f& a- #返回x和y的最大公约数" w3 ?* R% ^- }
- gcd(x, y) -> int ~4 p0 z4 v) u7 _. W# d
- greatest common divisor of x and y( f5 z# ?" ~" t& ^" X& Q9 Q. H2 V
- >>> math.gcd(8,6)
/ ]* G8 P! C: [# v8 }4 D7 E - 29 J, j6 l, _$ C7 P$ W- m& n5 J
- >>> math.gcd(40,20)
& z( f8 e; e6 Y6 S7 Z' t - 208 B z- H* x9 D1 V8 |8 v
- >>> math.gcd(8,12) p: N1 ?& k8 |( H* [
- 4
复制代码
6 c3 ?7 s5 h$ ?9 o1 w: cmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
+ {- g% R. j X: Z- #得到(x**2+y**2),平方的值 p- w$ J, t5 y! l7 A0 v
- hypot(x, y)
% T5 c, q4 v& C" @, `9 K$ m - Return the Euclidean distance, sqrt(x*x + y*y). g( N" a9 g7 n
- >>> math.hypot(3,4)9 t" r% q3 e2 s' |
- 5.0
, m( c5 ^1 Q) e/ Y$ R0 v8 G - >>> math.hypot(6,8)7 G4 p8 u$ {1 r
- 10.0
复制代码 4 ]1 ?# w4 p* b7 q8 v
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False5 w0 U! ?( o8 y; W s
- #如果x是不是无穷大的数字,则返回True,否则返回False
3 h7 i, h; ~3 g0 T - isfinite(x) -> bool
& Q5 k: g3 s1 ]: V0 y - Return True if x is neither an infinity nor a NaN, and False otherwise.$ d- ^" Y. M. n' ^
- >>> math.isfinite(100)
4 j. |, `/ } T$ {- c - True/ m% v6 ^# l9 E0 W6 C8 i$ d E3 z
- >>> math.isfinite(0)
3 b g/ R0 @/ ^" U: W0 R. D - True
5 R$ M4 K t( w' y% \5 e9 X! e/ c - >>> math.isfinite(0.1)
' [% u2 j8 O2 v z% u8 r; X1 i - True5 t2 y5 u3 H {5 ~+ O
- >>> math.isfinite("a")) o# g* _" Q# A9 i
- >>> math.isfinite(0.0001)
4 |& U3 ^! h: U3 ^# Z - True
复制代码 8 q [7 @ \, `
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
7 u3 C3 [$ ~3 k9 f$ [, r- #如果x是正无穷大或负无穷大,则返回True,否则返回False& z) A; a) U" ^7 e4 ?
- isinf(x) -> bool
4 E6 {8 `8 Y+ o - Return True if x is a positive or negative infinity, and False otherwise.
2 D& ~8 V! C% y* r/ d - >>> math.isinf(234)
) i! M C* s8 l7 K0 Q7 [ - False$ o0 P/ M' y: r2 X
- >>> math.isinf(0.1)" q/ [/ I5 n0 p& q" F
- False
复制代码 0 s$ J6 d/ q) b
math.isnan(x) 如果x不是数字True,否则返回False2 X: |; X3 J2 I
- #如果x不是数字True,否则返回False
! \" ~; o6 i4 k& f0 h: V! C - isnan(x) -> bool; k! J" D+ P8 ?6 B6 z
- Return True if x is a NaN (not a number), and False otherwise.$ i! m( J( i+ c$ N
- >>> math.isnan(23)3 W6 m* S% o+ B/ W, [, j
- False
7 u3 C n' j& @ _2 G - >>> math.isnan(0.01)
9 h B( J* l! d - False
复制代码
- M9 o, R" E, N8 w' D4 r V8 Y1 tmath.ldexp(x,i) 返回x*(2**i)的值
# V$ o! P0 [' X; `1 \- #返回x*(2**i)的值5 m. P+ |8 }. \. R! Z+ p' Y
- ldexp(x, i); g. q+ h8 j2 f1 b
- Return x * (2**i).- {$ E, Z. \* w% J' D/ R
- >>> math.ldexp(5,5), Z2 K: z$ H @) l i
- 160.09 v& b- @( j8 v4 T
- >>> math.ldexp(3,5)5 x$ M! U, K4 z
- 96.0
复制代码
. O, A7 h* d6 C2 X2 ?math.log10(x) 返回x的以10为底的对数
; k# X6 W/ G% _) B. S- #返回x的以10为底的对数
" B: J/ F6 \! y" r7 i. w - log10(x)
3 a- n1 ]: H9 V# B3 m. k G' E - Return the base 10 logarithm of x.# r) t2 P5 v; A" U4 J
- >>> math.log10(10)
/ N- F h3 h- p" K - 1.0+ S! E/ z/ ?9 [7 ~. h
- >>> math.log10(100)
; t# d5 |6 L" q, H1 w2 `5 z - 2.04 i) i1 b6 J) E' @/ U0 F
- #即10的1.3次方的结果为20
7 H1 A: i9 m3 q8 m( ~6 Q - >>> math.log10(20)" d5 Q$ k+ m1 R( V
- 1.3010299956639813
复制代码
7 w3 Y: m5 J+ @. b7 F( C9 ~4 A' }math.log1p(x) 返回x+1的自然对数(基数为e)的值0 ^/ N' R/ _; N0 C
- #返回x+1的自然对数(基数为e)的值) m! M( ^( `2 W! Q7 [# g* f& }
- log1p(x)6 ]6 g/ L, [1 z; h' e2 G5 y1 X& A
- Return the natural logarithm of 1+x (base e).
+ Q3 c! q4 m0 V. n, r - The result is computed in a way which is accurate for x near zero.+ j) G5 G @3 l1 J; D+ X) a
- >>> math.log(10)4 `* B6 G0 V' L+ ~/ h% O
- 2.302585092994046, l+ d; {5 z1 e
- >>> math.log1p(10): Y1 I2 U% V) z8 o
- 2.3978952727983707
$ w1 ^$ y( V7 h - >>> math.log(11)- ~& z0 ]5 _0 _, o$ `9 J
- 2.3978952727983707
复制代码 $ O9 r2 P1 @# E2 ?) q
math.log2(x) 返回x的基2对数
0 i# E8 i! ~) E7 i- #返回x的基2对数
, s) S* f4 }1 w$ L - log2(x)
6 P# ^ b8 ]: B) \6 V% m) Y7 M - Return the base 2 logarithm of x.( a" `8 }, Q8 S4 C3 K& R4 N2 a
- >>> math.log2(32)9 D% C2 h& W) F8 {' O
- 5.0
( H8 I- ^9 E) s, @# f1 n. }5 Y/ p - >>> math.log2(20)/ V+ N0 e- U! g' \" \
- 4.321928094887363
7 M+ n! z; ^$ n1 N5 V - >>> math.log2(16)
0 Z) t# p4 n X0 P0 k% r - 4.0
复制代码 " K' ^6 j" I' G0 p9 O& A" \0 a. \3 L
math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ l! n' u9 o: C8 W: O( |- #返回由x的小数部分和整数部分组成的元组2 @+ v$ ]: Y8 W6 N
- modf(x)
( J& [2 u$ c+ F, ^; ~: P6 m9 B - Return the fractional and integer parts of x. Both results carry the sign
% |* E! ^* c `1 ], _ - of x and are floats.
8 N4 {9 i% i2 ~" C - >>> math.modf(math.pi)* k j7 q t9 p5 G) u
- (0.14159265358979312, 3.0), l0 m9 {$ O8 P8 l) f
- >>> math.modf(12.34)! P) P$ @) _( L% c9 q% J
- (0.33999999999999986, 12.0)
复制代码 : d. _ Y( v$ h% W9 A; }' T. T
math.sqrt(x) 求x的平方根
# l- V b: I3 d; v- #求x的平方根3 Z- H7 x. Q: L5 h
- sqrt(x)
% S' W8 \& e1 y8 y' k - Return the square root of x.
" X$ C9 o* R' g; E8 v! X$ [ - >>> math.sqrt(100)0 r/ u7 l [0 G' M. k8 c L" T
- 10.0
1 p" W: _: z2 h; u1 X) M8 L - >>> math.sqrt(16)* n" ~- y# f* A7 D9 c0 V
- 4.0
1 M: D4 o" M5 @4 R* r - >>> math.sqrt(20)
( ^* ^% q8 f$ S7 w+ l; A' h - 4.47213595499958
复制代码 0 Y% f8 n8 \/ z, G* e J+ }$ A
math.trunc(x) 返回x的整数部分- #返回x的整数部分
3 W* H5 K# f* W' c0 \" R2 v - trunc(x:Real) -> Integral9 @9 A, j4 e! ^7 \
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
2 q$ I; _/ \* q/ o7 c - >>> math.trunc(6.789)
1 z+ u8 }+ D, m1 t - 6
" X- w! ]" |9 w, u2 m8 z1 W5 O. X - >>> math.trunc(math.pi)
4 Z2 a" X+ I7 Q - 38 A# G% C4 {* p \5 L
- >>> math.trunc(2.567)# z x& X( T0 w
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|