马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! ^4 X3 J6 n! w8 B; A& T【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, q# Z5 U# d! L6 Z, ]3 T& n& @0 R& q6 z# @ ?: h
方法1:. P8 n0 @8 i L% [0 s
- >>> import math
5 G7 ]' M$ e) X! m/ _ - >>> math.sqrt(9)% s! Z" e2 ^0 C: b2 T
- 3.0
复制代码 方法2:! ? X; M% T# v
- >>> from math import sqrt$ u3 L2 L; F# M. N4 N( _
- >>> sqrt(9)
4 s1 W( f' N' W' [% Q - 3.0
复制代码 4 Q: r+ Q# B) [% m, H$ |9 c6 I# }3 ^
- f& N* U# d' H1 L. Q1 I
math.e 表示一个常量6 {* p: }; v1 f# M! J# y5 a
- #表示一个常量+ }# G% v0 i8 U F
- >>> math.e& k5 [2 u2 z# t- T. R' I1 Q
- 2.718281828459045
复制代码 + j+ t/ G! _. H# D1 L& @ i
math.pi 数字常量,圆周率
( j ?2 D! P4 M+ g6 t2 V8 y+ j- #数字常量,圆周率. X' T; N# z7 C: }( Q* |
- >>> print(math.pi), @; a! t! l3 |3 N
- 3.141592653589793
复制代码 ( W% K' O$ v6 j6 W; j- Y3 ]# \% Z
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x. A8 k% \; y, ^
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 H+ B7 ?) R) U- }) m$ |2 \1 w4 E+ B) ? - ceil(x)9 N- O3 ^+ D$ `- w
- Return the ceiling of x as an int./ H1 H( z. \3 @9 x x. u
- This is the smallest integral value >= x.2 j, M8 }/ T$ U
- + ]: }5 P$ N' q
- >>> math.ceil(4.01)
7 y# k$ I3 ]2 p, T/ r& E - 5
/ `; c; s; q3 I1 N - >>> math.ceil(4.99)) m, m* a6 ]9 U0 ~+ Y2 Z f: ~' h( h
- 5+ s7 [. ^* \" G6 g
- >>> math.ceil(-3.99)
8 B5 o, V, E% |3 ~1 C - -3
6 ]8 p5 U5 K3 v! e1 E) x5 t - >>> math.ceil(-3.01)$ L4 y4 W- h; a. U" d
- -3
复制代码 % i- R# Q6 I. f0 C3 n/ _
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 Z6 |, J* b: |/ x) Y) d
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身! A. E. |* e2 \
- floor(x)0 }1 }6 k( K- n. e# Y
- Return the floor of x as an int.
. Y6 A( _$ i: F! t' ~, J - This is the largest integral value <= x.& k+ W) U2 a. ^& }8 {' d/ o
- >>> math.floor(4.1)5 Z- c- B3 l4 C# z: \8 T
- 4% T s3 e9 x" B; J
- >>> math.floor(4.999)1 y$ j6 U: T# M$ @
- 4
) Q+ @% n) {5 o$ h4 Q+ D% H' L - >>> math.floor(-4.999)2 H) P: s6 {6 G+ k5 {& a0 c
- -5
+ M+ ]" Z: p. h - >>> math.floor(-4.01)/ m& S, I# Q* V1 q) t+ x6 I9 T# G
- -5
复制代码
- e# F7 A- o7 o, X% M4 |4 |( dmath.pow(x,y) 返回x的y次方,即x**y
" o: s9 i) [- i% C( V4 D- #返回x的y次方,即x**y0 J2 I( V: t6 B( T# e! c
- pow(x, y)! H; q# q5 Q' y( A. t0 m C2 E! z
- Return x**y (x to the power of y).
9 [+ j( _" }9 B$ t: r! P6 r- q( r - >>> math.pow(3,4)
! N/ R6 r- U; U7 { - 81.0' J# ~; R4 U2 T6 w1 p9 c+ r
- >>>
1 v w. S% I. @; N: a - >>> math.pow(2,7)
# t: }1 G0 `: W( i6 ?1 n - 128.0
复制代码
1 b5 z# y0 S1 t! ? T, V! G* Hmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 h$ d3 d5 s. q1 @- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- {7 p* e) L/ e- q$ ~8 |7 f( n& a5 C
- log(x[, base]), @& H9 J, b2 C$ q# j$ Y
- Return the logarithm of x to the given base.
2 O) \* \8 m6 e; ], [$ P - If the base not specified, returns the natural logarithm (base e) of x.
6 x5 m* w+ `7 |' L9 j1 j - >>> math.log(10)4 ~4 D) O, _3 Y8 f1 d: A$ J
- 2.302585092994046
! p0 q4 r: R! _( ]' b& X& a - >>> math.log(11)
0 U# p; F. R7 I: E- ~! i - 2.3978952727983707: m! A9 U" N" Y. ]4 P& p. [9 B& C, D) d
- >>> math.log(20)6 L) a1 E2 k! I O2 m
- 2.995732273553991
复制代码 ' U3 W( T* A. B% Z0 X0 ?1 d
math.sin(x) 求x(x为弧度)的正弦值0 b+ ~* M4 {* Q
- #求x(x为弧度)的正弦值
) N( F# h1 Q# X7 Y: ]- G - sin(x)
# ]1 ?! w, G/ d I2 W! |% z - Return the sine of x (measured in radians).
+ b1 Z" o: _8 m9 S$ i+ R; {' s - >>> math.sin(math.pi/4)0 [+ I& c/ i2 z* n3 G ?9 H) B& x
- 0.7071067811865475$ V, J* i5 y1 a. E3 T
- >>> math.sin(math.pi/2)
. H/ t1 G# B* ?0 E - 1.0
( P" E6 W+ [* L) c+ ~" U - >>> math.sin(math.pi/3)' G0 ?, T5 q j7 A2 O8 P c% |
- 0.8660254037844386
复制代码 ) l. M+ E2 {& P
math.cos(x) 求x的余弦,x必须是弧度% p6 _& g3 w7 l7 R8 ?
- #求x的余弦,x必须是弧度 ?8 M E1 R9 [
- cos(x)
7 |! W b9 \+ U5 } v n8 u - Return the cosine of x (measured in radians).3 n: I0 \+ f6 d4 u
- #math.pi/4表示弧度,转换成角度为45度
+ k. B$ @( L9 }( p! j - >>> math.cos(math.pi/4)- p$ }" v4 N) G0 g, [
- 0.7071067811865476' e* r4 e6 N8 k- [. J* V; B
- math.pi/3表示弧度,转换成角度为60度
; C, }8 M$ }0 T3 I - >>> math.cos(math.pi/3)
( h2 w8 F0 ^6 m4 i1 ~( f5 K; `* w2 W - 0.5000000000000001
. f! K) e8 ]2 v6 V/ O/ Z: J - math.pi/6表示弧度,转换成角度为30度
1 P+ a+ ~& ?, F - >>> math.cos(math.pi/6)! `5 `" T* r" q( }) T1 w9 `4 c1 n
- 0.8660254037844387
复制代码 # C6 g6 \5 K q
math.tan(x) 返回x(x为弧度)的正切值' R1 X ^# R6 Z, h: `/ U* T
- #返回x(x为弧度)的正切值/ ~( Y1 H7 }/ a. M8 w2 b7 K5 f3 ~) h
- tan(x)
) P& [ m9 s# Z6 y+ J* S; ~ - Return the tangent of x (measured in radians).
% H7 @" [2 [+ Q" {3 J - >>> math.tan(math.pi/4)9 i0 l1 F/ \8 Y- w3 B
- 0.9999999999999999
/ ?3 K* P& e3 x/ L9 ~: u/ O - >>> math.tan(math.pi/6)
; I. t& ?! J( M3 p: O4 N - 0.5773502691896257- w2 y: a. b7 e
- >>> math.tan(math.pi/3)
( i& X. E8 o2 k4 P' z, e: H O7 x2 w - 1.7320508075688767
复制代码
, y- }5 @8 o/ m j' O. ]math.degrees(x) 把x从弧度转换成角度
. a" r5 E. x; r7 \8 \2 T! T- H. s- #把x从弧度转换成角度; ?' M5 k6 Z u1 j0 e/ V5 _) j
- degrees(x)
# J% q; J$ `9 K+ _8 X* ]8 _ - Convert angle x from radians to degrees.4 r; n+ H% w6 H G$ V$ J
- }0 {% H0 L9 k; I: r4 i2 h% e o, s- >>> math.degrees(math.pi/4)$ a* ~/ D- b' O6 U4 M1 d
- 45.0
' O' X D, z# s, a2 g6 e - >>> math.degrees(math.pi)5 U4 W5 T) b8 p6 ?0 u; Q. r5 A' d
- 180.0
0 s4 c; q! C. s6 ]/ L# ~ - >>> math.degrees(math.pi/6)
" o# h: A6 l6 r+ y - 29.999999999999996
- D# ~4 w0 S" K# t+ I8 C: Z - >>> math.degrees(math.pi/3), y1 X; ]* X3 p
- 59.99999999999999
复制代码 ! A9 Q: ~, R+ F2 w
math.radians(x) 把角度x转换成弧度& f6 j7 G, s) h* |* T
- #把角度x转换成弧度: w# \. }' Y& Y/ Z% { m( k2 v
- radians(x)" } ^. A2 }1 \8 L, q$ w
- Convert angle x from degrees to radians.* I& @' ?2 {! x7 n
- >>> math.radians(45)
6 Q) D6 Y; c7 e; S# L* q. d - 0.7853981633974483* y' n z7 ~9 Q6 @! M
- >>> math.radians(60)( s0 f+ [0 }3 a! ^
- 1.0471975511965976
复制代码 ! V4 h, W' P- T" k' T e
math.copysign(x,y) 把y的正负号加到x前面,可以使用0' M! r) a4 F( O3 Q
- #把y的正负号加到x前面,可以使用0% X! q' L, c6 |0 `1 Y% U
- copysign(x, y)/ v8 t6 ~) ?" A! [
- Return a float with the magnitude (absolute value) of x but the sign ; s& x" p& Q" B
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
9 U0 u- s8 b3 |( d+ A, L) L - returns -1.0.5 _2 ?! i$ Q' y* a# p( ~0 @$ ^9 U6 c
- k0 g7 y& [/ v$ X/ j* V9 t5 z- >>> math.copysign(2,3)! G( s, T' e$ G
- 2.00 o' p* A% X# e( q
- >>> math.copysign(2,-3)
* _& D: n( k l6 |- L. ~3 { - -2.0
- \2 l% V# W7 `: P - >>> math.copysign(3,8)
) u8 O/ }5 K6 ~! } - 3.0
^- r! Q) T2 P% c' l4 J - >>> math.copysign(3,-8)
. _$ T* f: _# K& u* a/ P' F5 y% g# r - -3.0
复制代码
( z6 j5 j; L z6 m8 `math.exp(x) 返回math.e,也就是2.71828的x次方! y/ X1 d- j. l; X8 u
- #返回math.e,也就是2.71828的x次方
" @# W5 I) z* l, { - exp(x)
H, C& B( l$ a0 G; p, u6 k2 S5 V - Return e raised to the power of x.$ O7 c1 |7 z9 I% r! D' v
* D% e* ^- c' s, ?+ |) X- >>> math.exp(1)" |4 a, U- e0 Z6 ^/ V
- 2.718281828459045" ~! A+ x: f3 z$ f( O8 z- o4 L
- >>> math.exp(2)/ \( h- _1 h+ b* B; } p+ b
- 7.38905609893065. P2 h7 u0 u# p* U& ^
- >>> math.exp(3)- ~. r; k* h2 r: C: D0 k
- 20.085536923187668
复制代码 ) S3 X8 z" }. Q( p0 X
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
& L* ^: B* ], f" }9 F- #返回math.e的x(其值为2.71828)次方的值减1+ i" E$ H3 ~8 l2 F6 ?
- expm1(x)
) ?- A& O/ P8 ]9 V" X' Q9 Y - Return exp(x)-1.3 B, `/ o$ n9 |' |9 z
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 E8 r, S( \- @; v0 Y
/ h$ ]' U9 |3 q u* L- >>> math.expm1(1)* L; A# j5 c. f
- 1.718281828459045
8 k1 P4 X3 b* q' t - >>> math.expm1(2)
& g; d( s1 K# C: B - 6.38905609893065
+ b! \9 M5 D3 S) p - >>> math.expm1(3)
, _( R* z! ?0 Z5 K$ F - 19.085536923187668
复制代码
8 b' J( s+ i0 e" N: z5 N3 kmath.fabs(x) 返回x的绝对值
. m' T9 E; \8 |* V; \- #返回x的绝对值7 g9 h! Q0 |- i. M+ ~4 I
- fabs(x)/ F+ J5 ^ f: b
- Return the absolute value of the float x.
9 v: u8 Q' s b+ Y& k( ^ G
$ O7 O* @, `2 X4 ]8 H" |- >>> math.fabs(-0.003)
9 R8 o: m# W1 @% x- l+ P- c! Z( A; j - 0.003
% B5 {( [$ }0 @ - >>> math.fabs(-110)
- C1 D4 I( J# z1 k' _ - 110.0
5 x* |( o. N5 y0 x* i) l* R - >>> math.fabs(100)
6 u/ S& V& _, L7 \ - 100.0
复制代码 v" P; b# f$ d' I0 @
math.factorial(x) 取x的阶乘的值3 [" a4 z d& l) Q
- #取x的阶乘的值0 e+ R I, [; S( C; Z. r8 O
- factorial(x) -> Integral3 ~9 v& m0 y: w a& ?2 h
- Find x!. Raise a ValueError if x is negative or non-integral.! h9 P" G! b ]4 u1 }$ T3 L: b" n
- >>> math.factorial(1)
5 e4 K8 ~5 k4 v2 o2 } - 1
! q# }, {5 [2 w& C$ A: C6 T - >>> math.factorial(2)3 {- m, o$ y2 `8 ]# V7 |) \" } @
- 2% u# r# r/ K1 p& ~, P3 [/ d4 m9 T
- >>> math.factorial(3); {5 Q1 \& Z, X: k6 L; N9 t9 a! _
- 6
: W2 l8 {2 H( w2 b, M Z3 v' H* P - >>> math.factorial(5)) q8 @; ]/ }) f+ Z- s" |' s% }3 k
- 120
3 C# R" r k2 ` - >>> math.factorial(10)
1 S. B) w6 N6 R# z/ e- C* I - 3628800
复制代码
: ^! z- e' x; S9 `math.fmod(x,y) 得到x/y的余数,其值是一个浮点数4 R2 h/ {4 ^7 P1 Z) T0 b
- #得到x/y的余数,其值是一个浮点数
y& [" c4 [. O( ^ - fmod(x, y)0 E) l3 J: S6 s# P# S$ {/ F
- Return fmod(x, y), according to platform C. x % y may differ.
* ~: m, A* @+ w p - >>> math.fmod(20,3)0 h0 W$ e( Y; e+ |
- 2.0
8 W1 p, w7 q- a - >>> math.fmod(20,7)& z$ D+ W% O' R
- 6.0
复制代码 4 R! k# n1 U* J( {% e: C5 u
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
1 C6 V8 @% _! G! b, l; D- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 E) @8 ? D: H
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 L/ E) I- J1 K0 _' J/ t
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ j; G5 c R4 C8 v - frexp(x)3 _# k4 W+ {( \7 F1 V. x* O; o
- Return the mantissa and exponent of x, as pair (m, e).
) B+ U3 O e7 q - m is a float and e is an int, such that x = m * 2.**e.
$ X! i- l: V/ M( s, x1 T( i" R6 W0 Y" s - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
/ ]4 T m2 |+ Y$ _7 I - >>> math.frexp(10)
( y4 V6 t# Y4 x - (0.625, 4)
( _- v& ]+ [# Q& \ - >>> math.frexp(75) C r1 w1 G) z- {2 C
- (0.5859375, 7)
0 D- s3 \* \0 G4 }; F/ c, Z - >>> math.frexp(-40)
) t% j1 h" K, C - (-0.625, 6) R; T5 P: |2 }5 u
- >>> math.frexp(-100)
1 s( E' H& H h" w+ {) M - (-0.78125, 7)
5 `6 p$ q5 K8 n1 G* E' X9 f - >>> math.frexp(100)4 E5 b3 H9 P6 \9 C1 ~! I5 f4 r/ ?
- (0.78125, 7)
复制代码 # i: K8 \: p/ s( `! j, p6 P
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
* u$ e, N# J. k& `3 `. E! X. F- #对迭代器里的每个元素进行求和操作" \. S5 @. d3 P. H6 C4 T- \2 v) o
- fsum(iterable); `4 K; |8 J E" e
- Return an accurate floating point sum of values in the iterable.
6 y& a/ u4 x7 D% E0 x% ?, e k - Assumes IEEE-754 floating point arithmetic.* C; F! t! S# S' O
- >>> math.fsum([1,2,3,4])
" G4 N9 T; \, F4 i0 K4 h% b - 10.0* P: T" r0 K4 p
- >>> math.fsum((1,2,3,4))
0 M/ M1 ?' k! P0 m7 k. k% t - 10.0
' m/ A3 S ]& ~$ B# e9 s: W1 J: i - >>> math.fsum((-1,-2,-3,-4))
( D, |" X$ b/ A1 f# j) q) e - -10.0
: s3 ]( l! w* v: y* F - >>> math.fsum([-1,-2,-3,-4])
3 t" ?8 f, Y m" V2 u# b% x3 \0 o: b - -10.0
复制代码
6 N( H1 S1 J) K8 Cmath.gcd(x,y) 返回x和y的最大公约数7 ~) ~5 l R/ e9 o6 D8 w
- #返回x和y的最大公约数. O5 L- @1 H% A" U. V
- gcd(x, y) -> int
1 U) l! a+ e% _' g! {$ Z! X( U+ n - greatest common divisor of x and y
: L. O8 J5 ~" U$ K - >>> math.gcd(8,6)
" u( r$ S& U( m" Y - 22 C9 q2 _4 o) } z3 E
- >>> math.gcd(40,20)1 v0 Q! ^, [) V0 D8 t: w
- 207 j3 F# R+ J" S+ z8 V; P
- >>> math.gcd(8,12)
5 C+ y5 N" b* s" F - 4
复制代码
" C( e8 p8 t0 A4 Imath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False! {) u. X) b0 ~
- #得到(x**2+y**2),平方的值' R& ^3 ]3 Q6 |( [ J+ r7 t! J# z
- hypot(x, y)
& U; W. ?, R# O+ _* _0 u6 y8 K - Return the Euclidean distance, sqrt(x*x + y*y).
" e% ^% ] U) [ x. B. E5 A& c - >>> math.hypot(3,4)
9 \+ U- g+ A$ b$ g q% S - 5.0" f+ B7 @# Z: X5 o t
- >>> math.hypot(6,8)# B4 d" ?7 Q% F v
- 10.0
复制代码 [8 d! I5 m& Y. c. c3 Q5 @, X
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False3 O% R, A# c8 j. J
- #如果x是不是无穷大的数字,则返回True,否则返回False! @+ @, M5 v- f. N% W4 ~8 t" p: ]
- isfinite(x) -> bool
, y8 ?. y3 ^+ G2 ~2 k A - Return True if x is neither an infinity nor a NaN, and False otherwise.
. B7 K* M6 C/ E& o7 F, G - >>> math.isfinite(100)) v3 }6 l) n3 h" ?- C4 k
- True5 U" R2 a w+ U/ `% S6 H* y) G
- >>> math.isfinite(0)
4 K1 L* a; J: M; x# G9 n5 B9 } - True
, g% H# ~- l6 n n* P& S/ \; Z - >>> math.isfinite(0.1)% T4 m4 [9 w1 {" T% a, ?5 N, H+ U
- True! F6 I Q0 i, |: j% N% n
- >>> math.isfinite("a")
. q+ ?/ y$ S' T# _: @9 X, C# E) o - >>> math.isfinite(0.0001), S1 J- `* p- q2 o' r) Y: {% s3 q
- True
复制代码 ( H" Q5 p7 \1 P5 c, P
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
6 T' b( J6 Y1 }* A m3 l- #如果x是正无穷大或负无穷大,则返回True,否则返回False+ h2 v; Q( Z: b
- isinf(x) -> bool3 r; y) d# R* g4 U! R t
- Return True if x is a positive or negative infinity, and False otherwise.
# L M0 x& S0 _( V9 W - >>> math.isinf(234)
, q& V% u. Q1 q8 G; V" h - False/ g. L/ E) l/ {; n' {
- >>> math.isinf(0.1)1 m6 k/ Y" l7 Y0 I! m4 C N0 t
- False
复制代码
. X, ]. q% \% V* _- k- T8 Dmath.isnan(x) 如果x不是数字True,否则返回False' I, K& G j3 }9 f6 I" d8 r& G |
- #如果x不是数字True,否则返回False
% h6 f6 U g% [# } - isnan(x) -> bool4 [/ X/ L& C6 _) s4 z0 s
- Return True if x is a NaN (not a number), and False otherwise.! y6 q0 A* V# {$ [3 {
- >>> math.isnan(23)% E3 G" j7 C$ d0 ~" y% y& s
- False4 A) G% u$ X c9 s- f
- >>> math.isnan(0.01)' X* R: ~: i7 G! o$ B$ y
- False
复制代码 % ^' E+ ?- E6 O/ G
math.ldexp(x,i) 返回x*(2**i)的值+ K8 O5 j* i& k+ g5 ]: \2 P( B
- #返回x*(2**i)的值: p( _) j2 C' Y- ]' }8 g0 i
- ldexp(x, i)
. V. ~$ d- s* j' U" y8 z - Return x * (2**i).3 n7 Y1 u2 \: q+ ]! b+ B; P w
- >>> math.ldexp(5,5)
# k) C1 w" J& ~4 w( S4 L - 160.00 j: Q0 q) P6 r
- >>> math.ldexp(3,5)
6 l3 F0 j5 {/ G7 N - 96.0
复制代码
' C" y6 b/ F; A+ ?math.log10(x) 返回x的以10为底的对数. Y; U+ H2 p2 f) r2 \8 A
- #返回x的以10为底的对数
# y! j2 j) e. K8 U& A3 ?' [' s - log10(x)$ y) I, t% S) w
- Return the base 10 logarithm of x.
/ j; B; M5 L1 X* R/ ~: v0 K' ]! ~ - >>> math.log10(10); n9 r$ p! G* C# u' Z6 Q
- 1.0
' d7 }! F, U0 ~$ |4 H9 k$ ] - >>> math.log10(100)
& v% E/ l% J7 p+ ]. s, r - 2.0
" s; B L# G. j. J1 c - #即10的1.3次方的结果为207 u, Q$ E% P1 v* _
- >>> math.log10(20) z) I1 t7 j" \( S
- 1.3010299956639813
复制代码 8 y; x5 o+ L1 P r
math.log1p(x) 返回x+1的自然对数(基数为e)的值
& ~ F; m! o7 J) H" {: j2 W- j- #返回x+1的自然对数(基数为e)的值
: A$ ~& P: A% z5 L3 m0 T - log1p(x)! d* u; t' L2 H0 N' `( b( v3 N
- Return the natural logarithm of 1+x (base e).
* C, G% n. N; o3 a+ |! Q5 D% c8 P - The result is computed in a way which is accurate for x near zero.5 E0 c; _, z& t: l
- >>> math.log(10)
; S8 v# j; B8 }! P+ o - 2.302585092994046
* I+ r: @% G% |4 ?' d } - >>> math.log1p(10)5 }1 _6 z# R- q1 n! [; W% D S
- 2.39789527279837077 V7 s3 M ?2 W0 z/ l
- >>> math.log(11)! l2 m" A& ]+ {/ Z3 G' g9 U7 `
- 2.3978952727983707
复制代码 . ~$ T* n2 e% s. l
math.log2(x) 返回x的基2对数
# k: }: x4 J( u7 N B( M- #返回x的基2对数. S. r9 y) W4 n" z; `
- log2(x)+ d+ y& A5 B& F& t" v# A
- Return the base 2 logarithm of x.6 _) d7 R d( w) V7 c& q! {
- >>> math.log2(32)* D7 p* a6 S6 Y6 f
- 5.0
4 o: O6 f( l8 F7 h, T/ N' K8 ? - >>> math.log2(20)
0 I F( ^6 P3 y# D; p* J; q8 }* e - 4.3219280948873631 i! Z: y# _! S9 @
- >>> math.log2(16)2 E; n4 w3 r9 o% r
- 4.0
复制代码 ( H5 t' T# h; p9 \5 U/ U
math.modf(x) 返回由x的小数部分和整数部分组成的元组 D2 F3 k7 F3 D: K- n# k3 q; m
- #返回由x的小数部分和整数部分组成的元组8 }8 U7 }# v6 g2 _$ M
- modf(x)7 V% E" L; B: ]" x
- Return the fractional and integer parts of x. Both results carry the sign. }: }* ~7 `" T$ V
- of x and are floats.7 p9 J" b% M' t
- >>> math.modf(math.pi)
9 d+ W* a+ ~6 p, V. N( s$ d; z3 ? - (0.14159265358979312, 3.0)
3 V- f9 R3 T/ P% j! b - >>> math.modf(12.34)
! V5 p5 b2 e. b# B - (0.33999999999999986, 12.0)
复制代码
' w8 R6 \( \1 i% H) u3 K6 G9 Vmath.sqrt(x) 求x的平方根6 A' j% h8 v: G. Z0 w0 I
- #求x的平方根
$ N' ]- d) Y v( w3 i7 t7 f7 \ - sqrt(x)5 S/ z4 H0 X1 k7 T4 p
- Return the square root of x.
7 Z+ P1 \6 h0 l: n3 a - >>> math.sqrt(100)
; A" a4 Y$ y$ h* B( j# s' S - 10.08 O! k* ^! s: U- v J0 b; K
- >>> math.sqrt(16)
e3 X0 o: o- J& F - 4.0' e! M' P0 S z8 R
- >>> math.sqrt(20)
4 k5 g5 e' w& R5 ?# V' E - 4.47213595499958
复制代码
Y7 T# `* j. I7 T+ omath.trunc(x) 返回x的整数部分- #返回x的整数部分
0 A4 x0 J3 |8 _' ~/ _ - trunc(x:Real) -> Integral
[6 d8 `! H# A6 ~$ F - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
. M! Y8 g k! Z - >>> math.trunc(6.789)5 _( |: r: A8 o5 _: q0 ^6 s
- 6
6 H6 v; `% @ ~! n0 A- a# A" J - >>> math.trunc(math.pi)
8 Q J8 ?9 Q; x) o - 3/ ?. E- r2 ?& t& I2 Y+ M
- >>> math.trunc(2.567)
) j+ P+ t4 s& `( @. I- N( e - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|