马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 d5 d# q( ]) w! J
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
8 Q3 @) }5 A* H3 h" J' r v0 _+ Z- e
方法1:6 c, O. e* Y! @0 [! Z& B0 _
- >>> import math$ F* R2 c$ i/ W6 f* y
- >>> math.sqrt(9)* M: s* e2 O& Y, f3 [. J
- 3.0
复制代码 方法2:. S* T0 k- U. ]7 p; d; |9 N
- >>> from math import sqrt: D$ @1 p7 d# }3 c/ b$ t4 Y. F& s
- >>> sqrt(9)" w- A. Q. b' P1 j/ ~+ |* P
- 3.0
复制代码 ; Z! W% m7 K' o& d, }
) V' F. u; e9 F( p% T2 {# r
math.e 表示一个常量) Q# n% N/ l; |
- #表示一个常量$ r J* j. q3 v8 m
- >>> math.e
) U0 B S/ t) g5 U7 m- X& i - 2.718281828459045
复制代码 ; C9 W6 {! O' A8 U' E" w
math.pi 数字常量,圆周率
3 u! J% z+ D [+ C; O( f7 x% k' {* j1 z- #数字常量,圆周率; e# ?) H# D M4 Z% q. G3 `
- >>> print(math.pi)
8 t9 _( D% a) l - 3.141592653589793
复制代码 3 ~5 x; p, l! N- u* [
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x( `% e! n% ^' W% w% l
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 X9 g" P8 r2 t/ ^
- ceil(x)0 t o8 c$ c X
- Return the ceiling of x as an int.* e8 n( \, Z4 A* A+ U: R
- This is the smallest integral value >= x.% I+ Y+ _# o* K- p( U
4 J/ M5 S$ [" S* D, z% }- >>> math.ceil(4.01)( i. i8 O. x. W Y% K# q3 f7 z
- 5
5 h5 l2 X# N) {. D6 q" q5 d( c - >>> math.ceil(4.99)
$ Q2 H4 ]0 _4 h" J0 w; Q+ { - 5; y2 [8 O+ A0 u; W, k* J
- >>> math.ceil(-3.99)) L% p8 P; y$ B
- -3/ q. S7 z$ S" p
- >>> math.ceil(-3.01), y$ C& f+ A5 h5 I7 L) M( w# t9 i
- -3
复制代码 * U; D& j$ d: v& P( _2 ~+ @
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 }+ q" F( M* M4 i7 e# V* A- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 f1 u5 v2 N/ ]7 P8 b! Z - floor(x)/ f& ^7 r/ r3 n: I
- Return the floor of x as an int.
3 _ B; a' K9 Q ? - This is the largest integral value <= x.
- E, m, q9 _& @9 r g - >>> math.floor(4.1)
% ?: ?: k8 z# f6 Y - 4
# _( f" z3 E0 _. F - >>> math.floor(4.999)
f! d" T9 [, {$ f - 41 I) R5 U2 c! R+ |; B
- >>> math.floor(-4.999)
" R% }9 }5 G* L - -5
/ A! m! d1 U( K1 ]) T. m - >>> math.floor(-4.01)0 B! [& B/ M; J% X. Y
- -5
复制代码 d# C5 w& S) p" t
math.pow(x,y) 返回x的y次方,即x**y
* `7 \' _" Q, l- #返回x的y次方,即x**y0 O4 a* U) B; V# t, c
- pow(x, y)
$ {+ x( ~$ f% @ - Return x**y (x to the power of y).
8 ^# e5 r0 i1 c" L: U - >>> math.pow(3,4)5 y) b! R$ w) X9 q7 p
- 81.0
0 X; N; |7 {0 m" N. R2 S- I" F - >>> / |" b3 M; G" M3 l, E: p
- >>> math.pow(2,7)! i& d: @2 u$ s( w
- 128.0
复制代码
5 M% K. c( h2 R5 R$ vmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# Y; E* P- ^7 V A- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 p+ `; X2 k3 A5 ~: c( A
- log(x[, base])
9 ?. z; ]2 s; T G - Return the logarithm of x to the given base.
- F& C. C, j* Q! \7 s. k, H - If the base not specified, returns the natural logarithm (base e) of x.$ F3 q1 O @ L
- >>> math.log(10)3 Y- @$ ]3 v" A. Z( i
- 2.3025850929940461 A+ W1 @$ j+ a( V6 ~
- >>> math.log(11)0 V! F$ k2 L" a. q7 H0 x- @1 U
- 2.3978952727983707; B0 F ]7 S; W) d7 D+ ~5 g( L# r
- >>> math.log(20); U1 M: \+ \- `) P% ~: a
- 2.995732273553991
复制代码 " @. J- z: e8 G
math.sin(x) 求x(x为弧度)的正弦值
" c$ x) ?8 H4 a( W! b8 Y% f j- #求x(x为弧度)的正弦值1 Q# Y" p- X; f1 N* t: s
- sin(x)4 V$ Q% h1 L3 b$ F- O, Q
- Return the sine of x (measured in radians).8 Z$ M( F. ?" B
- >>> math.sin(math.pi/4)
1 Q2 e/ C+ C/ M* R. S. B - 0.7071067811865475
- }4 f6 V1 P5 K" p( ~0 J+ i4 m5 A - >>> math.sin(math.pi/2)) s" l6 T% A+ {& A
- 1.04 g+ r' P) e* Z1 V2 I
- >>> math.sin(math.pi/3)! A; ~5 f4 S: I' Q* t, `
- 0.8660254037844386
复制代码 5 V' l6 k! h' `8 R& b0 z
math.cos(x) 求x的余弦,x必须是弧度' D' B2 F4 N f2 H% Q
- #求x的余弦,x必须是弧度$ D8 y' w$ y1 }9 L8 ^2 M4 E. R6 i/ r& r9 ~
- cos(x)6 B) Y$ @2 J5 }' q6 H! v# h
- Return the cosine of x (measured in radians).1 p4 \* N+ P6 b* s
- #math.pi/4表示弧度,转换成角度为45度( z! V- ]" g* z) x f- k
- >>> math.cos(math.pi/4)4 V9 C. S7 [9 E5 }
- 0.70710678118654766 H7 H' P5 d- j: a" Z1 \/ x( J# M+ r
- math.pi/3表示弧度,转换成角度为60度8 O& o' k5 D' s* L! X
- >>> math.cos(math.pi/3)
2 y& h6 V* x& M( {- G4 T - 0.5000000000000001
9 o K9 u9 x" V5 H/ a7 t* }. F - math.pi/6表示弧度,转换成角度为30度
X& W. X2 Q" \ ? - >>> math.cos(math.pi/6)
" U2 p7 R- b+ Q1 w a. u - 0.8660254037844387
复制代码
) L3 S' I- N+ r) Y1 [math.tan(x) 返回x(x为弧度)的正切值 T% N j! ?' Y5 [" W
- #返回x(x为弧度)的正切值
+ e$ X% Z6 |# L+ J - tan(x), `) ?3 d6 }) M8 n1 E4 R q2 K
- Return the tangent of x (measured in radians).
& a. I4 ~* I+ e5 d( a1 x5 \ - >>> math.tan(math.pi/4)
3 A( C3 Y) v% ?% D- r - 0.9999999999999999
; O \0 d$ [1 _) O- I# a - >>> math.tan(math.pi/6)
) M7 G& X5 f1 R! T) B! ?; @ - 0.5773502691896257
+ W7 [( y# \7 U- F8 Q& s - >>> math.tan(math.pi/3)
, R! B# L. Z& T/ m - 1.7320508075688767
复制代码
( m, Z9 } c) {( i6 nmath.degrees(x) 把x从弧度转换成角度
' Q( u U8 j: A5 ]7 U/ H- #把x从弧度转换成角度
$ N) |8 i. _1 r9 @2 N' [- G - degrees(x)4 Z$ h, N( j: F. B
- Convert angle x from radians to degrees.
9 J9 h' P D9 o p4 z
0 [+ ]5 Y7 N) j( J$ ~- >>> math.degrees(math.pi/4)7 Q5 l* ]0 d! I4 K3 P1 @+ w( f
- 45.0
9 S& @# S, p a6 Z - >>> math.degrees(math.pi)* M6 r, U- W/ Q2 s4 `" o+ i7 h- c
- 180.0
3 c) V3 N& ]& A6 c2 G! H - >>> math.degrees(math.pi/6)5 a7 e0 X" G* z) o$ l
- 29.999999999999996
+ O8 O2 X$ ]2 \) A2 w7 ? _% u4 ^ - >>> math.degrees(math.pi/3) a& E/ Y) D9 n8 M# @% q; Q/ I
- 59.99999999999999
复制代码 ' b5 b. {% S) V
math.radians(x) 把角度x转换成弧度: U- u' p3 C m4 y( M
- #把角度x转换成弧度5 Q1 K! w. z, i" q. j% J
- radians(x)2 c4 k1 c- e' r. ?3 N m) Q0 T9 a! C
- Convert angle x from degrees to radians.
1 ]( a8 t/ t2 I4 _1 I* ? - >>> math.radians(45) G. w4 F0 h* t+ z5 M( m
- 0.7853981633974483* N% ~' v8 T% d* p& B) k- t3 ]
- >>> math.radians(60)
/ N' o( E$ J% l0 H, i - 1.0471975511965976
复制代码 * h( z+ Q; a3 \) b# z) p% t( I" V/ x
math.copysign(x,y) 把y的正负号加到x前面,可以使用08 h2 d2 I$ j# q" m( C ^" P: M
- #把y的正负号加到x前面,可以使用08 j% ^- @4 J) p" n6 V
- copysign(x, y), B! K( Q. z. d! t$ K0 i: a
- Return a float with the magnitude (absolute value) of x but the sign
" z$ k: F/ T3 O - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
! y" J. V) s- f9 H& `. ]! ~& m6 ? - returns -1.0.# v6 e+ k& Y, H$ E [4 M
- " y9 F# j2 ^5 r
- >>> math.copysign(2,3)- `7 I2 w& A' H9 z7 k& \" D
- 2.0
. A! Q6 B/ l$ G1 j - >>> math.copysign(2,-3)* j! V8 o' U! x v' R: u
- -2.0
, N, R- Z5 j3 I& a - >>> math.copysign(3,8)& a4 f2 O( n; i+ u
- 3.0/ O: ]6 s6 o- r A5 }
- >>> math.copysign(3,-8)
1 ^ T7 }4 A5 ~5 y- e! x2 i. H - -3.0
复制代码
& ~ C4 }; i+ Amath.exp(x) 返回math.e,也就是2.71828的x次方
2 X# B ] ^ J! w' \& N$ P( d. Y7 v- #返回math.e,也就是2.71828的x次方* }$ p) d4 a6 X6 Q# v+ A9 ^8 R
- exp(x)' K" I ~$ J- Z
- Return e raised to the power of x.+ |, W$ e w8 u
: b8 P6 h% n9 z6 \% C- >>> math.exp(1)
2 I" R6 c( K* j& `+ q( ]2 }# v - 2.718281828459045% \% a5 I+ w8 e3 h5 t
- >>> math.exp(2)
: {- c& d, n: N. `& Q* N/ q - 7.38905609893065* H; K, K4 `+ g# s
- >>> math.exp(3), `5 [; k6 i3 u3 ~
- 20.085536923187668
复制代码
8 L5 B0 M# m) o* ?9 Ymath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 K7 K/ N4 Y# u6 z* [0 o
- #返回math.e的x(其值为2.71828)次方的值减1+ g/ C# z4 \ d1 c+ q9 l+ L
- expm1(x)
9 T# m7 @& @+ n$ ?' P- ] - Return exp(x)-1.
" J( z5 Q* e6 f* J+ W - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.- z! h7 G0 R0 l1 U* E
- 5 F( B f$ P) n1 w3 |+ q+ P2 ~
- >>> math.expm1(1)
% O$ ^# [. j$ U/ m4 j' u; A - 1.718281828459045
5 {( I& |% A f) N+ M) _ - >>> math.expm1(2)$ O, {, o0 T6 p3 Z* d2 k, H7 \$ }
- 6.389056098930655 s, G9 O1 l% z3 P
- >>> math.expm1(3)
7 J& D7 }2 K7 b8 C - 19.085536923187668
复制代码 " ?4 U6 {$ ]' n# f' a$ v6 k
math.fabs(x) 返回x的绝对值
0 ], Q4 }4 P. ?- #返回x的绝对值
; y1 o( e0 i5 \# e* o - fabs(x)
' S# ]" @2 ^, W, g& r& ^( J - Return the absolute value of the float x.3 c3 X5 A4 F. X" w- h
- / b3 H' @3 i8 k9 n6 V5 H8 O3 g9 S# J
- >>> math.fabs(-0.003)
3 \( y' t' r1 t! u) } - 0.003
' s0 ~8 }( t: S: w' s1 s) t! I& U - >>> math.fabs(-110)
/ Q F8 ]* w6 } \# o8 `) b' E/ a - 110.05 Q" a( r% D9 }' a# ~4 M( e
- >>> math.fabs(100)
7 B" Z; ~/ A Z8 \+ H - 100.0
复制代码 # x- w9 N3 P- R8 e# l" b5 O
math.factorial(x) 取x的阶乘的值" g- t ~9 H8 k
- #取x的阶乘的值
9 I5 D* Y- M7 ? - factorial(x) -> Integral, m1 p5 \( F5 B8 A
- Find x!. Raise a ValueError if x is negative or non-integral.
6 o9 X1 v; F( `( `: m0 m - >>> math.factorial(1)
: C; A+ k: m* G. D6 Q, x - 1
/ ]0 w+ ]( Q) j$ f8 N2 d @ - >>> math.factorial(2)' _& A) o% a+ ?8 E& T
- 2
1 j6 D# \3 R" T t2 U5 R ~" E. h' J" q - >>> math.factorial(3)# x1 y) A$ ]' h3 q. J7 a# w
- 6% T0 p; h8 Z; v) ~8 k* \
- >>> math.factorial(5)* [% t M9 }; i3 s) p3 U2 t
- 120
]/ ?4 y0 {& s0 [" E% V/ @ - >>> math.factorial(10)
+ B! P5 F# l) I% L7 V( f; x - 3628800
复制代码 + l$ n* C3 H! A3 S3 G" ?% N0 ?
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
- a9 w! `6 L2 u2 ^- #得到x/y的余数,其值是一个浮点数0 k+ A8 m: M1 w( {2 L
- fmod(x, y)
i1 W3 Y2 |& i! T; f - Return fmod(x, y), according to platform C. x % y may differ.9 ~" D$ d- N( b
- >>> math.fmod(20,3)
+ ~1 c# E+ G8 ?5 P3 ]+ M - 2.0
) T4 w4 ^+ e9 ~( g2 \ - >>> math.fmod(20,7)* |2 F5 o! ]8 o9 e: u& n$ P3 _
- 6.0
复制代码
! d0 }" `% i$ b! Kmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围* M' _* g* p9 D
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,8 V5 B x0 U" ~$ M
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: I' t6 `& d' Y0 Y, J5 k, U
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
: b4 _3 B; ~; v8 A - frexp(x); f# j8 z' S9 p6 ` f
- Return the mantissa and exponent of x, as pair (m, e).6 R4 |+ ~" m- ]7 h' B$ c; I
- m is a float and e is an int, such that x = m * 2.**e.
; b$ t5 R$ R* x& E# g - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
, F8 s- ]8 P! x - >>> math.frexp(10)
7 I6 {) ~/ }. I4 J' E* j1 ` - (0.625, 4)
# \+ C6 a* g; X. M# o8 }+ j# B - >>> math.frexp(75)0 Z0 c, V$ t" I: v. Z m; n( N. F% T
- (0.5859375, 7)( i8 A* }4 }4 o: `5 u/ W" J' k* [
- >>> math.frexp(-40)
0 C* U/ L- H7 V$ V1 e - (-0.625, 6)
3 v" f+ y( K' U - >>> math.frexp(-100)# A/ Q4 @6 ]$ M4 {3 i$ I
- (-0.78125, 7), [, H7 p7 n! H3 e! x$ k
- >>> math.frexp(100)3 }0 J3 g+ t" A0 f" ~
- (0.78125, 7)
复制代码 ! q9 i) ~- }& s' S) X5 @6 I
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列); S- [4 v$ j. Z
- #对迭代器里的每个元素进行求和操作
f$ N) [/ Q R$ Q - fsum(iterable)( L8 F! g0 s" K, t, j
- Return an accurate floating point sum of values in the iterable.
7 c; c! C& ? V/ ~1 O* V1 r9 R. u - Assumes IEEE-754 floating point arithmetic.5 N6 l5 P1 _% }7 K
- >>> math.fsum([1,2,3,4])
) s5 j& }, a" ~8 d6 f$ k7 ? - 10.0
, B* h3 L) Q" T4 H! F2 w, k - >>> math.fsum((1,2,3,4))# _ Z, C& w. s! ?6 Y' v' _; J' o
- 10.0
" R2 h) F6 s k/ R - >>> math.fsum((-1,-2,-3,-4)), [# b l3 B! X/ w4 f' t, {$ {
- -10.0( j7 |' @0 ]5 _; O0 L
- >>> math.fsum([-1,-2,-3,-4])7 ?$ b7 b4 r5 _
- -10.0
复制代码 # {$ B# w- D: X$ i7 f% l
math.gcd(x,y) 返回x和y的最大公约数
/ z) ]' p: E U8 h" [+ t9 I# i- #返回x和y的最大公约数4 p" `6 Y5 F2 Q9 z5 N
- gcd(x, y) -> int
2 k9 \( p; W% u3 Q, {0 ? g( { - greatest common divisor of x and y
% [& K3 L" f2 q0 R: \- [) Q* v* H - >>> math.gcd(8,6)
6 `% @6 q( _6 _ - 2
& y4 ]6 }$ I5 b - >>> math.gcd(40,20)# O6 y/ q4 [- B, A8 Y
- 20/ L( Q" H) }* _* |
- >>> math.gcd(8,12)
7 ~2 g* n+ T9 ?# a1 N- a - 4
复制代码
3 [. b& e" i7 a$ \: T& Cmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* \2 Z4 h, }+ x6 P9 D7 b- #得到(x**2+y**2),平方的值
/ U2 ?, S8 Y( d0 s$ o+ ~1 F - hypot(x, y)! o; Y9 D* D/ i% ]3 C9 v/ Q( t
- Return the Euclidean distance, sqrt(x*x + y*y).
4 u7 y4 l) t/ j7 J% W - >>> math.hypot(3,4)
4 R, q7 ]2 P, r$ n6 Y u, T( U0 ^ - 5.0& k+ J0 r0 y5 z N+ z( S5 [5 h0 F S
- >>> math.hypot(6,8)
' _$ f( ~9 d6 O5 E) C; e - 10.0
复制代码 - j& x/ V) h. c/ L1 r2 i
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 B) x) o6 ^/ E! J/ V- #如果x是不是无穷大的数字,则返回True,否则返回False- r& c" r1 g- y9 r1 e/ B& T" C% {
- isfinite(x) -> bool
( K7 H/ x9 M( B; {$ W - Return True if x is neither an infinity nor a NaN, and False otherwise.
0 B$ H/ k0 n+ S( G; E$ V4 N - >>> math.isfinite(100)
7 y6 V, \2 K7 [" o - True
0 C- D2 d* c' H7 z; j7 c/ Q6 Q( i - >>> math.isfinite(0)9 M1 O; |* ^$ U r! V) h
- True) |- {$ F {# Q( N
- >>> math.isfinite(0.1)! r3 I! E9 U, w9 H" C. ^
- True! G) K+ p' w4 s
- >>> math.isfinite("a")
1 @ Q T2 O9 R/ s; F$ {, H - >>> math.isfinite(0.0001)% \, I+ l# r8 m7 `. p
- True
复制代码 1 f; n5 P+ y1 t: ]' W5 k
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! C p* i7 `0 W# C- #如果x是正无穷大或负无穷大,则返回True,否则返回False0 a" ~, c, ^2 s1 r% e
- isinf(x) -> bool
) D) U) ?* Y4 p/ @2 k - Return True if x is a positive or negative infinity, and False otherwise.# b2 D, j" | E8 k1 q
- >>> math.isinf(234)
# y' ~) p9 ~ { - False
0 @5 x4 v3 a& Y! Q( j - >>> math.isinf(0.1): q7 K q; V6 E K- ~8 N
- False
复制代码 3 X3 V, w. K, L8 }- ^% K6 t, r
math.isnan(x) 如果x不是数字True,否则返回False
# l/ r% i1 ]2 [6 w' J6 ~- #如果x不是数字True,否则返回False
, y% g* g$ \( q, b - isnan(x) -> bool$ F! s$ l% `1 Y6 ]- G
- Return True if x is a NaN (not a number), and False otherwise.
0 T" w* A6 c" c - >>> math.isnan(23)/ B/ W6 r" ^( o: Z! d* m# j
- False3 O7 {1 h# n$ j( _/ `
- >>> math.isnan(0.01)
7 i& ]5 H3 c' c/ E* R - False
复制代码 0 }, W" m( T0 d
math.ldexp(x,i) 返回x*(2**i)的值0 N' Q# F, H h( k
- #返回x*(2**i)的值
8 l7 Z' g1 Y4 A- \ - ldexp(x, i)* J, y* s1 `7 n8 y3 L
- Return x * (2**i).; @7 c- s) M" ^% _! d1 Y
- >>> math.ldexp(5,5)
7 t7 T4 U: P, P; r3 g5 ]% n* L- ^( Z: n - 160.0
, _# e% a' O! ^; [1 A - >>> math.ldexp(3,5)
5 v; a t" N4 m6 H, O - 96.0
复制代码
& z8 p' `! ^, p% ?6 f4 _( smath.log10(x) 返回x的以10为底的对数 @. z% R9 Z6 B- Y7 ]
- #返回x的以10为底的对数
2 H& J s5 F# C4 A" a7 E. ?6 Q% N, u - log10(x)
: p- h4 i4 A, ~. i$ H0 E6 s1 u - Return the base 10 logarithm of x.
2 G# c/ B5 {- [1 Z. a9 D - >>> math.log10(10)
5 p" s- `" ]% Z- `( w, e" \3 N- h - 1.0
1 Z) Q# i7 y2 n - >>> math.log10(100)
3 Y- g( ]& p! ^3 S1 J - 2.0
6 C6 Q [3 z& y - #即10的1.3次方的结果为20
" w& y) D' O+ X: M, {% r" y, g: K - >>> math.log10(20)8 Z. t, p" }/ K4 `
- 1.3010299956639813
复制代码 / v& [: q9 U, g: d- k
math.log1p(x) 返回x+1的自然对数(基数为e)的值
' o% f- d' D' r/ u y% q- #返回x+1的自然对数(基数为e)的值1 _ L2 o0 q x0 \
- log1p(x)
' f, m4 j- r1 w9 O3 L% W - Return the natural logarithm of 1+x (base e).
- \0 a* }! a3 m, Y9 v6 `& @ - The result is computed in a way which is accurate for x near zero.$ F- K" C. [3 ^5 q
- >>> math.log(10)
2 `9 v, c' n% u- W( Z - 2.302585092994046
- s- b6 E8 p/ d - >>> math.log1p(10) N; m0 q, R; } y0 N& k# h
- 2.3978952727983707$ a9 a% ^) m$ D+ K/ V- a
- >>> math.log(11)1 {: ?, w, H1 L4 c
- 2.3978952727983707
复制代码 ) }/ K6 i& w; q+ f$ u# X- i4 C
math.log2(x) 返回x的基2对数6 K( q( D; F5 ^3 P$ j
- #返回x的基2对数4 Z& W, @1 D( }3 r) J0 F) j {
- log2(x)
. b3 U# H& q+ u7 q- W - Return the base 2 logarithm of x.
' ~0 z; c, L9 \8 l9 Z - >>> math.log2(32). q; G: v( \7 S
- 5.07 g0 _, v, F3 d3 y! t+ ?
- >>> math.log2(20)
7 D6 k* }0 J/ B$ h2 \7 r% r - 4.3219280948873632 i! D, H K/ z, m3 P$ l9 c! U
- >>> math.log2(16)& B- D7 Q, x" h8 I- C1 R. b4 A
- 4.0
复制代码
2 m5 z; H! P8 [0 f$ }; e; P- E8 Nmath.modf(x) 返回由x的小数部分和整数部分组成的元组
' P* E( w" A; N0 V6 a- Z. r, ?# z: e- #返回由x的小数部分和整数部分组成的元组1 t5 T4 c/ r. _5 |2 E2 i
- modf(x)
# W( ]1 E8 d: M- A' _* h9 t - Return the fractional and integer parts of x. Both results carry the sign9 h: S$ d7 `; B, r8 O
- of x and are floats.7 n- I0 j$ N/ n9 P& e% N
- >>> math.modf(math.pi)
% I8 @) g7 ?8 }0 @ - (0.14159265358979312, 3.0)
' A9 M" G' J2 U( L. t. u - >>> math.modf(12.34)
5 V1 g, Z/ w# K( c( ` - (0.33999999999999986, 12.0)
复制代码
! j6 O" P' U" E; K5 ^math.sqrt(x) 求x的平方根
5 i( U3 p) b0 u# Q- #求x的平方根8 n. R2 g1 [( e$ Y8 {
- sqrt(x)* L" J% l! v. ^) l. l, d
- Return the square root of x.$ U6 _ r/ {* q+ _0 o
- >>> math.sqrt(100)8 W. E, {9 p0 o- \, |4 {% M
- 10.02 X6 s6 q8 \$ }7 v" v
- >>> math.sqrt(16)
+ i: \, B1 Q4 X - 4.0
' ~# \, d+ p: z$ M+ i+ W - >>> math.sqrt(20)3 p& `5 w6 }$ C% ^* ^0 g
- 4.47213595499958
复制代码 . H/ X8 h2 j6 N8 J! V( L
math.trunc(x) 返回x的整数部分- #返回x的整数部分) m K0 F! P, [9 J! z
- trunc(x:Real) -> Integral
3 r7 t& n A* _5 S8 V4 M' N+ [$ \ - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% U9 H3 k( I" J8 V
- >>> math.trunc(6.789)$ O% n2 Z+ h" z# o
- 6
2 V0 Y0 f. ^- _1 b# V - >>> math.trunc(math.pi)
3 n8 q- S% Q4 f4 T2 T - 3
0 ^6 e* t& T0 @5 y P - >>> math.trunc(2.567)
* I8 v" ^+ A* J1 V - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|