马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# t9 i3 m! X ^0 C& A( Q【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
) V* q6 A" B6 e r* c z* T4 k3 S% _' O. V5 d/ B# |
方法1:5 j3 y8 p5 B6 L" {, Y! W2 H
- >>> import math
2 n) w+ x% H$ b! a - >>> math.sqrt(9)
) |7 y8 Q. T' _; @$ {* n - 3.0
复制代码 方法2:
N1 V0 o; L' L- Z- >>> from math import sqrt9 F8 Q" f! |# z* u- T5 b
- >>> sqrt(9)
2 Z5 }3 ?* m9 @2 j2 B/ I, j5 T j - 3.0
复制代码
/ w- W2 T6 U* w3 w+ f' z' W ) H/ E' f1 t, A8 K5 \3 Q: R
math.e 表示一个常量
% [' {; m) V k- #表示一个常量 c) ^0 ^4 t$ l, ]5 E7 P6 j! ^" u" j
- >>> math.e, b& R# H! `( D
- 2.718281828459045
复制代码 3 C! {3 {9 Z3 h- q( _
math.pi 数字常量,圆周率
2 F7 u$ [' k- H- #数字常量,圆周率
$ ]- @* E0 F0 m3 Y& ]6 ?' G - >>> print(math.pi)
' M3 G* V& H2 N& F3 c3 } - 3.141592653589793
复制代码
; @2 Q% X$ S9 R1 z( m6 Nmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
p7 H9 A* M8 N3 b; c" M3 ], a- #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 q9 r1 r# d, h5 c7 ^
- ceil(x)
8 T/ ?9 r& f7 | u$ ]1 C1 \7 H+ X) x, U - Return the ceiling of x as an int.
% T% a: h u4 U - This is the smallest integral value >= x.1 H) U8 @) y* m- c
- 2 d) `& l, @) d2 J
- >>> math.ceil(4.01)
* V* n5 O& R- i/ U% u - 5
9 J4 k# k( n* S$ D8 ~ - >>> math.ceil(4.99)
5 |" Y: z' B! k4 v( D/ A - 5
- O n3 b& W2 [. t( p - >>> math.ceil(-3.99)
; z. }* I9 d9 |4 H. n% b3 I0 a, w - -3
0 X5 K+ G; p9 @( N- ?0 s - >>> math.ceil(-3.01)
8 b+ p' `0 y D; x3 Y3 M' b5 m0 ] - -3
复制代码 1 ^3 x$ P. U- G" A; i1 m8 z3 H
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: `; t) S# v$ _: ], a! s- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ L+ U! I5 W. R; a; H& T/ j
- floor(x)$ M: l; x9 o( r$ o9 d+ o
- Return the floor of x as an int.
0 ~/ V; [ r9 b* ?+ c3 {0 Y- ~& X/ g - This is the largest integral value <= x.0 C, N% g0 d: l' s$ b0 R% D
- >>> math.floor(4.1)& m) d c) {- h( d* i5 U
- 4
4 C" Q7 B. J! ?/ L! m& d- x - >>> math.floor(4.999)
& X' ^! `3 k* S - 44 i: J2 {. c2 N* A* F5 U
- >>> math.floor(-4.999)' H5 B) x" q8 B! X. y
- -57 a z) G/ b3 E u
- >>> math.floor(-4.01)
7 B. |* B) n: p/ }9 p9 ` - -5
复制代码 4 O$ [( p; w6 P% W$ p
math.pow(x,y) 返回x的y次方,即x**y
9 L1 P, t7 {8 T, X2 t3 s- #返回x的y次方,即x**y
( b. }* R7 @" L$ c* Q - pow(x, y)- _/ l- G( D$ h; v
- Return x**y (x to the power of y).$ f; _& C; H, j
- >>> math.pow(3,4)
1 B" E0 T/ s/ I/ L - 81.0
8 f$ k0 F3 e8 \: P - >>> ! F3 y, b- c6 c! V0 |+ E
- >>> math.pow(2,7)
2 k& P5 u& ~1 w/ A - 128.0
复制代码
1 j) U* z3 R f/ y! t9 @math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& D. v A; X7 h6 W8 y; H- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 C7 h5 G0 O8 J T8 e; t - log(x[, base])
1 L s% y r. a8 g$ _ - Return the logarithm of x to the given base.
6 G5 c% F: _5 i - If the base not specified, returns the natural logarithm (base e) of x.
$ H( k' z8 J$ s F9 C" I$ {- p$ F - >>> math.log(10)
1 x: X4 V2 P& Q3 \ - 2.302585092994046- u' Q# q$ w) i+ C1 n7 ~1 X7 Z
- >>> math.log(11)
& s0 E: ]( Q. G - 2.3978952727983707
' K7 {- R: c/ J8 y: I- S8 H& d - >>> math.log(20)$ j( }, n% d) r6 r2 F* v' X; G
- 2.995732273553991
复制代码 + J) q5 T( }! E4 G; j8 N
math.sin(x) 求x(x为弧度)的正弦值 s% H9 t3 w% R
- #求x(x为弧度)的正弦值
" H- R& f% X7 [0 G! g$ V - sin(x)
( D4 w+ @& k4 j5 d - Return the sine of x (measured in radians).
3 d! z" Q5 e, F: P. s2 N - >>> math.sin(math.pi/4)
" D5 ~ a1 ]# n2 V& t7 }4 l& L - 0.7071067811865475
; U( {/ U8 g' J9 C; O8 {; r - >>> math.sin(math.pi/2)+ O7 ^! Q$ d' _ E( Y
- 1.0, i! G7 T" x: t
- >>> math.sin(math.pi/3)/ j/ A c5 ~' J" O7 y
- 0.8660254037844386
复制代码
3 e; C" d# m9 m. p: S! Z. ~math.cos(x) 求x的余弦,x必须是弧度
8 X P7 J! [/ d& Q/ W- #求x的余弦,x必须是弧度: z9 P9 Y& D: C5 r+ _4 c7 Y
- cos(x)9 O5 s4 z" W+ N7 W+ K; b, X
- Return the cosine of x (measured in radians).
1 J) O1 n! `! Y - #math.pi/4表示弧度,转换成角度为45度
2 x- j8 V; J; m) v' _ - >>> math.cos(math.pi/4)2 J* P, ]2 V1 N6 n0 Z
- 0.7071067811865476
( E& W, p7 b% ?+ V( C( z - math.pi/3表示弧度,转换成角度为60度) u0 V. X( C# [8 [- A
- >>> math.cos(math.pi/3)
5 g6 E7 a% K9 @4 x. q) I - 0.5000000000000001
. |+ q' N: e4 I) M o( q0 ~6 P - math.pi/6表示弧度,转换成角度为30度
" c" N) |' t/ G9 {$ V: B) ^ I: G - >>> math.cos(math.pi/6)) l+ W4 V5 S, Y
- 0.8660254037844387
复制代码
' Q* l& N# b* {) p6 r' |math.tan(x) 返回x(x为弧度)的正切值. t% j9 b( n; j& u# c& P- @; l
- #返回x(x为弧度)的正切值% T, G# \; k, d2 V3 W# V; X, O9 i
- tan(x); d- I, p6 Q2 O: F$ K
- Return the tangent of x (measured in radians).3 L+ P* s8 O; H, e
- >>> math.tan(math.pi/4)8 O8 j# Z# o! t' V6 T
- 0.9999999999999999
) z" c5 ^1 Q% V4 E& x - >>> math.tan(math.pi/6)
& X0 [& M' Z- Z3 R/ e$ [3 ~ - 0.5773502691896257 z* `& @7 f. y
- >>> math.tan(math.pi/3)
9 Y) L- L, A. _' [! {+ x* S# h - 1.7320508075688767
复制代码
' `- l$ v3 D- A1 u0 Rmath.degrees(x) 把x从弧度转换成角度
5 s7 N' ~/ w. @, D3 R8 q! |* K2 G- #把x从弧度转换成角度
2 V' ~4 |* n+ h - degrees(x) `! e* Y$ J4 H3 K% K
- Convert angle x from radians to degrees.2 R! ]6 h8 _& _+ ^) l
- 3 |+ r/ @: q! } a
- >>> math.degrees(math.pi/4)
+ c$ V$ w9 [9 l& `: H - 45.0. K$ I; U- A9 t O1 F" R2 o
- >>> math.degrees(math.pi)
! `+ y! p; S; |: e - 180.09 [! L: o5 ^6 y$ U
- >>> math.degrees(math.pi/6)
+ F/ J' F) y3 {# j6 T - 29.999999999999996
- C4 |( [2 w: y - >>> math.degrees(math.pi/3)
3 m4 s" X& \# }5 x2 k- d. ` - 59.99999999999999
复制代码
5 U. r! D( M7 emath.radians(x) 把角度x转换成弧度
; g) U3 P/ y3 H0 x6 C- #把角度x转换成弧度
9 s, l8 I5 R7 S! L! O! q - radians(x)
# p5 b6 @2 m! z, p# o7 @- C - Convert angle x from degrees to radians., x- r5 z" @4 l3 c1 p @& @8 s' u
- >>> math.radians(45)
7 [. I5 G9 w/ f+ L- P - 0.7853981633974483
3 k" ?- W; p# m: ? - >>> math.radians(60)
! T3 d7 E- i- W5 B$ j( T M; ^ | - 1.0471975511965976
复制代码 & S% ~. J* G3 E; s# [; w
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
$ Q+ N+ o: I$ f" D9 D6 F9 q- #把y的正负号加到x前面,可以使用0
. f9 y( Q; W) t( j4 k8 l - copysign(x, y)6 |, P a9 e) u* _
- Return a float with the magnitude (absolute value) of x but the sign
: F8 e k$ r2 P( Z - of y. On platforms that support signed zeros, copysign(1.0, -0.0) / D: ~! @9 F( g8 x9 j# x
- returns -1.0.) U2 l* u/ X- K8 _
" z, U& H: D' i8 p; m! _6 V4 o- >>> math.copysign(2,3)
( X A! P$ @* c y$ } - 2.0) X3 b2 t& @: `, e' d
- >>> math.copysign(2,-3)
/ Y1 m# m& Y+ e) e* Q6 i - -2.0
% |4 [( ?7 W" b( i5 n+ b - >>> math.copysign(3,8)
1 _: |4 }" J. i. C% U0 v& l - 3.0
3 e* b6 F, J3 n2 j - >>> math.copysign(3,-8)/ Y' q) m; a7 J+ U1 G# `7 e
- -3.0
复制代码
9 a- A# x) K; ?( amath.exp(x) 返回math.e,也就是2.71828的x次方9 J4 @1 i' g& S3 Q b# {& @
- #返回math.e,也就是2.71828的x次方
+ i% `1 m, | a; c6 } - exp(x): ~. Z; G$ x7 R
- Return e raised to the power of x.
' D1 ~) Y* Y4 j
/ ?' I& ^' @$ x: r: L7 ^- >>> math.exp(1)+ E" R+ S* H6 V# W" g6 `
- 2.718281828459045
O# \; W+ o! _! E1 p' z - >>> math.exp(2)
0 q$ d; h H8 x - 7.38905609893065
+ P! r% D, r! Y$ D - >>> math.exp(3)
* T# @% K4 p1 l6 H - 20.085536923187668
复制代码 / O% s5 J! ]5 f( [
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1# g* s5 r( T! {2 {
- #返回math.e的x(其值为2.71828)次方的值减1+ w, F! ^( `1 ^5 P3 X
- expm1(x)
) l5 a6 O s7 W# M, \- X( g - Return exp(x)-1.
; ~$ |) |8 ^) g/ l( e - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
3 g2 c0 q# q! q9 S. @- S - 2 b- n* l5 S; [% g
- >>> math.expm1(1)
7 Z+ z6 a) u# g; V& _) z& ^# c9 @8 L - 1.718281828459045* B4 g; g* e6 F2 J+ x4 ~7 O
- >>> math.expm1(2)
* _! I/ p8 ^* d - 6.38905609893065
* A0 K5 T) y5 T2 K - >>> math.expm1(3): t5 I5 ?) E$ `: _
- 19.085536923187668
复制代码 6 ~$ `) y1 |, z
math.fabs(x) 返回x的绝对值
2 k5 E4 s2 L3 S7 W, ]6 x- #返回x的绝对值( K) ]( Z. n8 |
- fabs(x)8 V; Q0 ?- _2 S4 T! g
- Return the absolute value of the float x.
7 u% M, d: F% Q! L0 U$ a' {
" R% e' D: J1 Y9 h& c- >>> math.fabs(-0.003)6 B: d% Z% p% s9 j d; w
- 0.003
! l2 c, k6 ?' A& ]9 c - >>> math.fabs(-110)3 M1 d2 G1 v' U0 O/ o3 ]* |
- 110.0. E2 z- Q# J* |# l: ^
- >>> math.fabs(100)
1 d, v# ?' r$ K; { - 100.0
复制代码 3 w9 y `. s# ]1 _9 P& q5 w% s7 |
math.factorial(x) 取x的阶乘的值& R# p: h* |* J$ _' Y
- #取x的阶乘的值
) Y3 N2 r* n" z - factorial(x) -> Integral+ Y) {" i) r* E) ?7 ]" m6 D% U
- Find x!. Raise a ValueError if x is negative or non-integral.
( P3 e- B! ?3 d1 |( G* U5 s - >>> math.factorial(1)
+ ]0 C3 a) ?( q+ p+ S8 V - 1
/ J8 ~$ r3 F% k5 u - >>> math.factorial(2)
- X, P+ S n; N6 K - 2' s) [6 D7 r2 U& p
- >>> math.factorial(3)! k0 g. b# h4 V$ `. @! L% N
- 62 o/ o5 z z0 A) c) A( H
- >>> math.factorial(5)# v8 |' I9 d; L2 K7 ^6 P4 L
- 120' u8 e# T% q7 p1 K0 f0 |. }* `
- >>> math.factorial(10)
2 b; T: C9 N' ^3 e/ t J5 ? - 3628800
复制代码 ; |. `. Z. d d% c
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
8 L% q0 e- h0 @/ g- #得到x/y的余数,其值是一个浮点数
5 C# b, L- t# L. P - fmod(x, y)( P! a( M1 z5 b. a# e+ M$ }' ~0 K
- Return fmod(x, y), according to platform C. x % y may differ.
4 F |4 Y) J+ { - >>> math.fmod(20,3)" s5 l1 ?) s1 N
- 2.0( B% b, z9 Q) h6 P; |" V7 d
- >>> math.fmod(20,7)
0 ^4 g% F, B1 S( {4 C4 N - 6.0
复制代码 9 q5 p& \0 {$ _. e! t- c. p
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: o) X+ L7 X3 P0 A. L# U, Z# I- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,7 x& P# ^- w: S2 L
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' \$ ?8 E$ u* t0 I' \" F7 w
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
' }1 [. K+ u$ S. M: u - frexp(x)
% r- w- \- _# ]: q& i - Return the mantissa and exponent of x, as pair (m, e).
- x+ c" z! @, B: w' D4 }# _ - m is a float and e is an int, such that x = m * 2.**e.
/ C% j9 R) n# l/ x- ~* R - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
) Q3 M8 R; p: ` F! k" m - >>> math.frexp(10)
' L6 [+ Q0 b( i1 i$ T$ J' j/ t+ k5 K - (0.625, 4)
- |3 F J5 B: ~# j - >>> math.frexp(75)
- P/ B. B% p* U+ N - (0.5859375, 7)+ r+ b: |1 E7 N8 T
- >>> math.frexp(-40)
/ n( \8 S& ]/ H, W - (-0.625, 6)3 Z2 |4 b) o- L& q% X" x9 O! E: s
- >>> math.frexp(-100)
0 g9 i' o" a( ?6 X/ s; m8 A - (-0.78125, 7)
5 f$ Q* b* ^, d! a - >>> math.frexp(100)2 y- E$ v9 J1 I& _ j# s! _) E3 b: V
- (0.78125, 7)
复制代码 & C4 u( {% s6 `) o
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& Z9 }: l/ `' F4 N5 I2 E4 y- #对迭代器里的每个元素进行求和操作9 m; \$ ?5 o% i" X) B8 e. S
- fsum(iterable)3 Q0 E( J V" F# @) w
- Return an accurate floating point sum of values in the iterable.
* s: b/ l4 n' M: c: x, ^/ P1 j - Assumes IEEE-754 floating point arithmetic.# j# s( { U) Z2 Y* J
- >>> math.fsum([1,2,3,4])
% w2 n/ p5 h9 z) M* s - 10.0
& ?& _' H7 \. [3 X K - >>> math.fsum((1,2,3,4))0 ~9 S2 |/ b4 w
- 10.0! h, F' [7 Z1 M
- >>> math.fsum((-1,-2,-3,-4))
) P0 [- v6 Y; }5 |8 k/ ] - -10.0% X) H& L' T$ d) [3 V
- >>> math.fsum([-1,-2,-3,-4])
! I; {7 H& p3 m- B - -10.0
复制代码 ; ?! L. N8 W( w( i) t
math.gcd(x,y) 返回x和y的最大公约数4 Y `4 ] Y7 }& h' J9 P
- #返回x和y的最大公约数& \; e& ~$ _2 |+ [
- gcd(x, y) -> int8 P( @2 J) l& B# i0 T. y8 D
- greatest common divisor of x and y
; a( v# N7 p" {; o2 l1 Q: U M( M1 W - >>> math.gcd(8,6)$ N# R- Z( _ C, u& O( l
- 2; C/ @& v, ?5 }
- >>> math.gcd(40,20) K" N# M, G4 g. T3 {* A) }
- 20
. D- I# R, d* x- ]/ p4 ^ - >>> math.gcd(8,12)
: e& [& ^3 p8 n$ Y9 e/ Q ~ - 4
复制代码 & D# L8 E# g. r Y: y5 y
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False b* j. M6 p% J
- #得到(x**2+y**2),平方的值
/ L- e" e+ t1 f% L7 ` - hypot(x, y)) n$ }; y u/ F/ a V! i8 T
- Return the Euclidean distance, sqrt(x*x + y*y).
5 S2 ?5 O9 Z6 ?$ ?% J8 S) g - >>> math.hypot(3,4)5 ?0 ?* @+ V1 i7 O- I" n
- 5.0
( C4 ]! ^- s: v - >>> math.hypot(6,8)
. s; Y! I _ C+ T ] f - 10.0
复制代码
* W% O k. |% B7 u& N4 a% rmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ z+ Z* N6 u# @& V3 }4 t- #如果x是不是无穷大的数字,则返回True,否则返回False' n1 }# Y4 ]6 W' D7 ?
- isfinite(x) -> bool
2 U8 S4 s: N- N! F) w9 T. O - Return True if x is neither an infinity nor a NaN, and False otherwise.
# ?3 L7 Z$ T2 G: N C - >>> math.isfinite(100)6 E" \* w8 ~: ?3 c
- True
& K8 j; s& f* h% _ G& R+ W1 `' D! I - >>> math.isfinite(0)! ^. I- o; g0 {* }+ }- y$ i8 k
- True% u4 _+ e# I% g( \1 T! ~
- >>> math.isfinite(0.1)
8 e' L/ `& I4 ?* \ - True
# D$ q. Q" a0 B; _8 X) T+ S - >>> math.isfinite("a")
1 R' k+ v" i& j' U0 m - >>> math.isfinite(0.0001)# `& A3 [8 N+ {5 g
- True
复制代码 ) q8 X/ o" C& D8 x U. y7 r
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
# l9 p' ^0 W4 g) H- #如果x是正无穷大或负无穷大,则返回True,否则返回False
; G- F9 O" s) l; ?+ W- T% |9 K0 ? - isinf(x) -> bool
& ]; |9 x. v1 X) a9 T( w% L - Return True if x is a positive or negative infinity, and False otherwise./ A( [) q: W8 U4 O/ p
- >>> math.isinf(234)8 j- V- c+ i! m! \# O8 o0 E3 q
- False
7 V- F6 \2 R# y - >>> math.isinf(0.1)
. k' @+ R" n; d9 @ - False
复制代码 + N3 V4 k1 ~' D8 P+ Q+ Y: ?
math.isnan(x) 如果x不是数字True,否则返回False6 q. z0 }. I# v
- #如果x不是数字True,否则返回False6 S1 D& l' P- [; g4 R
- isnan(x) -> bool- A, i3 r/ r! D( |' G7 y
- Return True if x is a NaN (not a number), and False otherwise., i7 v$ ]; ?, o% l
- >>> math.isnan(23)
3 Q+ ^2 }" o( j6 u/ O! y - False, d: O9 c0 O- h# V
- >>> math.isnan(0.01)/ f% l: Y/ R. j
- False
复制代码
a4 [, t( {% r0 p% k5 u) W( L' ymath.ldexp(x,i) 返回x*(2**i)的值. }8 X! h9 R# S8 g# {
- #返回x*(2**i)的值5 g& Y. Q0 R0 @$ J3 X
- ldexp(x, i)3 |$ y3 \- J+ N3 z) N8 h6 t! D
- Return x * (2**i).
+ C. ~ R$ t0 Z - >>> math.ldexp(5,5)
7 `, a* P6 L$ r9 v* p - 160.02 d8 N o7 P. d |) ^
- >>> math.ldexp(3,5)+ w# S9 c* y! Z: M* z+ b
- 96.0
复制代码 & y q- O5 G. m/ q# @
math.log10(x) 返回x的以10为底的对数. q5 a4 Z! h; d
- #返回x的以10为底的对数- G$ @: _$ `4 L4 M
- log10(x)
3 ]. \0 o4 }0 v - Return the base 10 logarithm of x.% y( k& G i- G1 f
- >>> math.log10(10)
( r7 t! D$ l# \1 ^/ k- @5 b0 g - 1.0+ ~ I3 I: A& `5 d6 A% I
- >>> math.log10(100)
% ]5 h" W! Q4 h' H2 E - 2.04 o9 o& i" D3 u; }9 p2 Q
- #即10的1.3次方的结果为20* ]4 Z7 T1 Q4 P
- >>> math.log10(20)
3 [- X. m3 N. P3 Q! B9 m - 1.3010299956639813
复制代码 , F" @; d% H7 U5 v, E( [: C& Q
math.log1p(x) 返回x+1的自然对数(基数为e)的值
" V3 a$ d G9 i, g* W! p6 I- #返回x+1的自然对数(基数为e)的值
0 Y0 Z3 F( v" @/ ~- T" X. w - log1p(x)
+ t( ?- v U9 i; l5 V - Return the natural logarithm of 1+x (base e).
% Q( v( s; F) U! y+ C3 z" v - The result is computed in a way which is accurate for x near zero.% N" t/ ^$ U! }* ?( e# W$ h0 g
- >>> math.log(10)$ B) h7 J' d- a$ N- x0 I' P3 }
- 2.302585092994046
0 ~/ r) _# Y5 j9 [& G7 } - >>> math.log1p(10)
% s4 y/ n) Y4 p7 F1 J - 2.3978952727983707; y7 `5 E! n, @. \4 L8 U
- >>> math.log(11)
- O( }' C# S' ^$ W - 2.3978952727983707
复制代码
- f) @+ _, t# K$ M6 z) umath.log2(x) 返回x的基2对数( B' P% ?6 @" S# _
- #返回x的基2对数& y3 J0 X8 a+ t' J
- log2(x)
( p3 L- m; W: Z - Return the base 2 logarithm of x.
}" y" t+ H4 s) t - >>> math.log2(32); C$ v( L z" o$ d" ]/ a3 G
- 5.0. J4 k0 j, _* g- x/ }! @* C
- >>> math.log2(20)' H. z" c8 |' ?
- 4.321928094887363' n# X: A" a9 |( |' T
- >>> math.log2(16)
9 b, W! J; {1 y r4 j- r* F - 4.0
复制代码 ' a0 Q U! i) e; ] U) @: G- W
math.modf(x) 返回由x的小数部分和整数部分组成的元组
# s. d0 {( U! z( S1 o- #返回由x的小数部分和整数部分组成的元组
1 [9 X" H' f4 b0 u. k7 k' l - modf(x)
: `9 @' R3 U |" N/ h$ b - Return the fractional and integer parts of x. Both results carry the sign% i- z; D% X0 ^8 C. y
- of x and are floats.
) {0 H# B% S# h$ \ - >>> math.modf(math.pi)
# u+ C( R0 e! B. u, l0 R' i' a, ?, ~ - (0.14159265358979312, 3.0)
( o0 S7 i* {. ~2 Y - >>> math.modf(12.34): h0 G3 K, x6 v& Y' ^: t
- (0.33999999999999986, 12.0)
复制代码 9 }$ b& j* `" Y+ m
math.sqrt(x) 求x的平方根# l; j2 a8 c& S% { Q/ C
- #求x的平方根; ?) B. c* T0 j1 D
- sqrt(x): X: r' C; U3 A* I
- Return the square root of x.
$ I9 ?# R1 h" ~4 m - >>> math.sqrt(100)
. `+ k# ?8 \' [) d - 10.06 F. s- Q4 _, w6 S3 ~; x0 t5 c# N# c
- >>> math.sqrt(16)
# m4 d6 u1 J) K - 4.0
5 Z1 _' N6 t$ @* L* C9 C - >>> math.sqrt(20)
7 ]7 X$ c: Z" G4 g9 { - 4.47213595499958
复制代码 ) A1 i& @1 N0 `1 m# w9 `
math.trunc(x) 返回x的整数部分- #返回x的整数部分
4 I& d. [# C: J0 H0 \. n& j I4 I* ` - trunc(x:Real) -> Integral# V- V/ c, E5 i+ V
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% N! j" @" u5 |7 a% P2 W
- >>> math.trunc(6.789)+ l# m& |! L x1 j4 ?$ K, N4 s/ U
- 6' M9 Y- V$ M1 x6 r$ M2 o5 m: h9 x4 z
- >>> math.trunc(math.pi)7 Y/ d" I5 [! h; U y
- 35 o) a( r" P' E5 {9 G, b& X7 m
- >>> math.trunc(2.567), V, F( i7 U) O% L- _/ Y( R, x
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|