马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- F- \# Y" Y5 @: F5 h. H! c- k
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" }5 m' L$ d K% h5 n' u! N! g7 ?5 O8 N; d
方法1:% r5 x$ D6 N; n' j! e2 a! j3 C, ^1 I
- >>> import math0 n2 r! ~6 ~2 y: i: A0 L5 A+ Y: {
- >>> math.sqrt(9)
6 t0 K/ N( _1 Y - 3.0
复制代码 方法2:
& H. }# g9 v" r& k8 l8 a) B% v- >>> from math import sqrt8 o+ h [; l+ h0 Y
- >>> sqrt(9)+ d5 L* Q# R; X( n5 b/ H
- 3.0
复制代码
. |3 o+ B1 J9 R( O# Q9 [* y& g2 H7 j
2 n5 F" r# H3 p& b" G7 _math.e 表示一个常量0 k" j" e/ w$ u0 B; M
- #表示一个常量
4 o7 C& n$ V! I# f; Z1 \1 W - >>> math.e* c {9 p3 F8 ?' d# M1 ] Z
- 2.718281828459045
复制代码 8 p, X1 `' O8 e/ `7 G
math.pi 数字常量,圆周率8 g: n! U& B) v+ p, Z: G
- #数字常量,圆周率
+ h1 d- L* h0 _ - >>> print(math.pi)* ~( i4 |; }& ~ N
- 3.141592653589793
复制代码
: D. p3 s' W0 Imath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 b% ? c) J+ K* S! e- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 T5 t2 X% \; J6 `; m - ceil(x)
! L! }" \ G; i" s" g+ ? - Return the ceiling of x as an int.2 y' n$ }9 F7 j) ?
- This is the smallest integral value >= x.+ R i' Y( G0 ?
- 9 Z5 f/ O! l) V7 a9 X
- >>> math.ceil(4.01)$ R! ^* x- b: f5 U+ ~
- 5
+ S% D* R% J9 U5 E+ k - >>> math.ceil(4.99)
: o; A! B1 z$ B: w. K: L& A; c$ ~ - 54 n. L# j, ?) b/ {
- >>> math.ceil(-3.99)
, }: T& A' I' ?; H: u/ V6 p( d - -3
! h8 R0 c0 O9 G$ n' C) J% F - >>> math.ceil(-3.01)
7 ^2 G9 P0 n7 p: k - -3
复制代码 0 F- f' A4 X7 z$ j* D# \
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; H2 \1 [! O3 T* J- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( f& \. c' b* A2 |: g - floor(x). h' i# s" f, @; q/ [/ q5 l0 e* G! s
- Return the floor of x as an int.! J6 {1 N9 X2 i2 ]- l- b
- This is the largest integral value <= x.
0 [! Q, M% T1 ?6 |4 e# R5 Y) J - >>> math.floor(4.1)
: E( H& B! D3 R- r" n - 42 C0 [) m5 V4 p2 H
- >>> math.floor(4.999)* U2 e. i3 H: v: d: u
- 4 A3 ~' M+ X; s9 y/ z9 c) b$ J
- >>> math.floor(-4.999)
+ q! U5 [" q0 j - -5. @9 U/ E+ J& \# b, a
- >>> math.floor(-4.01)0 R: Y& `* A- _% S. m
- -5
复制代码 # \* @9 ^" [/ { H# O! q- u7 o
math.pow(x,y) 返回x的y次方,即x**y! L) y u, t; K X& Y
- #返回x的y次方,即x**y" Q. T9 ~5 Z& h% H* N1 T% T" T! e# f
- pow(x, y)3 E4 x6 d7 u8 X& |2 l
- Return x**y (x to the power of y).8 _. y& U1 r: T6 T( t4 Q3 @7 q
- >>> math.pow(3,4)
4 m8 E& s; ]& E% B, h - 81.0# K( x4 t% _$ q; K& D7 z
- >>>
3 I2 d" u, |' L' `3 @$ w - >>> math.pow(2,7), r) P( j* n' f* r- T4 W! G) R
- 128.0
复制代码
- ^* Q/ `& R% amath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 r9 u3 j% p* Z) G6 C
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' o. ^4 N' B. P+ E$ M$ m# ` - log(x[, base])
; h/ j8 h9 B" E, ^: M - Return the logarithm of x to the given base.; j2 ^1 E( {: Y3 a% W
- If the base not specified, returns the natural logarithm (base e) of x.
/ E) x% ?0 M2 g, p( z D8 m O3 D - >>> math.log(10)" p1 O4 w4 J) Q* K3 q/ l3 P6 ~
- 2.302585092994046
2 S1 n( P, s6 w; H# l% ^ - >>> math.log(11)% A; \% z: z" s- N; C% s
- 2.39789527279837070 L* `9 v' w9 {' x
- >>> math.log(20)% k8 @" a+ M2 L \
- 2.995732273553991
复制代码
$ S5 ]& [8 A$ l& ?0 Kmath.sin(x) 求x(x为弧度)的正弦值
3 s! q+ K5 Y7 h3 W; h- #求x(x为弧度)的正弦值
& i) ]7 w2 C& v - sin(x)! V0 v+ x1 @; N
- Return the sine of x (measured in radians).
+ o! o$ ^2 J9 i5 w" c! _ - >>> math.sin(math.pi/4) j9 U; K) C! y& {" N2 `' v
- 0.7071067811865475
# F; m9 B0 W3 q* j+ l- G - >>> math.sin(math.pi/2)
/ `# R# Z2 a: ] - 1.0
: o" z! J1 c: G( s ]0 `8 q) y5 c - >>> math.sin(math.pi/3)
( j$ t& S, X d, h, K( b1 R - 0.8660254037844386
复制代码 & E& M7 O: T8 Y; A* C; l1 w2 v$ H1 i: A
math.cos(x) 求x的余弦,x必须是弧度
3 P2 ~& Q; U$ d. O9 e- #求x的余弦,x必须是弧度+ F2 f7 {; |( A3 S
- cos(x)
/ v. B; |( A* K - Return the cosine of x (measured in radians).7 V1 K0 n. N0 H, p! }1 u
- #math.pi/4表示弧度,转换成角度为45度
. B \. B0 @! o2 g - >>> math.cos(math.pi/4)
' p" {# G: s) b6 H8 x7 ~3 d8 t - 0.7071067811865476 G5 U$ K: c1 S9 x+ N& g
- math.pi/3表示弧度,转换成角度为60度
' \/ Y7 O, A$ j4 B- ?# Z - >>> math.cos(math.pi/3)+ t( `; C( o% i
- 0.5000000000000001
) Z; s3 \7 V! ?+ _- s - math.pi/6表示弧度,转换成角度为30度$ ^6 a! J7 m7 T
- >>> math.cos(math.pi/6)
' @ S1 \% X: ^5 B* R - 0.8660254037844387
复制代码
3 @% v7 D( | r' Z5 C& ~5 zmath.tan(x) 返回x(x为弧度)的正切值
" L- s5 E* e- z& x, V) H- #返回x(x为弧度)的正切值
) n8 N/ B- g1 @5 C( ~* ^: `5 v+ ? - tan(x)
7 P: Q6 T2 A( Z$ q+ Y8 f! T - Return the tangent of x (measured in radians).' h2 m- ]- K& b# o- q
- >>> math.tan(math.pi/4)
2 c0 H3 v) I' x3 N# K - 0.9999999999999999
8 F) x' x. q; r3 b* {7 F2 N - >>> math.tan(math.pi/6)6 i0 C- z2 H! X/ @7 q; B" k" m
- 0.57735026918962576 c! G. Y) x) i0 ]/ x
- >>> math.tan(math.pi/3)% J5 j; H+ \8 r6 h! j: u! ?
- 1.7320508075688767
复制代码 ! j4 w; r6 U; ~
math.degrees(x) 把x从弧度转换成角度6 {0 v. E) N, e4 D
- #把x从弧度转换成角度4 \& V, V! t6 y
- degrees(x)
6 g/ e) L" m9 z - Convert angle x from radians to degrees.2 M! _, A7 [) y0 c
, O g; a$ q. B- >>> math.degrees(math.pi/4)
2 g* N# R( ?8 @2 n7 b% ?; M - 45.0* R: U' }5 v! h+ k6 U& I( ]
- >>> math.degrees(math.pi)
& ^$ D- I" B# c* {) L& l1 m( \0 p - 180.0
; W" b# z4 q7 F9 I; ^3 } - >>> math.degrees(math.pi/6)7 @# Q: u; K0 N$ p
- 29.9999999999999964 ~$ h6 P% O. ^/ E$ {
- >>> math.degrees(math.pi/3)
[7 H! a+ Q- i) z( s5 ` - 59.99999999999999
复制代码
; S( b8 J/ P8 r2 vmath.radians(x) 把角度x转换成弧度$ o" G8 h- _$ w
- #把角度x转换成弧度
' Y! S! w/ w2 e0 B0 U - radians(x)
3 H! K6 B& ]' |0 \+ I# A' L - Convert angle x from degrees to radians.9 Q3 o6 d# L! m; F: `* f
- >>> math.radians(45)
3 w9 C1 r! h" Q ~2 M/ e - 0.7853981633974483
% k& a4 Q' G; O" C1 L. l) n - >>> math.radians(60)( a5 p& ~6 @9 ?8 i1 u) K
- 1.0471975511965976
复制代码 7 C5 l# N! d* I+ K# z
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 x* o1 N+ ^; C" a- #把y的正负号加到x前面,可以使用0
( x W9 C$ ^$ i3 ~ Z- e, G7 ~ - copysign(x, y); k( Z8 W# Z5 G0 O4 {) B/ U
- Return a float with the magnitude (absolute value) of x but the sign
o* @6 c/ X. F - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
4 n1 z: J5 ?& N - returns -1.0.
% U0 a" W5 b) N - ' O0 T- `+ o# q. }& J9 \3 U2 Q0 ]
- >>> math.copysign(2,3)( [. x5 O% |- Y: Z$ s- n
- 2.0' ~9 {" G8 O- r! N* O1 Z
- >>> math.copysign(2,-3)
! F" A- k! ~+ y7 ?+ ? - -2.0* y" P, E+ L# k$ c
- >>> math.copysign(3,8)
6 d$ J: ~2 L; l0 D5 a, \+ n2 G. p4 M- Z - 3.0
. d4 b" ]. U! h0 q+ L. X - >>> math.copysign(3,-8)( q" J: q0 R( n# F/ d+ ^
- -3.0
复制代码 - c, t/ o! d3 ?
math.exp(x) 返回math.e,也就是2.71828的x次方9 r% N6 Y- `6 F9 F
- #返回math.e,也就是2.71828的x次方
; I1 Y3 k$ n5 p3 F - exp(x)
( ^1 b3 E7 i2 X+ j' \' k - Return e raised to the power of x.: v( j2 m9 R+ u
, p* x5 e o& e7 b4 R# u! @+ W- >>> math.exp(1)
" ^- P4 \/ Y0 g: L# [9 ~ - 2.7182818284590451 G% j8 s6 D5 `! o( z
- >>> math.exp(2)- ~1 o' W+ [) y Q9 v5 {/ W' o. T
- 7.389056098930651 u. \7 q; a6 Y" K8 I) A4 l
- >>> math.exp(3); \1 f8 a$ h* M$ Z* R0 k
- 20.085536923187668
复制代码
/ [* Q+ K0 n% W/ Emath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减16 h+ _% Y& K+ S
- #返回math.e的x(其值为2.71828)次方的值减16 h2 v6 L# _8 L
- expm1(x)( n% V0 B3 M! k# x) B
- Return exp(x)-1.
# ]- y3 x* n8 I6 z' O ~* r - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 }' W6 |* f2 E2 F5 N! { - 6 }5 n, `, U1 r/ L& ~0 a4 F, r
- >>> math.expm1(1)5 o; T3 h) u1 L j( I
- 1.718281828459045! H9 P1 b6 {9 w1 P: m, M& {
- >>> math.expm1(2)
4 A1 J( n) `( \- ]- K! L- m. Y - 6.38905609893065
; \8 I6 F5 N' w; m& ^1 C/ }' M ]4 A - >>> math.expm1(3)
: ~ [4 U3 U3 G - 19.085536923187668
复制代码
: x& C R; ~+ P) hmath.fabs(x) 返回x的绝对值5 f6 ^6 \6 s$ z% v2 K: p. H- t
- #返回x的绝对值- P' ~8 D+ z: W2 r6 V/ P
- fabs(x)
" K! b* o, N5 T - Return the absolute value of the float x.3 E* s1 y6 [& N. `( H; p
- ; O7 b& R# R- ~- a p$ Z/ [
- >>> math.fabs(-0.003)
% _/ U: Q* S$ b6 a9 M# T* R1 B) ?+ `& j3 I - 0.003$ t6 r: P1 ~/ C" y7 f) P
- >>> math.fabs(-110)
" L% z; v1 @2 V) E6 I' R - 110.0
2 d' e3 U* v6 o2 Q - >>> math.fabs(100)0 A5 ^4 { L2 r1 b
- 100.0
复制代码 $ n# J2 z# R: |9 Z
math.factorial(x) 取x的阶乘的值
* |5 m: S. X0 J, o/ P/ i8 P5 y- #取x的阶乘的值 P! p- s; S' O d4 w
- factorial(x) -> Integral
6 n, o0 h( q8 V' c - Find x!. Raise a ValueError if x is negative or non-integral.
* F- N8 w" i$ L! c; d! C4 } - >>> math.factorial(1)2 v1 {+ w( Z+ R
- 1
; e" T( x# v" H - >>> math.factorial(2)
+ i+ l! F% g* ` - 20 y( _3 B' C$ x4 Z" v! Y; B
- >>> math.factorial(3)
) `/ R( Q. n, `; ]) _/ r, w3 } - 68 G1 p& k4 G! H! B7 E( g' @
- >>> math.factorial(5)
8 M6 m1 F0 `% E/ m2 t- w1 y( z( n - 120' s1 j1 h! B a$ I* r. j
- >>> math.factorial(10)" c: j$ x& L8 n/ m3 W% x& a
- 3628800
复制代码
' z1 x" S+ s0 ^/ s/ L( ]# K( P# O `math.fmod(x,y) 得到x/y的余数,其值是一个浮点数0 M( r8 H1 V) }. Q7 V+ d
- #得到x/y的余数,其值是一个浮点数
/ o1 H/ S: e5 V - fmod(x, y)# w* }0 h* ` A I* {4 U& H& N* `* P1 o
- Return fmod(x, y), according to platform C. x % y may differ.3 M) d( }# z8 V
- >>> math.fmod(20,3): U! M: f! r* o) o7 M2 ?1 {& i
- 2.0( C$ S8 ^* J8 [: T( f( Y* O7 u
- >>> math.fmod(20,7)$ r- s4 ~9 e7 u9 _7 [; V9 B/ _
- 6.0
复制代码 $ g4 _7 N" Z2 G+ J5 Y9 h
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
" h" Z0 c" W. [3 V: h4 U- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 _% u5 o' ~, u5 v6 u$ T - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
, T# ?, E( q2 S5 w. h/ m5 f- U - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
( t% o1 i6 X. {( u& N( O - frexp(x); L4 x8 M2 d- q9 u7 R/ D% e
- Return the mantissa and exponent of x, as pair (m, e).# ?9 q! `3 y" W: g
- m is a float and e is an int, such that x = m * 2.**e.
9 \2 Y( S' d5 `6 W" s8 e# a6 R6 w- X - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
$ N7 @% w# P0 E* k5 U" {- Y4 Z - >>> math.frexp(10)
, f7 ~* p. `; ]' A# L/ X - (0.625, 4)
. L7 w) }# f) x& k - >>> math.frexp(75)
: C/ m$ k! m$ a1 F - (0.5859375, 7)
3 m& s7 W: T+ H- K - >>> math.frexp(-40); W( q' } G# ]4 H _% T
- (-0.625, 6)6 q! u* ^* @; ^* L4 V8 }5 Q
- >>> math.frexp(-100)- ^# m+ Y) O8 R( }5 F a
- (-0.78125, 7). ~* u/ |& q4 s* P
- >>> math.frexp(100)
" B* S2 ?0 g9 z) T - (0.78125, 7)
复制代码
2 Z, j: Q ?: t r% n8 a5 q1 bmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
0 b6 ^/ E5 k- U0 q- #对迭代器里的每个元素进行求和操作! w. {+ |6 T6 N/ z" c
- fsum(iterable)" q D( s) Z& [8 H% H# t
- Return an accurate floating point sum of values in the iterable.( B, \4 e+ l% a/ o' B
- Assumes IEEE-754 floating point arithmetic.
# E m7 t: y2 @ m - >>> math.fsum([1,2,3,4])6 s7 u9 ]9 E* z' f
- 10.0
# O/ p p2 c" v! i. f - >>> math.fsum((1,2,3,4))
2 I6 D9 F3 C$ j8 B, k2 O2 N) }3 T! ^ - 10.0
* e# A) z2 P: _ - >>> math.fsum((-1,-2,-3,-4))
/ p0 }. X* z/ ^/ d$ G - -10.0* i0 m |7 s8 {
- >>> math.fsum([-1,-2,-3,-4])6 g/ w5 w- A# g/ U
- -10.0
复制代码
! ^2 o5 }4 u' f* ]8 mmath.gcd(x,y) 返回x和y的最大公约数
& ^. H$ t% C9 T$ f- #返回x和y的最大公约数- |* U8 v( W2 K
- gcd(x, y) -> int
, V% @ `! V Q! n0 ?5 n - greatest common divisor of x and y
8 m0 O7 J7 a+ X - >>> math.gcd(8,6)
% Q' Q( G0 F' J7 M& [' B) P - 2
7 @& U& `' t5 ~7 T! f - >>> math.gcd(40,20)
; H* s# |/ N+ I, `) T! q - 20
" X; @+ q' H' {% \( A. L' {( P& y - >>> math.gcd(8,12)
* Y. v- K. V" f: v% g/ r$ n - 4
复制代码 - y' p: U2 R1 }$ r
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False: d/ O, ?5 M& g% O) l# ]
- #得到(x**2+y**2),平方的值
5 _" j0 E: W9 H! \# v B7 H - hypot(x, y)
; |* p' h! ^# S& h - Return the Euclidean distance, sqrt(x*x + y*y).! _; ~3 O# G2 {) L
- >>> math.hypot(3,4)" z+ m' G' ^: g9 u
- 5.0: o/ H `" a0 w( Q8 N
- >>> math.hypot(6,8)
4 X2 u" _; w! a! \4 K - 10.0
复制代码 : G4 P! T; F% A6 P7 M4 d& a
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
4 f1 x* v n3 c+ j; R- #如果x是不是无穷大的数字,则返回True,否则返回False
8 |7 t6 z( z" Y8 }3 C% Y. b: ^3 Y, z3 M - isfinite(x) -> bool
, o+ V$ o( [9 q) N( K - Return True if x is neither an infinity nor a NaN, and False otherwise.( R, Z* w1 q7 ?* S2 z' e8 F
- >>> math.isfinite(100)
2 J/ ^0 u, B3 w - True
/ R) R1 b8 R0 R1 T# S! ?! F - >>> math.isfinite(0)
4 w+ d" ~3 H) _# \4 ~6 c - True
% A5 L; Y& p' o: |$ A% s - >>> math.isfinite(0.1)1 L9 B+ \$ v6 J1 U) L5 Y* s
- True
+ F2 E' I( ]6 B5 t - >>> math.isfinite("a")* V; y$ B% i/ U* e
- >>> math.isfinite(0.0001)
5 G; o e" n6 D, {5 G4 |- `* O8 H$ J( ? - True
复制代码
2 }) N" {& B6 ^( U/ g: X3 e3 pmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
. k, f+ P- j: k/ |- x3 z9 p- #如果x是正无穷大或负无穷大,则返回True,否则返回False
4 q/ P. N8 H: N7 y/ b - isinf(x) -> bool
/ x$ a0 a# ?) C2 `$ J - Return True if x is a positive or negative infinity, and False otherwise.5 s, r# m3 N+ f/ t
- >>> math.isinf(234)
- K2 a \. D: H8 P6 f [& k - False
" V8 E/ l) U# k. F2 C. }, X, P - >>> math.isinf(0.1)! r" I; N' z- U0 d
- False
复制代码 , V& s+ v$ S8 {- w
math.isnan(x) 如果x不是数字True,否则返回False
r' P0 j3 `& c4 z0 z- #如果x不是数字True,否则返回False7 `: |# |3 W2 X/ u
- isnan(x) -> bool
9 P$ W5 [! F V - Return True if x is a NaN (not a number), and False otherwise.
% e6 E9 _& \( k - >>> math.isnan(23)
3 Q" e) v5 o0 s S - False& {- D8 ?" K2 S* Y0 y0 L9 F7 _& R
- >>> math.isnan(0.01)
0 j) Z; z, v# N( P - False
复制代码
8 J7 P, u8 Y* S3 W, r- C4 Wmath.ldexp(x,i) 返回x*(2**i)的值4 V$ r3 h8 Z* P- W/ A$ R% ~, F
- #返回x*(2**i)的值
: U, V& e0 c" ~) _9 A - ldexp(x, i)
/ G# O7 t. j& j5 I8 \& k' | - Return x * (2**i).: ]( J% i! s" C* n# Y3 g( h. d
- >>> math.ldexp(5,5)& ~8 |7 F3 Q5 R9 z2 D1 S7 k
- 160.0
" |( N) c) p4 n) k) E4 ^ - >>> math.ldexp(3,5). o# T; R& V0 w4 x
- 96.0
复制代码
4 r5 O: d# C0 [7 Lmath.log10(x) 返回x的以10为底的对数" s& i3 S* E! ?6 G* q. `" S
- #返回x的以10为底的对数
! c+ \- P) W' k1 w a - log10(x)" i1 q4 B" T, F# j
- Return the base 10 logarithm of x.
& r6 C* @3 d; q6 q - >>> math.log10(10)
/ d& e# Z9 i+ S - 1.0
9 d4 L9 f) C5 q; c9 ?2 | - >>> math.log10(100)1 c6 g! S8 O" ~- l; U, Q# w( ?/ q: H* j' J
- 2.0) q$ e. X3 A- I
- #即10的1.3次方的结果为20( \7 E% U/ K' z1 w& O
- >>> math.log10(20)4 l- _5 ^- d$ d9 n& U* c4 [# Q
- 1.3010299956639813
复制代码 - I# [% t) s, u% H
math.log1p(x) 返回x+1的自然对数(基数为e)的值 J" d( e" L9 ^8 R) L$ {
- #返回x+1的自然对数(基数为e)的值. S6 A( w0 Z1 Z5 M4 q
- log1p(x)
$ p7 @! W8 R7 E" m0 _ - Return the natural logarithm of 1+x (base e). }! }2 S3 I3 z
- The result is computed in a way which is accurate for x near zero.7 g+ \/ E( l" @/ x/ ~
- >>> math.log(10)+ S" m2 ?7 b% R$ G1 V
- 2.302585092994046
: s4 s5 a) n- G1 X" w - >>> math.log1p(10)( j/ f0 P( N8 n: I9 Y7 N
- 2.3978952727983707
! b' Q; Z# ~! W; l9 C - >>> math.log(11)* P' ]. e; d: l( t
- 2.3978952727983707
复制代码 # D( k" N6 v4 u$ O3 n' f* R# z
math.log2(x) 返回x的基2对数$ @/ ?. e0 _% E
- #返回x的基2对数
6 K; P1 S3 o- U W$ n - log2(x)4 o/ V3 R# F% j; _
- Return the base 2 logarithm of x.
% A5 h, ]$ f4 d. n! N/ f: k9 G7 t - >>> math.log2(32)
4 N" }) y1 D, r7 c$ X - 5.0) {) g: O" ~. B. i: `9 \: K
- >>> math.log2(20)
6 T1 @8 E8 k* Y - 4.3219280948873639 K4 I" o, k1 Z, f" m
- >>> math.log2(16)
' k6 f1 {& }5 D3 q6 K - 4.0
复制代码 ! w" A6 N! D% l: ]& q
math.modf(x) 返回由x的小数部分和整数部分组成的元组6 A0 U4 c/ V j, Z* C7 y6 O
- #返回由x的小数部分和整数部分组成的元组
1 V6 V2 o1 K/ E/ Y - modf(x)
8 s. O9 S" J6 Q" R$ y! o8 H - Return the fractional and integer parts of x. Both results carry the sign$ U2 ~# c2 e9 _+ Q4 K! ~) `+ `
- of x and are floats.
, ]0 G+ m+ \) G0 |9 r/ _ - >>> math.modf(math.pi); A/ u9 ]; W& D
- (0.14159265358979312, 3.0)2 b9 U5 K2 m k& L. S
- >>> math.modf(12.34)* r5 E0 s9 w7 s) G( N: y
- (0.33999999999999986, 12.0)
复制代码
- Y2 D: ?$ {( J/ [math.sqrt(x) 求x的平方根
, w! \' ?1 S; ?( z. N9 a/ w k- #求x的平方根! F5 o4 O; j! c' Q" B
- sqrt(x)
% H) e$ ]& J3 \: R3 r& h# q - Return the square root of x.
( @' v0 f$ x" x$ Y9 f$ ? O7 X6 I2 d - >>> math.sqrt(100)
: w5 ~$ x S- @ } - 10.0
; T- `9 E2 a5 O9 X0 M4 g0 V7 f9 ^ - >>> math.sqrt(16)
% g' }7 V9 o) w# D3 A5 O* i9 x - 4.0
& |+ D! P' z, Z+ m/ B* Y8 X - >>> math.sqrt(20)
- a0 ^3 g$ E0 C X0 i5 o. r - 4.47213595499958
复制代码 2 `% Q* D' Q6 T5 R) a% G& S
math.trunc(x) 返回x的整数部分- #返回x的整数部分9 s9 z. i& _" Y. ^7 R }2 J1 y
- trunc(x:Real) -> Integral
6 b b5 `4 @4 F$ P - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
- c2 g1 A2 T9 N: j7 @. Z- O - >>> math.trunc(6.789)
9 A; P5 C I* e( V# |% b - 67 }4 Y+ v2 u! ?! f' S# R1 p" ~- @5 v
- >>> math.trunc(math.pi)
$ ]+ J9 a* z- c% z& X& W0 L5 h - 39 B& c) P! E, `! G e8 N" U, m
- >>> math.trunc(2.567)
) G' i# q% t) d3 G. E2 \2 s. m* C - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|