马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 {- h7 b0 H4 `0 l5 V) v: p, n
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
* k% X- K( @9 u" p8 `) N
' d4 R# w0 k9 F) J+ d1 g( e% ^- }方法1:
$ `8 [, T$ F4 q2 I$ E9 C5 }- >>> import math
9 F- J+ e2 H" x7 X# b3 _; n# x - >>> math.sqrt(9)
3 d3 ]" W4 ^5 C" f - 3.0
复制代码 方法2:
9 w9 U f2 N! d, |% U2 K- >>> from math import sqrt* m9 V( H& V5 |; j* X
- >>> sqrt(9)
4 J- J4 K" f( J4 Q Z4 i1 J - 3.0
复制代码 6 N8 \0 A+ _6 b* {% ? R) J C' X
+ @* E Y* a. M6 F0 d' {math.e 表示一个常量5 M/ W$ I! A- ?" Y8 A5 q
- #表示一个常量
/ m# j6 S0 x( @$ A3 c - >>> math.e
) t8 i) U5 S/ U2 Z4 ~ - 2.718281828459045
复制代码
l h+ P$ @8 d6 V* b7 _0 Omath.pi 数字常量,圆周率7 r) p, O4 _3 a
- #数字常量,圆周率3 |6 L0 f& ~: f3 w& h6 D+ h6 e& `
- >>> print(math.pi)0 O* E. g- w% C) }: }9 c
- 3.141592653589793
复制代码
( F6 I3 |& P# U, u& v. z! b* Pmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x2 M# E( ?# S* G! h" U( a
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x; J4 {0 V6 B! A' M6 z0 S
- ceil(x)
& h) l3 [/ `& }) U, q - Return the ceiling of x as an int.
4 i) r' ^2 V. M3 R& W - This is the smallest integral value >= x.
5 s! g3 Y7 x- r5 g) J- K, Y - 5 Y2 ~: r1 I. q' g
- >>> math.ceil(4.01)
) n3 A) z! k7 V! ?9 A - 50 n2 \4 q0 T; }
- >>> math.ceil(4.99)
" Y, A/ R, S2 r0 p - 5
. B( K8 Y1 b" M - >>> math.ceil(-3.99)
* X, I. X9 q# E6 y1 J2 X, q - -3- s! n& T$ T7 a: l
- >>> math.ceil(-3.01)* q/ s7 I. ?" M- N3 F! T
- -3
复制代码
2 X- L. A! l& R I }: C4 |math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身) J$ F( Z* K" a& Q
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ ~7 n; C( O8 u5 h8 Q; G' d1 \/ Z - floor(x)
( ]& b# G5 Y( Y6 C& d) m - Return the floor of x as an int.0 d- q- p8 I7 n) _' [3 L
- This is the largest integral value <= x.' _9 y+ {8 ?" e
- >>> math.floor(4.1)* x: e, ~; z4 _" u0 p
- 47 O* P4 K. ^9 z$ q3 }! h
- >>> math.floor(4.999) M2 _+ u' Y0 g" Z( R& N
- 4
; ]5 Q& x3 w! ]1 c2 @8 z# ?. r - >>> math.floor(-4.999)
- M+ d6 V, q8 Z! N0 W! p+ T) o - -5: Z6 B8 a3 q4 }# U! F
- >>> math.floor(-4.01)" D: `7 a) s0 \; X6 [$ Y' V1 |7 u
- -5
复制代码
. h7 A' ]% I2 J. ~( w* r. a$ qmath.pow(x,y) 返回x的y次方,即x**y' a$ `2 D$ Y7 t7 B H9 ]
- #返回x的y次方,即x**y% T1 W$ h' z4 N
- pow(x, y)
3 o- a9 F( Y2 E/ z; y. Q7 P9 i - Return x**y (x to the power of y).8 `# x8 T6 c: d7 w! N9 W
- >>> math.pow(3,4)
0 X. x! Y5 [. f n9 M - 81.01 b, o1 |* @4 ?3 M
- >>> 2 u: x; y/ i1 ^1 P; U6 V
- >>> math.pow(2,7)
. F7 m% B4 l; j& \2 }+ `; Z - 128.0
复制代码
& C3 P4 y% |9 D1 K# _7 Zmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& q, Y% w" r/ ~2 o- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! Z5 w1 O4 u& T1 j - log(x[, base])8 P( F5 n8 S7 H
- Return the logarithm of x to the given base.6 q( C! d/ X# m
- If the base not specified, returns the natural logarithm (base e) of x.
, b' q+ ]4 i# \2 K0 g' A4 m* W* c - >>> math.log(10)
; M" i0 @& p2 c6 X) [2 i - 2.302585092994046
+ C* f5 a& y% a0 Q! N9 q - >>> math.log(11); `0 I( O0 s8 k6 H3 d. v
- 2.3978952727983707, c0 d6 w- c M5 |7 {
- >>> math.log(20), T! \! d- n$ \
- 2.995732273553991
复制代码 8 }8 t! Q9 I7 B
math.sin(x) 求x(x为弧度)的正弦值9 H" s# y, a0 g# u% q& G8 N1 X5 K
- #求x(x为弧度)的正弦值/ {( d1 `! H! n* G
- sin(x)
9 u7 f! A7 O, `0 R - Return the sine of x (measured in radians).9 c% ?, T! b5 y! H( X0 n
- >>> math.sin(math.pi/4)+ W4 P1 G: j q \- \' L# u2 Q
- 0.7071067811865475% I6 B {/ w% F" h
- >>> math.sin(math.pi/2)+ R1 \' r9 D5 _9 u' j: V
- 1.0
3 @& d, @& o J5 w- A" D - >>> math.sin(math.pi/3)9 g; x6 y5 h3 W6 E! D
- 0.8660254037844386
复制代码 & |$ x; z5 L8 T. O i2 h* f" o# w
math.cos(x) 求x的余弦,x必须是弧度
* T+ p' A" K4 q8 B4 }( k7 N( Q2 Q- #求x的余弦,x必须是弧度
+ {6 n/ R* _1 T - cos(x)+ K5 m4 x9 m* r, W$ [9 j8 D
- Return the cosine of x (measured in radians).
# L5 l: g- [. w+ M$ I8 B7 @& Q - #math.pi/4表示弧度,转换成角度为45度" G) [2 n/ P' m, e! i
- >>> math.cos(math.pi/4)1 A* g/ c) k9 p7 M, h
- 0.70710678118654764 z1 j% v0 [* `
- math.pi/3表示弧度,转换成角度为60度7 L0 m o! j1 P5 a
- >>> math.cos(math.pi/3)$ y/ g% ?/ b7 w/ M
- 0.5000000000000001
' h o8 c# f R: R/ e7 R - math.pi/6表示弧度,转换成角度为30度
% U8 ]5 x) K1 Z" o8 A1 v - >>> math.cos(math.pi/6)7 f: V* D# V- T
- 0.8660254037844387
复制代码 9 Q7 x# ~! z5 P' ^. t4 ~* v' U4 b
math.tan(x) 返回x(x为弧度)的正切值
6 H1 _" s7 q: ?- #返回x(x为弧度)的正切值4 c) L# R2 o* ~: M' x" `- k
- tan(x)
3 b0 c% Y7 R0 q! l1 q2 u; P+ P - Return the tangent of x (measured in radians).
# _+ U& e* m h% Q - >>> math.tan(math.pi/4)
$ }9 f8 O5 z7 N. Q" k3 B - 0.9999999999999999! m* q0 J# o3 r: r( T4 \; S! d
- >>> math.tan(math.pi/6)$ y2 O! F3 O" h! k1 |4 I
- 0.5773502691896257
' \* m9 h: v$ ~3 s$ s/ r4 t+ P - >>> math.tan(math.pi/3), r& `+ ~4 D. O2 S; T
- 1.7320508075688767
复制代码
- F/ h7 e) D6 m1 D9 ~. e6 V, rmath.degrees(x) 把x从弧度转换成角度
" b. m& V P- K1 ], `- d: k* ]+ ?- #把x从弧度转换成角度) u4 Y! M7 G- Y; D( d
- degrees(x)
; J- n9 \1 e" `1 t! o* x - Convert angle x from radians to degrees.
9 i6 Z. z0 i5 ^3 v' S6 n9 g& o - % @. D9 o5 }. F& |0 z: g4 S
- >>> math.degrees(math.pi/4)8 A7 g h4 S2 m, G' u: {
- 45.0
6 K8 J1 @! X+ m2 y' T4 \ - >>> math.degrees(math.pi)
$ U7 @& w4 ]: h, u0 e6 k - 180.03 @/ g( B# e. w; L' I
- >>> math.degrees(math.pi/6)5 Y2 Q4 {! Y L6 e/ \; ?. Z8 Z' x
- 29.999999999999996) F8 F. A1 M0 ?5 q/ F7 W
- >>> math.degrees(math.pi/3)
" [' H$ Z2 [! a! i Q - 59.99999999999999
复制代码 ; E# v) s1 N; k7 D+ a" W; x
math.radians(x) 把角度x转换成弧度% w1 T. Q; t4 W; C- ]- @' I
- #把角度x转换成弧度7 @3 y% r2 n3 q$ L& r4 m3 h
- radians(x)
$ g, r2 u0 i( d( J. ^ - Convert angle x from degrees to radians.
7 @, _% A P8 x - >>> math.radians(45)
8 P: z# k1 H0 D* `5 w& @ - 0.7853981633974483
6 Z9 d5 z6 x% W# r8 e - >>> math.radians(60)# Q; g" v5 ^9 v( C# b
- 1.0471975511965976
复制代码 & m: k) u$ z, l( E$ _/ T$ ~% \
math.copysign(x,y) 把y的正负号加到x前面,可以使用0- ^9 o- c& ]# x" M0 {( k: R) _
- #把y的正负号加到x前面,可以使用0$ K) i |. W" f# y0 ?, L! X
- copysign(x, y)+ ?4 E$ c: b# R0 }* {
- Return a float with the magnitude (absolute value) of x but the sign 8 J7 D+ G5 G" J. X8 |9 Y
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, ]& l' I9 s6 v; T - returns -1.0.
; X+ i+ V4 ^- D3 Z6 b5 w1 U
$ V3 P& O( E2 S, J5 J; E2 y- >>> math.copysign(2,3)
, q8 Y4 V: i" T4 v - 2.0' D, M3 H5 A$ _2 k/ W
- >>> math.copysign(2,-3)" q1 `) M5 X# ~ r p3 G- R3 e$ {2 d
- -2.0" V( D t n# K8 ?8 D
- >>> math.copysign(3,8)5 E6 k7 N. l1 ^; ^9 _' H
- 3.0
$ a) s0 [" c: g0 E5 B - >>> math.copysign(3,-8)5 d9 ]# o8 e3 Y
- -3.0
复制代码
y; y9 [$ s. ]- V. b0 |3 }" M. L) Kmath.exp(x) 返回math.e,也就是2.71828的x次方 {- S n- N& P+ K' y0 b
- #返回math.e,也就是2.71828的x次方* \- q' F) U9 C: Q
- exp(x)4 e; s7 B7 b- C2 d& g
- Return e raised to the power of x.
9 ?6 N+ B: F7 C - 8 k4 @% {" o4 u! ?9 D. J/ i
- >>> math.exp(1)- q( |% l4 {% v/ a/ e
- 2.718281828459045' p4 b$ X8 D+ [+ u. x
- >>> math.exp(2)" ?% _ ], H! ~1 C$ h2 E5 k! b
- 7.38905609893065
" e1 H. b+ R3 a - >>> math.exp(3)$ X8 E3 C k' n! x- V
- 20.085536923187668
复制代码 % v( y/ R% f: k% Y% t& q
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
0 t/ ?/ X% J- q- #返回math.e的x(其值为2.71828)次方的值减1
2 Q$ l! `: }7 E0 G& z3 m+ o - expm1(x)2 r/ {- s9 D; `6 F8 I
- Return exp(x)-1.) c6 i! j( q/ x$ y0 Y' ^6 q1 e9 e
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
: ^& g6 a8 ?2 G0 {
9 N: v# ~2 O( y& W7 H- >>> math.expm1(1)* o5 h3 [# w' Z D" R
- 1.718281828459045
8 T, J. \5 ?; H7 n% L1 ^( O* p - >>> math.expm1(2)
m+ ^2 S& ~, [: k5 N - 6.389056098930656 t6 L4 l5 l3 f* V0 L' {
- >>> math.expm1(3)& n [3 X/ L: `) \+ a
- 19.085536923187668
复制代码
% }* \/ `, R- `4 N. j1 j) @' W4 imath.fabs(x) 返回x的绝对值
# {) I7 e( {- B$ m/ [: ?3 X- #返回x的绝对值
$ e- J4 Q( {" \/ a7 c# s - fabs(x)
5 e( |# L' g' P5 O1 H - Return the absolute value of the float x.
! Y- s- l* f* l& d: x* k/ S2 O
8 x, S3 b5 x- F. p- >>> math.fabs(-0.003)5 {3 p2 A0 J* {, d, X
- 0.0038 ]+ A8 `1 ]* p+ Q( Z7 k! X
- >>> math.fabs(-110)! i7 K/ s2 f! u2 T5 Q* R. |, l; D
- 110.0% _4 p( c, ?+ u o% X3 Q6 ?8 R
- >>> math.fabs(100)1 n- M; @6 `4 J. k
- 100.0
复制代码 0 M/ `4 X: ]0 G4 K* S/ O
math.factorial(x) 取x的阶乘的值% K1 ]2 }, q. V, K0 j
- #取x的阶乘的值+ f* J. q! n) q; Q! q4 D3 H* d! T
- factorial(x) -> Integral
% j2 L- G- d. D T3 V: M/ j' t4 D3 s9 p - Find x!. Raise a ValueError if x is negative or non-integral." y6 j V( V5 P4 g$ W* s
- >>> math.factorial(1); S0 T" b# V4 G! L! q% f
- 1$ y+ d" y* L8 R9 l( ^5 w
- >>> math.factorial(2)
! x" b3 L# _7 { t+ h; |! K( h, x; a! B - 2
2 }" J. B6 W- {% T - >>> math.factorial(3)
# p9 C/ L# ^/ B7 u; b1 U0 t - 6
* g. i% b+ o/ u - >>> math.factorial(5)
+ V$ s, G) o/ [8 d7 U, { - 120
2 F! y$ @8 Y5 S( i* ^& @ - >>> math.factorial(10)# d9 T$ k) F9 o/ x& X* u
- 3628800
复制代码 9 K: j2 t3 Z7 _3 L& |
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数 g! B' f9 a/ C4 h
- #得到x/y的余数,其值是一个浮点数
. e, K2 R: n% s6 p1 g - fmod(x, y)
2 f/ A" \* B8 k, E9 V1 u - Return fmod(x, y), according to platform C. x % y may differ., `( |+ J. L6 }( Y; M7 c7 y
- >>> math.fmod(20,3)
. r. H5 ]: R1 R |3 l5 D# p( g - 2.0% Y! ?) ` ^/ |# W" b4 P* c
- >>> math.fmod(20,7)
; H9 W& b3 W% t- }; x; |3 K - 6.0
复制代码
8 A: f2 H8 a: _+ A% ?math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 ~9 g- M0 _" }0 e& G
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 x9 w% T+ H6 U( a3 R
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 t2 Y6 z* D9 S5 t+ Z
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和13 ~$ ~! V( u) X5 }; q8 ~; ^8 o* I+ l
- frexp(x)
- \! V4 M( j. y+ x, ]$ k Z - Return the mantissa and exponent of x, as pair (m, e).
! q8 j, A6 T( t8 k( o - m is a float and e is an int, such that x = m * 2.**e.$ Q: E, x- _" [
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.. |# c8 A' Y9 C4 G0 [' K
- >>> math.frexp(10)1 U+ \2 r0 @# X# Z/ q* K& `6 s
- (0.625, 4)
/ G5 A S; o7 Y9 P- M - >>> math.frexp(75)
/ Q$ t9 B4 X; S3 N1 X - (0.5859375, 7)- `% l, F) d# F7 i& M+ S
- >>> math.frexp(-40)" W' h, |0 v* I5 ~, u1 M
- (-0.625, 6)5 z& p2 t# W" B- h# Q( f6 v
- >>> math.frexp(-100)
# i* Q- c* P" R. y3 K2 k& {* ^5 V - (-0.78125, 7)0 S1 g/ _6 p+ Z. L" h( B
- >>> math.frexp(100)
+ Y" u9 s9 q4 G/ `7 @0 z5 j8 z7 p0 ^ - (0.78125, 7)
复制代码
# T" u' ~# C8 ~math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 O [) \+ n0 z3 d- #对迭代器里的每个元素进行求和操作
2 n9 V3 X8 S# z: D/ i( D - fsum(iterable), e) z2 m9 }. y" z
- Return an accurate floating point sum of values in the iterable. z4 \) y- g x2 ]0 `
- Assumes IEEE-754 floating point arithmetic.
; H' m. y) C: z - >>> math.fsum([1,2,3,4])) l9 f; j# X7 ?: J
- 10.0
5 `! X# ~8 z% T( V( ?1 d - >>> math.fsum((1,2,3,4))
0 A- s6 r: j/ f" X% ^ - 10.0
- x& E; ~6 V m1 ]- c1 } Y - >>> math.fsum((-1,-2,-3,-4))5 y( G! s9 x2 V6 p2 b, Y
- -10.0
- Q/ N5 t; U) T - >>> math.fsum([-1,-2,-3,-4])& H0 v( a9 S2 b' n. G
- -10.0
复制代码 ! |$ C9 m+ A) N! ?# b% Z
math.gcd(x,y) 返回x和y的最大公约数: h, T4 o& E. G9 K8 B3 L
- #返回x和y的最大公约数( L. b6 }+ A8 c# n' U
- gcd(x, y) -> int
- A& A9 { P) q. y7 B - greatest common divisor of x and y
+ Z" n- ~/ S! G) h - >>> math.gcd(8,6)
+ ^6 Z. r$ F! H2 g - 2; v4 a; L, V/ o8 _
- >>> math.gcd(40,20)" N: i) l+ y' b. J1 C M, U4 b
- 20
8 F* Z% x) s; M8 n- g - >>> math.gcd(8,12)
+ E) V: B. O5 p. X7 l a+ I - 4
复制代码
% C- Q' n: O& K P! s8 zmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
, ?7 P; \5 S3 r' S6 G- #得到(x**2+y**2),平方的值
G, L& J$ P) [% |$ v - hypot(x, y)1 S) u% {$ E( @. S6 }3 L6 P4 E* v
- Return the Euclidean distance, sqrt(x*x + y*y).
/ q% m5 A% J; V - >>> math.hypot(3,4)
6 m0 l# S. ~/ N' U) E - 5.0. e7 }* | ~: s7 g A
- >>> math.hypot(6,8)& m! k9 H& W- p2 e _- b' N' G* t
- 10.0
复制代码 7 s7 O* H. r! W8 K9 F
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False# X# B: X t% } t8 Y+ i% r5 p
- #如果x是不是无穷大的数字,则返回True,否则返回False9 p% g3 h R3 i
- isfinite(x) -> bool& P" z+ U5 Y) H( u9 }; [
- Return True if x is neither an infinity nor a NaN, and False otherwise.
+ N; j8 O# i& A% t0 {; @ - >>> math.isfinite(100)" _7 V0 @# j: s* X
- True
4 W* R. k! O9 e5 m - >>> math.isfinite(0)- }, c% E- k& t S1 ?+ K& @
- True4 _+ x4 s, I+ p; d
- >>> math.isfinite(0.1)
6 z9 z9 L9 Y: }7 t& n" H" ~% Q+ h - True4 ^% U% |, Z$ f; h9 P, Z
- >>> math.isfinite("a")
8 d( A& `, ^1 B7 h. Z4 `0 K) V3 t - >>> math.isfinite(0.0001)! u/ o" k5 u2 C D- o
- True
复制代码
. }1 G6 A& \0 W0 w9 N9 K: R$ i) Omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! Q) O5 E4 N5 e' E1 u- #如果x是正无穷大或负无穷大,则返回True,否则返回False2 E# l9 U/ Z9 C h: F/ H- Y, g
- isinf(x) -> bool
! f Y# y4 V! B, K% h - Return True if x is a positive or negative infinity, and False otherwise.
% y3 {9 k2 j$ W8 y$ p$ L - >>> math.isinf(234); t! `/ K+ S4 v: B
- False
" P6 N" n; o; H+ P% ?6 e2 c* l - >>> math.isinf(0.1)
( i* G7 x& B1 ~4 ~: e - False
复制代码
j8 Z! T1 b Z$ c% c; Omath.isnan(x) 如果x不是数字True,否则返回False! t# A& y8 C/ F- I! @
- #如果x不是数字True,否则返回False. C4 j8 o; s- o& r
- isnan(x) -> bool8 V+ R' Y$ k0 @3 H* T
- Return True if x is a NaN (not a number), and False otherwise.* d3 I& V/ |+ x1 b8 x
- >>> math.isnan(23)
& t/ P% @, j1 T) b9 G5 Q6 a- ] - False& b ~+ p! U' B8 J) k
- >>> math.isnan(0.01)
9 J, V3 v" p% U( o7 z' _ - False
复制代码
# |+ V" {- {! t5 e) u" d" Vmath.ldexp(x,i) 返回x*(2**i)的值; ?; F' q) k1 y( }; _9 C7 p
- #返回x*(2**i)的值
6 V4 P! f* y1 u - ldexp(x, i)/ G3 ^+ V, ~5 r" q; s; \: C
- Return x * (2**i).
8 f8 Y) u# p/ C% v; j - >>> math.ldexp(5,5)
5 p2 ]- f0 n. R+ d6 v r - 160.00 ?( ~+ M( x6 g2 g* q
- >>> math.ldexp(3,5)
1 l4 g/ D; u5 f$ b" l; o, {2 |) m" M - 96.0
复制代码 * v5 d1 c7 m" p7 E( T+ L* F
math.log10(x) 返回x的以10为底的对数5 ^( h, q6 D, T. v {. E
- #返回x的以10为底的对数6 `3 a7 @3 x8 Y6 X9 _
- log10(x)9 [( g3 I5 `7 C2 ?8 W
- Return the base 10 logarithm of x.: T. f3 P/ V9 U3 T
- >>> math.log10(10). P: i) x0 X7 Z0 i; f2 y# Q
- 1.07 @$ \$ w1 t1 n5 x& x% v- d
- >>> math.log10(100)) `# {9 L: [; ~; S3 b
- 2.0% O8 o; t% k9 e
- #即10的1.3次方的结果为20& m" m+ n Y3 b4 E- ?
- >>> math.log10(20)- c% u# v4 p( q! e( Q
- 1.3010299956639813
复制代码
8 H a, S: M+ c6 x$ Z9 Nmath.log1p(x) 返回x+1的自然对数(基数为e)的值
- r/ u: p7 S# t7 h; K- o- #返回x+1的自然对数(基数为e)的值
0 o; g% ~6 T' _: I8 @8 P9 L - log1p(x)/ S5 Z) S. F% S c
- Return the natural logarithm of 1+x (base e).
* g6 w; [0 ]7 X3 n: d9 g& u - The result is computed in a way which is accurate for x near zero.+ }* I+ e& [5 _" g
- >>> math.log(10)* S# U8 G4 w: h+ R. l
- 2.302585092994046
/ \) ?, O1 K2 s% _) Q - >>> math.log1p(10)! n5 s. G5 W/ O% ~9 a
- 2.3978952727983707
* C1 d" J( o" b3 `, L0 j6 @- b* E - >>> math.log(11)
( Q" e1 p' d8 p% |4 g) D - 2.3978952727983707
复制代码
: n: d, F( q6 ^1 emath.log2(x) 返回x的基2对数) L* W: H& Z* K9 W$ v) k# S x! }
- #返回x的基2对数0 m1 v, ?' j3 X0 o. W
- log2(x)
4 O) {- c* R3 d" g - Return the base 2 logarithm of x.
; E4 @ q# n& k2 T3 ^ - >>> math.log2(32)
. M* H8 @# u' P' }' p - 5.0
4 U& e; a. P# n. u - >>> math.log2(20)9 b, D: J5 C3 i" d0 [' r( g# G, u
- 4.3219280948873637 n. Y) C0 K# R$ S4 A" f
- >>> math.log2(16)- i- O3 N @+ _$ E! S( p8 T$ a
- 4.0
复制代码 # C" \7 z q; H1 r [5 C. A
math.modf(x) 返回由x的小数部分和整数部分组成的元组7 V; y/ j! ]5 V2 X6 }9 C8 I7 E8 j
- #返回由x的小数部分和整数部分组成的元组
3 T5 m! ?* X$ N* n8 h4 b - modf(x)! o, a: a, X1 }; o' p2 Q
- Return the fractional and integer parts of x. Both results carry the sign6 q2 M$ \ W$ X0 |8 D: U" e
- of x and are floats.
3 C5 ^5 h5 g. n' [0 x - >>> math.modf(math.pi)7 P6 K( F& |( ~! x- I
- (0.14159265358979312, 3.0)- S0 q1 T! K {3 U0 C s
- >>> math.modf(12.34). }+ V; Y+ R5 L9 a5 h/ w* b& J1 R
- (0.33999999999999986, 12.0)
复制代码
& f' A- K# ^' ?1 H0 j# u$ Pmath.sqrt(x) 求x的平方根- W1 ]! m) s- P* g1 w. Z( c* C
- #求x的平方根+ n6 I* ]; P8 c$ _4 R: F3 x: I. r
- sqrt(x)
7 t* W- i- w; {4 d - Return the square root of x.
( a: ~% Y6 s6 X7 g( } - >>> math.sqrt(100)
6 V4 ^# v; M' ^6 G4 {$ D - 10.0. M% ~; \$ A$ e6 l( c# M; o
- >>> math.sqrt(16)
* s4 [, U1 h, G. I. w - 4.0
! l6 ?0 R$ G" B/ [& y) n( S' G - >>> math.sqrt(20)5 P$ j; C r8 |
- 4.47213595499958
复制代码 * E3 _! A7 R3 h( T
math.trunc(x) 返回x的整数部分- #返回x的整数部分
. x" J7 C( k% W. u3 [) J& s - trunc(x:Real) -> Integral
( B4 }# c, P9 P - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
" o: z, u4 ~$ r. E. ]( G6 \ - >>> math.trunc(6.789)/ Q8 E' P! U2 {
- 6
# m. P( Z% F& g+ k+ H7 ~6 v6 }, S2 V7 p - >>> math.trunc(math.pi)( y' o2 l5 Y3 _. O4 _
- 3
, g1 o. p+ |% C2 s" B - >>> math.trunc(2.567)0 V1 `/ F! |) y2 k5 O) l
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|