马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* }* e7 c6 D# L7 H3 P, t【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' c9 i. q8 q0 S# \8 |& u
1 r0 M$ Q G p. `7 ?方法1:+ U, `) a6 |# U# w6 j% H. M
- >>> import math2 d) Z4 ?. ], R# P, }
- >>> math.sqrt(9) x p0 l4 u& y
- 3.0
复制代码 方法2:
2 A# {) W$ m- {$ o- Z4 K( j- >>> from math import sqrt1 w1 e0 s f: n# f$ s) _: {
- >>> sqrt(9)
9 }4 ?( y. A d; h - 3.0
复制代码 $ i0 {2 e$ Z( O: n
( ]; b. F; z' S% Y5 H! G8 J
math.e 表示一个常量( g( r4 f9 R0 V
- #表示一个常量
9 E) {% D/ `" _+ k6 w, c+ m - >>> math.e
1 t3 n: e# ?* P4 V7 C1 s# | - 2.718281828459045
复制代码
5 J# ~" K% z o. i% rmath.pi 数字常量,圆周率! ^1 O' T6 @( x c2 E
- #数字常量,圆周率
" V# Z5 w: W: ^5 m( \: | - >>> print(math.pi)
3 y( L4 M7 [) M# i - 3.141592653589793
复制代码
, v; O; \" R6 L/ Z1 h4 A+ gmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x) d- K$ O2 @- E$ p- P
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 \6 ?, x3 E! L9 P1 @$ D' ?# g+ a - ceil(x)
7 E9 }0 J! i& W, s8 a( T6 K - Return the ceiling of x as an int.
8 R4 j* J9 W& q# i7 h( C - This is the smallest integral value >= x.
" Q% M) S6 Z6 a1 y6 r" H - R% g0 k. I: P7 ` [' ?/ O
- >>> math.ceil(4.01)
; x, l7 ~ K, k. k& Y; M! f/ P - 5
" k9 y; m" u# p& e; T; H - >>> math.ceil(4.99)
5 S4 M5 S, j6 v! }8 X8 A - 5
0 n0 c# }0 `# q4 ?7 u - >>> math.ceil(-3.99)& a A3 N. r) o) ]' E
- -3
) e: V z3 v H( h7 p0 F# o - >>> math.ceil(-3.01)+ I: S% l. c% K+ F. ~0 P
- -3
复制代码 P i3 L7 u w, _. ^: W9 ?
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 C2 _- j% Y+ D# Y- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 ]. t5 z; u2 n - floor(x)
) n* ] r/ D- d* I - Return the floor of x as an int.* i/ b- y0 n4 y. R/ O" N: R5 g; Z1 M
- This is the largest integral value <= x.8 `2 U F3 Z0 Y& h! N2 v! P0 y, z
- >>> math.floor(4.1)7 X" s" [ N3 g( o. W7 j8 F# T
- 46 k" o/ P* H2 r+ z8 U: Y
- >>> math.floor(4.999)
* i6 C3 I& t7 C' y( A. S - 4. L. L5 d" G4 ~9 W- w
- >>> math.floor(-4.999)
% q% I: {- g3 G0 l - -52 u8 t/ \+ m' j2 S
- >>> math.floor(-4.01)% f7 b- e4 s' V3 G! |! p) U# K
- -5
复制代码
: R3 z8 I* I. R$ d' \0 P) h K! {math.pow(x,y) 返回x的y次方,即x**y* Y0 m) Y7 j* {( s) |
- #返回x的y次方,即x**y
4 R! k/ u: J2 t# a8 u - pow(x, y)
: a' ?- p2 L7 w! T - Return x**y (x to the power of y).$ k% e8 N0 Y8 _% ^7 D& E
- >>> math.pow(3,4)6 L/ u# G, t9 I
- 81.0, s! X( @5 J9 H! Z, I o
- >>> 8 R: o$ y' @1 x. m/ `) F# h
- >>> math.pow(2,7)5 h' s' u: O4 N/ L! I
- 128.0
复制代码
6 h. E8 V6 b0 S9 P: cmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! T' O Y8 F2 U9 C/ w: w2 Z( @- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ V1 { w; M" C. C! P - log(x[, base])0 C) j; R4 x" |+ v# |
- Return the logarithm of x to the given base.
/ D. Y+ }! J: X - If the base not specified, returns the natural logarithm (base e) of x.
" s }6 _$ [8 l2 K( s1 |1 [ - >>> math.log(10)8 ~5 Q$ @+ A% x
- 2.302585092994046
' e) h: f& |2 [0 r f5 q. @" R% u - >>> math.log(11)
' y2 m1 X% |# r+ D2 c$ ^1 K$ U& R1 E - 2.3978952727983707* d6 z' j+ {% g& A
- >>> math.log(20)3 R [$ x/ w7 Q5 h4 r: F9 k: [
- 2.995732273553991
复制代码 3 o; Z# {" b- R
math.sin(x) 求x(x为弧度)的正弦值
+ r! Z/ |# q+ o) {- #求x(x为弧度)的正弦值+ ~$ \; G' O( w7 y( E; H' |
- sin(x)8 A0 [0 d# p% j
- Return the sine of x (measured in radians).9 ?3 U; \- k9 V7 q$ e0 A( D( v: X
- >>> math.sin(math.pi/4)1 a. |! k, u' x- T1 f* W$ n. K3 ^
- 0.7071067811865475& F' s% H- `8 k* s
- >>> math.sin(math.pi/2)0 o/ T. T' x* Y' y/ s& x
- 1.0 m8 O2 [8 l0 j9 w3 ?% K0 J
- >>> math.sin(math.pi/3) c2 y0 W) |% Y! u
- 0.8660254037844386
复制代码 : B' o0 b; A2 p; K: R- J( U
math.cos(x) 求x的余弦,x必须是弧度3 U+ v) p1 x( m$ t7 W' Y$ I
- #求x的余弦,x必须是弧度 Q9 z; V" x! Y$ d, d3 t
- cos(x)
6 h+ L$ ^* {! f- l7 B - Return the cosine of x (measured in radians).
' o X+ G9 @3 t2 Q$ n- _ j) d - #math.pi/4表示弧度,转换成角度为45度2 }8 ?% j8 w0 m- F; x
- >>> math.cos(math.pi/4)
' u/ f; G0 D. X+ _ - 0.70710678118654761 ]: d0 Y5 Q) t( d# l
- math.pi/3表示弧度,转换成角度为60度) n6 |2 R. d' U+ d8 m d% F( d
- >>> math.cos(math.pi/3)
0 L, l( E/ E$ P' P$ k4 g - 0.50000000000000017 |/ k! P N H ]% L
- math.pi/6表示弧度,转换成角度为30度% }+ U0 I: E- z( S
- >>> math.cos(math.pi/6)
; I4 [5 N) F% u/ r% ^4 L( i. q5 N/ f - 0.8660254037844387
复制代码
3 g6 A( W( a, Z5 K4 B9 vmath.tan(x) 返回x(x为弧度)的正切值3 e% E$ V; ^& s( r
- #返回x(x为弧度)的正切值 a! Z% D& a! z/ r# @, X" O# B
- tan(x)
& B9 Y( y5 \. X% d4 h" m - Return the tangent of x (measured in radians).
! L9 G: U0 H( @: B - >>> math.tan(math.pi/4)
( C# q$ \% P. @2 } - 0.9999999999999999
( t- M) {0 b* t. W4 B - >>> math.tan(math.pi/6) S3 j3 Y/ H7 Y- D( @# p* H0 _3 r5 n
- 0.57735026918962575 Z9 o! H: U1 x9 y. Z- {1 R
- >>> math.tan(math.pi/3)
2 u$ C+ m3 J+ |) A) ]* t - 1.7320508075688767
复制代码
$ r/ N5 ], e# c- V. I! X$ Imath.degrees(x) 把x从弧度转换成角度
: C/ }$ u! z; n) H# T9 s @- #把x从弧度转换成角度4 i/ n* \3 a6 I' B! S- [: `
- degrees(x)
5 l! X3 f. M0 g; X& O - Convert angle x from radians to degrees.
, J+ f: p! N: H - & e, Y$ Y4 K, E9 m: {
- >>> math.degrees(math.pi/4)
! e8 q' Z, F6 u3 O - 45.0
+ i; C; @- N! E - >>> math.degrees(math.pi)
: D* Y/ Y; X2 T& ?+ W - 180.0
, d* L& r- V" b9 N - >>> math.degrees(math.pi/6)
. Y0 z5 N% r. T+ h8 _ - 29.999999999999996" x) d' i" u, c) l) x
- >>> math.degrees(math.pi/3)
! C1 S$ u1 `& T0 p - 59.99999999999999
复制代码
# \1 V: |, u7 X) O: h6 kmath.radians(x) 把角度x转换成弧度) d$ _) J M: k4 |7 K4 q. x- [+ o
- #把角度x转换成弧度
- N$ E- E$ t4 S/ E2 S1 @! t1 p - radians(x) t6 p R* o, F: y& I8 x
- Convert angle x from degrees to radians.: S% b+ K5 ~1 d+ A. k$ g
- >>> math.radians(45)( m8 m- c( M4 K( N X1 o# J
- 0.7853981633974483! g, s4 X" y: j
- >>> math.radians(60): y- _& {7 E, w; V5 A7 g3 g* D( H
- 1.0471975511965976
复制代码 c$ z0 u7 ]0 o" @( a: n+ g
math.copysign(x,y) 把y的正负号加到x前面,可以使用03 s8 ~& y! ]8 h/ f6 n2 x; X
- #把y的正负号加到x前面,可以使用07 \5 A5 C" Q0 T3 m' T" w( d
- copysign(x, y)8 u/ X' d" M8 h; P! \, p
- Return a float with the magnitude (absolute value) of x but the sign 7 p, F% H9 l7 x$ }8 [( @) q
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( G/ W3 ^8 \) @& {
- returns -1.0.
5 W- I0 }; a2 _
! i, e; n* n" W+ K3 e5 U- >>> math.copysign(2,3)4 W) c7 c# V9 ]% b
- 2.0) M/ X* H/ \. a
- >>> math.copysign(2,-3)$ R7 u9 `( d7 `
- -2.0/ U& _% p3 F }
- >>> math.copysign(3,8)
- {. R; X4 M3 P' G6 V R7 s) W* Z - 3.0
8 _1 w% b4 d7 X0 g4 M! }6 M - >>> math.copysign(3,-8) e$ N: u' s) p$ S( s
- -3.0
复制代码
9 c" D* T- H! H0 @ Smath.exp(x) 返回math.e,也就是2.71828的x次方- @5 l) T. H9 S& Y5 |! q& V+ I9 K
- #返回math.e,也就是2.71828的x次方% V( W# P" [/ q
- exp(x), } X' ^. a) | @* \3 Q
- Return e raised to the power of x.
+ Q. v: ]. g& j1 y2 T: e9 Q1 n6 C. { K2 e' W
; N5 U4 e+ d( e9 L. X- >>> math.exp(1)
2 f3 i$ h8 \5 Q: S" p - 2.7182818284590458 h4 r, f7 ]' B. n9 y! j
- >>> math.exp(2)) Y( a) s e5 q8 C
- 7.38905609893065
+ Q+ C. i ?' K; o& q - >>> math.exp(3)
( \: o' n2 T, V - 20.085536923187668
复制代码
8 f2 _7 z* Q) x5 L1 B* g0 a/ w) F5 Fmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1" E' Y% J8 q4 n* e' U( F) ]
- #返回math.e的x(其值为2.71828)次方的值减1
, w# ^7 U( \/ V9 ]) x* l - expm1(x); _3 C$ M4 T! v d5 O, @, a3 W
- Return exp(x)-1.
) ]# T; q4 y1 Z7 a' ^ - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
; w I: d; I: r2 v$ }* r% N# Y
! d* _* Q1 U3 W0 B- @1 c- >>> math.expm1(1)) l4 O7 B, g' K7 Z5 I
- 1.718281828459045
/ w0 O- _# P, u2 h0 g) ~" i - >>> math.expm1(2)
, O ~- z% f' I. a& J1 ^4 q% z - 6.38905609893065
; `4 o6 G. x3 U* i - >>> math.expm1(3)5 v( Y+ H$ V% v7 {% P7 f
- 19.085536923187668
复制代码 . l! R6 G" N4 ^) v0 y; \, I
math.fabs(x) 返回x的绝对值4 K/ e5 E" O0 l9 ?
- #返回x的绝对值
0 m% C- Y, M7 d# o - fabs(x)6 j, u7 N# }5 w$ _5 I6 \; \/ v4 \- d
- Return the absolute value of the float x.
* x) g3 ?# k) X U& {$ w - 6 H; ~' b/ h5 ^' ~" \8 e2 y' ~
- >>> math.fabs(-0.003) \- f- x: Q" ]4 F) W4 t$ _6 o: @
- 0.003' k! E3 ^, |0 H
- >>> math.fabs(-110)
" A. v" E7 t+ ^6 C - 110.0
d0 U; O, }2 `6 T+ u - >>> math.fabs(100)
' s* [, G: o# @& b7 \3 I! Q - 100.0
复制代码 . ^8 s! ~2 V; G: `7 [+ w
math.factorial(x) 取x的阶乘的值& T1 Y' F4 |1 u7 R# n1 _. N
- #取x的阶乘的值8 A, @( V9 F" L
- factorial(x) -> Integral
# _7 q% ~$ }0 V( n& \ f - Find x!. Raise a ValueError if x is negative or non-integral.4 u" |: |0 ^& G M- |1 o( }
- >>> math.factorial(1)5 Y" x/ p$ x6 D8 N
- 1
" G. F, F9 y7 S - >>> math.factorial(2)9 [( B9 r1 \3 _3 O
- 2
- f o. o. U& t' Q; d - >>> math.factorial(3)2 X% h% i# h' x2 f- p$ l7 z' e
- 6
- E2 t# ~! M0 [" h9 j* n- T6 B$ } - >>> math.factorial(5)
* t& a; N f. Q: G, E$ w - 120
9 P s* n! n; r" I - >>> math.factorial(10)
5 x z+ E+ y/ N% _7 d( C - 3628800
复制代码
5 M; p2 L* [# R; D0 Umath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
8 p n8 @5 x" \7 H9 Y- #得到x/y的余数,其值是一个浮点数
. i+ W$ d+ |& f, [4 ` - fmod(x, y)
* u5 h6 W- ^$ G) p - Return fmod(x, y), according to platform C. x % y may differ.
7 K+ `! r2 ^' @& {1 D0 x' w( { - >>> math.fmod(20,3)
+ A" F) g1 J9 R0 F* e# ?; @' f - 2.0
! i" i. x. B5 u' W - >>> math.fmod(20,7)
! o9 h) g% z# b- a" C& B/ P' G$ ? - 6.0
复制代码 . I. ~- r& e2 n( e( I% ?% q
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
) T* B# |+ t. P# X$ c- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
3 p1 B2 C; Y2 }2 b+ M8 l& q* R% x - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ y B/ @) c2 P _4 f
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ | e. u |8 Q( V6 q
- frexp(x)$ n! o& M4 [+ \
- Return the mantissa and exponent of x, as pair (m, e).
$ E, S$ H! j6 M7 J. o# K- T - m is a float and e is an int, such that x = m * 2.**e.
/ L5 q6 m$ j2 `" P+ \8 ?9 y - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 z0 F* `5 _6 w - >>> math.frexp(10)% o7 x& o# @: `
- (0.625, 4)
, T, k; _6 {& x0 z/ A H - >>> math.frexp(75)
% p9 E- u& d8 W+ e/ u! L* ~" S - (0.5859375, 7)& t( a# Q5 V6 ~0 j) E
- >>> math.frexp(-40)
5 N7 u8 m% Z. b5 S [ - (-0.625, 6)
# R* W8 {% @7 l9 S) O - >>> math.frexp(-100)
' w3 i6 n; k; T( @7 l - (-0.78125, 7)) c" v* R: {) X$ e0 X
- >>> math.frexp(100)0 X0 o4 \9 u8 T4 U" s# S
- (0.78125, 7)
复制代码
3 W3 ~6 Y5 e- _math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
5 @- n e) W& q o6 o+ k- #对迭代器里的每个元素进行求和操作
4 d+ K- S' u. n: A - fsum(iterable)7 v+ T; q+ G' _' l2 ?3 i' ]
- Return an accurate floating point sum of values in the iterable./ n+ T% Z" y/ _
- Assumes IEEE-754 floating point arithmetic.
8 w% [/ i3 `: o. k& O/ g - >>> math.fsum([1,2,3,4])
. ~) F; f& q& g4 h - 10.0
1 [: x4 z* r8 _9 m - >>> math.fsum((1,2,3,4))% C& A3 M1 E/ t0 v% m9 Y
- 10.0
! B: a$ t r6 z V: o - >>> math.fsum((-1,-2,-3,-4))4 q# X% i1 `' H q" j
- -10.0# h) N6 c. k% {. C8 M/ C$ C. ?7 U
- >>> math.fsum([-1,-2,-3,-4])
; H5 g- }( g" m& p3 Y - -10.0
复制代码
: S9 l9 E" y! ]* Q1 Gmath.gcd(x,y) 返回x和y的最大公约数( q. G; t" [, F4 q7 a8 A
- #返回x和y的最大公约数" x' U, W# g% m" x5 S9 w
- gcd(x, y) -> int
2 [+ L) y& H6 M8 y - greatest common divisor of x and y
5 E( _9 z" L* {# }6 z - >>> math.gcd(8,6)( G, ~* k" L/ Z: T
- 29 j4 g5 y0 {. c& e
- >>> math.gcd(40,20)" }: F) m. |2 T7 {7 e
- 20
$ H9 ?( v* q2 G& @ - >>> math.gcd(8,12)# a, u- @* J$ f1 `7 I( \
- 4
复制代码 . D3 m8 }5 z7 Q; n2 w& v% x0 ?! Z
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False1 m% y( R& T9 L, R" _( |- _1 ^+ r
- #得到(x**2+y**2),平方的值
4 S# C% Q8 c8 i8 H - hypot(x, y)
* }4 `* a0 s( V" V - Return the Euclidean distance, sqrt(x*x + y*y).
" G8 N7 D. \% h6 B+ c - >>> math.hypot(3,4)
+ O! h- g; y) {* M: F; p0 U - 5.0
/ r! A; ~0 t1 L' {4 _# h1 M - >>> math.hypot(6,8); a9 |4 n. ]2 p5 r
- 10.0
复制代码
& s' S' A' _9 _. T0 Tmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
. s) g- g4 v0 M- #如果x是不是无穷大的数字,则返回True,否则返回False/ g, p5 k' _! F: }: u
- isfinite(x) -> bool! w! N4 q" y1 E
- Return True if x is neither an infinity nor a NaN, and False otherwise.
Q2 @+ M9 \1 e4 P6 `3 q! p% H - >>> math.isfinite(100)8 f( h& a- {2 v! }, g
- True
2 \4 \! w R6 s; v - >>> math.isfinite(0)
( F* R$ l# T* _ l* k1 x - True5 \) L e' b8 h7 b$ C
- >>> math.isfinite(0.1)
: g4 b+ K) ` z8 h; ~ - True. c8 @5 }' P+ i4 W
- >>> math.isfinite("a")3 t: n0 |+ q5 @6 } n1 H
- >>> math.isfinite(0.0001)+ P/ V0 S1 z2 L( v5 t
- True
复制代码 $ R( d7 s, u; V: I: ]; h
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False+ y$ p( a# M; T2 j8 ^! f
- #如果x是正无穷大或负无穷大,则返回True,否则返回False- ?3 W' g& Y' H" P: O
- isinf(x) -> bool. ?. c- D' Y/ i: p; @
- Return True if x is a positive or negative infinity, and False otherwise.
' Z6 |- ^4 u( Z3 i$ K& F+ l - >>> math.isinf(234)7 D' E( z1 T8 Z
- False/ K+ O% P! u! ^, K
- >>> math.isinf(0.1)) _8 u0 E% R7 f! w" k
- False
复制代码 1 e$ H4 N& b: F( [2 A: y
math.isnan(x) 如果x不是数字True,否则返回False
$ V9 v* F8 a# G! }6 Y: E- #如果x不是数字True,否则返回False
1 f. [; g# V6 C* Y0 f3 C, H7 t. ~ - isnan(x) -> bool2 K. Q5 ` {4 o l* q
- Return True if x is a NaN (not a number), and False otherwise.
! y% N& [& B% h! W, V - >>> math.isnan(23)
1 { Z$ j) m' Q3 L - False
6 U! X0 l' B. G5 `) B. j& p - >>> math.isnan(0.01)7 K x% n8 s/ x
- False
复制代码
' y- l* d* q6 m! ]) I2 pmath.ldexp(x,i) 返回x*(2**i)的值
, H* a8 r& g' g0 N- #返回x*(2**i)的值3 h# O; y* _8 v; d6 v- E' K9 `" Z
- ldexp(x, i)" @6 W, Y. s& @: X& R0 |5 ]
- Return x * (2**i).$ U* J" n& ~/ K6 l) E3 J/ R* N
- >>> math.ldexp(5,5)
- ~1 F8 V& H6 R% w( {% l# L. C - 160.0
& S1 B- f9 L1 S+ F - >>> math.ldexp(3,5)4 |8 Y2 G& T: r* N2 r9 V
- 96.0
复制代码 7 O# ^% x2 x/ A5 F! e* T5 C
math.log10(x) 返回x的以10为底的对数 P. |: M0 Z* x- G. e
- #返回x的以10为底的对数
' p' c, M0 _+ \! d) B2 E' p - log10(x)$ @: M8 E2 C1 C% h
- Return the base 10 logarithm of x.! l: }1 j" M* S! N7 f* d
- >>> math.log10(10)
@" ^0 J0 P% E# V9 K+ u+ d Z0 t - 1.0
' y* w7 b. D9 i( v6 {2 l0 Q4 L' _ - >>> math.log10(100)
; D5 I, s* Z* m' y& b6 l4 {1 V - 2.0
5 ~& Y, n6 C W2 J - #即10的1.3次方的结果为20, N6 ^& J: ^7 i: r+ V6 U: k
- >>> math.log10(20)
% T; k& i" Q+ \# c7 ]& W9 ~6 n5 K - 1.3010299956639813
复制代码
) W0 a1 Y- P2 wmath.log1p(x) 返回x+1的自然对数(基数为e)的值
2 F" }; M$ M# i, L" d/ F- #返回x+1的自然对数(基数为e)的值+ R3 L* ^0 a$ p E/ u H2 U
- log1p(x), _5 d7 o; H; ^# ]( A
- Return the natural logarithm of 1+x (base e).
6 D1 ~; \* E. m6 k - The result is computed in a way which is accurate for x near zero.
) A) _0 c' d; ?2 @4 ^ D - >>> math.log(10)
9 V+ M( i( ~" U# F - 2.302585092994046
0 r, N; K8 s' R7 j' T7 \$ x+ q$ S: p - >>> math.log1p(10)+ A1 J$ ]! d$ G1 V
- 2.3978952727983707
) c. ]" N6 }, A - >>> math.log(11)- A# X* r3 p( V
- 2.3978952727983707
复制代码 2 N) I# A) a2 ]. X
math.log2(x) 返回x的基2对数
" {. B! g& [" L% w- #返回x的基2对数# M Y5 w c5 v* W
- log2(x)
' u- b# |2 v: i - Return the base 2 logarithm of x.% M4 [9 I0 q3 R, k/ G4 x
- >>> math.log2(32)7 F4 Z2 N$ M" ^( b
- 5.0
8 y e7 E' h; Y. E% W" T& y+ ?* j7 M - >>> math.log2(20)# y7 N; i( \6 X C3 a' c# |
- 4.3219280948873635 ?9 r1 l/ C% j) ?
- >>> math.log2(16), j6 n% R i1 |2 B6 n' N" H
- 4.0
复制代码
1 j8 j2 W' S9 [# S' I$ `math.modf(x) 返回由x的小数部分和整数部分组成的元组* X2 M. Z8 t- C/ _
- #返回由x的小数部分和整数部分组成的元组5 A7 U3 I% Z; a o$ a1 ]
- modf(x)
% M* R6 \. A0 M8 d8 P - Return the fractional and integer parts of x. Both results carry the sign
& f$ x8 w }% W- S6 { g% a; B - of x and are floats.
( ~9 }6 f; x6 L; k# g* L4 D7 i. P0 i - >>> math.modf(math.pi)$ q8 O5 Y3 I6 q( B: }( v, y
- (0.14159265358979312, 3.0)
4 Q" I8 S/ g { z: E( M/ R6 }) ^8 T+ u - >>> math.modf(12.34)# a9 m$ L3 L3 v" u$ H
- (0.33999999999999986, 12.0)
复制代码 . z6 L" `. V/ O& Z
math.sqrt(x) 求x的平方根
7 B* w) s7 _. c( Y- #求x的平方根
8 _$ i3 h% v! t" W, L6 o6 F( B - sqrt(x)1 _6 [- @$ j' @
- Return the square root of x.9 ^, E( r& a7 B8 V, Q
- >>> math.sqrt(100)
) v! v; j @2 R - 10.0+ Q& m* M w/ M- Z) K
- >>> math.sqrt(16)9 I0 ^- I1 k* x7 W8 Z
- 4.0% m( v" @8 J8 I+ Z: Q4 m1 l0 B
- >>> math.sqrt(20)
' F4 F: f/ X8 G5 N1 O* @ - 4.47213595499958
复制代码 / k% d2 f8 Y3 }+ W, n/ B
math.trunc(x) 返回x的整数部分- #返回x的整数部分
, ?0 ]9 i2 M* o1 N( r - trunc(x:Real) -> Integral5 N c' x7 _7 B ?3 |* |2 R5 _
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
( L+ g: C `% H& ] - >>> math.trunc(6.789)
( P! O0 O: j: ~ - 61 D7 a& Q# ]9 ]- d& u% Z Z/ ]
- >>> math.trunc(math.pi) [# ^7 g6 e0 `* D
- 3
% _- r9 e+ O. C - >>> math.trunc(2.567)
3 a/ N' N( h7 F' p! ?; h Y+ L" I - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|