马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 H) C1 t' G/ L* ^) i+ W. y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& Q4 R7 I3 x+ i, K8 Q$ K
' y: p6 `$ Z i+ h方法1:) R# g7 h/ l' Z% T5 P- S0 S' v! U
- >>> import math; M+ G: o0 y* A0 ]4 Y, A
- >>> math.sqrt(9)
) D3 y& i7 r- X* b - 3.0
复制代码 方法2:& G! Z! y3 d( @% `
- >>> from math import sqrt' q3 G4 Q% c( P5 A* g) P/ b8 G9 E
- >>> sqrt(9); ^0 ?" p) u( d! r* k0 t3 z/ {* _ D; G
- 3.0
复制代码
5 h, y3 i- O) t/ ~; G0 H; M. r S
4 Y# x4 w* H1 v' ?! qmath.e 表示一个常量
7 O/ U5 H3 J/ ^, j- #表示一个常量5 a' Y4 C8 X3 y7 @$ F4 c7 o5 K
- >>> math.e
7 m; Q0 A m5 R! r* P. p1 ^% \ - 2.718281828459045
复制代码
* n# z: ?' N$ p$ Qmath.pi 数字常量,圆周率+ W! f) r/ B' O7 A/ B/ M1 U& ~
- #数字常量,圆周率
) h2 Y' |! r+ M. @5 u - >>> print(math.pi)
7 S$ C9 b2 ^1 F8 w8 {1 s - 3.141592653589793
复制代码 ; f; m* ^4 K% O" R! Z
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x- q- f( z. ]$ h
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ H# Y& v: W# A: \/ U
- ceil(x)
: p- A; k# ^0 | - Return the ceiling of x as an int.
" a. s0 P7 G, Y; U - This is the smallest integral value >= x.' u' d# [6 O4 Y4 E3 N2 H. k8 ?
- ' Y2 } j9 R( Y
- >>> math.ceil(4.01)1 R0 | i! m3 o
- 5
. N, E$ f6 x. d; ` - >>> math.ceil(4.99)5 Z% b, q1 A A' ~
- 5. @, |& t+ M- ~4 s h
- >>> math.ceil(-3.99)
6 e, A) z( t; b - -3
( ?, r) z L0 u, M, N - >>> math.ceil(-3.01)/ l4 ?8 i/ a6 Z7 e4 R
- -3
复制代码 $ c* m7 a2 N' r7 B* i" e* D
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身- e, C, `4 x/ g }% j& ^
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( ]! a% w3 z& ]$ K8 B: q - floor(x)' C: L6 X3 O; n4 m1 [) L* |
- Return the floor of x as an int.
, O+ d0 j0 o, Z N! l - This is the largest integral value <= x.
. z& E5 t) U: y& m. H. W8 U - >>> math.floor(4.1)
( P5 I& k# P' {3 v: _/ H/ Q+ n$ p - 45 }. ~9 Q# B& w }1 Y- ]
- >>> math.floor(4.999)
- H1 q3 ?( F1 C- _* D: K - 44 e: p7 \5 @5 i! i3 C) q
- >>> math.floor(-4.999)
( j% o0 r- J4 | - -5
4 u7 A1 f) Y" M: p) A$ e G - >>> math.floor(-4.01)
) C0 }7 K8 O7 ?4 ?1 V& f - -5
复制代码
' ]/ I: n, g4 [/ x7 `& [math.pow(x,y) 返回x的y次方,即x**y( ?) ^8 ~, P" A1 y( X' e R
- #返回x的y次方,即x**y
' h0 |$ ]2 A3 |9 e/ m- b' R - pow(x, y)
8 N5 I6 x0 O3 e) @( O! Z. I- C) S - Return x**y (x to the power of y).( l" Q6 ?1 y/ E& `& L7 A7 B: q; N
- >>> math.pow(3,4)* x) x! [" M# d0 `' w3 o4 Z2 H
- 81.0
( l, i. c! q. U+ f# F - >>>
: F4 C, p! ?+ @% R! R - >>> math.pow(2,7)1 N3 g2 H( B' C* l" [
- 128.0
复制代码
( O7 D$ H1 _8 a1 C: _6 l5 mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
. s; c0 M8 T" D3 R- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
5 m4 z. [0 S+ f2 w - log(x[, base])/ _9 d8 O- X9 y6 U# G2 P" V
- Return the logarithm of x to the given base.
' J7 I( g0 u& h Z2 T4 E' m' p - If the base not specified, returns the natural logarithm (base e) of x./ M9 C' G4 E: ~/ U) E
- >>> math.log(10)0 a" w( H0 Z3 _! [3 f- O4 D j/ R2 Q
- 2.302585092994046
* ~0 o ?% w3 `: S- `: r" ? - >>> math.log(11)
$ F" H! Q9 ?0 T# T! e) V8 s - 2.3978952727983707
! F/ ?# D: k6 N5 I - >>> math.log(20)
; @0 T0 d& {3 O3 p - 2.995732273553991
复制代码
$ i( Q. h) q0 {math.sin(x) 求x(x为弧度)的正弦值
% E' }1 V' |' d2 ^* T' K- #求x(x为弧度)的正弦值+ s- }, J* ^7 ?' [" C
- sin(x)
1 m" a& l+ E2 M- [ - Return the sine of x (measured in radians).
9 S0 w) k% N7 k - >>> math.sin(math.pi/4)' V0 R- M7 F: v/ u; X0 p% o, g( y
- 0.7071067811865475
4 k' c2 P& G( Z0 B - >>> math.sin(math.pi/2); o7 ^7 R- R( T* V$ v
- 1.0
' v7 w- r6 \4 N/ n& J - >>> math.sin(math.pi/3)
& t8 o$ n7 b- \3 E% Q+ ~ - 0.8660254037844386
复制代码 & W5 |! X4 d4 K! L' H% x% X; q! z0 e5 Y4 w
math.cos(x) 求x的余弦,x必须是弧度
1 z3 N7 \2 b# R& a! {- R6 o/ w- #求x的余弦,x必须是弧度
g0 P, v' V% ~, Q8 |3 i7 { - cos(x)$ Y9 B; `7 I1 @* K4 M6 A. l* ^ i
- Return the cosine of x (measured in radians).
' w5 L9 }% o w2 x' p - #math.pi/4表示弧度,转换成角度为45度7 |9 V5 N q! x! M) y
- >>> math.cos(math.pi/4)
7 K% m/ v# Q1 Q) u8 v3 D2 A - 0.7071067811865476
7 e+ @/ F2 B# z2 L: }; N - math.pi/3表示弧度,转换成角度为60度
$ M+ I" W2 Q" H0 r0 k - >>> math.cos(math.pi/3)
( f# z* Z7 c, }% u: U% A# } - 0.5000000000000001
6 d9 w- u6 j+ r' b - math.pi/6表示弧度,转换成角度为30度
* m2 r$ x, F! Y& @+ H - >>> math.cos(math.pi/6)
- ^- O# g' Q+ V0 N2 Y - 0.8660254037844387
复制代码 / ^& l! k- {; x, G$ n2 k/ g7 [7 V
math.tan(x) 返回x(x为弧度)的正切值
1 _6 [! d& @5 U' ^& L% F2 |- #返回x(x为弧度)的正切值
: z" P+ y( \# `# K9 g+ { - tan(x)
" A% Y Q; o8 V ~ - Return the tangent of x (measured in radians).
6 g, L$ } \4 f+ \$ I+ L - >>> math.tan(math.pi/4)* l% ^9 l$ h$ E, g$ R" H, d7 v
- 0.99999999999999991 G: M$ `" U! Z3 ^
- >>> math.tan(math.pi/6), L3 ]/ P/ A, F$ t1 h' |3 N. Z
- 0.5773502691896257
) H- \% z; |0 _9 C- q - >>> math.tan(math.pi/3)/ _: k5 Z) T8 I5 J8 O
- 1.7320508075688767
复制代码 ' a! {' X" G) h+ U7 G) J
math.degrees(x) 把x从弧度转换成角度) \2 ?; v6 C7 }+ c5 d
- #把x从弧度转换成角度
, V7 ^- U8 H0 z- N - degrees(x)7 r8 P1 |& ^ V) z, F8 _( I
- Convert angle x from radians to degrees.
- m4 ?! |3 J# O$ l3 Z' R - 2 U3 O2 |, }( K4 S4 |% R
- >>> math.degrees(math.pi/4)
3 {/ [/ K# I7 l4 m3 G# J1 S - 45.0
: d3 H6 N" U. d) c6 W& u( B6 W$ t - >>> math.degrees(math.pi)
' [+ v4 O* p( q# V' p - 180.0
4 }4 L4 B) F: G: e2 h - >>> math.degrees(math.pi/6)
- v G, W" t* B7 o1 d - 29.999999999999996+ c4 P' P6 Q0 }* l, D4 z0 y9 ~& M
- >>> math.degrees(math.pi/3), w, e1 @9 w' B* l/ v4 K
- 59.99999999999999
复制代码
; m2 q& S% m: t5 s* M: x* r. rmath.radians(x) 把角度x转换成弧度
+ C# @/ P3 b+ M- #把角度x转换成弧度
$ G( y5 |. O& x3 O0 l4 f, r4 t - radians(x)
! G3 b% ?! R6 I) V9 t - Convert angle x from degrees to radians.
( t$ l1 `$ w" R - >>> math.radians(45)% }( _8 f! q- p9 v; l4 r
- 0.7853981633974483
) F& s' `- T) d, p# l - >>> math.radians(60)
+ w7 O% Y( p) A: A1 t+ c* h+ i$ b6 P" V - 1.0471975511965976
复制代码
; K1 j/ r. ]( Z+ o( A- M) Rmath.copysign(x,y) 把y的正负号加到x前面,可以使用08 ]# h; x. i# z" M
- #把y的正负号加到x前面,可以使用0
) l' c9 I6 G( D2 b( a E - copysign(x, y)0 Y/ E. Y4 x$ \) s% q" b' Y) ]# l
- Return a float with the magnitude (absolute value) of x but the sign
. ^) [- s: m! ?; e - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
9 q, n5 a& Z6 I+ @ - returns -1.0.
4 ~4 k9 L0 U/ c2 e7 u) F7 w
( b* Q9 y U- o5 b$ H- >>> math.copysign(2,3)1 |4 V1 j$ ]' G l/ L
- 2.0
* r* T- P! N9 ?0 } - >>> math.copysign(2,-3)$ b: s7 E' j3 d2 V, W
- -2.0
& M- r. @ S Y# D( g - >>> math.copysign(3,8)4 [8 Y' X+ ?8 S: K; {8 e
- 3.0* p) N8 h- F N' Y$ W m/ S
- >>> math.copysign(3,-8)
6 {) K( j* ^ l( W - -3.0
复制代码 0 \/ R' }+ q& O3 K" @8 Y0 L% t
math.exp(x) 返回math.e,也就是2.71828的x次方& {1 W+ ]; k j/ E
- #返回math.e,也就是2.71828的x次方
! u+ r# c$ U) T7 p4 J4 S8 k - exp(x)1 }" m7 {, X" I) e
- Return e raised to the power of x.
! Z, l, ~6 l( @# ?0 e. N; g: G+ F
* B: p7 [/ q/ Z- >>> math.exp(1)$ p) G6 ^; w& d2 p! c
- 2.7182818284590455 _& b4 g, C, N& C) a
- >>> math.exp(2)
2 E5 T, c ^5 F# Z$ m - 7.38905609893065, E/ y/ b5 B$ {8 R4 D
- >>> math.exp(3)4 S, a, ^1 Q( Y* N
- 20.085536923187668
复制代码
, Q( S# m' A7 u! K1 u1 B0 Jmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
7 M8 }! A% c, Q- #返回math.e的x(其值为2.71828)次方的值减1
: x; ?" }/ v9 P) z$ e - expm1(x)( P6 W. Y8 \+ Z( s* m3 @: m
- Return exp(x)-1.: t8 N: P' U7 k8 S7 T' b
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x. x' _7 O7 A1 ~3 x. `
9 v- ]1 Y' o. K- >>> math.expm1(1)+ g( E% m8 F8 C/ D: K
- 1.718281828459045
. R. I. S# w' N1 k. N4 J - >>> math.expm1(2)
. c( G. [' \) N% Q9 d3 q - 6.38905609893065
9 Z* E4 O0 E9 o1 d& c$ ^ - >>> math.expm1(3)
$ i# q3 _, ^- S, ] - 19.085536923187668
复制代码
; Q2 K) Q8 E5 O# C/ u- ]% e1 Bmath.fabs(x) 返回x的绝对值
% S. a. n. ?3 @+ ^. h& _0 n- #返回x的绝对值
* b4 i3 h$ m6 z* D - fabs(x)1 T. B( J; `6 V- p( \- i( _
- Return the absolute value of the float x.
7 j" c. o& u) u& e7 M5 c
- @* l' y% Y& X+ x+ B* s- >>> math.fabs(-0.003)" P; ?- R, d- u8 W( R: z
- 0.0036 q& q/ [6 d9 ] H
- >>> math.fabs(-110)
1 V% r+ _$ A( p/ L) {& R/ f( E# X - 110.02 w O4 l: G+ K5 n; }
- >>> math.fabs(100)
4 U6 r5 n" X; W* j - 100.0
复制代码 + u2 I4 f( E: {) B
math.factorial(x) 取x的阶乘的值1 X8 W6 c. Z* Q
- #取x的阶乘的值4 p: U E: `6 ~) i
- factorial(x) -> Integral
" \+ J" y6 T5 { - Find x!. Raise a ValueError if x is negative or non-integral.
0 N6 ^- u$ p" g" Y2 s3 T: h - >>> math.factorial(1)
( X* J# j1 r, g* F+ y& b( i - 1. c; i) L9 E0 b/ O6 d
- >>> math.factorial(2)
; i/ v$ H5 Q5 B0 a0 [- F u! g - 29 ~6 _. L4 P' o( Q, ?
- >>> math.factorial(3); w" P' {$ K8 }
- 6$ S' n" l4 a. `- z; {2 s( f
- >>> math.factorial(5)
* x! V' @$ D2 i" ]. `/ N* E6 ` - 120/ j& r+ g. E7 h6 y. o% U0 M. Q
- >>> math.factorial(10)5 D; N5 k. f1 L# J9 N
- 3628800
复制代码
; j, K6 P; d- smath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
& b: t: |# ~9 G5 R r5 ~ w* \- #得到x/y的余数,其值是一个浮点数! s: R8 o" u. C; @2 L
- fmod(x, y)
) ^& @6 e( @) M9 J - Return fmod(x, y), according to platform C. x % y may differ.
# {6 \. `7 [6 A! F+ d - >>> math.fmod(20,3)# M. B% m* n' t
- 2.0& E7 @& |% B, w
- >>> math.fmod(20,7)
/ q3 g/ v8 B3 ]" B% H* r - 6.0
复制代码
; J6 a# x" p0 }; R7 K4 kmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 L. @! N s6 X; w; {3 j- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- y' k8 n# D C
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
- h2 _, ~! K' N+ }) Z7 k - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
4 x C9 ] u7 t7 C! N6 h! G: Y - frexp(x)+ I3 C( h% u- K2 G9 N4 }* r) U
- Return the mantissa and exponent of x, as pair (m, e).
4 X- e; g, }$ \+ F% @5 V - m is a float and e is an int, such that x = m * 2.**e.& {& n6 X1 C! J4 H
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0./ \6 U8 b1 ?# q9 \1 C7 ]
- >>> math.frexp(10)% a' p: o( \% x' L
- (0.625, 4)8 _/ O9 @ ]7 H
- >>> math.frexp(75)9 d) v6 R; w, @) e/ a
- (0.5859375, 7)7 f9 T7 G8 _" Y- D$ S6 w9 M1 g
- >>> math.frexp(-40)2 [- z0 ]% g" p4 Q
- (-0.625, 6)- q, N/ O8 Q J; V
- >>> math.frexp(-100)) e2 l' b$ M G7 ]6 N( p
- (-0.78125, 7)9 E0 j3 D# K; @0 o9 ^, v+ f
- >>> math.frexp(100)
7 v+ W; u) Y" ` - (0.78125, 7)
复制代码 # Z8 N1 m H: G8 g
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& M4 a1 B1 a L8 X5 D, |" \ q- #对迭代器里的每个元素进行求和操作
( M9 a, N6 o8 l9 b7 L% K - fsum(iterable)
0 j2 |1 O7 g) l# Q5 v - Return an accurate floating point sum of values in the iterable.
9 e6 e5 X/ N. M) ]" z: ?! M, { - Assumes IEEE-754 floating point arithmetic., y# O2 A- i9 K+ }% s
- >>> math.fsum([1,2,3,4]); a% _) G% _6 Z3 ^/ H- ?2 O6 `
- 10.0
& O* [' E5 V% Q - >>> math.fsum((1,2,3,4))" E* I9 f6 W( j4 t
- 10.0" }6 n1 b9 o |0 i# T
- >>> math.fsum((-1,-2,-3,-4)) @0 v3 B& ~9 H! Q
- -10.0$ M: K" f' U$ ~
- >>> math.fsum([-1,-2,-3,-4])
% c' e5 z' P2 f# y: l" K; P0 G j7 c - -10.0
复制代码
% o, m) j1 `% D' w3 d6 ]math.gcd(x,y) 返回x和y的最大公约数9 C& W8 p0 I& h5 V" Q) g9 Q4 f
- #返回x和y的最大公约数" Q* S) q$ ~3 ]2 U: p
- gcd(x, y) -> int1 d, N* ]3 o+ z( `. X& k4 c- g( a6 l
- greatest common divisor of x and y
" b9 F8 L" ]' X7 R - >>> math.gcd(8,6)7 }0 W4 M4 c, I
- 26 u& V Q" Z2 N" @! L+ F7 g
- >>> math.gcd(40,20)4 i. t% X* Q% u- M8 B! G- _
- 20
+ I5 M* L w3 D - >>> math.gcd(8,12)4 x. G, g- w: p; l8 ]
- 4
复制代码
' M ]' m7 [* X' g' O2 omath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
" n/ z* Y5 q8 V; D- #得到(x**2+y**2),平方的值
+ S# Z" w, Y$ V$ B6 W7 F - hypot(x, y): O5 M' o) X% m' @& n
- Return the Euclidean distance, sqrt(x*x + y*y).
5 G( c. H' d2 N, a7 m, [( R* b5 u - >>> math.hypot(3,4)
. N8 i% q4 O( M - 5.07 P7 s) z2 u+ O5 E
- >>> math.hypot(6,8)6 r' F& q) h+ R1 ?4 i& t3 t: g
- 10.0
复制代码
9 p0 p+ \ U1 y; z; B) K/ F* Jmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False/ A0 W- @9 J5 g+ t! N
- #如果x是不是无穷大的数字,则返回True,否则返回False
3 ^% m9 Y4 C- Y( v1 t& w- } - isfinite(x) -> bool! B9 O# @6 }# d9 J/ C
- Return True if x is neither an infinity nor a NaN, and False otherwise.
! q5 D" @' Y& Z S$ J - >>> math.isfinite(100)
) s2 O# \. l: T. N$ [* L - True
9 ?2 v$ [7 ]9 e% R2 {8 s - >>> math.isfinite(0). _$ b" n/ d, S4 `3 j l
- True
/ S) x+ C; o/ {5 J% o' ~5 g, ^" q1 } - >>> math.isfinite(0.1)
3 M. P4 p5 P4 U - True
4 c- j+ D; ~: E& q - >>> math.isfinite("a")- m0 L$ {# i- Z6 O2 W' A- p% u2 K1 Z+ B! _
- >>> math.isfinite(0.0001)
; ?! P+ Z; Q9 f- E, M! l# j - True
复制代码 6 l3 M9 u# p" o0 _9 h8 H1 F
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False" v% }8 p: ~$ j0 k+ [! m& p
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
2 o; t4 e% U1 }+ p - isinf(x) -> bool/ n l/ d1 i' K6 T* w; x
- Return True if x is a positive or negative infinity, and False otherwise.
3 Z& Y+ R8 w- l8 S J - >>> math.isinf(234)
6 }) w# I% A6 f6 M - False; a$ k& _. y4 K! w2 p- f
- >>> math.isinf(0.1): r4 t' g0 v/ F' }- ]
- False
复制代码 9 g% ` l2 S6 x2 U
math.isnan(x) 如果x不是数字True,否则返回False
# l' c! E) D& D' v. d- #如果x不是数字True,否则返回False7 y) j. b+ r1 g4 q5 F
- isnan(x) -> bool
: g; x( O6 j) w/ ^2 @* ?: |- Z' K - Return True if x is a NaN (not a number), and False otherwise.
$ K: F4 Y& g! R# Z H( w3 G - >>> math.isnan(23), i( a y2 }+ h2 W# t( S
- False
: n2 i+ P3 R$ I6 q" y% { - >>> math.isnan(0.01)' C1 L$ d N- ^( o' T( B. ?
- False
复制代码
) P, M/ E8 L5 ?% a0 Mmath.ldexp(x,i) 返回x*(2**i)的值9 l' s5 Q& M8 i6 {. Z1 V3 Z
- #返回x*(2**i)的值
+ K3 H; a, X' G. \4 D- ~ - ldexp(x, i)$ i9 t; s' |6 l. x* j; \! c# ^
- Return x * (2**i).
' k) K5 d# W6 f& H6 y9 i - >>> math.ldexp(5,5)% x8 H# ^2 r7 [" O1 a; F9 r: K0 D
- 160.06 g/ i: W( a5 a% A
- >>> math.ldexp(3,5)! ^& l. {6 Q6 P8 u) B# f) `/ L" `
- 96.0
复制代码 - J0 B, ^' y2 g# m6 c' ~
math.log10(x) 返回x的以10为底的对数' I i2 s3 _3 v9 ~; H# j6 Y
- #返回x的以10为底的对数 g, W# } @4 d9 f# S3 n( d
- log10(x)3 G7 Z( K" v& e* ? ^1 x/ \
- Return the base 10 logarithm of x.
( I2 Z2 q% i {& M- g I6 D - >>> math.log10(10)8 l6 U' f. v2 e' h
- 1.0
. v! x7 M9 E; l# w; U/ r% I' B - >>> math.log10(100)
, s4 K. k. I5 N- s% p - 2.04 _# X! d3 `( u: ?% L7 t
- #即10的1.3次方的结果为20
: L# A% r# V3 C0 \: \8 n# n. l - >>> math.log10(20)
3 _! H) d5 z1 J8 z! Q$ S! K - 1.3010299956639813
复制代码
# B: ]. q6 Q {( W/ S3 A( l4 \math.log1p(x) 返回x+1的自然对数(基数为e)的值
! a" _. M6 e% v# K& h% [6 l3 l- #返回x+1的自然对数(基数为e)的值( C' T6 X/ i9 d4 K, A/ i' m) V% Z) K
- log1p(x) S6 n- A! ]" Q2 z ~& {3 d& |6 }! T
- Return the natural logarithm of 1+x (base e).
+ `/ p+ M+ o! d, J* t - The result is computed in a way which is accurate for x near zero.
* i4 \9 E( e( m) T1 r9 M - >>> math.log(10)) D5 H2 l% d2 u" t& U
- 2.302585092994046+ b' t9 E2 q" e, Z" x
- >>> math.log1p(10)* ?2 p2 X! A& ` S' {+ Q: t, g
- 2.3978952727983707, y& U! a8 s" D: D# A
- >>> math.log(11)
5 _2 y, }" O% B) f* ] - 2.3978952727983707
复制代码
5 H; V; Q' F. R6 [, Q7 m! `" b7 {math.log2(x) 返回x的基2对数
+ g8 V2 Y/ s% H3 q' q M2 F- #返回x的基2对数3 f- ^+ j& g4 A6 N1 a* m
- log2(x)
8 h( g1 w- s6 K! T - Return the base 2 logarithm of x.- k, i$ L8 U& }0 T
- >>> math.log2(32)
( z6 S) p/ n# i. l* J6 |7 X! ~ - 5.0: T8 y% `( c' C) b1 \4 g' z$ }$ u
- >>> math.log2(20)
, E- P6 `) w# P7 b1 x n5 n - 4.321928094887363& `9 n6 m0 W$ ]1 e) n
- >>> math.log2(16)
3 B5 F+ c4 y4 h$ y4 z( F5 P - 4.0
复制代码 0 c- z" _- C3 L3 _- Q
math.modf(x) 返回由x的小数部分和整数部分组成的元组
3 Q) d( n+ ~6 n3 P0 h! {; Q- #返回由x的小数部分和整数部分组成的元组3 S v' L* m, Z2 [, X
- modf(x)
) a9 q! O+ M: D3 v) X: P% V3 o+ B - Return the fractional and integer parts of x. Both results carry the sign: u" s' a( g g% r
- of x and are floats.
7 l. {4 B2 I3 b z% D - >>> math.modf(math.pi)
$ T) p m% |) O7 E& [ - (0.14159265358979312, 3.0)8 s: a; e/ B- r+ K& C8 E% J7 a9 D3 J
- >>> math.modf(12.34)$ j& v) e% v0 X7 X, E+ h# T
- (0.33999999999999986, 12.0)
复制代码 % D U% o1 @3 A- A* ^# o: q
math.sqrt(x) 求x的平方根
) i* q9 O: w! E, p3 s- #求x的平方根" N; \ ^: D t! l
- sqrt(x)) H F, o: @/ e$ |/ K& v8 [( N
- Return the square root of x.9 t+ E9 S" G, Q5 Z
- >>> math.sqrt(100)% w% N" E0 [' Y4 e" K" J9 a6 z
- 10.0
# f. f/ S' w' B2 w( c$ H! c - >>> math.sqrt(16)( W1 d7 V) Z5 `6 H+ d
- 4.09 A5 k: b$ e" ~3 e) Z9 Z# ~7 H
- >>> math.sqrt(20)$ u) O+ M+ M" K' w% X' U
- 4.47213595499958
复制代码 ! o4 K9 V4 d* y' F m n$ f
math.trunc(x) 返回x的整数部分- #返回x的整数部分/ H2 T, r! U% _( p
- trunc(x:Real) -> Integral
$ p* ~4 ~! O1 Y% l" u! I - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
! w- L0 S2 g: \' t - >>> math.trunc(6.789)8 Q) ?% o" q; p
- 6
* Y5 O3 ]0 F' H% ?( ~7 r9 p - >>> math.trunc(math.pi)# m# P* m r7 t9 u7 p+ ~
- 3 f( N) P/ {2 r, `2 ?
- >>> math.trunc(2.567)! M, u5 W7 X5 b9 k
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|