马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" V. o5 E! I1 U9 h( b; k【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 t7 g- v4 I# D8 S, v
- H- B/ h! b9 w8 X" [. y% o. q方法1:
; X: w6 j& ]! T; R( J, J- b- >>> import math
/ X+ k- o4 _+ ` `6 T - >>> math.sqrt(9)
4 u g7 m7 ~( Q# r# l! f0 U - 3.0
复制代码 方法2:
7 d: r# [) n9 u$ l' \4 M- >>> from math import sqrt# D$ I- n2 Q, R$ \. o/ L4 ~7 H
- >>> sqrt(9)
. r: q4 o) O/ D) _) a2 ^- C - 3.0
复制代码
! N+ \5 ?! U6 B/ o, @. [( J) l
0 j- N) d( `! h. h5 L& Wmath.e 表示一个常量
* H0 s$ G; `8 X/ |% M# E/ N- #表示一个常量
* e* n' i5 I I' z6 v - >>> math.e6 C8 G: F1 x# M# ?
- 2.718281828459045
复制代码
2 ] q0 w& y$ n! u# ^: e/ b3 R& v! Qmath.pi 数字常量,圆周率/ F( n8 R8 U9 o5 E* ^+ b
- #数字常量,圆周率' P4 h3 f8 T- C7 h. |
- >>> print(math.pi)
. S. X2 |& u0 p/ a+ F, H - 3.141592653589793
复制代码 - {2 U! Y( |9 D9 }6 \2 ?: k3 e$ o
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 ]4 F0 O- C+ Q) q
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x1 j3 }. o- H' k, A5 }2 k& Q0 h, L
- ceil(x)% q+ P5 \. g z- u% t
- Return the ceiling of x as an int.% l. E9 Y. p9 T, W
- This is the smallest integral value >= x.
8 C# M9 m5 t/ _5 U
/ [0 |6 Z3 d& _# ]! U5 a% E- >>> math.ceil(4.01)' [- _8 p6 d( B* G |8 b
- 5* N9 J5 b6 a l ^7 m: o: e' K& V
- >>> math.ceil(4.99)/ ?: E& t6 ~* s" G) f- S; p
- 56 }' `" g4 F8 I$ F) j
- >>> math.ceil(-3.99)) }; j4 \: a/ o
- -3- g& y3 T7 Z5 P8 s6 p
- >>> math.ceil(-3.01)& y! i" Y4 H% t- T
- -3
复制代码 . A1 l: z+ I! W+ Y( H. H
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 m; }2 i+ ~; @% X3 R
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) c% L) K3 R1 y5 @& F' O - floor(x)
, u1 ^8 x$ f" H+ R - Return the floor of x as an int.; F/ t1 }4 e' P' }
- This is the largest integral value <= x.- Q& z6 i8 K; E4 G4 o- w% k
- >>> math.floor(4.1)
3 y0 D1 U5 I8 } - 4: m8 E2 \& H# Y& h% @
- >>> math.floor(4.999)8 r9 T" M; b! d4 s
- 45 R; f, @- r2 C0 m
- >>> math.floor(-4.999)2 X1 K" V9 D, |$ L! |: f: H
- -5
* |8 f& w3 ^4 e: [ - >>> math.floor(-4.01)8 L S" p' z# N( i$ s+ r. u+ f
- -5
复制代码 . `; a5 k2 D4 i! I9 o: d$ Z5 g
math.pow(x,y) 返回x的y次方,即x**y2 a! ]: q4 O) C4 k. `/ O
- #返回x的y次方,即x**y
: A8 S& f5 u/ |# j z$ P - pow(x, y)9 i) f' q: B$ B/ @
- Return x**y (x to the power of y).9 T, ]! G Y1 E
- >>> math.pow(3,4)
2 v0 N. ? _* [: S - 81.0
" b: W2 v5 y7 I1 L0 z - >>>
# Z& a/ ?9 s( _5 l - >>> math.pow(2,7); I& l x1 x X1 P) c
- 128.0
复制代码 w% F4 l! t/ a7 y& [6 g6 c
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). T! G5 I0 I/ e, C' y, ?; r# ?
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ G/ U5 U; x- o9 \2 R0 s
- log(x[, base])
7 [4 B( D1 C+ ]0 g; o: D - Return the logarithm of x to the given base.
& w7 ~# C; @6 q2 e1 R - If the base not specified, returns the natural logarithm (base e) of x.
6 S; m& J/ [2 ~. \0 o) O; V - >>> math.log(10) f( T$ ~5 t7 w2 }% A
- 2.3025850929940465 S+ v$ s( B; c q! g
- >>> math.log(11)
5 I5 }+ k% z- r4 B9 y- h% h - 2.3978952727983707' Z- z; { U9 I" ^1 |" L
- >>> math.log(20)
) P( D, {" ?7 i9 w; T7 b7 r. W - 2.995732273553991
复制代码 ! J9 i% E7 K$ _
math.sin(x) 求x(x为弧度)的正弦值
* P; r, L1 i: ?- #求x(x为弧度)的正弦值
_0 l3 k* J! A$ @3 U" g& e& t - sin(x)5 Y. k% L' ^* ?
- Return the sine of x (measured in radians).# s+ f( k$ r8 }3 ~$ J8 ?7 g) V3 K
- >>> math.sin(math.pi/4)
( h4 d; P/ n* j0 M) U. l0 z& B - 0.7071067811865475
3 S" C- K9 t( s - >>> math.sin(math.pi/2)& h$ l4 i; \2 S9 r2 N% ] e1 I
- 1.0
( y% O% x1 Z# S' H. M6 @ - >>> math.sin(math.pi/3)
" m' E4 r1 }8 D1 t) ^0 `, N - 0.8660254037844386
复制代码 & b. o- C [; I: m) C: @
math.cos(x) 求x的余弦,x必须是弧度: w2 ^) F* @+ m y) F6 N6 @
- #求x的余弦,x必须是弧度
+ Z% E- N+ G$ N- U, h8 A - cos(x)- w7 |+ L% J) P) D
- Return the cosine of x (measured in radians).
* K( q7 F# M4 p - #math.pi/4表示弧度,转换成角度为45度
" |+ s3 H) J( u+ `' g* v! F* C - >>> math.cos(math.pi/4)
; g( i" |2 {' {- P% w% \ - 0.7071067811865476
0 F( s; M- d W - math.pi/3表示弧度,转换成角度为60度2 U2 e' V4 d+ Y' M8 T) a: C
- >>> math.cos(math.pi/3)$ l# J! b$ R* L& m
- 0.5000000000000001, Y3 E$ f2 Y( f' s
- math.pi/6表示弧度,转换成角度为30度
- ^- I! E# o/ a( o3 [2 H9 V0 t - >>> math.cos(math.pi/6)+ T$ `2 } `0 O# r# t
- 0.8660254037844387
复制代码
" i( x- Z% | q9 n& d5 [1 Xmath.tan(x) 返回x(x为弧度)的正切值
2 {% N' K# J7 X9 G6 V) l- #返回x(x为弧度)的正切值* l' c& Q" j* n- U
- tan(x)
T* ?% y6 a7 e+ K# S - Return the tangent of x (measured in radians).
; b; ~3 o+ \4 A* a5 V( q - >>> math.tan(math.pi/4)
' Q. c/ _) C, b% S Q. h6 J - 0.9999999999999999
" a6 M. r6 P2 o5 ?2 f2 i+ {8 } - >>> math.tan(math.pi/6)& F$ g( e/ P3 K+ Q. ^8 x2 j: r
- 0.5773502691896257
5 j, }* V) U0 {* L - >>> math.tan(math.pi/3)6 b" @7 C; p; ~
- 1.7320508075688767
复制代码 ; L1 M; V* q' o
math.degrees(x) 把x从弧度转换成角度: }! X: o8 z; s, O: H5 h6 z6 q
- #把x从弧度转换成角度
. ?7 O0 M `0 X; |' p - degrees(x)5 K. g j' P7 w* l
- Convert angle x from radians to degrees.
$ d4 q2 E& {# f/ j$ R0 w8 W" {
& C; W8 Y, d+ O7 K0 H9 N( g- >>> math.degrees(math.pi/4)
- H( r6 o7 i% L - 45.07 e8 e; k( R9 S; v
- >>> math.degrees(math.pi)
8 O4 V% d3 N8 u2 L: N' G - 180.0, W0 V) `. {/ I
- >>> math.degrees(math.pi/6)- Y% a$ e3 |3 C
- 29.999999999999996" ]: ^6 c( ]% U3 A' ~8 M
- >>> math.degrees(math.pi/3)
$ h8 g* Q ^8 a8 Q9 q - 59.99999999999999
复制代码
" O: F7 X7 [: E1 A+ L9 {math.radians(x) 把角度x转换成弧度
0 r+ H$ |) w6 J$ T5 |/ @4 _, B0 {- #把角度x转换成弧度
' o" h1 X+ [1 H& N. |: } - radians(x)
- H7 K, q, n! w - Convert angle x from degrees to radians.
) G8 T* l' W- A - >>> math.radians(45)
0 L5 B# E3 z* v: r& Y - 0.7853981633974483$ n! ~! _* N9 ?4 _4 j! w
- >>> math.radians(60)
6 x, |8 A; Z9 d3 W0 ^' J# R - 1.0471975511965976
复制代码
1 J- X! S2 S0 S X: \/ imath.copysign(x,y) 把y的正负号加到x前面,可以使用0
2 M' P7 ^, w) t: F- #把y的正负号加到x前面,可以使用0 S5 X: U: g$ G* `: h* e, t
- copysign(x, y)
7 } J, M. R) y; S2 Y; K, H! H - Return a float with the magnitude (absolute value) of x but the sign . z- H5 V# g9 c9 ?" A: r8 _* `# p5 `
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 Y9 G0 p' q2 U$ S0 w - returns -1.0.
! @( G4 G. ^- z7 h - 4 q3 R2 |, ]8 B4 y. M
- >>> math.copysign(2,3)$ G! ]: u' j, Z0 L/ d* T3 x& M
- 2.0
, Y3 c: h* u9 v! `: L - >>> math.copysign(2,-3)3 t/ ^( n" I k& Y" X
- -2.0& p% R! Y1 B0 L
- >>> math.copysign(3,8)
( I. j8 A! t% E& M. z/ O - 3.03 V( j% t) Y. L5 Y9 I
- >>> math.copysign(3,-8)
4 m' N$ p* I1 b$ ^9 K' E1 v - -3.0
复制代码 6 Q. U% A9 b# M" M( i- l$ a
math.exp(x) 返回math.e,也就是2.71828的x次方
6 `/ ]; U4 S& }- #返回math.e,也就是2.71828的x次方
5 L" X' ?& V. B& ? - exp(x)
* o' ?, n/ d1 _4 z' m! ` - Return e raised to the power of x.
6 F" ^: d) a) S& {; z
) u3 n4 h/ W, |$ P/ }. e- >>> math.exp(1)5 _% w# D2 x' l! `) `6 }9 x
- 2.718281828459045
3 c; }# m+ s* F& t; Q: V3 \. T: F - >>> math.exp(2)/ k2 K* U6 `" L
- 7.38905609893065
+ b3 O* n) w5 K1 c - >>> math.exp(3)4 x V/ Q6 x+ v0 a7 ]
- 20.085536923187668
复制代码 4 C! I( l; E9 w, F
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减11 H' H _: f# G+ }+ B9 g
- #返回math.e的x(其值为2.71828)次方的值减1/ D: _4 ?) t( G
- expm1(x)
- H4 u9 v# ?$ S8 N1 @7 ]2 q m; M - Return exp(x)-1.
" j% z" V% E* f }3 |4 e# z% U1 m7 ] - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
. w$ K3 e+ |: c9 @
: F h: B4 R8 X5 K; N. p- >>> math.expm1(1)
+ E L7 O. g. r2 k8 F& J- Y& x - 1.718281828459045
9 z- V( M: I+ O- }9 G! ] - >>> math.expm1(2)5 B3 Z1 H3 R4 q2 d3 z
- 6.38905609893065, _1 F( }3 B; g$ P" v0 y& F0 i
- >>> math.expm1(3)* i* L, w9 C) B" u" R
- 19.085536923187668
复制代码
O3 \1 F ^; O' p( imath.fabs(x) 返回x的绝对值
6 x( f$ d/ E7 w# ^+ H- #返回x的绝对值
5 t8 I' ], x" p9 o: D8 _ - fabs(x)
* z, K+ }5 R3 G7 m/ B - Return the absolute value of the float x.
5 O! K) _8 M& _# ?: F - . v+ w- W3 W3 H' C
- >>> math.fabs(-0.003)! G2 q9 P% D/ Y5 A/ h6 L) M6 v
- 0.003
+ `* _8 l" O* F$ u% r' b/ I - >>> math.fabs(-110): p: i% |. |1 T
- 110.0
% B$ H/ s4 n4 T; g" ~4 h+ a0 o* f - >>> math.fabs(100)4 S% X& r/ G1 @3 c ^
- 100.0
复制代码
) N3 ~4 N' S. pmath.factorial(x) 取x的阶乘的值
; b- y" _& Q6 S& B6 M. j# f1 i7 O- #取x的阶乘的值
5 C8 N6 L/ p7 r& F8 n - factorial(x) -> Integral
& [, d0 Y2 f$ M k( ] - Find x!. Raise a ValueError if x is negative or non-integral.# L8 R4 r" k' g: H8 G5 Q" v
- >>> math.factorial(1)6 Q6 S0 X" h6 q4 Y
- 17 C$ I" U3 z* {. l. k. q1 m
- >>> math.factorial(2)+ o6 V% v/ x, s7 W# B( Y
- 28 P) u4 B1 X: Z, h. Q( B# e
- >>> math.factorial(3)
3 M. U" B5 y! W4 H; f i - 6
/ s: f( |6 ^" `6 C - >>> math.factorial(5)& B6 F3 ~: X' |$ h
- 120
1 p. U. U) ^" ?) ` - >>> math.factorial(10)
, T2 M* i z. [; X6 [ - 3628800
复制代码 1 l, @1 i* S" x; P- U! {" K
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
3 u- q$ n* E. B3 ^7 j$ I- #得到x/y的余数,其值是一个浮点数4 \. K* f: V5 Z
- fmod(x, y)# t `- J3 i, _5 g7 b
- Return fmod(x, y), according to platform C. x % y may differ.3 B. D V4 l5 j1 V# X5 I+ m6 ?. I: l- M( m
- >>> math.fmod(20,3)
2 m5 {: K+ l' c9 v- G; z5 c5 q: J - 2.05 m: S! g! T) ~9 H
- >>> math.fmod(20,7)
3 V# r( T( Q2 S! V - 6.0
复制代码
2 V5 V" m1 N% _. jmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 J: V4 f) d* R
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* E; X- Q* z7 u+ s0 ]9 s
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
" e# ~+ F) P4 n - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
& U# |: u6 \7 s0 ^ s - frexp(x), \* s1 `$ I: @# i! v
- Return the mantissa and exponent of x, as pair (m, e).
8 p$ E# i2 C; E6 |2 d [( W- | - m is a float and e is an int, such that x = m * 2.**e. D/ `0 T; K n& ]. Q
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.0 n, k; r" U2 d
- >>> math.frexp(10)& w/ @0 m8 X1 q0 Q# n" p/ [1 Q: S
- (0.625, 4)
" `8 @& y7 b- Y - >>> math.frexp(75)+ T, z1 b# F( A: Y1 E; d+ w
- (0.5859375, 7)
/ Y) R+ _$ V4 Q4 ^ - >>> math.frexp(-40)3 g5 e- p. q7 u/ d4 N+ M9 r
- (-0.625, 6)
8 Y ~# ~8 d' z) a - >>> math.frexp(-100)
* {: Z" b! D, Y1 E6 R% J! l9 I4 U - (-0.78125, 7)7 z! N; P2 A+ \" n3 S- O! O: q& `
- >>> math.frexp(100)+ P) W4 z' G0 n1 D8 d9 a
- (0.78125, 7)
复制代码 " t* M, |- A- T0 w) e. A
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)4 p: u) ]( x9 Q& a6 i( ?% g `" |
- #对迭代器里的每个元素进行求和操作
* K" Z0 [' C: u - fsum(iterable): D9 y1 q) z, }2 p
- Return an accurate floating point sum of values in the iterable.& {* F! W: ] S1 D. r& k
- Assumes IEEE-754 floating point arithmetic.' J" L% O8 o# |' x, }1 }8 A9 V5 F
- >>> math.fsum([1,2,3,4]), G/ n1 q u3 r8 A1 K
- 10.0
* D5 S4 s$ a( b# r0 ~3 o) x - >>> math.fsum((1,2,3,4))
' ]/ x! `2 Q/ v9 g - 10.09 ^; G, o5 G R- r4 L4 p7 R
- >>> math.fsum((-1,-2,-3,-4))+ Q% d9 F( u* T L. q, ]
- -10.0% d& m. w5 w) j; w
- >>> math.fsum([-1,-2,-3,-4])
1 X# f- O5 V- F+ O - -10.0
复制代码
1 a* J3 e- K# d* Z$ jmath.gcd(x,y) 返回x和y的最大公约数) F. N x4 ^* f1 y% g$ e- g; }6 n8 s! X
- #返回x和y的最大公约数. \! m% P4 G: K4 ?8 E9 X' {
- gcd(x, y) -> int3 \4 }8 X- q& m0 c" n/ r
- greatest common divisor of x and y7 @$ j' J% g: n7 R1 i. d# I& _
- >>> math.gcd(8,6)
# [" c8 Z9 o( h! k - 2
' @$ W, [/ N; Y! B# C; }# m+ [ - >>> math.gcd(40,20)$ y( t7 |" I, o8 N% ~: j9 `
- 20
$ K$ j) l! V- H' D7 D( Z- i- E7 M* m" p - >>> math.gcd(8,12), i/ L6 l7 f; {* j
- 4
复制代码 8 {7 n _8 b3 A1 l; e
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
' `3 w) Y% u, u6 U C' u! F1 e- #得到(x**2+y**2),平方的值5 G+ [. I6 u! ]
- hypot(x, y)3 ~" H) X# `$ O
- Return the Euclidean distance, sqrt(x*x + y*y).
|1 O0 ^3 ]- D - >>> math.hypot(3,4)+ z4 D6 D. I' a
- 5.0/ M+ y. U0 |( o, W& k$ U$ w
- >>> math.hypot(6,8)
4 Z: N( M1 C8 k0 x/ G - 10.0
复制代码 7 n: a3 u# Q& a f
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 Z8 K. F, X; l! G* E
- #如果x是不是无穷大的数字,则返回True,否则返回False
% S) _1 g; A* Y# ~! J - isfinite(x) -> bool" i: T7 ]. F. l$ Z) z
- Return True if x is neither an infinity nor a NaN, and False otherwise.9 }0 r$ A' m6 k7 u
- >>> math.isfinite(100)
. x+ c& K" w% _: z, I( m# v( } - True8 I# |8 F! j, b# O
- >>> math.isfinite(0)$ _: x! b6 H( W$ p4 Z
- True
O, S; w- V7 p7 M% M - >>> math.isfinite(0.1)6 b' _" {; k2 a/ W" }
- True8 l6 O6 c7 @) g
- >>> math.isfinite("a")* c; i1 d5 s% X: Z
- >>> math.isfinite(0.0001)
f q2 S% }2 x/ D2 K - True
复制代码 + u2 O5 d% S8 k
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
; \; U' p9 {% k7 J- A- #如果x是正无穷大或负无穷大,则返回True,否则返回False
/ D4 h4 A& j* c* q. L. q0 M - isinf(x) -> bool
- y) a% o$ |# | ? - Return True if x is a positive or negative infinity, and False otherwise.5 p/ d7 p! E$ B
- >>> math.isinf(234)
6 @* g U' ~* H' [, e) Q - False
, w; R3 S4 Y0 i0 M/ t2 J$ ^9 {4 a - >>> math.isinf(0.1)
- U2 K5 q8 X3 s. e/ [ - False
复制代码 7 ]6 f7 q" e; |* v
math.isnan(x) 如果x不是数字True,否则返回False7 ^/ r+ A; K) Z3 h% z0 e: L- e3 u, z, a
- #如果x不是数字True,否则返回False
: l' B' {! m1 H% x - isnan(x) -> bool M- I# d# Z k) ]$ A
- Return True if x is a NaN (not a number), and False otherwise.; ?5 d& E/ h. f+ s9 @( U- X
- >>> math.isnan(23)
9 O6 n3 B- T- n3 h* ? - False! u. q5 A5 g. y" ^2 W0 {5 O+ R
- >>> math.isnan(0.01)
6 e/ e y7 l) i( c - False
复制代码 0 N, D/ R2 b4 b7 t9 X. P
math.ldexp(x,i) 返回x*(2**i)的值) e6 X& C: q5 z& f) ~; O; B2 F, U
- #返回x*(2**i)的值
% J0 |" Q* w* y4 S7 n6 b - ldexp(x, i)
; d- v: Q3 V1 E0 S6 C - Return x * (2**i).5 Z: h8 W5 q7 Q; \3 X, K: S
- >>> math.ldexp(5,5)
, W1 J" p$ M/ r" @% t - 160.0- G3 ~4 S9 o' r: x4 Z! _0 t+ R
- >>> math.ldexp(3,5)- m. Y* C8 D5 d2 k" v& B
- 96.0
复制代码 8 A4 L8 a, Z' Y
math.log10(x) 返回x的以10为底的对数7 | {7 n7 k& t; L
- #返回x的以10为底的对数
9 \; N2 \& O( Y - log10(x)
$ [1 o2 T- J$ {1 x0 T/ t) H - Return the base 10 logarithm of x.2 w2 F0 k; v8 \; h" l' L/ ^' |
- >>> math.log10(10)
1 v7 o7 r5 N: u6 u M6 e7 K - 1.0% w0 V p. _; L' Z+ X8 r
- >>> math.log10(100) W3 T$ L+ B" t0 V/ o8 y
- 2.0
( [' v9 @7 \. p( i - #即10的1.3次方的结果为20* C$ s8 D' {+ z+ J
- >>> math.log10(20)! T7 g0 v6 R9 P8 b
- 1.3010299956639813
复制代码
! r7 A) H9 m+ K3 l* O( _, Bmath.log1p(x) 返回x+1的自然对数(基数为e)的值
& e& p& s7 X$ U2 F% Q2 m2 F# R& p# A" R- }- #返回x+1的自然对数(基数为e)的值
0 j' }2 F3 h( d) R - log1p(x)
7 } M7 R. K1 |) J5 z: K, d2 a3 M - Return the natural logarithm of 1+x (base e).' A8 Y1 }! q9 W7 S; J! \7 z, s
- The result is computed in a way which is accurate for x near zero.9 {; i+ V; t" j" K0 |+ O! Z
- >>> math.log(10)
: Q- [$ O" X4 w9 f- |* p - 2.302585092994046
" Y/ A! n' J" k- n - >>> math.log1p(10)+ R/ n3 H4 N8 w
- 2.39789527279837077 f5 h/ m$ L$ f( T. z2 j
- >>> math.log(11)% ?0 `2 B, X3 g9 M9 M
- 2.3978952727983707
复制代码
$ l. ]0 |" T6 [8 Y: mmath.log2(x) 返回x的基2对数
/ Y0 M0 H* n1 D/ d- T7 o- #返回x的基2对数" N8 D6 f$ e8 a1 Z( d# Z
- log2(x)3 n, e1 f, p' U" T ?" h; ?6 g4 K
- Return the base 2 logarithm of x.
4 L5 ~! w% h4 V! j# I# m - >>> math.log2(32)# }7 V& L6 m) N" h! f6 V9 Q+ k
- 5.0; |4 f4 h7 y8 Y* r. ^' s, W
- >>> math.log2(20)/ u9 E& c) J9 S+ K6 [; w
- 4.321928094887363% y7 S9 R$ U8 O* ?7 ]# s
- >>> math.log2(16)
2 p9 J0 }' f& c# z$ }1 m4 w2 ~ q0 M - 4.0
复制代码
7 ~4 S3 R. b/ W" {& @ pmath.modf(x) 返回由x的小数部分和整数部分组成的元组4 b9 q8 c' A$ p3 o* K* B
- #返回由x的小数部分和整数部分组成的元组5 ]- l t% ^& G6 P M9 _
- modf(x)
9 p- }6 B0 _; M9 O: a( v8 N - Return the fractional and integer parts of x. Both results carry the sign& [+ x1 D% T. x; e7 H; E
- of x and are floats.
7 K6 a) _2 G4 D$ Q! g4 L - >>> math.modf(math.pi)
- {& V6 I% z" l% E" D6 P: M9 a - (0.14159265358979312, 3.0)/ [2 c" B6 G% }* @4 }3 ^
- >>> math.modf(12.34)
; {9 g5 d* j! W: B' d2 L - (0.33999999999999986, 12.0)
复制代码 " i2 J1 I+ @- b+ v
math.sqrt(x) 求x的平方根
% j, N6 C: ], D5 y8 f- #求x的平方根
' ]. Z: v- S# j% G4 a5 J+ r3 M - sqrt(x). P: x' R* [) P/ t0 p
- Return the square root of x.7 `0 ?! ]. _, P4 U. N
- >>> math.sqrt(100)+ E; {5 o4 S4 U r5 b* T: R
- 10.0: r- N$ T) N) T8 q5 Q2 S
- >>> math.sqrt(16)6 S* o& o* _1 `, j# V% g- }3 J
- 4.0
2 D" ?- ?) v* R; P1 _) C - >>> math.sqrt(20)
8 n; {) K1 e0 D: a: u - 4.47213595499958
复制代码 $ w! W# v/ a5 [9 ?
math.trunc(x) 返回x的整数部分- #返回x的整数部分, }; T9 H# J. e2 b( w' Z2 x6 j
- trunc(x:Real) -> Integral7 V7 K o7 ^" ?
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.- r2 n5 \$ ~% w% G! T2 B6 x& m
- >>> math.trunc(6.789)
# D& a3 y' F' R - 6' Q$ U" z6 ?: q3 A7 I% v
- >>> math.trunc(math.pi)
# |3 G+ T1 ?/ ^ - 3- C# C( O9 \5 J" I- p0 X
- >>> math.trunc(2.567)
, ]7 f! v R1 ^9 l - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|