马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 o% ^9 q2 w: f% o% ?0 _
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ @9 e/ H" U% Z# R$ G6 m4 i# o8 P0 M/ C9 a! B8 v
方法1:
% B- A" g0 x! p4 a- n4 Q+ ~- >>> import math
/ p. l B# u0 _' L- V7 c* f - >>> math.sqrt(9)
4 P* ?, j6 @* M6 J% g! c4 T - 3.0
复制代码 方法2:
! M. g3 x3 Q0 i; D; D+ E- >>> from math import sqrt
1 B [2 V5 l- V8 N9 _2 M+ r - >>> sqrt(9)
. `# R; p$ u# T) f$ y2 T6 ~ - 3.0
复制代码
, X$ L$ F3 s ~0 d
8 ~0 b' u2 V2 V. zmath.e 表示一个常量( w% h5 ? W$ l; w/ F+ X
- #表示一个常量5 O7 n: s& `. ?1 T( A- s" `8 }
- >>> math.e
: X: D0 q0 t3 m* v# o/ X3 i1 K/ Z - 2.718281828459045
复制代码
) n R, I% |% A* N0 d+ U. hmath.pi 数字常量,圆周率
+ y9 _8 D( k7 E/ l6 r- #数字常量,圆周率0 a. T2 W5 {. l3 }
- >>> print(math.pi)' a% C. j" ]0 a9 ]( x) y
- 3.141592653589793
复制代码 2 U2 [8 ^. `1 C
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, G6 Q: J6 Q: ]- l' U$ ~
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 w7 {( A* o. i, U0 A! S6 K' _' r - ceil(x)
# _% J% y2 V) B - Return the ceiling of x as an int.- m- w' ?. z J8 P
- This is the smallest integral value >= x.
2 N( R% w" Z; L9 b! _- }$ j% K# }
& k0 x% i# o9 t5 z- >>> math.ceil(4.01)
% }+ b( T9 Y5 `( S - 5
: u" n5 Z6 }6 G/ d - >>> math.ceil(4.99)$ N1 w' `2 v: Y/ ?
- 5
" e3 o; I n c' D - >>> math.ceil(-3.99)- f# J. P$ o. w. h
- -3- F b0 a0 E8 P: w
- >>> math.ceil(-3.01)
$ F" u8 r; M8 J) B# L& n) ?- G - -3
复制代码 , P( W$ ~; h2 u; [ N$ J
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. j% k/ j! J8 X8 f5 Z' H- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# u% _2 Z. e' Q. Z - floor(x); _) i- X8 n* J% `" q4 P p1 Z
- Return the floor of x as an int.
; C5 C+ {% d) R8 S - This is the largest integral value <= x.- c' l8 S I6 y; E! h7 Y& g
- >>> math.floor(4.1)& Z' r" S ]1 y
- 4
9 \* Q4 k4 H, r+ v- K* _ - >>> math.floor(4.999)
, [4 s9 a7 v! x5 {4 q - 4
2 G/ b; o3 n* t3 h4 v7 s - >>> math.floor(-4.999)2 H0 @; c# L, e! ]
- -50 s5 C) B; s9 }" h0 ~
- >>> math.floor(-4.01); v M8 J1 H" c1 j. i; n+ k
- -5
复制代码
* G: r7 R* {/ X; _math.pow(x,y) 返回x的y次方,即x**y/ ?7 k- ~1 I! ^, f7 k! F) g- k4 f
- #返回x的y次方,即x**y9 X3 B9 f6 }* i2 i& g; p0 l
- pow(x, y)
+ {- N" S/ a* i& [: J! S - Return x**y (x to the power of y).
7 B' i! ]" G) v/ f9 O9 X - >>> math.pow(3,4)/ A/ V( F& f# K9 y7 T" H
- 81.0: g2 K7 P6 G8 h; U( m B
- >>>
) T f, h3 q2 r! m- ` - >>> math.pow(2,7)
! H2 u( a# P) Y) O% H- ? - 128.0
复制代码 2 a' f7 P7 n" G @
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 {* x- j( n$ ]- ?9 Z- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), \7 u+ m/ t4 E
- log(x[, base])
; h! m8 u3 x& [ - Return the logarithm of x to the given base.5 X! ?: [; S3 b0 O4 M' _* w) x
- If the base not specified, returns the natural logarithm (base e) of x." \/ d i: S' b$ L
- >>> math.log(10)! f: i8 T2 n6 O
- 2.302585092994046
4 i. I1 D! e# \ - >>> math.log(11)) {" \4 F& H+ ~& g5 i
- 2.3978952727983707' |/ E3 f# t+ a" }+ ]2 }
- >>> math.log(20)
, }) T& ^1 T& w# W# j - 2.995732273553991
复制代码
# p8 {$ `% x/ i4 b6 D6 r+ x* R: Smath.sin(x) 求x(x为弧度)的正弦值( P% ~" e) a5 v. B% }+ B5 I3 H
- #求x(x为弧度)的正弦值" \! A. i5 P' ]6 G, `$ C
- sin(x), O. g0 L- g1 I- W
- Return the sine of x (measured in radians).
% j" T0 g( i9 f4 q7 N- M+ l6 e - >>> math.sin(math.pi/4)1 q* h7 @0 x6 O; ^# b7 }
- 0.7071067811865475
0 v0 ^; x% G" R - >>> math.sin(math.pi/2)1 i) h: h0 c" ?0 n# ]9 }
- 1.0" | g# S5 a5 M' d8 ?
- >>> math.sin(math.pi/3) n2 m( A4 x4 E, u
- 0.8660254037844386
复制代码
+ y" \/ ?* k K# P' wmath.cos(x) 求x的余弦,x必须是弧度
; C! a$ H% \3 K. {+ l- #求x的余弦,x必须是弧度
- n8 x) f% [0 y: a2 w - cos(x)
5 N' W% ?) d: s; J6 c5 f& q2 s/ O - Return the cosine of x (measured in radians).4 @) ^6 q& ?% \) M4 O
- #math.pi/4表示弧度,转换成角度为45度" K: B+ f: J0 ?" u
- >>> math.cos(math.pi/4)
" G, }; M4 k$ t- z - 0.7071067811865476
0 p, v4 h( h8 ?- z% V/ E - math.pi/3表示弧度,转换成角度为60度
" q6 V: N: K- r - >>> math.cos(math.pi/3)
. ~+ ^4 p5 H! A2 L- |5 Z - 0.5000000000000001
: v6 R6 _- y0 x# a8 C4 ?! O - math.pi/6表示弧度,转换成角度为30度7 M: R3 x/ W/ k
- >>> math.cos(math.pi/6). k* ?& B9 I- Y- \2 b9 |
- 0.8660254037844387
复制代码
& f/ d& V* |( _math.tan(x) 返回x(x为弧度)的正切值
1 O3 Y, a+ p+ Z8 V# F- #返回x(x为弧度)的正切值
4 |# Q, D: G0 ? - tan(x)3 [5 s( }( ?) ?: w+ F; e
- Return the tangent of x (measured in radians).& |, C9 A7 M# F* m- O' K( V
- >>> math.tan(math.pi/4)" \1 y5 f6 d' l+ L, Z; i% O8 ^
- 0.99999999999999991 P" x' Z+ D' v9 n( Y* @. m+ {
- >>> math.tan(math.pi/6), l* M* V' s/ {1 E. W8 l
- 0.5773502691896257. v9 Y( B) k/ b* z: @
- >>> math.tan(math.pi/3)4 M$ ?6 ]3 `5 e" H3 p) b* j
- 1.7320508075688767
复制代码
, {! a( e& v C, K9 e0 j3 _: Vmath.degrees(x) 把x从弧度转换成角度
1 G) s. |1 ~# P1 d+ U- #把x从弧度转换成角度# R$ _# o7 d! D* |2 E0 N, b% o
- degrees(x)# A& s4 w7 f* D; Y" J
- Convert angle x from radians to degrees.
) V) q& @- U. g9 N/ `, H, _# s
+ w% h! F% H" ?- >>> math.degrees(math.pi/4)+ [4 s% s \# Z) p0 d
- 45.0+ k7 u! F# A# @& v5 z* h8 E
- >>> math.degrees(math.pi), i4 A; ?! ]( u) g
- 180.0
, ^' i) D8 R8 O S( \ - >>> math.degrees(math.pi/6)5 C5 c1 [: ~4 j4 L6 N8 M
- 29.999999999999996/ y) |/ D( L( y( q
- >>> math.degrees(math.pi/3)
9 v) o* J" T8 D: Y - 59.99999999999999
复制代码 % v: ~" _. j0 N
math.radians(x) 把角度x转换成弧度, l: L. ~' Z7 x/ l8 @: Q" ~; D
- #把角度x转换成弧度
; l) R9 ~+ [* l' Z" H0 x) Y" [ - radians(x)& G0 |) z& ~" \
- Convert angle x from degrees to radians.+ B* X8 z2 e4 e/ ^' v$ ^; w b
- >>> math.radians(45)( y: S4 @' m8 `
- 0.78539816339744835 V0 c4 B4 c- W' z9 K
- >>> math.radians(60)2 d! z% e" A2 O, R3 Z3 h
- 1.0471975511965976
复制代码 ; Q0 L$ g: E* z4 h: i
math.copysign(x,y) 把y的正负号加到x前面,可以使用0: O5 H, t) {0 T/ w; n! n A
- #把y的正负号加到x前面,可以使用0, [& K# J7 L* b0 h( l6 f
- copysign(x, y)$ m- q! o& f7 u
- Return a float with the magnitude (absolute value) of x but the sign
7 @( O+ A4 o5 L4 w9 e4 F! L - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
/ R' G% j' U! \2 t8 a - returns -1.0., x* R' U6 ?9 |" ^
- 3 c3 |! M( _8 v( H/ B
- >>> math.copysign(2,3)# a# H& |5 G$ B$ X. C
- 2.0% j6 D' z6 J6 r% a+ x! t9 m: K! \
- >>> math.copysign(2,-3): J: E8 ?( S% U) g% U4 G b
- -2.0 v2 y+ p' R) k! b3 ^! z* _
- >>> math.copysign(3,8)
+ Q6 n4 {- i0 m+ D1 @1 A! ~ s - 3.0
! a$ j/ F! s/ n- A) ]: w7 e3 B# d% ?3 c9 B - >>> math.copysign(3,-8)9 q1 M0 v9 Q; @* p* A3 c. ~
- -3.0
复制代码
" G" y; w) E8 J' B1 Kmath.exp(x) 返回math.e,也就是2.71828的x次方9 v. n* O" [/ d) m5 W3 f& m( B' _/ m
- #返回math.e,也就是2.71828的x次方
( n7 Y' z) b2 o* U: Y a* U! B3 V6 m - exp(x)/ }# f4 |' d5 A2 |8 O4 [
- Return e raised to the power of x.7 \' ?3 s' m3 | F
/ V5 D0 k, A7 l8 B, l0 v1 V- >>> math.exp(1)
; l) j4 ?. h: c( T- m - 2.718281828459045) }! i1 Z) P" s; C0 ]/ _
- >>> math.exp(2)0 e5 @5 L) K) F5 P4 v
- 7.38905609893065
3 _, c+ }9 e; F. M/ W9 ^1 D - >>> math.exp(3)" q7 a8 L: f* v
- 20.085536923187668
复制代码
! i! z& g9 M6 d5 q# H6 imath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减11 b# _) K% e0 g( E" L) l7 k( Q
- #返回math.e的x(其值为2.71828)次方的值减12 l F# ^8 A& F7 q0 r
- expm1(x)1 H. v/ X! Z1 H
- Return exp(x)-1.& _2 j8 L* Y9 W' m( F5 a
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
. {* g: `3 t& M& F$ B0 X - . ?1 [; M# S3 w; S7 Q9 a) {+ R6 @
- >>> math.expm1(1)
3 j& e0 h& |1 D2 [0 o; j$ l1 p7 L" Y - 1.7182818284590450 U1 y2 Z. {9 @4 V
- >>> math.expm1(2)
7 i" Q3 c' X' C. C9 E - 6.38905609893065
# `0 x, g. M) O4 n7 D1 g - >>> math.expm1(3)
% i# W4 `2 f* \' B: y- S+ W - 19.085536923187668
复制代码
, v/ s; v) Z6 `math.fabs(x) 返回x的绝对值
$ @. O+ f" ?4 Z! b7 k, r, Y6 c- #返回x的绝对值8 O8 x0 ~+ ~- y9 }6 v- G( E7 ^) Y
- fabs(x)
: I8 D$ N9 C/ V( ?. r( d4 T9 |/ r: m4 M - Return the absolute value of the float x.
- t& ~$ ~/ v/ V8 ~% |: q+ H# d
W. u$ O6 u: e, z7 C. y- >>> math.fabs(-0.003)7 E: @ F# {; ~- R# L* C& r
- 0.003' G9 P9 G- R% y* |1 o
- >>> math.fabs(-110)
2 {0 D" I; q6 i; }& D8 I - 110.0
% l) ~: T9 |$ K+ S1 N - >>> math.fabs(100)# W" I6 _2 a( j
- 100.0
复制代码
3 J. g, c! O# a# A, @math.factorial(x) 取x的阶乘的值8 P: O8 O, ~& x
- #取x的阶乘的值
5 T; u- ]$ h& b - factorial(x) -> Integral6 b0 K# i- d5 ]
- Find x!. Raise a ValueError if x is negative or non-integral.
6 ~0 J% O5 T* E* Q8 f, N - >>> math.factorial(1); G1 p9 C7 S5 u6 f7 @7 E; B
- 17 R3 T& [7 r4 V% M5 U) f+ I+ t# N
- >>> math.factorial(2)4 h O9 A; j9 }$ ^. n
- 2
" e) i& _' a" y - >>> math.factorial(3)
9 |& }5 i( h5 v& O; c - 6
$ a% W9 X: L' o# g1 r% `. l" u - >>> math.factorial(5). s! d I: x! G& V$ n, U4 F
- 120# g, L& d% W5 S( V% V; r* j! E! d
- >>> math.factorial(10)
8 E4 Q2 n( I2 I - 3628800
复制代码 2 V" Q4 y* w! w) E9 H4 J/ t
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
: e/ ^. O; @( K" e( p: u' R, f- #得到x/y的余数,其值是一个浮点数
9 a5 _) R: h: H9 x - fmod(x, y)8 n- ?! @: l* ~- q
- Return fmod(x, y), according to platform C. x % y may differ.
# U3 |/ w' q" N0 X' H - >>> math.fmod(20,3)
+ Q# Z. Z5 A1 b. O$ W, J0 p - 2.05 `1 A5 m( l5 v7 l$ f
- >>> math.fmod(20,7)
$ ^! P% I0 G) B- Z, H: p9 G - 6.0
复制代码 6 P+ y: v: L, c
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围; [! Q! C) s- M6 G$ N- T
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ r6 b6 j+ v3 C
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; @" c9 @5 r9 b6 W& r9 Q, G; [! z
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
1 X' v i2 u" E& d - frexp(x)$ h7 z+ S9 |/ g, f
- Return the mantissa and exponent of x, as pair (m, e).
" K' Y& p9 l; p - m is a float and e is an int, such that x = m * 2.**e.
$ p' J4 L+ m" J, Y# ]; A - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.( @' G3 J, S4 Y( ^
- >>> math.frexp(10)& a+ }& ~, g/ n, k6 N8 M
- (0.625, 4)
# R0 D5 J5 c8 D' q) L3 T - >>> math.frexp(75)
% G) [9 |+ w$ @ l2 M - (0.5859375, 7)! L/ n% ?7 p' f4 V3 u
- >>> math.frexp(-40)
3 q( r, H& b5 F) m0 j - (-0.625, 6)
( h( E$ A; k& L6 E" b - >>> math.frexp(-100)+ ], J" a5 `- L0 `- X8 g
- (-0.78125, 7)
6 |1 A3 [, J' N, A7 r. X - >>> math.frexp(100)
9 N" S4 I, n! U# | - (0.78125, 7)
复制代码 5 }+ w, }& z; J7 F6 i
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)" r0 m2 r) O0 k1 L4 b2 d: N9 x
- #对迭代器里的每个元素进行求和操作8 s# [) H% e% f/ B2 a* M) b5 P
- fsum(iterable)" k% t- j% Y" l# e* N1 `$ O! h
- Return an accurate floating point sum of values in the iterable.0 u. v J9 N l5 ~- \: [2 G7 `* Q
- Assumes IEEE-754 floating point arithmetic.
- D# o( x* ?. W4 {/ s' u - >>> math.fsum([1,2,3,4])( _+ f1 N8 R5 C# ~: i
- 10.0- X; K8 b# Q, O, T
- >>> math.fsum((1,2,3,4)): X' ~# f- C; c- @& | i$ a
- 10.0% Z8 n8 D, m. H# E: R
- >>> math.fsum((-1,-2,-3,-4)). a6 q6 i9 r1 X
- -10.0
0 C% H8 g. l( f5 z9 x' n% U8 q - >>> math.fsum([-1,-2,-3,-4])6 @- } a0 w, d3 z4 A
- -10.0
复制代码 8 f% p# `; k4 p# s) i
math.gcd(x,y) 返回x和y的最大公约数: ]8 c. j/ A6 }4 c- V8 z" r- z- T
- #返回x和y的最大公约数
, H7 ?0 b2 v0 q3 x( \ - gcd(x, y) -> int
6 Y6 K1 f9 K% [( e: t2 i - greatest common divisor of x and y
0 p @( l8 [) X& v6 s" x \2 ?# X - >>> math.gcd(8,6)& J) D' |1 X: B: G$ | ?
- 23 ~) j3 Z7 ~8 e6 |: q2 g' t
- >>> math.gcd(40,20)3 {( O& {% Q8 C+ x" o" ]
- 202 @: T1 w8 u/ s" r) o" b- ~; Z
- >>> math.gcd(8,12)
# b3 \7 h2 `7 Y! r8 b - 4
复制代码
7 I1 n3 Y- @. Q, smath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False( \) s% `6 s. m4 g1 h. y" g6 |
- #得到(x**2+y**2),平方的值5 S5 I+ e* B5 A0 i
- hypot(x, y)8 ?& w: [6 J4 `
- Return the Euclidean distance, sqrt(x*x + y*y).
, G6 Y: B6 R' q$ x8 ?1 u - >>> math.hypot(3,4)' t3 L! X' r2 P2 J
- 5.0
1 C7 d3 i6 U/ C" W, U1 a/ } - >>> math.hypot(6,8)
; ^4 |. Z, m8 j - 10.0
复制代码 ( A" B) Q3 |( T- V& U
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
: ^: p3 m b. h: u9 n' \* z7 \- #如果x是不是无穷大的数字,则返回True,否则返回False
" j0 a# T6 B) R( J& A4 ^ - isfinite(x) -> bool
k; g, V1 X3 f! n0 D - Return True if x is neither an infinity nor a NaN, and False otherwise.
B6 B4 d4 |$ f/ ] r - >>> math.isfinite(100)
2 f. }- h3 Q, P( D - True
4 y% {9 f% X2 g; ~# ] - >>> math.isfinite(0)
7 G; p# d1 Y2 Z% E- r/ w( ]) P - True8 M( }7 v- d' b7 d4 ?" m# @
- >>> math.isfinite(0.1)/ X3 |4 L8 e: ]; y3 s# y6 i
- True
* u3 k. e8 a$ r, P - >>> math.isfinite("a")
$ s1 Y, F6 U& |0 D. i2 A - >>> math.isfinite(0.0001): R6 a6 ^5 H" B4 z! n7 H5 ]
- True
复制代码
5 C" ?" m i- y1 V0 |$ Fmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False- |8 r% Z& [# ~" v
- #如果x是正无穷大或负无穷大,则返回True,否则返回False: p, ]" W$ J4 b/ h2 I1 j7 ?' K. R6 q
- isinf(x) -> bool
. k2 K r3 [8 Y6 c" e( T - Return True if x is a positive or negative infinity, and False otherwise.
; }3 }+ _ M% t2 C - >>> math.isinf(234)" u$ R; Z3 M3 b" _' G4 D/ X7 M9 y
- False( p$ X( j* Q- f% j# T
- >>> math.isinf(0.1)
- M; ~4 D5 Y, x3 z: S# r - False
复制代码 2 s( t \: |3 E2 _4 J3 I
math.isnan(x) 如果x不是数字True,否则返回False
, o9 k" s- t, I# `4 U$ V% o% u# d- #如果x不是数字True,否则返回False
8 O* b, v7 e( I6 W9 i7 O - isnan(x) -> bool5 G- e- l' M4 @1 w. v
- Return True if x is a NaN (not a number), and False otherwise." O1 l6 B5 o3 Z* p0 v" ^3 w' Y
- >>> math.isnan(23)3 U, |, _3 Y$ \5 V
- False
" V, P8 [+ s8 P" A2 }3 q+ l6 a+ E - >>> math.isnan(0.01)* q' b% r- `& c n+ l/ ^
- False
复制代码
/ g- M' e8 N' m3 I/ l0 fmath.ldexp(x,i) 返回x*(2**i)的值
+ c7 `% v$ u$ S2 x- #返回x*(2**i)的值
; k1 C, T b0 t4 Z3 o - ldexp(x, i)* C, E# R) C6 T
- Return x * (2**i).
& o* f3 z* N) ~( W N' b - >>> math.ldexp(5,5)
% i# r% |+ P& |' Q% u0 ? - 160.0
]/ J( T# z' n - >>> math.ldexp(3,5), M$ P) h2 f4 G! d5 a
- 96.0
复制代码
) j; f. E& b; w' `- Qmath.log10(x) 返回x的以10为底的对数8 t% Q9 j9 f( @& W. x- o X
- #返回x的以10为底的对数/ O7 M y( E+ m2 D+ H0 Q! y
- log10(x)
- z6 _* t5 i! m1 X$ V - Return the base 10 logarithm of x.0 |4 t3 g7 b5 q) O2 s; @
- >>> math.log10(10) _0 M* @8 G! |% t( \
- 1.0
! ^1 u, A; {( q) ]1 C. ^ - >>> math.log10(100)
8 \. [, r5 F4 h7 o; z - 2.0
( M) _/ _+ i! G( F - #即10的1.3次方的结果为20! q* [0 x; y, y# }5 b/ @6 l/ b
- >>> math.log10(20)+ ^1 g+ \# M5 X7 `: S
- 1.3010299956639813
复制代码
1 a5 t7 o% o/ O# c# Xmath.log1p(x) 返回x+1的自然对数(基数为e)的值& q# m/ k: a" ~6 U) m+ `4 ]
- #返回x+1的自然对数(基数为e)的值
- v+ y6 `2 }6 d% X' d6 |6 N7 \6 p - log1p(x)
& P" e6 q$ M, R& F* O - Return the natural logarithm of 1+x (base e).
- G: t3 J. x9 O; e! ]! T - The result is computed in a way which is accurate for x near zero.
' d5 g U9 w" {) b; l - >>> math.log(10)3 K K+ U0 v% p. a6 X9 b6 P, T
- 2.3025850929940465 `& u4 j2 ?1 T# z/ F% B( q! X
- >>> math.log1p(10)2 ]* F2 e. v! Y- |- u2 p
- 2.3978952727983707& \) g I( y+ _$ [+ U. w
- >>> math.log(11)
! c! U- J o6 ?& X* a8 N - 2.3978952727983707
复制代码 6 u0 k: k* V2 K8 k$ S
math.log2(x) 返回x的基2对数- ^! A6 M9 q: ]* n" S. ~! i
- #返回x的基2对数+ e; W$ n. R( L& ^$ d: ], q4 D5 v
- log2(x)
$ {( z; c: e/ g7 a! ^5 G! X2 ^8 a7 \ - Return the base 2 logarithm of x.
: H% B1 n' J& Y E m3 V0 [ - >>> math.log2(32)
1 a4 _. R7 b, J' `. }! Q% | - 5.0
" I4 G1 v9 Z$ f) [- o" O2 [9 y0 Q - >>> math.log2(20)
6 E9 P- o5 R% B3 J- e! E - 4.321928094887363
' W! f' ]. n# u$ Y - >>> math.log2(16): Y* s' c, o. x) i+ K4 ^
- 4.0
复制代码 ' E% P! W+ q: q# M* u
math.modf(x) 返回由x的小数部分和整数部分组成的元组. j4 c& {& `# a3 T1 X5 K; F( T
- #返回由x的小数部分和整数部分组成的元组
4 F/ I3 Y( y9 `0 Z' c. x+ ?5 A - modf(x)
7 ] v% j( a, s! t - Return the fractional and integer parts of x. Both results carry the sign
- `2 ?8 ~3 k. j) @( h, \5 n' e - of x and are floats., ]# K U* c; d* i4 Y2 g% q7 Q
- >>> math.modf(math.pi)) l4 g- c3 U7 K9 N/ c) i
- (0.14159265358979312, 3.0)" l) a, j4 K: G5 l7 e- k& L" H w
- >>> math.modf(12.34)) C) M% s* f- U# T. n' Z5 {) i
- (0.33999999999999986, 12.0)
复制代码 # a: o* U7 O5 D: m% T- t
math.sqrt(x) 求x的平方根$ ]$ ?+ ~9 \9 x
- #求x的平方根/ }' h/ z, k0 ?" @( G$ Y! l- z
- sqrt(x)
n, ]" y- ^& g, {) {3 u - Return the square root of x.
1 Z- K% o9 m" p8 b1 k - >>> math.sqrt(100)
N% h) b9 [, ^8 b, F4 J7 ? - 10.0/ Q' T* z/ O* K
- >>> math.sqrt(16)
1 x" f% i7 H B2 U: d( l - 4.02 K, ?0 @( f& _9 l! L) Y2 m
- >>> math.sqrt(20) m* D' u* x9 ]0 Z
- 4.47213595499958
复制代码 1 F" a2 B: R' I' Z
math.trunc(x) 返回x的整数部分- #返回x的整数部分; Q2 @/ w4 e! b/ ~' w; f
- trunc(x:Real) -> Integral
- h: n" g2 q' o; K3 ` - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: L' n7 [- O/ s8 C$ r
- >>> math.trunc(6.789), r5 u7 s( `( `3 [. d
- 6
# T. Q! W' {" ]2 X% j6 q7 v; U: [ - >>> math.trunc(math.pi)
* U0 [* X1 X" Y" O7 r& A - 3
6 a7 x3 a2 P$ w1 D8 k" F# [ - >>> math.trunc(2.567)
- H+ \' X+ ?' _ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|