马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ W4 c* L, s9 ~4 z
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
3 O( K# a/ C) ^! f4 O- i
7 P J2 ]6 L6 D3 J5 R" f3 q方法1:& D V* b" D5 C8 r- W+ s$ p Z
- >>> import math) R6 P4 X, x/ ]* h6 ?9 }( @3 _
- >>> math.sqrt(9)% A$ N) i8 ^$ ^& J5 t' m* x
- 3.0
复制代码 方法2:/ @: o0 O& `8 r* T# q, O
- >>> from math import sqrt
, W0 l. Z, [: S1 `: W - >>> sqrt(9)7 g' ]9 y0 I% }9 l" H1 b
- 3.0
复制代码
1 `* J# o+ y& R4 j5 n- S' \
1 _% j7 F# ?* E; B& r$ `& u6 e" Vmath.e 表示一个常量
K, @5 p# D1 L6 [- A6 @; S- #表示一个常量; x& ?" \& _2 }$ G
- >>> math.e
+ O" @, K; k) `/ R: ^) j - 2.718281828459045
复制代码 - Q8 s6 A, E* N E6 k% x& T
math.pi 数字常量,圆周率
$ y2 p3 j3 D! l. u5 K- #数字常量,圆周率5 S5 z# G2 C$ k2 V6 e5 `
- >>> print(math.pi)8 r# W7 H. B+ s
- 3.141592653589793
复制代码
" v! i9 c1 v: Q0 u+ {math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 u5 w1 J7 w' B- P2 P5 I- #取大于等于x的最小的整数值,如果x是一个整数,则返回x& i2 A6 f/ x9 @) K
- ceil(x); u# F6 i& ~9 _" X- Z
- Return the ceiling of x as an int.
. T- o' d o z% O0 O8 a' W - This is the smallest integral value >= x.
7 K1 N# H. f' _
- g4 x8 ?1 N# \# K& L& C/ U- >>> math.ceil(4.01)
- A. W$ X& q; d! P6 T - 5
6 ~# X* h/ Q+ d' @4 d# g - >>> math.ceil(4.99)1 T- H+ b9 @" c. N, J$ V) H3 e
- 5* {+ p: s1 f/ h% A6 r
- >>> math.ceil(-3.99)6 L# x/ U. V8 i. B# H, }/ T
- -3
' r8 @; O# t; ~0 G9 L - >>> math.ceil(-3.01)' N4 A: T% G9 L+ `+ r- p
- -3
复制代码 ) C7 a, S( O! a6 A$ d8 Q
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 H6 [8 e! x9 N- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 A. L# Q% x. K" c - floor(x)
, A, r. X) A5 v* s$ J, j - Return the floor of x as an int.
% S% p% G1 D1 l/ \ - This is the largest integral value <= x.
9 E& f( [+ @3 e1 n7 f9 A$ }2 Q5 } - >>> math.floor(4.1)
. Q% O1 U. L# P - 48 v! t: @+ u: R0 G2 f
- >>> math.floor(4.999)% ?0 g: C9 g6 F" x4 K" @
- 4- k/ Y* i4 {, l I# y2 s! ^; O& v
- >>> math.floor(-4.999)
' Q9 Y6 C2 p, ] - -5- ?0 i6 R8 I% m2 c8 o
- >>> math.floor(-4.01)
" w L0 X; b. i5 D2 ~8 N - -5
复制代码 ( W! ^! ?" Q8 [: y, @
math.pow(x,y) 返回x的y次方,即x**y
4 _: v' z! c5 N" T+ ?- #返回x的y次方,即x**y% s- I) g$ m& ]* X, B4 u6 g7 s. N
- pow(x, y)
& c9 B* X2 U+ Y; H: @ - Return x**y (x to the power of y).; B5 U& t( p. ~; [# C" I6 R
- >>> math.pow(3,4)% U- x3 \6 y' P' C- M
- 81.0- ?, S" J1 ]! t
- >>>
0 Y' G, X$ }+ ~8 ~) `! Y$ F - >>> math.pow(2,7)$ u. h/ z$ Y% q* b
- 128.0
复制代码
7 W# \; I2 t1 a( m; \, F5 w6 o! F! |+ Pmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 |; j3 {2 n: r' p+ z* z/ H- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 ~+ B! N) U1 S& C6 F z5 n - log(x[, base])
' n' f! v) D, t% X% {1 r - Return the logarithm of x to the given base.9 p- v" e( Z0 X& G l
- If the base not specified, returns the natural logarithm (base e) of x.- D8 _* q% z! x7 R( [7 ~% q
- >>> math.log(10)5 `. Z8 P. K! f2 k1 J' r
- 2.302585092994046
+ t5 ~6 `( k& J4 p) A0 j - >>> math.log(11)
3 ^% R' a% w% W - 2.39789527279837071 p; e% H5 F% u
- >>> math.log(20)
$ h/ p( l$ L e/ ?0 v0 w - 2.995732273553991
复制代码 ) a& Q- B/ G) g5 z" L) f' A" x
math.sin(x) 求x(x为弧度)的正弦值
, o: j4 x4 E7 U4 X- ^+ k; }% q- #求x(x为弧度)的正弦值
8 B5 J+ V7 h- o- y P* w - sin(x)
" ]0 Y: {3 a9 g - Return the sine of x (measured in radians).
' l/ ?" x/ {9 ?# W, w" E& g - >>> math.sin(math.pi/4)
( Q. r5 R( R3 ` - 0.7071067811865475
' M6 h8 x8 g$ E; S2 Y3 { - >>> math.sin(math.pi/2)
3 }% n* W( L- n# A6 m - 1.0& {! f( Q& @" E: f' {
- >>> math.sin(math.pi/3)
) W; o- }" ] u- N8 e; p - 0.8660254037844386
复制代码 . Q5 z6 g' |- s2 O" g% S0 V
math.cos(x) 求x的余弦,x必须是弧度
5 D3 r2 b4 T: ^' p' [! y- #求x的余弦,x必须是弧度- I3 O" H& |4 `
- cos(x)) g# `: ?2 S, a% y8 z" `
- Return the cosine of x (measured in radians).
* S9 a9 o" j% o4 |6 w - #math.pi/4表示弧度,转换成角度为45度
; n4 e! G+ ?6 R - >>> math.cos(math.pi/4)
% L" }- l2 o5 ~+ V( J$ w; o* U - 0.7071067811865476
' T9 A. ]2 L. U1 @) ?1 [, a - math.pi/3表示弧度,转换成角度为60度
+ B3 v2 v& K/ U! J - >>> math.cos(math.pi/3)
$ C' @- J5 A3 r& o, B+ B- X - 0.50000000000000014 x, n& P: c4 I R( ]
- math.pi/6表示弧度,转换成角度为30度4 m% o- i4 Y& `8 F" K$ n
- >>> math.cos(math.pi/6)
( r' e- c# |8 J& _) H - 0.8660254037844387
复制代码
% G/ D: O* [0 W# Wmath.tan(x) 返回x(x为弧度)的正切值
3 ^) H8 z, W( M; z) S- #返回x(x为弧度)的正切值
) I4 {: r% c B2 I - tan(x); ]9 j8 n4 P. x$ c
- Return the tangent of x (measured in radians).9 O' \, z4 L$ Z9 z
- >>> math.tan(math.pi/4)
9 \6 T( E* d9 o& W% ^) y - 0.9999999999999999' w$ M! X. q6 F- \- N
- >>> math.tan(math.pi/6)$ O$ X/ P: q; d. {) E
- 0.5773502691896257- G, d( P2 O& e3 {% x: V+ t. c/ U- F
- >>> math.tan(math.pi/3)$ \+ l* ~ n" \( {
- 1.7320508075688767
复制代码 . W; G* e& V( H0 G8 V1 x
math.degrees(x) 把x从弧度转换成角度& h3 D! x C |$ }9 i* C# E
- #把x从弧度转换成角度
: _# W+ k$ U7 o: Z( ?9 x$ k! a - degrees(x)
2 n: Z5 E. n4 h% g% Q - Convert angle x from radians to degrees.
0 A) s0 f/ S& K6 w/ G1 R. d. j) A - 5 g0 ]& G; @: S5 T8 m
- >>> math.degrees(math.pi/4)
4 H: U! Z% f* F C, v% r" ]4 b - 45.0
f2 D' F4 ] ~6 R4 O - >>> math.degrees(math.pi)
: {6 }: [ }, C( T3 s& a - 180.06 v4 u' \. q4 V7 d% L6 a
- >>> math.degrees(math.pi/6)
4 l5 ^, G3 \/ U4 R - 29.9999999999999963 s" w: G; U- G- ~6 M% r* ~
- >>> math.degrees(math.pi/3)0 z" q& u* j% o8 j0 l8 \/ y
- 59.99999999999999
复制代码 ; q/ ]) @ \+ _: Z
math.radians(x) 把角度x转换成弧度: U; w0 i# [6 y5 z
- #把角度x转换成弧度. _: v! n: T* k- H
- radians(x)7 `- }( V2 v5 J0 @1 D3 q8 a
- Convert angle x from degrees to radians.; ?- {2 P/ x, ] l$ B
- >>> math.radians(45)3 j) M% D) n F. A6 A- D/ M
- 0.7853981633974483) P7 i6 H; ]) ?' s
- >>> math.radians(60)
4 ]# j+ k& v2 x# Z: v0 ] - 1.0471975511965976
复制代码 3 h2 ^+ K; n; \9 ^7 O
math.copysign(x,y) 把y的正负号加到x前面,可以使用0/ {1 d. o9 _0 q+ ~5 J+ g
- #把y的正负号加到x前面,可以使用09 i# Z3 i' Z' j( x7 x# ~
- copysign(x, y)
' X* L1 x! F- g- i' A( | - Return a float with the magnitude (absolute value) of x but the sign }* r$ X' | o0 k7 D. h3 Y8 d% V
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) / y1 e P5 I& O0 h" h
- returns -1.0.- d- e6 r' Q) K5 ]
- " b- H9 i D# {2 s: K. u
- >>> math.copysign(2,3)' `! ]1 K4 O- V9 `1 {$ d
- 2.0
: P* R' z) h/ ^4 B k - >>> math.copysign(2,-3)6 G B# {$ B9 D. p9 i
- -2.0
$ L& ~8 R. M1 H% x! P7 @ U. Y/ Q - >>> math.copysign(3,8)
. \) {, G) J% ^ L5 G% M1 u - 3.0* G, V2 P: H3 Q6 { b) Q
- >>> math.copysign(3,-8)
7 y t( [2 f/ Z% _ - -3.0
复制代码 ' K4 s$ ^; R. z& G" ?7 ]9 H/ E' r
math.exp(x) 返回math.e,也就是2.71828的x次方
) e" D' C+ i1 `7 y# p* W4 `$ |- #返回math.e,也就是2.71828的x次方( s" a' F0 X% i y; F, \! v
- exp(x)
: B& P0 ? T3 h. j" V - Return e raised to the power of x.
: z9 j$ ]$ e! g2 u
. A1 m' r/ H7 E, w6 d1 d- >>> math.exp(1)
3 S- d8 P+ [, v" R+ l6 b; T- e+ Y - 2.7182818284590455 s+ o5 Z! F+ `# \* r
- >>> math.exp(2) {' r. f1 h# U" S$ D7 x
- 7.389056098930653 [9 c4 Y% g" H1 e1 X Q) C3 _
- >>> math.exp(3)/ q0 w3 m; D ]' L( w, f" b, N
- 20.085536923187668
复制代码
# c8 |) \2 H. h7 D& m* xmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1' A1 k8 G( u% ?' L& q& n
- #返回math.e的x(其值为2.71828)次方的值减1
6 }9 O# q; T7 J' i( { - expm1(x)
# Q; i. H) ^: ^4 r - Return exp(x)-1., c+ J) b8 Y% B5 p# m: Z
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 o6 C4 k Z: i. H% u* V6 i) [
6 W! D5 A5 ~" q: o! ]# H- >>> math.expm1(1)$ K8 F9 }% k% r5 W& w( y$ }
- 1.718281828459045) t' F& ]2 M L2 G0 s ?
- >>> math.expm1(2)
' }$ w0 ^. l+ j$ F% @# M o - 6.389056098930653 C) V) D" d. R8 d; U# O
- >>> math.expm1(3)
6 Z) }$ E2 G6 i6 t, _6 c) H1 W - 19.085536923187668
复制代码
! X7 j8 P4 y) q/ T! ^" u8 z6 Fmath.fabs(x) 返回x的绝对值/ D% H% e% |5 |2 p
- #返回x的绝对值
4 G5 ?. N5 J9 w+ A7 ?; w; z; J - fabs(x)% G% H& J$ ~% y/ w8 a% \/ C1 a0 T
- Return the absolute value of the float x.
6 R; L$ ^9 g& Y
% p9 X/ M7 y( \, m- >>> math.fabs(-0.003) A+ ?, {- y4 c# X; O; r
- 0.003
) W2 }% f1 Y) m - >>> math.fabs(-110)
3 K T. T+ k& y2 z h- B6 {% H - 110.01 v, t0 O& G' \
- >>> math.fabs(100)
' \6 g# @ l7 s - 100.0
复制代码
- M# W3 e# R" F- B2 Vmath.factorial(x) 取x的阶乘的值5 l! J) C) D. Z1 G
- #取x的阶乘的值4 H! c0 l* r5 o9 S
- factorial(x) -> Integral
0 M! y8 s/ K, B! {4 ^6 A& x - Find x!. Raise a ValueError if x is negative or non-integral.
$ U8 K6 _6 ] E Z; f4 } - >>> math.factorial(1); Y1 d5 N* q; @3 O
- 1; P: N7 J' [6 I3 R5 J* |/ c
- >>> math.factorial(2)( m; `! g9 m/ o/ }( e4 C
- 2
$ @3 h ]6 H# A* `5 @ - >>> math.factorial(3) _( j; s- O" l( E
- 6, k; ]" Q: |1 `
- >>> math.factorial(5): [1 C4 x7 G) ]# ?& w% [' R
- 120
, u/ `3 A/ Q( p: n3 W, `1 V+ t9 o - >>> math.factorial(10)
5 v6 y* K q+ t: f) z! u - 3628800
复制代码
9 h, H8 e n0 L" [# y0 X& Z( o8 Nmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数4 `( d8 T5 C0 k2 g& {6 `6 W8 X2 u5 I
- #得到x/y的余数,其值是一个浮点数
+ o+ l. t. l1 F& v) y$ O! Q& Q - fmod(x, y)" Z/ g2 r0 A7 B- ^- b$ b, J+ ~
- Return fmod(x, y), according to platform C. x % y may differ.
5 \# P9 S1 [3 o) V, {, ^$ s - >>> math.fmod(20,3)4 h6 _! I* H0 `8 Z
- 2.0- a; F/ B; Y- ] P
- >>> math.fmod(20,7)
2 D! ]; g- o0 c# W* i: X2 F2 [ - 6.0
复制代码
! o+ _; g) J I4 K6 L; @7 W0 c. ~math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 ~0 i, h7 ]9 c1 N' S; ?& o0 o1 k7 e
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,8 ~9 p0 {$ j! d8 h- W( s
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 L# l& W1 \/ A$ e+ f
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
, w7 d% v$ ~- w5 s& j$ L - frexp(x)( \$ y! ^% K5 R6 A% j
- Return the mantissa and exponent of x, as pair (m, e).0 l! O3 w5 l# j2 O
- m is a float and e is an int, such that x = m * 2.**e.
' w% m# P9 y0 B0 P; v; |/ ` - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
# _! G$ E' U4 O - >>> math.frexp(10)
& f z9 E I, ]6 n6 q& \3 X! ]: G - (0.625, 4). U# [; c) Y4 v! {7 ?
- >>> math.frexp(75)
. k0 n8 b1 _3 A0 }; Q - (0.5859375, 7)
$ r' T( o3 j5 J" m - >>> math.frexp(-40)
; ^" b6 j" K U - (-0.625, 6). h- Q8 c, r& k7 V& L9 ?# L' R% k6 ^) D
- >>> math.frexp(-100)! G/ }2 h \; g% T7 a- `
- (-0.78125, 7)
+ X4 z1 v# r+ B: h2 e' ? - >>> math.frexp(100)
) `( r) v4 W! ^, t - (0.78125, 7)
复制代码 , l. f5 [) N) U y$ g z
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)/ g2 T. {9 f; t
- #对迭代器里的每个元素进行求和操作
0 g+ ?! l. _. ?, Y% K& b+ x9 X& H - fsum(iterable)
( B9 n! y2 v1 X& U% b9 t - Return an accurate floating point sum of values in the iterable.
1 c U3 c8 S5 _* ]) J4 i - Assumes IEEE-754 floating point arithmetic.
8 [0 ], @2 |' m - >>> math.fsum([1,2,3,4])+ G5 E$ o5 N% s( A: o* i$ K
- 10.0% x# p5 Q1 n* V, _5 d) L
- >>> math.fsum((1,2,3,4))' P i( b! n* R
- 10.0
6 | ?( j: f( T. N - >>> math.fsum((-1,-2,-3,-4))9 k0 K5 h v) @) R
- -10.0( ?" J/ k9 ]( Q9 A* ]# U$ O; `
- >>> math.fsum([-1,-2,-3,-4])* G- K! N0 a$ v' S9 h( ~
- -10.0
复制代码 9 X, U3 ]9 u9 v/ T {! m4 b
math.gcd(x,y) 返回x和y的最大公约数' n7 F) x+ P3 I# S9 T. Z/ s
- #返回x和y的最大公约数
* M7 R5 U8 ^6 D; f8 ]* B: i$ U% K - gcd(x, y) -> int
1 z& D. \/ E% v5 r. E - greatest common divisor of x and y& w, a( L. `# M# K, ~! \
- >>> math.gcd(8,6)
2 d% k: i' X( S5 ~; W4 G - 2
2 |0 ^7 @5 q( K4 b }# q& S% } - >>> math.gcd(40,20) I% ]. w: D5 r( T u6 ]" p# G
- 20
3 d7 `( Z4 ?( }. K - >>> math.gcd(8,12)
7 Q* s6 w4 \2 z1 X - 4
复制代码
$ s9 N7 i2 ]7 G7 ~$ G) bmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
$ u. }2 ^+ C1 g+ ]- #得到(x**2+y**2),平方的值
) ]- c7 | d0 Q& [; A - hypot(x, y)! \. |: _6 P* }4 ~' J
- Return the Euclidean distance, sqrt(x*x + y*y).0 B+ S( d5 ^1 i R6 u; u
- >>> math.hypot(3,4): r7 m4 N3 j% k! Y+ w! P
- 5.0
7 R; h; ]9 |& c0 A. Z: x - >>> math.hypot(6,8) g/ {/ k0 G7 M! Z
- 10.0
复制代码
- X! J! \3 p4 s4 _- c2 Q- I: Qmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
/ U# y# v5 W2 J8 C' d' I- #如果x是不是无穷大的数字,则返回True,否则返回False
! L5 j+ ~! t1 X5 [ - isfinite(x) -> bool' \8 ?' _# k' M% B! z
- Return True if x is neither an infinity nor a NaN, and False otherwise.
9 f+ b/ U+ K. A7 m6 F) K8 D - >>> math.isfinite(100)
9 S( x9 w3 f2 t$ e2 u - True" d: N; a3 d9 m" @, I
- >>> math.isfinite(0)( |+ @2 G5 ~ u( _4 x
- True
: w3 c5 s+ c/ W, K1 ^3 n) c - >>> math.isfinite(0.1)
& B) b, [8 E( i# x - True4 h- K L5 ^6 M; x7 d- A
- >>> math.isfinite("a"). W! S& ^7 y. s$ y* o! ]* @( ]3 N
- >>> math.isfinite(0.0001): g7 p* b7 d$ w/ K% o
- True
复制代码 # Q' m) V3 G! k! ?
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
( z* W G$ n& F. b3 c- #如果x是正无穷大或负无穷大,则返回True,否则返回False1 O( U( D0 ^) g) Q% K, H' i
- isinf(x) -> bool
" ?# E3 u5 r6 P" ^ - Return True if x is a positive or negative infinity, and False otherwise.
. E: u7 Q, F3 `0 O& C3 m( a6 x - >>> math.isinf(234)
0 Z3 S- L; D# e3 R - False7 |- v7 C) |4 K
- >>> math.isinf(0.1)
* H. \- J# W5 ~* L% t+ N% g/ J - False
复制代码 % X8 L. c& Q% g1 u" Y: V
math.isnan(x) 如果x不是数字True,否则返回False! I2 F0 t$ O' ?- F8 l# ]
- #如果x不是数字True,否则返回False
w4 C0 b! m* V* W - isnan(x) -> bool
( u+ M7 A# A1 x7 m - Return True if x is a NaN (not a number), and False otherwise.6 g6 p Z" Q3 h/ Q& }
- >>> math.isnan(23), Z' b! c; R! I2 K: @5 p5 V
- False
# p3 L* a9 b/ x& J7 n, k - >>> math.isnan(0.01)5 I B* L0 f' r4 J, b# S$ G t
- False
复制代码
0 v# W$ x; p3 q3 Q9 i/ [& \. lmath.ldexp(x,i) 返回x*(2**i)的值' K0 y# x1 M' @, U8 R% `
- #返回x*(2**i)的值
& L2 H) H( f3 n( h9 @ - ldexp(x, i)0 G: J. S L0 a7 h2 y6 u9 a
- Return x * (2**i). s9 \1 h- s) R# y
- >>> math.ldexp(5,5)5 W5 H# l2 X; x& l/ P
- 160.0/ r/ u8 L; C0 c @7 m
- >>> math.ldexp(3,5)6 l% u: p/ V1 c- {4 o
- 96.0
复制代码
7 {3 C! R; o w8 d& S/ ]2 n2 Wmath.log10(x) 返回x的以10为底的对数
( D$ s9 p9 L/ O+ Z- #返回x的以10为底的对数
" x% \( S+ X: q7 h - log10(x)$ B, N; H( c" H% x5 ^
- Return the base 10 logarithm of x.1 M7 c4 k$ F; H% O2 T( A+ N
- >>> math.log10(10)
% B" C: s/ J: ~% {6 ` - 1.0& o+ N9 \1 s, r* H5 S+ u5 ?- l( ~0 ?7 x
- >>> math.log10(100)2 ?( x* p- Y8 |$ V- N
- 2.0: c n* b6 Z7 E+ s; I
- #即10的1.3次方的结果为20' M3 Q9 C, ?, ^5 j# t
- >>> math.log10(20)
( t4 b |/ K0 N! v* F - 1.3010299956639813
复制代码 7 E1 M/ Y! p' S: O: `
math.log1p(x) 返回x+1的自然对数(基数为e)的值3 u. [& ~% n/ t
- #返回x+1的自然对数(基数为e)的值
8 k! @ u' p+ W8 j! ?, y8 Z - log1p(x), E( D* n) e& C% i; S& O
- Return the natural logarithm of 1+x (base e).
8 \; N; b: D; C1 E: E B - The result is computed in a way which is accurate for x near zero.
( D. w( h# i R - >>> math.log(10); w: q% [, q1 v( W) x
- 2.302585092994046. }$ l# ~3 x8 o! t) H
- >>> math.log1p(10)# Q% T" G C8 c! { p% r9 ]
- 2.3978952727983707( m2 n7 ?2 e, ?! H) R
- >>> math.log(11)- y- q/ E+ a. }/ Q. G$ Z
- 2.3978952727983707
复制代码 3 f" H; x9 U+ ~) q
math.log2(x) 返回x的基2对数
3 t* g, P! z3 r) c k( v- #返回x的基2对数 O/ X1 V8 ~0 J% [3 R4 D
- log2(x)
2 Q& G5 B1 ^9 J' l2 t: t) G - Return the base 2 logarithm of x.! e9 `! H( w5 {7 z
- >>> math.log2(32), ^5 E6 _! m/ ~! O2 W l- C" [+ t
- 5.0
# r- ^1 r. D1 C( Q* \+ V4 W - >>> math.log2(20); f# G: q1 [$ \: c7 c. @
- 4.321928094887363
$ Z. ~. Q% t. A. {3 A - >>> math.log2(16)
0 @' K* a6 {! R, c, K( v - 4.0
复制代码
! K/ B+ e h, O2 |0 h6 }math.modf(x) 返回由x的小数部分和整数部分组成的元组
6 S5 q _9 a# z- #返回由x的小数部分和整数部分组成的元组
% \4 a+ R3 E: P" K - modf(x)' [% B7 f( J8 O. `9 l" n H8 q- ^
- Return the fractional and integer parts of x. Both results carry the sign9 _% _! k- r& h) k: D8 Y) f; B
- of x and are floats.
" p; h' ~+ j6 y) d) t7 I - >>> math.modf(math.pi)
, B/ p |7 M" R% j9 }6 r Q f - (0.14159265358979312, 3.0)
" y$ N5 e& j" L$ J; q - >>> math.modf(12.34)$ B L" R# d$ L- t0 G: E- y: L9 A
- (0.33999999999999986, 12.0)
复制代码
. x0 [5 }& y9 ^math.sqrt(x) 求x的平方根* ]' ~& D) {: G( d0 x& D( G; h3 L
- #求x的平方根
% \- h/ K+ P& s$ u" d5 b8 o1 f - sqrt(x), A% E1 [$ X! h7 S
- Return the square root of x.
' L" z. N9 T& b: ` - >>> math.sqrt(100)
; o. D4 k7 N8 ?$ I8 E, { - 10.0
* o1 {/ M# V' U. _ - >>> math.sqrt(16)
u2 G4 G* o- b - 4.0
: m7 c# o" }- l - >>> math.sqrt(20)
3 e! ?' E% M- F( @( Y! M - 4.47213595499958
复制代码 " l6 [0 @8 Q: L' F" A; ]2 _: S
math.trunc(x) 返回x的整数部分- #返回x的整数部分: n' y& C: N A' B& q% v
- trunc(x:Real) -> Integral1 n2 X/ h' `% t; U1 Q
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
+ i. F( l0 ~4 ]) A8 S& Y* E - >>> math.trunc(6.789)
8 F' q: T [7 B - 67 N9 G6 z4 f* q* O- j! @7 x2 }
- >>> math.trunc(math.pi)' h2 v7 P# b' e3 A( p* x
- 3
8 J8 `) M; S" G+ [" B8 j - >>> math.trunc(2.567)) C# E9 p( M. r
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|