马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 t% H1 ~, {3 s5 X# t4 Z8 l# @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。3 T* x' q8 N0 q7 L# }- y+ N$ S
l- L4 A6 B5 }方法1:6 M) Q; o" b. c; Q: \
- >>> import math
0 i( @$ Z* `% B+ j% P$ Z - >>> math.sqrt(9)
- [# P' \: l! Q5 A; E r, ^ - 3.0
复制代码 方法2:# |$ ^2 a- o) U* f7 c6 q
- >>> from math import sqrt
9 n/ ?! D$ X' N" n, ~ - >>> sqrt(9)
) [3 r# O1 s& b1 k1 H& b, x v - 3.0
复制代码 $ O& L# Y. s; |5 B! g' y" ]
! X. E# m$ @" G7 n3 j6 h3 ]
math.e 表示一个常量
4 d5 f! T' S, f/ }9 I- #表示一个常量- s2 f$ R, \0 J7 S% P3 n& O
- >>> math.e
. A& M' j: ?- b* t - 2.718281828459045
复制代码 $ K3 Z) W1 ~( x' q$ y
math.pi 数字常量,圆周率
3 M# P% p% Q, O2 N/ V- #数字常量,圆周率
! a9 n1 x. ?5 v: i$ U( q- d - >>> print(math.pi)9 R- Q; K9 Z( ?0 C
- 3.141592653589793
复制代码
/ E. q: E& }% o5 A7 Vmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x; t, }/ p& f5 ?
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
' n n! r1 |" B2 {$ Q/ c( S- | - ceil(x)$ \- M/ g z' P5 p
- Return the ceiling of x as an int.
- T- \2 T2 S5 r4 f% o- f - This is the smallest integral value >= x.
; _1 d, l3 \5 f( h* y4 M3 X
7 H: k5 m3 x) d2 b: n& ]4 |- >>> math.ceil(4.01)
. Q' ^3 V* U( A2 y$ a - 5% t* m6 K( e7 i3 ?% M' R4 l8 x% U2 E
- >>> math.ceil(4.99)
# k3 } K8 `# \ X8 |& M; G! y - 57 q) V1 q `' r" u! ]) ?
- >>> math.ceil(-3.99)0 \6 \& i3 d* B* X" {+ G
- -3 F3 F' U) A2 }3 e M# N
- >>> math.ceil(-3.01)- G: ]3 P: k+ y6 E/ R$ B, N
- -3
复制代码
) h& }8 `1 j& K+ K% emath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ n, t" `. T8 G3 \6 |: a4 ?- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ d7 g" R2 }9 l$ R8 v/ @& y2 m - floor(x)6 F; Z' o' q3 q! |* q
- Return the floor of x as an int." o* f0 q( ?& w3 o
- This is the largest integral value <= x.% {7 j( r2 O" z* s: G
- >>> math.floor(4.1)
. `% }; ]5 C7 [6 ]. J! o- K. u - 4( q+ Q# h, }, Y/ o6 V. i3 |: o+ O
- >>> math.floor(4.999)
7 G6 {7 e! k; V+ F - 49 y8 c9 R! P; d# ]! T' `
- >>> math.floor(-4.999), V* f1 g8 H0 B( B9 f) h! Q
- -5
1 O5 @+ }) E9 L0 H" s - >>> math.floor(-4.01)1 O3 F: d$ C' A
- -5
复制代码 - d4 R! k; D& i$ ?4 U$ n
math.pow(x,y) 返回x的y次方,即x**y
7 w2 r' X1 ]1 S: f4 l: J; d* z+ L- #返回x的y次方,即x**y
9 c0 h) L. a7 |" @" [& E# A - pow(x, y)& @6 R7 j" `) z5 b R# X& {2 @1 B
- Return x**y (x to the power of y)., z }# b% u9 v+ t: P2 H9 L: a
- >>> math.pow(3,4)9 `0 a7 `# Q2 P' K( a! Z
- 81.0
$ W1 z" b/ B' g0 B - >>> & Y8 z5 o4 d" R2 c
- >>> math.pow(2,7)
% G' _" t1 `& A7 g, r$ j! L$ A - 128.0
复制代码 & i7 t4 ^( T1 L0 M2 w' H
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) n8 N: H' Z& g" ?+ S
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) A1 F ~5 j6 p/ e5 d, O
- log(x[, base])
0 ~8 G$ Q5 C/ j. G - Return the logarithm of x to the given base.
8 K% p( c9 n# p: N8 z; \9 N; _ - If the base not specified, returns the natural logarithm (base e) of x.
+ }9 g+ B6 o+ X7 i" j) t% A - >>> math.log(10)
2 Q# R; e# G6 L) {; I: L - 2.302585092994046
1 h$ \2 C1 y; f' f+ O: D8 u" U4 A | - >>> math.log(11)
, @7 K: u: O' ~( u/ d. m. p - 2.3978952727983707+ A0 W9 k& A |- P2 x. s& o: A
- >>> math.log(20)
3 t5 g9 S6 b& k8 } - 2.995732273553991
复制代码 & T3 l3 O9 ^5 v3 i7 C0 {
math.sin(x) 求x(x为弧度)的正弦值
! K+ o( z. R! J# E. ~- #求x(x为弧度)的正弦值! @' A' z6 a4 P6 l' _
- sin(x)- F" q! T( V* j; h% N \" g) n
- Return the sine of x (measured in radians).8 F6 X# v+ Y2 H l! M
- >>> math.sin(math.pi/4)
5 b1 M9 J: K; r6 w4 ?3 m - 0.7071067811865475
6 L* i! Y0 L& d# m - >>> math.sin(math.pi/2)
: H, V( n# C4 x% w! T2 {' Z) w6 Y) k - 1.0( b9 h" S* ?: m
- >>> math.sin(math.pi/3)5 ]! m9 o, c0 r6 i7 d8 }
- 0.8660254037844386
复制代码 & D1 b: m0 f2 |* W8 x% m
math.cos(x) 求x的余弦,x必须是弧度
" H+ V: d% B4 E; d4 O- #求x的余弦,x必须是弧度
7 \, V6 a/ c% F! y7 j+ @5 } - cos(x)7 C$ k# r( y, a5 ?% K
- Return the cosine of x (measured in radians).
; r. R( |8 G+ e% n' f1 v - #math.pi/4表示弧度,转换成角度为45度3 w" J( r* N- R& m% P: l" o' x9 a
- >>> math.cos(math.pi/4)# f& {* i. \1 j! T1 x& S
- 0.7071067811865476
$ @/ R3 W. v' N5 D8 t1 \ - math.pi/3表示弧度,转换成角度为60度% c) x" |! C8 V- G; A0 E9 [
- >>> math.cos(math.pi/3)
* D' c7 |# k- U1 Z - 0.5000000000000001
' B( }, ^5 A0 g7 |1 T0 ~: o - math.pi/6表示弧度,转换成角度为30度# B4 r" b( g2 B& G, w
- >>> math.cos(math.pi/6)
8 b# j" [* \( Q" I! e; D# A+ I- q; a* T - 0.8660254037844387
复制代码
$ z2 P; G* e1 y8 b7 h" \$ Nmath.tan(x) 返回x(x为弧度)的正切值; ^* [1 d# z7 Y: } |& F0 a+ @
- #返回x(x为弧度)的正切值
& A3 Q3 D7 s/ \8 m) o - tan(x)
6 b; f; O* b; ]* L0 L2 D; J# u - Return the tangent of x (measured in radians)." @9 L3 n- e5 \
- >>> math.tan(math.pi/4)
T4 U8 R& s7 p# c9 e' V: A( Y - 0.9999999999999999
7 y9 g9 N6 s0 @ - >>> math.tan(math.pi/6)
9 C, c/ a6 H' @( J' _* X' O- v! v - 0.5773502691896257
( c: l0 K2 @+ N. q7 p4 _ - >>> math.tan(math.pi/3)# ]- S, I! l( X( j0 a4 ~3 A
- 1.7320508075688767
复制代码
: X! I/ t8 X* U) Gmath.degrees(x) 把x从弧度转换成角度
1 U+ N( y2 ^) H4 e; E1 x1 u, l- #把x从弧度转换成角度1 Q, O# R) O2 Y, @; V- X+ z
- degrees(x)
# o& a: F/ e" l" U0 `8 C - Convert angle x from radians to degrees.3 s: B0 |& k4 B% V3 l5 p" X
; C3 d! e9 S* k0 M- ]4 c! l- >>> math.degrees(math.pi/4)3 c/ l3 ^ e+ v* P5 s7 e1 @
- 45.0% c1 l' [ c! x; Q0 w6 H. y" \
- >>> math.degrees(math.pi)! S' v8 O: L+ I3 B0 c- G
- 180.0
4 h2 H# ^$ a5 O - >>> math.degrees(math.pi/6)7 \# J& z; s" c, o- v
- 29.999999999999996
2 G+ E4 `1 I$ b/ z4 X - >>> math.degrees(math.pi/3)3 R3 l# l7 G6 Z. |, V6 X9 j/ B
- 59.99999999999999
复制代码 $ i- e/ V2 |, ~, U8 m: A! V0 I
math.radians(x) 把角度x转换成弧度
/ p+ l9 s, E" {- #把角度x转换成弧度
]" X6 u5 t/ K6 B3 o - radians(x)
' {% L! r4 L6 o0 V/ e) \; A - Convert angle x from degrees to radians.- ~7 K* h# |6 y
- >>> math.radians(45)6 K) M; l5 `! J# B' j" z$ Z9 Z
- 0.7853981633974483. e- `& x, i2 O
- >>> math.radians(60)* D" O. ?8 z' B _
- 1.0471975511965976
复制代码
/ p' |3 I# @" g! r2 [2 w+ i! X0 fmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
) f3 Q7 t6 I+ a6 u+ [- #把y的正负号加到x前面,可以使用0
Z% f% A# |, x% }( q( A - copysign(x, y)
- a% W! C9 m) t7 _6 m9 z - Return a float with the magnitude (absolute value) of x but the sign
% A! e5 O& m7 x' i% x! _1 L4 T - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 }* H% L3 C) }! ?9 W2 ]1 s+ P
- returns -1.0.
! ^# J% y4 b2 `* c! \4 Q6 k. i - 3 {6 L( N7 M O: S7 o# Z3 U, D
- >>> math.copysign(2,3)3 ^* Q# p' F8 K
- 2.0
" c1 Z+ D3 y: p* ?: i' n) k - >>> math.copysign(2,-3)
8 @- |* m$ l1 d) n - -2.0- i1 u0 {1 s" F; o/ I2 @
- >>> math.copysign(3,8)- J5 t! s5 a) w7 G0 ?; r
- 3.0
) p9 q r% M C! n3 U$ M2 h+ q - >>> math.copysign(3,-8)9 v- {$ l4 J: s$ S- g
- -3.0
复制代码 & U1 J5 Y3 V7 g9 Q
math.exp(x) 返回math.e,也就是2.71828的x次方
! | h" ^' E& b/ Z+ t- #返回math.e,也就是2.71828的x次方* z' L2 ^/ i: f
- exp(x), Y& |/ u4 ?, U) \. D) X& l$ ]
- Return e raised to the power of x.0 h. t" k; @! S3 C
) `* O/ s' m0 b A2 c- >>> math.exp(1)
# @0 k2 A! d9 ^& s$ u$ `) ` - 2.718281828459045! Z4 E4 v) s7 s& k/ _, F
- >>> math.exp(2)
* s0 z3 X, g; i: E/ P - 7.38905609893065, L& i9 g' A4 `" F
- >>> math.exp(3)
/ b* e% q* \9 \ - 20.085536923187668
复制代码 0 h2 k2 M* n! ~. ]4 s, o0 \
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减11 L# h! z2 R. v s/ k
- #返回math.e的x(其值为2.71828)次方的值减1
, b3 s- l2 u4 N% ^2 C# W4 ^ - expm1(x)
) S0 w% f; _; B0 L' J6 U - Return exp(x)-1.
+ x. b; u D8 U - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 s: L" e7 x; i, D1 }
' T3 n0 R+ x) p6 f0 D" ^1 }. I- >>> math.expm1(1)* H$ S" J- N3 L6 z! s
- 1.718281828459045
; M9 D* n7 [ {1 p/ x - >>> math.expm1(2)
5 e" x9 V3 T5 w, W: Q& i - 6.38905609893065
0 \ A4 f9 y8 ^9 V, a/ Q* N; V - >>> math.expm1(3)
# K3 R" \5 W. U z$ g3 O0 y( F - 19.085536923187668
复制代码 , }/ Q7 J, X- K( ~
math.fabs(x) 返回x的绝对值
L1 {) K7 J9 z" W- #返回x的绝对值4 o* J" N* }- K- n- y* ]
- fabs(x)3 @0 |# G, I- P' t; V0 Q$ U
- Return the absolute value of the float x.# ~9 ]9 y. N3 y/ B+ y& K$ T0 s
% [, K. Q- z2 a2 v5 h3 b- >>> math.fabs(-0.003)
& x0 V$ D) X P% D0 x5 W - 0.003
$ V, S+ d, Z1 u0 v5 Z4 J) K - >>> math.fabs(-110)
/ n% c+ W5 K: U, t1 H' E - 110.0! C: _3 |" X* l( G3 D8 z* x
- >>> math.fabs(100)
8 _4 @* x4 M9 i2 b0 v: l; l { - 100.0
复制代码
8 K8 w2 m( X/ @! l3 O9 E' ]( Amath.factorial(x) 取x的阶乘的值
$ k5 }! \( U. N- #取x的阶乘的值1 c/ c" S ]7 T, x. \" X
- factorial(x) -> Integral- Y4 l. p! o0 _' Q
- Find x!. Raise a ValueError if x is negative or non-integral.
l. @, X5 t8 Z - >>> math.factorial(1)9 I1 }& F) x# H, O$ J
- 1
- a; a& C5 H$ J- y* N - >>> math.factorial(2)6 ?$ D7 J/ r4 m+ i+ z/ Z0 A8 b( h
- 2
: k8 x3 w0 u d) N3 l$ @0 b" b. J - >>> math.factorial(3)2 T/ c7 i+ ~) G! U8 D6 T! C& j$ h
- 6
7 O* B+ E% K6 M2 h, v, i- b* N1 l - >>> math.factorial(5)4 ~0 E, \% i# H
- 120
7 p. G q# y6 @8 \+ j5 T: t$ n - >>> math.factorial(10)
$ {- u% V* O! |3 z - 3628800
复制代码 ) e, `% e& o6 O
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数/ S- x7 {1 `- G
- #得到x/y的余数,其值是一个浮点数7 r# I: D x" F
- fmod(x, y)9 N( ?* F. c# x# n b; ?
- Return fmod(x, y), according to platform C. x % y may differ.
5 }& j0 r* B4 K8 C& ^ - >>> math.fmod(20,3)- }6 ^: W; \4 j1 l% b2 N; C7 ?
- 2.0
1 o% }3 a: N3 M7 E; B: m* F - >>> math.fmod(20,7)
' D% ?5 t B' o9 r4 S - 6.0
复制代码
3 E% ~+ m/ b8 Rmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 l( ]: Y. h; d# F7 k- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
]+ u6 c1 h3 b$ g& l3 S' e - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值1 g }& ?: I3 A
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( }* a. Z) |( t, L
- frexp(x)
( T% V6 t2 ^8 {; a& n1 z$ H - Return the mantissa and exponent of x, as pair (m, e).( U: t2 g* `: D, G
- m is a float and e is an int, such that x = m * 2.**e.
% Q0 W; }2 s* o; e' d. L% L e2 H3 W - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.. K8 G" S/ w4 Z9 _5 H
- >>> math.frexp(10)
6 b) p6 v8 A% G0 L( ^3 } V - (0.625, 4)
+ X5 C1 |" ]) c$ Q - >>> math.frexp(75)
( K, I" [# ^. U - (0.5859375, 7)2 [1 r0 p' L. Z. f0 @* I" r& h2 a7 M+ @
- >>> math.frexp(-40)5 Q) _3 J1 z/ J9 | ]
- (-0.625, 6)
, X% Z9 s7 }9 H# R/ a) @2 Y - >>> math.frexp(-100)
$ K: P9 N, H4 O6 L( B, o - (-0.78125, 7)' m5 ]% M' a# y& `
- >>> math.frexp(100)# @' S( l- s, D. T7 S' U
- (0.78125, 7)
复制代码 / e; `8 X/ m3 t; h, l' _
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
; E! l7 C7 z+ k: p+ X0 A- #对迭代器里的每个元素进行求和操作
' g5 W3 w0 g/ b2 X0 T - fsum(iterable)1 ~- b1 B! q5 p% H$ |; `
- Return an accurate floating point sum of values in the iterable.
- F9 n1 j5 l: d& ?6 K8 d - Assumes IEEE-754 floating point arithmetic.
9 I6 r3 ]4 A9 G+ b- O% `9 B% o - >>> math.fsum([1,2,3,4])
6 X5 I) i/ T( y H" T( q, b. X - 10.0$ t2 g K: O1 {& H$ J9 w) }
- >>> math.fsum((1,2,3,4))
5 C [0 s5 U" {8 G/ g - 10.0
& M5 w) G- W+ y% e1 E7 Y- A' N( o - >>> math.fsum((-1,-2,-3,-4))- n6 }& E8 Y# {' v9 w4 r4 H; R
- -10.09 P- o0 E0 z) M7 ~3 R9 Q
- >>> math.fsum([-1,-2,-3,-4])" d: j; ~5 r- M4 m' G
- -10.0
复制代码
- b% j$ W3 F! d7 S+ bmath.gcd(x,y) 返回x和y的最大公约数
- G3 T) ?' {6 n$ g- #返回x和y的最大公约数# m/ `: E' N8 _. u7 m
- gcd(x, y) -> int0 Y3 y" H- D4 [4 |$ V( F
- greatest common divisor of x and y+ x, `. B2 K' Z! ]. ] W8 O9 J
- >>> math.gcd(8,6)
; _4 I* g) B$ }. W1 y: U - 2' C5 n8 ^: s& s1 ^ I
- >>> math.gcd(40,20)
6 I) I& E+ K# U8 H- o: b# x - 20
) T* P& W: q9 [* O9 a - >>> math.gcd(8,12)$ g5 H) C W( f
- 4
复制代码
, D5 l* h2 H Emath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 U8 y# u' f. ] L7 r. r% v$ D
- #得到(x**2+y**2),平方的值" }, M4 ~ N; I3 N, }* Q5 d6 F
- hypot(x, y)
& U9 F6 c* m& m9 J5 R& m: d - Return the Euclidean distance, sqrt(x*x + y*y).
1 W) W' o& ^: e - >>> math.hypot(3,4)
3 b2 g5 d1 I' ` - 5.0, t) I; ^' _/ c
- >>> math.hypot(6,8); A/ D6 j6 c' \$ {$ k
- 10.0
复制代码 / M( }0 C& f3 n# E/ ]" B5 L8 L! ?6 P
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False M# {# m; w9 M6 S F
- #如果x是不是无穷大的数字,则返回True,否则返回False5 d. F4 v9 X! k
- isfinite(x) -> bool
: e& ^4 `# T. }8 v% b6 k, M& K4 x - Return True if x is neither an infinity nor a NaN, and False otherwise.
) }' U5 d* V% q) F2 j* `/ r - >>> math.isfinite(100)
2 X& q8 F0 ^, \3 [ J) h& i - True3 \/ n! E0 a$ _4 k9 v6 C0 r0 w6 A
- >>> math.isfinite(0)
0 R. z; C' L. z) B; V - True) ]7 v k. h( P6 s5 H3 v+ Z
- >>> math.isfinite(0.1)$ e( t* {8 O. L8 x0 J
- True
. C+ w# R6 M0 D) T# p5 O- d7 J - >>> math.isfinite("a"): c1 N; ` m* w& Y; p
- >>> math.isfinite(0.0001)
8 i+ X' k" ?0 O, T' `( I- p - True
复制代码
/ s. Y5 F+ u# s0 D/ Xmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False" y7 n# X. f) A- c2 y6 Y
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
1 K( D/ \1 j% R9 F7 t3 P+ k( q - isinf(x) -> bool. a4 q" M/ l4 |
- Return True if x is a positive or negative infinity, and False otherwise.
) {- h+ u( y7 f$ n8 h" _ - >>> math.isinf(234)2 o5 _. ~! v$ U# B( [
- False
) f- @1 {2 D9 E! u+ } - >>> math.isinf(0.1)8 M, o5 W" {9 R* Z9 M
- False
复制代码 2 }$ J# M6 Q& b: r1 R
math.isnan(x) 如果x不是数字True,否则返回False2 c( h( Z7 f1 M. ~2 O0 ^
- #如果x不是数字True,否则返回False1 H0 v# M0 m4 r
- isnan(x) -> bool$ j7 i0 S4 G5 |" V% ~
- Return True if x is a NaN (not a number), and False otherwise.
; [0 n7 |% h( M$ l - >>> math.isnan(23)0 R! Y% B3 J% m; R7 A
- False
* l9 [/ y8 H( Q5 w% N7 F6 A - >>> math.isnan(0.01)
/ W! o; D* w( t6 p* C8 D - False
复制代码
1 X& ~. h. y7 C y7 V7 ~8 Rmath.ldexp(x,i) 返回x*(2**i)的值
3 o+ a6 D. c" @, N" X; S- #返回x*(2**i)的值, r4 ~6 I" W# a
- ldexp(x, i)6 a& M2 B* [6 M
- Return x * (2**i).$ S$ v' T9 W R0 x% ^" h- ]
- >>> math.ldexp(5,5)9 n1 F# z8 s; W/ D
- 160.0; i" e4 R+ G" p( _8 q' B/ e
- >>> math.ldexp(3,5)
5 S, h! T$ v" K - 96.0
复制代码
7 r3 f! R( W+ A C9 c6 B2 n- A- wmath.log10(x) 返回x的以10为底的对数
5 o7 Z" W$ g# z* V+ H0 t# ?0 N- #返回x的以10为底的对数
& n# ^: F i. o% y" q" Q - log10(x)
4 r$ n' r7 g/ C - Return the base 10 logarithm of x.2 e8 T: q1 z( N: n3 ]# Y2 i/ W
- >>> math.log10(10)7 \* ~5 D5 N/ b- _ @0 L. m
- 1.0; V8 ] R3 I3 e
- >>> math.log10(100)
3 G o& K C$ N6 Q) h - 2.0( y/ q$ i6 j2 h3 `: |
- #即10的1.3次方的结果为20/ \" n# ?/ Z h* r q+ b
- >>> math.log10(20)
+ k8 R( j; |6 m - 1.3010299956639813
复制代码 4 n' W5 Y: y, v4 G6 B* C; _1 G X
math.log1p(x) 返回x+1的自然对数(基数为e)的值
# C. f2 S9 i# _* S- #返回x+1的自然对数(基数为e)的值
+ `9 l$ V8 b) Q# B9 { b/ ~ - log1p(x)6 T) ~- P' W% |. K# t- t: K' M$ i
- Return the natural logarithm of 1+x (base e).5 n& A9 K1 Z$ {( [, ?& c/ D
- The result is computed in a way which is accurate for x near zero.0 h/ J0 k/ D- s. S' M+ v, r
- >>> math.log(10)
$ X3 k( B. j' L+ J - 2.3025850929940460 a5 N; g L, `3 w' ~9 W0 c
- >>> math.log1p(10)
/ v7 p. {. B1 _% u - 2.3978952727983707' R! t/ B1 \" u) V8 D, C
- >>> math.log(11). K' v; D/ ?) F# ` [
- 2.3978952727983707
复制代码 " ] K2 p( x; O8 l$ N
math.log2(x) 返回x的基2对数' s/ q* [" ^* W0 z, W
- #返回x的基2对数9 M) I0 C7 w2 X' \/ G
- log2(x)# Y4 p) r8 ?2 \5 x" Y/ x- Y5 `. H
- Return the base 2 logarithm of x.# Z! J- y5 m: ?, Z- c
- >>> math.log2(32)
" t5 |- C6 i; y# H0 f( Q" }" z% V - 5.05 t s7 L8 `+ X) T T5 y; p( y7 l
- >>> math.log2(20)
0 g9 `) ?! j( Z+ H; A: D' |* ] - 4.3219280948873632 l0 w: ?* b3 n
- >>> math.log2(16)
3 [: |# Y$ o, P - 4.0
复制代码 ( N# K9 R) |/ h6 Q0 e L5 O
math.modf(x) 返回由x的小数部分和整数部分组成的元组3 J/ B: w$ i. E. b* f# ]
- #返回由x的小数部分和整数部分组成的元组
6 A4 ?3 [+ S& e; Y: G. W" \ - modf(x) x3 f7 g* M0 ^5 c6 k
- Return the fractional and integer parts of x. Both results carry the sign
( P6 V* U H( S. H) L. W - of x and are floats.1 z+ V) R8 Y4 e3 G" c
- >>> math.modf(math.pi)
* l% j( q* I# u4 n W7 D/ a - (0.14159265358979312, 3.0)* j+ O2 K) U" a5 [$ {: M' T1 V
- >>> math.modf(12.34)
- Z3 {; L+ q* h5 O) V6 Y# k- z - (0.33999999999999986, 12.0)
复制代码
9 }" N# F. q' v' n/ V2 O7 @math.sqrt(x) 求x的平方根$ e0 e) |, R: }+ s: V* o% m) q) V/ B6 u: H
- #求x的平方根" r: `' h1 t' W! \7 A' F/ K1 I* ?
- sqrt(x)! I! S7 ]1 ?% |6 N0 }
- Return the square root of x., F& G: ~! r2 m Q
- >>> math.sqrt(100)4 n/ c4 S9 |* r" d
- 10.0; d" w# r. I" @: |9 j) d* Z
- >>> math.sqrt(16)
8 P, {5 [3 h8 M7 F3 ` - 4.0' A3 y8 L4 F; W, H+ y
- >>> math.sqrt(20)
1 I$ D* Q$ Z4 N+ V9 R" S- F4 [8 A& g - 4.47213595499958
复制代码
4 I; E8 W% i$ o& q3 S0 b! Zmath.trunc(x) 返回x的整数部分- #返回x的整数部分
3 \) y! x! F+ R* x. B - trunc(x:Real) -> Integral
: M. x. D5 V5 p* r0 X/ i. w - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# I9 B' j( s$ Y. S# ? h4 O( j( ]: |
- >>> math.trunc(6.789)
0 \. I, @+ b& Q. V( | - 67 \. g. t& Y) k5 I; @
- >>> math.trunc(math.pi)
4 S( T0 L( F% G f4 R - 3
. j' ~ L( Q) i - >>> math.trunc(2.567)9 P2 t3 v, f, F& V# l r9 C7 `* p
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|