马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 S0 q& o: ~9 b1 W【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: T9 y3 K: j! b2 ]9 N# p: K
9 Q. E, u {0 z
方法1:! `9 I0 Q1 v( S) S/ y6 ?+ |% L
- >>> import math) C* k9 Y9 O% ]; P# j
- >>> math.sqrt(9)
( `/ f2 F; ~& k+ Y J0 }! p0 n - 3.0
复制代码 方法2:
; |0 |9 k; a; x) a( X1 R- >>> from math import sqrt( u* K- z' k. q A7 }6 u" m! z0 [) \: l: f
- >>> sqrt(9)" U2 I M; W8 j: c7 }
- 3.0
复制代码
) D" h3 n& p* x8 l0 ]% b i/ j B: z$ R
7 H7 y1 G0 _- _$ E7 xmath.e 表示一个常量
c- L! L2 ]: Q! C6 Q1 H- #表示一个常量! `5 L, a. u* J0 z; v* t, p, A
- >>> math.e4 k: ^$ c! M1 _& M
- 2.718281828459045
复制代码 ' V9 \+ z1 I- [( B+ N% M
math.pi 数字常量,圆周率
?* v6 | w5 u2 [$ F$ ?- #数字常量,圆周率
2 h, x% h$ Q: C& r1 g% Q# k& ] I - >>> print(math.pi)2 L) C. i, Z5 H8 T6 {! m! J0 ^
- 3.141592653589793
复制代码
3 t8 }. p# ^+ V; g$ f, u0 P4 qmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 P0 U( @* f+ K! g9 n- #取大于等于x的最小的整数值,如果x是一个整数,则返回x: x, }1 r# G* I, \% ]" ~) C. q
- ceil(x)7 ?8 x' o' i8 r/ d6 C- w
- Return the ceiling of x as an int.8 W4 ~ o4 x; A0 A
- This is the smallest integral value >= x.* x' t. |' p- X; o7 n
- # i, v: Y s' p
- >>> math.ceil(4.01)+ g6 `; h3 ]5 u0 {( s' ~
- 51 }. U; _. Q( J: ~9 V8 b: Z4 w# q
- >>> math.ceil(4.99)2 x' [8 t. Y& j
- 5
1 y7 b9 u- C: Q - >>> math.ceil(-3.99)8 s, r/ _ B$ h9 u5 \
- -3
1 D* M2 b) g+ b - >>> math.ceil(-3.01)
4 h8 r0 U, x! p. p5 {" ~; o# L - -3
复制代码
6 I& Z9 k. L% Z& {+ `- k- @2 C+ Imath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& Q/ v8 `$ M, l- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 A" n( j" _4 c* a - floor(x) H( z7 X) c f6 v" Q0 u, Q
- Return the floor of x as an int.& y& {! N% ^& R$ _
- This is the largest integral value <= x.
. ^7 R3 i: p* e: Z5 u0 ^+ K4 z! m) S+ I - >>> math.floor(4.1)
5 {, r( }0 M: @+ I9 Z - 40 C+ F2 G" P9 g: K: h
- >>> math.floor(4.999)( {& u4 w, G9 d; S3 F4 N! M6 H
- 4
& z) O- c3 d5 C* \2 J - >>> math.floor(-4.999)
/ V0 }$ A& g3 D& ] - -5- j7 i/ K- S8 a6 t1 p1 H# t7 a
- >>> math.floor(-4.01)
8 |7 [+ i) C( o {9 T2 d$ I* m - -5
复制代码 & j" A% C3 h! p/ {* a
math.pow(x,y) 返回x的y次方,即x**y
Y8 [& a& N* ?2 f# x! J- #返回x的y次方,即x**y; K/ x2 I3 m( H7 a
- pow(x, y)& b3 s7 E' E1 Y" L! o& K9 e9 w5 G
- Return x**y (x to the power of y).
( l" N7 j# A- g" U) Z! {0 _# Q - >>> math.pow(3,4)! Y% i4 D4 ~1 ~# Y+ R
- 81.0: y% h+ S+ y8 M( ?4 H) \" D
- >>>
. f _) I2 B6 @0 t; ^9 [ - >>> math.pow(2,7)
- X5 K) s8 r2 C- s( T - 128.0
复制代码
" `' n* u! I I8 t% ?math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): P0 I2 K, w) p6 j& h3 m* Y
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' @. A5 S- z& V. R- a - log(x[, base])
' a+ l. H I+ k- V7 R) S7 m - Return the logarithm of x to the given base.
0 r0 E" ?( C R6 ^% [ - If the base not specified, returns the natural logarithm (base e) of x.
; e& i/ [$ V! `1 j1 z, G/ \( ? - >>> math.log(10)
Y1 ?8 {7 h+ o3 x6 i) v- W - 2.302585092994046
) R0 P$ {( S, I- t ~2 C, C - >>> math.log(11)+ A1 }7 C" T n7 ?7 k F8 V
- 2.3978952727983707
5 ~/ z( H* K( Z3 N) A* ~ - >>> math.log(20). d' q9 G2 n) @2 `7 [" j
- 2.995732273553991
复制代码
) Q0 Y$ E4 a, J/ e2 Tmath.sin(x) 求x(x为弧度)的正弦值2 T! D( w# @0 q% y
- #求x(x为弧度)的正弦值" B+ B+ ?1 F1 B* y, f0 S
- sin(x)
3 z" f" o' f q3 I$ J - Return the sine of x (measured in radians).
! `# k% X |" }2 s - >>> math.sin(math.pi/4)
5 K4 Z. x/ s% s( `+ f8 Z - 0.7071067811865475/ Q0 o+ E6 m( E
- >>> math.sin(math.pi/2)% B# {& }; o) r
- 1.0
0 Y3 U1 z, b# s" Q4 G6 B5 Z! y - >>> math.sin(math.pi/3)
$ g, W% L' i+ s- s' G# ] - 0.8660254037844386
复制代码
3 K9 b) a( r O" q! S+ cmath.cos(x) 求x的余弦,x必须是弧度 b- M0 {3 H6 n4 U9 a* o1 s
- #求x的余弦,x必须是弧度
, _$ Y* d, ~" X% m - cos(x)
9 |0 `: |( W9 h( [4 r4 L/ z2 J - Return the cosine of x (measured in radians).3 X: [& `9 `9 E+ U
- #math.pi/4表示弧度,转换成角度为45度1 d; G( o g3 ]
- >>> math.cos(math.pi/4)
% ?2 v! @7 G$ X+ i2 V3 Q6 u0 _' ` - 0.70710678118654765 f0 p; N; _( |' k# V3 h- F
- math.pi/3表示弧度,转换成角度为60度9 p/ a% C( B9 ?
- >>> math.cos(math.pi/3)
3 ?2 o% w# U, a4 q) f; d3 f9 P - 0.5000000000000001
( P# _) t2 B0 y: \( q. g - math.pi/6表示弧度,转换成角度为30度: Q) A7 T" J4 Z% Z* P7 \
- >>> math.cos(math.pi/6)
5 t N" b2 ?" K3 P - 0.8660254037844387
复制代码 - p, z0 j# k& N
math.tan(x) 返回x(x为弧度)的正切值) T# U% y% a8 h4 x- L
- #返回x(x为弧度)的正切值8 ~9 z2 t$ U6 q+ a+ q! J0 I
- tan(x)
2 ?1 t* g9 ]' T: p9 ?( D - Return the tangent of x (measured in radians).# s u5 V' ~1 k* v4 t
- >>> math.tan(math.pi/4), k' n8 K }* V( T" k
- 0.9999999999999999
& J5 s# F; ?6 D# G7 z$ ` - >>> math.tan(math.pi/6)
, P+ x3 d- H; o0 z# b: q1 i% V0 p - 0.5773502691896257- ~' h) \5 B0 s
- >>> math.tan(math.pi/3)
9 W( j- S; E" p - 1.7320508075688767
复制代码
% _- }. E6 w! w: l: `; i: ymath.degrees(x) 把x从弧度转换成角度2 X8 r, ?$ s, i4 C+ f$ j4 C
- #把x从弧度转换成角度! M* G3 y6 F/ m
- degrees(x)5 b9 W3 S* U d1 n% z
- Convert angle x from radians to degrees.
: |- _; v2 R. l) p" c8 O- _ - : P. e- Y! u9 q; u6 D
- >>> math.degrees(math.pi/4). b- M2 M9 k+ ~, }5 V7 e# Z9 [
- 45.0
5 S- N4 c/ d- u3 ]& H - >>> math.degrees(math.pi)8 h9 ?+ a4 W& H' O
- 180.04 G3 k8 i: T: t' h; [; t& \- f. D
- >>> math.degrees(math.pi/6)) U$ J! l$ J5 n9 p7 C
- 29.999999999999996
' C9 T7 ^+ T) t" g0 c - >>> math.degrees(math.pi/3)( i/ r5 `: T, n2 P8 m
- 59.99999999999999
复制代码 2 e% N. D; W$ B+ y6 W
math.radians(x) 把角度x转换成弧度/ Y$ z! S% b* X: K5 Z$ b
- #把角度x转换成弧度
4 t" z- ]' R4 Q8 t% Q* _ - radians(x)
' y- v0 M* m- c5 U% P0 B9 n) T - Convert angle x from degrees to radians. \( l5 k) s$ J7 \% D- i \
- >>> math.radians(45)# W1 ]( R$ K4 Y: {# T2 H; r1 W$ ]
- 0.7853981633974483
/ X) S: T0 ^' b5 h* a - >>> math.radians(60)
. a2 ?' M# k9 M7 k9 G4 z0 C6 Z# N - 1.0471975511965976
复制代码 $ G0 d# y! ?, q- \+ @
math.copysign(x,y) 把y的正负号加到x前面,可以使用0- R) w3 `% C: n$ y4 V- A! D
- #把y的正负号加到x前面,可以使用0' Z! t# U" T( w- k
- copysign(x, y)5 F. ?1 S5 H P* p- P
- Return a float with the magnitude (absolute value) of x but the sign
% ^9 V: }1 Y# ]% o - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
' Q) i4 z! g* g8 C* C: ~6 N - returns -1.0.- T6 o8 e0 A/ x( M. L' X
- : L6 J" n* J# L& }8 C' H
- >>> math.copysign(2,3)
* w' n) B$ D- P- D8 w: F - 2.0+ V& T2 M$ l5 H4 H$ b. n
- >>> math.copysign(2,-3)2 b) V5 N) |' S) d+ }
- -2.0
4 c) ?* k; H$ T- b' Z3 a - >>> math.copysign(3,8)' A' u! q7 v k) F0 M
- 3.08 Y% G; b1 `; o0 u9 L1 X6 d
- >>> math.copysign(3,-8)
0 j- J( H& x1 v4 \ - -3.0
复制代码
- A3 C, r0 |2 m0 N9 T& \math.exp(x) 返回math.e,也就是2.71828的x次方
" D& {, M- X, s# B- #返回math.e,也就是2.71828的x次方3 c0 c5 I! b" V* `) p% p, h
- exp(x)
- ^3 H% }3 F' z: N6 {% y - Return e raised to the power of x.( k; A( K' D% g& y: l6 U
- C/ b0 V5 n3 s! G# j- >>> math.exp(1), H$ N- M& ~7 |; a" [
- 2.718281828459045
8 J0 N; J, k. [" e! x0 _. T - >>> math.exp(2)3 \$ I& h7 Z: ]( h' s! {
- 7.38905609893065! |" s- a! {- W; i$ @
- >>> math.exp(3)
* p k9 o# [' n5 [ - 20.085536923187668
复制代码 " V' W0 W5 O- S+ p# T
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1" j& ^0 B* o, [" F
- #返回math.e的x(其值为2.71828)次方的值减1
9 E& Z: U1 e! d3 F* D - expm1(x)
# C( k$ v5 U% c6 C, r - Return exp(x)-1.) b. c4 n: f# b; G
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
+ i3 w6 x. z) n7 C9 v- Z0 }
) A0 m/ A: W& m- >>> math.expm1(1)
+ ~ U% F8 C* M# w- B( \7 r4 s - 1.718281828459045
: q# h% D# _ U7 _, G4 I( b% h- X - >>> math.expm1(2)# P$ v& r6 T0 l
- 6.38905609893065
2 l" g. Y M9 F - >>> math.expm1(3)
) e h. N0 o, Q4 }9 G - 19.085536923187668
复制代码
7 Z V# {8 c0 f! ?math.fabs(x) 返回x的绝对值
) O# ?5 y: ?) T- #返回x的绝对值
: J4 \* g' l, v q9 }! n7 g/ K - fabs(x)+ O3 t3 U, b q1 H
- Return the absolute value of the float x.
8 l; H- G7 P5 f1 Q% ?4 A
7 e! q3 ]) A1 T4 d4 f- >>> math.fabs(-0.003)8 J6 I. I9 o! S' d* C5 X |1 A3 P
- 0.003' V8 Y: A6 L! B) ^; `, j8 j8 K
- >>> math.fabs(-110)
0 V. n3 x+ n' U4 E& R - 110.06 H9 l9 V5 ^# z
- >>> math.fabs(100)9 x3 o! W) A, B2 t& M; y- D4 L
- 100.0
复制代码 1 k) i2 m3 |: L2 }) P% B
math.factorial(x) 取x的阶乘的值+ |/ k0 ^% j b4 K: X7 g
- #取x的阶乘的值9 F) w2 C" X6 C" p( ]; I8 C" r
- factorial(x) -> Integral
3 v# L* k) u0 b( F - Find x!. Raise a ValueError if x is negative or non-integral.
2 M, W8 v& d* ]- l; z' f - >>> math.factorial(1)* @/ G; V0 B/ a
- 1! ?3 g' d6 E2 m8 H, ^. w
- >>> math.factorial(2)7 W1 S2 c0 X0 d
- 2
+ t* o! F( R7 N+ ]' H. c - >>> math.factorial(3)
2 ^, w' M2 A4 d; c& U1 s( T9 S - 6
2 t% `7 r1 m: b1 y0 h$ O4 k - >>> math.factorial(5) n% n9 u/ s6 S6 l8 s' U; P, [; B
- 120
7 m, y) p6 q! E) {& j7 G3 p - >>> math.factorial(10)! H" ]3 L: t/ \) _4 E1 x5 \
- 3628800
复制代码
/ Q8 D8 ^6 r# ^# h0 Emath.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 o5 A' u& k9 |! s4 P
- #得到x/y的余数,其值是一个浮点数
* k+ m- P! ~! O9 S7 ] - fmod(x, y) w/ X+ @" D7 c
- Return fmod(x, y), according to platform C. x % y may differ.) J+ m3 |- {) t" Q
- >>> math.fmod(20,3)% |- A7 a" v, S! D+ O
- 2.0
, |! U6 `( o+ V& s6 l - >>> math.fmod(20,7)
. k$ w w3 X" J - 6.0
复制代码 2 H- Z" }3 e! \& Z. e
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围+ w- Y! ?6 |; o4 M: A) h
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
: ^% W5 e& s2 J6 F& l0 d& A% R - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
! U! t# _ K- R- e0 w+ D - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" X" w3 _% P; q- k2 |! {: X( A8 p0 g
- frexp(x)
# g- s9 W% {$ T% V; g/ F0 Q& M - Return the mantissa and exponent of x, as pair (m, e).0 r$ Y# x4 ^% y r8 }0 x9 w
- m is a float and e is an int, such that x = m * 2.**e.
U* u8 r2 l/ \. P* ] - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
6 c& b$ Z' Z' d) n0 o- U - >>> math.frexp(10)
1 L6 P. K3 {) v. a% i- Z - (0.625, 4)
7 F5 N t6 a$ E - >>> math.frexp(75)
( B( h x2 b K) d$ m: ?% M - (0.5859375, 7)
$ a4 o7 M! o- ^" ]0 f' y - >>> math.frexp(-40); r' t+ Z7 X7 M, O1 _! D
- (-0.625, 6) h4 `' B* f" h# m8 C! g6 _
- >>> math.frexp(-100)
6 w8 U1 Z4 G9 c3 s - (-0.78125, 7)3 ^( v- y4 Z( B9 U% F
- >>> math.frexp(100)) [- r' T& U8 [
- (0.78125, 7)
复制代码 ' H; p/ Z9 ~; Y$ q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)6 z6 u# | C+ A( }7 K
- #对迭代器里的每个元素进行求和操作
, a% e5 W% m { - fsum(iterable). h6 C' E: n8 b1 u7 I6 _: s
- Return an accurate floating point sum of values in the iterable.
8 ~3 f. G/ ]2 ^! [4 @/ o4 ^ - Assumes IEEE-754 floating point arithmetic.
% |* C+ \ |7 E# `2 x1 s2 E7 H7 X% k - >>> math.fsum([1,2,3,4]); }! \+ b/ k. W/ k, q
- 10.0
4 ]. q9 l, A. p O. ]' v- a, v - >>> math.fsum((1,2,3,4))
$ L8 ]! e3 q; {. q- J% X( v - 10.0! r5 z) x s' }7 A) B
- >>> math.fsum((-1,-2,-3,-4))
& v7 L! f2 V1 E l% L P* ^ - -10.0
% w1 k! j8 H% b/ a - >>> math.fsum([-1,-2,-3,-4])
$ Y! ^; w% G6 F: c# g" K - -10.0
复制代码 6 [$ G- [. N5 `2 r
math.gcd(x,y) 返回x和y的最大公约数
% p( d; [- s6 ?. _- #返回x和y的最大公约数# B$ e8 r. n b! g$ s7 C
- gcd(x, y) -> int' E% L; N8 D+ O* {
- greatest common divisor of x and y! L2 Z1 T7 F; ^4 a3 M* T- B/ a2 ~/ M: X
- >>> math.gcd(8,6)
( {) x9 B* ]3 Y9 W' {% U - 2
7 }! b& o# K/ X3 X3 ^7 s - >>> math.gcd(40,20)
+ N' t" v$ V$ \; j, n: z5 } - 20
2 { H8 H* A* ^6 g7 t1 ^ - >>> math.gcd(8,12)8 \% L% h; A' b; O( h, U
- 4
复制代码 0 k5 s, g* O) j( x4 ^: i3 n# C: l
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False; g" \- m: ~3 |, _8 r
- #得到(x**2+y**2),平方的值& T+ J" Y* G! I5 ~/ p/ I
- hypot(x, y)
6 T% L0 G8 N: ~, |+ Q - Return the Euclidean distance, sqrt(x*x + y*y)." D% B/ L* c u. y/ h$ I8 j9 c) M
- >>> math.hypot(3,4)4 E, u) b& ?: i5 Q
- 5.0
/ O( v |: W. ^/ F; U8 \ - >>> math.hypot(6,8)
# l8 M; I1 A( p/ u) i - 10.0
复制代码 * S7 M1 X- E7 L* V1 v
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False2 |! n; e6 p- h$ v: e' M
- #如果x是不是无穷大的数字,则返回True,否则返回False
) u9 j' P2 O) B# M - isfinite(x) -> bool
4 v& S# j& }- @+ o% N7 f4 s" N - Return True if x is neither an infinity nor a NaN, and False otherwise.
" F/ a7 H, V9 T# ~6 O E) F6 [+ P+ f - >>> math.isfinite(100). H* A5 Q/ F& L- b8 T4 C7 |" ^
- True
! c; s& d- D2 K/ v/ s* ] - >>> math.isfinite(0)
0 P. O/ N* g/ G& ]! a/ [+ @5 k h - True7 ?9 X0 f7 i% K6 U; R8 B# A/ `) P
- >>> math.isfinite(0.1)
$ |7 c! i4 F% u* e - True
5 [. L, A+ q' d2 B/ \3 V" H - >>> math.isfinite("a")
& W; W3 w# D+ L! o" h* X - >>> math.isfinite(0.0001)
5 V3 S4 P2 P) j( `& D - True
复制代码
8 u8 l; s/ F# B* u9 k0 e/ k: lmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, d* f5 q& h0 a; z( {( e; G' D7 F
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
i# L, p. c6 }' n' J/ r - isinf(x) -> bool
8 }. K4 C* {, x9 i# Z: f3 D - Return True if x is a positive or negative infinity, and False otherwise.
" G7 @+ U N( N q& ^) V - >>> math.isinf(234)# ^: l5 z: ]3 \4 [
- False
# L0 e' h9 D2 W- K, _ R9 M- \ - >>> math.isinf(0.1)
) S2 m6 `& ^0 Y5 q9 X - False
复制代码 ! x+ ^" \' N3 O/ q+ Z- Q& {
math.isnan(x) 如果x不是数字True,否则返回False2 H# e \, F; Q* P. @9 o
- #如果x不是数字True,否则返回False8 ^: l" }9 ~, Z! ?5 h& K
- isnan(x) -> bool
3 x6 e0 |/ X c" |4 N# U t - Return True if x is a NaN (not a number), and False otherwise.$ ~& G5 Y) i: H2 Q9 T/ h" h
- >>> math.isnan(23)5 N, c3 @* k2 j9 J' P! |
- False4 z1 z& M) l7 L
- >>> math.isnan(0.01)5 y/ ~2 P# ^& j0 G& O; w e
- False
复制代码 2 i, s+ \% e4 v# f9 T. h5 T4 C
math.ldexp(x,i) 返回x*(2**i)的值
9 N* P+ V( \' k8 F; k4 k" D5 Z- #返回x*(2**i)的值7 x6 x6 b( a2 [* B3 g! v: S
- ldexp(x, i)
2 a M0 D- a2 @" X5 c8 Z - Return x * (2**i).
' @0 ~0 N+ o- g. C/ r( v - >>> math.ldexp(5,5)
( [! X) V2 N, f, b; Y& r- W- q - 160.0 R8 w5 i+ j; {( R+ c7 X: B
- >>> math.ldexp(3,5); F( ^6 @' @6 u5 t( k
- 96.0
复制代码 ) L: a3 i' z8 U: h2 K* y# [- |
math.log10(x) 返回x的以10为底的对数
8 m* m) B; p- E0 Q& O, j- #返回x的以10为底的对数# B' e+ ?: a5 N1 C
- log10(x). f1 p3 l- Z1 F9 t
- Return the base 10 logarithm of x.
: E$ E$ h$ X4 W5 j# M1 l - >>> math.log10(10)3 X( T/ h) g/ P6 j7 M1 k
- 1.06 W; _2 |# E; g% n N/ j @
- >>> math.log10(100)
' P: p! I2 u, k2 V) ^& W8 r - 2.0
: h# `. l# [2 b( p0 E; K2 } - #即10的1.3次方的结果为20
( v R' V9 [. y4 Z4 Z8 M - >>> math.log10(20)
, p4 j% ?/ `' F" ~; ?: l - 1.3010299956639813
复制代码 % G% S3 J( m% `4 r% j5 n5 c& E( h
math.log1p(x) 返回x+1的自然对数(基数为e)的值
! [; K9 I% J. _& O: f- #返回x+1的自然对数(基数为e)的值6 K0 J; T5 F6 D% _ y
- log1p(x)/ r0 G" }% d6 N# N, L' v$ c* U9 g
- Return the natural logarithm of 1+x (base e).( x0 B' C7 S' e' D& Y# W( Q' W
- The result is computed in a way which is accurate for x near zero.
, B# V1 U4 _7 m - >>> math.log(10) s* ~: |# A% \
- 2.302585092994046
2 _3 x& [2 z+ L% X X - >>> math.log1p(10)
3 S, N2 ?. P. D$ V( U. ? - 2.3978952727983707; M! S3 j) s# F. V2 F
- >>> math.log(11)
% S/ I7 P8 ]* R' _& i - 2.3978952727983707
复制代码
- U" s+ {4 ]- r" Rmath.log2(x) 返回x的基2对数
2 K/ D3 H( s& l9 H( w# J$ I% J3 e- #返回x的基2对数
) O7 T# W$ _) ^* _& e - log2(x)2 a F8 U: B- w' ^0 ~) x) j5 x
- Return the base 2 logarithm of x.
% U j' Y9 n. B2 h. ~, w/ ~3 p - >>> math.log2(32)4 j# A, k& O1 v# s) b/ I& r
- 5.01 l! x P/ D9 ^0 }$ n6 Q$ F
- >>> math.log2(20)
, j: f6 v2 A+ r; \3 t Y: v- S) m - 4.321928094887363
( k, P5 l9 ?: Z! |9 C+ R - >>> math.log2(16)* @' m# u, H) o3 k% V2 Q
- 4.0
复制代码 / ]7 i* S) [0 |! Y v" o+ N4 n- p
math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ A$ d) e- U! j: y, u/ q- #返回由x的小数部分和整数部分组成的元组& q3 _6 h# {$ e; l
- modf(x)4 R$ Y# v" u# y1 Q' K' i+ n. c) u; w
- Return the fractional and integer parts of x. Both results carry the sign* E0 \1 L4 T* f, B9 ^
- of x and are floats.- x/ H0 T' s) W' X
- >>> math.modf(math.pi)
: p' v5 P& a$ w3 n$ r - (0.14159265358979312, 3.0)
K5 d6 ^& m6 r0 U% G+ a/ A4 a& ^ - >>> math.modf(12.34)) h) W$ O. o. y* m- u
- (0.33999999999999986, 12.0)
复制代码
- _% X, w$ n, r5 m# w- Jmath.sqrt(x) 求x的平方根: } ^3 k# |: I0 K0 ] O
- #求x的平方根
0 c) D+ s$ n. J( D3 x4 v! t - sqrt(x)* J: S& m( y* w- W7 w0 U
- Return the square root of x.
8 T7 z; G( [) B - >>> math.sqrt(100), Q) t+ r3 \+ ^, y/ v( w$ H' i
- 10.0
% j0 P) L; G' B/ h& w: f1 d# S - >>> math.sqrt(16)0 ~5 l' W& h7 |6 n6 C# k
- 4.0# Q& O+ l/ p7 ^+ ~
- >>> math.sqrt(20)
9 k- f' x" v1 s6 c - 4.47213595499958
复制代码 # n z( ?9 p' V7 G
math.trunc(x) 返回x的整数部分- #返回x的整数部分
6 W! `# N- d' W" w2 }. Y7 U - trunc(x:Real) -> Integral# q |9 X m8 s% ~" |+ p
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
! i; o3 o$ H1 A+ e, j2 r - >>> math.trunc(6.789), E2 W: x: z0 W9 l% \. Z
- 6; O8 y d, k9 }4 r# ^
- >>> math.trunc(math.pi)
) J+ f( [# Z/ A2 x% H9 q% o - 3
+ h, k, }& T* A5 D7 S0 T - >>> math.trunc(2.567)
- ~' A: x# _$ u+ A& g8 c! k - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|