马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
% J; x# ~4 F( A1 D8 m) h' x
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 q' |# L- y; U9 P, H
' {! E& U% A. `$ G8 Z* J方法1:
- D, i: E& E8 B1 ?+ J- >>> import math6 l: a9 Y( F3 V8 J5 W' v
- >>> math.sqrt(9); P5 x8 t; _4 O. J7 o) s
- 3.0
复制代码 方法2:
( P {* ?/ A% S- >>> from math import sqrt
! k8 I& d0 C7 O6 d7 F - >>> sqrt(9)/ b! d$ J0 [) V# F3 p
- 3.0
复制代码
$ M+ y3 `9 F7 k( P 6 Y2 ^1 v$ I1 u8 L. u+ _5 J* ~6 `/ c
math.e 表示一个常量
+ j7 h$ p ]3 ^( t6 a6 {- #表示一个常量
3 }* ?. G1 g7 ]6 \( v1 v - >>> math.e- Y+ E2 ^) H$ k: R
- 2.718281828459045
复制代码
: X- W% e% \2 ]* w7 p# Qmath.pi 数字常量,圆周率& E( u7 M9 o8 z9 e% u% \
- #数字常量,圆周率# y. V4 u- C" r. I. d, | t
- >>> print(math.pi)
6 w3 E0 S' N# R - 3.141592653589793
复制代码 9 I' l2 t' f: ^7 f
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x: m# ?; w6 S! H5 M
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x( l2 T c9 Y% G
- ceil(x)
2 Z0 }; J( x9 v& I- M# M - Return the ceiling of x as an int.
2 z& ~/ f* L! }& | - This is the smallest integral value >= x.
) I: ^* `7 ^) p+ K8 ?0 f9 a+ [
- a" _( o0 e* x2 p! }& z% i- >>> math.ceil(4.01)
7 A8 ~4 A/ d6 ^2 b' x - 5
( I2 O3 O2 q/ c" Q. p - >>> math.ceil(4.99)0 o1 u- @# k6 b/ E+ _2 z: J
- 5
8 r$ e' ?6 G) v2 j' }" N* C - >>> math.ceil(-3.99)7 t+ \) l. V2 Q% A
- -32 i: ]2 ^; G4 C
- >>> math.ceil(-3.01)7 C1 J$ L/ H4 l& W/ _. A/ p
- -3
复制代码
# O0 D* o# Z: l4 F* e J V" Imath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& g: k& S1 E" ^5 y- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 s2 ]5 a" z: U6 ?# u - floor(x)# F0 _+ w" ?- W4 D+ G
- Return the floor of x as an int., R( o! t! N1 g# z) I; v- u
- This is the largest integral value <= x.$ G8 X" l8 F7 q) B
- >>> math.floor(4.1)
1 a# f8 z" f# x1 i7 f0 v - 44 t. z! E; Q' @
- >>> math.floor(4.999)
7 i$ H% x$ s% S1 l- V$ u: b( j# W - 4" n& R& A% a& E) b' o
- >>> math.floor(-4.999)
- n9 f5 w5 u1 O* [2 I, V e - -5
% z9 |8 z6 m) A6 `$ B - >>> math.floor(-4.01)& w4 Q1 j5 h: r( h9 [
- -5
复制代码
& B; u9 \* j* ~$ P- L3 `math.pow(x,y) 返回x的y次方,即x**y
4 p h1 U/ m, A) r; l- #返回x的y次方,即x**y
* p* U# b8 O2 v% F( k' \ O - pow(x, y)5 Z7 d7 o8 K0 @7 h
- Return x**y (x to the power of y).
* M# a* [3 e6 `4 j$ Q B - >>> math.pow(3,4)
1 |1 K y4 Z* x% P - 81.0
9 e/ F& C2 F9 n Q2 \4 m; h( T$ h+ ]: V - >>>
/ C8 _5 m; g! @% c* X6 S - >>> math.pow(2,7)5 v& M9 N; b8 Q4 ?3 O; K7 q1 a
- 128.0
复制代码 # \: }; C: O+ p- K( v* w8 h
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 @, u+ B- R8 u U
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) \' q- W, W" T7 p1 Y, y$ J) o2 D) L0 p' m
- log(x[, base])' Z1 t3 l& D! b: c- b0 D7 Z5 r, g
- Return the logarithm of x to the given base.
0 z t6 B/ `3 M& h" W4 u0 c - If the base not specified, returns the natural logarithm (base e) of x.) p# A- y+ p; _
- >>> math.log(10)
) e! `. R, ^7 M4 f t( K3 `' C - 2.302585092994046
# m4 U, t2 w+ _9 t0 M/ O4 v1 h: V - >>> math.log(11)3 n) m3 b2 X, Z z% y; E, w
- 2.3978952727983707" \( [) D& R6 W$ a- b
- >>> math.log(20)
+ b# ?6 Q7 z- a) W% T( u L - 2.995732273553991
复制代码 3 _+ {3 t4 D! Y
math.sin(x) 求x(x为弧度)的正弦值& S) A( K' u9 `' q; L, r( a+ n6 x
- #求x(x为弧度)的正弦值8 H6 T5 d3 |1 ?8 ~1 U# S/ I: b
- sin(x)6 U% h6 ]( C3 w/ E% D* t5 n- h
- Return the sine of x (measured in radians).% A M7 p% e5 ~5 s. L
- >>> math.sin(math.pi/4)0 a8 ^9 ]5 s* o4 o- I
- 0.7071067811865475
2 |% M) R) T# q2 |5 F - >>> math.sin(math.pi/2)
' ~1 m3 u" H/ ^0 v4 M - 1.0
( y" K) p) M1 n& q8 i. S( @* H - >>> math.sin(math.pi/3)
* R. g' b2 a6 Q% d - 0.8660254037844386
复制代码
6 A2 X$ L. {$ ^" }4 ~* Dmath.cos(x) 求x的余弦,x必须是弧度
8 j6 ?! |0 x6 P- #求x的余弦,x必须是弧度$ f7 x5 l8 X; H" \% j) J# m
- cos(x)
% O) L3 ?) z7 b9 x% z2 P# ` - Return the cosine of x (measured in radians).
3 j$ I" J) V- O5 x* N - #math.pi/4表示弧度,转换成角度为45度
; u8 c4 j& J5 |# y2 I x: O - >>> math.cos(math.pi/4)
0 F+ Y8 K4 w% F) K) B; L6 Z( X - 0.7071067811865476" V9 D- b2 v' `- _: S
- math.pi/3表示弧度,转换成角度为60度
$ i4 W& o! T k( r3 O& R7 | - >>> math.cos(math.pi/3)
" ~; {0 o* j7 N - 0.5000000000000001& \5 f) O9 i$ ?6 @
- math.pi/6表示弧度,转换成角度为30度( L7 P9 a7 z7 C K, `& _3 L
- >>> math.cos(math.pi/6)
; T: H+ h: Y# k* y1 ~$ T - 0.8660254037844387
复制代码
4 a+ H" ` H3 N. U( c! dmath.tan(x) 返回x(x为弧度)的正切值$ A, a$ h* p/ N5 o- L
- #返回x(x为弧度)的正切值
) S5 p/ r- R$ x$ \: u m5 Q - tan(x)& u, w% P x: P6 F
- Return the tangent of x (measured in radians).5 x' t9 u7 c- {5 y
- >>> math.tan(math.pi/4)
6 `; J7 ?. a7 n# |0 X [" v! e - 0.9999999999999999
+ b( O( Y) X! a) O) o - >>> math.tan(math.pi/6)% G% ]# x* N# z1 N6 w- Z, R
- 0.5773502691896257) m/ ]' q) A J+ I# ^$ F z
- >>> math.tan(math.pi/3)1 _& [ ~, o: e6 m a+ Y. L$ \
- 1.7320508075688767
复制代码
+ z. j/ |* u8 F5 N$ G* ] Amath.degrees(x) 把x从弧度转换成角度
" j# R% c: V; Y H0 z% r- #把x从弧度转换成角度( F; w9 b& m( B+ j2 H" q
- degrees(x)
! L/ C2 }5 M: ?8 q - Convert angle x from radians to degrees.
3 X, {. V# a& @7 x3 F - : G9 k# [2 N& k" _. Y
- >>> math.degrees(math.pi/4)
& E4 a! P8 N$ O' m. W6 P H" } - 45.0
$ {, p8 e$ _/ h; b) p - >>> math.degrees(math.pi)
6 ^ Q. a- b. _, }/ P - 180.0
5 C) V7 q1 P5 Q1 | v9 w - >>> math.degrees(math.pi/6)% K( U# B2 h1 B: a" X$ t8 o
- 29.999999999999996. r3 ?: y1 x' S9 p3 L3 U
- >>> math.degrees(math.pi/3)1 ], O; C/ W( x1 l% @8 v
- 59.99999999999999
复制代码 : x# B; e3 D `+ e
math.radians(x) 把角度x转换成弧度
; ^1 F6 x& `" A- #把角度x转换成弧度 U1 i; m! w8 B
- radians(x)
9 j. O' ?# j5 Q5 I - Convert angle x from degrees to radians.7 w& c8 Z& U4 n7 M
- >>> math.radians(45)% |6 i( W8 P6 V
- 0.78539816339744834 W( t" \ O2 ^4 E, ]
- >>> math.radians(60)% a" ^. p$ S- K# `8 J/ m3 g2 Y
- 1.0471975511965976
复制代码 ; C# F! x- q/ I$ \% w4 o5 m8 m
math.copysign(x,y) 把y的正负号加到x前面,可以使用02 C' _9 i/ D6 J& |3 Q
- #把y的正负号加到x前面,可以使用0
S6 F3 ~0 [9 l% ]3 M4 E - copysign(x, y)
# A/ z/ K( E7 W - Return a float with the magnitude (absolute value) of x but the sign . @) @7 J$ b7 y9 n3 J& A# D, C
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
0 T# n- `* e- ?/ v0 n D v) ]+ \# t - returns -1.0.
) j- Y8 h( T+ s' l1 W9 _5 O - 8 E9 p! P; f* l& n
- >>> math.copysign(2,3)
1 x: K3 e6 O( M - 2.0
, F% D& i2 ]( _# z% ]6 t4 E - >>> math.copysign(2,-3)
; s; U9 i0 W- D e+ `/ e) f* _ - -2.0
R# i0 b6 f s# l - >>> math.copysign(3,8)
: } v* o- n5 x- J4 H% y& h9 v - 3.0
8 E w0 l5 W! S- J- }3 l - >>> math.copysign(3,-8)* S6 a& G: I$ b+ U( [' W0 S2 U- b
- -3.0
复制代码 & ^9 \* h: S. ^6 k
math.exp(x) 返回math.e,也就是2.71828的x次方
5 L+ x9 f% Y, T4 V' x$ T2 i) u$ P- #返回math.e,也就是2.71828的x次方
8 u: s* x% s) x5 Y* R - exp(x)2 E! N% y/ ?) J+ T4 t, f% q
- Return e raised to the power of x.2 ]" Y6 r- h, Q& a
W+ L: u$ P' m- V, q: l1 X% P- >>> math.exp(1)0 G- @% ^8 p0 B6 ?3 r2 v t
- 2.718281828459045
6 g* x8 L' y" { - >>> math.exp(2)
! g4 |2 Q1 D5 W5 S- C - 7.38905609893065% X C4 |% v9 U" p- W" H+ h
- >>> math.exp(3)1 @2 X0 c, q+ q l. s8 M: D
- 20.085536923187668
复制代码 / c0 a4 N0 w1 x* Q1 r' D3 u
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
1 y, V1 b3 l, s1 Z, W- w- #返回math.e的x(其值为2.71828)次方的值减1/ q- b3 m: M/ l7 Q
- expm1(x): p! s" A; |4 J$ Y$ t6 h) }
- Return exp(x)-1.6 [% K6 x+ I0 V5 T
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 u8 K* i3 y, v+ ]# b5 E" G
- 6 }0 p& n, _' ?# c1 S
- >>> math.expm1(1)& U* i! Q/ n, s* _, ]5 I
- 1.718281828459045$ b' d- Y& f* F, T$ O0 b7 f
- >>> math.expm1(2)5 I# {9 n7 q( l* q3 `9 Y& f8 {
- 6.38905609893065
3 V2 M2 z4 V+ Q2 Q! D; _' O - >>> math.expm1(3)
- o1 m* e, d" q! |5 @ - 19.085536923187668
复制代码
0 [% ^8 v* R/ ^- ^math.fabs(x) 返回x的绝对值- B9 v0 ^# G r1 H# W" b
- #返回x的绝对值9 Y$ {/ V7 ^0 n- Z) U) d1 m
- fabs(x)
9 Z* J. O+ ]4 B' w( K& v) I: }8 a - Return the absolute value of the float x.' ~1 o) K% U$ F) U1 [, Z
- + J3 k4 `6 [- x7 E, Y4 q
- >>> math.fabs(-0.003)4 H# B4 x" |5 E4 z! g, F1 }0 k
- 0.003
. U4 i" e, X! z8 c+ P$ b - >>> math.fabs(-110)5 o9 Z. G& b$ x5 T
- 110.06 y; @- S3 P3 R8 M) e
- >>> math.fabs(100), Q2 s5 Y+ }# Z) \, o7 D5 q
- 100.0
复制代码
0 |5 W. l5 ?* ^, |math.factorial(x) 取x的阶乘的值
* w+ }1 S' M2 s3 v: G$ q- #取x的阶乘的值
9 v! w$ _/ T! b& P3 w - factorial(x) -> Integral( j6 t7 R6 v5 w4 y4 t4 ~& H7 ?
- Find x!. Raise a ValueError if x is negative or non-integral.
& \6 z; {4 m) c$ D - >>> math.factorial(1)
+ {: O; ?/ e# J- s& _ - 1$ M/ _$ Z: ]. y3 p
- >>> math.factorial(2)
3 {( S5 N8 f* e0 O Y; s8 o+ o - 2! N" X R4 d4 D( f5 b9 X7 y
- >>> math.factorial(3)$ X l' X) d* b3 G. Q" E' S
- 64 P# ~/ N5 j/ |* s
- >>> math.factorial(5), D. g; j) G3 Q \6 W7 I0 i
- 120 a5 F& f" r& N
- >>> math.factorial(10)
( Z& Z# U5 ~5 H. ]1 g - 3628800
复制代码
0 Z) S# [3 k" V: cmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数9 N- h+ a6 W' ?. H
- #得到x/y的余数,其值是一个浮点数
4 Z. y/ H6 [0 X- ]# ~7 [" | - fmod(x, y)
" C: V1 B. g" Y1 |# f - Return fmod(x, y), according to platform C. x % y may differ.0 c. D$ e. i6 |/ r D4 v( U
- >>> math.fmod(20,3)% u3 j( v2 Z5 g. p8 \: r% }( A+ A
- 2.0
1 y; R4 o, R& N: h. z8 Q I. } - >>> math.fmod(20,7)
! ^" q( T( x( f3 ^ - 6.0
复制代码 ' C4 _0 ^4 |. q
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 r% m: U9 P- |% ^- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,8 P% R7 | ^8 ]$ f; |2 ~/ L
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值8 X9 {+ M; A' \2 \0 P
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, S& M' A1 C2 `' p2 s( Q) [0 y5 T; E
- frexp(x)
1 C% L& n( g1 d Q7 Q: o/ W3 u- ~ - Return the mantissa and exponent of x, as pair (m, e).# P8 K) y( E9 u+ e3 D9 V) J* Q) w1 w
- m is a float and e is an int, such that x = m * 2.**e.# y' P4 l% X! c% U2 s# P; p1 {
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.4 f* x7 W0 e: j0 K! H7 S' i& Z
- >>> math.frexp(10)0 z% ~9 G+ }/ U3 l
- (0.625, 4)5 l7 _0 q" c2 L
- >>> math.frexp(75)
9 w4 M! M; k1 v+ a& Z - (0.5859375, 7)# L1 g7 y$ [+ u. f: l1 _
- >>> math.frexp(-40)* v9 g8 L7 u7 }% s! ]/ J5 F$ \
- (-0.625, 6)$ p+ J. U+ w* \2 [6 U
- >>> math.frexp(-100) q$ {9 Z& m2 Y" C- ^
- (-0.78125, 7)
9 @4 x( e1 ?. [' j - >>> math.frexp(100)& K& {( `2 e, X I) u' V
- (0.78125, 7)
复制代码 - H* ~: Q+ H: k, d7 _3 R
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
; X8 T3 W! @2 B) \! d4 P- #对迭代器里的每个元素进行求和操作
. w' p) m+ e V R; q/ {$ Q9 F - fsum(iterable)
+ v& _. _& Q+ d! U% v, k8 Q' @ - Return an accurate floating point sum of values in the iterable.% J4 \8 n# h# g; Z/ _& h: P$ C
- Assumes IEEE-754 floating point arithmetic.
$ Q5 _! G2 t% B) p3 s/ Y - >>> math.fsum([1,2,3,4])
( _1 A: e& y, m3 m9 O" G; P3 l; r9 E - 10.0
: a/ x$ r" n" I- { - >>> math.fsum((1,2,3,4))
6 Z# f! s0 V3 Y" ^ - 10.0$ E" y L5 L8 M) ^2 z$ i
- >>> math.fsum((-1,-2,-3,-4))
k- d1 _# U4 `% R8 i( q - -10.0
# h7 e0 g' e5 Z/ E9 A - >>> math.fsum([-1,-2,-3,-4])
+ C0 F( A8 R$ {' C/ M& y" a& K - -10.0
复制代码 / y9 G/ i* ~! }7 Q. D
math.gcd(x,y) 返回x和y的最大公约数
/ E7 k" }9 ~0 k1 d- a, _- #返回x和y的最大公约数
4 k5 c& v: p0 s, j2 G - gcd(x, y) -> int; ]* \) s ~: l
- greatest common divisor of x and y
8 W- x' f6 N& ^7 p. D - >>> math.gcd(8,6)) D! ^2 O L) @6 t# i3 p4 c% K
- 2! M: G* P7 M; A' k
- >>> math.gcd(40,20)& v# p6 _# }2 s/ g, Z7 J& r
- 20: G9 p( }9 j5 _) R2 Q/ R
- >>> math.gcd(8,12)) T) S' ?: r6 S3 M6 ~3 l2 N8 F" L8 b
- 4
复制代码 ( \7 H* x( ?$ J2 |' E! S D
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
8 C: ^( f" H4 S/ w9 z- #得到(x**2+y**2),平方的值
$ R4 k7 E7 n+ |" d b* s; i - hypot(x, y)
4 m' s- q( a" R) u* Z& r - Return the Euclidean distance, sqrt(x*x + y*y).
% \# d8 ^- \; j% v - >>> math.hypot(3,4)0 S E4 t/ w+ s8 B. S
- 5.05 j" n8 w, F# l4 ?
- >>> math.hypot(6,8)# m1 P5 A. [, ~
- 10.0
复制代码
$ k; _, O1 q, q8 B7 z5 r8 K3 j' _! Dmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 M% c" o3 H" I; w& Y& Q- #如果x是不是无穷大的数字,则返回True,否则返回False/ u: d# i' P8 W3 M( s; X w2 i
- isfinite(x) -> bool
' ]. f+ v- L1 t% l+ Z; v' b4 B - Return True if x is neither an infinity nor a NaN, and False otherwise.
4 A' A7 y8 g% F5 w9 ?% A- n - >>> math.isfinite(100)0 w/ r. H3 n8 t* L: }
- True
. v( ?; Z% c' l3 i - >>> math.isfinite(0)
3 A: J% j. A! N+ w- @1 Z( _! ]# { - True0 P3 K; c7 d/ Q% Q7 d2 o
- >>> math.isfinite(0.1)
! R) k4 {: E4 z$ M: _$ s$ Q% l B - True1 V1 ?5 B4 A) C' Q- o7 F; m
- >>> math.isfinite("a")( h6 d" a! ]/ e7 T3 F; t/ `3 q) A
- >>> math.isfinite(0.0001)
. N0 [% A0 t, V - True
复制代码
$ ]- X3 H% c( q0 R# Dmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ U& g! w0 V7 ?. Q- #如果x是正无穷大或负无穷大,则返回True,否则返回False
* x) A5 a" _3 k3 @- v. S - isinf(x) -> bool. x6 W9 n4 T0 t$ c& j
- Return True if x is a positive or negative infinity, and False otherwise.& W: j0 q+ _; k# G8 D* e! H
- >>> math.isinf(234)3 w' B ]2 V4 u
- False
3 r* b A6 p! B4 p6 H/ ^ - >>> math.isinf(0.1)5 J- y9 [& F2 T/ r* q0 ]
- False
复制代码 & G& {$ E# t' f6 {2 A
math.isnan(x) 如果x不是数字True,否则返回False
9 ?8 z/ U$ @' N) z! u- #如果x不是数字True,否则返回False& ?9 Y# V2 g7 T4 |- A( t+ j+ T
- isnan(x) -> bool
& ?' r8 G9 X: H7 R" \9 K8 t/ ]) R - Return True if x is a NaN (not a number), and False otherwise.7 ? L* w: b" G& {/ `
- >>> math.isnan(23)% R. Z# v$ a3 L9 T9 f: m% M& z8 w
- False1 f) c% |- B ~& ?2 n$ N
- >>> math.isnan(0.01)
! x' F; `, D& y& r4 g. J. _ - False
复制代码 3 J: M$ V" p2 j( |! v
math.ldexp(x,i) 返回x*(2**i)的值8 O: a) `9 n) j2 P! K- F
- #返回x*(2**i)的值
, P5 e( ~: t+ c! k% P - ldexp(x, i)$ J! u* d* W5 n& |- D. F: |
- Return x * (2**i)." P5 \" v K, X4 K8 c* }
- >>> math.ldexp(5,5)& o6 |# d$ [3 [# q1 v/ \; s
- 160.06 P) X( n) t, R# }
- >>> math.ldexp(3,5)
+ E" I y6 g9 J4 S9 L - 96.0
复制代码
R% m' d- u5 R( U8 ]9 |: tmath.log10(x) 返回x的以10为底的对数
1 o7 P3 t! F. _' s) K- #返回x的以10为底的对数
2 y7 Z! b* ]! q; N5 C - log10(x)6 s2 A( q2 @9 a% c
- Return the base 10 logarithm of x.3 _# O: x V# i# {+ P, v H2 A
- >>> math.log10(10)
7 W$ A$ t% S" h! _: W - 1.0
1 z. r0 B* @; e) N# ] J1 o9 P - >>> math.log10(100)- Y- c& s8 d% |# c
- 2.0. g- x) Z1 k/ y, u' x
- #即10的1.3次方的结果为208 c3 [0 e5 _& e2 n8 f- K
- >>> math.log10(20): J1 ]/ u5 b2 j. I- y, K# N
- 1.3010299956639813
复制代码
% o" G2 j- s" \math.log1p(x) 返回x+1的自然对数(基数为e)的值
0 ?! z1 W _" X7 \ m! c# A& T0 f- #返回x+1的自然对数(基数为e)的值
0 Y7 X- l1 g+ Y0 E5 d ?0 c) n - log1p(x)2 d6 O/ e& V, }
- Return the natural logarithm of 1+x (base e).7 Q! v; Y7 a3 u$ T* a5 h+ k
- The result is computed in a way which is accurate for x near zero.% _( D& [7 s; \8 K8 H2 S# U7 h
- >>> math.log(10)9 S: R& A7 _* S; F
- 2.302585092994046
" {9 X/ y$ o0 O: u1 t: {. K0 e - >>> math.log1p(10)/ W; K; ]! p5 _' R1 F$ k
- 2.3978952727983707% K5 d. `1 x9 g* e
- >>> math.log(11)4 j* g h; O5 L3 e n" C7 D' h, x
- 2.3978952727983707
复制代码
; T$ J; i* b3 V" K0 W$ Y& g% o6 Bmath.log2(x) 返回x的基2对数
- d Y+ J2 S& r) G ~; _& L- #返回x的基2对数
0 `0 x8 W8 R. B9 }" X; l) I0 f - log2(x)
7 {! i/ G- M0 @' O. I+ }1 R* @4 o - Return the base 2 logarithm of x.2 k' X2 R/ a! s
- >>> math.log2(32)
8 T ?- s5 O" T - 5.0& ?) _9 I5 h ~9 t' H6 A/ n3 |" [
- >>> math.log2(20)0 e# f' r5 {) S8 ]$ F8 E7 o
- 4.3219280948873630 A0 b: f+ x1 c) B; |* C( u
- >>> math.log2(16)
8 m& A! q" h9 ]2 Q3 S* M - 4.0
复制代码
9 g+ i5 ] H* ~1 }8 Y2 |3 Wmath.modf(x) 返回由x的小数部分和整数部分组成的元组8 z# w! S+ {7 u1 B. [3 P
- #返回由x的小数部分和整数部分组成的元组
# i: O" l- V( N5 S/ ^ - modf(x)4 a* o' j0 \/ D" D# _4 f3 p- W
- Return the fractional and integer parts of x. Both results carry the sign& e0 X7 I7 ^8 F' H$ F: T0 o' B f
- of x and are floats.
% I" x. }$ P# O0 B. u) X - >>> math.modf(math.pi)
+ T0 ~+ C4 a) T& M. M! k3 k- y# ? - (0.14159265358979312, 3.0)
1 T8 \$ ^; K1 e1 k/ e$ {, P - >>> math.modf(12.34)9 i* m. j. y9 u" n
- (0.33999999999999986, 12.0)
复制代码
) h5 y a- _8 Z4 o) w' imath.sqrt(x) 求x的平方根 M; E6 q" A% F* l0 w( F
- #求x的平方根' Z; | _4 T+ h+ b: b* W; _3 u3 f
- sqrt(x)8 | C' j W% |+ \
- Return the square root of x.
/ a9 B9 q. z( Y0 m1 I - >>> math.sqrt(100)6 z' N! b6 I+ ], E
- 10.08 t4 z3 E1 C1 F7 S4 a
- >>> math.sqrt(16) Y4 }5 Y) w9 H* S) b: u% p
- 4.0) D- h5 ~" K2 f M0 S. n
- >>> math.sqrt(20)- I% L9 \) l) S8 `/ a+ ?( y
- 4.47213595499958
复制代码
4 E2 Z6 ?7 w# ^$ s r( R) A" vmath.trunc(x) 返回x的整数部分- #返回x的整数部分
7 f k; g/ l( G5 J, N- Z; q% ?% H - trunc(x:Real) -> Integral* z0 G' P- k4 S3 U6 h$ ?0 P! Q" f. q
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
\ n2 D$ L. k. Q - >>> math.trunc(6.789)7 p+ ]2 s! f& J, H( _1 q
- 6
' h+ \- B! `1 g/ B8 k& _/ K1 A - >>> math.trunc(math.pi)2 ~& |9 T- S% D z
- 3 O( F3 p" w5 [: q* L- j( h
- >>> math.trunc(2.567)
3 D+ d9 \. k5 Y; h/ P% B6 n - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|