马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
& r( r$ k, e1 E: l1 [
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。3 o% }) R* ~: ?
' |6 C: _; b ~
方法1:3 o, N- n4 X( g `
- >>> import math; Q4 `& V8 l% J0 ^3 s! r" d+ z
- >>> math.sqrt(9)
6 b2 @3 V H4 P/ Q( e7 n - 3.0
复制代码 方法2:
( i4 O% E7 o+ h3 E( D" c9 Q- >>> from math import sqrt
$ D8 ]0 u1 Z* ]9 x3 Q% O4 @/ r - >>> sqrt(9)
/ A. K0 r) a' Y4 o; ]' j0 { - 3.0
复制代码
' O2 [! ]+ s- ~* e' Z7 W
! X, X. I: S2 I7 J4 fmath.e 表示一个常量+ i9 j& T6 Q; h2 x. J$ X
- #表示一个常量) e/ H. C, ?, W4 ~
- >>> math.e( Y4 ~' _ P; y0 O, S/ I6 t
- 2.718281828459045
复制代码 9 p2 g+ S5 i, }" d
math.pi 数字常量,圆周率
3 Y2 h0 G. p. c: C) e- #数字常量,圆周率
8 i! q, g2 F& v3 w - >>> print(math.pi)
: k( O& ~/ i: h) u, C - 3.141592653589793
复制代码
' U7 Z. `6 D, i1 f% _) N& ]math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 c) y9 w! S s, J; W' V" B* ~- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 ], y1 b2 \6 T - ceil(x)7 L) q/ P }7 S) B1 N
- Return the ceiling of x as an int.
3 F1 b8 ]5 \4 Q7 {9 s2 G1 _' `9 c - This is the smallest integral value >= x.
& Z7 w% X! I3 ~# X- r1 k
! B2 M4 W4 t/ M2 g; Z( b$ g" g/ x: g- >>> math.ceil(4.01)
* e6 P$ [" ^, l; a! U9 ^4 U, A - 5
8 \1 K c$ @5 f" c - >>> math.ceil(4.99)6 f. k6 t" n- z3 z2 L7 B- F& m
- 5% k* g) s1 v1 {
- >>> math.ceil(-3.99)5 m) K: p2 \4 p# w9 f, u
- -3: Z- |5 `0 d! e# [' {! o
- >>> math.ceil(-3.01)
( M1 @, R" F* R1 S5 j. W8 N - -3
复制代码 0 k6 m7 `& y; e/ F3 O
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ g" @9 ]5 N5 [- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
t) h. m+ ^: D& a. _ - floor(x)4 j; o. h+ S* ^) P Q. f. p# p7 k
- Return the floor of x as an int.7 E; d! K1 t( i$ m& ]
- This is the largest integral value <= x.# M& }. ^1 m6 n( z( h. [! c5 H& A+ v
- >>> math.floor(4.1)
: d$ B1 v: Q c/ j - 4" W" H1 ?# |2 G( a ]9 Q
- >>> math.floor(4.999)8 g+ d) [! Z( k4 t7 J0 C" g
- 4; @ I6 z$ L' ], F
- >>> math.floor(-4.999)- z, B; h" h0 W
- -5# @7 n S- t# C% l. W4 q
- >>> math.floor(-4.01): Q% w2 C- S q: b- H1 |
- -5
复制代码
: n: L' \6 K+ N- s6 |math.pow(x,y) 返回x的y次方,即x**y
5 C4 U& }7 f2 R; j' @9 ]- #返回x的y次方,即x**y
, ^2 ?% c3 a6 W: P7 R, b3 m8 A+ E - pow(x, y)
* i/ r3 A7 Y$ w2 J0 ? - Return x**y (x to the power of y).
. W; b8 b. w! z+ S8 d, E! e4 b5 ^ - >>> math.pow(3,4)
/ H+ Y2 `7 x4 L9 I/ H, P& O. ^4 t% A - 81.0
4 U7 b- m! L/ R4 R - >>>
7 I( @& G( t; d! n7 p! ~ - >>> math.pow(2,7)! y3 Q' F- Y% B1 \9 k' m
- 128.0
复制代码
1 i5 E* l0 q& m. X% qmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ i# F" F) R9 C
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 ?# N1 O2 ?% h
- log(x[, base])
+ i& T5 h ~/ e+ [) | - Return the logarithm of x to the given base.. I5 a3 n& S$ C
- If the base not specified, returns the natural logarithm (base e) of x., U2 Q+ d9 t* B+ O6 u
- >>> math.log(10)4 {. [" Z+ V! l/ s, B
- 2.302585092994046) m/ Y# w+ \1 o* @ f$ u; `
- >>> math.log(11); a9 c, d5 K6 a8 v$ A* d4 a4 e
- 2.3978952727983707( s# o9 y' F+ Y P& b; M
- >>> math.log(20) [0 i {" f0 K( z5 p8 [
- 2.995732273553991
复制代码 7 y) \' M/ [+ P, i3 l
math.sin(x) 求x(x为弧度)的正弦值5 R+ \) A. O" ~
- #求x(x为弧度)的正弦值
% g6 D$ o% e7 |/ s - sin(x)
- }0 y+ O- `0 _3 E1 G - Return the sine of x (measured in radians).
! X0 j# C$ J/ r4 Y3 P9 _, [; I+ } - >>> math.sin(math.pi/4)0 b& s! x9 F9 y ?3 G) `
- 0.7071067811865475" n* v: ]; ~4 D+ P6 ?
- >>> math.sin(math.pi/2)0 \+ h9 T; H7 c* p3 L/ b6 k
- 1.0
' {4 {% P8 n; W+ p; e - >>> math.sin(math.pi/3)
' o6 F! [6 O; Q* W: Y$ u - 0.8660254037844386
复制代码 2 p. u' ?# Y7 ?
math.cos(x) 求x的余弦,x必须是弧度
) r) Y& I5 j; l( R; P- #求x的余弦,x必须是弧度
5 v0 X2 @" `% K - cos(x)
) P4 N* u6 n7 [5 K - Return the cosine of x (measured in radians).
* |( Z; T( E) r& D - #math.pi/4表示弧度,转换成角度为45度% M+ S8 b+ I5 B L
- >>> math.cos(math.pi/4)
/ \2 u8 n/ P2 [5 g1 q2 a/ ? - 0.7071067811865476$ [5 Y8 O! J8 ] n
- math.pi/3表示弧度,转换成角度为60度! Q' o, p+ m5 k) @' ]# r
- >>> math.cos(math.pi/3)
8 w8 f/ N. b* t8 _- y' l# o - 0.5000000000000001
1 E4 H) x& Y; \. O# p2 u& f, n4 A - math.pi/6表示弧度,转换成角度为30度1 @% g; p( E; n
- >>> math.cos(math.pi/6). }) d r. b% C- l& x" M5 | p% `
- 0.8660254037844387
复制代码 6 U& o' b0 L4 c9 V' w4 Z/ A
math.tan(x) 返回x(x为弧度)的正切值
8 n6 `4 {3 y2 c$ s& [! F$ n1 c- #返回x(x为弧度)的正切值: F$ G2 I8 O& c/ x# Y
- tan(x)
; G! p9 U Z5 G - Return the tangent of x (measured in radians)." k' `0 \, ^( P$ o, I* T' h2 I
- >>> math.tan(math.pi/4)
* F8 _- U) v1 J% B6 v1 R - 0.9999999999999999
1 ?; D2 ]( t% Y - >>> math.tan(math.pi/6)
6 K6 R+ g& o/ q# g0 M% | - 0.5773502691896257
1 N( w- p) R# s8 i# l - >>> math.tan(math.pi/3)
$ x B* r/ m' x5 f, Q6 _ - 1.7320508075688767
复制代码
& c& o- h1 P$ J, V- l! Cmath.degrees(x) 把x从弧度转换成角度
: u# P& H* l" ^" C* C1 a- #把x从弧度转换成角度
( e* H9 ?: c7 N! w. k4 [ - degrees(x)
# P% X/ m, E% B7 G1 _$ y. s - Convert angle x from radians to degrees.
% s7 l9 p/ m L% q3 L9 ? j4 p* I
' Y- Q+ M2 B5 O; l: w, n( I4 k, O- >>> math.degrees(math.pi/4)1 t6 C J+ F0 K
- 45.0; A# ~$ J0 \6 V4 {" M
- >>> math.degrees(math.pi)( q# K) r J$ }, n
- 180.0
. G8 {! X& Q2 N) R4 ? - >>> math.degrees(math.pi/6)
: V4 }' `/ I8 I$ C: Y5 j; U - 29.999999999999996
, p* `' A* K6 J3 a( n' ^1 R - >>> math.degrees(math.pi/3)
' j5 }" H' p) q6 T - 59.99999999999999
复制代码 # [0 N, J1 u+ S! i* ?; p; q! ]
math.radians(x) 把角度x转换成弧度
$ I$ |; s- s$ X- W/ _3 K! e- #把角度x转换成弧度
3 S b" F) Z/ z; E$ ?1 x7 r6 H - radians(x)
. Y1 j( c ~$ y3 B8 h& j1 b/ k - Convert angle x from degrees to radians.
0 v! A: W6 P# x* [ - >>> math.radians(45)
& u( s9 U# |+ ?* o* n, W - 0.7853981633974483
! H3 |" g7 w+ N( A* F - >>> math.radians(60)' L! @, R5 i( V+ j8 W3 M9 [; \
- 1.0471975511965976
复制代码
: F/ A+ _' j" q* emath.copysign(x,y) 把y的正负号加到x前面,可以使用0: I8 C' g5 t" c/ a$ K9 a: O
- #把y的正负号加到x前面,可以使用0. h2 L6 m3 G* P) ]
- copysign(x, y)
: S8 p1 j, i, ] - Return a float with the magnitude (absolute value) of x but the sign * x1 i0 z- H' _6 p8 P4 M) ]
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
: O0 m$ D$ u' P' b - returns -1.0.$ w8 V' k2 ^! Q. z4 d7 ^8 F
9 \5 Z0 a& w* P3 c) r2 H0 W) K- R- >>> math.copysign(2,3)
: ~5 K9 m( w. A8 k, H: {. b - 2.0
" n; h7 b: A! A* n - >>> math.copysign(2,-3)
9 f8 y$ ], H+ ^7 R; H/ T - -2.0
+ I! u* n! m0 j, V: U, | - >>> math.copysign(3,8)) C9 [: n! x0 }2 {3 g4 J
- 3.0
8 |/ @2 m* u# j9 O - >>> math.copysign(3,-8)
/ w) h5 Z4 t# {/ l - -3.0
复制代码
T0 Q7 q) Y A" Z; t/ t9 }math.exp(x) 返回math.e,也就是2.71828的x次方0 O( i* d3 R$ V2 I: Z
- #返回math.e,也就是2.71828的x次方& W1 F3 f7 o+ h( P/ o3 U0 w
- exp(x)2 j k1 s9 J; F, ~7 G" Y! B! s
- Return e raised to the power of x.& |' `: D ~, z& C$ [# V
- " O0 [3 K- |0 f+ P7 i d& L
- >>> math.exp(1)
! y- j" V6 W5 S t5 q6 j, l/ ^, h - 2.718281828459045
: W. f U' @( o4 s; a3 R0 g# @ - >>> math.exp(2)
+ k( v3 G' F( G - 7.38905609893065+ N6 e/ `5 o$ P8 x; \6 W2 Y# g
- >>> math.exp(3)4 K) D1 Y( G, W# x" Y
- 20.085536923187668
复制代码
, D, d+ f/ A. u8 Z9 N0 Lmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
" _/ s6 ?% {' l& f; l8 M$ B- #返回math.e的x(其值为2.71828)次方的值减1 d" m2 F8 ]' S# X
- expm1(x)
- J" ?1 x# T7 D# z1 g - Return exp(x)-1.
) D' c, N) E# P+ A - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
0 \/ E& C, P9 b5 b9 V5 e+ k% H
; L& ^+ r- y- U) i8 w- b. q$ v/ B- >>> math.expm1(1)
4 W Y# [+ G% O3 v( N - 1.718281828459045
& l" u: e8 S, n6 }5 | - >>> math.expm1(2)
) l6 A8 _2 u J9 ^ - 6.38905609893065
/ s9 H {( R) |- e0 X* ^6 o - >>> math.expm1(3); V& ?4 R8 A6 F2 R* D
- 19.085536923187668
复制代码 7 C0 w' S4 r! ^; r) e! m1 U
math.fabs(x) 返回x的绝对值
0 d, i6 o1 h5 I0 B2 h( B- #返回x的绝对值
& a& n. v& l |9 M: e# O: p; p - fabs(x)
2 N9 ]' h$ L& G+ S7 E - Return the absolute value of the float x.
* z1 O. s. W% A8 J8 N0 B - & b& _5 ?/ D/ t9 v" i7 O9 z! h
- >>> math.fabs(-0.003)
+ p7 w% T! C. z' V0 r9 M - 0.003: _8 |& `" p x: P7 D7 X
- >>> math.fabs(-110) V: @4 H5 w/ Z( G0 ]
- 110.0( m# u. Z" f$ C0 F! u1 i$ z
- >>> math.fabs(100)# H. [! z+ c# a3 M
- 100.0
复制代码 ( D$ i5 ^% l# W8 X* H* E6 \% C3 B
math.factorial(x) 取x的阶乘的值* G) t+ I# l F0 ^
- #取x的阶乘的值
5 t0 X! `- m- r - factorial(x) -> Integral
( y" {' ^; u: B$ J" d+ \# Z/ r7 T' K - Find x!. Raise a ValueError if x is negative or non-integral.
5 M& O I7 `- }2 z& X5 |( E1 ` - >>> math.factorial(1)
# ^9 D: E _7 H7 D- w B, t- u - 1
6 H, |( F: ?5 [1 _( f( b }5 _2 n2 e - >>> math.factorial(2)
, G3 _) {% T# t& e$ m - 2
% I4 b' h2 M2 e7 B1 P - >>> math.factorial(3)
3 T1 a$ _! q% ]: o" t' t - 60 h5 u1 M% L% B' S5 Q5 X& V
- >>> math.factorial(5)/ K( P( l' d5 `& c
- 120
7 N7 i" J( t6 c* b+ r) ` - >>> math.factorial(10)3 d7 y* e* w, V+ ]) R
- 3628800
复制代码 8 z& X. s! {" X- t
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
; k7 t) v1 [" R. n# D- #得到x/y的余数,其值是一个浮点数! b4 a8 Y+ a6 }( d/ q
- fmod(x, y)
; M) k& p! P# @. _8 e+ ?' i8 A - Return fmod(x, y), according to platform C. x % y may differ.
/ [; s- ^/ Y, J- j - >>> math.fmod(20,3)
( _ g* o" Q9 @/ b - 2.0 i+ q+ X, z) j" N+ \- W2 P
- >>> math.fmod(20,7), R! T( c1 a5 r! o% w5 V6 c
- 6.0
复制代码
1 D' y# P. E4 u# B: C9 L Pmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ ~$ P- I, G$ u" V* k& Y2 {- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' E( R& j" o; v. g: ~; y( c" f3 _9 w
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
2 G8 F: p5 Z x' s% F8 R7 { - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 u5 P" k% A1 t' @' { N2 d - frexp(x)
# }- f0 { L8 X& s. { - Return the mantissa and exponent of x, as pair (m, e).+ K) H2 ~* T$ G6 P' M: ]0 Z
- m is a float and e is an int, such that x = m * 2.**e.
& d& ]0 S2 m5 [1 ?- J: Q4 i - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- D6 \( D* N0 a( o' f6 Y
- >>> math.frexp(10)- j4 p; U& ?! k" ~, S! p' E- g9 k1 C
- (0.625, 4)
' {4 g4 g9 m+ j& f9 K3 n. X4 g - >>> math.frexp(75)
* Z' \+ _$ k' S7 ]! }% ? - (0.5859375, 7)
- u y& }+ X: R( i( N, s - >>> math.frexp(-40)
7 e F3 e4 n: y! o; t' w - (-0.625, 6)
$ N* g2 W5 _ w - >>> math.frexp(-100)/ g E/ B5 g7 A H, v3 s
- (-0.78125, 7)$ K3 f" R+ O" D0 l. Y4 Y6 p
- >>> math.frexp(100)
8 r3 p; A) h, O) O1 W7 g* G - (0.78125, 7)
复制代码 % }5 |& h. u' X+ \$ S: {
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
% t) K- Y7 K* Y: I5 G4 x- #对迭代器里的每个元素进行求和操作
/ X4 H6 ~- a( K) N4 Y8 {8 W - fsum(iterable)
# g# x! B- W) L0 h - Return an accurate floating point sum of values in the iterable.
7 h. h4 |7 j, K( `) P7 y. p# y - Assumes IEEE-754 floating point arithmetic.
9 J$ E( K! Q* I# R! z3 Z" c - >>> math.fsum([1,2,3,4])
; C+ u6 |; W8 y: D2 A* K - 10.0
! o& C! }9 H/ ~( c+ _ - >>> math.fsum((1,2,3,4))
5 u6 D) K4 k$ w: T$ X, A1 `' Q0 v& g - 10.0
/ i( [: Q0 e7 P - >>> math.fsum((-1,-2,-3,-4))2 l8 B7 ]$ `) o+ r4 C+ P; \$ o
- -10.0
/ ?$ Z7 F3 h: \, U7 \+ r - >>> math.fsum([-1,-2,-3,-4])
; d8 j4 }, P2 u" `* H - -10.0
复制代码
# h$ w- T7 G$ b) r& k/ V% O) _math.gcd(x,y) 返回x和y的最大公约数0 ^, a" g* S! p' X
- #返回x和y的最大公约数3 i* b. Y% K* l: b) m
- gcd(x, y) -> int) `9 }. I7 Q) U, Y/ G) n
- greatest common divisor of x and y
0 E+ S- X- d* g3 Q4 w+ Q - >>> math.gcd(8,6)
# w' H0 }4 S5 Y/ a. b - 2, Z7 c; A! D# @4 c
- >>> math.gcd(40,20)
' a7 H3 d0 g8 N% b7 R2 ~1 X, g* z - 20% z4 f9 i& m% g" l5 |& Q! _
- >>> math.gcd(8,12)
9 p' K( {" J3 @2 [; G - 4
复制代码
/ p! O* j7 l) ^math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
) r3 \4 a5 l+ c- #得到(x**2+y**2),平方的值) |1 J; e/ f8 `9 E2 G
- hypot(x, y)( o8 _* A+ h% y2 S ]! u% Q
- Return the Euclidean distance, sqrt(x*x + y*y).
5 r+ B* m) B- n/ w1 y* I - >>> math.hypot(3,4)3 I& l F- N" ?* p, U" i# o% R
- 5.0( s P% N q" }2 }. O- L. Z
- >>> math.hypot(6,8)
8 ]( H: t5 C" {5 B9 w1 D0 P; T - 10.0
复制代码
; l8 ~3 e |' V+ [; [/ @math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
3 X1 a- Z% K$ n$ N4 f. `- Q8 `- #如果x是不是无穷大的数字,则返回True,否则返回False! O; M% B" S) X7 X
- isfinite(x) -> bool* @. {! u3 G1 u- W
- Return True if x is neither an infinity nor a NaN, and False otherwise.
. P( b4 g% E5 V7 O5 o3 g - >>> math.isfinite(100)+ ~3 U& Y% q; g. h- u! ~0 H
- True
. d6 [" R& q0 ?2 L3 { - >>> math.isfinite(0)/ B! G" V: f) R0 ~1 ^! S* ^3 q( n
- True
. `; j* F' Q V - >>> math.isfinite(0.1)) d) T7 M3 e; o7 Q H( @- o0 s8 P
- True
7 x f" X* w+ i; W; Y( U+ D - >>> math.isfinite("a")
1 \. @ Q" C) e" q, k - >>> math.isfinite(0.0001)( p1 _! p4 G Q: A6 ^
- True
复制代码 : ?$ [- z2 b4 [0 ~1 j/ K
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
- M9 d$ o. z! x' T* w+ A# O' e- #如果x是正无穷大或负无穷大,则返回True,否则返回False* X( j- u, R' c% b8 v, r8 Y8 g
- isinf(x) -> bool
+ Q; f7 i, P( A0 J; t/ c: ?. X* w$ z - Return True if x is a positive or negative infinity, and False otherwise.0 V1 m A/ y" v- [/ T p8 S/ Z
- >>> math.isinf(234)0 N+ S7 R) m6 R9 b* ?6 m N
- False
) s, f2 j4 b1 y0 v' x - >>> math.isinf(0.1)
, f5 I7 _( ]/ r - False
复制代码 8 S6 y% G4 W: I( S9 o( N. q
math.isnan(x) 如果x不是数字True,否则返回False
9 S( A3 u# w; L: x: R+ A- #如果x不是数字True,否则返回False
* J( ~* O5 ]. Q1 D0 H) I - isnan(x) -> bool
R$ u9 p q/ b8 Y) c - Return True if x is a NaN (not a number), and False otherwise.7 o! m: p8 ]6 T
- >>> math.isnan(23) O5 g# ]0 h/ q* B7 y- H# x
- False0 q! t7 Z( F# C8 E
- >>> math.isnan(0.01)
1 B5 e/ W2 P- S. X - False
复制代码 0 u$ p6 t: w9 v8 U6 u7 l: q: H
math.ldexp(x,i) 返回x*(2**i)的值
! O4 v+ n4 [6 z5 K- #返回x*(2**i)的值
4 w$ Z0 e" Y: N" ` - ldexp(x, i)9 z+ F+ p( F3 J& [7 e
- Return x * (2**i).) G% g, e: x* F7 v0 B1 E
- >>> math.ldexp(5,5)4 G" ?# m: x+ g4 o- ]1 C
- 160.09 `( Y1 {& S1 e) b: @: d
- >>> math.ldexp(3,5)* y) [) Q5 h# \# U0 m+ a: F* N
- 96.0
复制代码 6 z5 U4 N4 S$ w- Y' P
math.log10(x) 返回x的以10为底的对数
! k" h! Z8 ~4 N- #返回x的以10为底的对数
! V( G K6 E \) @7 a: ? - log10(x)
1 _! A# `, ?5 e6 n5 \$ @% P - Return the base 10 logarithm of x.; K7 U/ i2 T9 t; s0 P
- >>> math.log10(10)
8 r) Z2 K' z# W: G9 O( A3 Z - 1.0
, ]5 u% v/ i) x6 I$ a" L - >>> math.log10(100)
T; _- ~. }& N- i. x5 P8 V# w - 2.0
4 K# d5 h8 G0 H7 m" r0 H9 d3 ]. F - #即10的1.3次方的结果为200 m, k6 ~' O, `/ X9 P
- >>> math.log10(20)2 c$ o' t, M( M9 W; v( r5 b
- 1.3010299956639813
复制代码 5 }( f, ` {* N
math.log1p(x) 返回x+1的自然对数(基数为e)的值
9 y' D7 n% K, y) l- #返回x+1的自然对数(基数为e)的值8 i( M) b0 _* R
- log1p(x)
! p _& i4 ?" i0 y) I - Return the natural logarithm of 1+x (base e).
) u4 ~8 i' Y3 s# c G, d; j) p - The result is computed in a way which is accurate for x near zero.( y! {6 ^! J _3 _* x; [8 ]
- >>> math.log(10)
' ?& k* x5 E" J* B& E - 2.3025850929940467 c5 P0 m |% X3 I/ Z, t% W. V
- >>> math.log1p(10) B0 a) h9 m4 o
- 2.3978952727983707* \) n! E1 |0 ^9 X$ N: }7 l6 c2 T
- >>> math.log(11)3 Q; ?* h. ^) N+ h/ t& J
- 2.3978952727983707
复制代码
. j& S0 u# V& g9 V7 b4 n& P/ B* [math.log2(x) 返回x的基2对数6 t0 g7 n! e9 k4 m) O- d" Y1 J* n. _
- #返回x的基2对数8 R1 Z9 i; O' C
- log2(x)9 M# q u/ B3 q* B
- Return the base 2 logarithm of x.
5 N" L% S0 J$ J6 j; C( ?9 { - >>> math.log2(32)6 Z- C9 I7 y* p( s) f( C# D
- 5.0
- C' N8 b1 P5 p! F; i, F - >>> math.log2(20)! N. O% U4 m; Z3 m
- 4.3219280948873639 R6 V5 j6 z& W8 e5 N8 S" W! M
- >>> math.log2(16)- G7 |$ S7 C, Z4 n+ \
- 4.0
复制代码
- D1 j; z% ]2 a* ^( pmath.modf(x) 返回由x的小数部分和整数部分组成的元组
' \6 p: i% }# k, Q2 w1 g- #返回由x的小数部分和整数部分组成的元组% m4 i. J* g" [+ K
- modf(x)
" G% d" S+ I1 S$ B6 U: X1 j - Return the fractional and integer parts of x. Both results carry the sign8 V8 y2 Q8 @/ n, n6 ?. Z
- of x and are floats.
5 T4 x5 p8 l" j& B - >>> math.modf(math.pi)1 a- J; ]( e! p7 K6 ?* F5 l
- (0.14159265358979312, 3.0)) Z7 q, G+ l! o" P3 D" Y, h
- >>> math.modf(12.34)
! e" u( q' o; V( q* h8 ` - (0.33999999999999986, 12.0)
复制代码
0 r% N- w) W- Ymath.sqrt(x) 求x的平方根
+ j6 `: w, x- y9 l6 E- #求x的平方根3 W' T2 Z# a8 q8 _
- sqrt(x)7 n4 U4 O, ?/ h/ O9 P
- Return the square root of x., {, Q8 K) j& f; V3 J
- >>> math.sqrt(100) D; g1 |# ?) T. ~% ?
- 10.0
1 e7 R' W+ c- t; B - >>> math.sqrt(16)
4 s$ F5 {9 {9 U; R6 k1 O) L - 4.0
# g& c/ Q$ O2 v' v' J - >>> math.sqrt(20)
6 u1 b# N8 [8 K- \, X - 4.47213595499958
复制代码
$ [) x# a9 X# ?7 ^ o ?math.trunc(x) 返回x的整数部分- #返回x的整数部分% a; F1 Q. S( ~+ n$ m/ k
- trunc(x:Real) -> Integral6 x9 |0 a6 @7 @) u C
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 D( K3 x. B: K" m7 |6 l4 x0 ] - >>> math.trunc(6.789)
, A! {6 ]$ b" k3 N3 ~$ I - 60 i6 J: l. Y" F1 [2 h7 j' f2 t
- >>> math.trunc(math.pi)7 O' P/ u! i9 G. E8 B# j
- 3 S% c; h/ v9 {2 y% V/ H9 z
- >>> math.trunc(2.567)
, b, }/ }3 B5 T! R7 B5 T; C, Y% { - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|