马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 a; O2 C+ E8 Z& A9 W
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 K: L- N' _: C# W
- ^7 v- c- x6 ?2 u# C1 ~% t方法1:+ ?5 F# v' v! P- A3 \% Y. T/ G
- >>> import math1 X2 s9 l/ @+ v6 d
- >>> math.sqrt(9)2 M& w5 Y3 @* L- z4 \$ p$ n
- 3.0
复制代码 方法2:5 ~% I: N& X9 ]* b' C& ~+ r% o9 R/ |
- >>> from math import sqrt
/ u: V% ^( @8 _/ e6 a - >>> sqrt(9)) Q& }. O$ P/ g' L( ~* {# u: {3 v' X
- 3.0
复制代码 " N' Z! l [# q( L1 n
* F- v8 {! ^: Z/ W7 ~! }math.e 表示一个常量
3 a0 c- C* T0 K t/ c1 ]5 |9 Y2 c0 Q- #表示一个常量
6 @" t; g. o# |) g1 T$ }) D - >>> math.e7 V0 W: f7 g1 i) h" }
- 2.718281828459045
复制代码
4 L G6 ^9 k# i9 Z$ p6 r( ]math.pi 数字常量,圆周率
& }/ ]6 P* ]! m" w7 c; ^- #数字常量,圆周率
; N0 D4 c8 Y+ z( ]% g' W3 s, O+ ` - >>> print(math.pi)
1 n" Q5 P0 O& y2 V& h - 3.141592653589793
复制代码 : i% O6 _0 S6 ]- g/ ^' `
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
. D w# `9 K- V6 b9 i- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 ^( d7 F; b5 u y7 `1 ?7 y - ceil(x)
0 y+ H _2 N1 n7 G( B - Return the ceiling of x as an int.
2 C' z1 e$ ]' R3 r1 V8 ]# h- w - This is the smallest integral value >= x.
, Y/ k$ m, S3 Y# r( }
: r& x6 T5 H8 S; z V0 y- >>> math.ceil(4.01)+ |) n7 ?- U/ W- o) O
- 5: R. E* x) Z Z5 Z/ [+ L5 k' A
- >>> math.ceil(4.99)7 d! O7 T2 Y- s# w5 N8 x" K
- 5
* K+ x% e1 l. \. `! m3 j- o - >>> math.ceil(-3.99)
9 N( k$ K. j2 Y8 } - -3
$ z- ~/ q) k7 _. ^0 Z' ]; b - >>> math.ceil(-3.01)
! H% x, [. M% ^, V - -3
复制代码
. B! j* P1 ]( s( Q1 }2 }0 B" @math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! O" [0 y6 R! _9 }- q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 `# [& O) Q, l5 ] - floor(x)$ L4 i; U% ?, @4 q) ^6 ~ P/ \ A7 u
- Return the floor of x as an int.
! A" |$ i6 T0 n$ l! E - This is the largest integral value <= x.1 }1 g+ e! R# {! O: K0 T$ `
- >>> math.floor(4.1)
$ ^6 F Y" B/ X9 v( {& p; `! T3 T( M - 43 P" Y/ G4 u: Q$ h0 P
- >>> math.floor(4.999)* t& x- D; G/ C3 i/ F
- 4
$ F' N3 o( U/ f$ u. \& Z1 W) W - >>> math.floor(-4.999)
6 B+ [0 z) I9 C1 g M+ ] - -5+ D! i4 N5 e9 X" Y( ]
- >>> math.floor(-4.01)
1 k3 z7 G- U! O' L, F3 p9 [: j) d; c. O - -5
复制代码 8 i5 K' u9 X# u$ j9 M
math.pow(x,y) 返回x的y次方,即x**y e! F/ U5 J8 S+ b/ {# Z% b
- #返回x的y次方,即x**y4 N7 q' N( ]8 Y6 a9 c6 u) {4 |
- pow(x, y)3 U$ U# ?2 J) d" k& i
- Return x**y (x to the power of y).! E `, s- G C$ `
- >>> math.pow(3,4)0 w- b, p) B, m& W
- 81.0
# |; D5 `6 R$ h U" Q - >>>
% S" M, H2 V* Z' ]$ E - >>> math.pow(2,7)
: C2 A5 F/ ^ u/ n& o' ` - 128.0
复制代码
9 e# [ `3 K& nmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' r8 l5 g; F0 A& ` E3 G7 I- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) u1 Y* H+ G& \& a
- log(x[, base])
z0 Z! p+ s- e* T. M - Return the logarithm of x to the given base.
" D& E$ _" s+ d+ K6 [2 ?: n4 ? - If the base not specified, returns the natural logarithm (base e) of x. p( _- C& O- g+ R8 G
- >>> math.log(10)
( T- K- e- c4 U& }8 ]+ j+ y/ _ - 2.302585092994046$ q" W ~' L- r3 b0 L; }
- >>> math.log(11)0 m2 y& {* L! q6 v5 ^
- 2.3978952727983707
0 N& e' G; _( \, x) S _' U0 m - >>> math.log(20)& Z( T" A Q. o i' S) K& _9 X
- 2.995732273553991
复制代码 # q# x5 m, d8 q% g" m1 v. B
math.sin(x) 求x(x为弧度)的正弦值
6 h6 L B# D' B/ } Q7 w+ Q* w- #求x(x为弧度)的正弦值
6 r! V9 Q) z' t2 h - sin(x)
+ d1 N) a/ h) J) Q& z - Return the sine of x (measured in radians).
* h$ h- t3 V8 o - >>> math.sin(math.pi/4)
. `) S7 [/ ]8 W) m& @& `% s/ f - 0.70710678118654756 {' v/ m$ n# `; O2 K. m4 G- Y
- >>> math.sin(math.pi/2)2 J, Q5 W! C! @, }6 E* j' p+ O* I! k& o
- 1.0
, @/ h0 y) o$ C2 y - >>> math.sin(math.pi/3)
) R4 L8 V7 q! U6 ?4 P - 0.8660254037844386
复制代码
1 w$ |7 q/ p. ^# d, L! Mmath.cos(x) 求x的余弦,x必须是弧度
% o# Y! T1 T2 L0 i- W+ H- C- #求x的余弦,x必须是弧度
a7 b9 ^/ z' ~, N( A/ X# K1 s - cos(x): E3 R R$ i) Y, G N
- Return the cosine of x (measured in radians)./ x8 x! @: i' M" t5 N- I
- #math.pi/4表示弧度,转换成角度为45度
; r: t6 _8 f* N - >>> math.cos(math.pi/4)
" W+ e) n: p& h& N) V4 @ - 0.7071067811865476
/ E% q/ \3 M, t0 i9 ]" e - math.pi/3表示弧度,转换成角度为60度
* j+ W1 }2 J5 b K+ @ - >>> math.cos(math.pi/3): L' I( x) Z) a! z; H u
- 0.5000000000000001
" ?! H J0 @6 ?2 e: L- K& _' A - math.pi/6表示弧度,转换成角度为30度1 a' @# x/ |8 e; ?( Q& P
- >>> math.cos(math.pi/6)( Q8 C1 l, M- R8 `$ B h8 O$ O
- 0.8660254037844387
复制代码 ! {% S% H8 y9 G& c2 C E- j
math.tan(x) 返回x(x为弧度)的正切值" _( e: q( C: o% B% G# n
- #返回x(x为弧度)的正切值
/ L, q5 S6 h7 _, ^& Z - tan(x)
0 L6 T6 D! N( K" g/ d1 v+ n& i6 K6 y - Return the tangent of x (measured in radians).1 u# |! l6 c3 v! J9 }4 a& M7 `
- >>> math.tan(math.pi/4)9 }6 ]# C1 G& g' q$ X/ S+ Q) S
- 0.9999999999999999
' I, E% R+ w3 ?, _1 d - >>> math.tan(math.pi/6)6 |7 B. p9 A4 `# J
- 0.5773502691896257' e2 \0 J1 ?8 D% T: L3 f' p
- >>> math.tan(math.pi/3)
: d& w6 ~2 l, j2 K - 1.7320508075688767
复制代码 : Z& r; g: D8 ~3 D
math.degrees(x) 把x从弧度转换成角度
5 \5 B" o7 ^! X+ |0 i- #把x从弧度转换成角度
2 E4 H" `1 { h, ~: ?; g - degrees(x)
8 i' X( m: `9 ^( c" b3 ~ - Convert angle x from radians to degrees.
9 ?7 B1 e/ U- A: ^ - + W7 P2 `' b. W J' l. K+ |
- >>> math.degrees(math.pi/4)
% C: o5 m. v+ Y( N& y - 45.08 |! q/ X6 u) b6 K+ @7 e
- >>> math.degrees(math.pi)* Z" R3 f4 p a, P: ]7 w
- 180.0
% p1 `$ l! Y; s0 t R: F; b - >>> math.degrees(math.pi/6)
0 M) m; B( _% r+ D - 29.999999999999996" f4 Y1 U3 W7 Y% h% S1 `
- >>> math.degrees(math.pi/3): n' Y8 m5 V: L {- d
- 59.99999999999999
复制代码
5 F# X% l6 w5 O0 D0 Lmath.radians(x) 把角度x转换成弧度8 g0 h% V9 C2 I2 j! ?5 p. a
- #把角度x转换成弧度
; E0 {0 S$ S% @ - radians(x). ?+ m9 g7 a# }& A/ \9 ?
- Convert angle x from degrees to radians.
8 U% Y! i' u4 x* k5 q9 ` - >>> math.radians(45)3 T3 e+ h, a9 {
- 0.7853981633974483
; B4 O/ }' G' v9 e3 h" h - >>> math.radians(60)' u- d0 u+ i8 J t- C0 h( ]
- 1.0471975511965976
复制代码
. k( W4 P( e) Bmath.copysign(x,y) 把y的正负号加到x前面,可以使用0' R" R* \9 t6 u) v% x5 k
- #把y的正负号加到x前面,可以使用0
9 v! Y: |( ^, I8 s# n - copysign(x, y)
. _3 L5 v* D7 t$ { - Return a float with the magnitude (absolute value) of x but the sign
0 S. E* D* d3 S9 h - of y. On platforms that support signed zeros, copysign(1.0, -0.0) S) h* k/ z: i* G$ W* b/ ^
- returns -1.0.
$ w" L" X z: w! m* x. g
) y1 b* W( ~3 W- >>> math.copysign(2,3), P9 z+ K/ h- D+ R7 c$ c
- 2.0
- T1 |5 T8 Z6 a4 ?, t+ m8 Q- G - >>> math.copysign(2,-3)
. m! H/ w& e; W- r" n+ G, i- p - -2.0
6 G5 A% m. X: A) r3 [: }) J7 ^+ e, ^+ m - >>> math.copysign(3,8)
; p9 Y% J; U( b; H( M2 D0 s' Q3 t - 3.0- f+ g$ S5 i9 K5 ^' @
- >>> math.copysign(3,-8)
: M% |8 ^% I7 g8 H - -3.0
复制代码 4 g+ b8 G+ m) Z, p' `
math.exp(x) 返回math.e,也就是2.71828的x次方
) h. d j: o$ h s4 s# O- #返回math.e,也就是2.71828的x次方
2 `6 S: ]9 ` D2 A- a `" f# W - exp(x)7 g. ^* w% ~$ }. A' ~
- Return e raised to the power of x.
7 f) v0 J0 Q* [0 a1 S* c! R
) y: z1 N8 Q2 E: \- >>> math.exp(1), t) b# E5 X$ c7 L+ `* F2 {5 A( a
- 2.718281828459045
9 g/ D$ q9 u- x: l - >>> math.exp(2)
6 \9 [+ n4 J9 k - 7.38905609893065: w: l/ o2 ?! w3 S$ o
- >>> math.exp(3)7 T1 b8 O: b# k! I z0 O
- 20.085536923187668
复制代码
- @ N3 x7 I3 @: m, kmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
2 }5 L4 u& M/ W- #返回math.e的x(其值为2.71828)次方的值减18 p6 {; z4 p k( o0 ] t
- expm1(x)
) h- V, ]1 u, _% i - Return exp(x)-1.! y% q& [' x9 z% @) f c, ~ L
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 P0 v6 R5 `0 K4 p/ y4 V9 Z
5 R$ W; a/ g! A3 r- >>> math.expm1(1)2 Q+ M: h8 Y8 Z4 m1 T; k- z; @
- 1.718281828459045( S' ]7 r% g! _, r5 C: Y
- >>> math.expm1(2)
4 ?9 \! n$ q& ?, B: m+ M9 J$ t - 6.389056098930653 o) d3 h+ \$ h! V# S# M9 e4 o
- >>> math.expm1(3)
6 n, D& k, W& {5 d7 l' k - 19.085536923187668
复制代码 1 K' ~6 |7 u" k# o1 |6 c( y1 y
math.fabs(x) 返回x的绝对值
/ B' A! O% [! H- #返回x的绝对值
: ]% Z0 o( N) y- E6 W - fabs(x)
0 s5 p8 O; }* D: y: y$ c- ~; r - Return the absolute value of the float x.
" O- D9 j+ v( U7 v/ i
' @2 {; z: A8 @+ v- >>> math.fabs(-0.003)
8 {; Q( Z) m; s& ]6 ? - 0.003
" j; M# z6 G# ^; P - >>> math.fabs(-110)
0 d, _% e$ f3 V/ ^ - 110.0
5 O% B; [* X, e& g; \6 c. U. _1 L - >>> math.fabs(100)' g; H, z# i# o6 z% ?8 }
- 100.0
复制代码
8 n3 B8 U+ z( F+ @# g. omath.factorial(x) 取x的阶乘的值5 h. S4 C8 a* k5 V# y
- #取x的阶乘的值& n7 N/ r# A9 ?1 A8 T8 h
- factorial(x) -> Integral2 v+ A! r) N, G% h! S) I
- Find x!. Raise a ValueError if x is negative or non-integral.
, n: v5 u/ |8 H1 m! B - >>> math.factorial(1)" U0 Y+ a7 H2 c% |" ^3 @# C4 j( ~+ n* F
- 1
1 q# e |% L/ j% |7 g2 S - >>> math.factorial(2)" {% |9 L7 f6 @! v9 c; O$ y
- 20 W; d. \) D$ ?4 x
- >>> math.factorial(3); R. ]) S5 y+ z
- 69 B4 C/ x8 A' T; s% i8 j" C
- >>> math.factorial(5)
* d" D+ T, \! J4 q - 120
% }; x! p0 {8 t - >>> math.factorial(10)
5 F) c* T+ }. c" O# K - 3628800
复制代码
5 w! n9 ^5 d9 R4 c8 R$ pmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
; y" `! H" x. t, D! u0 F' L9 p. s8 ~; d- #得到x/y的余数,其值是一个浮点数
$ w* E* f" Q7 |, r0 a5 V - fmod(x, y)$ J. N: b7 f d9 w9 O ?" t
- Return fmod(x, y), according to platform C. x % y may differ.8 M: u% j( Q6 I+ E* R/ U% V
- >>> math.fmod(20,3)
( ^8 A: T+ n+ ]) ]8 d+ M# S( S - 2.0
) G( Z4 r1 g) q6 N - >>> math.fmod(20,7)
+ |$ I7 f- t0 o* K - 6.0
复制代码
3 I" X' N" n& P. ]math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
, M( r a4 x/ S9 [- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 q. y: T- N; \, x4 @5 U+ Q* Z6 F [
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
: Z Y9 [& x" m/ t- V) M - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) V7 O) @# u+ {9 L$ @2 m
- frexp(x)
3 X+ P! ^. @" t+ q7 [" o - Return the mantissa and exponent of x, as pair (m, e).
9 d1 A+ t& e" N( j2 T - m is a float and e is an int, such that x = m * 2.**e.
* F; P. N2 F1 ~+ [. c, n - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.3 h& D7 @4 _2 v B+ f* \: `& }
- >>> math.frexp(10)0 a- U. d6 N/ Y
- (0.625, 4)
M9 [2 @& {8 J0 f% Q - >>> math.frexp(75)* c0 X* Z, m6 ?8 Q! P2 M# V! P
- (0.5859375, 7)
5 w- Y! [6 E/ D9 e! O- H0 [0 [ - >>> math.frexp(-40)
$ z% o ?- v! h: h - (-0.625, 6)* J$ \* E8 G) W: x
- >>> math.frexp(-100)
; a5 M' F! \' d# Z: u: [" A - (-0.78125, 7)
, i0 O7 [# z6 t W: T - >>> math.frexp(100)
) Q* s7 F2 \: u; H% d) @. P - (0.78125, 7)
复制代码
" r; k& P( n* v. ymath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)) k& Y7 m8 e" H
- #对迭代器里的每个元素进行求和操作
) R o- S% [. }5 L/ ? - fsum(iterable)& F6 x! f$ T W: A
- Return an accurate floating point sum of values in the iterable.
! J/ A4 o Y4 N! \4 Z, O' `5 v - Assumes IEEE-754 floating point arithmetic.
6 C- J# n" j5 D - >>> math.fsum([1,2,3,4])
1 U! v7 E' |, J0 V6 U F. I* [ - 10.0
+ f! Z% d) o+ ^' U# i" ^3 { - >>> math.fsum((1,2,3,4))& j, b4 p3 c- k! ?1 _: {
- 10.0$ y2 g J c) D& }& \
- >>> math.fsum((-1,-2,-3,-4))2 T% @6 i8 d9 `4 h
- -10.0* B; m9 k# M: ]8 a5 e
- >>> math.fsum([-1,-2,-3,-4])% d; u( k* s/ A4 N6 E/ g
- -10.0
复制代码 0 ^, e/ [% m2 R0 i" Q, r
math.gcd(x,y) 返回x和y的最大公约数9 F* J1 T, ?) F* B
- #返回x和y的最大公约数
h1 [* d( {8 s! y - gcd(x, y) -> int4 r1 c8 D& P2 |( i3 U' d
- greatest common divisor of x and y4 n3 B2 K. Y( [4 R V& K
- >>> math.gcd(8,6)! _$ z4 Z. Q$ \: B. X, H
- 20 c) h% J1 Y; }7 m% S. J, g
- >>> math.gcd(40,20)) z8 X# |* K7 J0 o6 c6 ]' p$ Q. b
- 20% |* Q) K/ w4 |& U( K: W
- >>> math.gcd(8,12)
4 l' {( W5 _" \5 m8 g# e3 } - 4
复制代码 ! C8 Q" X% {, m7 g7 X4 @3 u7 T w
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
" c0 z( F/ ~ v0 l' @" `- #得到(x**2+y**2),平方的值6 j, N% b9 H* E" x/ ~& r1 D
- hypot(x, y) M, [: _# Y- l1 q& @
- Return the Euclidean distance, sqrt(x*x + y*y).
# i7 J9 {3 F+ P) [* N5 w# Q, j+ B6 { - >>> math.hypot(3,4)
, r7 I8 v* g& M* H5 K - 5.00 p; E' T. Y5 F B$ B, W+ K
- >>> math.hypot(6,8)
- J0 u ~7 W2 z J* y/ H - 10.0
复制代码 1 z& O9 x2 ?) ^
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False( i8 ?( j& q+ b U
- #如果x是不是无穷大的数字,则返回True,否则返回False" M! \7 q: Y! q4 |: x5 {. z6 f" ^4 S
- isfinite(x) -> bool) z( B( S$ u! \6 r& B: O: X
- Return True if x is neither an infinity nor a NaN, and False otherwise.; x! I/ W p; j4 G, x
- >>> math.isfinite(100)
& Z, L: @! z/ G/ t+ |0 L - True
6 O4 M. m3 a5 c6 b - >>> math.isfinite(0)' |6 a2 I5 H9 f, s5 A, k
- True. L/ z# s; N% t4 v- o! }1 w/ d
- >>> math.isfinite(0.1)
3 n, |& ?. Q2 y0 R - True1 h/ Z+ \5 k0 U1 ~
- >>> math.isfinite("a")% r2 v% s2 m; J! z. z7 j* E
- >>> math.isfinite(0.0001)
- ~/ N* F1 e5 d" Q4 K - True
复制代码 ! i7 }0 l7 ?. i% b0 ]
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
' @$ G& ^& |4 _+ f6 h- #如果x是正无穷大或负无穷大,则返回True,否则返回False
$ A" f; b. \7 z/ M - isinf(x) -> bool
; p g/ @1 b$ L; h) s6 I( P/ e* z - Return True if x is a positive or negative infinity, and False otherwise.
4 J8 Z$ }# z/ \1 D - >>> math.isinf(234)
8 H, M$ ]4 T! s6 [! _ - False% Z% W& c& p1 @4 l) v! w
- >>> math.isinf(0.1)! A Z6 O, [+ e8 L5 ]; K' T
- False
复制代码
, \5 E4 q: y% ~1 a' y4 I! Qmath.isnan(x) 如果x不是数字True,否则返回False
+ H6 m! a3 B+ x0 U4 K2 n- #如果x不是数字True,否则返回False4 N7 O0 H: n4 h. O6 C4 D+ c3 G* ]
- isnan(x) -> bool" {1 \$ ?5 X4 c8 ?
- Return True if x is a NaN (not a number), and False otherwise.; P7 b5 B& y0 `
- >>> math.isnan(23)
O' E+ G( Z. `$ ^ - False2 j1 M q* U- r4 U1 A1 a
- >>> math.isnan(0.01)" Y+ \: V0 J% A% L( | @
- False
复制代码
, g) g9 O: a. S! {, a0 }math.ldexp(x,i) 返回x*(2**i)的值
/ f3 j. h n" l* l- ^7 X7 H- `- #返回x*(2**i)的值8 U1 Z* v) B* \! w- y5 I# Z
- ldexp(x, i)5 g- P/ A5 q+ n/ |% {
- Return x * (2**i).
) p! e7 r6 T4 P - >>> math.ldexp(5,5)7 l. c4 [2 W5 J; |1 V! c
- 160.0
G* S( n5 D2 s' B! H7 _3 b - >>> math.ldexp(3,5)4 J- {9 ~( j+ J f: x7 R
- 96.0
复制代码
+ l. L/ g6 ^- I. s$ xmath.log10(x) 返回x的以10为底的对数2 X5 U1 i' `( G& F4 ~
- #返回x的以10为底的对数
; W- r9 X6 F' u4 l - log10(x)
+ X B, e% ?' g1 W0 l9 @ - Return the base 10 logarithm of x.
4 o. G# G2 j% `$ i5 M6 H$ l - >>> math.log10(10)2 n. b: f, y' Z: z; z0 i
- 1.0
7 M& s* O2 o3 a2 W/ W - >>> math.log10(100)
& o- B$ |+ I0 j# L6 B - 2.0$ r% x# c4 R0 T2 j) x
- #即10的1.3次方的结果为20
& Q5 i7 d0 t4 o2 O! Y' P - >>> math.log10(20)
4 M5 c, y Z7 k6 c6 P - 1.3010299956639813
复制代码
' t" n" X# j Z0 N2 `' {7 Nmath.log1p(x) 返回x+1的自然对数(基数为e)的值0 ?4 y3 }/ W e' G( v" W6 M
- #返回x+1的自然对数(基数为e)的值
4 v+ |# ]+ p0 G - log1p(x)
9 f8 a( B' w4 l5 o% z8 } - Return the natural logarithm of 1+x (base e)./ a- A9 k6 Y$ X* Z( o6 M
- The result is computed in a way which is accurate for x near zero.
: @) k$ R' t: y) T. T8 @: _, E* Z - >>> math.log(10)+ u& u' \8 h: b4 l- K& A
- 2.302585092994046
( H, s$ ?8 A% M9 p! n; U - >>> math.log1p(10)
% T% m' Q1 j( r+ t; L4 i) M - 2.3978952727983707
2 c" W# n; q; A6 _* f - >>> math.log(11)
1 W0 Q7 U# @1 G: S - 2.3978952727983707
复制代码
0 n j: t) h9 [2 Cmath.log2(x) 返回x的基2对数
" |* g) M: g+ Z) N+ b- #返回x的基2对数- F; j- ]+ {# T* z, G1 M/ I
- log2(x). \$ K1 _9 U* o b6 t
- Return the base 2 logarithm of x.
; h. ~ l- K# g7 T' B5 x; P. q - >>> math.log2(32)
+ q# O/ a& H5 g: o - 5.0: q8 _" W9 I4 n
- >>> math.log2(20)- R* @1 X3 E" j$ T- ?
- 4.321928094887363
6 u( _7 }# z3 x. p+ D1 U' l - >>> math.log2(16)* O- J3 C1 Y, a: d' H5 I
- 4.0
复制代码
1 d& ]; ^0 L/ D6 ^$ z: v# smath.modf(x) 返回由x的小数部分和整数部分组成的元组
( ^# c3 q) l4 y; O& m0 }. d3 x+ P# [8 N- #返回由x的小数部分和整数部分组成的元组
1 q+ X+ P/ J% M1 N. Y - modf(x)( v% l. B: n; @, R5 N
- Return the fractional and integer parts of x. Both results carry the sign# D& f$ r. `, v! B$ c
- of x and are floats.
4 U0 O3 m8 n: }6 ~) E - >>> math.modf(math.pi)
. c% z4 b1 [/ l! h* h/ ~ - (0.14159265358979312, 3.0)9 m. u, ]$ {# L! M. c2 D
- >>> math.modf(12.34)
" [& h) ]; S" o$ d' \ - (0.33999999999999986, 12.0)
复制代码 : w5 R3 [0 a$ F2 |
math.sqrt(x) 求x的平方根& S5 }' @4 i* T- C/ D/ F
- #求x的平方根
; M/ n3 W& M/ ~4 E) ^/ x - sqrt(x)9 T, R; F u7 T% k, ^; v
- Return the square root of x.
0 L; X% a" _! _' T1 j# t; }" j - >>> math.sqrt(100)
2 c1 S+ o0 p* g6 e) i0 F" A& z* |# [4 E - 10.0
; T, S }: ^. [ K& l) X1 [4 v - >>> math.sqrt(16)4 l1 W+ m+ V) F3 j- l9 K7 t
- 4.0: h+ N3 r2 G: @2 k4 }, Z- b) @
- >>> math.sqrt(20)7 B' ]9 ?# Q. V: P$ o4 g+ r+ v
- 4.47213595499958
复制代码
% ]6 g& q4 b3 e. l$ _math.trunc(x) 返回x的整数部分- #返回x的整数部分
, T" u, D4 `3 d3 T - trunc(x:Real) -> Integral
; ~8 [. e7 w; U; S7 E! s7 s - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
' l: j8 t$ q) b g - >>> math.trunc(6.789)% c% {: M8 A$ t) T: F
- 6' J- E% q. M' B
- >>> math.trunc(math.pi)1 X* v0 ?- r$ |. n+ X
- 3
: I2 O! U" _$ w* X - >>> math.trunc(2.567)
" V2 |' I: I' z4 h - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|