马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ b" N! j7 g, i3 R& S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, Q$ `5 ]9 m ~4 T
# M4 K( F. ]. t$ U- q6 T/ T5 d, Y% \方法1:
7 H; O) h( N; M- >>> import math
0 K1 R+ Q/ |" C9 N( U1 e - >>> math.sqrt(9)# t) o0 V A- m2 ~0 \ a7 m
- 3.0
复制代码 方法2:% }" D- l; s4 E
- >>> from math import sqrt, T0 e- S* f. U) r5 j8 K
- >>> sqrt(9)
2 ?& s$ A$ m P7 R7 ]0 ` - 3.0
复制代码 1 E3 A9 k5 v& U2 D
, Q. e( l( u6 T' a, \& S
math.e 表示一个常量 ?4 V n ~0 C0 D- Z0 n
- #表示一个常量9 T, C* e; ~: ?( }2 w
- >>> math.e
7 k4 f0 c5 z) e9 B% f: n/ x8 J - 2.718281828459045
复制代码
6 b) @/ {$ b* w& l2 qmath.pi 数字常量,圆周率- X0 {' V6 V. s, N# M3 h; r
- #数字常量,圆周率
5 V; ?" m, l$ @/ t1 Y$ E: N - >>> print(math.pi)
" s1 F) a3 b# _5 }2 ?* j: [ - 3.141592653589793
复制代码 - ~9 U. V% m4 I1 |4 C0 ]- \9 ]
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 y9 O$ c- ]8 d! V$ d6 s
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x% U# x9 K: N$ m5 b7 x( O/ I, c. Q
- ceil(x)3 X1 _' y0 [7 _& g' }7 R/ Q5 V7 @5 X
- Return the ceiling of x as an int.
5 H8 M% ^! e" K8 Z - This is the smallest integral value >= x.& h: n( P- i1 c4 a4 R3 j$ h
4 Z7 M. y9 i2 N) C7 R- >>> math.ceil(4.01)7 j7 j9 l$ x) \$ G$ q( Y# J% Q
- 5 C. S9 Z2 H( `/ E% T7 e6 |
- >>> math.ceil(4.99)* Y+ B, `3 k0 p$ i1 ]. v
- 5
/ C* ]9 `0 @4 }- v) x+ I - >>> math.ceil(-3.99)+ D& ?1 d0 }6 z) W- G; s
- -3% I2 \2 A6 ^) D, e: w3 E$ `5 p
- >>> math.ceil(-3.01)
) k. [& J$ n! K( k - -3
复制代码
( r- b3 w( V9 Q& b Hmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: d) j8 E$ F4 g' B( R& c2 P- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) l. _) r# q/ s
- floor(x)
, ?; c! I$ [4 S# a3 _- n# S - Return the floor of x as an int." e3 p* ?: G- w* h" }1 W6 w0 }
- This is the largest integral value <= x.& K: r' {# I$ K W- J
- >>> math.floor(4.1)- n( U' x% [2 m# h, v1 K6 j; R# v
- 4
" Y! t6 C% P( R$ r) X8 h" R - >>> math.floor(4.999)
0 G1 E0 {$ [; G& l: N/ ^2 M - 4
) t$ L- G) x. W7 l8 L! D - >>> math.floor(-4.999)
% K8 a* T% d }0 [6 c9 A - -5
6 N. K8 \. o" h( w) a$ |/ ^0 s - >>> math.floor(-4.01)
) k( y- B( C+ ]6 e - -5
复制代码
5 o2 d$ x4 U! d0 h; g/ _math.pow(x,y) 返回x的y次方,即x**y
: H9 U& \# R5 a, h- #返回x的y次方,即x**y. u) y9 x8 d6 Q3 y# ]
- pow(x, y)
# ]) l* y- i1 m& R - Return x**y (x to the power of y).
" `" Q% X3 R* Z9 K - >>> math.pow(3,4)
1 g- S% x" e6 g9 s - 81.0
( C! J7 v2 m) U5 l" Y ~& r. m - >>> % p$ p x/ ?+ [. o
- >>> math.pow(2,7)
X: |' @- B. c. R- |5 t - 128.0
复制代码 2 e# p- Y5 c l
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" N9 r7 A% d( @8 j' J
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# e! s& W5 w% I7 P) L8 x7 V
- log(x[, base])
% y$ Q a. O" V3 E/ o7 L% X' f8 Q - Return the logarithm of x to the given base.
$ }$ v% U. L5 H- v - If the base not specified, returns the natural logarithm (base e) of x.: n. c5 q, y5 u4 O
- >>> math.log(10)$ M/ _9 h& c) s7 F) Z0 X( J Z- O
- 2.3025850929940465 |) O' o+ p' h$ d/ g5 y
- >>> math.log(11)
' r; b& o. L4 s' j - 2.3978952727983707
3 m9 T. t8 m% Q - >>> math.log(20)
* Q! b9 C8 z* [( F: l - 2.995732273553991
复制代码
/ I F$ M9 V: f! x/ h* dmath.sin(x) 求x(x为弧度)的正弦值8 `- K5 O* ^) U: s" X7 r5 E8 s, [
- #求x(x为弧度)的正弦值3 @9 q* h5 g3 j; L
- sin(x)5 R& w1 ?8 H. H; G; z, F* l$ ^/ t* w
- Return the sine of x (measured in radians).
$ ^4 D" Z8 e! B( R) o* ? - >>> math.sin(math.pi/4)
( o( x% l1 R8 Y6 B - 0.7071067811865475- f1 w2 W4 X$ X0 ]6 o% C7 H
- >>> math.sin(math.pi/2)
3 M, V& O( h+ M5 f: k3 I - 1.0# z. L5 q2 C+ b; b
- >>> math.sin(math.pi/3)
7 M( b z" d' d4 y: N2 o. C - 0.8660254037844386
复制代码
; E9 b: g8 F; z' Hmath.cos(x) 求x的余弦,x必须是弧度8 i0 E& M1 ]5 N$ S: y6 u# x) T9 y8 f
- #求x的余弦,x必须是弧度
/ d1 e# ?3 r' v n, r - cos(x)
0 D# P2 {9 [7 |: y0 b - Return the cosine of x (measured in radians).
* z3 H. j. K$ l# E4 Y& s& V - #math.pi/4表示弧度,转换成角度为45度
( z0 g. U, \- b9 D" F9 ` - >>> math.cos(math.pi/4)
$ O4 x. V Q/ l c& u4 s - 0.70710678118654760 a) y1 [. h7 i. d* j
- math.pi/3表示弧度,转换成角度为60度
/ I7 V4 ^; m6 a( t2 Z" s - >>> math.cos(math.pi/3)
$ k. U; P' i9 [+ e7 [! p - 0.5000000000000001! h9 m( j/ K3 _; C
- math.pi/6表示弧度,转换成角度为30度
* n6 {, Q T; T: a* j- u - >>> math.cos(math.pi/6)
- Y( }$ u- k! Y - 0.8660254037844387
复制代码
. X) C1 Q7 s% r6 `math.tan(x) 返回x(x为弧度)的正切值
0 C/ }% Y2 w* G. S0 J- #返回x(x为弧度)的正切值9 }# h# }* T) f5 c. E
- tan(x)8 |: R( B% n- L
- Return the tangent of x (measured in radians).
4 z- {8 [/ o- _ - >>> math.tan(math.pi/4)
1 g/ h( b. _! D$ B, Q9 g8 `5 q* J* E - 0.9999999999999999- q! f+ s3 Y+ M, { U- ~
- >>> math.tan(math.pi/6)
$ I$ `( n9 u' U) J* T2 h; X - 0.5773502691896257
4 k% b$ c. R: W$ r - >>> math.tan(math.pi/3)8 i& `- L, o; U: }) }+ d+ A
- 1.7320508075688767
复制代码
4 y, f4 h5 M) w9 ]6 h& Pmath.degrees(x) 把x从弧度转换成角度
. o, C2 R) u5 D; ]& Z# G ]- #把x从弧度转换成角度
% O; {% _% J7 q2 a+ h5 a1 y( i: k3 h - degrees(x)& h. o2 @5 H/ s6 E' p4 R
- Convert angle x from radians to degrees.
- P+ f+ F9 m3 G( w6 @3 d5 y - , N& z9 O6 i" G! Z
- >>> math.degrees(math.pi/4)
& `6 S$ ^* W. G4 h - 45.0
) h; m: C8 _0 S* ?+ W. E - >>> math.degrees(math.pi)' x, u+ E: G0 K$ b8 x3 ~5 `/ O
- 180.0
6 |3 ~- J2 G% v4 \ - >>> math.degrees(math.pi/6)
# u' s2 l5 l1 N2 G4 { - 29.999999999999996! x. {* `9 `& T1 a
- >>> math.degrees(math.pi/3)( U( `2 B8 o3 P2 b
- 59.99999999999999
复制代码
- o4 J# }2 B T3 Y* i0 q* H7 K# Ymath.radians(x) 把角度x转换成弧度, `- s, X1 t3 z$ z5 R
- #把角度x转换成弧度
2 @& l! C1 x: ] E# V - radians(x)
, a2 Y+ d) ]' B" a+ Z - Convert angle x from degrees to radians.' v- a) K# u" X; ]
- >>> math.radians(45): X B* K( l% a5 q1 J8 R+ J& U/ C5 h0 e
- 0.7853981633974483
+ c5 i+ V" I" ?7 ^' r ` - >>> math.radians(60)
/ Q6 o7 j. t" D3 ] - 1.0471975511965976
复制代码
" f" g4 |: U/ b% R: Q' u0 Mmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
9 g: |! H9 g, P, a- #把y的正负号加到x前面,可以使用0
' _ a, k7 c4 L4 K4 s8 t& B - copysign(x, y). e, k% |5 u, C1 o
- Return a float with the magnitude (absolute value) of x but the sign
0 y2 ]$ t% y% f, l - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
8 |% u J1 \& \3 e; k - returns -1.0.
' r" o! ^' o2 u: t1 m: o7 F1 T7 ?
! g# a* h* T: {" [1 O1 S' C# ]- >>> math.copysign(2,3)# Q6 t9 Z; Y+ \
- 2.0- E" I8 ^7 `3 q5 S9 x7 d
- >>> math.copysign(2,-3)) r1 P: J7 k% C+ j4 o* b
- -2.00 Q' w) Y# H4 r1 |6 y
- >>> math.copysign(3,8)' J. I( P, S' W* f, Q/ Z0 \
- 3.0$ I$ F, t8 A8 n% x$ a) d2 A
- >>> math.copysign(3,-8), }5 `1 X- i) _
- -3.0
复制代码
7 {' O% D' K9 U& A2 g( U6 pmath.exp(x) 返回math.e,也就是2.71828的x次方9 M5 D/ @5 ~9 e, Q1 M1 F+ k2 l
- #返回math.e,也就是2.71828的x次方0 A8 c" B2 Q+ V! T# U
- exp(x)
3 i0 K; t# w5 |6 l - Return e raised to the power of x.; X9 z3 [. M0 ~- i- }
! O* F& W! N9 w9 a- >>> math.exp(1); v2 @9 c; y8 z
- 2.718281828459045
0 E) E5 x% {- L5 d$ X3 ~2 l! ? - >>> math.exp(2)
4 U" z& k3 ~( v - 7.38905609893065
3 [0 j/ E6 |% x( a4 E - >>> math.exp(3)
1 _9 ^' E& k1 Y - 20.085536923187668
复制代码 & Z# L$ ]8 j% ?7 b- Z$ [2 _
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
' |+ F1 x2 P* v8 r7 Y2 j Y" O& \- #返回math.e的x(其值为2.71828)次方的值减1% A* ^5 l* X. c5 I/ M
- expm1(x)
8 N# n( U) ?/ a: X5 k7 z/ I - Return exp(x)-1.; y: o8 V; L, j6 v Z0 R% q
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ b+ P. b5 l0 ^+ F
, d. F( N5 o' S0 G$ o$ `- >>> math.expm1(1)
, Q& ?" b( w* i" @* u _/ [8 E; c6 l2 Z/ Z - 1.718281828459045- ]+ I" V2 j. N
- >>> math.expm1(2)+ ~; C/ Y- m; B( k( ? G
- 6.38905609893065
8 ~9 N: c+ T9 O* ~' v! { - >>> math.expm1(3)
% D/ K0 I" e) M, |( ?6 G! r - 19.085536923187668
复制代码 ' m* D3 A6 Z4 U$ s. W8 k
math.fabs(x) 返回x的绝对值1 M% d6 o; Z+ G! @/ O7 o! F$ l8 p8 t
- #返回x的绝对值% I2 b( o: e0 J) k- ]- _/ u
- fabs(x)! _3 g0 I& q* | j- ?3 n5 z
- Return the absolute value of the float x.! j- G) u% x. a2 `/ V+ z
P6 }5 p ]" }9 M; Z/ u- >>> math.fabs(-0.003)
3 [" c* x* y8 C2 l9 P - 0.003
9 I+ U. s: M2 T: C - >>> math.fabs(-110)
. g: _5 x0 e! X9 N' W& n# m3 b" B+ u - 110.0
7 S/ R9 v5 i# F6 f& r: R0 W$ G - >>> math.fabs(100)
F; E* B5 |* w7 i - 100.0
复制代码 5 A7 I: p; i9 D5 M# \
math.factorial(x) 取x的阶乘的值
) r G) H4 y+ \& t5 w- #取x的阶乘的值- Z {. D7 N. S1 j% M0 V& {2 B# A' [
- factorial(x) -> Integral( g t! [) `2 E, [" l9 p8 c
- Find x!. Raise a ValueError if x is negative or non-integral.$ w5 m" ?( y) Y o, X$ i
- >>> math.factorial(1)
# _$ I, k& y# b! {' w - 11 H6 v% X- u* X0 q8 i
- >>> math.factorial(2)$ R2 u+ y; \% i2 f. w
- 26 j _6 `" _2 G3 T+ q4 h
- >>> math.factorial(3)
6 \7 r# }$ n1 H2 r - 6: @9 w Z0 ~) ?4 P
- >>> math.factorial(5)
; X8 `5 Y" }7 A! P - 120& D/ |* n R; t" ^( h0 `: `& `
- >>> math.factorial(10)
% ~8 o7 u6 f+ I) h) G+ B w - 3628800
复制代码 4 o6 q9 O/ \+ y! D
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
% o; [7 b6 N P2 F/ W9 S: @- #得到x/y的余数,其值是一个浮点数
- {7 W0 q4 }: x( l" f) D9 m4 o - fmod(x, y)- m$ _2 _. O* F6 a
- Return fmod(x, y), according to platform C. x % y may differ.
. j' M& r; M4 S2 _7 |& z - >>> math.fmod(20,3)
7 p# b2 w( z, I' i5 b$ r. l4 R - 2.03 m1 B, P; F" l1 T' m4 y0 ]
- >>> math.fmod(20,7)
6 i% c9 W9 K0 n& @ - 6.0
复制代码 / t s$ T2 a7 X( {$ x
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
( N( L: F- t% X4 f8 `6 i' w- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% |0 N+ _7 C# o( v5 j
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
: Z" Q) d% l; A5 ` - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 c* [& t# K4 m: i! J
- frexp(x)5 v V: v( Q# v- _
- Return the mantissa and exponent of x, as pair (m, e).
" l- O$ a* l0 E8 e - m is a float and e is an int, such that x = m * 2.**e.0 Q4 O! c- g! _5 ?) b% W2 L1 r9 J
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
% j% L( n2 A0 L. w0 h - >>> math.frexp(10)' u5 j4 d. `+ z h% Z% F
- (0.625, 4)
- `0 i& I) u& M: o: L0 B) L - >>> math.frexp(75)
a' }: F" p' G! U* } - (0.5859375, 7)" M. m. e+ \1 J# Z: u; b9 U8 N3 v
- >>> math.frexp(-40)
, v& M) z: \9 ]! Z% Z! C' e4 G1 S - (-0.625, 6)
% |/ L: [8 W- p& C% c& E5 h - >>> math.frexp(-100): m$ W0 | y7 c) N
- (-0.78125, 7)* g' I6 _+ H3 S# l$ ^4 Q! |( q9 R7 A
- >>> math.frexp(100)6 E' r) C! g) A5 B! i
- (0.78125, 7)
复制代码 : a% b' f; @- D1 `
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)( I) x( p- b- \* M
- #对迭代器里的每个元素进行求和操作6 ~" q1 Y6 e& U( x4 O" h
- fsum(iterable)
- i/ _) l8 ~; _( \ - Return an accurate floating point sum of values in the iterable.
& k- ~2 H7 Y X: N - Assumes IEEE-754 floating point arithmetic.
6 O( D( P; i1 G: z: P5 o - >>> math.fsum([1,2,3,4])
6 s: g b8 b2 E - 10.0
, R( ?/ C9 f6 q7 x: X - >>> math.fsum((1,2,3,4))
3 Y) Q1 z- ?& P l - 10.03 _! Y! Q; A$ @# @
- >>> math.fsum((-1,-2,-3,-4))
! t$ W5 s. S. F - -10.0
, W7 Z! G: J( G+ c - >>> math.fsum([-1,-2,-3,-4])
/ s0 }8 |' C1 d( Z6 k - -10.0
复制代码
( _# Y+ {+ G* P% a2 x; U3 Ymath.gcd(x,y) 返回x和y的最大公约数( X# b) k2 w$ j" j9 ^
- #返回x和y的最大公约数# m, B1 A6 d" s. s2 p; E ~
- gcd(x, y) -> int; ~& g" g" I" [. D
- greatest common divisor of x and y
* k7 G, U" p8 z3 F" O; ~$ u - >>> math.gcd(8,6)
; d1 s* W: ~$ F8 b% p6 f0 J# b - 2( v1 |9 @; G* m7 C$ I) ^. Z
- >>> math.gcd(40,20)
1 v0 b c6 v( `9 W - 20! U; C* ]8 B; y$ z
- >>> math.gcd(8,12)7 {) C; J& a7 {( h
- 4
复制代码 ( |; `4 I X+ x! M) t: S5 E, l, _
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
5 d) k% f8 m0 w( P, M- #得到(x**2+y**2),平方的值
# f" W, p9 R5 i: B - hypot(x, y)
- e; t5 K# J9 _2 y$ O! H - Return the Euclidean distance, sqrt(x*x + y*y).- C: p, f" I6 J: y {" y4 s+ I
- >>> math.hypot(3,4)
+ P; x8 j. z- S! e/ ?! Z - 5.0
8 ?& p9 d& d `# `; ~4 [0 v3 M/ K - >>> math.hypot(6,8)3 R/ g" |3 p4 E: e2 a% M9 W& H
- 10.0
复制代码 9 u8 N0 M" C8 }( _7 i
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
1 s. P A0 |" ~8 W' P" l- #如果x是不是无穷大的数字,则返回True,否则返回False
|/ E# X) f; I% Q, H7 h/ R) p8 u - isfinite(x) -> bool
3 U# ] q h f+ S' c - Return True if x is neither an infinity nor a NaN, and False otherwise.
9 f5 P4 f9 O1 M( U! M - >>> math.isfinite(100)
; F3 i8 ~" a: f8 m2 w! X4 O6 J - True
' x% u( l4 q9 [" Z4 o6 s - >>> math.isfinite(0)# z4 X! _4 }# G" j$ e3 L
- True
4 U: [5 f* c; F a" I/ v - >>> math.isfinite(0.1)* J+ G3 c( _8 O, H* J
- True% ^$ }1 c6 q/ ^ m
- >>> math.isfinite("a")
O4 t& a' [$ z- _: G# Z - >>> math.isfinite(0.0001)7 i% h; j. h! m% n
- True
复制代码 0 S- i* r' Y* H! D. I
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False8 s4 b6 a2 G/ R3 F# S/ F8 h9 [+ B
- #如果x是正无穷大或负无穷大,则返回True,否则返回False- @( R" H( W6 g3 ~: y S
- isinf(x) -> bool+ D! G% s& b c" Q5 @) |
- Return True if x is a positive or negative infinity, and False otherwise.0 T0 b! _# P2 b$ x, `8 i
- >>> math.isinf(234)
' \( F% y9 `4 Q' I( Q1 M' U$ W3 Z; E - False
: ~5 C: a6 q- o6 d+ S" } - >>> math.isinf(0.1)* ?( j" M' ~4 d% ]! T+ ~
- False
复制代码 ( q' T# ~6 H7 x
math.isnan(x) 如果x不是数字True,否则返回False, U: G! ~ `$ C' z8 l( P
- #如果x不是数字True,否则返回False
- k9 p7 o( G$ [6 k - isnan(x) -> bool7 e# f3 j1 B) e2 d2 B
- Return True if x is a NaN (not a number), and False otherwise./ \- F7 H; B5 I+ O0 m9 a' _
- >>> math.isnan(23)1 u8 Z+ A! C/ K( c/ S8 t
- False" B% | D4 v5 x9 q8 g
- >>> math.isnan(0.01)
) `6 T1 H; h) \3 {$ ^8 X - False
复制代码 * o, R( ?6 L) Y( v$ k4 v/ F
math.ldexp(x,i) 返回x*(2**i)的值) m! j0 n5 R* d6 s+ w
- #返回x*(2**i)的值
/ \& t- j. A; S) Y1 o% J3 a3 Y \ - ldexp(x, i)* J) X+ _; y( N( ~+ Q. }) _8 p
- Return x * (2**i).
1 a( u6 a5 g2 |. e& g2 J$ [ - >>> math.ldexp(5,5)
! x1 K" T- x9 f& `! p9 R k" \ - 160.0% h. e8 u: Z/ x5 S) x& Q2 x
- >>> math.ldexp(3,5)
( _* C; o0 j2 H% J( S) t) z - 96.0
复制代码 9 o! U5 y6 t e: z, f! L8 T+ I$ o
math.log10(x) 返回x的以10为底的对数
( q; H: Z2 M2 A3 a- n- #返回x的以10为底的对数
7 d' Z* I: w+ h9 a4 d6 M: _& H. [ - log10(x)
$ ?9 ]. F1 F, d; q - Return the base 10 logarithm of x.
/ |- Y ~- \ O - >>> math.log10(10)6 ?* W( X0 \- G x3 c$ b7 b
- 1.08 D1 m9 u: L" i+ \
- >>> math.log10(100)
' d0 U; R* c6 ~4 @1 R* Q - 2.0% W6 Y& ]3 ^& v# y
- #即10的1.3次方的结果为20
% O( V8 Y: C4 g6 L0 A - >>> math.log10(20)
1 Q: m$ M) I2 r1 l. m3 M - 1.3010299956639813
复制代码
" l) v" `' H6 C& W0 d8 W3 t1 W0 w; G6 rmath.log1p(x) 返回x+1的自然对数(基数为e)的值7 N& T ~+ A! h: o* H
- #返回x+1的自然对数(基数为e)的值
, m& {) y: Y$ n$ b: e! ? - log1p(x)
" R( I) ?8 S. Z! R, [3 b - Return the natural logarithm of 1+x (base e).. d3 q$ \+ o2 k( D$ l+ F; c6 l$ @
- The result is computed in a way which is accurate for x near zero.& E" U5 K! j: i6 L, h+ @* g1 G
- >>> math.log(10)6 \6 U; X% m4 Q! t2 I
- 2.3025850929940461 V+ [7 s9 w8 E! c {. i; _
- >>> math.log1p(10)1 ?+ r+ {3 q. J6 d0 C7 \
- 2.3978952727983707; m: \, a, x- d+ a* j
- >>> math.log(11)
: b; u, x; s8 E6 D1 s/ S - 2.3978952727983707
复制代码 ) I% t) g& C% t2 p1 R7 r+ k
math.log2(x) 返回x的基2对数
_9 v! \: H! Y; [- #返回x的基2对数4 ]0 i5 }5 Z& W* c# V/ M
- log2(x): s$ @- E5 g0 o) v7 `6 |6 u' {' Y9 n4 V
- Return the base 2 logarithm of x.
( ?; Y/ g5 s a% d B% M' p - >>> math.log2(32)* A$ T$ W! I$ @& K0 Z5 t8 g+ z
- 5.0, S6 w5 e8 X0 U4 U6 Q2 S+ H
- >>> math.log2(20); f1 Z$ @; `7 U& \. r' D' b1 Q
- 4.3219280948873639 L; O& n9 w! z/ ~/ C7 n+ y. q
- >>> math.log2(16)! t( K3 u, Y* E5 y5 }
- 4.0
复制代码
2 {% h; d- d, ]' l" i6 \9 rmath.modf(x) 返回由x的小数部分和整数部分组成的元组
; D. l( @. }# I7 D l- #返回由x的小数部分和整数部分组成的元组
6 \; {8 o9 U6 i6 R: M - modf(x)5 G$ k* I. y$ s/ R$ @5 n
- Return the fractional and integer parts of x. Both results carry the sign
% T0 V, S7 D4 ~8 c# T7 Z - of x and are floats.3 y8 [ T$ M- p/ m- i5 p' q" n2 c
- >>> math.modf(math.pi)
% D' }2 S# J* g4 I) S/ L - (0.14159265358979312, 3.0)
1 n$ e! b0 m6 _ - >>> math.modf(12.34)! o- y+ l1 }$ t) Z
- (0.33999999999999986, 12.0)
复制代码
$ t! w8 _- C1 h4 M6 K3 Wmath.sqrt(x) 求x的平方根
* }1 U# j/ \/ K- #求x的平方根
6 @- Z# h% o6 G. o - sqrt(x)9 h0 O" f+ g7 \% }' l z4 @
- Return the square root of x.6 ` u5 d/ @( D- E0 p+ @& A
- >>> math.sqrt(100)1 V$ i3 U6 d; l# z2 ?
- 10.0
/ l! `8 @4 M n1 p - >>> math.sqrt(16)
& D, E3 l& g7 g; m/ s6 {4 o, t - 4.0: L' i8 ?/ r+ V9 I
- >>> math.sqrt(20)
* d) G& G# x& k. q) q, p - 4.47213595499958
复制代码
/ H9 J3 [( o: @" r. vmath.trunc(x) 返回x的整数部分- #返回x的整数部分* K$ K$ B3 r: ~" H
- trunc(x:Real) -> Integral
* R/ C- h1 o' G9 g& L( {) V - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
' {+ a" B/ u' W4 t$ c6 ^: c; ~' ] - >>> math.trunc(6.789)
; ~2 Y P- A. O/ x( z - 69 @: ?* R# w. t8 {* |8 e2 N" Y
- >>> math.trunc(math.pi)
( O. U- w6 P6 T - 3* F1 C# y5 U3 h) S2 ^
- >>> math.trunc(2.567)
3 V0 S8 s+ n% h) R2 C - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|