马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* W" n- T. S# b6 t5 R: V9 x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% A6 Q* T/ k: o) z0 d$ ~6 [7 z! y
* G6 @+ d! |/ m/ ?6 K方法1:" ]; V1 v+ T3 L O/ H* T! n
- >>> import math
) ?1 y, W |8 }7 ^. A* q$ H - >>> math.sqrt(9)6 T1 H+ b" y* C2 W; \" a0 w
- 3.0
复制代码 方法2:
; X9 x# ^0 N9 N. J) l G7 Q U- >>> from math import sqrt
8 ~, ?- l: k% }9 V8 I& F - >>> sqrt(9)! i! a% s8 {* \9 Q$ ?: Q9 W3 \$ J
- 3.0
复制代码
9 z. D" ^" g8 j' S- d: D0 U1 X# c# N . f4 [2 Z w* n
math.e 表示一个常量- v7 I) C' S0 S* n' a0 w; J" T
- #表示一个常量2 h4 _7 ^7 G/ \4 I" b& w* K# ?' z
- >>> math.e7 I |2 P+ O% A9 X, \0 X
- 2.718281828459045
复制代码
. Z8 [0 \/ V" Y; \! k8 Gmath.pi 数字常量,圆周率
# J" p, h, `1 }( x6 }; @$ a- #数字常量,圆周率
$ X1 D* y$ L ^2 _. s - >>> print(math.pi)
* l3 }1 B: W' t8 {8 L0 G; q - 3.141592653589793
复制代码 : P9 i& H8 P+ Y% S1 ]
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ ~3 F( o* c0 L2 Y2 l- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
: ^- m7 R) Q" \) H( [+ t - ceil(x)
5 f( p8 s) i6 A - Return the ceiling of x as an int. p! Z8 D* L/ m* E# k) [! R+ w* u
- This is the smallest integral value >= x.
- M% ]% h! y/ [
6 _ P# @# N# O/ d- >>> math.ceil(4.01)$ l& ]. G; Z" q- ^! ]
- 5
9 ]# V K0 |/ o' _- ]) ~ - >>> math.ceil(4.99)
3 R$ Q) q F2 p9 O - 5/ D+ E7 b0 z) `7 x7 u' C+ Z
- >>> math.ceil(-3.99), i- C* [6 j& T
- -3
, I% v7 |9 d% H7 S1 L9 W) Z - >>> math.ceil(-3.01)
4 V( @( r/ Q: \1 V6 Z V - -3
复制代码
3 G% U" \! f1 C) S E+ C# a# Omath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身% l8 c# g8 D9 k4 g
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& Z9 `; v' s% R) _8 h g - floor(x)2 G8 B. o8 M, b( x
- Return the floor of x as an int.# n" j( e+ m7 n3 M' B
- This is the largest integral value <= x.
+ S/ h h1 l) |+ @! _ - >>> math.floor(4.1)7 \9 y! q9 l6 L1 s/ b6 G6 @6 b
- 4* d( u" u: p' h$ o
- >>> math.floor(4.999)
. h( ~3 q5 E4 A0 P4 K1 X - 4
1 }$ w$ l7 r: e' p7 B - >>> math.floor(-4.999)& V+ x! T) A/ b7 {
- -5/ a) M2 t3 A1 A5 K1 u: _, U
- >>> math.floor(-4.01)9 m( ~: `- i |* |
- -5
复制代码
, C/ w2 O6 r- k" x: f# O4 _) wmath.pow(x,y) 返回x的y次方,即x**y
% Q& X! ^$ Q( R* U- #返回x的y次方,即x**y
6 ^1 Z! i! B+ m# V - pow(x, y)$ K$ X3 f& l t* n" n
- Return x**y (x to the power of y).
/ U3 g( L, d4 }) M - >>> math.pow(3,4)6 P8 [1 o3 y& [! ^ N! N
- 81.0( G' S6 r1 W. o; z# F' M2 ?0 L
- >>> ; k; v& H B: o: G- Z, ?
- >>> math.pow(2,7)! ^: [1 E7 b; A! R7 ~) D
- 128.0
复制代码 & Q; T, _0 I. T# u
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 E! I! r; f8 i0 v
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ d2 a7 r3 F* k' R, |
- log(x[, base])
6 G7 g; a' n! u' K2 E1 q - Return the logarithm of x to the given base.
1 [. f g7 W! t" E" P+ I - If the base not specified, returns the natural logarithm (base e) of x.
, l1 t E6 g* k; x - >>> math.log(10)
* h+ G- T4 ^; k% C* e- ~: M - 2.302585092994046
c% A9 \: I) k! G3 \ - >>> math.log(11)
. g' [0 o& n+ [% A$ [& m - 2.3978952727983707
% E( q3 y1 j9 c" Q7 H9 m3 J - >>> math.log(20)
- Y$ S* }0 G( k& N% ]7 M$ W8 o - 2.995732273553991
复制代码
3 R2 H$ p4 Q7 Nmath.sin(x) 求x(x为弧度)的正弦值, ~5 T# g3 G$ I; p
- #求x(x为弧度)的正弦值! P, k' {# _4 x2 |$ L
- sin(x)
% [" t! k1 o; j2 n B$ ~1 C2 z) N - Return the sine of x (measured in radians).
K9 b! A' Q$ p, ]. Q3 v - >>> math.sin(math.pi/4)4 k# C# z0 M5 w! x F
- 0.70710678118654754 \) `4 R% ]5 _+ F$ Y- M! g1 {, S
- >>> math.sin(math.pi/2), H- w1 ~- D c6 D$ Z" g R8 m" r
- 1.0$ \" Q+ h0 o: Z& ?9 N2 G2 n7 b
- >>> math.sin(math.pi/3)
& v0 P% O4 Q. D' U- _* g9 | - 0.8660254037844386
复制代码 3 W2 k/ {% |9 t6 G0 J. V
math.cos(x) 求x的余弦,x必须是弧度
4 V7 } N2 I u! i- f& w( D- #求x的余弦,x必须是弧度
# C& b# b7 q: n) y6 {% ? V - cos(x)
5 R6 M2 _1 U6 ? - Return the cosine of x (measured in radians).0 ^' R' Q% A0 U# |9 N
- #math.pi/4表示弧度,转换成角度为45度
5 P5 Z" {' K* u) `: L - >>> math.cos(math.pi/4)& }2 ^% V6 T. v5 g
- 0.7071067811865476
( r- C5 o& L; m( D - math.pi/3表示弧度,转换成角度为60度2 Q( ^6 ?& B) E- s8 W
- >>> math.cos(math.pi/3)1 {9 {* e( C r9 `7 c6 S* h. E
- 0.5000000000000001) v" v$ X; a( {2 a% Y
- math.pi/6表示弧度,转换成角度为30度3 _% R6 U5 C' X5 ^# Y
- >>> math.cos(math.pi/6)% ^0 d0 i! t6 A/ Y+ r% z# }. c
- 0.8660254037844387
复制代码 7 p; T, J1 V: g' n
math.tan(x) 返回x(x为弧度)的正切值
6 L$ U% O1 Z- x# c& R- #返回x(x为弧度)的正切值
, i6 B: a1 F% ?* e H& Q2 s" q; @ - tan(x)
6 W* i& D( f7 D* n' M - Return the tangent of x (measured in radians).4 c7 f6 v3 c+ F% O. b, Y
- >>> math.tan(math.pi/4)% N/ q- D% H% ]- t
- 0.9999999999999999
9 i# [2 l. w5 p3 T5 X& I - >>> math.tan(math.pi/6)) ~* M! B5 S* V; X8 u; i
- 0.5773502691896257
# R$ t3 _; \, k - >>> math.tan(math.pi/3)+ ?6 J, o( G. G* e8 B1 c' D
- 1.7320508075688767
复制代码 / }' a; V/ Q" P. N4 Q1 D
math.degrees(x) 把x从弧度转换成角度$ F7 E4 S4 C4 }+ Q7 a
- #把x从弧度转换成角度
6 [+ t( K: h: i& a7 [. \ - degrees(x)
* H) ~$ h2 g- }; r - Convert angle x from radians to degrees.
8 d% I/ E: s9 o* F3 `% U - 7 k0 p0 ]9 [ i2 [
- >>> math.degrees(math.pi/4)$ T& W( ~# e& h; h3 v }7 H: l
- 45.0" d1 j a1 L& ~, q' T8 [
- >>> math.degrees(math.pi)7 L; M* r4 I4 e- D6 }- K
- 180.0
2 q5 L+ A9 q5 | c/ r: |" H( Y U - >>> math.degrees(math.pi/6)& G" f8 p7 s- d/ {: w5 v6 j- k6 ~# P
- 29.9999999999999961 T) X( L" e T: q
- >>> math.degrees(math.pi/3)
0 m' I! z: q$ [) k0 h# }" J9 H% f - 59.99999999999999
复制代码 3 T& {3 s$ P* t/ M* e: z4 }. D
math.radians(x) 把角度x转换成弧度. e3 |! R u1 W8 [ ]# u5 G7 n
- #把角度x转换成弧度& A' b" n+ W; {3 t6 E' U% K
- radians(x)/ r/ i5 ]. Y! n) ^6 ?% G W m6 }* T
- Convert angle x from degrees to radians.
) [+ y; F( n: D* T; Q1 O - >>> math.radians(45)9 q& ^3 l/ O+ G0 @, A3 q0 }
- 0.7853981633974483
' h6 D! E/ P, E( Z# | - >>> math.radians(60)
8 Q8 w( T0 A5 @$ c, v* J. k - 1.0471975511965976
复制代码 6 w- M4 x3 f, x* F1 G" F9 m6 U
math.copysign(x,y) 把y的正负号加到x前面,可以使用0/ N8 k L6 r/ B* P# M2 F$ W: a/ t* E
- #把y的正负号加到x前面,可以使用0+ ~2 b2 { A8 v% y: a K& ]
- copysign(x, y)
: m0 X; y/ _, ~# E- H - Return a float with the magnitude (absolute value) of x but the sign 4 c7 J- _' A' E/ z
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & p+ n0 x$ Y2 R
- returns -1.0.- ]! e/ W( l% I- b
- 1 X9 S- s' L8 X9 X8 G0 w
- >>> math.copysign(2,3)
) ?$ l, w6 J- o! T" W7 e - 2.02 @1 V7 W' u3 E: {1 Q! U0 a
- >>> math.copysign(2,-3), K) R* Z. E% u' s
- -2.0
, P" v: G& y- ^' q - >>> math.copysign(3,8)5 a9 \% x5 G' {# Z. ^, q7 B
- 3.02 }: Q: t; j2 D5 }# p! j' b0 N; I
- >>> math.copysign(3,-8)
+ ]& ]6 L5 y1 M; H# a! R& `" b& D - -3.0
复制代码 4 T/ [ s* Q. d1 o: Z5 t9 l* o
math.exp(x) 返回math.e,也就是2.71828的x次方
* A6 o# d4 N1 i; F$ ^( S N- #返回math.e,也就是2.71828的x次方
2 U; `2 ]9 f0 f, u$ |) p - exp(x)' X6 G0 F! A' h. Q( ?9 g, R
- Return e raised to the power of x.; K) u0 k5 O5 R* x9 N0 o
/ M9 o8 d: T. t0 J, p- >>> math.exp(1)
+ a( D; E" ]4 }1 u$ C$ \5 ? - 2.718281828459045: F1 h+ J. C5 ]; y; ^3 N
- >>> math.exp(2)
& E" V" z) f9 \7 N - 7.389056098930657 b# X- o1 o) a$ G, g) l, m
- >>> math.exp(3) H( ?- q. ]- G) w+ k
- 20.085536923187668
复制代码 . f% B. o' x8 Q
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 Q2 t7 \) N* V6 S. T
- #返回math.e的x(其值为2.71828)次方的值减1& p& V% W- Q& H1 K7 A" O
- expm1(x)
3 e0 S4 V* E2 A" V1 Q" r - Return exp(x)-1.
; \3 M' I3 E3 w, s4 U& J* K - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
6 [0 _# U% W4 E3 _5 d
P g z3 ^$ d- q6 P- >>> math.expm1(1)5 E2 V! s- u7 ?8 Q/ Z2 o
- 1.718281828459045* u/ P/ Y( \5 V( L
- >>> math.expm1(2)
* H' H- x* S1 p4 R7 I9 s# X( q! R - 6.38905609893065
0 }# `% z' K0 M6 L) v" l - >>> math.expm1(3)
y8 ]1 p9 f; Z1 l( T0 ] - 19.085536923187668
复制代码 $ _: {6 t' ^0 H8 L6 l9 S0 s
math.fabs(x) 返回x的绝对值6 B. G; @! U5 d y- C& P; b
- #返回x的绝对值& Q* y( _+ Y M9 U7 ]; _, g) t' ` }
- fabs(x)
* ]: E, A& _! G - Return the absolute value of the float x.7 N- w' F+ O' B
- 4 e4 ~* G$ K' R' h" e! ~. I5 Q
- >>> math.fabs(-0.003)+ U; H6 |. l* I: f
- 0.003
. m* _: r/ p9 b- b# }# _ - >>> math.fabs(-110)# A3 I& J+ Z8 S% a
- 110.0& }2 L5 r; {+ X, c1 \
- >>> math.fabs(100)
+ x# S* E1 K, h0 }0 ? - 100.0
复制代码 : p! w* S3 N! J6 T( ?) _0 z
math.factorial(x) 取x的阶乘的值& |- S; M" e7 p C
- #取x的阶乘的值
7 n7 ~$ x. S" _# K7 L - factorial(x) -> Integral" n4 O2 W( K4 v' R. _ F
- Find x!. Raise a ValueError if x is negative or non-integral.$ g: K/ m$ q' e# k3 y5 h
- >>> math.factorial(1)
7 J' ?5 k2 E1 J5 ?$ |' f& f0 D - 10 `7 `6 D. ?1 L
- >>> math.factorial(2)
& V0 ]. `$ F. B& x - 2
( Y$ O/ e( q8 @ - >>> math.factorial(3)+ Z3 S& l- J) }% t0 y% l0 O! |. F! f
- 6$ D1 e$ B z3 r: p! C$ h8 w( |. m
- >>> math.factorial(5)4 H2 N& M+ V7 f* q" ]1 O" y: h- l
- 1203 |' j& |5 B6 F, w
- >>> math.factorial(10)
/ a+ `/ D- V) w: V# X - 3628800
复制代码 2 B8 `' V" Y- }! g( E- U
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! U: d& I6 y; Y- j! t! f- #得到x/y的余数,其值是一个浮点数
* h f* o, }1 u2 x4 j - fmod(x, y)+ U5 N. c( U; p7 I8 G
- Return fmod(x, y), according to platform C. x % y may differ.& k4 J- W- o0 }
- >>> math.fmod(20,3)/ d( t7 b1 } q" \- O: w# E9 U! o( t" u
- 2.0
" g* B" o& e- e: n& }) I# c+ q9 k - >>> math.fmod(20,7)3 s4 y( r/ ?$ E5 b* K* D& q
- 6.0
复制代码
! u. x' l( z4 u5 d( z6 gmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
& O- V! O' ^, Z- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 e7 f x3 }+ ]8 u9 x6 M% w - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. H" L- ^4 T4 [
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ V" K2 \9 H! @: f5 g" s - frexp(x)
4 s1 F8 O1 h! n2 |* P9 |4 X - Return the mantissa and exponent of x, as pair (m, e).- j# S3 F# P' T2 m
- m is a float and e is an int, such that x = m * 2.**e.
' U; V, e/ s+ }' R G4 ^' _ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.; s2 u6 v$ G: ~9 X6 ? u M) A9 O
- >>> math.frexp(10)& g& p; Q* v7 E( R6 T; X2 G
- (0.625, 4)1 ~- K' k! e2 b" @8 e6 S
- >>> math.frexp(75)
# \8 e: p' j U# k" r: ~, r - (0.5859375, 7)' l( U4 h4 J* c$ e7 X6 ]" k4 _, M Y
- >>> math.frexp(-40)/ h: `3 U! L6 V
- (-0.625, 6)9 R" O. o: l k; m" T2 W8 r
- >>> math.frexp(-100)4 f! T$ } @0 A* @
- (-0.78125, 7)
2 m* p6 V$ v& {4 B - >>> math.frexp(100)$ ?# a$ ~4 U7 p1 N; W) a0 a1 M
- (0.78125, 7)
复制代码 & r n, r9 D- F
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
) t: k! F2 J8 {6 C9 n- #对迭代器里的每个元素进行求和操作/ c1 x& x0 l: G
- fsum(iterable)
S' b* I0 W6 |4 k h0 c - Return an accurate floating point sum of values in the iterable.
( p! u2 y' Y0 o6 P! m. h: N9 Y4 y' F - Assumes IEEE-754 floating point arithmetic.
: j& e1 T0 e) G G - >>> math.fsum([1,2,3,4])
( ]& z* J- F3 B1 E3 ` - 10.0
* T# b/ N" e: S8 r J - >>> math.fsum((1,2,3,4))/ A! y/ O6 `- }; Y
- 10.0# d- }3 u7 x5 v3 c7 m
- >>> math.fsum((-1,-2,-3,-4))
6 b; V: ^3 b7 R; X% B - -10.0
) d9 U: ]. Q, N# ? e - >>> math.fsum([-1,-2,-3,-4])
$ W& i' J0 _' o# Y - -10.0
复制代码 5 z7 y: s6 Z+ R, N( y* f
math.gcd(x,y) 返回x和y的最大公约数9 _% f8 B9 G2 b# |2 _, _
- #返回x和y的最大公约数' s+ B7 i) I2 w. {* N
- gcd(x, y) -> int* h" `- j* q$ R$ l6 ~4 i$ L3 B
- greatest common divisor of x and y
" F- z4 _2 Q5 B1 g. |+ h v; P8 i - >>> math.gcd(8,6)
; t }+ _, q7 q# _ U - 2
( E3 v d& X9 w( E - >>> math.gcd(40,20)
& ?6 k6 k0 O9 k4 T - 20' m8 V6 ~0 g/ z$ c
- >>> math.gcd(8,12)1 }, _6 d- ^8 r% ?6 e c7 k
- 4
复制代码 0 i/ V4 }: e c" V' T7 e
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
) t& s- Z$ J" e, n9 r- #得到(x**2+y**2),平方的值
7 N4 h8 _: D8 P3 K2 O- Q - hypot(x, y)6 N( P# U7 c- o
- Return the Euclidean distance, sqrt(x*x + y*y).
k; [$ H, v7 u9 ^' M4 c* ` - >>> math.hypot(3,4)2 \0 a+ R9 }2 I& c. i3 l- p9 |
- 5.0% W) a* u. L* X# _ R- B
- >>> math.hypot(6,8)
* w) \# ]0 j* Q5 K0 q% w1 R( h - 10.0
复制代码
" P3 _5 N% U7 `7 G: q4 }6 Q$ smath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False1 J1 Q: Q5 o% c) X
- #如果x是不是无穷大的数字,则返回True,否则返回False
5 U k! j. m6 T- v1 B4 b/ l) B/ c - isfinite(x) -> bool, s4 ^0 D) x7 I8 K
- Return True if x is neither an infinity nor a NaN, and False otherwise.9 p$ P$ }: L! [3 O
- >>> math.isfinite(100)
" u7 M- ~5 C# { - True
! m+ S% S/ j" u* E5 q$ [2 g - >>> math.isfinite(0); t8 ?( M9 C- {* i
- True6 y* ~- K$ \8 _8 ]6 t7 m# O* p( C6 ~
- >>> math.isfinite(0.1)
8 s1 I3 Q8 K. |, o: f4 e. y - True* O) m! D4 j# C" B* c2 L
- >>> math.isfinite("a")
" P; O/ Y) K5 ~* C' ^. L - >>> math.isfinite(0.0001)! l/ S0 R2 g o$ e
- True
复制代码
$ I- ]" \' I! Y5 I" Umath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False6 b' s( m8 f7 O3 q f2 m
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
" @) }/ w. ?& g: P% ^9 z# J - isinf(x) -> bool
* m# \, M& I4 t# B- b3 i' G. R2 H - Return True if x is a positive or negative infinity, and False otherwise.0 [+ U3 S5 E, E) z, V1 M7 j7 S
- >>> math.isinf(234)) @+ i; ^) E8 i G5 S
- False- x0 g- L2 h6 q( X7 F) n0 Y- ^
- >>> math.isinf(0.1)
/ N" j; t8 @ G' w - False
复制代码 - I) ~+ L+ V" u5 x. p- C+ I3 O
math.isnan(x) 如果x不是数字True,否则返回False- |# A1 v/ k4 M7 M9 F( a& b
- #如果x不是数字True,否则返回False
* R! G% s' P/ _' l% w4 C9 l1 M& x% Y - isnan(x) -> bool0 @* H4 Y- ?4 s& z
- Return True if x is a NaN (not a number), and False otherwise.* s& v3 i) ~1 v" A$ R b I4 |: {
- >>> math.isnan(23)2 S9 q$ j& x( ^, g& w0 Z/ d
- False
$ P4 N+ M$ V% `# A# _# S( f- T - >>> math.isnan(0.01)
5 V. g5 v% [; @2 q) {- a - False
复制代码
$ y x d, I3 u8 T6 rmath.ldexp(x,i) 返回x*(2**i)的值
0 z# e b% O7 a' s& D6 C, S6 ^; u- #返回x*(2**i)的值
) A- x7 z# u! ^2 O% o - ldexp(x, i)
* y/ U: I+ d4 |) Z - Return x * (2**i).
/ m5 J! x1 _3 A6 V* ?9 S5 Z - >>> math.ldexp(5,5)2 Z) k) ~" K b8 L6 k$ P7 m, U
- 160.0
) R( @) `; q2 N! o/ F" ` - >>> math.ldexp(3,5)
" t9 e, T* z6 [ - 96.0
复制代码
3 M( x' M7 C2 t$ `+ jmath.log10(x) 返回x的以10为底的对数5 h( p9 ^1 x$ `
- #返回x的以10为底的对数
1 H: X. ?' n& c% ` - log10(x)8 z7 @9 z% u; c3 E# A
- Return the base 10 logarithm of x.
3 a) }* ]6 [* {% |# C+ L8 H - >>> math.log10(10)) {5 b, W3 [/ q8 O9 G
- 1.0
8 ?* x+ z1 m; E( E4 c9 A$ f - >>> math.log10(100)
1 B2 i) A8 x) A& P2 k: _ - 2.0
: B6 S* F s. a3 S3 P" U( P - #即10的1.3次方的结果为209 Z* m. U( p# `& t
- >>> math.log10(20)
4 P3 u1 e; y* v) ~. B* @& q - 1.3010299956639813
复制代码 ) c+ O1 i- }$ P
math.log1p(x) 返回x+1的自然对数(基数为e)的值+ P! B+ h6 i. z1 j# u; A
- #返回x+1的自然对数(基数为e)的值
' U+ e$ q/ u1 s( \! l. [% m0 |" {$ a - log1p(x)
8 x" d ^5 a+ R- s: Z - Return the natural logarithm of 1+x (base e).
8 B7 c7 Z' O2 } - The result is computed in a way which is accurate for x near zero.
) P9 W+ v+ a, s( @( _& ~ - >>> math.log(10) A+ G$ X8 N! F$ {( d) y& T/ k
- 2.302585092994046( a$ z) {, s8 i8 C- H0 D* I
- >>> math.log1p(10)
- P5 \' J% n7 X) ]5 O0 c - 2.3978952727983707
. W: w$ R" C1 g$ l; Y* @" ]" w - >>> math.log(11)
; W3 Y* u9 R% |/ M' M0 ?* O3 y- [# X - 2.3978952727983707
复制代码
7 i8 x( ~. f2 `! \# Jmath.log2(x) 返回x的基2对数! W! x0 I# q: S2 ], w; X; G* y
- #返回x的基2对数
! g4 ?7 S1 K) i+ C$ O( n - log2(x), {) z1 A8 z" M2 c/ k
- Return the base 2 logarithm of x.
& K3 g' I; m# c% W+ x2 _ - >>> math.log2(32)
0 K4 I4 Z% [2 u0 m' g - 5.0
# F% z9 h' o5 q - >>> math.log2(20)
& M0 H$ g/ A q0 D6 L - 4.321928094887363
7 u7 |$ Q5 M+ N: r2 H' | - >>> math.log2(16)
8 t: `* Z( {8 k( ], w - 4.0
复制代码
# x1 t6 t9 U3 V5 m. e# y( g6 z' Mmath.modf(x) 返回由x的小数部分和整数部分组成的元组+ J" J% q' A8 }& N: ]$ }
- #返回由x的小数部分和整数部分组成的元组; K. b" n0 @3 G' r v |
- modf(x)
3 [1 `5 ?; Y# ~7 E - Return the fractional and integer parts of x. Both results carry the sign7 e* w. \* ~4 T8 Y: A
- of x and are floats." S9 ~* O& g! B2 i1 z" Y' \
- >>> math.modf(math.pi)
# i/ o3 R: r6 X - (0.14159265358979312, 3.0)
7 n2 E& Q8 S& T1 X- e0 |8 k - >>> math.modf(12.34)
' `' z+ \5 Y e+ z3 W* C$ d6 ?* Y x - (0.33999999999999986, 12.0)
复制代码 4 u3 B7 l% c7 `$ b8 B; ?
math.sqrt(x) 求x的平方根
$ U* f. `& ~; u! s# m0 I- #求x的平方根
& I+ ^9 |/ u% X( Q5 r, o - sqrt(x)
1 Z, D+ ~7 z; O) p) m. ~ [. s5 u - Return the square root of x.. i$ \/ c( U# u/ P) @
- >>> math.sqrt(100) I% {1 k7 u5 ?' W! w
- 10.0
1 I, ]) H6 p* C( D* F6 j* i, ] - >>> math.sqrt(16)
% S4 `1 ]5 [0 r& Y - 4.0
! P" h W0 o0 ^- a: `2 l - >>> math.sqrt(20)
d. e! ?" ?0 l9 `7 o$ { - 4.47213595499958
复制代码
4 Q" Q" L' t7 C# T# m ~math.trunc(x) 返回x的整数部分- #返回x的整数部分1 k8 x+ g$ F: M
- trunc(x:Real) -> Integral
; Q" o; m H. P% n - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
9 Q; p8 j; b; b, h% I - >>> math.trunc(6.789)
$ l% o5 }# J# s - 6; o# w( Q+ S% \' r
- >>> math.trunc(math.pi)3 B; H0 b7 r( E
- 3
7 i5 W' w$ c0 q+ H% R, V- r - >>> math.trunc(2.567)& Z4 C. z4 Q( Q% p+ J2 L2 X. I" Q
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|