马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 k& t2 |7 ~( w3 F【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! ]1 `2 T% o; I% U
) X: H: a+ q9 E- C: ~/ L9 X& n" q. j' U方法1:
- z0 B7 C% M8 f2 _( w6 n- >>> import math4 Q3 Z! E) ?& ~/ r7 W
- >>> math.sqrt(9)
, Y( B2 w1 A( s; b: E" o( h - 3.0
复制代码 方法2:
' p: R) C$ ]( Z M6 I# p$ P- >>> from math import sqrt
7 g8 l- P1 b3 I) l2 n( z8 S8 B: Z& H - >>> sqrt(9)8 D9 f& F7 s w/ g5 s6 {9 X; y5 f
- 3.0
复制代码 , X5 l- G( c# N+ a, m+ X. j, @3 ]
, D- s- g5 z# ]% C! I
math.e 表示一个常量$ Y, `5 {& N1 n) `
- #表示一个常量$ z$ D" s9 h8 a6 H8 g
- >>> math.e- q; Y0 D5 Z! `6 T
- 2.718281828459045
复制代码
) @ t9 F0 u' H; wmath.pi 数字常量,圆周率) B& x& `, |; ]: ~
- #数字常量,圆周率- Y1 L! g7 `. ~, o3 c9 B; o+ F) x i
- >>> print(math.pi)0 D3 y2 Z) k; v
- 3.141592653589793
复制代码 ; o. o1 W$ a! T3 T. t
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 E0 }# H* {* m- }3 Y: C. h- #取大于等于x的最小的整数值,如果x是一个整数,则返回x4 i: `8 V; I, F6 y A5 ^
- ceil(x)+ @0 ]5 E! e% @* x" A; \1 O/ z
- Return the ceiling of x as an int.6 w [; b) h# @7 b: `( v+ X5 {
- This is the smallest integral value >= x.- j- j' a) M* G2 w& S+ f k
7 t! k4 f: a$ F- >>> math.ceil(4.01)
* O6 I& R2 O: U6 Y& J - 5, k# A2 h* c! y& K6 h2 \
- >>> math.ceil(4.99)& B- S) N5 w# V& R# H% K* q# w9 t
- 53 a) x8 Z, J. U- l- ?
- >>> math.ceil(-3.99)+ c! y7 G# A* b0 a7 A
- -3
5 M) O: W( `; [" x2 F" O! [ - >>> math.ceil(-3.01)
. i6 K2 B2 ^! O' }# ` - -3
复制代码 & I! L: X! t( V0 l ?" z: a
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 M5 ^7 D4 O9 H) A3 b" J- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身. F* d1 Z; j* ?' C, \5 h. t
- floor(x)
+ m& \4 u- W- d5 | - Return the floor of x as an int.0 U& w( e; e$ O4 E# T# y
- This is the largest integral value <= x.
: v% W- b0 f! f: G: v# O h - >>> math.floor(4.1)% k( P+ }9 n7 M1 r5 ], x7 z
- 4
3 C' ^1 h9 M4 u( A6 R; N) b8 }* @ - >>> math.floor(4.999)
! F& u/ V1 Q8 ?+ N; F. Q, o4 R g; J - 4
' v4 c$ y' ?5 Z6 o - >>> math.floor(-4.999)
! x' P% l, a9 ]( t) T - -5
, I4 \( X7 ^* J- @ - >>> math.floor(-4.01)
r" e: C k1 k6 p" } - -5
复制代码 5 ~, A r4 X7 {0 T4 w$ M7 P8 D
math.pow(x,y) 返回x的y次方,即x**y4 I7 x5 u" v* K- G8 t1 U
- #返回x的y次方,即x**y
" t( G$ p2 b; E! \9 v$ h1 y - pow(x, y)) H% y3 ~7 _. A" U6 T6 a
- Return x**y (x to the power of y).
- H' A) A- R5 }. F9 Y1 r2 H; C - >>> math.pow(3,4)
) C! b b: A! O, l - 81.0
' `- I8 ]+ x& k& h - >>>
0 c+ n% c, L: k+ [9 | - >>> math.pow(2,7)
6 c- o e$ }: k% O( n0 ^ - 128.0
复制代码
z% W; O( T% A( C: q' h* }math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' M; ^* U/ y- z! i( q; V% w- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 r6 S' @; t. |. h% F# q - log(x[, base])& \1 y0 w% A4 E9 U! j
- Return the logarithm of x to the given base.! R6 T0 F7 J$ h8 S2 \1 Q* w
- If the base not specified, returns the natural logarithm (base e) of x.
- d! M3 l; P% ` c! k. C5 Y - >>> math.log(10)( t7 ]. ?1 c) w) H. T; z* l
- 2.302585092994046
# ^& v; y% x9 j4 w& ]5 c" l+ l - >>> math.log(11)
# }" w3 h. |5 m8 ]7 n4 M2 T - 2.3978952727983707
, F. {' M( v8 i8 y. j& s* T9 ~ - >>> math.log(20)& r$ r' I: ]1 P7 L' M* B5 U- C! n
- 2.995732273553991
复制代码 / Q" d2 G h& V! |5 d
math.sin(x) 求x(x为弧度)的正弦值
! T' V. b0 O# q3 e# P1 Z4 i) G- #求x(x为弧度)的正弦值
; f1 P. G# R8 D2 @' O+ J s$ D - sin(x)
) ?6 l* V9 Z# m9 P4 @9 p6 v - Return the sine of x (measured in radians).
0 N1 }/ g+ M3 y, ], S1 c, } - >>> math.sin(math.pi/4)! K0 k& b X/ C/ [3 y
- 0.7071067811865475& c1 ]% I3 |8 z5 b1 |+ T8 }
- >>> math.sin(math.pi/2)+ O$ b( K$ j) B7 h: |
- 1.0
8 j/ t7 Z t& @: @) e5 Q5 n' p+ l' k - >>> math.sin(math.pi/3)) e$ t; @6 I2 z& @! d0 E
- 0.8660254037844386
复制代码
) t1 h! n" K( R2 n# B) i& {5 {+ ?math.cos(x) 求x的余弦,x必须是弧度
$ L; q6 z1 o2 Y6 O x- #求x的余弦,x必须是弧度
3 g/ o3 I. u$ ? - cos(x)
- ?3 J8 \- N5 r8 Z" y2 Q; @ - Return the cosine of x (measured in radians).
3 x: Q% D2 o4 }' n6 ]5 C8 U2 T - #math.pi/4表示弧度,转换成角度为45度
5 P: X: q2 R6 x/ i7 D' B - >>> math.cos(math.pi/4)
: @" H& X! p" p7 N - 0.7071067811865476" d$ `# @, f9 `5 `
- math.pi/3表示弧度,转换成角度为60度
- z; K* U6 G& ~% X2 ^* } - >>> math.cos(math.pi/3)2 K; K6 V' E$ e8 `) E9 c+ u3 U
- 0.5000000000000001
6 i) T( _! j* V3 ^& b - math.pi/6表示弧度,转换成角度为30度
( [) x# ]; x% e: m) H3 l! A - >>> math.cos(math.pi/6)8 U% u- n+ r7 u' C2 [2 Q
- 0.8660254037844387
复制代码
' X; ]9 b2 T( _( l& ^math.tan(x) 返回x(x为弧度)的正切值% K3 q: |) x5 c" x4 t2 f
- #返回x(x为弧度)的正切值
5 D' k& T \+ y - tan(x)
5 D% o' d$ E1 S. J - Return the tangent of x (measured in radians).
0 Q* h5 h6 A, t4 t2 d" y; F% s1 h - >>> math.tan(math.pi/4)8 M, _( d: ?1 f, g
- 0.9999999999999999
2 b3 s; w. {6 D - >>> math.tan(math.pi/6)
8 A$ v8 T. b3 ?- D) ~0 q - 0.57735026918962571 a' M0 U6 @, F! S1 y9 L
- >>> math.tan(math.pi/3)& I' d5 U' H. }/ d: t9 ?& |3 I2 q, ?
- 1.7320508075688767
复制代码
# G0 N8 F: X+ K6 y/ Amath.degrees(x) 把x从弧度转换成角度
% y& d- w, m" s: m+ D Y0 _- #把x从弧度转换成角度6 \3 }; Q5 l. r6 V) m
- degrees(x); {9 _4 d* d0 { z7 X# y) ?# `
- Convert angle x from radians to degrees.
# {' r& D& F6 k# D - / L( O# t' t3 u/ h! j7 r
- >>> math.degrees(math.pi/4); {8 g! I& `* ]1 o$ Q, ^3 F. C- g$ b
- 45.0
- h" ~( J! }- @" R. m - >>> math.degrees(math.pi)6 R' l( B& V8 W5 Q; v
- 180.0
I4 L' f K# Q, r* w% M3 r5 i' ] - >>> math.degrees(math.pi/6)+ v( ^. O1 G: A3 x4 B- F
- 29.999999999999996
$ O2 ~0 t/ a) f8 Z' d - >>> math.degrees(math.pi/3)2 `" |2 t( A4 o) @/ j# b& N: d, }& ]
- 59.99999999999999
复制代码 4 P0 G* z8 I0 ^/ z+ \
math.radians(x) 把角度x转换成弧度( W: T m7 p' ]8 X( ^- h
- #把角度x转换成弧度
a/ h# T) U9 t: V5 d. {3 a* o; ^ - radians(x)5 W/ v" Z; [3 R5 ^* m, L
- Convert angle x from degrees to radians.8 r5 z. U, V) a* J6 d* |8 }9 D/ s. T
- >>> math.radians(45)( b! t }0 Y1 F% f i6 b
- 0.78539816339744833 T8 [ |& b6 y( j. v$ {
- >>> math.radians(60)' S; r4 W% D Z) q. K9 {5 b: I& L
- 1.0471975511965976
复制代码 2 P# P3 M4 J2 g. ]
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
5 X0 g* N2 ^; k$ ~7 w7 z; |- #把y的正负号加到x前面,可以使用0
$ n, A* P# }7 C; c J - copysign(x, y)6 Y9 s& f1 O" B& O0 Q5 |
- Return a float with the magnitude (absolute value) of x but the sign 9 r3 X% ^/ v4 c1 Y( |
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
/ C; H4 X% t O: E" @ - returns -1.0.: C0 v% y r5 n, f- d9 v
- % X9 g+ z1 p# f+ s
- >>> math.copysign(2,3)
1 I: o# S& f3 I. r+ l3 L; Y - 2.0
( Q5 a0 F3 f) C/ S" ]/ W7 l* L- i - >>> math.copysign(2,-3)# K! v7 |% P- M/ b9 r
- -2.0, ~% ~! @8 s' k( q. k7 \- m
- >>> math.copysign(3,8)
/ X" r9 D5 _1 q$ w - 3.0
" o: p7 w! N% z: L - >>> math.copysign(3,-8)
}8 ` q; K+ y' Z3 c - -3.0
复制代码 # r; C: z3 f7 h2 ]* t
math.exp(x) 返回math.e,也就是2.71828的x次方/ U; n, |( Q' c; b9 C
- #返回math.e,也就是2.71828的x次方
- X* y" l4 R) C" _% v6 P" t& | - exp(x)5 W& H5 l( h/ O4 e2 Q2 j3 A X
- Return e raised to the power of x.4 O$ M% n# E& g) Q2 G& X( @
- ) n, |4 A$ n/ d- R3 C8 d
- >>> math.exp(1)1 ^0 x! c- d6 i& ]8 H5 e- W5 q/ X
- 2.718281828459045
* Z& d: H W9 y+ s& z9 V- I3 ]% f - >>> math.exp(2)8 ~* T: ?; o. s. N: J R+ n6 C! s6 O
- 7.38905609893065* \/ u* O2 W- G* g0 E& Q
- >>> math.exp(3)
" \: k( s) ~6 n7 i7 t) @$ Z# P - 20.085536923187668
复制代码 ) L3 {+ k+ P( e* r8 |" L- a
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减14 @; e3 M9 ^0 I; T7 U
- #返回math.e的x(其值为2.71828)次方的值减1: v4 C& P* z4 C! `6 j9 o
- expm1(x)1 k7 O6 c! \- T1 I" l. Q
- Return exp(x)-1.) R; k$ `( K4 y4 L
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; {+ O) E+ H$ Z2 [1 I
2 [- o9 k' v2 @; w- >>> math.expm1(1)
. B! j' W2 u0 W" X - 1.718281828459045( t/ L8 c; ]* a: m' t! U1 f
- >>> math.expm1(2)
3 D8 t& ?0 R7 B$ p9 K - 6.38905609893065
! @! j+ d* y1 ] - >>> math.expm1(3)
5 K1 I( f) ^9 r8 M4 f - 19.085536923187668
复制代码 ( h5 m C, U) Z: k* K
math.fabs(x) 返回x的绝对值5 R5 @$ @; I, j3 w7 ?, D6 L8 U
- #返回x的绝对值" L+ P$ n3 N" Q( `
- fabs(x), D! d7 v; U$ m6 U# i1 h* d
- Return the absolute value of the float x.7 Y6 C4 D& J* A6 A/ X% I( `& L
- - y0 m. B' p# m% y% v) m8 D
- >>> math.fabs(-0.003)
. o7 Z1 f5 H& }, t* L" A5 { - 0.003
4 M! E9 T7 W- _) ^1 e+ J! e - >>> math.fabs(-110)
: q0 E" Q+ j5 y2 ^ - 110.0
1 a4 N7 p/ b; l6 ]- `1 _/ @$ h! s; b - >>> math.fabs(100)
9 J! S& z; @! e: I y - 100.0
复制代码 B, ^6 T, a% W+ X9 y
math.factorial(x) 取x的阶乘的值2 [) i. N: P- S& N, l) ?& L9 h
- #取x的阶乘的值
. L9 G. z: Y9 E7 h5 V - factorial(x) -> Integral0 F& E9 v* ?' O- _0 q$ w! N \3 o
- Find x!. Raise a ValueError if x is negative or non-integral.
( y/ L+ ~4 @6 z d/ L! G/ ^3 m" _ - >>> math.factorial(1)
1 j& f! w. G( E( A) R3 @/ K - 1
* i4 L, U5 T5 [( p- f - >>> math.factorial(2)/ g$ d+ a7 O7 n
- 2, h7 c8 Q" L3 q7 H. d0 W! v
- >>> math.factorial(3)0 X/ n/ o+ O- m; _
- 6
4 Y7 f& [6 O4 o( Z# |) d - >>> math.factorial(5) v0 @+ i% \" L6 t5 \
- 120
4 ^( h. _" a3 i. o8 \ - >>> math.factorial(10)- k8 \; k4 B1 [% ]* n( d9 E
- 3628800
复制代码 0 K8 V) D9 L0 D( S9 K5 j# V: d
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
3 \2 k) l/ [) y' p. @3 r: y- #得到x/y的余数,其值是一个浮点数
" B. J# K' ~+ T+ l2 L - fmod(x, y), G @' @/ o3 \! N9 G
- Return fmod(x, y), according to platform C. x % y may differ./ m, S8 a0 T4 q
- >>> math.fmod(20,3), w/ z3 W' W7 I$ n9 A2 Y4 o; P5 E( W. r
- 2.04 O, I+ V# ]+ r9 v2 e3 }% r
- >>> math.fmod(20,7)
( z* V- _" t* R) V: ~0 P# N - 6.0
复制代码 . b7 N( q. S* W, F e
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ ]- X% }" L% n; [
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 j6 s: ]; F" w7 [) H L
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 d) e+ W5 y& ~. w$ d7 d
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ r/ K( A7 T$ [5 d
- frexp(x)! m( f8 v( T+ F+ x2 r* ^
- Return the mantissa and exponent of x, as pair (m, e).8 R7 M/ `( ]% s3 _2 Y
- m is a float and e is an int, such that x = m * 2.**e.3 g" d8 O* A+ u- [3 K! U. U& b
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
4 c( H2 M/ x! N9 k) R) B - >>> math.frexp(10)
t5 ]/ o. x2 c/ \% ?" s - (0.625, 4); O: M% m7 }6 n& r9 Z
- >>> math.frexp(75)- ], Q5 S: M5 N( X8 v
- (0.5859375, 7)- W- n) B$ p( O: p3 a' r# R2 _1 X
- >>> math.frexp(-40): ^- f% I& k6 E) A; B ^
- (-0.625, 6)2 l1 a$ ]) t9 U, |2 J* E+ {6 o
- >>> math.frexp(-100)
8 c2 a+ X4 Y: b - (-0.78125, 7); e2 s9 q# s6 F; Y4 U1 ]" J
- >>> math.frexp(100)' x5 X9 l& N2 `( i
- (0.78125, 7)
复制代码 & x6 h% y% Q% ?0 ~0 k3 V0 |
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
( L$ O: v% V% R$ |2 c- #对迭代器里的每个元素进行求和操作+ W; r* L2 h% _) O- e
- fsum(iterable)
# ?& c# y' M+ o$ |9 k! x& B - Return an accurate floating point sum of values in the iterable.
6 F4 I+ K8 o8 G& G5 K8 G- o/ { - Assumes IEEE-754 floating point arithmetic.0 E( S V9 A: E1 z4 I+ I
- >>> math.fsum([1,2,3,4])
) k2 Y. D1 s7 D0 P+ l. s - 10.0: x: `' t+ D* { d7 h( ?- o
- >>> math.fsum((1,2,3,4))- G! H: L1 U" m% Q" G+ J, Y3 @& }
- 10.0
8 q( U+ z! Q3 J - >>> math.fsum((-1,-2,-3,-4))% l# Z( e! [6 m6 ~" l( ~
- -10.06 Y- U' k6 N& \, U: Q
- >>> math.fsum([-1,-2,-3,-4])0 x9 l* I: ^' J r G5 b
- -10.0
复制代码 4 h* R6 n6 D% v2 N9 S, s
math.gcd(x,y) 返回x和y的最大公约数
8 m1 J! W4 d0 d- a/ G" H2 h- #返回x和y的最大公约数9 Q3 k3 ]7 A: G G( h
- gcd(x, y) -> int
, ~7 U$ F6 Q) N2 n+ t: d - greatest common divisor of x and y
1 i% @) A2 |9 r. \# I! [4 f - >>> math.gcd(8,6)
$ @9 T1 x9 U) ? - 2
' a, K0 C, d7 c" |- E, n/ l. O - >>> math.gcd(40,20)
0 h6 f) K( B6 L - 20
7 T" L' o# ~* m" t0 e$ @ u0 b - >>> math.gcd(8,12)5 h, v, i/ b4 o. R1 V8 K1 U
- 4
复制代码
9 i8 f. m, G. ~5 K% i$ dmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
: {, ~" m6 I: O1 s* E- #得到(x**2+y**2),平方的值
0 L8 W" k% W; e; ^) i. _& q) L - hypot(x, y)
3 K f* {" j! p- c% g8 e - Return the Euclidean distance, sqrt(x*x + y*y).
' J& j2 n/ Q" T/ p/ v8 S - >>> math.hypot(3,4): B! E- [( G3 i8 { m, R
- 5.0" a2 z9 T; R. D0 q, y2 k, N
- >>> math.hypot(6,8)$ e* ^( f( a* j( d. S
- 10.0
复制代码 4 I* N E* s2 @/ O
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 E X: q% a+ q
- #如果x是不是无穷大的数字,则返回True,否则返回False
8 f2 c# y! y6 | C* u; M& x - isfinite(x) -> bool) h6 C# ~0 Y0 B7 ?
- Return True if x is neither an infinity nor a NaN, and False otherwise.. V1 y2 b% ^2 t; w+ \4 R! S9 S
- >>> math.isfinite(100)3 }( B( ]# v2 i0 `8 q) p2 t
- True& l* s8 N3 r$ |0 \+ a; [: e
- >>> math.isfinite(0)3 Z6 _% o3 s/ t
- True3 T3 E, C9 T! P( A9 K" X
- >>> math.isfinite(0.1)4 m1 I2 A; @! n( j
- True. a# z* r. W! d- k x3 ?0 w
- >>> math.isfinite("a"): D+ e! Y( T: w. r; |% x
- >>> math.isfinite(0.0001)" j9 j% X' B: l. J+ j% m- n9 Y
- True
复制代码
# X. F3 `; J3 \math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ w' F3 s8 c: p$ X. g8 S$ k- #如果x是正无穷大或负无穷大,则返回True,否则返回False& x) [% H/ m1 K: ]
- isinf(x) -> bool
7 m, x8 E5 b6 |6 r - Return True if x is a positive or negative infinity, and False otherwise.: v8 B- e1 @) L9 e2 Z$ s
- >>> math.isinf(234)
( s1 J3 _3 Y! H - False
$ W6 V; y8 l" H6 y - >>> math.isinf(0.1)) Q* d% U( m' X$ Q: J+ V
- False
复制代码 ! F: _ b5 C+ X, W5 O, ~
math.isnan(x) 如果x不是数字True,否则返回False
* q/ p+ _+ a# w' d- #如果x不是数字True,否则返回False1 n, A9 a! P9 g
- isnan(x) -> bool
N6 r! h/ h" D$ H- J1 n - Return True if x is a NaN (not a number), and False otherwise., K- u- X- \1 v6 _, L5 i
- >>> math.isnan(23)2 B8 J- n! k: b; [1 I/ v
- False6 K7 l' W; B K/ e
- >>> math.isnan(0.01)
2 }3 I3 J0 a6 s) X* p/ }. Z) A - False
复制代码 - T" h. }4 E! e# Y# k j
math.ldexp(x,i) 返回x*(2**i)的值) G3 H7 e( M! Y% \& B1 f: ^
- #返回x*(2**i)的值8 H, s. G7 O8 }2 I1 ~
- ldexp(x, i)
. u q8 Z, H+ g& s3 @1 R - Return x * (2**i).0 C v0 l! e" a# ]7 F4 T! p1 E8 y
- >>> math.ldexp(5,5)' B" y+ F! z9 |0 O
- 160.09 {1 d( }4 {; T& T8 Z' ^* `0 `
- >>> math.ldexp(3,5)# f, j8 H1 x8 M" A6 K
- 96.0
复制代码 , i* ?$ Z: e" X7 b; Z
math.log10(x) 返回x的以10为底的对数
0 d8 U* L6 \' e+ \) k; |. c- A- v- #返回x的以10为底的对数
! ^+ M" Q5 O$ v( Y2 I - log10(x)
' g9 G; N& ]$ m3 P2 z& m, s3 | - Return the base 10 logarithm of x.
/ A$ ~ L) k% ^- H7 _ - >>> math.log10(10)/ C. R" b7 v+ o) w# n9 H7 M
- 1.0( a+ M2 y% n7 m/ j, e4 W$ u1 p
- >>> math.log10(100)
# x$ Q# w$ J. p) i1 T( o - 2.0* x* j/ C3 X+ O- I% _ }4 J2 v
- #即10的1.3次方的结果为20
4 q0 ^" E! P" O) `3 x- T - >>> math.log10(20)
6 j* s. o* Z' W. P3 j! x1 _9 p - 1.3010299956639813
复制代码
2 |* N) {0 M8 F2 x) Rmath.log1p(x) 返回x+1的自然对数(基数为e)的值
2 k: O( ^0 u6 ^% |- g- #返回x+1的自然对数(基数为e)的值
; \& C( F- \1 b' E$ G - log1p(x)
: ]$ f* p3 ^' ?8 ?' r( x# m - Return the natural logarithm of 1+x (base e).. w7 d* O" b8 I- ^5 F* d5 y4 m
- The result is computed in a way which is accurate for x near zero.0 {; Z+ Z7 O$ s, B$ N1 K: V( h
- >>> math.log(10)
& v( S# J( c0 L, o4 g! M% c - 2.302585092994046( c' @, m/ s4 u$ D9 V3 Y5 H) I8 _* n
- >>> math.log1p(10)
9 T: k) v3 O1 I - 2.3978952727983707
6 c. w% P/ ] v9 b! n; |7 h' {2 j" x - >>> math.log(11), S5 U5 e/ k# y, U4 b5 E
- 2.3978952727983707
复制代码
# h/ g' ~; U- d U3 Vmath.log2(x) 返回x的基2对数
3 c6 f" ]% S7 D* s5 d+ R+ f- #返回x的基2对数
4 ^" m; D8 O/ b( {+ h$ C8 y - log2(x)3 a* h8 l S9 d6 }
- Return the base 2 logarithm of x.
9 s1 ]( z# W* V2 E" C - >>> math.log2(32)
" B. c" [, |; w+ R \' i - 5.0
+ Z; n% n: F7 } - >>> math.log2(20)
6 c V! c. r" j2 c( o6 p1 u - 4.321928094887363
$ Z9 L; m- G$ z6 v/ C - >>> math.log2(16)
3 n' O4 @3 K5 P( U. C, {/ M7 A X - 4.0
复制代码
% s3 i$ d. d# u6 \) Omath.modf(x) 返回由x的小数部分和整数部分组成的元组
8 J! _) ]) h; q* U) Y- #返回由x的小数部分和整数部分组成的元组3 `1 V. P4 @. O. x+ |
- modf(x)
+ q% d2 T7 t& {: d7 l1 g0 s - Return the fractional and integer parts of x. Both results carry the sign
: ]$ R- A6 X& A% a5 J( @ - of x and are floats.5 O/ l8 ]5 b: W0 u/ j
- >>> math.modf(math.pi)
: P" w& a! W* O& F) w: a - (0.14159265358979312, 3.0)
% h. G9 H- V% J1 W) G/ _" q# Q* z - >>> math.modf(12.34)
, y# a j! u6 G1 N7 W - (0.33999999999999986, 12.0)
复制代码 " L$ Z3 w5 X6 l& h8 W# J' I
math.sqrt(x) 求x的平方根. I7 T) j/ R; `) h' S' z; K# g
- #求x的平方根
. z' W% R! V' |* B - sqrt(x)9 ?" c% V4 ?) i" v) [* @- h3 o8 g
- Return the square root of x.
& ^; l0 v( o" ^6 @) U% z - >>> math.sqrt(100)* M, b2 e" h" P$ ^
- 10.0( I9 Q' d+ ]% `" b9 `+ {
- >>> math.sqrt(16)
# K% U0 f- r, G P - 4.0
# \; k; p6 a6 A* Z6 J' F6 R - >>> math.sqrt(20)
+ R ^3 _" Y5 Q% Q - 4.47213595499958
复制代码 " S" ?$ p/ W, X/ }8 o" |% l! h
math.trunc(x) 返回x的整数部分- #返回x的整数部分
& i/ \" L. r+ C ]% f" |% o9 v - trunc(x:Real) -> Integral
% L N5 F8 A1 p W0 v - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
& b, U) D$ S, {2 l2 a# Z - >>> math.trunc(6.789)
% y9 a+ h$ `- d - 6 ?8 k2 C; e* N: P
- >>> math.trunc(math.pi)
# L8 {1 q' H# G% w" S - 3/ O- n: J3 a& v+ J
- >>> math.trunc(2.567)7 p& ?* I5 l. u( a+ o1 T
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|