马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# M1 x2 ?, J" |' ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: P5 }4 E! x2 N' J6 u0 M
2 M+ Q! [8 v3 V- A" [+ F: U/ M方法1:- j5 Z/ N6 z# Y* S9 B
- >>> import math
) i4 O- [8 o% f) t! S - >>> math.sqrt(9)
7 P' M9 V5 J5 G5 ]# ^5 H - 3.0
复制代码 方法2:# s+ y/ q; _ @7 m" w: t
- >>> from math import sqrt! l' R7 u/ C" r
- >>> sqrt(9)! m: A; y/ @* _2 v3 k
- 3.0
复制代码 - G( L; u1 q$ t: Y: \' r# M. ~4 R7 R* X
, U z* F$ R! ]7 N- [# n4 |% I( \
math.e 表示一个常量
* b% K V2 z) I9 g: a- #表示一个常量
- [$ A: O5 `3 F0 n" g7 ~ m - >>> math.e: m0 }* ?' T0 A/ g6 r5 h
- 2.718281828459045
复制代码 / v8 |6 [+ u+ Q
math.pi 数字常量,圆周率6 {$ s6 ~1 F* o% g( x* H
- #数字常量,圆周率
% I @$ t- P: [/ ~& J* p# H - >>> print(math.pi)! z" {+ W9 R; \3 W6 ?/ p
- 3.141592653589793
复制代码
6 j2 t0 J8 F: v" b$ D7 X5 amath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
& D+ V) l- W' _% @4 J; ?) z- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
- `3 [$ Q+ X9 C6 Z6 P) O - ceil(x)
e, ~4 y5 r) w' j+ { - Return the ceiling of x as an int. e. Z/ ~0 C m* X' k6 L
- This is the smallest integral value >= x.
# ]5 W, R4 M& f( Q! e+ s8 h
$ m! A R. a3 B* K3 F5 ? b5 Y5 B- >>> math.ceil(4.01)4 r7 A% [* w8 D$ _' R/ f
- 5" b9 A8 @8 j5 ]3 `5 M- u1 y4 c
- >>> math.ceil(4.99)
$ Z3 W$ P* H/ W0 d, u) v - 5
- ^6 I" S+ V# K: k - >>> math.ceil(-3.99)6 B+ ^6 F: n4 g7 U
- -30 l! U- t4 w9 G* s% _
- >>> math.ceil(-3.01)
7 A7 X7 u. c3 }. \# m4 H) {( ~ - -3
复制代码
9 r X/ x; B, wmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身. c# P+ @4 n- O/ B `
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ k) _/ T! H- R, O
- floor(x)
! X. i- A# u! }* A8 z - Return the floor of x as an int.0 S: S6 Y. Q, k; ^0 U5 b- f' k
- This is the largest integral value <= x.
, H( e( l, L8 c' ]( q& X - >>> math.floor(4.1)
/ q2 @/ c% H; L: D6 \ - 4
+ b5 ?5 L9 l" j* J/ N0 C - >>> math.floor(4.999)( E% f6 U4 e4 q! _! m1 J
- 4
' G8 `( J$ G2 z( V& d5 i; L# @ - >>> math.floor(-4.999), {% }2 w* k0 h3 e1 w' h, S
- -5$ Q1 Q" E8 c B3 `+ D! L* b# ^4 W$ `
- >>> math.floor(-4.01)( [# x. a0 |* n. t( f! h5 g
- -5
复制代码 " V" ]" v' D" b0 O0 Q' q
math.pow(x,y) 返回x的y次方,即x**y
& Z' @% U* U# F; f! Q3 c- #返回x的y次方,即x**y* U0 g* X' ?+ F: r/ d
- pow(x, y)
/ m% g. R j3 E8 H8 \ - Return x**y (x to the power of y)." p+ X7 k$ ?* c, @8 J& f) Y
- >>> math.pow(3,4)
, L; t7 x7 k" |3 J - 81.0
% P* e: b/ ]! N3 J8 e7 ^ }6 ^: H6 P - >>> 2 X Z) U) l. b$ k J& L8 T7 G' { b
- >>> math.pow(2,7)2 W; i7 s' P' \# _2 m+ F
- 128.0
复制代码 ) o$ W1 N0 V, f& ~; @
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 g& @3 s8 `: H2 R$ C- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* X: T" `4 r/ t* Z
- log(x[, base])
: V( a+ H. I, B% z - Return the logarithm of x to the given base.% f5 n9 N% l& x8 C3 e
- If the base not specified, returns the natural logarithm (base e) of x.
$ F% k* Y g) [* W ]5 J - >>> math.log(10)# v/ a% u s* ]4 p
- 2.302585092994046
5 j/ {% P9 v6 e' o4 }, p- |& t - >>> math.log(11)
4 C9 F; Q' F( J7 y$ c5 V: n8 B - 2.39789527279837073 k7 ]* _, g( i1 X0 g" K
- >>> math.log(20)
, {2 d- q0 N; Q" v A - 2.995732273553991
复制代码 - [1 U1 q, C3 ?4 O" z4 {! P8 `
math.sin(x) 求x(x为弧度)的正弦值. _! O. n* S9 ?& v
- #求x(x为弧度)的正弦值9 V- W5 S* y! G7 Y! }' |
- sin(x)+ m9 {) l) c: p/ N
- Return the sine of x (measured in radians).+ z. ?. n0 |7 m& y
- >>> math.sin(math.pi/4)! r' B( O7 G) X( r- D2 I
- 0.7071067811865475
, o9 G( f. @5 i" n6 T1 F - >>> math.sin(math.pi/2)
1 X+ B" F6 ~5 \- ]- j$ e - 1.0* p: e# e3 T- d% G# D0 V$ b$ f% |
- >>> math.sin(math.pi/3); I5 m4 A' L: P' Y1 D
- 0.8660254037844386
复制代码 4 |. ~3 s1 q0 R/ f4 P
math.cos(x) 求x的余弦,x必须是弧度% Y% P S7 o2 o* m
- #求x的余弦,x必须是弧度+ v" J) F% N% I7 w$ V- B% e
- cos(x)
+ N3 J! P5 O) s; l5 F - Return the cosine of x (measured in radians).) }0 w* z5 S* r
- #math.pi/4表示弧度,转换成角度为45度8 X3 `% X7 g1 Y& ]/ T2 I3 X
- >>> math.cos(math.pi/4)7 H4 C- t# C+ t. R1 M) G1 j
- 0.70710678118654761 n0 Z' P1 q& X8 I f4 ~
- math.pi/3表示弧度,转换成角度为60度
) V, x; f% ]# c2 p2 R. T: x - >>> math.cos(math.pi/3)
; w1 {0 @3 x* q: q! t- l - 0.50000000000000013 g+ I5 G2 o4 v. \
- math.pi/6表示弧度,转换成角度为30度0 |" `1 F4 n" e! Z
- >>> math.cos(math.pi/6)
! D& y. |' r$ K4 r - 0.8660254037844387
复制代码 . j5 ?; H, x O+ K5 o; m
math.tan(x) 返回x(x为弧度)的正切值- W ~/ A5 P" }8 r7 B
- #返回x(x为弧度)的正切值
3 @6 o4 P" p) i+ h) q - tan(x)
' O- _0 J2 U$ a' q- }0 g - Return the tangent of x (measured in radians).
) W z) {- e/ i9 n9 l1 T7 [- ` - >>> math.tan(math.pi/4)- W) a2 R1 w6 r) H+ ]: J" N
- 0.99999999999999991 P, y7 [0 W* A. F
- >>> math.tan(math.pi/6)
% I0 O3 I- t3 Z4 I - 0.5773502691896257
: r2 h- e, Y: z - >>> math.tan(math.pi/3)! {# n1 t m2 T# ~6 `7 @
- 1.7320508075688767
复制代码 # v7 o' F7 q4 d$ a: n
math.degrees(x) 把x从弧度转换成角度2 O: Y1 ]& r& Q' L W+ `# J
- #把x从弧度转换成角度9 X2 E J* }: H" e5 O5 S
- degrees(x)
5 Z8 Y$ P" O0 [' b - Convert angle x from radians to degrees.
$ e* F, d" L# R: j2 ]( }$ j
3 r: m7 T6 [5 a D7 [- >>> math.degrees(math.pi/4)9 I0 ]! B# Z/ o# M
- 45.04 i: v7 t2 ?& T: _0 w2 X
- >>> math.degrees(math.pi)
9 `# N: r' n! q: ~ - 180.0* @0 N; I+ h! e8 ^: R( q. @
- >>> math.degrees(math.pi/6)
6 Q/ G) m& H8 u9 }( X+ S - 29.999999999999996
: h, Q* w& X+ r+ K - >>> math.degrees(math.pi/3)
/ y, o3 f, r! I, \) [! K - 59.99999999999999
复制代码
2 j/ V6 o/ x" imath.radians(x) 把角度x转换成弧度9 I! [8 o5 o7 A9 {7 Z) V
- #把角度x转换成弧度% ~3 t( ?* b% V
- radians(x) j+ S" z! @/ c4 I8 L& C! m
- Convert angle x from degrees to radians.
X e7 p0 q6 }- @- J - >>> math.radians(45)
( O* f+ F& V0 Z9 Y9 r; | - 0.7853981633974483
- ], d2 L% C! [ - >>> math.radians(60)
7 l5 @8 z0 N0 B3 q" v - 1.0471975511965976
复制代码 2 g: U; _# P1 K6 _
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
) b* A$ a3 V' @, y! U- #把y的正负号加到x前面,可以使用0
0 P ~0 |$ W9 B* b. \/ o$ G$ O. `/ Z - copysign(x, y)
: }: N( {. A( s. w+ s& H" B# V' S - Return a float with the magnitude (absolute value) of x but the sign 8 }4 ~! R! y! C% l* F# {
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) , d1 l6 s% ` k. p, U4 \: ~
- returns -1.0.
! {+ B- ~) E# T2 f
1 ^. Q- J3 E( n: J$ U1 ^" U- >>> math.copysign(2,3): d8 p2 E7 ?) t/ |
- 2.0
. Y- c5 f: g, O @3 J - >>> math.copysign(2,-3)
) i/ w8 M& t* @0 j. P1 i2 P - -2.0: l0 [4 F2 ^& [( R/ I
- >>> math.copysign(3,8)
) ]8 `" ^- c2 Q3 N& e - 3.0) n$ Z% M/ y/ ^9 `
- >>> math.copysign(3,-8)
2 v' s# M$ s! M0 E g - -3.0
复制代码
3 I+ ^! m8 x+ f' X6 ]: s) Rmath.exp(x) 返回math.e,也就是2.71828的x次方
8 S1 F: x4 F5 M- #返回math.e,也就是2.71828的x次方
; C# y; A, i/ J* P - exp(x): O; o3 g1 ?, [9 ^, X G
- Return e raised to the power of x.
7 _$ n7 L* t2 V - ) j! f, x6 O- G) b
- >>> math.exp(1)
7 g% J: l: p8 @6 ~8 K, R) s! k& z - 2.718281828459045! \$ |* S( B. o l% |; ^
- >>> math.exp(2)
- E" _0 z8 Z; O! b* x0 t - 7.38905609893065
2 |# S+ n. ]2 e& o6 M# u" S - >>> math.exp(3)
% O' A$ t. `) h# { - 20.085536923187668
复制代码
- y* j( t2 w, b" a! ?math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
7 |; W4 D: Y- Q% Y* D% F- #返回math.e的x(其值为2.71828)次方的值减1
$ b5 U9 K4 N. A3 A' L - expm1(x); i, i; n, P7 d2 [+ J: f
- Return exp(x)-1.
. Z) R7 ?2 B9 K: Z5 y - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
+ `* F& n* e4 r6 j
% s5 E8 F+ \: F' [- >>> math.expm1(1)
& J9 l$ W$ r5 l5 B- F% Y0 C) P - 1.718281828459045
0 O6 W6 x8 u3 t - >>> math.expm1(2)3 L4 u3 p- S# A2 Y
- 6.38905609893065
/ u5 F3 R3 Y8 ~# v6 f - >>> math.expm1(3)2 V% i, X; p8 [. c. j- {
- 19.085536923187668
复制代码 . V3 q- k7 O( n2 w% v5 z$ z
math.fabs(x) 返回x的绝对值
h! @9 k$ D$ X0 j' U0 P# \7 O P- #返回x的绝对值
$ z2 e1 z; l G - fabs(x)& x. `' ]1 v1 O. A4 j/ q/ @
- Return the absolute value of the float x.5 S. \7 B2 S8 C
- 3 O9 s. a" F) e) A- D
- >>> math.fabs(-0.003)# S4 p3 y4 B' `( D
- 0.003, q+ v3 A \$ V5 e: A
- >>> math.fabs(-110)% ^" e' ~- ~& K! j& @
- 110.03 z4 z' G% a8 W8 f% I
- >>> math.fabs(100)
7 @; C) T% y4 E, L* O* G/ ?% l - 100.0
复制代码
9 n. z8 e7 v, a* U/ @math.factorial(x) 取x的阶乘的值, x1 N) B. F) ]8 l
- #取x的阶乘的值
9 j6 J) {$ J; o" f5 n - factorial(x) -> Integral; Z1 l( k9 x+ }1 ^1 X* ~. @- j
- Find x!. Raise a ValueError if x is negative or non-integral. t( x9 R' s1 J
- >>> math.factorial(1)$ z! p' Z& f* \8 |' X" A. a
- 1
" P' u/ \1 c2 H% h' z6 L3 e$ q - >>> math.factorial(2)% A2 i% Z: ?2 d( Q4 ?" }
- 23 b' _0 I M1 x
- >>> math.factorial(3)
% W" d4 }4 q/ H3 P8 c( w - 6
' `+ W) w+ i {$ T& a+ o - >>> math.factorial(5)( ^* a! I7 I( V1 [7 d
- 1205 S$ g) S% I, ?2 i' _. h- b, Z) w& I" F
- >>> math.factorial(10)
1 c- w+ h- [4 I- o! b - 3628800
复制代码 6 P. v$ i. y9 ^/ J% Z% ]
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数0 j6 T R$ f `8 W
- #得到x/y的余数,其值是一个浮点数$ y9 g9 V1 d& k9 R2 D3 B+ Q
- fmod(x, y)
1 h1 b; ~) a0 Z! u D, }# I' G: D - Return fmod(x, y), according to platform C. x % y may differ.
+ n& f! I% d6 U% E: Q - >>> math.fmod(20,3)
/ O t( K/ p" U# _ - 2.0
" u9 N p1 P, p - >>> math.fmod(20,7)
! [- o2 V( S( ?7 M+ _ - 6.0
复制代码 9 r/ _$ U7 X! A$ K$ O' j
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 ^( _ ?& }% w' _
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
- B4 V8 Z4 L3 ~" m4 k5 o. ~. W - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值) s# b4 T* y$ x( E/ u: p
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 q. U s# s& R6 b4 g% P+ P
- frexp(x)
5 @9 g, h4 b7 T6 K5 N5 _ - Return the mantissa and exponent of x, as pair (m, e)." |" R& f' w5 U& c8 V
- m is a float and e is an int, such that x = m * 2.**e.7 s7 I: L' i' Q; A# [8 ^
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
- }# A6 d" h* r* p - >>> math.frexp(10)
! d3 F9 o7 d4 j; X - (0.625, 4)
3 H7 @" x$ h( k8 Q! G3 C - >>> math.frexp(75)
( J- _+ j' s% B. ?* h' L - (0.5859375, 7)5 l) p8 S9 F2 a5 V, _, e/ Z0 H$ A
- >>> math.frexp(-40). y4 Q q1 z/ k% V1 J Q! g
- (-0.625, 6), G0 T0 h( l- R7 \: k
- >>> math.frexp(-100): K5 Z+ Z" F4 R0 K; W& |
- (-0.78125, 7)" D$ ^8 e/ Z2 t: b
- >>> math.frexp(100)5 k2 k) B7 D* |% X3 R. i/ O
- (0.78125, 7)
复制代码
0 `2 Q( c0 j( kmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
' b$ t5 |5 M3 x2 I& ^- #对迭代器里的每个元素进行求和操作
9 N3 h9 f; x( [" f3 @ - fsum(iterable); S. ^8 L1 I ~* b& o
- Return an accurate floating point sum of values in the iterable.
6 J, ^; J' R$ b7 x+ ? - Assumes IEEE-754 floating point arithmetic.
& C6 O, W" @+ {* R @, R" }; T3 S9 w% t! U - >>> math.fsum([1,2,3,4]): |* i+ L6 L/ u0 d/ A* K/ B/ V/ Y. _
- 10.0
_) s: p! b( @: G' q - >>> math.fsum((1,2,3,4))+ a1 B& l J9 N. ?
- 10.0
j, g; g& ~" _, } - >>> math.fsum((-1,-2,-3,-4))
9 W* s5 L3 A4 \( s4 b1 r - -10.08 ~6 K' Z8 y. |; ?% X
- >>> math.fsum([-1,-2,-3,-4])
+ X6 p& y, l9 I3 x8 Z - -10.0
复制代码
% ]" Z2 N+ J) H. v) U, fmath.gcd(x,y) 返回x和y的最大公约数3 F/ _. X6 Y1 o# g( V, p
- #返回x和y的最大公约数: m1 ]# m+ x ]
- gcd(x, y) -> int
3 W& D) T- u: V" ~ - greatest common divisor of x and y
1 C2 Q- n f+ i+ S3 g0 i3 | - >>> math.gcd(8,6)
9 w; J: i/ }/ x0 d- R# a# m - 2
/ b$ ?' F& V1 u6 Z3 e3 K6 n - >>> math.gcd(40,20): O0 o- ?" \4 P( y$ R: J
- 20; W) m0 o, I/ M6 p
- >>> math.gcd(8,12)
0 x6 B! u% O6 M) q - 4
复制代码
0 F. o8 O' p( w$ xmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
0 J! O) \' d9 M+ j" @8 h& J- #得到(x**2+y**2),平方的值
; G- n5 O1 c1 s7 B - hypot(x, y)
+ A8 F2 S( D. k2 W+ Q3 i - Return the Euclidean distance, sqrt(x*x + y*y).% e; C7 }1 |0 L9 ~
- >>> math.hypot(3,4)
1 ?, a% R( Q$ V7 W - 5.0
8 P; j9 \7 D( g. M3 n; u" A% ` - >>> math.hypot(6,8)! i3 Z7 k* ]5 J1 C" \) J: E; C$ k
- 10.0
复制代码 + d+ q: W4 n4 `+ f, F; ]
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False1 U* }4 e9 C. e! H; q; @' C
- #如果x是不是无穷大的数字,则返回True,否则返回False
$ S0 p' b+ f" U - isfinite(x) -> bool! v6 |4 E- a6 y
- Return True if x is neither an infinity nor a NaN, and False otherwise.
6 h1 U$ p* T3 D2 c7 s4 u* K - >>> math.isfinite(100). r/ y3 J Y) \" e8 M
- True2 ]/ j# i+ `# \ [# ^
- >>> math.isfinite(0)6 T/ h" k5 P1 G; r
- True
4 m8 j6 n$ a* Y5 K - >>> math.isfinite(0.1) e5 f. ^" ?8 i- ], ]+ z6 y
- True& \$ \8 }9 f9 a
- >>> math.isfinite("a")
- x2 f' J% T% S0 z2 S7 S( j0 v - >>> math.isfinite(0.0001)
$ g) q. x3 N7 J: O* o+ r - True
复制代码
; s$ f+ e9 `# B* jmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 s" _0 |+ v: s2 P4 | r1 _0 F- #如果x是正无穷大或负无穷大,则返回True,否则返回False1 P3 k% ]' @) V [
- isinf(x) -> bool
5 j5 ~* T9 u. v0 R7 a) O$ N4 U - Return True if x is a positive or negative infinity, and False otherwise.' f5 }8 r3 @; r) l* b" |
- >>> math.isinf(234)
! x+ c6 R* ^& q2 D; \' ?: ^ - False% I0 N1 T3 E8 J: Q
- >>> math.isinf(0.1)1 d5 N' g5 Z4 e& O
- False
复制代码 & M4 z) H3 Y, B
math.isnan(x) 如果x不是数字True,否则返回False
, v- A4 X, @" ]- #如果x不是数字True,否则返回False. j, I# i' I, ]) e
- isnan(x) -> bool
8 }+ \# {& {. W3 v& K9 B G$ g - Return True if x is a NaN (not a number), and False otherwise.
8 e2 q* H) R/ `' A: c3 c3 o - >>> math.isnan(23)
- P7 y8 {( R. a" z5 ~$ C - False
' I) o6 J. J& G1 y" @. V - >>> math.isnan(0.01)
6 R7 q5 \) m0 l- O5 D - False
复制代码
7 \' N" `: W$ x1 P f- lmath.ldexp(x,i) 返回x*(2**i)的值
- r" R/ a! a0 n0 i- #返回x*(2**i)的值
* l0 w) O, M. a6 S2 y! s - ldexp(x, i)
6 f' E8 R2 B8 }$ N* _ y$ l6 Z - Return x * (2**i).3 R1 u2 K: Y) m
- >>> math.ldexp(5,5)
! N, V, B& I1 ?! q - 160.0! c2 x3 a& v5 g7 [
- >>> math.ldexp(3,5)
" W/ Z8 x2 H" ]+ ] m - 96.0
复制代码 5 U/ X- ?5 |* ?: x
math.log10(x) 返回x的以10为底的对数
. c# K* V2 S8 e, `& V9 T- #返回x的以10为底的对数, {" L7 k$ N' M
- log10(x)( c0 M3 {. H$ j0 m8 Y' b* L
- Return the base 10 logarithm of x.
0 c. h. c' Y0 Y; }7 I - >>> math.log10(10)" m& i0 f9 c9 ]8 d& ^$ f
- 1.09 _- p1 s N2 |
- >>> math.log10(100)
2 C8 l0 E* T. }/ |- G - 2.0: C! M6 m. [, g: o- D
- #即10的1.3次方的结果为20
( h/ h- [# B* {& Y! X3 x$ b - >>> math.log10(20)1 f; U- n, K' m7 N8 u1 a
- 1.3010299956639813
复制代码 8 v$ j9 z0 B7 W2 n8 `9 e
math.log1p(x) 返回x+1的自然对数(基数为e)的值
3 Q- i9 Z3 E% ^: u, q& w- #返回x+1的自然对数(基数为e)的值, G& X! T+ ]5 `9 m- k
- log1p(x)
6 ^, Q4 a- C! `# U5 c& w0 T0 Y0 B - Return the natural logarithm of 1+x (base e).& Q6 @4 X6 a! F4 W% H* c7 x
- The result is computed in a way which is accurate for x near zero." _' }' w0 i5 \2 [( U
- >>> math.log(10)
4 Y, e0 H" b v2 [4 F - 2.302585092994046
8 O7 M# v, }2 t& Z* b6 d, K* c" w - >>> math.log1p(10)
5 q& _1 W2 t0 z - 2.3978952727983707
3 m0 k3 G' J0 {$ n, V2 J1 v - >>> math.log(11)% f/ R4 Q; u$ m! D( ]/ R
- 2.3978952727983707
复制代码
g3 N6 J1 C9 H* ymath.log2(x) 返回x的基2对数
9 D0 A( }; N& U: y( n) g- #返回x的基2对数% G; l6 g, e3 q5 p/ M+ h2 z
- log2(x)
" v% z H. } ~* J - Return the base 2 logarithm of x.0 h/ {' ]0 E2 N" H
- >>> math.log2(32): _3 R* y$ ^! ^* [* @2 @
- 5.04 h, q" |2 I1 y- ^ u2 F
- >>> math.log2(20)
& L8 F" l9 C; x8 C - 4.321928094887363% y2 t6 H- ~6 l, E2 e7 B6 K
- >>> math.log2(16)8 q8 t3 Q7 ~8 \5 e- H$ r# |3 C
- 4.0
复制代码 ( g8 {/ H) t3 N# o
math.modf(x) 返回由x的小数部分和整数部分组成的元组
, }9 X! K" h; ^! @# G2 o- #返回由x的小数部分和整数部分组成的元组. a J1 C+ K3 \4 d% J
- modf(x)9 B. q+ K2 I' L* Q
- Return the fractional and integer parts of x. Both results carry the sign
1 K( l4 S% I- r - of x and are floats. u) I6 B, C9 n% l$ l' m% u
- >>> math.modf(math.pi)8 b5 O- u4 e+ Q( n5 f
- (0.14159265358979312, 3.0)
# H% w4 I! C+ @" W$ h - >>> math.modf(12.34)
& P, J y9 B4 q - (0.33999999999999986, 12.0)
复制代码 0 O% y& c' `; K- B0 E( B
math.sqrt(x) 求x的平方根
# y+ s. \+ z3 L" i8 @- #求x的平方根
% W$ |8 D3 Q$ H! p7 f+ r) H4 k - sqrt(x)
9 M4 {9 {' f# S8 } - Return the square root of x.
h9 G6 C# j3 M2 M/ D' a. o - >>> math.sqrt(100)' s7 o( g/ M. l; D/ g- t/ h, M
- 10.0
+ e; O3 c' y" \' n! i0 A - >>> math.sqrt(16)
0 G7 H X+ q1 d E - 4.0 R) M) T5 ]/ ~4 Z2 l! t
- >>> math.sqrt(20)
6 K/ B& U! u8 p P" b2 k - 4.47213595499958
复制代码 7 l. F4 b) _/ j
math.trunc(x) 返回x的整数部分- #返回x的整数部分! u2 \1 \. c' n* @% A4 {" |% P
- trunc(x:Real) -> Integral
& i+ y8 ~) X' u& `& H! ]3 E% l - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
( Y O% ]- ~( {; Y7 O4 j& ~ - >>> math.trunc(6.789)4 j% ~& N* Y$ U8 u% [5 g
- 6
& u9 N& l3 z* W! a1 F - >>> math.trunc(math.pi)
; k9 G8 k/ I5 @ - 37 O+ o, v/ `9 k% w1 A
- >>> math.trunc(2.567)
b* O) o! n" T6 |: C: i9 X$ L0 U: D - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|