马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
; y; Q) ~8 Q E& d/ V! r) j# O p
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 h8 P; d ]. y, c( N6 Q1 |: H+ `% w3 L- ~" Q! l. y
方法1:
9 {, ]+ Q6 r; o1 P7 p3 x" O- >>> import math
6 C1 O1 E- ?% p0 ^0 K, w" O - >>> math.sqrt(9)' B+ l' b5 d7 l6 _6 L/ m) D6 ?
- 3.0
复制代码 方法2:
" e% G( d3 F3 M- >>> from math import sqrt
+ g3 n x5 R) l+ ^, Q - >>> sqrt(9)
) B) _. \# [) _ - 3.0
复制代码
# u# S D1 b; @& d. R+ e2 T5 |
) z# z( z" `# }: n8 p* _math.e 表示一个常量
D' a- [$ T/ o9 P1 C" A4 H- #表示一个常量3 R' _4 X4 G8 N' L
- >>> math.e0 B/ W3 {) q. S
- 2.718281828459045
复制代码 7 `5 J% x' Z2 s! v1 _' ~" p! Z
math.pi 数字常量,圆周率
; n; r) o; ^, a( f- #数字常量,圆周率 C9 s7 E, Q! P
- >>> print(math.pi)
# j! O& {8 {; k+ j/ T1 t - 3.141592653589793
复制代码
. s3 ^1 Q9 D$ F$ D Smath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
( r% ?# b% c$ s" `) H- #取大于等于x的最小的整数值,如果x是一个整数,则返回x A1 w! z1 T. E+ R
- ceil(x)2 n7 Y& I8 k, S2 Y# E9 J) j$ a
- Return the ceiling of x as an int.
3 y8 c; i6 W7 V" m1 a9 K6 L - This is the smallest integral value >= x.
( {7 N- i% ]0 }: o' X" d! A
% Q9 D. F, B' l9 o; I- >>> math.ceil(4.01)* v% T+ i( O- F* S; |
- 5
9 m' N* {0 S$ `5 q& v% r - >>> math.ceil(4.99)4 ?) z5 P* c; w# \, o1 D8 N$ ~2 D
- 5; ?2 w4 E4 j8 W) s
- >>> math.ceil(-3.99)/ M" P9 t! E* `* a8 |& Q4 s/ h
- -3
$ Z( m) T2 _( D* D4 S( ?( m - >>> math.ceil(-3.01)4 x% t7 M& C. u6 i/ w: a
- -3
复制代码 9 s, p+ z" F: C! u2 b
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; O5 \$ {) B% s- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 L8 [ R) X& G! X$ m. V; R - floor(x) E: F5 a+ r$ e) b0 m
- Return the floor of x as an int.( W- C+ ]8 [" x4 m7 c8 `1 N: {/ k
- This is the largest integral value <= x.- I5 u+ H9 A6 |* a- u' `
- >>> math.floor(4.1)+ x3 i/ M! v6 g
- 47 k6 r2 v, f! L$ A* ^' V
- >>> math.floor(4.999)* X! }' x& q" s2 c: R" ?
- 4
& n' w. y- M8 O& P* O0 t - >>> math.floor(-4.999)
- W3 C7 \7 K# B6 L) f' I - -5
* O; ]' M3 d- u - >>> math.floor(-4.01)) e% [" ]( ], `: j+ i2 `
- -5
复制代码
0 R3 U) `% |% ?math.pow(x,y) 返回x的y次方,即x**y. V" E/ `" d. w6 \6 l/ v u) |0 l
- #返回x的y次方,即x**y6 c/ G7 o8 D; Z! \. `
- pow(x, y)
- G7 X. I( T* Z2 X - Return x**y (x to the power of y).0 F. t6 x( d; E3 `
- >>> math.pow(3,4)
- `) R- X- u0 d5 q: _ - 81.0$ d. {6 k5 v- a# \
- >>> 9 C5 @: x' v( _' H7 T" d
- >>> math.pow(2,7); G! f4 z; ], E8 X& ]
- 128.0
复制代码
! c- d" d. \6 G8 Z. vmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' ~$ D/ @# J! Y& ?- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ W* t- B' a* s - log(x[, base])6 n6 C* Z7 O# C. V1 }8 l) ~
- Return the logarithm of x to the given base.5 D% C3 Y b2 J: _! z u
- If the base not specified, returns the natural logarithm (base e) of x.1 Y: ^: Q* O& e7 b, Y7 x2 O. M
- >>> math.log(10), Z8 s2 [, R: d* R! o
- 2.3025850929940460 e1 j) \/ U! E ^
- >>> math.log(11)! d/ ]# q; k# a- Y, O' i9 \
- 2.39789527279837076 ~2 r2 o& ]1 [( |# a
- >>> math.log(20)5 C2 B, ?) ]: H* H$ u
- 2.995732273553991
复制代码
' ~* L4 v, X3 R+ Fmath.sin(x) 求x(x为弧度)的正弦值, ~$ }8 M- E& g8 G5 m3 M S3 Z/ y- T
- #求x(x为弧度)的正弦值" V+ ]: A. g+ s
- sin(x)
4 H; d6 o9 y0 c, ^ - Return the sine of x (measured in radians).
' W- Y! m @& A" e& O+ Y8 M4 F* [ - >>> math.sin(math.pi/4), a7 `5 b6 ]! ?
- 0.7071067811865475
6 x5 b% }8 o& n - >>> math.sin(math.pi/2)
- q7 z! o* |, T) ^# y) N9 _9 o - 1.09 j# t# w. [$ a! `" U
- >>> math.sin(math.pi/3)% C- X8 O: X, r6 x; [' ~! d5 ~
- 0.8660254037844386
复制代码 z# ~# G* ^! G' D1 U
math.cos(x) 求x的余弦,x必须是弧度
3 G, ^* K( V+ `- #求x的余弦,x必须是弧度
/ }1 ~ l% k: R' y9 e7 F. R - cos(x)7 [8 K5 d. O: r8 n5 x0 W# f8 J- _
- Return the cosine of x (measured in radians).
$ ?$ s+ o) E3 y1 _/ y/ @ - #math.pi/4表示弧度,转换成角度为45度* Z6 V3 Z8 q( [ W' I/ X# s
- >>> math.cos(math.pi/4)# Z( T w) B9 ?- M) b9 @ P1 ^
- 0.7071067811865476& V1 F# g3 k0 V+ f
- math.pi/3表示弧度,转换成角度为60度! o( _! @7 \& v! Y% n0 ^
- >>> math.cos(math.pi/3)
9 T7 h- v! c' I8 X* l - 0.50000000000000014 T/ G) a4 A6 A
- math.pi/6表示弧度,转换成角度为30度8 c3 W# A; o4 t* G
- >>> math.cos(math.pi/6)$ q0 q9 F. R7 S; v& T8 M
- 0.8660254037844387
复制代码 2 I, J, f T0 H5 G3 _7 J
math.tan(x) 返回x(x为弧度)的正切值
$ X; V( h: N! H1 g3 G4 f7 e) I) R- #返回x(x为弧度)的正切值0 ]% H, r) H' F
- tan(x)% x) I g' j5 g6 q9 _, p+ x7 Y
- Return the tangent of x (measured in radians)." m' Y" `% S8 n
- >>> math.tan(math.pi/4)$ a+ ]' I3 _0 f9 o0 G/ b/ _+ ~
- 0.9999999999999999
" S. y& e, Z7 v* Y$ Z - >>> math.tan(math.pi/6)
' R3 b8 Z( D8 J3 @ - 0.5773502691896257
% ^9 D& e3 F" K9 N; e8 k: M$ Z - >>> math.tan(math.pi/3)
! G* T& Y1 b4 {) k% w) ~3 r8 g - 1.7320508075688767
复制代码
& {! d: W; O5 d/ p2 I, H, Tmath.degrees(x) 把x从弧度转换成角度. Z4 Z: m! i- V8 v# b* a
- #把x从弧度转换成角度1 A5 D" C) r1 j
- degrees(x)+ I9 T$ {/ q+ V
- Convert angle x from radians to degrees.
4 F; a0 j$ k% P) K
/ B9 Z$ p% K* ^( K9 j6 p% }& N- >>> math.degrees(math.pi/4)
* j& h# A3 [, Y w - 45.0
2 Q" F0 U3 I! w+ s5 y* @ - >>> math.degrees(math.pi)+ V; [/ C1 h; b" M' T$ {- `
- 180.07 s3 v- T z& L+ c
- >>> math.degrees(math.pi/6)
7 |1 ^3 ` h0 f; i. L k4 _/ w - 29.999999999999996" h( A% Q; \! Q8 r( C! _8 u0 V
- >>> math.degrees(math.pi/3)4 _2 P' D. n: P9 {+ C
- 59.99999999999999
复制代码 0 j% `: }0 Y* X1 m
math.radians(x) 把角度x转换成弧度5 B5 i$ l2 L( V* H% ?
- #把角度x转换成弧度
3 J& p U' C1 e; F - radians(x)
$ t% B9 G1 [& e7 t - Convert angle x from degrees to radians.0 E; j$ T8 {& G
- >>> math.radians(45)6 g& F1 s2 y! D+ r' E) f
- 0.7853981633974483
- a) s% j; d0 [7 a- v& C - >>> math.radians(60)
5 ?3 k* O: z7 _; |; \. K2 b - 1.0471975511965976
复制代码 * E: K/ t9 o9 W) B" E% j8 A0 }
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
* L. ?6 H$ |) ` T- #把y的正负号加到x前面,可以使用0
! X+ m& {3 J S$ s. f - copysign(x, y)
! ^: C4 {; a/ T8 h) P6 a: Y - Return a float with the magnitude (absolute value) of x but the sign
9 B$ g' V% q e0 s7 e8 h - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 C1 l+ r" B" E* }$ z$ d6 @: Y
- returns -1.0.+ g- y( {: z ^% R& ]
- * `1 N. n9 r1 r7 q' Y: S
- >>> math.copysign(2,3)
5 K5 ^/ z+ ~/ A( e n - 2.04 g7 ]# _6 O% ~- F
- >>> math.copysign(2,-3)
# ? ] |/ `/ Z6 Y - -2.0
) @' E1 L0 Q! x, y/ ~- k - >>> math.copysign(3,8)
6 P/ }" ^2 x* J8 L6 b - 3.0
6 B( z9 P, l5 ?$ G - >>> math.copysign(3,-8)8 r: y5 `+ o4 F1 F# X$ |
- -3.0
复制代码
8 t% D* R) B! r7 f$ O7 d. o" b1 zmath.exp(x) 返回math.e,也就是2.71828的x次方
6 Q) y# a" o' W- #返回math.e,也就是2.71828的x次方; b0 ]/ i# s# ^$ o- x6 ^
- exp(x) ^& P9 |5 L+ i% `- S
- Return e raised to the power of x.
- h% O+ @: W0 P- ]- b8 M - ! \! l7 X g1 ^" }0 E
- >>> math.exp(1)8 U5 |( `/ t6 v$ C3 ^$ `+ F' T
- 2.718281828459045
3 s4 M" ]7 `) U( h' J - >>> math.exp(2)
) F& p0 @3 q ] - 7.38905609893065: x1 r) P. _8 J1 m; f4 I
- >>> math.exp(3)% c, q2 x5 D: B; {4 T8 m
- 20.085536923187668
复制代码 # i& M# q* @' e2 c, Q
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1$ h( s1 e: d! k' ?9 @, |
- #返回math.e的x(其值为2.71828)次方的值减19 @3 E* @! y$ k0 V4 k
- expm1(x)
( I2 u* ?! B: \0 ?' D' Z - Return exp(x)-1.7 W7 D1 U' j7 r) L/ w
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. n; n/ T1 [, i0 L" x0 Z% L7 ~; Z: Z
* M( N# Z! x$ R& T' Y8 V. X& W0 J- >>> math.expm1(1)
k5 |# J! Z& E/ t5 I1 \- z7 C% w - 1.718281828459045; u# L) _6 u6 `7 d, s* X& _
- >>> math.expm1(2)* ]( Y9 `7 _( r2 x' I' k9 _# v
- 6.389056098930650 k% J8 r3 j0 d& |) J& k; r9 P% W% c
- >>> math.expm1(3)
! a8 M& W! f* ^3 t4 b; w% D - 19.085536923187668
复制代码
0 L* S& u/ ~: s" L/ jmath.fabs(x) 返回x的绝对值
: [! `. t% z. K7 O- @6 I6 C- #返回x的绝对值 q J/ m6 p& }7 Y3 \+ J
- fabs(x), O% q% Q/ e( |& a
- Return the absolute value of the float x.
: D2 a) _) \( ^/ C- q$ P1 x
+ M% P/ K4 I6 N+ S$ [7 w- >>> math.fabs(-0.003)
9 H. p* u: W; @! J; E4 v w' K: N - 0.003, T7 H" f% j6 e* t9 V, Q
- >>> math.fabs(-110)
7 H6 A/ ]+ a- D - 110.0, x0 z- Z# t$ L6 l/ P8 b# C ?0 B6 Y1 k
- >>> math.fabs(100)$ h" q3 Y( n9 R+ x7 s
- 100.0
复制代码
) l! [" [ a1 ~; i$ d* ~5 U9 b& \8 ?math.factorial(x) 取x的阶乘的值5 ?8 ]& T% z/ F" l$ G% M; N* m, W
- #取x的阶乘的值
5 \; {1 e$ [% s5 x+ @ - factorial(x) -> Integral- w7 v# ? }" O
- Find x!. Raise a ValueError if x is negative or non-integral.# ^: g8 [% t* m3 Y2 l) V) _
- >>> math.factorial(1)) P _) X$ I" S! x8 j
- 13 c s, z& o4 {1 |. l; r7 ^4 `; v
- >>> math.factorial(2)9 Z* v7 Z( }& B
- 2& \, s9 @5 }% r* o7 b7 a6 I
- >>> math.factorial(3)* f! L1 Z" c0 q, E. e. N% x
- 6
) X m7 V+ L# M& n) k - >>> math.factorial(5) }! u% d+ O+ w( @5 F4 x
- 120! q$ C+ K, z5 f+ c
- >>> math.factorial(10)3 ~& E, J& H; E' H: O6 W- I
- 3628800
复制代码
" O. g' t3 j4 Z8 wmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! O. }7 j- ^0 j, X* z- #得到x/y的余数,其值是一个浮点数, y, q% w9 s; g* Q, d$ `% H0 @1 W
- fmod(x, y)
8 i! k2 Y% s. O1 {" P - Return fmod(x, y), according to platform C. x % y may differ.
2 W$ ]# l' Z0 V, @* J h, ? - >>> math.fmod(20,3) ^' \0 a6 f& _! w* B
- 2.0
1 K' y/ N. I) d6 s, Z: X. }! t, t - >>> math.fmod(20,7)
" K" \+ Y2 ?0 G* N - 6.0
复制代码
1 J. I4 t7 |, G. t& t+ Rmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 i) c3 R+ m# ?& c1 p7 |- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
( D' `/ \. b3 t - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值+ Z: n h3 V4 T/ Y
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 y9 W$ e3 B; M. T4 F8 c2 j
- frexp(x): j* s# q% T! ]/ ^ q2 }' q4 ?4 Q
- Return the mantissa and exponent of x, as pair (m, e).
" G$ V# ]$ y4 h$ @2 k. ` - m is a float and e is an int, such that x = m * 2.**e.
" W5 X$ D ?# [# r4 L N - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.% l. a; v U: c% n+ m! P0 x
- >>> math.frexp(10)* b/ }- U! M9 ?1 G9 ?: w8 p' E
- (0.625, 4)
8 j( S9 E0 N: w4 x/ I/ ?! B$ _+ F - >>> math.frexp(75)
- }5 X- d" U0 P2 W7 Y. K5 j/ _; J - (0.5859375, 7)( v# h$ f( v; K2 J1 X0 X) j
- >>> math.frexp(-40)
5 _; i6 w$ O2 B7 N- o. W - (-0.625, 6)+ q; v. U$ B- w+ I% {: N/ e9 ?
- >>> math.frexp(-100)
& j, J* Y0 N2 y4 |1 U: L - (-0.78125, 7)
: A/ ]- x" V) Z- V4 { - >>> math.frexp(100)7 t1 V4 v* b+ B8 m
- (0.78125, 7)
复制代码 , \/ @' T. j: |2 [# P) l. H( V
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
0 Q, n+ q7 ~+ E6 Z& o- #对迭代器里的每个元素进行求和操作
: P z) B3 r& e; n- E - fsum(iterable)" v% _0 d9 r% B7 Z P' j* u
- Return an accurate floating point sum of values in the iterable./ q/ V* g$ i+ t, n( G Q7 |( J: Z' c
- Assumes IEEE-754 floating point arithmetic.! e# I& F* h e' H3 _. z& a
- >>> math.fsum([1,2,3,4])% s% F4 D! H6 T* t. g1 F
- 10.0
1 b) z p4 i) Q; Z" T) j, a6 d - >>> math.fsum((1,2,3,4))3 }( [$ h: W0 `% M/ r- w2 l
- 10.0
1 u. D( g7 U( h4 I. B1 ]9 R - >>> math.fsum((-1,-2,-3,-4))
1 B% f' h" k- I) g - -10.0
2 h# ^2 \" I3 e) a5 [% f/ p - >>> math.fsum([-1,-2,-3,-4])! L! Q9 Z' C; T7 y! U* c
- -10.0
复制代码 $ {( G! q7 K& E9 W" ^" [ A
math.gcd(x,y) 返回x和y的最大公约数
9 L8 u( P9 [8 r- v- #返回x和y的最大公约数
; n1 f' s; K5 z; Y; O - gcd(x, y) -> int- Q% m3 o( p' ?* l% D6 W; m0 X
- greatest common divisor of x and y t% R- W9 s l5 j. ]7 E u
- >>> math.gcd(8,6)
; W- X E3 p: x# t+ F6 ~ - 2
/ N+ Y" j5 q0 g3 t0 q$ p - >>> math.gcd(40,20)
4 m& _) v: C7 K; n8 q# `2 j+ V9 w - 20
2 p) g( o9 g, S - >>> math.gcd(8,12)( V6 x$ o K$ D. ]7 N; m1 e
- 4
复制代码 1 O( U& p( ^) c
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False z- J" E- T1 o8 C
- #得到(x**2+y**2),平方的值
4 k9 q9 _3 Y: s# e4 X3 l+ r' F - hypot(x, y)
3 b7 l/ v' m- H, N' \) }& i u* v - Return the Euclidean distance, sqrt(x*x + y*y).
% w4 G, \9 f- z( K. c4 N - >>> math.hypot(3,4)! w# u z& \' y; s# s9 t
- 5.0% {" I# ^0 M6 p* |: v) _
- >>> math.hypot(6,8)9 f4 ^$ T% l p4 _8 X" b; M+ n
- 10.0
复制代码 1 x/ V9 O* S9 |1 f6 s+ V- P3 C
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False+ y+ |5 d) K$ W/ |( c$ d) g
- #如果x是不是无穷大的数字,则返回True,否则返回False7 G0 a( f; g: F8 ?- w5 F
- isfinite(x) -> bool, r3 t8 C: ] L7 Y, ?2 B/ d
- Return True if x is neither an infinity nor a NaN, and False otherwise.
# s" j+ m2 F2 ?; m - >>> math.isfinite(100)
8 f% h9 _7 W+ b' s# m - True4 c9 I5 w/ M0 V$ j/ p* p, L- A
- >>> math.isfinite(0)8 L' O6 { u+ ?+ A; V$ J- F: U
- True- i8 [7 w' }) j2 H
- >>> math.isfinite(0.1)
+ z1 U- Q# e8 C; l" C - True, N w, D1 {3 \3 m; O6 A
- >>> math.isfinite("a")
" \+ ~, O0 k& u - >>> math.isfinite(0.0001)
5 i0 }, j& i8 ~- _/ s& ~ - True
复制代码 ! i. |" t) T. q5 J6 p- r
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
% R2 ^7 R( w* _% t; G; c/ M) P) y- #如果x是正无穷大或负无穷大,则返回True,否则返回False
# |1 T' {3 N5 \) P - isinf(x) -> bool# f% d) g- a) m; i7 M6 E1 R1 Z% ~
- Return True if x is a positive or negative infinity, and False otherwise.
- X8 r+ ^% R1 @# H4 R - >>> math.isinf(234)
! T- ^! j% r" h6 k" A! l+ { - False7 p6 Y) Z1 G$ o/ s' h. n
- >>> math.isinf(0.1)
5 c j6 w8 d+ b# n - False
复制代码
2 R$ p8 N& ^$ G2 @3 k& Lmath.isnan(x) 如果x不是数字True,否则返回False
K/ M3 r3 E& K. b- X4 l- #如果x不是数字True,否则返回False
# o) Y& g- e x. N1 K3 q/ M7 G8 \( O - isnan(x) -> bool7 O8 {7 m3 d4 b% ]! p4 E0 Q* {
- Return True if x is a NaN (not a number), and False otherwise.4 |' D: D f& j, ~' _ D: p. A
- >>> math.isnan(23)) ^! L( J9 b/ W! @% i
- False& K% @: t; }; ]6 ^- E6 E1 V7 @" O
- >>> math.isnan(0.01)
1 I% ` e2 i, m( H$ v' | - False
复制代码 ) S9 |' H. D1 D8 x: P: l2 I5 H0 e0 x
math.ldexp(x,i) 返回x*(2**i)的值
. g( o# w4 B1 J+ F0 @- #返回x*(2**i)的值
, D. w' I: M! h4 g% n) T - ldexp(x, i)
6 _$ b4 s% n6 I8 z' S9 U, V - Return x * (2**i).
: }+ P7 ?/ r2 m/ t, n - >>> math.ldexp(5,5)
4 P) |. |9 H4 z; y; q - 160.0
' y2 P, z) C( `- v, h0 j9 Q - >>> math.ldexp(3,5)
3 d1 d9 r& _ B2 ~8 V7 j% ^# y - 96.0
复制代码 : g( }' T" r- d; @/ Y# G
math.log10(x) 返回x的以10为底的对数
0 \; }, p* `" ]& ~( z" v- #返回x的以10为底的对数% \/ w! Z' w; S4 `/ p
- log10(x)9 z- u) ~; r8 o" J
- Return the base 10 logarithm of x.
, B- u8 E# J+ u/ f4 y - >>> math.log10(10)& `+ B/ R H) s
- 1.0/ Y6 i! |. N( G! B4 A
- >>> math.log10(100)
2 ?( c/ i& Y3 }2 E1 P" x" G# j - 2.0
9 M5 i0 Z `7 W; N% R9 }. I - #即10的1.3次方的结果为20
* }% l) M" X. y - >>> math.log10(20)# L# ^: l1 O5 U7 B
- 1.3010299956639813
复制代码
9 Y" R E; }- j2 N/ Kmath.log1p(x) 返回x+1的自然对数(基数为e)的值
. T" Q$ _7 N! k5 J" [" O- #返回x+1的自然对数(基数为e)的值
5 v8 b2 G0 @1 _& B6 R2 B - log1p(x)
* }. f# b4 g$ o- q5 w) T - Return the natural logarithm of 1+x (base e).3 H" S a& m/ j& d8 @
- The result is computed in a way which is accurate for x near zero.
; { C! N3 |5 {* f - >>> math.log(10)
& g8 U2 T. Y5 o' E - 2.302585092994046
+ O1 x# s: { _' s8 C3 l: | - >>> math.log1p(10)
. |- n5 x2 R5 j; A' v, o - 2.3978952727983707
* K! i8 h; c- E - >>> math.log(11)
! g1 M5 o- l4 _6 A6 N: y - 2.3978952727983707
复制代码 1 o' X* r4 o8 e% u
math.log2(x) 返回x的基2对数
2 w9 N5 H5 K- _5 L- #返回x的基2对数$ H6 @" `- f- ?* F7 z( ^4 H
- log2(x)- [- U" P* b# \) A9 I' Y7 z' R
- Return the base 2 logarithm of x.
5 g' V' `+ Y* D" U: Y - >>> math.log2(32), X Y. q, u4 s% V0 x. G* S% x
- 5.03 |( U1 { Z0 T, g
- >>> math.log2(20)& c" `6 n: J- }7 f. J
- 4.3219280948873636 K J9 E& ]$ V' ?+ m
- >>> math.log2(16)% d4 K/ H, S& e- x# l
- 4.0
复制代码
: B; e$ Y8 Q( O4 Amath.modf(x) 返回由x的小数部分和整数部分组成的元组2 Q' `" u7 n: W P/ e
- #返回由x的小数部分和整数部分组成的元组( |- m! L1 J* U) I# T2 a$ S# K0 v
- modf(x)
& P. B* K4 ~# n5 L3 M- N - Return the fractional and integer parts of x. Both results carry the sign
' r$ {0 W$ M C4 j: v( g3 Q4 Z - of x and are floats.
. Q! S$ j1 G7 f) s" ? - >>> math.modf(math.pi)6 N, v2 C0 V" ~$ [4 `8 R% a3 P( R4 B
- (0.14159265358979312, 3.0)5 s" Y! @- F0 A: N
- >>> math.modf(12.34)$ b B7 H, x. N3 P+ l
- (0.33999999999999986, 12.0)
复制代码 ' s* K+ L: @9 `$ m6 Q) } K, d
math.sqrt(x) 求x的平方根( D: T% S9 q. t
- #求x的平方根5 t) |% T7 \& C$ [% a
- sqrt(x)2 V% x6 b: [7 m3 r' Z+ Q
- Return the square root of x.- C$ L+ t/ F6 R( h' y2 @5 r& ~
- >>> math.sqrt(100)
t+ q0 x! t" p# n- m8 u7 | - 10.0& T3 }& d: I! J3 {% T2 P& d8 U" I
- >>> math.sqrt(16)
9 f0 ^# \! X' n: q# g6 r( h - 4.06 I" ?% J) z& g, c
- >>> math.sqrt(20)
) Z0 O6 Z; _8 B$ x - 4.47213595499958
复制代码 ( x3 u* K$ q" v ^
math.trunc(x) 返回x的整数部分- #返回x的整数部分' y. J5 Q7 a$ m, s# q
- trunc(x:Real) -> Integral( y! I6 `/ `+ P; c4 f( U
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
. P6 o8 R l% }. B: D - >>> math.trunc(6.789)
& V8 W/ x4 e1 r/ K - 6
, r# M7 h/ C. S' d7 L* S7 D# r5 T - >>> math.trunc(math.pi)
% W+ L. ]8 r. X: L/ c+ H - 3
2 k0 Y1 `0 T7 k2 J( D% [& v - >>> math.trunc(2.567)
( o7 `7 P- P8 ~ r3 i - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|