马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
: {, G# G6 j: |. M, a
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. j( O) A. T2 K# R- C- e1 z0 C1 `2 ~3 M
5 P0 U! _% R6 I$ L
方法1:- n) i, y5 ^" g
- >>> import math3 y& M* ]8 m/ t) b, {3 j$ w
- >>> math.sqrt(9)0 Q* W. H( j; @6 ^, o; S; t
- 3.0
复制代码 方法2:5 x. O4 T0 \2 b+ _- Y3 b/ C L
- >>> from math import sqrt
$ r$ C4 f" b- | - >>> sqrt(9)
2 E3 c+ j: ]0 g% f& c. `/ v7 ~2 _7 E - 3.0
复制代码 + @9 e7 k7 V* [& V' c0 P4 o) ~
) ~2 C" I6 L7 l$ \/ x' Umath.e 表示一个常量: d/ F3 Y W" C$ i; Q8 `! k
- #表示一个常量# o! {. _4 i- u" u
- >>> math.e
6 `; c9 ~7 ^5 T d - 2.718281828459045
复制代码 6 s6 `* q' i7 T) e T
math.pi 数字常量,圆周率
- ?" e/ \& E7 H! [' Z- #数字常量,圆周率
& H* `& q' F( w- B - >>> print(math.pi)
* x) y/ ^5 @4 ? - 3.141592653589793
复制代码
) Z, j7 G. n/ K% L, K& }math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 ]3 u3 K7 z$ u$ h5 M- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
+ z- G# Z" f: |' e i - ceil(x)8 F* Q3 i& h- U, B
- Return the ceiling of x as an int.$ v, S! v, L, J0 V9 ?0 J
- This is the smallest integral value >= x.5 u$ O4 E D0 ?1 H( g$ q; G
- 3 e; k! f9 u! L# O
- >>> math.ceil(4.01)
! i5 b% X9 a ]1 I1 r - 5
) f* K. z$ J9 t/ X2 N - >>> math.ceil(4.99)5 L2 B7 K) _1 j" K
- 5
! L/ x0 Y0 y; C' `2 F: o& p( w# r - >>> math.ceil(-3.99)) H& K7 Q) k2 t# k& z' I
- -39 A8 \1 v3 p( e" ]1 ]6 s _+ ]
- >>> math.ceil(-3.01)
8 l- [: h/ i/ s3 W) g1 ^ - -3
复制代码
; B8 L# ?* L ]: S: Fmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身' r9 ]3 j' ^0 {8 `# z8 W
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身( x1 t9 L$ h2 X. K4 Z: k$ k& l' s3 f
- floor(x)9 ?4 x5 E2 B& X9 B C
- Return the floor of x as an int.$ B; @5 i0 U$ p3 c( }- G
- This is the largest integral value <= x.
$ Z) N: E+ u" z+ D0 u$ w; v - >>> math.floor(4.1)
* `- b1 N1 J; R. `3 z( A% | - 4
7 N; A9 \' G! n9 n0 n5 v - >>> math.floor(4.999)
8 m2 J3 v4 m: e( v* a4 g6 i - 4
3 A- ` \4 s1 A$ i9 H+ q$ Q - >>> math.floor(-4.999); M- w- F2 N4 T. n' u
- -5! t$ Z' b& X V7 Z9 q) E0 {3 X0 X
- >>> math.floor(-4.01)
, r7 i3 ?9 w& f+ v/ }5 f - -5
复制代码
o; e& ]( ?7 q8 h& Wmath.pow(x,y) 返回x的y次方,即x**y
1 ^$ }$ G8 W3 ^0 K- #返回x的y次方,即x**y
$ V: ] B, m% @/ r# I - pow(x, y)2 b7 G/ l! N) u. v; h
- Return x**y (x to the power of y).' J | I. q0 v/ H. O
- >>> math.pow(3,4)& \4 V1 B6 x' H# B7 r7 L
- 81.0
7 S$ o/ }2 t9 u) R' `, Q# _8 F - >>>
$ G* @* G( j, A+ S/ U - >>> math.pow(2,7). G3 S# m, F5 ]- Y( h6 w
- 128.0
复制代码
! i3 O' S+ Y2 mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): f2 _5 S3 V, Y
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( M$ H% V+ I' o2 ]0 e' g8 f4 z/ P
- log(x[, base])
1 `) ^- B" G+ C6 b& W+ @5 K! R - Return the logarithm of x to the given base.
( l2 t0 d7 q& v$ p - If the base not specified, returns the natural logarithm (base e) of x.
+ B! C! | X! z& o/ Q: ~/ { - >>> math.log(10)
5 P: r4 O( b6 K, H - 2.3025850929940464 J& Z5 [1 ~! G5 T" ` a
- >>> math.log(11), U1 G/ z4 r& R5 D
- 2.3978952727983707: T) T! M }' ^& v* f7 [; c9 |# k
- >>> math.log(20)
) b* a: U9 n( t& T* }, x - 2.995732273553991
复制代码
+ W4 v" Z. w. F! V7 [. dmath.sin(x) 求x(x为弧度)的正弦值
; N, s- }; [" S- #求x(x为弧度)的正弦值
+ K7 i; J* U# n5 m4 q - sin(x)' B* |4 n* k0 A7 a
- Return the sine of x (measured in radians).4 M, g9 A, [3 Q- ~% p
- >>> math.sin(math.pi/4): w5 N3 s+ f; O& W- ?7 R
- 0.7071067811865475 G' x+ u6 g7 j6 ]$ o6 m/ t
- >>> math.sin(math.pi/2)- z: _+ {2 U# j L: n, h1 {
- 1.0
4 G; c$ h( q2 ~2 z. e - >>> math.sin(math.pi/3)5 |: |+ O) x9 s# ^7 n) n$ Q# c
- 0.8660254037844386
复制代码
1 b- j) h7 Y8 R8 x+ j6 rmath.cos(x) 求x的余弦,x必须是弧度# s8 B$ c3 j+ i. s7 a$ C7 f
- #求x的余弦,x必须是弧度. E+ c* P/ I; O( {- ?
- cos(x)7 Y# S+ ~. k" K* _( F7 d2 j' f' ]
- Return the cosine of x (measured in radians).
! p, T ~: J' p" i# }4 r - #math.pi/4表示弧度,转换成角度为45度1 _+ _. T* w8 U; `+ G5 d- a0 }: Z
- >>> math.cos(math.pi/4)3 T7 K, d9 E! G9 t9 E' \- F' `' I
- 0.70710678118654763 m: u+ e. F% U
- math.pi/3表示弧度,转换成角度为60度 g- T6 O v! B. d/ E, q' B
- >>> math.cos(math.pi/3) y: i9 y7 M/ W+ ?6 I! j4 ?! ~
- 0.5000000000000001/ e7 w5 Y, k; |2 _ W
- math.pi/6表示弧度,转换成角度为30度
6 m" U& U- R9 L - >>> math.cos(math.pi/6)
8 o# m( N, Q/ N1 J: X - 0.8660254037844387
复制代码
/ [% B' M1 d& ]5 i& amath.tan(x) 返回x(x为弧度)的正切值
$ X; \: o! r+ _8 P& i6 x& d! j- #返回x(x为弧度)的正切值
2 @5 M! k1 c# B. c3 q6 m: @0 k8 x# U( k - tan(x)7 K) E5 [& C0 L, E: q
- Return the tangent of x (measured in radians).
7 `* ?% E8 m1 T: |/ {5 ? - >>> math.tan(math.pi/4)
7 a) G4 U2 _, |+ u$ k - 0.9999999999999999# j* t, y y% h6 _2 e* c$ P
- >>> math.tan(math.pi/6); T3 j. \8 w) W6 h& O3 y
- 0.5773502691896257
9 H" E C3 i- K' a( J - >>> math.tan(math.pi/3)6 O6 r, J! @; V" z- v) Z. K
- 1.7320508075688767
复制代码 8 L* S5 B' Y9 E# w8 f! u6 f
math.degrees(x) 把x从弧度转换成角度% S' [0 ~2 b, X1 n( x2 i
- #把x从弧度转换成角度
$ Y3 k/ O# ^! C - degrees(x)* M, ^4 L" J9 l% v% @1 I2 ~6 Y
- Convert angle x from radians to degrees.* g8 w% |& v* e, M- A& Q/ z3 N% D
* `2 j4 |% B9 f0 W- >>> math.degrees(math.pi/4): N t Z- s5 a' ^8 p# c0 A
- 45.0( [! @0 q+ \, A0 e! L
- >>> math.degrees(math.pi)
% g) V( F& A. e/ m: ^ - 180.08 W3 V2 f0 z' h( Y
- >>> math.degrees(math.pi/6)
; A+ N: H6 x0 s; i+ ?0 _5 t - 29.999999999999996& ?+ U/ o! A& J1 D+ b
- >>> math.degrees(math.pi/3)
; A7 z% e9 v' k4 N \2 A/ |# ? - 59.99999999999999
复制代码
4 A3 ?1 p9 C/ j1 x* imath.radians(x) 把角度x转换成弧度" S, q/ B# Y! n( L! F9 V9 ^9 d ^
- #把角度x转换成弧度
% n' o# f+ x" h5 K' i4 ~3 @: p0 E - radians(x)' U# Q$ O7 |6 G# }% |: K
- Convert angle x from degrees to radians.
' i* ^% j: L# j D; `. S$ _+ C - >>> math.radians(45)
& f. e- o# f$ [0 Q' s - 0.7853981633974483
8 |3 \2 o4 |( K - >>> math.radians(60)
' S# |4 J0 J( Q9 {- |- v - 1.0471975511965976
复制代码
8 W0 X0 D8 D# Z: S y0 X* _math.copysign(x,y) 把y的正负号加到x前面,可以使用0
' o+ n2 j0 o- `0 g0 |- #把y的正负号加到x前面,可以使用0
/ q3 P8 P& z2 g& L: y - copysign(x, y)& Q" P1 o d6 K4 B7 u9 E
- Return a float with the magnitude (absolute value) of x but the sign . c. H# [& w7 l
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
0 L% ~2 f3 O7 m7 G - returns -1.0." e. R3 G8 B# A. ~. {
* | q$ F4 e/ P- >>> math.copysign(2,3)
+ I7 E( |2 z, t% W9 w: d- L - 2.0
4 e, K" V: d+ { b+ G% {3 V0 f8 n - >>> math.copysign(2,-3)
' d9 s1 H( D, y( ?7 u - -2.0: _3 }7 u3 D6 s9 d. g
- >>> math.copysign(3,8)
, @ r$ T3 }6 w4 S# d - 3.0
( U: {8 e) N D- Z' b - >>> math.copysign(3,-8)$ s$ D* T6 S, y) @: |
- -3.0
复制代码
0 K$ U M2 a6 V5 r. ? hmath.exp(x) 返回math.e,也就是2.71828的x次方! ?( T* h% ~* S, ^7 C* R
- #返回math.e,也就是2.71828的x次方
/ n2 P" X6 J- U# i' s, y! k. ^ - exp(x)
9 z' v; y; _; X5 l6 V l - Return e raised to the power of x." A6 Y' j. |! T# e2 k( O+ K
- : j: O. Q! I- A$ x. O
- >>> math.exp(1)
6 k) ~. O# [3 n6 X - 2.718281828459045
* z4 v1 r5 b( L( F4 O - >>> math.exp(2)
6 _& e( b/ N' X4 H" U) O8 h" Y - 7.38905609893065
& A* k/ Z# @& l! s, q! `9 W- L7 v - >>> math.exp(3)
' s4 T) K4 ^5 ~ N! Y+ Z3 I - 20.085536923187668
复制代码
8 f# m8 }2 _8 p0 m( rmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1! [+ G! `% ]0 b
- #返回math.e的x(其值为2.71828)次方的值减1
3 h3 `7 k1 t+ P' \# f% `: ^7 B - expm1(x)0 E4 r- x- ]3 f D) `
- Return exp(x)-1.
/ t* B! d+ r0 c$ X# q - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, N* F* R, K$ j4 F- n- O
- _* g! s+ F9 Q5 M- >>> math.expm1(1)! s5 G+ S2 d. m. f6 s% a
- 1.718281828459045
+ `* B6 L+ }/ a w - >>> math.expm1(2)
+ E& T% t$ F3 X. n9 [/ x+ F" e - 6.389056098930659 ]8 Y# b& O+ g, ]6 a2 b
- >>> math.expm1(3)
- M6 n% Q% N- {! q! A! f2 i: B - 19.085536923187668
复制代码
' a' c. O# r: v! d3 [math.fabs(x) 返回x的绝对值
5 v# M3 s9 ^& f4 q- z- #返回x的绝对值
9 g3 F$ s6 O% E+ z. k7 c - fabs(x)
) S2 Y9 i2 A# r1 ?3 G) o' X - Return the absolute value of the float x.
2 E3 b4 g; @& m9 W2 K; p0 z2 {) Y% ` - # l. |3 O, u; K
- >>> math.fabs(-0.003)
% Y" q4 ]5 h# a8 T - 0.003
, [$ b; f- S0 S& C& | - >>> math.fabs(-110)7 P6 N6 \- g; K: _/ d1 I/ P9 w6 u
- 110.0. M) m+ S0 Y# p/ k( D- j
- >>> math.fabs(100)- g5 d& x( t% k% I; i/ h: d
- 100.0
复制代码 3 R7 g. D9 Q2 ~3 o, E3 N) q
math.factorial(x) 取x的阶乘的值
) b/ @. m0 b/ ?* I5 J% } X- #取x的阶乘的值# D8 m5 D0 D, T) z" D) j9 D
- factorial(x) -> Integral
2 a! q+ ^+ T; e% h$ Y+ m3 f! R$ q - Find x!. Raise a ValueError if x is negative or non-integral.
" u& G* y( o' d# d ?$ I - >>> math.factorial(1)2 F/ a$ ? _. O
- 1
3 }5 t! }) k S* w9 K% y - >>> math.factorial(2)
2 K; w4 a+ F+ O, [9 N9 |' H# h - 2+ ?: S8 P7 O- T+ z7 M. l
- >>> math.factorial(3)$ i" j" H0 Q4 ]: H! k4 k' e( Q. `
- 6' [& o) Z$ A2 |; b5 D+ i( t9 y
- >>> math.factorial(5)
S. f+ T. L, h5 |& p/ r - 120
7 T6 K3 p9 Q) T) ], a - >>> math.factorial(10); ]- T& k% W# t. {* ]9 u
- 3628800
复制代码 - R& D! H; Z J0 d" w+ u
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数+ y6 w+ k8 @4 B0 V1 C
- #得到x/y的余数,其值是一个浮点数, B# h" ~+ |( b3 n. v! U
- fmod(x, y)5 ]& u! x. N/ r' f( n2 r: ]
- Return fmod(x, y), according to platform C. x % y may differ.+ I% [* j9 v5 ^/ T8 j9 `
- >>> math.fmod(20,3)9 j5 j9 Z0 I: K5 {7 M
- 2.0" n! T2 k& i0 T/ [3 e
- >>> math.fmod(20,7)
( _0 e- O }) ]+ w$ y4 M - 6.0
复制代码
, c0 u) O5 `. b6 T: Q( o% mmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ [* M1 H0 |: Z, U5 z3 h
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 e% b' L. @% a9 z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
) w9 ?. I4 r" O( G4 { - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
m) J) K& J! z) |, w$ l - frexp(x)
8 m: x. d; z _0 e1 I$ I7 V - Return the mantissa and exponent of x, as pair (m, e).
# ]1 g& _' |* f/ m& l. ] - m is a float and e is an int, such that x = m * 2.**e.
& _* ? K& p0 D/ S! R - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.! x! B# R, q3 ^' k
- >>> math.frexp(10)
$ H, q! K+ n/ ], S) D: w& n - (0.625, 4)5 s0 I" a" Q+ a, G7 |0 m
- >>> math.frexp(75)
7 l( @! q8 A6 M/ w' p - (0.5859375, 7)3 a/ g& _5 U8 b [1 V+ k* r
- >>> math.frexp(-40)
4 Z- [5 G, P j% W+ e - (-0.625, 6) [. N/ @* i% Y. s2 I9 [. s
- >>> math.frexp(-100)7 r' S6 ]% O2 t+ i! G/ n1 R
- (-0.78125, 7)
2 n$ D7 k* Q: B) y - >>> math.frexp(100)5 K& `1 v4 C: u$ g: h& U9 B2 [
- (0.78125, 7)
复制代码
) d0 J4 Z/ Y/ N3 \) |math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
* g, C& B* E6 @5 T- #对迭代器里的每个元素进行求和操作
* _4 s% m. G+ z& w/ y# i - fsum(iterable)
i; T8 e! \0 P. r: A) w2 } - Return an accurate floating point sum of values in the iterable." }7 _& ?! g# i5 I! M
- Assumes IEEE-754 floating point arithmetic.
: f, H9 ?& |, n7 H - >>> math.fsum([1,2,3,4])/ e& ^ g! i5 K5 p" {: {6 {% @2 X
- 10.0
# Y! d! P) j; h1 I% m - >>> math.fsum((1,2,3,4)); B- g3 _* Q( Q1 k
- 10.0
# _, |0 K x, s( A3 f1 F6 U - >>> math.fsum((-1,-2,-3,-4))
7 w2 ^1 }& O7 M9 g& o - -10.0; T2 r. z8 a. Z, y, J
- >>> math.fsum([-1,-2,-3,-4])
: q2 X+ P$ g6 G1 t - -10.0
复制代码 ( E1 s- \5 ^ W! g3 |6 q
math.gcd(x,y) 返回x和y的最大公约数
7 i! S# y( Y( T. _" o7 y0 }- d- #返回x和y的最大公约数% X/ O: ^) O i
- gcd(x, y) -> int, l1 H0 L3 Z# U1 V3 q) g
- greatest common divisor of x and y
7 P$ G' R3 Q! N: K - >>> math.gcd(8,6)
' S+ {6 F6 @/ K2 V0 I9 K3 m - 2
" n/ T" E0 B; E9 K' v0 w - >>> math.gcd(40,20)
& o4 y/ s# |7 z4 @- W# @ - 202 i0 N9 G% ^% D) h, p9 f4 Q6 x) d% ]
- >>> math.gcd(8,12). |% v% K3 A; t$ j- [, a
- 4
复制代码 & R: K' d Q; |$ |0 B
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
' m* O! n7 V1 Y/ }5 N- #得到(x**2+y**2),平方的值* u3 I2 t& T4 ]
- hypot(x, y)
: R$ a- e: h- j6 n - Return the Euclidean distance, sqrt(x*x + y*y).
% L: ]" d. G$ y& D+ k! \1 i - >>> math.hypot(3,4)
& r3 |3 Z8 {: T3 V* ~* H7 d- [" y - 5.0* m$ a) E S; e# ~$ }6 {
- >>> math.hypot(6,8)% \& `% C% T* W0 z( P" x$ e/ }
- 10.0
复制代码 8 ~' |$ Q, t& v3 @4 N
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False& E8 S, d9 t' f
- #如果x是不是无穷大的数字,则返回True,否则返回False$ ^$ G/ P s" f( H0 S) V
- isfinite(x) -> bool
1 _4 f, h( R n8 W" D - Return True if x is neither an infinity nor a NaN, and False otherwise.
4 Z4 b4 V* `9 O1 d" i4 Q - >>> math.isfinite(100)
4 G" ~6 r) v! }8 y6 `0 V5 J - True% }7 D6 q( {& h# P& W
- >>> math.isfinite(0)
9 \( z0 k2 {& |- v& [ - True& Z# p: }3 @6 P7 {
- >>> math.isfinite(0.1)
7 Q/ Q* p0 f6 A" I - True
1 Y3 b; n/ J3 J4 }1 b; l8 [ - >>> math.isfinite("a")
. E: H8 l$ U: v/ W. q - >>> math.isfinite(0.0001)
* M, t. y$ y ~5 q y4 o1 ^8 v - True
复制代码
: C. Y, F" |1 G$ d2 b7 Tmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 k# a4 A( ^: A: `& s- #如果x是正无穷大或负无穷大,则返回True,否则返回False% D4 a! m n- L. x9 }
- isinf(x) -> bool* d% n3 `, e& H/ o' g; }
- Return True if x is a positive or negative infinity, and False otherwise.
( a; F4 O% v- l9 N) q1 c - >>> math.isinf(234)% r j8 @3 I; k. f. Y7 o2 V. h
- False" f- S+ Z8 g; }8 n) {6 k4 e6 `
- >>> math.isinf(0.1)
, U5 |' P% a& G# e - False
复制代码
: r3 o, u5 F4 F; emath.isnan(x) 如果x不是数字True,否则返回False8 R' E; v0 O4 J {% R0 V, r
- #如果x不是数字True,否则返回False
! c+ b6 S1 K. Y/ _5 ^ - isnan(x) -> bool* o& _% _ [% i$ U
- Return True if x is a NaN (not a number), and False otherwise.
~) _; M/ c+ K( S& k - >>> math.isnan(23)
+ ^$ X/ `* U+ O+ H. S - False
' m' ~" i F; w4 g( p - >>> math.isnan(0.01)
% n/ |. ?. I' `' n$ b - False
复制代码 1 f' b2 w* L! Y! X0 s( x6 n9 ~' e
math.ldexp(x,i) 返回x*(2**i)的值% q* o( ] c0 j& W
- #返回x*(2**i)的值
" y [1 r: I' i - ldexp(x, i)
5 L w C- N& f# w( U9 r& ? - Return x * (2**i).5 F0 ]5 J. x& U3 d& j* U) \
- >>> math.ldexp(5,5); s% F* W ^9 w
- 160.0
6 x$ g/ n! R n- x, I6 Q; h: [$ m4 c1 o - >>> math.ldexp(3,5)5 q% h* ^, N" \! V; m
- 96.0
复制代码
2 d+ U% `. O/ Z1 g4 Jmath.log10(x) 返回x的以10为底的对数
0 _. ]6 p" Y, C& E! M4 @6 U/ ?- #返回x的以10为底的对数5 F7 T, Z* B" L
- log10(x)
; [+ p7 o& r" |. ~8 W6 p - Return the base 10 logarithm of x.& F1 h6 K- t3 ?( S' k. |+ ?7 Y
- >>> math.log10(10)* v/ F1 r6 ]7 m* l
- 1.0
1 f& q- E9 s# K+ m& T - >>> math.log10(100)
. m! o, l) x, f6 ?) q4 y. v - 2.0
5 F/ M1 |& j- r - #即10的1.3次方的结果为20- o& s' d( c' g0 Y0 r# u. j
- >>> math.log10(20); A- B2 k8 q2 t! @1 {. Z2 L9 b
- 1.3010299956639813
复制代码
! }2 C! I4 W; r7 G' v5 gmath.log1p(x) 返回x+1的自然对数(基数为e)的值- R$ C8 ^, P! u' v \& k! _
- #返回x+1的自然对数(基数为e)的值
9 k& J( o, O2 M. x - log1p(x)3 y0 l$ @1 D6 b5 a, Y9 ]" p$ u
- Return the natural logarithm of 1+x (base e). p, M' j* x6 w0 f& p: u' f
- The result is computed in a way which is accurate for x near zero.
) v) W/ O6 C8 u$ F5 y - >>> math.log(10)6 d0 y0 E; i+ i- x5 A
- 2.302585092994046
; e. o. X3 A5 ] - >>> math.log1p(10)
( I( K$ C# A* v! B3 l: @ - 2.3978952727983707
5 S. y! O; F5 y- x% w - >>> math.log(11)
, }3 u/ ^% J: _- J H - 2.3978952727983707
复制代码
( g+ M2 c& M- \+ ]math.log2(x) 返回x的基2对数
5 E) l* \1 Z4 B# l# @" V g$ V3 ~- #返回x的基2对数# y2 U# `9 e0 `# r! s
- log2(x)
4 M9 H# ?7 }9 t* Y" R - Return the base 2 logarithm of x., h2 j" C- j1 M3 f+ m
- >>> math.log2(32)3 i( p0 _ D& Z5 H
- 5.04 G; P, | Z& g0 [- b* p
- >>> math.log2(20)
0 i9 h% J: ~* t! Q4 N - 4.321928094887363
& u A4 S' l/ Z; x7 @7 S - >>> math.log2(16) [* w+ M5 H+ z4 w W& r
- 4.0
复制代码 2 i) f* B0 u- o9 b! v+ M$ o$ |' r
math.modf(x) 返回由x的小数部分和整数部分组成的元组, I8 O3 \& f7 a. D, g( j) w/ e
- #返回由x的小数部分和整数部分组成的元组
! [% F; C: y" y8 n- Z! J" e% L" Y5 p - modf(x)
. u$ F$ s. g q; ?2 y) g - Return the fractional and integer parts of x. Both results carry the sign. n2 c: R3 w w4 B/ x$ [# m3 j
- of x and are floats.
' B5 \; R5 q6 M# E0 h+ M - >>> math.modf(math.pi)
9 B1 i0 h5 @* N" ^# {' | - (0.14159265358979312, 3.0)
# z( H! }6 {6 w, m: d% d' o - >>> math.modf(12.34)
! C5 i _7 D& y3 [/ q! S - (0.33999999999999986, 12.0)
复制代码
# g% G6 ~$ i) ]$ d; c3 H% S- o! }math.sqrt(x) 求x的平方根& N" o+ g4 W* z$ s7 K! X
- #求x的平方根
( L- p# y- \' j4 m# O) J! o. P - sqrt(x)9 W B. V7 D3 o5 _( E
- Return the square root of x.6 E- S5 [2 ?% Z' M# p# Q
- >>> math.sqrt(100)
4 A& V8 {. W, G2 w' Q; `; C - 10.0$ _5 z& A# g9 _
- >>> math.sqrt(16)
% \/ R6 v+ f, I# }( X - 4.0
& c# Z6 |' v% Y: ^+ e z( o - >>> math.sqrt(20)8 M+ h# N2 F l2 i2 p0 J: Y9 M
- 4.47213595499958
复制代码 ' a% ^' c1 Z+ X. q
math.trunc(x) 返回x的整数部分- #返回x的整数部分# m N7 I7 u# M/ d/ [
- trunc(x:Real) -> Integral
" i$ a4 A; P4 o+ ~1 H2 n0 K - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
0 \! o, ~+ z V# M s - >>> math.trunc(6.789)$ u7 h6 k# Y6 _8 M$ c
- 6
) F- @% T S- r" ?' C' p) m - >>> math.trunc(math.pi)' P/ i7 |% [$ ~0 n/ t+ A
- 3
2 p" c# [# f! A6 ]* }2 l: N - >>> math.trunc(2.567)8 k2 `6 f# Y8 P1 I! |9 C
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|