马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 I, ^) d: u C& ?% i7 e; l
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 J1 T& ^4 l% ~/ a* j2 x* A
; \; p: J! k& }+ d p方法1:& T1 B4 H4 }2 [$ ~8 w
- >>> import math, s- G3 _2 d1 e
- >>> math.sqrt(9): x% A/ V$ M" M3 _7 Q/ M7 g
- 3.0
复制代码 方法2:
$ C4 H& Y" U0 {$ s- >>> from math import sqrt! T5 L; x5 `2 E
- >>> sqrt(9)
* b* V( N! P' D+ z: ~( v - 3.0
复制代码 ( T4 f) @& L& ?' Q3 H9 ]! ]7 W7 f
/ u9 h W5 [; y9 p4 {& amath.e 表示一个常量8 h" d3 P' v" [) _5 X* u% Y
- #表示一个常量
5 f, Y) Z' W p# K4 a. b2 L! | - >>> math.e
5 G' u% c; A; @! O* ?9 \+ ~$ f - 2.718281828459045
复制代码 6 | k9 w3 X- t% ]( D
math.pi 数字常量,圆周率2 ]( p& ?# D6 M! ~1 J; J
- #数字常量,圆周率) h; _( c3 Z4 B4 r, V# \5 i7 m
- >>> print(math.pi)
4 C" f# X0 q) O+ N - 3.141592653589793
复制代码 . [5 b+ f0 o9 S6 V0 ~ r
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 z% A5 l: I& l; t8 Q- #取大于等于x的最小的整数值,如果x是一个整数,则返回x) F% T- B" r3 [; C' a
- ceil(x)% \. `/ G9 J2 P' Y! A9 W
- Return the ceiling of x as an int.
, J. c( l, _ b; v - This is the smallest integral value >= x.
# N! V- h- \4 ]+ _ - ! c# f1 Q. a6 ~0 Q$ E7 M
- >>> math.ceil(4.01). m" d; j6 }$ w V; o
- 5
9 r9 d: {* P1 [0 f# |. V1 | - >>> math.ceil(4.99)3 P) w; K% |0 \, M
- 5
3 }7 H( M/ e+ ^1 H* z1 }4 C( d' } - >>> math.ceil(-3.99): q7 h% a8 a$ s/ A; X: S, Y
- -3
4 z4 s8 h* m* u7 K - >>> math.ceil(-3.01)
8 E; W8 I* h' R5 ^) a P - -3
复制代码 8 C" K- l' R$ S, `) n' V7 z
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) _/ E# ?: [( i- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
* n% S. t7 ]/ a - floor(x)+ h/ G, V$ I# p. @5 P$ |
- Return the floor of x as an int.$ R2 {' y" p, {/ x2 _5 J
- This is the largest integral value <= x.
" V' m% m/ G& t5 i - >>> math.floor(4.1)# e0 |: a4 J, ^6 j+ G8 M9 b
- 4
$ }% F- r0 Z# X - >>> math.floor(4.999)0 `0 @8 M( P: G
- 4
9 U6 J2 W1 ?: t- Z% g - >>> math.floor(-4.999)
! N5 [6 L8 O' |8 l! x' } - -5
9 k/ y/ x7 S1 P! g - >>> math.floor(-4.01)- ~9 M! K2 A y+ P/ E: n# p: q1 F
- -5
复制代码 b+ y9 O7 F0 [& ^
math.pow(x,y) 返回x的y次方,即x**y' c2 W6 \' y, c7 b3 V1 v
- #返回x的y次方,即x**y
5 u/ T9 ~4 l, r% b8 h - pow(x, y)
$ Q/ i2 g! Q+ J# E3 e4 I - Return x**y (x to the power of y)." f5 m5 B4 g' G, e1 W
- >>> math.pow(3,4)
8 P8 J4 ]% F: F - 81.0
1 F) P) G' u8 v! h - >>> ) v! d3 |3 d8 W
- >>> math.pow(2,7)4 g6 ]: j- i. W1 @
- 128.0
复制代码
: l! ]- j8 H6 C R% N1 G Q6 b, Smath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ s( S* `+ S4 ~) B7 r' t$ r- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ i- R: W1 i+ X) `( Y$ j/ W - log(x[, base])3 k' _ D& m6 G
- Return the logarithm of x to the given base.
- s- X% O6 O4 L" J# Y - If the base not specified, returns the natural logarithm (base e) of x.
+ X0 X5 k7 G; E - >>> math.log(10)4 s1 q! r7 h5 j4 o
- 2.302585092994046
1 _0 N) X6 ^3 r; z ` - >>> math.log(11)
7 w! y6 x( a( J: N - 2.3978952727983707, t* o% {/ u! C+ A
- >>> math.log(20)+ p# a d0 A" t6 a. l# h$ g
- 2.995732273553991
复制代码 $ u$ Q. y' t. E/ a9 S- o
math.sin(x) 求x(x为弧度)的正弦值; m$ {% \- {5 l% O5 a. C2 d" X
- #求x(x为弧度)的正弦值8 `" B6 S! ` x
- sin(x)
. X% Q0 E) ^) p' j - Return the sine of x (measured in radians). U3 {$ s G d4 N. y+ @) a
- >>> math.sin(math.pi/4)
% j8 @; |! g# f1 C- `' s - 0.7071067811865475
0 f) Q# H+ W K1 W$ |- o - >>> math.sin(math.pi/2)
. s0 e& v; `& M - 1.05 o1 Z! }, X) k
- >>> math.sin(math.pi/3). c' K5 O2 a8 T8 x6 a
- 0.8660254037844386
复制代码 , p( A* G% X, O8 i4 o1 w
math.cos(x) 求x的余弦,x必须是弧度
/ h+ b& M k9 v- #求x的余弦,x必须是弧度
6 }# ^+ d& |/ W - cos(x)
4 K4 N- K$ w/ j& ]3 X/ t8 i0 O - Return the cosine of x (measured in radians).
+ }1 p) y$ k7 k: Y b/ r5 y( M - #math.pi/4表示弧度,转换成角度为45度) x1 H7 k9 f$ a+ K0 U
- >>> math.cos(math.pi/4)
6 ^! q f7 [0 I - 0.7071067811865476
0 J& x3 H2 d: h" V5 x - math.pi/3表示弧度,转换成角度为60度! o5 ]+ L: y, F
- >>> math.cos(math.pi/3)
4 O @8 ^9 r( b+ V6 V; L - 0.5000000000000001$ n# [7 i ~* L" m# D
- math.pi/6表示弧度,转换成角度为30度
/ p2 ?7 t. {2 Q7 j - >>> math.cos(math.pi/6)4 @% h% m& k! F9 L# B# Q( Y" L4 ?
- 0.8660254037844387
复制代码 6 H* S' I' |+ b3 E) a4 G
math.tan(x) 返回x(x为弧度)的正切值) Y8 w/ L3 a. ~! o' z2 f: y
- #返回x(x为弧度)的正切值
3 M9 W* J, n: N - tan(x)
9 @8 E- p- `# E2 N1 S; j - Return the tangent of x (measured in radians).4 P* F K- @: b9 q0 u8 N
- >>> math.tan(math.pi/4)3 v9 k" e9 Q; p$ w( O
- 0.9999999999999999. o) |( j* H' u2 d( O
- >>> math.tan(math.pi/6)! v* q: J R* g9 z* K# m* M; B
- 0.5773502691896257
! k# i1 t7 ^! u; U1 `" [" W, H/ j$ m - >>> math.tan(math.pi/3)
) t8 I: [; Q2 X& c* z - 1.7320508075688767
复制代码
: V& W% J2 C- H: _& W5 G; imath.degrees(x) 把x从弧度转换成角度0 }2 A% H- H P8 U% t4 T
- #把x从弧度转换成角度# X+ m1 @: K* o5 |( ?( U
- degrees(x)8 C. e, ]" k+ g# e% V( s
- Convert angle x from radians to degrees.
1 ^+ j5 k( @% Y4 ]$ Z( P - 2 A* g; G1 q. P- d2 P1 _2 P
- >>> math.degrees(math.pi/4)
- O+ f, t$ f8 i4 W5 S. I - 45.0
# U- o x S! z% D1 [ - >>> math.degrees(math.pi)
5 @+ b4 C9 v. b2 [% j - 180.0
* \1 ^% O3 v0 `- ^ - >>> math.degrees(math.pi/6)
# M! P5 f' ^3 N* h' l - 29.999999999999996, H) o2 u# O1 h) d
- >>> math.degrees(math.pi/3)
( R" ~# P; K, O" I# j - 59.99999999999999
复制代码 + U$ Q3 l3 p" Y* Z) H
math.radians(x) 把角度x转换成弧度: i; \# W. i/ {
- #把角度x转换成弧度
/ y' H4 l! {7 r( @/ p: Z - radians(x)) z% G3 I5 J" F7 Z6 F
- Convert angle x from degrees to radians." B0 w' c( Y% ^+ V% q
- >>> math.radians(45)
; D5 Z9 ]6 s4 j& ~0 P' j - 0.7853981633974483
) ?% h7 i( a' N# U - >>> math.radians(60)
* J4 e+ B! O: ^7 R& L - 1.0471975511965976
复制代码
% ~* [7 \/ O! O7 ^) Xmath.copysign(x,y) 把y的正负号加到x前面,可以使用0% r9 T% A# k) u
- #把y的正负号加到x前面,可以使用0
7 R, O2 G( J4 x+ } - copysign(x, y): o! a3 q( b7 k1 A J! T* M( A
- Return a float with the magnitude (absolute value) of x but the sign
& x9 _" ^# S$ q T - of y. On platforms that support signed zeros, copysign(1.0, -0.0) # ~9 z9 |6 ]8 u. v
- returns -1.0.
% }; b: L# R7 y - ( |0 r$ R; w& w! `) r0 I' M
- >>> math.copysign(2,3)
3 [0 I% N. m$ B( j0 l) L% ~ - 2.0
5 Y1 w4 L1 E, f2 m/ D7 d( f# a - >>> math.copysign(2,-3)8 F+ T& K5 s2 C+ t, j$ V
- -2.0
G7 d+ v, Q. L& \* P; o - >>> math.copysign(3,8)3 b; }+ W6 _4 _3 s* H
- 3.0
* K! P9 [( ?' t$ I - >>> math.copysign(3,-8)
, N: `8 J- d$ \! w0 N+ i- e - -3.0
复制代码
' a K" R! `0 vmath.exp(x) 返回math.e,也就是2.71828的x次方
8 {8 L7 `" t: k+ F# p- #返回math.e,也就是2.71828的x次方# |- n+ x+ y$ g3 b
- exp(x)6 }$ C I# Q Z5 W1 t8 s2 w
- Return e raised to the power of x.
% q0 g) b L/ k2 [! b - 4 f( W1 j3 f3 d2 U0 @$ [/ q
- >>> math.exp(1)
/ [; }' I% R# C' s0 M9 h$ F' @; w - 2.7182818284590451 D5 i: S/ ~ d" D9 k
- >>> math.exp(2)
1 H8 b& ?' s& Y) g5 E9 Z: n - 7.38905609893065% ^. h# |2 O- x* g. P; L( V2 X: }
- >>> math.exp(3)
2 ], F* Z( |7 Z( h; p4 v5 c - 20.085536923187668
复制代码
- i: q% c7 W+ N+ i, Smath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) y e$ G$ k! K! N8 ]# n
- #返回math.e的x(其值为2.71828)次方的值减1
- a" M/ s* G7 t; I2 H; H - expm1(x)
3 I2 [" F. H8 Q \ - Return exp(x)-1.( o3 u; M* m' p
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 g4 x E% \: @6 a3 @ - $ l3 s! Z; V9 o# O' ?/ v$ F
- >>> math.expm1(1)' Z. q, @; Z3 o2 K% s0 C, w+ o, C4 ?7 E
- 1.718281828459045
2 ~# N/ _+ G1 z2 M; u$ ~ - >>> math.expm1(2)5 ^& z5 a: a6 O T" h9 x) U
- 6.38905609893065
h% z" p' q/ P* y e( S - >>> math.expm1(3)8 Y' r5 E* _6 C
- 19.085536923187668
复制代码 ; T' i* A" ]; N
math.fabs(x) 返回x的绝对值0 a% c8 B. i; Q
- #返回x的绝对值
4 p/ p9 v' b3 e" Z1 `; [5 @( w - fabs(x)% B7 x2 J g7 E+ i& J
- Return the absolute value of the float x.2 |% d( B9 {% g4 _8 Q# ` P. n
- 2 Y) w# f; V, x* I
- >>> math.fabs(-0.003)6 p; C! |8 U# |! a
- 0.003
9 C8 y. ~1 D8 A- w6 ? - >>> math.fabs(-110) J* Y$ S4 z4 H8 ~
- 110.0 w* r* _4 u0 I6 R {
- >>> math.fabs(100)
, c4 d( R$ Y L( h8 \! ]4 g8 D - 100.0
复制代码
& q8 k6 \6 N, L+ o' \/ N' B0 hmath.factorial(x) 取x的阶乘的值1 S1 f% O) D. q! Y
- #取x的阶乘的值; j5 p* D$ X( h) l- x7 d& |! v' K
- factorial(x) -> Integral
- A# s1 H. G: d8 ]' Z. @ - Find x!. Raise a ValueError if x is negative or non-integral./ l: S# A5 d4 J/ x
- >>> math.factorial(1)& d2 t+ A& z8 f! \$ w
- 1% [8 m2 O% z! q2 [ O' Y _- @, A
- >>> math.factorial(2)
% W" m$ {4 w; L1 x - 2
8 z$ Z) Q( z M+ r6 e) y6 T - >>> math.factorial(3)
# r0 t% ^( M6 f p/ v - 6
8 ]4 f/ W4 B- ~1 d0 F5 Y7 a( R$ @ - >>> math.factorial(5)
& x5 H' z, b9 w- @4 A - 1200 U! P4 O5 I* \
- >>> math.factorial(10)
: d( H( {. ]4 q7 Q' ~' O+ a - 3628800
复制代码 0 b7 i& Q2 j- P
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
1 _$ x0 n( K/ V3 C" b; ~' e% A7 q7 H2 V- #得到x/y的余数,其值是一个浮点数; n* J, n: y# b2 i
- fmod(x, y)' ^1 c. E2 t# Z# ?
- Return fmod(x, y), according to platform C. x % y may differ." G7 v9 F( S' w0 p' ?
- >>> math.fmod(20,3)
7 M/ s, x- L, e/ {! Y. n - 2.0
' ]5 Y4 u x5 b% F( h8 r X - >>> math.fmod(20,7)
# U# j, c! K4 }& t. C - 6.0
复制代码 $ z" v+ B* Y# j
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 `- Q; E# a: K3 k) c- c0 F1 R; {/ ~- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
- c* p) @) c p6 c5 C$ `1 } - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
% l1 p, _$ i2 J7 K& b* J, P ? - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
! A7 t: x# q' c - frexp(x) q+ l. k; k W. |0 c8 y
- Return the mantissa and exponent of x, as pair (m, e).5 t: L1 [9 v8 Z
- m is a float and e is an int, such that x = m * 2.**e.
# C' Y; c6 _; W/ b+ b* l - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
. F6 Z4 |# U' y3 K1 A# m - >>> math.frexp(10)" m7 s( n" n: x1 @( I8 v* S
- (0.625, 4)
7 r9 H! [8 x1 d0 w, w; a - >>> math.frexp(75)
, F: K8 K0 k. n j. _1 h) B5 T - (0.5859375, 7)
$ M" H! E8 z0 P3 n0 Y9 c/ `2 ]# J' i" C5 {3 e - >>> math.frexp(-40)0 e7 R& l' F& V3 @8 o
- (-0.625, 6)
7 Z3 I% P; }" B8 o0 i, t - >>> math.frexp(-100)1 b6 b7 O+ @0 |1 X, `
- (-0.78125, 7)* P' V Z$ Y! C4 W" F
- >>> math.frexp(100)
* y" |- p0 T4 U - (0.78125, 7)
复制代码 - O3 Q' P" z& l
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)6 O/ g' Y; K. I/ s
- #对迭代器里的每个元素进行求和操作
8 A1 g1 \8 H1 X" B3 q6 z - fsum(iterable)
- }+ J; h# X, H8 x) t- a" O$ }, o - Return an accurate floating point sum of values in the iterable.3 |' J7 h I2 R7 j
- Assumes IEEE-754 floating point arithmetic.2 B. K+ N) P# R" u6 [4 j$ ]
- >>> math.fsum([1,2,3,4])$ k$ p- n1 Z( K# `2 f) R
- 10.0
# m0 ]! C8 @7 S- x5 [( V( `1 |6 j - >>> math.fsum((1,2,3,4))5 J* ^' _, {; F e* Y7 y+ ^/ O0 Y6 j
- 10.0
7 J7 D% _" Z. f( ~ c; h! f) A: H) f - >>> math.fsum((-1,-2,-3,-4))
9 ?1 [ M' I' Q - -10.0
) J+ t- h4 ]' @1 | - >>> math.fsum([-1,-2,-3,-4])( b/ b& j0 y+ [7 _2 D
- -10.0
复制代码 2 a& Z/ L) y" G S
math.gcd(x,y) 返回x和y的最大公约数
4 M" k$ a/ w9 y5 ~* i4 x" v- #返回x和y的最大公约数
& m5 _) c) Z8 k! Y - gcd(x, y) -> int
# D" N' O# _' K( y - greatest common divisor of x and y
, A- _- X. F& l) C - >>> math.gcd(8,6)
! H, i# f# m$ E1 @5 K5 | - 22 o. X. \6 R$ V! \4 O7 |
- >>> math.gcd(40,20)
! E) |1 ]1 k" Z; X% F. D - 20( ~; T" p% \ ^
- >>> math.gcd(8,12)$ T2 i! G2 Q1 {5 h& F/ I
- 4
复制代码
5 H; I1 W# I2 @4 N" [5 zmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* y& R) c# l) Q8 Q/ u( i4 M. Z- #得到(x**2+y**2),平方的值3 N. B6 q0 `8 E, I+ Q
- hypot(x, y)8 } H* L+ N0 t9 ^3 F" O
- Return the Euclidean distance, sqrt(x*x + y*y).
. Y9 K4 I; Y/ c' u - >>> math.hypot(3,4) \& k4 r G) h
- 5.0' S3 U0 @2 {8 _7 P2 x3 Z( G+ e; M1 y) i
- >>> math.hypot(6,8)( ~- E; ^( ]" ?
- 10.0
复制代码 " L2 y1 x5 I) S
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False3 {: K- s+ W3 C2 K
- #如果x是不是无穷大的数字,则返回True,否则返回False
4 \5 A+ x# i% `9 Y( v& l - isfinite(x) -> bool
]+ O; h6 O s3 K# `. [' @: b - Return True if x is neither an infinity nor a NaN, and False otherwise.6 h' m* a! [$ {5 z/ E* h8 F; J" O+ a- K
- >>> math.isfinite(100)
- |; I* l* w/ m. u - True; N0 N2 q: ]& Q% G; j8 i3 R
- >>> math.isfinite(0)! |. Y6 y9 E, \: n# a+ ~
- True
% n, P5 D- j9 @! j - >>> math.isfinite(0.1)! j' J% h) f& O
- True
) M7 y7 u, `4 u! c% I j4 n4 F* O n - >>> math.isfinite("a")! R2 Y: m# H% K& z0 |
- >>> math.isfinite(0.0001)( u6 i$ n/ z& y. c8 S: y
- True
复制代码 % @3 ]$ l4 S1 J+ e& H. z% b1 |
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, x1 U1 ?0 L3 @5 C+ R6 X
- #如果x是正无穷大或负无穷大,则返回True,否则返回False9 a$ |) X( {+ u. i
- isinf(x) -> bool" Q" y5 d4 y7 _, h- J
- Return True if x is a positive or negative infinity, and False otherwise.
) D2 f! S0 z" Y3 Z7 K$ v - >>> math.isinf(234)
; `5 J% t7 X+ {4 {0 W - False: R- J8 r0 Y$ W% z
- >>> math.isinf(0.1)8 s. e+ ^! m2 x0 y5 y7 y1 C3 @
- False
复制代码 & ]% S+ B i6 }2 d9 H7 H6 a2 I9 k- l
math.isnan(x) 如果x不是数字True,否则返回False
" T t" z. u# ^3 E3 O- #如果x不是数字True,否则返回False: U( B+ M& }7 t+ B
- isnan(x) -> bool
$ z# ^9 q% E' G2 N/ G4 I( \ - Return True if x is a NaN (not a number), and False otherwise.
6 T v: q/ Q; h) _( k4 B* y# I$ X+ u - >>> math.isnan(23)
1 [1 C+ ~% C7 P% _- V+ T - False
. ?) E# s! _5 U9 p. I% t5 U - >>> math.isnan(0.01)" o8 A9 o3 D+ d& O! _
- False
复制代码 ( p+ R8 g0 Y( ` S8 _
math.ldexp(x,i) 返回x*(2**i)的值# `* b: d* B N8 z+ O
- #返回x*(2**i)的值! f2 c" p2 \* t' F/ ~1 W1 K
- ldexp(x, i). T( @( Y4 e5 r
- Return x * (2**i).
1 g q1 a. u0 P( ~ @0 O - >>> math.ldexp(5,5)7 ]8 t9 P2 n2 r, D# o% O& ~$ P
- 160.01 x% r1 b/ q; y4 w0 r, G) ?
- >>> math.ldexp(3,5)
6 a; L8 }* K0 s: F - 96.0
复制代码
. t& |2 L! u, e- w1 Y: n2 D, qmath.log10(x) 返回x的以10为底的对数* }& G5 |8 C8 o* c" ^$ X* ?
- #返回x的以10为底的对数
0 ~9 Q( } O- _5 B( Y _$ B4 T - log10(x)$ ~2 Q' S) f6 T9 D h2 |
- Return the base 10 logarithm of x.: R0 G8 q( W' ?$ i
- >>> math.log10(10)1 p* G! J* U. J! p
- 1.0- [; `; R( h* k
- >>> math.log10(100)
8 \. t4 w4 n" E2 ~6 a1 r/ U - 2.0' J6 {; b d( W/ I5 j
- #即10的1.3次方的结果为207 I7 ~4 a8 Y! |9 Y& [8 f1 S% N! |
- >>> math.log10(20)
2 i5 Q& m8 F7 x3 b! O - 1.3010299956639813
复制代码
' [$ G* P5 H2 ?; y1 Z* ~math.log1p(x) 返回x+1的自然对数(基数为e)的值
8 Z% x5 S2 l! X5 c1 d" S! o- #返回x+1的自然对数(基数为e)的值! L* w& A3 f6 H, ]9 H& p y' E
- log1p(x)
& j, P/ C, Y# W8 y - Return the natural logarithm of 1+x (base e).
& i* ~8 i/ k8 O' P% u1 z7 f, C" R - The result is computed in a way which is accurate for x near zero.
, n9 G; J9 J! }# ]; e - >>> math.log(10)# _& ^7 a2 w- O; D( a
- 2.302585092994046
) J6 T N4 L0 z4 y9 W( [, M- D - >>> math.log1p(10). g$ e/ q' ^, {! r. {- s: A5 f
- 2.3978952727983707
8 p4 s3 Y9 M; F - >>> math.log(11)- y1 ^0 j0 z" S# e; S
- 2.3978952727983707
复制代码 4 h3 _2 g9 s8 v# p& i
math.log2(x) 返回x的基2对数 u, C. |! f4 @3 ~1 X4 _/ q
- #返回x的基2对数
6 k9 a$ j5 l5 z" A! q9 h9 E - log2(x)
% s/ \9 b) D6 T0 o+ i1 } - Return the base 2 logarithm of x.0 X& k3 Q6 z! i$ H4 I4 P- M2 p% o- w0 F
- >>> math.log2(32)6 \ b5 [& d9 h' j6 q) V
- 5.0
, }! n7 H0 v: Q - >>> math.log2(20)
( s+ f" P% k" G: u - 4.321928094887363
- e- B" X1 n7 @, [* U - >>> math.log2(16)2 u8 `- N6 U$ ]
- 4.0
复制代码
0 d, h9 |5 U& M9 Vmath.modf(x) 返回由x的小数部分和整数部分组成的元组9 I. E2 F% D1 C1 o( q- S3 y1 Q# n5 B
- #返回由x的小数部分和整数部分组成的元组
& V0 t% }, Z3 x3 M5 u - modf(x)' z7 u# j/ m0 P! M! T1 n3 Z% }
- Return the fractional and integer parts of x. Both results carry the sign0 @1 J- N, |" o I% k& [
- of x and are floats.0 U& b+ K3 |! t3 V- L& S" f( O
- >>> math.modf(math.pi)
/ Z8 H5 s3 n6 @) M+ b M M* l - (0.14159265358979312, 3.0)
9 U, C" I2 Q& L( X - >>> math.modf(12.34)3 T4 ^# N; Y& O( |0 a
- (0.33999999999999986, 12.0)
复制代码
6 H# n5 X3 w8 z! S/ ymath.sqrt(x) 求x的平方根
# N8 z. ~5 r! _4 b ?1 @- #求x的平方根7 n. d6 e4 h. ]) n' D+ {% F
- sqrt(x)
$ _: O. F/ \6 [" }6 T7 s - Return the square root of x.& ^ g: K: h! O, s
- >>> math.sqrt(100)* C4 A( N- J5 {+ ]6 Z/ t
- 10.0( L' K1 w% A2 q! O7 f
- >>> math.sqrt(16)+ C% Q7 Z$ x2 H, y! E s2 }
- 4.0
4 p# o( s* }1 s6 K% C$ J6 b R - >>> math.sqrt(20)1 c# N! y( ^! ^: Q( R& _# `
- 4.47213595499958
复制代码 % G2 i2 e: v7 `5 o6 D, J! @* g
math.trunc(x) 返回x的整数部分- #返回x的整数部分3 a. E* B( m& \, K( h* l
- trunc(x:Real) -> Integral
' H& ]+ |8 o1 E" [0 ~2 c2 ] j - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ C4 L! D* @& e' N
- >>> math.trunc(6.789), d2 @; F5 R, f$ |/ F& p. H$ N5 W
- 6
% F5 N' `3 s8 e x0 k: b - >>> math.trunc(math.pi)4 T; ~! g: L. o: V; }( {1 h
- 3
8 p/ |# f2 m, g; y% n - >>> math.trunc(2.567)8 \5 W1 _( d/ e+ q6 G
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|