马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ b. T$ x9 J4 X6 E& p$ K' _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 _9 q8 a+ p" ?2 j( p
5 r- D W) P. k
方法1:( e4 T8 `: x, m( [0 [
- >>> import math
. @4 f4 a3 G( a2 J - >>> math.sqrt(9)+ E' |8 j1 P* M7 z- F3 h+ ~5 E. ?
- 3.0
复制代码 方法2:
+ G; `: L3 g6 ]0 T0 u- >>> from math import sqrt
0 W9 A. H" l: a - >>> sqrt(9)
8 w* t$ ]4 E5 _ - 3.0
复制代码 8 U7 L' b( ^. b0 Z8 t
6 q3 H, ~* e# N7 a6 ?math.e 表示一个常量
$ {' S# q& r2 Y5 L1 ^; \- #表示一个常量# O: \* \4 b0 U
- >>> math.e4 t& d4 D- i6 |8 u) D# {
- 2.718281828459045
复制代码 " Z. q1 y4 ~* ]3 U4 x
math.pi 数字常量,圆周率# i0 Q$ b. n/ N2 W
- #数字常量,圆周率
* ]4 T7 K5 i! ~ - >>> print(math.pi)( B! _6 K5 g( q' z3 _8 M3 \4 v
- 3.141592653589793
复制代码 ! {7 ?, l6 {( ~' Q: Y& ]& |5 S1 n
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
n! _+ G4 a, m7 a% e5 O# {/ D- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 y' A4 B7 D0 B1 a( e - ceil(x)
7 {: @* f5 _+ Q j% W; B9 V - Return the ceiling of x as an int.
a+ f+ F, x: \! D) Y6 z% A0 U - This is the smallest integral value >= x.
/ X. E0 \( V) R" P+ f- Q
3 c; [; u2 `7 ~4 v5 }) o- >>> math.ceil(4.01): w( }' }9 b$ j, Q/ v7 B' y
- 54 [. C5 ?# C% q+ ]( W
- >>> math.ceil(4.99)
4 ~* @ m$ P# N$ g - 5
8 J3 {! }; J3 D& `' t - >>> math.ceil(-3.99)
" `; e7 T; ]9 B - -3
, r4 r5 \" V$ ]( E" c/ |$ f) P - >>> math.ceil(-3.01)! E$ v4 n4 L8 p `% x+ m9 d; f, D1 s) a
- -3
复制代码 ! U& J/ q r( F3 b: ]3 P
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身& f( q* M2 g1 d1 D+ W
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 w7 ?9 F# J5 l* e5 x
- floor(x)1 M. U8 S1 _4 M4 T1 G
- Return the floor of x as an int.
3 t+ F+ ]# N- o8 x4 o; Q - This is the largest integral value <= x.
, G' t6 ^! g# b. i - >>> math.floor(4.1)
' ~; I+ l3 {9 j: t/ ~% ` - 4
, n1 ^) w* \/ S: Q3 m- a' m, p - >>> math.floor(4.999)
5 M" ?9 ?' D. G5 O, R, _ - 4
J1 v/ h) [, r& G6 w - >>> math.floor(-4.999)
1 H9 S# e; e1 j7 ~4 q% `* j$ m - -5
4 c: c' W, h% o, c; R( u - >>> math.floor(-4.01)# c5 I# g4 {7 {" ~
- -5
复制代码
B8 ?7 V( X8 ]8 p$ ^# m+ y. Dmath.pow(x,y) 返回x的y次方,即x**y/ S) x$ J9 E6 R4 A
- #返回x的y次方,即x**y$ }( f. x) g) _1 [* A
- pow(x, y)
. Y* R5 Y Z8 J0 z9 `3 o - Return x**y (x to the power of y).* Z! y6 h) R* }& D) u9 t$ T" J' \ g
- >>> math.pow(3,4). M3 W6 _8 v! c" {1 c$ j" j! A2 x
- 81.0
* R/ J. d* p; d( k - >>> / S9 G. }: v2 m/ S m
- >>> math.pow(2,7)
' j* G( K* U2 Z1 q& z2 O- m0 ` - 128.0
复制代码
9 O" A$ [6 A; ]; }4 ymath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" P- V# M* q J- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' U' p' i" g" R2 K T' _
- log(x[, base])
' p6 q- r9 H$ N - Return the logarithm of x to the given base.
) C; v: V. Y& b - If the base not specified, returns the natural logarithm (base e) of x.
4 I' I6 j X% s, U - >>> math.log(10)
% l3 o" g% A: Q - 2.302585092994046
$ N5 q( m& ~* R, a, k+ _6 R# t; B0 v - >>> math.log(11)
2 l) { _/ n9 L - 2.3978952727983707
4 u! n3 _) f" ~( n& p1 k0 P7 l - >>> math.log(20)( q1 H6 x2 X$ W* Y7 F( [; q* h7 z
- 2.995732273553991
复制代码
6 y; \# J7 l4 G; S! emath.sin(x) 求x(x为弧度)的正弦值4 Y+ v, h5 v1 Z
- #求x(x为弧度)的正弦值" A4 N+ K3 r& u
- sin(x)
+ x- Q: l# T9 v# @ - Return the sine of x (measured in radians).1 l* a/ `% G% H
- >>> math.sin(math.pi/4)# _, T4 t8 z/ ?) i! E! z, Y: R
- 0.7071067811865475
$ ^ @! l" ?) X! Q! m - >>> math.sin(math.pi/2)
: q: I$ {- B, V5 ~ h4 \9 U - 1.06 {0 ^- _8 O/ B" H) b1 i. Q
- >>> math.sin(math.pi/3)
1 K' ?5 h8 D1 Q# S) P. P3 R - 0.8660254037844386
复制代码
1 t- J" p5 t7 v3 I9 l' `% nmath.cos(x) 求x的余弦,x必须是弧度8 E: c0 o$ R w/ o0 v0 y8 @, v
- #求x的余弦,x必须是弧度$ K# O% |8 d6 I. u: h3 R7 N
- cos(x)( P, p/ ], f# v2 p9 \3 K9 G. U
- Return the cosine of x (measured in radians).
3 U! g* L' F' I3 f8 `+ [/ d - #math.pi/4表示弧度,转换成角度为45度# h l$ S7 J" E$ [ h0 t
- >>> math.cos(math.pi/4)! ^1 T# x! O& h) K P
- 0.7071067811865476
# ~ [ Q; L% [ - math.pi/3表示弧度,转换成角度为60度
/ F* ^. {, w( o0 H* X - >>> math.cos(math.pi/3), V" h3 Q7 }: m z6 ^
- 0.5000000000000001
6 `( e6 D* g7 d! I - math.pi/6表示弧度,转换成角度为30度
, B- n0 V7 z5 {8 }) o+ h& n - >>> math.cos(math.pi/6)
8 G0 _. U3 C5 s" f - 0.8660254037844387
复制代码 1 t' `1 R; G; Z/ c
math.tan(x) 返回x(x为弧度)的正切值
' `% i) Z, Q! u! m# l& ]9 d- #返回x(x为弧度)的正切值
+ f% n4 i7 u7 ? - tan(x)
) M3 a& }& L8 @0 g7 s" T9 |1 ? - Return the tangent of x (measured in radians).
5 m% r0 g5 L9 O, g: G Q7 S% j - >>> math.tan(math.pi/4)/ a8 o% O5 s8 r
- 0.99999999999999997 W& @! {/ l1 ^8 o1 S! j6 \) ~4 s( _
- >>> math.tan(math.pi/6)
( ?2 b$ R; S5 D! M1 e3 j) d3 g - 0.5773502691896257
7 E& b3 U/ y1 [: N/ I( K - >>> math.tan(math.pi/3)+ t* y4 u! H! N9 j1 j. {& ]* g. d
- 1.7320508075688767
复制代码
5 }- y2 m$ Q8 Z6 ^+ [! T9 omath.degrees(x) 把x从弧度转换成角度. V9 z! {( U7 w# ~, S
- #把x从弧度转换成角度( R7 ^7 u2 x7 r F0 W
- degrees(x)- ]2 X. b# O1 j E' A5 p* H) q
- Convert angle x from radians to degrees.% D4 L. h% p( P' U S
- $ F/ Q4 e' K5 d' j
- >>> math.degrees(math.pi/4)
% F( P' t7 W! b. z+ `# d* L& W% H - 45.0
' S: w0 k3 [" M3 z - >>> math.degrees(math.pi)
2 C4 a" ?& [; B9 I1 Q* Y - 180.0
% B; U- v* d9 A1 P5 P6 k% C - >>> math.degrees(math.pi/6)) W% o$ u- B% {. @# w) L
- 29.999999999999996
5 M% \/ [* {- s+ t: C/ N# L: T; W- b, \ - >>> math.degrees(math.pi/3)$ }8 y+ G6 Z) p
- 59.99999999999999
复制代码 ) g4 R) P; l! p8 m' c _
math.radians(x) 把角度x转换成弧度
7 q9 v2 l+ M) R' y- #把角度x转换成弧度
6 |4 A% a% O7 a$ \" t - radians(x)# G, ~% r' T# u. u }
- Convert angle x from degrees to radians.
( u5 L* ~' }/ n' y* l* b9 |( w - >>> math.radians(45): T8 P6 c5 [! U! {9 q
- 0.7853981633974483
5 N/ D7 c# H$ o) X6 [ - >>> math.radians(60)* O3 l: {, r- _
- 1.0471975511965976
复制代码
4 ~# v. v- q' w; `& Z6 i0 Z7 imath.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 d* {- w) }7 V* Z+ h- #把y的正负号加到x前面,可以使用0- c! _, W4 p& _* S9 f
- copysign(x, y)' U( ]2 ]) A/ }4 Y/ c
- Return a float with the magnitude (absolute value) of x but the sign
0 N) O8 q/ f, X+ S' b - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
9 s# V% Y3 ^2 J% K. p! [! g' V - returns -1.0.
6 R6 S. Q3 M/ L& c- v! M - * B! b; |. S" s6 Z
- >>> math.copysign(2,3): [& [+ I) P3 i- H. ^
- 2.0% ~% o! j* J* V7 k8 y& s
- >>> math.copysign(2,-3)
/ ? E+ K3 [/ x9 l8 O - -2.0
( l- `5 \6 k, y& H' d3 p+ Y - >>> math.copysign(3,8) l/ ~! I. F- g! F" j7 h
- 3.0
# S+ s$ q- F8 D7 ^0 |) u- A - >>> math.copysign(3,-8); _. C# y" B* s- ~" c
- -3.0
复制代码
' G7 Q7 S5 I+ {& I0 n; Omath.exp(x) 返回math.e,也就是2.71828的x次方$ N) k4 p5 s( }* ] x7 a
- #返回math.e,也就是2.71828的x次方; x: ?+ [: _1 Q2 c, q, f
- exp(x)
! o' b3 P7 F/ t; c3 d - Return e raised to the power of x.
7 h8 y6 [5 W L - 3 Z! K `- v0 [( I5 g' {3 H9 ~6 k
- >>> math.exp(1)# w) V! X5 h( y A
- 2.718281828459045- K- u" Z" h ]7 s) A |, B4 G
- >>> math.exp(2)% I; {" p' s: ~% N4 ?
- 7.38905609893065; O& s( t ^% e1 u" N
- >>> math.exp(3)
* \- g, S1 d9 u: ~ - 20.085536923187668
复制代码 1 X: a, n! g* ?5 q! j) s
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
4 J7 o5 t$ p& ^1 X8 Y) i1 m# b- #返回math.e的x(其值为2.71828)次方的值减1: F) f% G$ q) J1 t& L
- expm1(x)
& N. Y9 N& L5 d" x4 k - Return exp(x)-1.
% k& Z4 M, S: e) m+ G* r - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
8 v, P0 A' h# P* H
$ z# @+ O4 |2 b% s- >>> math.expm1(1)
; ~, U( p* R/ W - 1.7182818284590458 y( H, `3 J- r4 |
- >>> math.expm1(2)9 I0 r1 B5 q3 i2 K p% F6 b" _
- 6.38905609893065& \' Q9 O7 U/ K0 P8 h# Z5 Q
- >>> math.expm1(3)4 B- j [ L N$ m7 o: s i5 E
- 19.085536923187668
复制代码 0 s3 S5 ~, W# ^- N
math.fabs(x) 返回x的绝对值
+ a5 g# f( Z2 @; W- #返回x的绝对值
# ~- D% c& q$ f+ |% V3 ^ - fabs(x)* X. }3 G0 J& G S9 O: a& U
- Return the absolute value of the float x.
8 o% z9 y5 I6 L2 B, i# K: m" N: q
8 A/ S, x4 y; D$ ?* O- >>> math.fabs(-0.003)) @2 ~' b+ ]! b* h* h- ?# L
- 0.003
; |4 I3 o7 k) ?# B - >>> math.fabs(-110)
1 l6 f1 h' X! H8 J* A6 P# {' c" Q - 110.09 N* {7 n8 f. k
- >>> math.fabs(100) ?5 U. W- L/ Y1 Y
- 100.0
复制代码
/ D. ]1 _3 [1 }& E q% mmath.factorial(x) 取x的阶乘的值
1 R6 ^$ C4 f5 j3 @* [! e& {2 @9 L- #取x的阶乘的值. H. h& ^" j" x4 x4 g0 X9 i
- factorial(x) -> Integral) O' c# i4 P& w( X1 [( d" ^
- Find x!. Raise a ValueError if x is negative or non-integral.
3 K! I; i; r- N) N: B- f9 H: L8 ~1 _% \ - >>> math.factorial(1)0 u7 `( Q R' V. k% r+ R9 ^. M
- 1
8 ?& N5 u! |/ _. x5 j! w - >>> math.factorial(2), H$ b4 z0 u7 z: [6 [( _
- 2/ e0 S- s4 j. J
- >>> math.factorial(3)9 E' X/ R# @9 y3 A/ Q' K
- 6* C. [7 o z# t, {" p8 h* x
- >>> math.factorial(5)
7 L, I. t$ c5 U9 z - 120
* x- S/ I) k" j - >>> math.factorial(10)
- `1 s- P! E: y: r n) R9 X - 3628800
复制代码 0 q3 Z4 ^3 p3 c
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数! o/ r+ I8 n3 Y; }' E
- #得到x/y的余数,其值是一个浮点数
: b X1 Y6 g1 h' D& c0 i. t# }8 W - fmod(x, y)" {# s. t4 n, ~+ V
- Return fmod(x, y), according to platform C. x % y may differ.8 @, q% o7 @3 S7 J
- >>> math.fmod(20,3)
% Y8 ]0 c3 C( k: w% t2 ?. l, b& _7 L - 2.0 d9 W% R3 `: O2 d
- >>> math.fmod(20,7)( L9 J) l% g) D4 r* Z: o7 Z$ q
- 6.0
复制代码 / Z2 Y) ^ v+ `
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
7 Z5 r7 C. h m% b2 Q6 Z3 J# w- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,# N& h/ _0 z s. o0 _1 P% K' K
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
5 z" V( I1 x0 ~( G" t" Q - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
" o8 V( n0 |3 F% c/ T, B - frexp(x)
$ H4 v1 b' z) J - Return the mantissa and exponent of x, as pair (m, e).: c3 r" Y4 n2 a) g/ u* |& `
- m is a float and e is an int, such that x = m * 2.**e.
& e' }1 o6 S/ h4 [ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.; P) P( P3 n/ \+ }
- >>> math.frexp(10): @, Y' w/ Y$ m. L; [/ c! D% u
- (0.625, 4)
$ d. ~& y$ {9 g6 v5 H/ ?- t - >>> math.frexp(75)
2 `- Q+ H- s6 e0 E3 P - (0.5859375, 7)8 S- K& k- w: i3 |2 b; P
- >>> math.frexp(-40)
6 W5 o+ K: [# e* d1 ]( i$ v% q# L - (-0.625, 6)
9 ~- R0 `. ~4 s0 f - >>> math.frexp(-100)& h- c! |5 l% A4 S
- (-0.78125, 7); G: l2 _/ W! O
- >>> math.frexp(100)
. ~% q1 `; n3 k - (0.78125, 7)
复制代码 * H+ k1 L) M4 l
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)' E8 g6 {0 _& D; r- F/ w/ g" t7 q
- #对迭代器里的每个元素进行求和操作
& g$ {: U! g" y$ T - fsum(iterable)
4 [6 ^/ x7 C' t2 I - Return an accurate floating point sum of values in the iterable.
1 l. V# Y8 \- } - Assumes IEEE-754 floating point arithmetic., {! J% x/ [- {2 H7 |
- >>> math.fsum([1,2,3,4])% j: V1 t9 \ |
- 10.0
n2 W# f5 q5 O- J# W" g8 N' | - >>> math.fsum((1,2,3,4))
: n4 p/ M" v* S! m6 p- D - 10.04 @9 a: q& l H* g
- >>> math.fsum((-1,-2,-3,-4)), {# C/ b; D6 F" b
- -10.0
6 ~4 X2 {, `; U1 i' ? - >>> math.fsum([-1,-2,-3,-4])# U. a9 I5 P- ^5 u
- -10.0
复制代码 , l4 o- j; e2 G; E( P) G
math.gcd(x,y) 返回x和y的最大公约数0 f7 A f7 s6 I% J4 Z- _
- #返回x和y的最大公约数/ e5 ^( O8 H8 P8 U+ r+ n, f; f' ]
- gcd(x, y) -> int
& ^7 w* w& M) t j - greatest common divisor of x and y
8 }4 J; N& {( h! u, M# k/ u. ? - >>> math.gcd(8,6)
7 `/ s% i2 y" _) j* L, \ - 21 c5 ]' E. o) M6 t" d
- >>> math.gcd(40,20)
2 ~9 C7 u2 ]! }: I3 U1 x: n2 A7 x - 20
3 d# z6 K. Q7 J+ \ - >>> math.gcd(8,12)
2 C9 H' Q5 b. C0 I r D7 r - 4
复制代码 2 j; |2 c. G# Z
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False- ]" e# g; D }% ?" R
- #得到(x**2+y**2),平方的值. b }' m! ^! R2 q3 p% q
- hypot(x, y)
3 U9 J# r: @/ P! x/ a3 o: U& w - Return the Euclidean distance, sqrt(x*x + y*y).
4 D( I5 r& Q! V - >>> math.hypot(3,4)) b* x4 ?/ k, z& }8 A
- 5.0, g& d5 _4 q2 t; h s
- >>> math.hypot(6,8)
4 P. N& o2 c$ d: `; q: ] - 10.0
复制代码
, z2 ]8 Q6 A& v0 S+ Xmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
* h, [7 e2 l j0 [- #如果x是不是无穷大的数字,则返回True,否则返回False2 s3 q. ~) ]) P7 Z8 C
- isfinite(x) -> bool6 Y& }3 n! x, b
- Return True if x is neither an infinity nor a NaN, and False otherwise.
3 r( {( \, v$ T$ g4 M/ j7 c9 v - >>> math.isfinite(100)) Z2 e# _' X! Q( Q/ B
- True. `% r! T& |" y( w
- >>> math.isfinite(0). C- u6 g m: r
- True8 c+ l: d/ Q; t6 J1 p) g6 p
- >>> math.isfinite(0.1)
W u- P1 b$ F# J$ i - True
6 W" L6 n- q- o0 s+ E/ l - >>> math.isfinite("a")
6 @* N4 S- q2 J) \9 p2 A - >>> math.isfinite(0.0001)# c' Y( P# ~( z6 g" m9 r9 Q$ d
- True
复制代码
6 f: Q6 f0 D+ f& g z; l% omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 A% U' v9 f/ ~5 C& {2 W* L- #如果x是正无穷大或负无穷大,则返回True,否则返回False
" J, j. D9 Q9 i6 A ^! h" N - isinf(x) -> bool) _! V4 @7 M8 i: Y4 M% _
- Return True if x is a positive or negative infinity, and False otherwise.- x }. [4 ?, l" r" n3 q* Y
- >>> math.isinf(234)" X$ {% v: D }9 t# ` r
- False" S3 y5 z& Q2 |4 _3 w& {
- >>> math.isinf(0.1)# k } f R( G4 R6 y
- False
复制代码 2 n, [4 n) O* w& f
math.isnan(x) 如果x不是数字True,否则返回False
/ [" N; m4 E1 P% N2 j- #如果x不是数字True,否则返回False
$ m5 w* O7 }* r, r, g) ?0 e - isnan(x) -> bool/ `: X" A: z1 I$ i$ Z6 \* }1 J% o
- Return True if x is a NaN (not a number), and False otherwise.! H; G# i. o5 c& U0 z& _
- >>> math.isnan(23): \1 A# `0 ^" F
- False
' ^" `. x/ Q6 d; o - >>> math.isnan(0.01): N, w3 `3 e7 P% J5 c
- False
复制代码
9 W5 \1 X6 L/ V4 y9 U' gmath.ldexp(x,i) 返回x*(2**i)的值
+ [1 T9 ?6 }0 q2 K: D- #返回x*(2**i)的值9 R) S9 ~3 [, z
- ldexp(x, i): D* Q1 s" t. ?6 z) w
- Return x * (2**i).
; E- S) J2 k/ R) z - >>> math.ldexp(5,5)
9 R0 ^. g) V+ c9 ^: s9 L - 160.0
) }7 ^$ ~ \% P/ d - >>> math.ldexp(3,5)
8 k( S% x2 K6 d7 m; k - 96.0
复制代码
" P( J" j9 E* }" z9 t4 N- Xmath.log10(x) 返回x的以10为底的对数9 h, R& l: Q8 M% T( g K% j
- #返回x的以10为底的对数
% i- v* B" f0 Z: i% v+ x$ E3 v - log10(x)' E K, ?* }% I1 h" }5 e& y
- Return the base 10 logarithm of x.
2 p+ U: ^' H2 D5 e7 g5 O- w- o1 ` - >>> math.log10(10)
$ C: o' w/ t( \; M - 1.0
" s5 ]+ @) t5 c) w$ m - >>> math.log10(100); r5 J8 o1 P' i, u
- 2.0
5 g- k8 V5 {9 ~% |3 B9 Q6 U% l p0 c - #即10的1.3次方的结果为207 O- I4 h* K1 A4 @: p' w
- >>> math.log10(20): C4 ]/ V+ m; w1 Q
- 1.3010299956639813
复制代码 1 S% P4 Z S8 M; m8 T! F: ?% S
math.log1p(x) 返回x+1的自然对数(基数为e)的值
& O, E' @& }7 e- l9 Q: ^- #返回x+1的自然对数(基数为e)的值 u" P" ^9 p! h/ D4 P
- log1p(x)2 C% v+ i: [! S1 [8 J1 Z c
- Return the natural logarithm of 1+x (base e).
0 M, h" W/ ~) n4 h0 W- M( I6 ] - The result is computed in a way which is accurate for x near zero.
8 _( Z9 p4 i1 A/ k - >>> math.log(10)
2 i. l7 Y; a7 `2 w, ] - 2.3025850929940460 H0 w2 r( N0 a
- >>> math.log1p(10)
% r+ m) V9 s: G/ n& p9 ~2 j - 2.3978952727983707
+ q% N% P# A" }1 P' C' w& a - >>> math.log(11)
: }, l8 n( v; p1 D& Z9 Y# P+ { - 2.3978952727983707
复制代码
, _: n) l6 ]1 @" C& S) Omath.log2(x) 返回x的基2对数" w$ \7 u. A8 k$ T
- #返回x的基2对数& {& B8 ?6 x/ b' V5 e5 D
- log2(x)
' R, F8 z7 U/ l1 ^; c/ ? - Return the base 2 logarithm of x.0 x& i/ Q/ b& _ ?8 K) l
- >>> math.log2(32)
6 a% k( i0 ]1 _! G* t - 5.0
( R8 u) C2 t7 K - >>> math.log2(20)
1 ?5 q2 Q8 E! R% R% E8 r - 4.321928094887363
; `& W8 [' l8 |! X" I$ J - >>> math.log2(16)
6 N/ w+ M; k. u - 4.0
复制代码
: l" ]/ W3 V# y2 H1 h8 Pmath.modf(x) 返回由x的小数部分和整数部分组成的元组
b2 x2 F& b+ }9 ~( r, e' i4 J. }: ^- #返回由x的小数部分和整数部分组成的元组) A$ v, I6 j/ W% S9 h% z
- modf(x)
% i3 V( t2 T' K8 [# A - Return the fractional and integer parts of x. Both results carry the sign& x, y! U8 A; @- X
- of x and are floats.# U6 `- t1 ]7 Q( Q" E2 u$ f& _, m! V) y
- >>> math.modf(math.pi)
) d; A; q8 n1 e0 q2 P4 U5 z - (0.14159265358979312, 3.0)' ^$ v1 g% X; u( {
- >>> math.modf(12.34)
# U* ]% u! b u' H5 L& ?4 k+ L$ O% C! F& Y3 k - (0.33999999999999986, 12.0)
复制代码 : x$ z3 t! V! L; `
math.sqrt(x) 求x的平方根9 r% W- c: e$ h5 Y: H
- #求x的平方根
; j$ j0 U/ `3 B7 A4 K+ Q - sqrt(x)
2 N7 C$ _5 _& ^+ |+ K- | - Return the square root of x.$ v3 z$ q( B, R0 b
- >>> math.sqrt(100)
! P5 ^* p8 |# G+ U# r: ^. Q - 10.0
( X4 Y% `! C+ a$ S$ h% J - >>> math.sqrt(16)$ x9 j. x9 _: h' n5 @6 B( n
- 4.0
( x2 [, E* a' o' W - >>> math.sqrt(20)( r, w' O2 T& E7 {! x- ?! B0 _3 Y6 o
- 4.47213595499958
复制代码
' r* C( M- h. ?math.trunc(x) 返回x的整数部分- #返回x的整数部分
2 ?0 E/ F. H: j; g2 R( I) S - trunc(x:Real) -> Integral9 \/ m( m% A4 ?: S( K- x% J
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
, G, W, V, b4 F$ W3 H9 k. x b, W - >>> math.trunc(6.789)* s4 D9 ^0 ^0 ^) [ x
- 60 i; }6 m9 E$ X5 E% [/ X' X
- >>> math.trunc(math.pi); O. \1 [1 ?/ }% X! b2 e
- 3
' u! f( m2 C. l/ j2 l0 r - >>> math.trunc(2.567)
9 `$ y- ^) t, k3 e$ o - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|