马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* `( w) ]4 x& _, D& d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& Q5 N) T9 \. S/ s- R$ Q6 z
; v( x" {4 Z' P* y$ b2 m0 P
方法1:: L& T2 S- @' D' W4 Z
- >>> import math
1 F& k6 { D- ?4 G; |3 S - >>> math.sqrt(9)
7 k2 ?3 n! n8 t. Z3 G! K/ _ - 3.0
复制代码 方法2:
8 p* D' j3 `" \6 T0 E* j d$ l$ f- >>> from math import sqrt
- I/ K3 y9 Q+ ~9 p6 ]0 C2 q' i - >>> sqrt(9)
; H4 D/ Z. s/ A T) g, V! N: K- W& f - 3.0
复制代码
( i4 j. w7 P3 F 1 O7 k9 [( W9 g2 J
math.e 表示一个常量9 f3 S. h+ j' P+ {3 R
- #表示一个常量: u% I s- t9 \! h; U
- >>> math.e, B% w7 ~* e( Q0 z' `! ]: E
- 2.718281828459045
复制代码
: u% F7 W! R" N& I8 N: i# H7 H: I8 ^math.pi 数字常量,圆周率
$ J9 D$ i" R0 |% Y- #数字常量,圆周率
- d9 m+ W* `2 e n: n - >>> print(math.pi)8 P. B0 _* h8 i# \' h {7 j3 X0 P
- 3.141592653589793
复制代码 . H5 d; O. Z$ g9 A2 k7 X
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x1 B; i+ m) `% Q, `8 z$ Z5 G
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 V1 V2 u3 W. U: r3 E% u' K' B# g - ceil(x)
' _* D1 [" W! `0 ]! U - Return the ceiling of x as an int.; N8 A, Z+ f) h, W) c
- This is the smallest integral value >= x.
" J- X. E& Y& J% j
& M" i7 {8 d- v8 s- >>> math.ceil(4.01)
* B( M, R a0 p: D5 S% T - 55 i U+ |- Z; z! i1 ^; T2 D- S
- >>> math.ceil(4.99)
3 x0 F" I8 i9 t. H. \ - 5# X- x9 b! _: M) P1 v% X6 Q
- >>> math.ceil(-3.99)
& }0 ]. U4 C6 N3 ]1 ~" |" F - -36 E [+ W5 t4 q% E& M
- >>> math.ceil(-3.01)
3 {: E& N5 [$ \: e! m: s# \( U+ S - -3
复制代码
* Q! U3 k, }& n; l: J) e8 j" c( wmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 i) t% k: o$ @
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% M/ Q2 o( F' e# ~% h- x& @) L
- floor(x)
, I2 a5 x. H3 ^$ C2 { - Return the floor of x as an int.% n# l& X* T/ H5 E4 k$ k
- This is the largest integral value <= x.2 Y; _! C+ D: Q
- >>> math.floor(4.1)
& q& P& w8 w3 ^: { - 4
4 V% v i+ z5 V3 j - >>> math.floor(4.999)
: Y; T0 c" ?6 [5 U* [ - 4
& X+ W" Q0 k! h# p4 q+ K) D3 U# Y( Y - >>> math.floor(-4.999)3 b. J/ g" U% E$ o
- -5. a. \: @/ v. j. |. M1 {4 ?
- >>> math.floor(-4.01)
4 |' j4 i& H% C4 a) ?6 c9 M+ X& ~ - -5
复制代码
7 G0 w; o) j; J, Rmath.pow(x,y) 返回x的y次方,即x**y
* j4 p& v& }6 h+ T# |% x- #返回x的y次方,即x**y% I8 V5 b, F) _. ^: }3 Y
- pow(x, y)" O( m x5 ~! _3 |; n ]& [& K& z
- Return x**y (x to the power of y).$ q* @- L2 i1 B% h
- >>> math.pow(3,4)
) N0 O; W V$ r' k0 n8 m - 81.0
4 }& j# ^8 ?! g - >>>
1 Q- R: g$ a# K0 X+ P" u - >>> math.pow(2,7)
! m! X% H, t% @, W p3 a, D - 128.0
复制代码
, @* K7 d, o" L" b; Qmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 a' ?1 O* e& d/ p# e2 V& S
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' u0 M: w2 q. o
- log(x[, base])* }% K7 O1 E0 m$ k9 U0 c
- Return the logarithm of x to the given base.& U8 N5 P* |7 U; t
- If the base not specified, returns the natural logarithm (base e) of x.5 {* T' [# l! O8 R. K
- >>> math.log(10)
( x1 v* L% {5 @ u# V- Q5 [ - 2.302585092994046- s( Y# q0 X: e1 X) O
- >>> math.log(11)
& m$ A, j6 v, m* a( [ - 2.3978952727983707
; L" T9 D7 o$ w* `. L - >>> math.log(20)
& ~, o+ l8 `+ X8 N+ w# T - 2.995732273553991
复制代码
$ F& }+ Y% q4 l* \/ jmath.sin(x) 求x(x为弧度)的正弦值5 L! h# q) d# \- R
- #求x(x为弧度)的正弦值
4 O/ E% i) ]0 S9 U- d. u - sin(x) { b! L4 u$ D" H, C- r' ]
- Return the sine of x (measured in radians).
& H# g6 V2 [* ?* u. q. P - >>> math.sin(math.pi/4)$ c! M9 y% L$ J; U% Y9 n
- 0.7071067811865475
_: b" L# K# \; ^ - >>> math.sin(math.pi/2). b9 i- P1 e4 x# h( T. b$ P; U& j
- 1.0
. ?4 ]# H( R" u0 Z- D2 z - >>> math.sin(math.pi/3); F' R1 d! Q/ J2 x0 J3 ~
- 0.8660254037844386
复制代码
$ ]3 |8 D7 I C" D+ mmath.cos(x) 求x的余弦,x必须是弧度. r2 h; e3 X, m" c- n0 r6 F ]
- #求x的余弦,x必须是弧度: B$ @8 W! L/ I+ {- L) M. P
- cos(x)
. Z% C; l( W6 A7 l E - Return the cosine of x (measured in radians).$ k* {" B. M; m& U5 f& ^. @
- #math.pi/4表示弧度,转换成角度为45度
- }3 v7 g% E# Y. t4 ?: ~ - >>> math.cos(math.pi/4)4 m! F2 d) B& H3 J/ v3 g. V
- 0.70710678118654769 l# i2 O/ r# _" t
- math.pi/3表示弧度,转换成角度为60度
" R$ g8 C+ e J; y) A: ~7 ] - >>> math.cos(math.pi/3)) N. K* j3 c z3 G
- 0.50000000000000018 {4 J) N4 e B
- math.pi/6表示弧度,转换成角度为30度" T) ]- l5 Q6 C$ ^/ z" o
- >>> math.cos(math.pi/6)
+ f" k* H( x8 `% b' |/ k6 q - 0.8660254037844387
复制代码
; n0 X* r9 \) L1 q, {6 E& L( n5 V, lmath.tan(x) 返回x(x为弧度)的正切值
( v; ~& b+ }' `( D- #返回x(x为弧度)的正切值
' S6 f( ?7 x+ ^9 |3 a/ ~ - tan(x)% m5 j% G! f) J8 v2 ^
- Return the tangent of x (measured in radians)., S0 t0 O# A3 Q, C$ ^
- >>> math.tan(math.pi/4)% `' v; \" S1 T) o9 r
- 0.9999999999999999
4 G- l9 s) `& f8 b/ k' d - >>> math.tan(math.pi/6): e+ x% l3 h* N4 f
- 0.5773502691896257
. `, z% _, }9 O6 |( O - >>> math.tan(math.pi/3)& A5 v1 y) U' v# Y; Y, @
- 1.7320508075688767
复制代码 : w+ @" ]! u6 k: _6 ?1 n% W& c g
math.degrees(x) 把x从弧度转换成角度0 ^& Q9 R; {6 ?& U- Q# g: }, \
- #把x从弧度转换成角度! @1 }% w8 F* _; u" v0 Y: X
- degrees(x)
6 C/ R: v0 Q5 a, v. R - Convert angle x from radians to degrees.( e5 Q, r. j# T7 p) y+ M) X
- x/ r, a8 e; P& J- >>> math.degrees(math.pi/4)
* b1 A, F5 i9 c0 |9 s - 45.0
& Q. R6 `! D/ y Q( \ - >>> math.degrees(math.pi), m3 n' B6 g S/ C- F% ]: L
- 180.0
+ G0 Q6 p: q* ~" n - >>> math.degrees(math.pi/6)0 g" g: I& R' V( d. s
- 29.9999999999999964 Q- J, [& R) U) H+ U$ M0 q0 d. s
- >>> math.degrees(math.pi/3)- F) A. X* B0 i c% N
- 59.99999999999999
复制代码
8 Q' C5 _% K" Z+ Vmath.radians(x) 把角度x转换成弧度/ E# {% e1 i, P0 q
- #把角度x转换成弧度/ G; T6 p8 q7 r0 {8 a u
- radians(x)+ G4 _# y ~0 F
- Convert angle x from degrees to radians.
( ?3 p( H$ u- t8 b3 h* g - >>> math.radians(45)) o U5 y: }% s6 W- y, h/ j
- 0.7853981633974483* ~* J6 }4 ~/ v2 d
- >>> math.radians(60)
3 M5 X: x1 n6 P - 1.0471975511965976
复制代码 + }( }. D: o& n7 b8 X
math.copysign(x,y) 把y的正负号加到x前面,可以使用0- e/ K. d6 I/ p
- #把y的正负号加到x前面,可以使用0
: q K2 c# |8 }# @ - copysign(x, y)
: y+ N, ?6 E. n) x( B - Return a float with the magnitude (absolute value) of x but the sign
]& J u/ e3 j4 ]. {* T - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 j6 U2 s: T& c8 ~& l - returns -1.0.! L$ B7 Z$ `8 Z1 q6 ^' h
- 1 [1 b8 j& E* O; W( z! C) r; u
- >>> math.copysign(2,3)
0 q4 C) w$ H `* t! H - 2.0' @: C s5 P3 u5 S- Q% x0 G
- >>> math.copysign(2,-3)
; f; n0 X" ?( V. S4 T& R - -2.02 T! m& J8 L) H
- >>> math.copysign(3,8): m2 X4 A( ?, n3 {
- 3.0+ B7 F; A6 d8 U: a
- >>> math.copysign(3,-8)( O- z- q- I6 i+ c% \
- -3.0
复制代码
: P% f2 y# `1 Bmath.exp(x) 返回math.e,也就是2.71828的x次方
3 L/ x7 c8 E% X) J# V- #返回math.e,也就是2.71828的x次方% p ^2 Y& g6 ^8 `/ c5 ~: E
- exp(x)
& D9 Q# ^" h& E/ }! G - Return e raised to the power of x.
$ g+ G, F V( f4 O+ V- k3 V% _# O) A2 K7 ^ - 4 g1 ?/ b/ M5 |# l: o1 G1 h
- >>> math.exp(1)
6 s: [7 w5 b# l" R - 2.7182818284590457 P9 m5 Z C+ s+ P; b
- >>> math.exp(2)
0 f, F: A) y% P2 o - 7.38905609893065: e+ @ y/ ` p2 R4 b/ y3 y/ H% h/ ~! J
- >>> math.exp(3) c2 S6 y! [2 s2 |+ s
- 20.085536923187668
复制代码 ( e" ?- {9 Z& u
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# D* R6 F- k2 v2 c5 q2 l: T0 f6 f- #返回math.e的x(其值为2.71828)次方的值减1
6 Z }& O! U4 j" ?# Q7 N' | - expm1(x)4 l2 f3 R' n/ F @& p" }
- Return exp(x)-1.) e5 i6 D D9 M' C- P; ]
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" A6 i( k2 x3 o% L" j0 x
& i+ K5 s9 E0 \' {1 i0 t6 e- >>> math.expm1(1)
3 i7 T$ K w3 y5 B! r) U - 1.718281828459045
: l" y0 K/ W" W - >>> math.expm1(2)
& e5 P, k W+ K/ z( `- d - 6.38905609893065
- D- }$ n$ { A0 V) Y - >>> math.expm1(3)
$ Y* ~$ ?$ y4 k9 A ]5 P - 19.085536923187668
复制代码
" Z' R& i9 j1 O4 ~! Qmath.fabs(x) 返回x的绝对值
4 d) @& e0 I& d, p3 V, x7 ?/ b) @0 o- #返回x的绝对值8 A, _- f) V% {! q* m6 |) y! f
- fabs(x)
) k- a. Y8 ?: }2 V; X; {% D" B - Return the absolute value of the float x.
+ I% j. i$ J$ }4 u) e: n - - F; E# X: ~8 C5 `
- >>> math.fabs(-0.003)
1 L v6 H/ q3 ^& O' x" i8 I! f - 0.003
u8 v( ^* p) p8 |( B- p* c& S+ R - >>> math.fabs(-110)
$ Z5 K( D) R( k* u% l' r - 110.0
1 F- k7 P1 W& ?5 r+ d - >>> math.fabs(100)
2 r- v9 S# d" O' F8 Q) E - 100.0
复制代码 8 \8 m8 w) p8 N; D
math.factorial(x) 取x的阶乘的值5 N* O, D) z. @. m& q" i. M4 z7 S
- #取x的阶乘的值% g7 [# ?6 i' x) ^! R
- factorial(x) -> Integral- R1 c7 E$ d8 B& {- H
- Find x!. Raise a ValueError if x is negative or non-integral./ o, w+ n5 \) `( p: s+ l8 C
- >>> math.factorial(1)
$ M- ^5 C' E* l+ A& F( K - 1
4 z0 t( D# M1 } Z; W* ` - >>> math.factorial(2)
4 `$ P! b2 h) ^5 ~" Q1 s( A$ e - 21 j2 {# d! V- r0 j
- >>> math.factorial(3)
6 _* k8 M( I$ T - 6
, _/ n; ?- ]. d4 {( l - >>> math.factorial(5), j+ s/ u% p$ k0 N" o
- 120
! p- n: K8 j/ z( P* b* f8 ? - >>> math.factorial(10)
b" q7 y; u5 A, W% B6 s - 3628800
复制代码 ' Y; o) A/ g: {
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
* I& W. Z' x, x- #得到x/y的余数,其值是一个浮点数
4 `* F7 C' b' D" \0 \2 ? - fmod(x, y)+ K9 ~1 I( w* w- v7 K( I
- Return fmod(x, y), according to platform C. x % y may differ.
1 \ c" e, T; b0 @( e& `" _ - >>> math.fmod(20,3)
6 |1 g/ |$ }7 f5 u - 2.0
" n0 |5 n/ W3 U7 G ]9 U - >>> math.fmod(20,7)7 }. x7 E6 w. P, L4 [) a
- 6.0
复制代码
0 O7 a' g' D8 g2 H. z: y _0 Y# Emath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; |# C1 @* z" i( V: r! {- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. v) I8 j; [0 d _
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值4 d" D/ n w7 \0 r
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
% i/ e, U9 p4 G+ q, m, i/ k% Z7 O - frexp(x)9 J6 {& J$ O! }3 ?+ `
- Return the mantissa and exponent of x, as pair (m, e).
+ ~" w* l# n5 k: I - m is a float and e is an int, such that x = m * 2.**e.6 f9 V }: D7 U+ X' G7 M& e9 B$ J }
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.! D4 F/ r3 [8 N0 b
- >>> math.frexp(10)
: `: x4 C# ]2 w6 e3 X2 S% G f - (0.625, 4)0 y7 Q3 C! ] s
- >>> math.frexp(75). Y" c, l3 v7 a2 D; { j
- (0.5859375, 7). r7 w$ L& C( a
- >>> math.frexp(-40)
1 B0 \% y' q! S( K; q8 }+ I - (-0.625, 6). V9 b6 R) u }. J! Q T. N
- >>> math.frexp(-100)
; } W" Q; g3 i, }! y: ? - (-0.78125, 7), A- f1 Q _, z, e$ E
- >>> math.frexp(100)
* @/ T9 N$ u" l - (0.78125, 7)
复制代码 ( d' F5 S9 E: [2 o0 a) R
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
5 D& y& H) `# r6 t- #对迭代器里的每个元素进行求和操作
\) A1 l" B. j; A6 g: P - fsum(iterable). @; C# K" b, M7 @' X
- Return an accurate floating point sum of values in the iterable.: U/ y& o# N& L; r+ @' V
- Assumes IEEE-754 floating point arithmetic.
( ?! Q: G0 |/ a5 ] - >>> math.fsum([1,2,3,4])$ r" J7 \6 @1 D
- 10.0 o1 j8 D8 R% R4 z
- >>> math.fsum((1,2,3,4))
" z$ H: q& a0 }, a4 E) Z3 ?* h - 10.0( z! P+ G$ e- P
- >>> math.fsum((-1,-2,-3,-4))
* E* w, M: Z e - -10.0
: n- j$ H. t# b8 v4 o$ L - >>> math.fsum([-1,-2,-3,-4])7 j/ r/ Z! [8 W" `3 K
- -10.0
复制代码 ' }, v% u: \) q8 Q: |+ d* Y
math.gcd(x,y) 返回x和y的最大公约数
, n: o$ v0 r+ y+ Y# ]! v' p6 C8 I0 p: R- #返回x和y的最大公约数
6 M3 `9 @5 a( ^- e- u - gcd(x, y) -> int
3 q2 ?* a, M* v3 W" r - greatest common divisor of x and y! B: N8 T4 k3 t( G3 T' S- v/ f
- >>> math.gcd(8,6): F8 j) a o( M* H/ d3 X# B
- 2
! D9 k; u; ?& `7 P5 Q' S3 p% k) b - >>> math.gcd(40,20)
1 ?7 B: k& ~( _ Q5 X4 o& u - 20
: V" r S. O: x; p, b. v4 O - >>> math.gcd(8,12). @2 j- N- Q3 l" l6 e( R) L8 ]9 u
- 4
复制代码 # {; b+ C; k- v9 u% |2 C
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
( a: i! n+ u* b6 b- #得到(x**2+y**2),平方的值
# g2 ]; L6 u r+ ` - hypot(x, y)+ V* n, ^) ?& F
- Return the Euclidean distance, sqrt(x*x + y*y).6 ~- e5 u1 _& _* y
- >>> math.hypot(3,4)* H3 K7 c$ ^6 b: K$ j7 ]
- 5.03 ?1 c. e* F4 T! ]3 p$ U5 f* E
- >>> math.hypot(6,8)
* O7 g6 i4 O5 Y- G3 j! K) z - 10.0
复制代码
% h3 R/ r ^1 o. ]math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False* U) O' H/ t9 N5 [# n
- #如果x是不是无穷大的数字,则返回True,否则返回False
) T; s, C9 o* p/ J% w - isfinite(x) -> bool$ w" T) }+ Q- ^* o1 t
- Return True if x is neither an infinity nor a NaN, and False otherwise.* m( G/ F/ e: _( T- U/ o# C/ v4 A
- >>> math.isfinite(100)
+ a5 I) G4 L; q5 h* E - True9 c& s+ {, w! h. w( ?8 E$ E
- >>> math.isfinite(0)
$ c7 F1 k9 ]6 {! ?2 D - True
) t7 Z8 I% g7 X) P& P5 X2 { - >>> math.isfinite(0.1)1 Z% f; X% R. x" \3 g8 J: X/ |
- True
) C3 U0 \: F, q4 Y# F - >>> math.isfinite("a")
( ~1 D7 O9 h' M+ k5 \( } - >>> math.isfinite(0.0001)
4 Y( a! j+ A T8 w0 Q" V# m0 w - True
复制代码
# E! l7 @9 P( H9 N5 Z1 z7 n5 b6 umath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
" h: X( G/ i% @3 R6 ~- #如果x是正无穷大或负无穷大,则返回True,否则返回False
' @/ S6 S" X3 e# k) p - isinf(x) -> bool6 i4 ^" |: a- }9 P6 H
- Return True if x is a positive or negative infinity, and False otherwise.. [% O0 ]/ H, [( j9 c6 u& M8 N+ C" u
- >>> math.isinf(234)
% [/ s9 u+ W% G H - False
o* I* s" x4 g4 J - >>> math.isinf(0.1)5 h+ Y: `; R* `. z, q. G# m/ H
- False
复制代码
3 L5 Q$ S" l% l) g( X; \( Jmath.isnan(x) 如果x不是数字True,否则返回False/ f1 e) s+ l" g* }" S3 S# p
- #如果x不是数字True,否则返回False
! w; f8 }1 d. e+ z- c- o( w+ U. I - isnan(x) -> bool( H+ Z- {# c2 e* |% B# [ \8 b
- Return True if x is a NaN (not a number), and False otherwise.
/ e+ y+ I3 {4 {2 r c7 U - >>> math.isnan(23)
: ]3 [; q9 _1 \6 Y: Y - False
0 ~+ B7 e4 X. ~( `2 x& r; C6 E+ o - >>> math.isnan(0.01)$ l4 Y( O: Z+ H/ M0 o9 o/ f
- False
复制代码 " a" K! r" c$ N" ]
math.ldexp(x,i) 返回x*(2**i)的值
v$ @, z% f5 b* @3 i6 s' d- #返回x*(2**i)的值
7 g9 L' ?% d* s! r7 v7 X U5 _; v - ldexp(x, i)( G+ g; \! _& ]& Y& K/ \
- Return x * (2**i).- i& P9 u& n) ~. l3 c" b, Z
- >>> math.ldexp(5,5)& p- b* W" `' ?! m# a$ L4 W. H
- 160.01 ] d: f$ m Z1 `8 V, [- A, K
- >>> math.ldexp(3,5)9 H, s% c& J2 `5 a: ^
- 96.0
复制代码 5 A( [' e, k8 r1 v/ O
math.log10(x) 返回x的以10为底的对数0 C) u1 m. Y' H* H: O
- #返回x的以10为底的对数
b4 \9 U& A# U" U - log10(x)
9 Y5 f1 X; A/ P& |. W8 q2 } - Return the base 10 logarithm of x.4 |. C; a2 v# \/ \' P
- >>> math.log10(10)* N+ K0 o/ t1 c; n% B
- 1.00 `' c2 K! m5 \# E
- >>> math.log10(100)
, [$ _8 ~2 R: C% h/ N - 2.0 I8 r' `1 Z! W) R4 y
- #即10的1.3次方的结果为20
6 F1 @. H: I. W) J' ]2 [+ h - >>> math.log10(20)6 D; d4 W; `! m8 j2 {/ t; @( x ?/ v
- 1.3010299956639813
复制代码
: u6 s1 {/ N1 e& F, G" q* `8 zmath.log1p(x) 返回x+1的自然对数(基数为e)的值
& Z. J; p+ Q1 L# g& K/ j' K3 j- #返回x+1的自然对数(基数为e)的值
, U3 `, D, `" @9 W% w - log1p(x)" Z3 q. K! g: a. o' w' Z$ p
- Return the natural logarithm of 1+x (base e).
8 q( }9 }6 X6 c* \* b, f( E - The result is computed in a way which is accurate for x near zero.
2 B h& H. @; U! g$ G0 J - >>> math.log(10)) @+ Q5 x2 c* K
- 2.302585092994046
' L& ^; ]" f2 }* ? - >>> math.log1p(10)
9 P' `8 ]3 N3 }' P/ l% ~% Z - 2.3978952727983707( j. j* n% G- p- ]. ~7 L
- >>> math.log(11)
: B7 |; _) V& @ - 2.3978952727983707
复制代码 % V( X8 R0 A/ v2 w
math.log2(x) 返回x的基2对数
x" p, D/ m2 |9 R) b* k- #返回x的基2对数
$ S, s0 n; Q/ M# v' F! p - log2(x)6 o7 [; [) @: n; f J7 Y/ L0 C
- Return the base 2 logarithm of x.$ |0 M( F8 {& u, ?4 }( \0 g
- >>> math.log2(32)2 j9 u! A' t; h
- 5.0 n3 h. J& [- P2 f* |
- >>> math.log2(20)
* k* C, t+ @" E# g - 4.321928094887363
1 |" V h$ _, [8 b' I9 d1 T( Q5 w - >>> math.log2(16)
8 F, r7 F3 i+ @6 u& t7 ]* w - 4.0
复制代码
; O, w0 A& _1 m5 e+ Zmath.modf(x) 返回由x的小数部分和整数部分组成的元组3 }6 Z- @8 n+ @3 m
- #返回由x的小数部分和整数部分组成的元组. X8 Y( `0 E. N7 ^: F% K( x
- modf(x)
+ {3 ]; G& r/ o& j: ?& b7 e - Return the fractional and integer parts of x. Both results carry the sign
, I3 m% z' z0 R - of x and are floats.5 n1 a, w- D4 U# _
- >>> math.modf(math.pi)
0 t( T6 O, r' E% j$ r: |( _; a# w - (0.14159265358979312, 3.0): h; D& l+ i9 p1 H, ~# F; F
- >>> math.modf(12.34)
) `/ E0 K3 d2 T1 }2 L4 f - (0.33999999999999986, 12.0)
复制代码
! B! x( z6 y9 e! a! e. B! kmath.sqrt(x) 求x的平方根+ @! Y5 ?4 R1 T! \/ X
- #求x的平方根
; @& w8 c+ G5 [ - sqrt(x)) f3 T6 ?) p% W0 H) L h) L
- Return the square root of x.% _- @3 r' W. c5 ^% ~
- >>> math.sqrt(100)6 e2 P$ z M. ]+ f+ u. r$ r
- 10.0
% k+ |8 }4 B, `) A9 X% s( r - >>> math.sqrt(16)
$ s" C/ e5 C# F - 4.0
$ L. w" u; t2 n" d/ ? - >>> math.sqrt(20)" N1 j( O& ]- q
- 4.47213595499958
复制代码
/ t# P/ n& i. S1 B1 g0 J' kmath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 J$ i* k; @$ \+ Z8 ^$ }/ } - trunc(x:Real) -> Integral
. T5 J3 ]+ y0 t6 b; j - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ x. n2 C$ U+ l9 u5 c1 D' p
- >>> math.trunc(6.789)+ z- v6 p1 O7 S9 n$ Z1 P; b7 X( J6 k
- 63 H9 L- l4 L6 M; o/ L
- >>> math.trunc(math.pi)% \- }- k1 U8 @( q
- 38 |+ E7 C3 ]" Z8 ~6 A: D! U
- >>> math.trunc(2.567)
* G6 C& m/ \) ? - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|