马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 G) Z0 R! @( w4 w- u+ X$ S) d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 W5 s% r0 a, Q8 _5 x$ w2 C& i" R3 k: o C: n. i0 _- G
方法1:' r) X8 z" \* u! b/ c
- >>> import math$ h& |- e) } B
- >>> math.sqrt(9)$ o) [4 s4 M7 g3 T' S) h+ {
- 3.0
复制代码 方法2:& f% p1 g. j. Z- A; \
- >>> from math import sqrt9 R0 h1 F9 |1 e8 L- f6 t: y. c8 D1 R
- >>> sqrt(9)
& @% F+ I# r7 @" K5 d3 F - 3.0
复制代码
+ b1 e' _& M9 c5 ^! C # |( E, H5 F- z R- `3 D# H$ Y
math.e 表示一个常量5 o, [' K3 p# b
- #表示一个常量
3 n3 ~; g5 `6 [2 d - >>> math.e
; F5 J7 c3 |) s& h; g5 H$ l6 g2 D - 2.718281828459045
复制代码 $ ?) E0 f# N2 A1 E
math.pi 数字常量,圆周率0 c* l1 t4 |3 C6 ?$ z
- #数字常量,圆周率
, k* |( d ^: b$ H7 |! p* { - >>> print(math.pi)8 \7 U% O' r- z" ]- |: L' M
- 3.141592653589793
复制代码 5 C0 O4 J) h, \6 |! A! J/ J( v
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x) I, S( |8 l% s# w* s& ]
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
) W& w/ h' E# K' {: ]9 i6 n# U9 o - ceil(x)
" w) j' P9 r* ~/ V$ C - Return the ceiling of x as an int.
, g- ^. W7 u, b2 z4 r+ m$ w - This is the smallest integral value >= x.
( c0 i2 y& y) z! |! x. Q4 P( Z - 8 D# `3 E. N% S4 I/ S3 z. z
- >>> math.ceil(4.01), G6 l4 J& P8 d' X6 I2 x9 `
- 5
# C8 T8 N/ ] H - >>> math.ceil(4.99)6 n# R: q" \+ \6 f, U9 P
- 5
% T& r6 D1 D6 I - >>> math.ceil(-3.99)
7 c: {0 } Q# Y - -3# ~7 e2 w6 ]% N ]
- >>> math.ceil(-3.01)' W: c- ~% L/ O+ i! ?. r
- -3
复制代码 # _4 t+ c8 Q, b' [$ z- H
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ U# x: c+ n, Q( {$ ?: b. v8 b- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% u$ }$ y# \- `# R5 Y# h
- floor(x)
, Z% i' H1 b/ z# B* F+ H - Return the floor of x as an int.0 E& N2 K& V. ]! \5 _
- This is the largest integral value <= x.: I$ z5 ~9 P1 n; p2 |! z
- >>> math.floor(4.1)
2 c6 V. X) L7 }7 m; l - 4
6 ~+ x! K& u' E4 l0 t' | - >>> math.floor(4.999)
; d( h2 ]% v8 q; Y6 W" `' e - 4: \/ j- H* u) ^( y# K; V; x
- >>> math.floor(-4.999)5 b: R! o" h2 Y0 R
- -5 [6 h( X4 e/ M* }6 Z6 H' x
- >>> math.floor(-4.01)5 b# d. ~8 B3 ]" q
- -5
复制代码 % P' V( X! e3 G: z) N8 r
math.pow(x,y) 返回x的y次方,即x**y) F3 I% f' A2 E" r8 Q
- #返回x的y次方,即x**y+ w0 U! H! @/ O; R+ i4 K/ L
- pow(x, y)
2 c5 C" {- n) q - Return x**y (x to the power of y).
7 u, m b8 K: i% m5 D1 H. ] - >>> math.pow(3,4)
/ L) J, c+ _& g: V$ J$ m - 81.06 V+ P+ e2 j, q
- >>> ( N& r5 U5 K, [5 x6 ^
- >>> math.pow(2,7): b4 ^$ J7 |& L
- 128.0
复制代码 6 A+ g, k, a9 l
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ \* T L' c1 ]& k& E- P! `- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); Q! c4 K! F8 }+ F( G
- log(x[, base])8 S. P) H7 G1 h1 ?6 e
- Return the logarithm of x to the given base.
4 `1 k4 O$ W4 ^( i" H. J! n M$ ? - If the base not specified, returns the natural logarithm (base e) of x.
1 a+ J& D. D4 ?: |! D; i - >>> math.log(10); A" ~: E4 E: \) l( ?/ g
- 2.302585092994046" _; `& b. ]5 d) i* s: P" k' z
- >>> math.log(11)' I) Y/ o! o* O0 l
- 2.3978952727983707. ]% ]/ C0 J* C8 s4 ?5 b
- >>> math.log(20)
% U, j/ i, i8 j/ y/ D# t J, l2 B6 j - 2.995732273553991
复制代码
8 }0 _- L$ u: k% C8 _2 mmath.sin(x) 求x(x为弧度)的正弦值
4 K6 `! T% o+ Q4 j- #求x(x为弧度)的正弦值$ [2 e' C6 P N( @1 V$ }# c- `
- sin(x)
, O6 w& u4 J/ M% t/ ^$ ` - Return the sine of x (measured in radians).
+ ^( |, j6 n- Y q! ^$ z - >>> math.sin(math.pi/4), x& F1 A# ^9 P4 s% C/ |
- 0.7071067811865475
9 |( z0 P0 D L: m H( I2 @ - >>> math.sin(math.pi/2). z$ o! h" U6 E7 O" H
- 1.0 @( j* w# W4 h! I2 S3 X+ p3 `
- >>> math.sin(math.pi/3)
$ G8 H# [9 x( z( m" H - 0.8660254037844386
复制代码
0 s( c* y; f# r$ Q- N, j8 a- Amath.cos(x) 求x的余弦,x必须是弧度$ o) r1 J p4 D. i2 j* }8 |# |5 F
- #求x的余弦,x必须是弧度
0 b% O8 h% Q# M: x - cos(x)) l0 o' s8 Z# I5 s4 s+ {
- Return the cosine of x (measured in radians).
, @, U( j# `9 C# {* M - #math.pi/4表示弧度,转换成角度为45度9 B U" J1 K% y$ [5 B" ]
- >>> math.cos(math.pi/4)
2 E% {2 O4 c. q5 e+ d' j6 K - 0.7071067811865476
1 \9 T; w! k+ I6 G - math.pi/3表示弧度,转换成角度为60度
% v' y/ F& x! k9 O' @ - >>> math.cos(math.pi/3)9 X1 L, n# Y ]0 s0 ~* o
- 0.5000000000000001' ^5 Y7 @* D2 c0 H; S' G! L" ^
- math.pi/6表示弧度,转换成角度为30度3 P0 C& W. t, |, D6 N
- >>> math.cos(math.pi/6)3 h. y6 Y. |4 h& \4 p
- 0.8660254037844387
复制代码
" W& ~1 V/ J% m4 Z% X6 H: [/ Wmath.tan(x) 返回x(x为弧度)的正切值
! c. d+ P$ w6 Z1 C5 R- #返回x(x为弧度)的正切值
f, m1 l' |0 T0 G! o - tan(x). V7 h* }" B3 z0 `1 _+ U! l% k$ d
- Return the tangent of x (measured in radians).
d) u7 S9 c7 x1 j# S$ \$ U - >>> math.tan(math.pi/4)1 ]# w1 z. i$ D" I" A2 s! {8 ~! i
- 0.9999999999999999; p e! L6 P# v, ^: `2 ?
- >>> math.tan(math.pi/6)& `/ S. P2 F* s& T3 o9 F
- 0.5773502691896257
7 W5 }2 }9 A; h0 H - >>> math.tan(math.pi/3)8 j h( `! @) A$ L% ~
- 1.7320508075688767
复制代码 8 X& Z' H" `( D$ g, m7 h" Y; m2 m; d
math.degrees(x) 把x从弧度转换成角度* M" U! u! D6 Z9 I% {) T6 J1 y8 S
- #把x从弧度转换成角度' ]6 \* }& T( l6 j3 w. _' \1 ~
- degrees(x)9 ? D+ \1 k* `. E) p3 J/ d
- Convert angle x from radians to degrees.. I( ^5 G1 d- t+ f) ]
- $ M c# v; ?' B" E) l: B
- >>> math.degrees(math.pi/4)
- Q$ d2 b1 i* U1 { - 45.0
: v2 p( C4 ]. H/ ]5 D - >>> math.degrees(math.pi)
5 \( F# M- F& Q* a - 180.0% q- U% t. \0 }/ D% d
- >>> math.degrees(math.pi/6)& b; {2 }& n' C! z' T( A; M0 T1 E( ^
- 29.999999999999996
* P! ^2 T& J' U - >>> math.degrees(math.pi/3)# F- B8 R' d' Q
- 59.99999999999999
复制代码 + V* i6 B0 F% q! g9 X
math.radians(x) 把角度x转换成弧度7 g; c. p1 }/ O/ B. P
- #把角度x转换成弧度' I7 R: _3 Y( v2 K
- radians(x)3 w& Y( _+ `% h( b7 R, @
- Convert angle x from degrees to radians.) u$ y1 X5 ^# L+ }
- >>> math.radians(45). y0 m# l, |8 J9 I; Z! p" j G7 N
- 0.78539816339744836 r+ x8 q1 T. g7 v, e1 B6 }
- >>> math.radians(60)
$ H+ _ R8 D: b) x9 I; i' ] - 1.0471975511965976
复制代码
# |3 p/ ~& K6 l3 Z9 Fmath.copysign(x,y) 把y的正负号加到x前面,可以使用0' e N- k, u) U) N4 C
- #把y的正负号加到x前面,可以使用0% W2 y8 D( p/ V: c
- copysign(x, y)
( ?# v: k' |2 d. A: ]( r0 F5 W - Return a float with the magnitude (absolute value) of x but the sign
) A8 ^5 F( v$ W! E8 e% S, A - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, T/ \2 V3 J5 w* x! ? - returns -1.0.3 n5 W$ C8 W: f8 E
8 G% b b3 d( K- p! G- >>> math.copysign(2,3)% w! _( v( k+ K( R
- 2.0. X; V- J& X7 y2 A
- >>> math.copysign(2,-3)
+ o' A( s6 X+ u. e6 Q5 T6 R1 e; t' q - -2.0% b B8 h6 ^! e( X# x7 g, _! B' n3 t
- >>> math.copysign(3,8)2 h `& E$ x" Z( x5 t0 W. L
- 3.0! T* J5 I% D$ z# t- \. s
- >>> math.copysign(3,-8)
; C5 f- Y- m: {" ^6 G. G7 E$ a- m - -3.0
复制代码 1 F2 ^4 ^* m0 h0 S2 B
math.exp(x) 返回math.e,也就是2.71828的x次方
. {6 u) F: x, o7 R- #返回math.e,也就是2.71828的x次方/ ^/ f/ u; \7 g( X& S# k/ j
- exp(x)6 A' h, J* T6 w7 s, F$ E" q
- Return e raised to the power of x.
( H h( L8 e, X - . N w N- g& i6 B6 z! g
- >>> math.exp(1); v5 a) e b' p x) ]5 W9 d
- 2.718281828459045: B* I6 u, g3 O* k3 `3 i$ g8 U
- >>> math.exp(2)
# I* K& A4 \! H/ ~0 T - 7.38905609893065& Q6 M) B- F/ ?! P6 v, } \% V
- >>> math.exp(3)
! d W9 H6 a# G' J6 Y" Q$ ?' M - 20.085536923187668
复制代码
: {9 @) u; q4 D! Mmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1* D8 L7 I8 ~' a+ _ G
- #返回math.e的x(其值为2.71828)次方的值减1 N1 u0 b \5 p3 e$ u6 w
- expm1(x)
2 P9 T3 l5 `! \! d: Z' k - Return exp(x)-1.. }1 T6 H. L) e7 v% I9 @3 p w
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x., H* {8 [4 a- P. a0 s( V
8 P! j O$ q, Q) h2 B, `" J- >>> math.expm1(1)
5 O$ z3 ~+ u- ^, g4 B4 V& F. t7 S - 1.718281828459045
0 m8 n4 ?+ s' K+ O' R - >>> math.expm1(2)
: _: b+ \# c8 g" t. X - 6.38905609893065
% p# r+ m# ~7 \4 D - >>> math.expm1(3)
% l* _7 D& H1 Y; @ - 19.085536923187668
复制代码 : ~2 z# a3 Q9 ~: P, |6 a3 x+ F
math.fabs(x) 返回x的绝对值
) }+ B0 ^' @; N- u0 c, b- #返回x的绝对值
+ _: D$ \) l1 h, z/ T - fabs(x)
, [% a0 c9 U+ _ - Return the absolute value of the float x.. e2 P* t0 J1 P- D) f3 m+ B
. I" N* p% O& ^ b, h9 A; S5 {- >>> math.fabs(-0.003)" J+ |' H, Q% d# X" O
- 0.003
K$ e8 Y; }4 G; h - >>> math.fabs(-110)8 B3 f% p* R* S" ?9 `1 M1 ?: z' _
- 110.08 {1 A2 b! I! L/ A |5 Y: m
- >>> math.fabs(100)
2 t& f6 t: L2 l1 ]$ l a. \& h1 r9 c - 100.0
复制代码 ) ^- O- K0 j# R% ]# b1 Y9 h; `: c" }
math.factorial(x) 取x的阶乘的值0 ^5 S, B/ T H- B2 \, G
- #取x的阶乘的值2 j1 e4 r& R) x) V" v/ s
- factorial(x) -> Integral" ?5 Y' v: i$ j7 W( C. F# L
- Find x!. Raise a ValueError if x is negative or non-integral.
0 s9 m- u e, K% ^# F, Y* q( `, | - >>> math.factorial(1)* [ h$ G8 F H& t* M+ Q7 U* D; \+ y
- 1
J/ Z9 _- \8 o/ X } - >>> math.factorial(2)
) t9 ~6 |& e& a- R3 L - 2
v$ a: R# d$ [# j6 f - >>> math.factorial(3)
3 ?$ v2 X6 L2 u+ c7 E4 r2 V/ h - 6" V* U: a) e- W/ o( T
- >>> math.factorial(5)
2 \( w/ M1 }" G# {/ n - 120( U. j6 {5 g, x
- >>> math.factorial(10)
2 t5 q8 r/ ^" Q1 S5 S( m f1 @ - 3628800
复制代码
8 w9 _; R) ?1 kmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
+ }! ?2 r2 p. h. g- #得到x/y的余数,其值是一个浮点数
! C3 u1 D2 a8 p$ Q2 l - fmod(x, y)9 W# o$ c: U2 i4 H2 j
- Return fmod(x, y), according to platform C. x % y may differ.
4 z1 U" S/ @9 j$ a9 J& \ - >>> math.fmod(20,3)
5 O/ h& t9 K* |- Y! t1 S - 2.00 P3 \0 \0 U& \8 R5 t
- >>> math.fmod(20,7)4 ]3 }% K6 e8 a8 R' O% i
- 6.0
复制代码 + |& x5 Z8 U0 f2 j
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" c+ ~' u3 H% d: J2 r& `
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
! e& q6 z i2 D k9 E* A - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
9 f' U1 w7 ?0 E# `/ v - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! K+ @3 F5 `* z: s0 j- c
- frexp(x), D$ A8 B$ S2 Q$ m& @1 y. M
- Return the mantissa and exponent of x, as pair (m, e).
: ~ v* [" N2 o/ \, A' N+ p& ~% ~ - m is a float and e is an int, such that x = m * 2.**e.9 W, L, M+ n' Z# ~
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.$ H% [# o3 f# E' u8 m* k
- >>> math.frexp(10)+ z! ]8 [* X4 A& z: y
- (0.625, 4)
' v, b( F$ t% w. X8 T - >>> math.frexp(75)
5 ?" h; q& S: l! n - (0.5859375, 7)- c# r2 J' Y8 k( H7 V
- >>> math.frexp(-40)0 M4 @' Q) n+ a4 }" H. F' W
- (-0.625, 6)
( {. S- f4 y9 p! Q8 S - >>> math.frexp(-100)5 a& h/ f0 w5 z" E8 R
- (-0.78125, 7)
; M R0 J/ e4 p - >>> math.frexp(100)
8 y1 c9 Z9 J" J' y/ L9 L2 P, Y+ K - (0.78125, 7)
复制代码 * s/ m$ z- A- p3 T: f' j' @, q- Z
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)% K" J' k l" g
- #对迭代器里的每个元素进行求和操作
6 ]3 ^* s2 B6 v$ v: h8 u7 A- e - fsum(iterable)
7 O/ I* N( L* _, X/ P) [8 Z0 o - Return an accurate floating point sum of values in the iterable.
% ?- m9 F6 Z, v, A7 ^ - Assumes IEEE-754 floating point arithmetic.
* \# g% L o3 V- {/ a7 A - >>> math.fsum([1,2,3,4])
( L9 m* j' W: J# d) ~ - 10.08 V; ~% `3 L- o& E. M
- >>> math.fsum((1,2,3,4))
; ^/ \" ? e5 a8 u% O - 10.0
. o \. E: f. T4 Z$ M( Q1 c# G - >>> math.fsum((-1,-2,-3,-4))
$ x2 G1 @; q @% f - -10.0
; n, \" P+ L+ H, @" t1 [ - >>> math.fsum([-1,-2,-3,-4])
3 j: E, N5 }- b - -10.0
复制代码 7 D5 ?- R5 O! \( p* g7 F
math.gcd(x,y) 返回x和y的最大公约数" Y, g; i/ @1 ~% B1 ~3 H
- #返回x和y的最大公约数
8 z, k; Z3 L" F+ h5 o; k0 d9 | - gcd(x, y) -> int+ x* w5 R# f) z
- greatest common divisor of x and y7 N8 g; J* {& V3 x4 H3 J& C
- >>> math.gcd(8,6)
( i! L9 \3 ^6 O - 2
0 N9 g+ ]4 w! ] |1 D& @8 m - >>> math.gcd(40,20)- t$ t1 P5 f) h% Q
- 20
% Y7 }+ `% V. e - >>> math.gcd(8,12)
4 V9 o, U- w, i% q; q" {; a - 4
复制代码
p" R& ~8 E; t" w% X: e4 h# M& ?math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
, p9 m, B, m. h5 M+ i/ N. @. j- #得到(x**2+y**2),平方的值! v" m! V+ F6 a: A. M0 w
- hypot(x, y)! t7 I0 s! j3 k& P
- Return the Euclidean distance, sqrt(x*x + y*y).
, U6 [! I+ N# x. ^ - >>> math.hypot(3,4)
- n8 r3 R% I: ] - 5.0. d; b# J4 W& Z/ s
- >>> math.hypot(6,8)+ l5 O& [1 g5 Y9 n
- 10.0
复制代码 7 A* ~6 ]! A+ q& u& B$ m
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False( f0 b2 U7 v) F: l* l
- #如果x是不是无穷大的数字,则返回True,否则返回False
( h% ~! o# ?5 x* r - isfinite(x) -> bool6 q& N/ v" a3 _/ X; N" l
- Return True if x is neither an infinity nor a NaN, and False otherwise.) c2 g% S. ?1 o4 ^7 P' Z
- >>> math.isfinite(100)6 V5 y% N2 t v+ b. r( V
- True- _3 m8 t1 ]" M* g7 d
- >>> math.isfinite(0)
6 O" r7 j1 E# R, N - True# `& z0 o& ]- u$ \
- >>> math.isfinite(0.1). ~: j1 \+ \$ z9 A1 @/ i
- True
. N: R" Y" g* x% q6 \& K& J! S - >>> math.isfinite("a")
, k- [7 n& p8 F, u" @% x2 F, q& U, l - >>> math.isfinite(0.0001)
: e6 _8 i7 G$ w- t1 E0 K4 [( H - True
复制代码 2 r o" g1 Z9 H; e$ n( r% e% o
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
, }& T) G$ B) p* r' r- #如果x是正无穷大或负无穷大,则返回True,否则返回False U$ A1 r* k* u& l
- isinf(x) -> bool7 ~* {1 B) r5 e ~ E
- Return True if x is a positive or negative infinity, and False otherwise.
, G \6 }; b% t7 K - >>> math.isinf(234)9 v" n- P( O% V6 {. L
- False! ? \4 m8 Q+ o/ U; p! m6 R6 |
- >>> math.isinf(0.1)7 f; X& _% ^; N% M3 a. I: e
- False
复制代码
- X" r5 C1 c8 X. C0 `4 G" S; d& N. _math.isnan(x) 如果x不是数字True,否则返回False
5 O. E3 A' Q) H' a- #如果x不是数字True,否则返回False) T6 `2 x$ q8 H( U, k0 b
- isnan(x) -> bool/ F9 ^6 G2 U% r }0 ]% K) j8 n
- Return True if x is a NaN (not a number), and False otherwise.
3 p9 c# z$ q) S6 v% Z( C$ Y - >>> math.isnan(23)
" P# l* c2 {* u! F$ r6 _ - False
% h, L5 o* w5 ]0 }: v* ^ - >>> math.isnan(0.01)+ [) x: q8 y C& y
- False
复制代码 ( t# M; j& h2 h) n
math.ldexp(x,i) 返回x*(2**i)的值7 m3 Y7 V5 T5 y0 ~; i- F' R: f X
- #返回x*(2**i)的值
2 \& n" G( }7 B+ p+ z- k: P2 O - ldexp(x, i)
% @" d/ A% Z- |" l8 ~ - Return x * (2**i).
/ I+ F+ i+ f: ^$ v& {' L - >>> math.ldexp(5,5)( C& o$ H* [2 z1 @- L
- 160.05 M' X* V: L! t3 M1 c
- >>> math.ldexp(3,5)
+ ?1 J* `4 }0 F8 U! g - 96.0
复制代码
. \* z, K% H% G J1 Cmath.log10(x) 返回x的以10为底的对数1 b% M9 E# I! A& w
- #返回x的以10为底的对数# X3 V8 J9 C- ~5 A5 W! H4 |
- log10(x)
8 I/ D ]" D7 M1 g6 y( Y9 Z" f - Return the base 10 logarithm of x.
+ m( @+ \ M" u& V - >>> math.log10(10)
/ T" _0 s" T3 d - 1.0
0 y8 S6 u4 ]& @) M, r" a - >>> math.log10(100)# k2 @$ g2 o) Q# k- ~, F' ^$ L4 P) f2 O
- 2.0
% N# f3 a! z J$ h* I5 } - #即10的1.3次方的结果为20" r# ] _ e8 k% k, S
- >>> math.log10(20)* P; {8 }7 y; G9 S; i
- 1.3010299956639813
复制代码
. S: A& Y/ N8 @# [! }) Jmath.log1p(x) 返回x+1的自然对数(基数为e)的值
* ?; V# `: ?( {6 _$ U9 P( y- #返回x+1的自然对数(基数为e)的值
( x; i6 _, N+ R - log1p(x)# G; r, J) _! J' f
- Return the natural logarithm of 1+x (base e).
1 o2 Q, U" o2 o k2 e( z - The result is computed in a way which is accurate for x near zero., M3 S& K% ]) ~
- >>> math.log(10)
% V/ X0 f6 R. s5 B$ w - 2.302585092994046
3 ]5 P2 x: ^3 Z. K) [& s4 E - >>> math.log1p(10)' y# y5 I+ L' D. `
- 2.3978952727983707) \) z) R* G9 W& R
- >>> math.log(11)
1 K. D1 a8 V E0 s" I# L - 2.3978952727983707
复制代码 9 p* G$ L5 f- t# n' k+ A. p0 w
math.log2(x) 返回x的基2对数
$ f# J2 a I4 z6 T8 W; p- #返回x的基2对数; s0 q- e, i' x; U& |
- log2(x)7 F; Q& ?! U- Q! m4 j
- Return the base 2 logarithm of x., M* I/ a& w+ E. p/ ?8 H$ p
- >>> math.log2(32)8 S" ~- f) z/ _" n8 m
- 5.0
+ A2 n( G8 \7 L% g4 T- x* n2 L - >>> math.log2(20)
. B, F' d$ i$ f5 [2 F$ Q |, Y - 4.321928094887363
; B5 U. d5 \9 {5 f( k' h U8 q - >>> math.log2(16)
& y0 F0 ]+ y) _6 v - 4.0
复制代码 1 ]8 k! M9 u/ E6 Z3 y8 i
math.modf(x) 返回由x的小数部分和整数部分组成的元组
' R7 X$ l2 ?6 I. N! Q- #返回由x的小数部分和整数部分组成的元组- ^2 Y) W+ Y" g5 V) ]% g4 e
- modf(x)
6 K/ C/ @. z7 ], B8 i5 D# g9 ? - Return the fractional and integer parts of x. Both results carry the sign. u1 _5 x9 j, B2 p3 |
- of x and are floats.
$ z% y, b7 V' _ - >>> math.modf(math.pi)
7 }$ d, ]' W0 s5 u - (0.14159265358979312, 3.0)7 t8 K/ W& l8 \/ L4 O( U
- >>> math.modf(12.34), z5 m) c' A) u+ Y8 e4 z( _+ K" |8 j1 Z
- (0.33999999999999986, 12.0)
复制代码
- W% X; H; O8 o$ vmath.sqrt(x) 求x的平方根# J& G# w/ m+ o7 b% k: @
- #求x的平方根: T A2 s3 {- ^- j" @& z
- sqrt(x)4 M% G) d0 u7 M! ?4 X9 b9 T
- Return the square root of x.' D4 |" j8 V2 B& {) _
- >>> math.sqrt(100)
" ^7 Y) _8 A2 y/ r - 10.0! O# a4 u8 B( F6 k
- >>> math.sqrt(16); a# V. I( G3 E) V; \2 q+ s$ W
- 4.0
/ C1 K- s" a% e- A8 { - >>> math.sqrt(20) y! B: G, q) J) k$ q- w+ o
- 4.47213595499958
复制代码 ' k- N2 u7 M" Y! Y! a+ w+ h
math.trunc(x) 返回x的整数部分- #返回x的整数部分
" k3 l& m# f; X$ h - trunc(x:Real) -> Integral
' t3 ]+ x: d, f9 P - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: f, |5 H) p; d7 \7 u) h3 H3 j
- >>> math.trunc(6.789)
9 V9 E- T8 a& i* r7 E: m, K. d& \ - 6: X3 I4 \- J; \" T. e! |0 U! {
- >>> math.trunc(math.pi)! f, }4 I2 Q+ B% l; Q( Q F
- 3
d* D, [3 I4 n0 A: N - >>> math.trunc(2.567)+ A% o" a2 |$ v4 N) G& C
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|