马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 K! }) w( ?: K! e/ g
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- Z, f$ n0 n6 H! o
( W- Y7 h J* h$ f q1 _方法1:
& d T. Z' ?6 t2 ]9 ^- d3 [# C: u- >>> import math0 B) a- i! t- q6 B
- >>> math.sqrt(9)
, [ L5 a6 j# w* z - 3.0
复制代码 方法2:, m& s" G; m" Q. o) h
- >>> from math import sqrt Z4 U5 j ?0 r$ j* O
- >>> sqrt(9)
/ a2 n6 V, b' _( s/ P" ~; O% I) I; | - 3.0
复制代码 3 }# S3 V% J2 }
. m( X/ N( B1 s. K3 Hmath.e 表示一个常量
1 {7 f7 b0 }7 d- e( m3 e- #表示一个常量
7 c& x! l& v. j, z: j) Z - >>> math.e/ M$ n/ [5 d0 B# H$ j h' s
- 2.718281828459045
复制代码 ; Y. X) V* u+ ^* l
math.pi 数字常量,圆周率
2 o5 q" c( y& p: k* \- #数字常量,圆周率
5 b- C6 d1 W5 }" J% t6 T3 L - >>> print(math.pi)0 Z- x$ P, i- c( P
- 3.141592653589793
复制代码
. M& M* R6 C' B8 h% F0 A: I6 T9 cmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x( q' A4 [% g) j ]" ~
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 K1 Z# {' l7 Z# d1 D - ceil(x)& j. |; x& n* u) ~2 t: p( H# n
- Return the ceiling of x as an int.
. q. S5 `, p' A/ V( W A - This is the smallest integral value >= x.
7 b9 c2 P4 `1 F - # N# G9 ?0 E: C! ]1 o- Y2 k
- >>> math.ceil(4.01)
" @# A* J( W! B. S - 58 z- t/ L* Y3 Y1 |
- >>> math.ceil(4.99)
( f' Y, k1 B, B - 5; Y J5 T8 I1 T! T2 b1 t
- >>> math.ceil(-3.99)7 | N1 S/ I. F) W- u, k- H
- -3
7 T* Z; |9 o( _! P. k; o# d - >>> math.ceil(-3.01)
; L* _1 O N; S - -3
复制代码 6 W1 J' Q9 Z+ z* S. N+ Y) d: L0 T
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# X$ W: n9 Y6 h; E- K/ n- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 D5 K# h( e! {2 W$ P - floor(x)7 d+ C4 K K% H; D- p+ n }
- Return the floor of x as an int.
1 W) s) J" Z2 N) j5 v0 ] - This is the largest integral value <= x.- T! j2 o) ~$ I: q) Y6 A% Q" \
- >>> math.floor(4.1)- K4 J4 L* x* q/ K2 d% P2 n% Z
- 4
; |9 B4 t3 r$ j) b# H& D - >>> math.floor(4.999)" o7 ]+ i% V1 Z- R7 b) [
- 4! y' \/ H, s$ T* e. x9 y
- >>> math.floor(-4.999)
" g8 P4 u4 e/ v! ` P" T# w - -5! _- q9 O$ f) M& |
- >>> math.floor(-4.01)! F8 [6 Y+ |4 E9 k4 I' e F9 c
- -5
复制代码
1 J5 }: P2 O7 d: D ^math.pow(x,y) 返回x的y次方,即x**y
: x& n- _2 }, g% Q1 u/ l& J- #返回x的y次方,即x**y
% A, j. E- ]' h; |5 b - pow(x, y)
; l$ F0 a8 }* c2 b - Return x**y (x to the power of y).7 d. h" U6 g! e
- >>> math.pow(3,4) H' A: L# ^! W3 Y
- 81.0+ ~+ h1 k) `0 g/ W& w4 O
- >>> 6 U( p* g- e; U) M3 J
- >>> math.pow(2,7) Q, ^9 o& V0 m
- 128.0
复制代码
( V; {% h3 F/ {) {0 h$ Pmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 C; E7 z* {5 C. a% |- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ l: o' x3 F$ M. L- A( k) c
- log(x[, base])
, c/ G$ c& H( {# u2 h* W - Return the logarithm of x to the given base.
+ _- {/ H* K% c$ `, W- P4 T - If the base not specified, returns the natural logarithm (base e) of x.$ `2 u6 ^! q: T6 o0 O
- >>> math.log(10)
. p8 Z. Z4 G' R! i/ | - 2.302585092994046
1 D8 N0 T2 R+ K/ z3 L - >>> math.log(11)
9 J" l. K- x8 O" Q. g% M7 Q - 2.3978952727983707, w+ W& W4 {5 u8 D
- >>> math.log(20)
$ `) u, d# M, E7 t2 D x - 2.995732273553991
复制代码 ( b( t8 y q" K ~/ r8 ]4 {; M7 w C* h
math.sin(x) 求x(x为弧度)的正弦值4 t7 D% Z9 Z( Y. G+ r3 ~- Q/ }
- #求x(x为弧度)的正弦值
& Y# e: L. s0 {' Y - sin(x): e' r( ]6 N; I0 H9 {7 f3 d
- Return the sine of x (measured in radians)./ Z9 x% x; |/ m# U/ g, v1 R
- >>> math.sin(math.pi/4)2 P- p( c. e, R) C; ~( N2 M- n
- 0.7071067811865475/ ` u8 R6 ^8 B- H: W
- >>> math.sin(math.pi/2)7 k+ n8 Y7 A" J3 n
- 1.02 i/ C( t0 u! s- c4 ~
- >>> math.sin(math.pi/3)
1 D% n c, F, r8 }/ c) C1 }: T - 0.8660254037844386
复制代码
& z' [2 V; X/ p) C/ Z1 l! p) Wmath.cos(x) 求x的余弦,x必须是弧度6 f# W$ p. G) I
- #求x的余弦,x必须是弧度
5 g3 U; z4 {4 o; V' Q' I' H5 L3 a - cos(x)9 Z1 f; ]! T- W4 P3 J; f
- Return the cosine of x (measured in radians).' g/ u0 m6 o4 K# l# a! r4 _+ |
- #math.pi/4表示弧度,转换成角度为45度
5 V/ \& g6 u4 `0 p+ {9 s3 F, Q - >>> math.cos(math.pi/4)
, Q& F9 g& Y9 |8 l B: h- D - 0.7071067811865476# j1 [+ ]" t$ y" q
- math.pi/3表示弧度,转换成角度为60度5 l$ B- c! P& C: V; y: |
- >>> math.cos(math.pi/3)) S9 b! H- v7 k8 d4 C* H
- 0.5000000000000001
, M. o2 x. H7 ~ - math.pi/6表示弧度,转换成角度为30度
: [, }/ ^% z5 l7 t- Z( i$ B - >>> math.cos(math.pi/6)* c/ P+ R) U6 C
- 0.8660254037844387
复制代码 ( D# u1 ?( d5 J8 G. ~' R
math.tan(x) 返回x(x为弧度)的正切值
7 |+ V4 t5 {$ f- #返回x(x为弧度)的正切值
! b1 A, } K2 M! N8 ~ - tan(x)9 \/ O1 x! h6 V0 T, Y4 x
- Return the tangent of x (measured in radians).
" a* g4 F$ K( B6 `6 r - >>> math.tan(math.pi/4)0 V* @4 W. s$ J: @! l" s( A+ U' J! R
- 0.9999999999999999
8 m, y6 p3 n+ \, x" c. M4 i - >>> math.tan(math.pi/6)- l4 }& i4 }+ S: S, h; G
- 0.5773502691896257
( Y/ h3 A6 a# A* q u - >>> math.tan(math.pi/3)
8 Q4 d8 T3 ~" y* s9 u6 g ?, C4 v2 q+ ` - 1.7320508075688767
复制代码
' z% d9 K" `' Omath.degrees(x) 把x从弧度转换成角度
6 a/ i u' P- e9 r0 A) j9 Q- #把x从弧度转换成角度$ P. ~2 V- U, @) {8 q
- degrees(x)/ v* `$ l4 S) p; f0 r' U
- Convert angle x from radians to degrees.9 `+ d# T3 V; \' t
- ) m' y5 A s' h* ]/ N
- >>> math.degrees(math.pi/4) A6 C0 ?: {% V K* g' O9 n _
- 45.0+ H8 R/ b7 ~2 m2 R2 t7 F
- >>> math.degrees(math.pi)
" P6 {8 _+ S$ P4 \* A& u5 R# J - 180.0/ h# F: S; g( M% x$ w& M
- >>> math.degrees(math.pi/6)
% O) ~" T% q6 m7 T: s) f* R - 29.999999999999996
" C I; a. P' r; y) E% n. h5 `: { - >>> math.degrees(math.pi/3)
/ D) H) v$ J( @8 t; F. D - 59.99999999999999
复制代码
. P0 S2 I8 S. t' e% wmath.radians(x) 把角度x转换成弧度
( v4 [; W* n+ B' \! F- #把角度x转换成弧度
" Y) n. t; b9 ^0 H - radians(x)* Q' M1 E& Z M& p9 \ g0 Q1 g! x
- Convert angle x from degrees to radians.
1 [ t3 J" Z9 w - >>> math.radians(45)/ T7 e$ @9 o6 T! n9 d+ d2 V! ~# |
- 0.78539816339744833 I1 D2 W% v0 s s9 f6 e
- >>> math.radians(60)( R, u/ }% T6 f* O% d! h- i5 X
- 1.0471975511965976
复制代码
: P2 p- F F6 Cmath.copysign(x,y) 把y的正负号加到x前面,可以使用04 O2 s& i' Q! U- j$ ?
- #把y的正负号加到x前面,可以使用0
6 P1 R7 y# ]- w- }7 Q' a - copysign(x, y)
& {; }6 ?( R6 N2 b+ i+ I - Return a float with the magnitude (absolute value) of x but the sign + q; n# n3 P/ I7 l& p; c6 ~$ j( e
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, M7 { |$ @0 W ^8 C/ k2 _ - returns -1.0.# P1 ^" o2 P; ]/ R+ m+ q
- - {% z; V4 n p+ }, O' U! C, B+ l( [
- >>> math.copysign(2,3)
& X2 d# u, Z3 m) Y: a9 I" e: ^1 z - 2.0
1 a$ K+ P1 N9 Y% L( k F$ p5 d - >>> math.copysign(2,-3)$ U& z4 k6 _/ |0 d
- -2.0
* I& u4 A1 v$ W; A% q, ] - >>> math.copysign(3,8)
1 H# R" m; S& S' H2 W4 l2 u - 3.0% K. i' h. y4 g6 G2 N
- >>> math.copysign(3,-8)% q8 y5 a" v; z8 w
- -3.0
复制代码
1 k1 H* I; ]) k' {3 }* L1 zmath.exp(x) 返回math.e,也就是2.71828的x次方7 m% z6 u6 J a0 j9 J8 ?& a
- #返回math.e,也就是2.71828的x次方
% c, Y" W. }3 d4 Y - exp(x)" G, V+ F/ c, A1 p* C: K& K
- Return e raised to the power of x.' G' {! Q" w; K- A; P" G+ ?
- 3 e# n$ ?0 H3 N9 J6 z% u4 o
- >>> math.exp(1)
: b3 y/ U7 Y, o: a( r6 K' \, T- O% D - 2.718281828459045* F: E* \( D m! y( S
- >>> math.exp(2)1 W$ f+ L9 c; f, |: t( k8 |
- 7.38905609893065: C4 `: s9 U, s2 r5 M1 c
- >>> math.exp(3)# R# ^7 q- s0 z0 y* T g# F
- 20.085536923187668
复制代码
# C6 L6 B: I1 N6 \( Omath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 ~' e# R% T" C3 P- Y" n% v
- #返回math.e的x(其值为2.71828)次方的值减1
l: h/ H* Q) N, F5 @9 q% C% Q. } - expm1(x)8 d* `; h( {; t' x& P7 |
- Return exp(x)-1.
9 ~1 c( D% ]+ K* r - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
( g/ n! S Z( |4 C4 Y
+ ^5 _2 m4 y/ q |; Y, z" d/ ^) q- >>> math.expm1(1)- ^7 h9 |0 @7 _
- 1.718281828459045
" M" g' h, Q% o' f2 q - >>> math.expm1(2)
! ]; |# ~3 A5 w" ] t - 6.38905609893065/ O' W" }8 S' X1 X8 \2 i
- >>> math.expm1(3)& q3 N3 h8 C0 w5 T O8 R
- 19.085536923187668
复制代码
6 i& n/ B. t D& G0 K" q9 _math.fabs(x) 返回x的绝对值0 Y( l9 n: C2 K& U% @
- #返回x的绝对值0 R, l# S; e/ m) O2 S
- fabs(x)
* E( b W [; J/ k8 ` - Return the absolute value of the float x.
+ P+ f( \6 V" J+ l% z
1 ]( f! y. V5 k2 l" [# Z- >>> math.fabs(-0.003)
; ?8 E3 x5 n8 Q$ L - 0.0031 F4 N# c4 A+ a: R T7 }
- >>> math.fabs(-110)4 ]* P) N0 a0 I9 n
- 110.0
* F! k. h% _# c: _ - >>> math.fabs(100)
0 G: Q) q2 a- X& o) R7 B1 I7 q - 100.0
复制代码 7 U, H+ T: R7 c5 \5 B, H7 Z ~+ i
math.factorial(x) 取x的阶乘的值
+ @( k u9 O! U$ q7 b2 d- #取x的阶乘的值' s6 S! i0 R- b8 K4 S
- factorial(x) -> Integral5 f/ u4 o. I6 i8 A: E- M
- Find x!. Raise a ValueError if x is negative or non-integral.5 Q% j2 M1 p; |+ r u; q
- >>> math.factorial(1)
/ Q* u7 S* |+ n( I - 1
) y [+ N! g4 \- p9 i - >>> math.factorial(2)0 K, R* S8 H4 c) Y' x4 o
- 29 \( }! b4 O, H3 h
- >>> math.factorial(3)$ {) B7 I# U$ E; T
- 6
. G# @- }9 m3 r3 M- b - >>> math.factorial(5)
" X5 B9 P/ B! O8 Z* y# w - 1201 d7 X# \9 Y$ ]0 G8 q
- >>> math.factorial(10)0 W7 \, m# t; y0 a' r/ A
- 3628800
复制代码 ' b- j1 |) [3 t3 S
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
9 l: L; o8 f7 u9 J- #得到x/y的余数,其值是一个浮点数
; O- E* B7 j- x; f$ C - fmod(x, y)
1 y- j+ D' x/ M7 E - Return fmod(x, y), according to platform C. x % y may differ.9 L, @4 c* k/ {$ s6 R6 n; P
- >>> math.fmod(20,3)
2 P. X% _% H; l3 A - 2.0
$ ^' T2 X' D5 O- s - >>> math.fmod(20,7)* l0 d4 a! s, o, k9 M2 I: q# h
- 6.0
复制代码
3 B) j- r( r3 i* o4 h1 Mmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2 a% R5 Z) p- m) _* W; O- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
0 i( L P0 H0 E/ m" l. |) x - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: B# s9 }- y, ]
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 U) q9 h3 s$ ]8 ~8 Y. @ - frexp(x)! y! i/ n& ]- `. b- Q* s
- Return the mantissa and exponent of x, as pair (m, e).
7 g) C; v0 {3 E S H - m is a float and e is an int, such that x = m * 2.**e.
: \ S4 |7 @8 U1 w; f: t7 n - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
% J. Y( S7 z, A - >>> math.frexp(10)
1 j& C, h4 S& A7 G' p2 ? - (0.625, 4)3 J' Q+ q* z& ~- \
- >>> math.frexp(75)4 i6 c) n# G2 I3 W# }& N
- (0.5859375, 7)
# Y. M7 c7 @, t* R - >>> math.frexp(-40)& U2 B! C0 o1 A4 a5 P% i
- (-0.625, 6)
' d$ h% \2 g1 I9 w% }7 t - >>> math.frexp(-100)
1 X3 ?& K E* d4 ] - (-0.78125, 7)
. T% q( V- m& M3 K - >>> math.frexp(100). [# {8 I) S/ }4 F
- (0.78125, 7)
复制代码 ) P: |& X% f1 V" K% j) t
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
2 c4 \* N; Q3 R4 Y- #对迭代器里的每个元素进行求和操作
/ K w' S. e8 G- |; v; Z - fsum(iterable)7 F& ~* }; G# e* [% X- k
- Return an accurate floating point sum of values in the iterable.4 M2 h5 D# Z: {* ^1 u( t
- Assumes IEEE-754 floating point arithmetic.
4 h1 |* m3 f& w1 I3 x5 Z M - >>> math.fsum([1,2,3,4])
' }, W B0 x6 O1 h3 ]; i: O; l( a/ { - 10.0
/ ~9 K" _0 }% n1 z# z/ f - >>> math.fsum((1,2,3,4)), G4 ?5 O# D* W0 b
- 10.0
4 g; U1 X9 R. \ P: S - >>> math.fsum((-1,-2,-3,-4))7 Z w, }+ Q* i! K- l( A o# O
- -10.09 Q( q! i0 _, _: a, H) i( S* M
- >>> math.fsum([-1,-2,-3,-4])
1 _, a& k7 q" o0 Q' ` - -10.0
复制代码 . K; `) a1 G2 `+ B+ ^
math.gcd(x,y) 返回x和y的最大公约数
" R) i, w9 ]- {8 T1 l$ t8 s8 m/ B- #返回x和y的最大公约数' Z6 |6 S' c3 K
- gcd(x, y) -> int
0 }9 O- U2 l/ Q, X4 ]4 U - greatest common divisor of x and y2 ~) Q7 m" c7 `% k1 K O
- >>> math.gcd(8,6)
1 l1 l7 a1 f) r% Q5 S - 2
( c. e- N, T. _. k) S; u* Z' l, | - >>> math.gcd(40,20)
, M! ]6 I& t7 b! f q6 h; T - 208 @' y( a3 c. x! F; B# k! L1 o" }
- >>> math.gcd(8,12)3 \: Y, P" A3 f0 \5 h; U" m9 n; {
- 4
复制代码
1 ]# S9 ]" Q5 d0 Ymath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
; x6 X9 l. O8 I7 w( n- #得到(x**2+y**2),平方的值
# h! x4 d8 n8 [* z - hypot(x, y)
m) |- ~. k% @ k& A! m% f - Return the Euclidean distance, sqrt(x*x + y*y).! g$ g( W2 k; W2 h7 D) l) T
- >>> math.hypot(3,4)
) {9 ]) {0 `8 L0 h - 5.08 q! l, ?& \: [# A
- >>> math.hypot(6,8)
3 ^% w; g ~, ~' X! _2 \6 N1 u - 10.0
复制代码 , ?" r$ f3 z" y0 t* s
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False h% f6 D* I+ t' w
- #如果x是不是无穷大的数字,则返回True,否则返回False
2 E; S7 e9 {6 u( ? - isfinite(x) -> bool, z$ R1 W- ^! {% u" q! L$ ~
- Return True if x is neither an infinity nor a NaN, and False otherwise.
% e) ^4 h! x4 q* R. s - >>> math.isfinite(100)
6 L* e% m9 L: y6 s. v, C1 @5 e - True
+ t. F' ^0 m8 q# y# e! C2 }0 M - >>> math.isfinite(0)) Y, i. T. d1 X! h' V6 z* p
- True3 Q; v# C. A& S
- >>> math.isfinite(0.1)8 P. l5 `. w$ i0 I
- True
i H5 ?) W6 b- \8 J" Q - >>> math.isfinite("a")0 ^6 s3 B0 }& L$ Y4 M! v/ d& _
- >>> math.isfinite(0.0001)2 m z0 F' z9 s; |( I- z
- True
复制代码
6 W! s9 Q% E9 a) Xmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
/ ?; b% Q3 i/ |- #如果x是正无穷大或负无穷大,则返回True,否则返回False" S. D9 f2 W4 ^# m$ Y# q
- isinf(x) -> bool
) i9 @9 H# U+ G+ m3 ]( Q - Return True if x is a positive or negative infinity, and False otherwise." v5 e8 m' e0 W& |3 }/ a
- >>> math.isinf(234)" m9 K+ U7 D s- t6 K# J
- False
. s% E% o2 G% i4 H9 q - >>> math.isinf(0.1)
1 j$ h+ L' o' q - False
复制代码
- ^5 ~. E9 c* N& Xmath.isnan(x) 如果x不是数字True,否则返回False
7 ?- [" G4 h) d9 [7 |- #如果x不是数字True,否则返回False
* }' F7 _- k2 t9 Q) O7 X - isnan(x) -> bool. f' E0 I* r6 X Z
- Return True if x is a NaN (not a number), and False otherwise.( A/ D& c9 j( N7 y
- >>> math.isnan(23)' O' g8 J. U" [; S$ Q
- False
! k" w, r/ c5 Q6 M& g! S8 B - >>> math.isnan(0.01)9 N1 Q& t; F+ d X* o
- False
复制代码 1 H! z8 ~3 i0 Y% G7 A" p9 x
math.ldexp(x,i) 返回x*(2**i)的值. m& `, h' d K) |/ o
- #返回x*(2**i)的值
; w7 i( ~" q' r, R; w6 [ - ldexp(x, i)
' s: m3 \( ?8 n# r2 n0 \) _6 u - Return x * (2**i).
" K8 K1 ]$ e8 Q: V' A - >>> math.ldexp(5,5)
9 s1 i3 i! g6 N, }/ {3 f - 160.0
7 K% t/ P/ |5 w% @1 m$ R - >>> math.ldexp(3,5)
7 \# @5 U& z) E+ M - 96.0
复制代码 ' {. W3 y- m# q0 Q$ s5 q% T
math.log10(x) 返回x的以10为底的对数. U; A; {6 Y8 ^
- #返回x的以10为底的对数
6 h; l' m1 m5 v0 V! @2 \ - log10(x)
3 E3 a0 v1 [$ J8 k4 N6 K( ~8 R - Return the base 10 logarithm of x.
6 {& E- U; f& Y$ j+ L - >>> math.log10(10)2 L% Y1 g* W7 W' I
- 1.0
( f5 I, f0 x+ O' d' `* I - >>> math.log10(100)
$ r6 O% W& x" Y - 2.0: ?3 @+ {# Y3 I# o% I
- #即10的1.3次方的结果为20
8 R: @+ }7 ^. D3 K. I5 T - >>> math.log10(20)
4 b4 A& `" q+ P2 R- e& \! H9 E - 1.3010299956639813
复制代码
, r) b& u( C) ]: amath.log1p(x) 返回x+1的自然对数(基数为e)的值
! ~" g, L4 v! h" G. e$ b8 ?- #返回x+1的自然对数(基数为e)的值" N6 K6 n, N1 h$ ?" q
- log1p(x)- R! |$ r3 _: o% j5 m" m# _
- Return the natural logarithm of 1+x (base e).
6 L8 Q5 S" f( [# o9 q - The result is computed in a way which is accurate for x near zero.
3 E7 s6 X) e& [0 ^: O" ?3 P/ E1 F1 M: q - >>> math.log(10)
" v% ^8 h) U: P4 D/ W+ A - 2.302585092994046
$ g) d: }* N( u: Q - >>> math.log1p(10)
) b4 z7 }$ L5 B! e - 2.3978952727983707
, T9 S1 k7 q, z6 m% L, k - >>> math.log(11)8 w( P- r& T! V, R
- 2.3978952727983707
复制代码 % `2 }& g$ r- M" G' j1 L. v$ z W1 y
math.log2(x) 返回x的基2对数; I! S& S/ E3 }7 m( K
- #返回x的基2对数
- Z/ h4 P: B9 Y# S, U2 V* G5 y/ B - log2(x). g4 Y% O2 `4 N* y, J# V! b9 m
- Return the base 2 logarithm of x.
) L5 R. ? ^1 t# x$ N2 z - >>> math.log2(32). k9 b' z% d! F p% @+ a. _
- 5.0. x& t; |% \3 }* ]6 w+ D7 l
- >>> math.log2(20)3 R/ a) c5 n( f
- 4.3219280948873636 X/ W$ E% A5 S2 r' T% t: X
- >>> math.log2(16)1 _7 V, o! h# e0 C' y4 U
- 4.0
复制代码 3 l* Z' B9 R$ P" r' i# J4 e2 c6 r" X
math.modf(x) 返回由x的小数部分和整数部分组成的元组
2 C- N. A( ]! t% Y7 {* A: k- #返回由x的小数部分和整数部分组成的元组# f B; Y/ c6 k
- modf(x)# a! A+ e; @9 H# F$ c* O/ p6 t
- Return the fractional and integer parts of x. Both results carry the sign
$ A* k7 b4 A. s3 c- Z6 y6 J - of x and are floats.# W+ Q7 P& C8 x& H# s4 l. x' Y
- >>> math.modf(math.pi)4 L) [2 o; e4 Q6 A; R) `
- (0.14159265358979312, 3.0)4 J' w W& s* m, T. b2 B
- >>> math.modf(12.34)
* ^+ S6 q+ t# N, r, _ - (0.33999999999999986, 12.0)
复制代码
: ]& a: o$ B+ ?0 a8 i4 I( Zmath.sqrt(x) 求x的平方根2 m8 b% q8 n8 Y# @# B0 y( L
- #求x的平方根: c' U5 e3 s6 k9 m; H9 f- V- ]% w
- sqrt(x)
b; T* ]; z% S; T6 T - Return the square root of x.
2 w6 d, g0 h; L3 e( t5 I - >>> math.sqrt(100)
8 h5 ]4 G9 A0 r& Z - 10.0
# |6 ]4 Y, W$ _9 N' Q* D- o - >>> math.sqrt(16)
$ ^$ K/ Y5 D" O* h$ c - 4.05 E* V3 W0 @" @4 S) M% [) R/ |
- >>> math.sqrt(20)7 X& |! V( s' R
- 4.47213595499958
复制代码 " p# x" e. }% M
math.trunc(x) 返回x的整数部分- #返回x的整数部分9 f9 C7 j7 {4 R Q+ r V2 P- ]' ]
- trunc(x:Real) -> Integral
( n+ [! A5 g$ t( i% l* n0 j9 y - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
0 d7 A! p: E X - >>> math.trunc(6.789)
: a& I7 ?1 ]' g8 M5 M. `/ k - 6/ t5 W. ?( J! [1 M3 ~$ A' V: l3 `: O
- >>> math.trunc(math.pi)
4 d- X+ f: }+ ]1 u2 m$ P - 3. T/ |9 ~7 a* y, I! ~; Q: G B
- >>> math.trunc(2.567)1 ?' v2 M7 O |' E7 f8 }, q% E
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|