马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
W4 j# E6 Y; @0 }' |$ m【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
# e* i* c$ w" K6 t; {1 f
8 i) z8 q/ @8 d0 u. h8 G方法1:
+ E: S" I2 ^# ~( c' H* L6 [- >>> import math; x; u/ q9 |5 A! T
- >>> math.sqrt(9)
) P; d- s r* [* W" B8 p! ~ - 3.0
复制代码 方法2:
4 j ?+ Y% c* i- >>> from math import sqrt$ O: U% P! P" \4 ~6 g& E5 B
- >>> sqrt(9)2 u1 a( v: v; F3 u3 p0 T" z
- 3.0
复制代码
( i8 ?5 E5 {& P
. _; w8 ] g9 P- Q& z# M) Y) H9 A; kmath.e 表示一个常量6 K( p0 C: ] F7 s+ K( o: r
- #表示一个常量
3 f+ J. `# O' F - >>> math.e. G. c! j @/ f, T1 |
- 2.718281828459045
复制代码 ) m+ V4 d2 h/ W! j( J( J
math.pi 数字常量,圆周率5 e1 h4 M9 x2 I" L' s- T
- #数字常量,圆周率! e8 T R. N4 P/ @, b4 P, F" r
- >>> print(math.pi)
' ^5 ~! G- W ]! {* p4 H" Q - 3.141592653589793
复制代码
- c+ w. N" g ?0 _4 S& umath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 u1 n* v a2 i- l$ B1 y* g# s- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 x) F T8 I1 n) L) U - ceil(x)4 k9 g' x3 c2 O9 z6 [7 x; n$ v
- Return the ceiling of x as an int., y3 ^% Q; W8 v+ x' r- j
- This is the smallest integral value >= x.4 U: u, x/ T @: X/ ?! z
: M/ Y( Z! u/ h" R- >>> math.ceil(4.01)% T" ]: T: a/ d- r" s5 o
- 5
; v0 H$ S. D6 C5 F7 @* K - >>> math.ceil(4.99)
& \5 b" l& j$ B - 5
. t, C) N9 r* ?) v' b6 D - >>> math.ceil(-3.99)$ u$ r+ r1 |2 g
- -39 I) h! k0 z* h4 J
- >>> math.ceil(-3.01)( @" I+ I: e x, R" g9 h
- -3
复制代码 ! F7 u/ J b9 k" J1 M: A, F
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! W) `8 w5 g7 f* K- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; D q' a9 z; e) w; b9 U - floor(x)+ b0 _3 G' D: F, c9 |6 R
- Return the floor of x as an int.# [7 L% ~+ v$ F( S/ S
- This is the largest integral value <= x.% E4 D. o4 o ^
- >>> math.floor(4.1)6 V# b5 H% s/ {7 {) B$ I: t
- 47 i/ T6 r/ }# V5 i& W/ }2 p8 L7 @$ }
- >>> math.floor(4.999)
3 H# d/ @. j9 I - 4
4 g) A9 r# M$ O& Q. b - >>> math.floor(-4.999)
3 N4 d7 N# s' ? - -5, n2 _9 b% E* k, `5 h6 R
- >>> math.floor(-4.01)+ _+ F$ O" v. p) J+ M5 n+ k
- -5
复制代码
3 U& ^ }- k) x9 Q d% i1 _4 R8 ]: nmath.pow(x,y) 返回x的y次方,即x**y
5 P) H9 \& e- C6 z( G% o/ G6 a- #返回x的y次方,即x**y3 a/ F$ h# j N3 I- {8 X
- pow(x, y)# K5 ~" i$ n+ L1 K# M
- Return x**y (x to the power of y).
6 A" @+ l( i4 o7 h - >>> math.pow(3,4)
. d% j+ `! J/ V3 m, J/ E- k J - 81.01 K6 V6 a* _* n, B9 C. [2 G% _
- >>> - C3 o( x4 [3 T6 }
- >>> math.pow(2,7)
& ]; f/ s3 ]. ~; x. y2 P - 128.0
复制代码 . g; k4 k' {$ o5 G `# I
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' _4 s5 }5 [, ^
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 h; L# D8 p9 r6 \
- log(x[, base])6 B, Y6 A5 w( U
- Return the logarithm of x to the given base.$ r3 C& |+ S% b F( U) v
- If the base not specified, returns the natural logarithm (base e) of x." g; x4 |4 B0 Q( Y* ^
- >>> math.log(10)
! V* _1 m" J5 B/ s7 v - 2.3025850929940461 |2 r2 x4 y; b, q* W$ k g
- >>> math.log(11)+ D/ F, @7 K: S0 S9 Q* J6 @, x! }
- 2.3978952727983707% ]8 H2 b' h. i2 \/ O
- >>> math.log(20)! ]+ f& ]0 v4 L e
- 2.995732273553991
复制代码
; z5 E0 V, U9 ?! ~math.sin(x) 求x(x为弧度)的正弦值$ s+ F: @9 V3 U+ o6 t i( o* X
- #求x(x为弧度)的正弦值$ Q: r2 E8 Y+ B* W# O5 \7 `+ Q
- sin(x)- E, G6 ~$ x: K( L C
- Return the sine of x (measured in radians).1 o) w" P; n. P6 T
- >>> math.sin(math.pi/4)
$ ]. H* C5 I" g; P6 J [ - 0.7071067811865475 B8 d; r$ Z8 a; ?5 d% j
- >>> math.sin(math.pi/2)# r6 `$ x, l9 _: V9 n* S
- 1.0& i8 L' `6 K& `5 K: s
- >>> math.sin(math.pi/3)
+ U+ @; ^6 s+ U) c; J) r - 0.8660254037844386
复制代码
6 s) S1 ^1 M3 O2 m) T" U3 [math.cos(x) 求x的余弦,x必须是弧度
3 ~2 ^# x: B# w6 X- #求x的余弦,x必须是弧度( Y* G& E/ ^- q3 B
- cos(x)* l, W) D' u' f0 P2 J$ T8 f* b
- Return the cosine of x (measured in radians).
* ?4 {/ z6 `2 a - #math.pi/4表示弧度,转换成角度为45度4 }7 R, N$ N* x# d" N3 D
- >>> math.cos(math.pi/4)
5 ]" g C8 T/ |2 _" K- H; m - 0.7071067811865476
+ B+ i3 A# Y; Q' Z - math.pi/3表示弧度,转换成角度为60度" U6 W: b, @: v
- >>> math.cos(math.pi/3): i7 j; ~. P6 T9 g
- 0.50000000000000012 l: l: i8 T3 V$ {# o0 W' Z# j3 a
- math.pi/6表示弧度,转换成角度为30度
# f5 z& ?# E$ K - >>> math.cos(math.pi/6)
' S$ |5 h! L" X* E7 Z - 0.8660254037844387
复制代码 6 n z5 u9 y- _7 u6 b
math.tan(x) 返回x(x为弧度)的正切值
( t" E- J" g% o0 n% M! y4 L( M$ D" ?- #返回x(x为弧度)的正切值
. y, M% E9 o; o: Q) w) g$ r+ X - tan(x)( {3 C' }- Q8 K6 [1 t
- Return the tangent of x (measured in radians).) M9 K2 q3 W! n1 o
- >>> math.tan(math.pi/4). k0 C& y6 N Z! ^3 ?9 Q3 p
- 0.9999999999999999
3 ?# w# B. v: N* M1 m( H - >>> math.tan(math.pi/6)
2 \0 {8 ]0 A. d3 {4 q - 0.5773502691896257
. k( o$ v. H0 D2 H- l1 j - >>> math.tan(math.pi/3)
- f" q! T0 ]2 T - 1.7320508075688767
复制代码
& Y2 h3 H% ^9 P3 Imath.degrees(x) 把x从弧度转换成角度
; d q, w, Y0 ~3 P) ^- #把x从弧度转换成角度4 g. q5 t6 _1 F( `" d
- degrees(x)" C) Q7 |3 f3 G: G' V1 A
- Convert angle x from radians to degrees.
$ l2 I& \1 l! W5 M* j
6 Y# T2 L. G0 K' s- >>> math.degrees(math.pi/4)
1 y4 ?9 ^1 A. s5 z2 V! T- ] - 45.0* ~7 D9 f4 U" @; Q
- >>> math.degrees(math.pi)
6 h, J9 ^# \! O) Z5 W - 180.0
) i% e# g1 c3 Q# p - >>> math.degrees(math.pi/6)
5 x) y+ R0 A9 n3 x9 X - 29.999999999999996( g$ R" |6 w# r S0 T a
- >>> math.degrees(math.pi/3): @& v( k# b8 C# c$ y7 v
- 59.99999999999999
复制代码
( m: ]* M Y' z/ l0 omath.radians(x) 把角度x转换成弧度
$ z& m7 Q3 k* p# B6 W) p# c- d- #把角度x转换成弧度
* m+ R% _* s3 B5 G+ m& K A - radians(x)" h7 a1 v' V( _
- Convert angle x from degrees to radians.
. L7 L- U8 }8 l2 D - >>> math.radians(45)
0 B. k9 J4 t+ f - 0.7853981633974483 N9 M( C0 b4 ~
- >>> math.radians(60)
! A: H) P, \$ P: h4 I* ? - 1.0471975511965976
复制代码
3 F/ i8 @( ~0 Qmath.copysign(x,y) 把y的正负号加到x前面,可以使用0" Z. P4 z1 _ G+ F0 {* A8 k
- #把y的正负号加到x前面,可以使用04 }$ ], I `* o1 e
- copysign(x, y). c) u4 ?, M9 y0 [( b; }( ^
- Return a float with the magnitude (absolute value) of x but the sign
) s* I8 a4 g7 c& w. a9 D - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
$ u% z: J- b% }$ a* D - returns -1.0.
" Y( C- }, c+ e' e% U9 `1 G7 t* X! D/ z' [
$ y2 l* e5 v% w% A; f/ M6 @, d9 G' @- >>> math.copysign(2,3), H9 g! Z: F3 a/ e9 O0 k
- 2.0
8 I, o0 T- u, L7 ^- [! w; G0 \ - >>> math.copysign(2,-3)
r( `- H2 e' ]& h. L - -2.0
$ X/ y2 O+ U' U" J, h - >>> math.copysign(3,8)
+ K1 _8 W$ f2 k8 m& W - 3.0
/ ^4 c% [ e2 E9 ], B( I - >>> math.copysign(3,-8)/ d1 f3 P2 ?3 m. e" k
- -3.0
复制代码
0 s" j/ Z7 [, H7 S7 c3 y7 Nmath.exp(x) 返回math.e,也就是2.71828的x次方8 Y+ n$ V9 y5 G3 N) I9 q
- #返回math.e,也就是2.71828的x次方
7 ^8 W8 k1 k8 \' H% L' h* Z9 S - exp(x)& X7 Y: w, j+ T
- Return e raised to the power of x.# U( D: ^- t' `9 x" w/ |6 D( n
. k) Y2 A0 b: s, T6 W' d3 s- >>> math.exp(1)
. C/ Q5 ]1 E7 \' M$ f: I - 2.718281828459045
: E; h3 i, j6 ]/ k - >>> math.exp(2)- D) h) H$ c E9 u. E
- 7.38905609893065
! K. K% q. D- `, T0 `8 s1 P1 { - >>> math.exp(3)
) X5 |* w+ X. p% j* E - 20.085536923187668
复制代码 , Q) F# D. L/ h9 }/ c
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
* U/ N* H; [$ c) [% L' u" K/ J- #返回math.e的x(其值为2.71828)次方的值减1! I5 P7 W2 V2 @) }7 M4 g3 F
- expm1(x)
/ o: i' q& d0 r0 V- n( f, x! o - Return exp(x)-1.
9 L* L5 P. o( Z) R* T7 _ A - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
5 n$ J" J" t P0 v. x, u - ( H& b1 F" y8 } G- x3 X
- >>> math.expm1(1)1 y2 H/ C% t/ T% u6 p; P; K
- 1.718281828459045 J3 L. ]3 k; ]
- >>> math.expm1(2)
1 p. d4 N. V4 B) d, x - 6.389056098930659 N! E2 T. F6 w0 U0 H! `
- >>> math.expm1(3). x) d$ D A& c4 W
- 19.085536923187668
复制代码
7 o( V9 J7 b2 e8 n, ^+ B) fmath.fabs(x) 返回x的绝对值
+ ~/ ^' p; s0 [0 l3 K1 t5 ]6 b3 P- #返回x的绝对值+ [# [6 U% P$ B% u% I0 ~
- fabs(x)
E) { z; h* X3 Y+ t - Return the absolute value of the float x.
/ s! Y4 U0 C- l
2 f! O0 P2 k+ b- ?6 e- k( `- >>> math.fabs(-0.003)
9 K& t/ j% ]* ?' L - 0.003" @9 U- _6 Z' | t) Y! J
- >>> math.fabs(-110), ^# G3 M5 @5 }6 H3 @& W
- 110.00 G/ F- I, d& N# B! F2 x f& ^4 k
- >>> math.fabs(100)
A" f2 P" B" `# D& G: c4 j* }1 X - 100.0
复制代码 , q, N# S/ m8 J' g. R2 V+ Y8 Q
math.factorial(x) 取x的阶乘的值# a: R$ j2 a1 M4 S, g& o0 k+ \# z
- #取x的阶乘的值
c; d t9 M- d c - factorial(x) -> Integral
6 U& m9 M6 h' T. B1 O) k. S) x - Find x!. Raise a ValueError if x is negative or non-integral.- I1 Y4 y, w$ h+ n5 q. W; r
- >>> math.factorial(1)- ?: K) d V+ o
- 1; ]3 D$ C* b- c' s/ h
- >>> math.factorial(2)0 S# M" [! y( @6 s( t
- 2% C4 E/ {! w2 |8 c4 S
- >>> math.factorial(3)
+ V% C# g" e8 X# h' v! V - 67 D' |4 |% B. Z1 _8 Y: n5 j
- >>> math.factorial(5)
; p& p( t, m' }7 F1 ?2 `# z - 1205 Q2 r( T! ]/ R$ M# [7 V- c
- >>> math.factorial(10)/ L: D6 I* F" K4 Y9 U' O
- 3628800
复制代码 - l8 u5 K. J( w2 s5 E
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
, C1 R0 h7 e! D. l- #得到x/y的余数,其值是一个浮点数
- r) S1 E: M, u7 s5 J) e& Z7 ? J - fmod(x, y)
4 [8 M% |+ V9 Z. m/ s - Return fmod(x, y), according to platform C. x % y may differ. k8 m/ D5 u6 s0 R6 b; Y; M( R( M
- >>> math.fmod(20,3)9 u2 y: B. R: }% n* S
- 2.0
5 _: f: g& Y% U4 F* Q( l; ]( ?" s - >>> math.fmod(20,7)% v1 p9 a7 f" V# Y! @
- 6.0
复制代码 . X8 t+ }9 [! J+ H4 m- J# j
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 \- M b* I. [) b- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
0 e$ F# G4 ^" ? - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
, E# u! [. A# `% t" E - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! y* T7 Z* X; R( p
- frexp(x)
0 H$ S. L/ |: O+ c& I9 l. H - Return the mantissa and exponent of x, as pair (m, e).8 t: {" ] x; u6 G5 Z: n* H. M
- m is a float and e is an int, such that x = m * 2.**e.
. j" P) V6 d( p$ p# R9 z - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.5 k: f. e! @- X! s& X3 ?% l
- >>> math.frexp(10)
1 H8 R, y- d$ y6 E. N9 B7 v! v/ `0 C - (0.625, 4); K' O( Y5 A- X- `+ D* t. e
- >>> math.frexp(75)1 I7 A* w0 W& U. g
- (0.5859375, 7)
/ n! i) d0 `3 Y) Q' r) ~ - >>> math.frexp(-40)3 M+ X' V% _' V% V& \4 e
- (-0.625, 6)9 B2 G0 b* y! F; U/ T, d" h1 v
- >>> math.frexp(-100)9 n! u- K9 A& M) J2 D) W; e
- (-0.78125, 7)
2 c: w9 v7 v+ @& o( F5 T - >>> math.frexp(100)+ m8 b% r) t4 @1 A
- (0.78125, 7)
复制代码
; a( Z; q( m- X5 Y6 d4 r1 ]math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)4 x+ N7 _" Y% D* I( Q4 H
- #对迭代器里的每个元素进行求和操作
j- \9 J9 x7 a! V - fsum(iterable)9 @& A, e$ o3 {
- Return an accurate floating point sum of values in the iterable.
$ A$ K- X) F" ~ - Assumes IEEE-754 floating point arithmetic.
* N. O8 F, m5 d+ V- f2 }* P - >>> math.fsum([1,2,3,4])
: z$ \8 |1 g! e% A b* L - 10.0
. g) H/ M4 h+ g+ Y6 ]' | - >>> math.fsum((1,2,3,4))# V& ~; k+ M- M( b% B/ D2 T
- 10.0
4 W5 @+ Q( u$ ?! v2 @ - >>> math.fsum((-1,-2,-3,-4))
( p8 r O' P1 ~ - -10.0
4 }6 u0 t1 J" Y- F' e3 _8 {. | - >>> math.fsum([-1,-2,-3,-4])$ W6 _" ~$ J3 `* J% ]9 p. { o) S* u
- -10.0
复制代码
. \" o# a: V. j. q& ?math.gcd(x,y) 返回x和y的最大公约数
- W3 W' O- I) S0 |' m, _, E- #返回x和y的最大公约数
8 u2 k- c+ S- \1 ~5 V5 L0 r6 ? - gcd(x, y) -> int: ] n8 E! V. K- Q! N3 x& R' [' {
- greatest common divisor of x and y
8 `' X+ `2 c8 g0 t9 t8 \8 B9 Z - >>> math.gcd(8,6)! O0 W1 g4 ~) u: g* c
- 2
i; o- s1 p6 l9 k. \6 ]& D- ~ - >>> math.gcd(40,20)
* f# C6 r6 n) N7 l$ h9 O - 20! a9 m0 I k* }" K" F
- >>> math.gcd(8,12)4 ^' B4 E) I6 B0 G7 p3 L
- 4
复制代码
* A" }( ]( K7 Umath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False0 [7 h' ]8 y$ c! Y6 P
- #得到(x**2+y**2),平方的值5 b9 |% j! x1 n9 {
- hypot(x, y)
( k4 O- z: S" g* x/ ~* C5 N - Return the Euclidean distance, sqrt(x*x + y*y).* Q4 C2 {% p/ o/ P: P# m
- >>> math.hypot(3,4) h+ L' }- k8 n: Z1 E1 L
- 5.0: O% R* Q0 \" F0 |
- >>> math.hypot(6,8)
! @# O8 U$ O9 {1 q - 10.0
复制代码
3 V8 o( f* q, q/ q3 Wmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False" h, p3 T8 u/ h# v
- #如果x是不是无穷大的数字,则返回True,否则返回False
" m+ @$ S. {9 G - isfinite(x) -> bool) {- |: c, p9 k: C2 j
- Return True if x is neither an infinity nor a NaN, and False otherwise.
1 ~/ E' ^+ h3 r - >>> math.isfinite(100)
( ~, v' H. K1 g9 j3 W- Q, E - True+ Q t1 Q- t' G6 B1 n
- >>> math.isfinite(0)9 j: s5 A+ k/ P
- True1 u6 }! Y% D- k5 @9 p+ ]) s& E
- >>> math.isfinite(0.1)4 X: I" h" {9 l4 i, g& |( E
- True' G) O5 H( [* R7 O
- >>> math.isfinite("a")
& `6 J* K! W( k) V4 H - >>> math.isfinite(0.0001)4 ~4 v# d X3 y) {
- True
复制代码 ; ]' G5 e! l c4 `
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, q) A; Y# s2 _6 m* H
- #如果x是正无穷大或负无穷大,则返回True,否则返回False# z) d6 g5 X# d0 N& z0 r, k
- isinf(x) -> bool! g6 Q2 D: h, f; N$ K
- Return True if x is a positive or negative infinity, and False otherwise.2 o; d* ?6 H4 m& Z
- >>> math.isinf(234)
( w# j& L+ d1 H& X8 M1 X - False
+ G5 O/ ]/ K. t C& H4 R# h - >>> math.isinf(0.1)
9 P" x m* V3 A, z - False
复制代码 ( ?* C) z+ B8 w0 Q
math.isnan(x) 如果x不是数字True,否则返回False
( O2 K% @) V0 Y- #如果x不是数字True,否则返回False
* m( Y- n. h+ y) t+ k+ W6 D - isnan(x) -> bool* {/ q6 N. |1 \; l9 i3 g
- Return True if x is a NaN (not a number), and False otherwise.0 I% R5 z# m5 f+ V/ q+ V# h. X$ E
- >>> math.isnan(23)# `8 d- e* W d0 W8 G" r
- False; k/ U) G. [0 E3 N
- >>> math.isnan(0.01)
$ [, b& I/ M& v; z1 P o - False
复制代码 ) e, J' E& j6 m. F1 E
math.ldexp(x,i) 返回x*(2**i)的值0 n& G8 h% q' k$ B1 U3 y& N- G& a
- #返回x*(2**i)的值$ \) k' x; V$ B
- ldexp(x, i)
( U4 t3 y% w3 _; G; c$ F" \. e7 \ - Return x * (2**i). d. _3 D" S8 z8 \
- >>> math.ldexp(5,5): W) i7 z9 ?1 R
- 160.0
, H, b# i$ c6 ]' N7 U - >>> math.ldexp(3,5)
u4 ]# p/ ^. x' r - 96.0
复制代码 + R) N) ] ~+ m: l) D: \ w0 a
math.log10(x) 返回x的以10为底的对数- W2 R- a' m( ~
- #返回x的以10为底的对数8 @, f, T) p/ U. f+ L/ {
- log10(x)
. s0 q6 C [2 Z9 D8 W- C4 a; o - Return the base 10 logarithm of x.
. J5 S- v, x, W - >>> math.log10(10)! m& A& c) v9 b+ V' e: K" \, ^
- 1.0/ C+ b5 D' C" N" ?9 J) U8 ~
- >>> math.log10(100)3 s) ^* `7 k( u, W
- 2.0
9 s. Q) ^& m; O4 j& V - #即10的1.3次方的结果为20
) _! t$ ]' F. K A. K - >>> math.log10(20)
* j- |1 G+ t `: | b4 v - 1.3010299956639813
复制代码 z* C4 c6 [" l$ y" _/ a. r3 H
math.log1p(x) 返回x+1的自然对数(基数为e)的值. W8 Y( b: Z' \. a/ S. |
- #返回x+1的自然对数(基数为e)的值
/ t- {& ^4 r1 `6 D/ R, s& T - log1p(x)
/ `" V4 k4 c B: H5 g2 V - Return the natural logarithm of 1+x (base e).0 K3 v2 B: |2 B5 B0 H6 E" r
- The result is computed in a way which is accurate for x near zero.% d8 X; B$ \. Q2 W! ^
- >>> math.log(10)6 p& U) Q8 k. b
- 2.302585092994046
9 h2 T& d( e& x5 K2 L - >>> math.log1p(10)
2 n% {; {) c7 b" M: ?- x - 2.3978952727983707
# b5 b# C, i' T' n, B% H8 c - >>> math.log(11)" l, a5 \0 f9 X% [9 y
- 2.3978952727983707
复制代码
3 W& w# k6 \) I# gmath.log2(x) 返回x的基2对数, S/ M6 _1 ]5 O- }* u f0 w. d, _
- #返回x的基2对数 c! P# w* b: Z8 F
- log2(x)
+ g! P2 r [+ n$ } q o - Return the base 2 logarithm of x.
A$ S8 Q& W6 v' M, T - >>> math.log2(32)
! o/ o" G! g9 R - 5.0' ~& b/ M* C6 Y6 C* Z
- >>> math.log2(20)2 i9 Q+ O4 `/ e: ?
- 4.321928094887363- Y* R5 a5 m- E) |
- >>> math.log2(16)
; S( F+ i5 q6 Y, u2 c' y - 4.0
复制代码
N; I) X: m Z4 Q/ pmath.modf(x) 返回由x的小数部分和整数部分组成的元组
% x/ B3 N" B' Z) I3 X; T" s; N! o6 Q- #返回由x的小数部分和整数部分组成的元组
5 b+ R. f8 W5 f( ^ - modf(x)8 J) S4 q, U5 [& i4 Z1 ]: t7 ^- M: F# m
- Return the fractional and integer parts of x. Both results carry the sign+ n/ }6 `& |% u2 r- }
- of x and are floats.+ @, x' D: ~/ { x6 r
- >>> math.modf(math.pi)- [7 x. g& h' g* A/ d5 [9 U
- (0.14159265358979312, 3.0)6 n% z: ~4 f3 M m2 p' P
- >>> math.modf(12.34)
# h/ O8 Y& C5 f$ { - (0.33999999999999986, 12.0)
复制代码
; p9 \: |- i- c# B2 mmath.sqrt(x) 求x的平方根 g' d1 f& Q* @/ ]" Z
- #求x的平方根
8 n5 P8 A$ n; K/ w - sqrt(x)3 B% ^2 B5 @ I) K9 g- H* K
- Return the square root of x.
5 a Q' S" z& a X+ j3 n# ` - >>> math.sqrt(100)4 x3 M% u. N; ^% a
- 10.0
6 L( \6 O# \, W- @ - >>> math.sqrt(16)
) I7 u1 d" g6 R% s2 q2 o; A - 4.0
7 \" `) ]$ ~4 R. m6 \* t - >>> math.sqrt(20)
9 e/ W4 M% |/ Z5 c( t' c# m' D - 4.47213595499958
复制代码
4 E: ]! Q/ U6 G3 T5 [+ d6 Dmath.trunc(x) 返回x的整数部分- #返回x的整数部分
/ m1 X4 i$ q2 Z: P% X/ P - trunc(x:Real) -> Integral# K) ^+ M4 @9 _; K
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
+ S7 C/ x3 p E) Z& r - >>> math.trunc(6.789)
* T4 ~/ f( B( Y - 6
+ s2 t. r! S' }7 n, @' | - >>> math.trunc(math.pi)& A$ |% I$ a9 N( s! k( t
- 3! D, w" T) }+ d- }0 b
- >>> math.trunc(2.567)7 W; Q* I: y. }; ^# b3 i- ^! h- G
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|