马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) S7 X! G& L2 f- g# j) `6 z" I# \【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! A, o* }% ]' o( P5 \
0 b8 ? v: g2 D D7 {9 w
方法1:1 _$ w) j ^% ^/ J( w
- >>> import math8 Z! m9 A" X' Y" Z: W( ]! |1 B4 m% V
- >>> math.sqrt(9)
6 S5 p' z" c1 s - 3.0
复制代码 方法2:$ Y- m/ H7 q! A5 N3 E. q+ X$ p
- >>> from math import sqrt
4 s1 u' |) l' [8 K - >>> sqrt(9)3 f7 @( h' o$ B
- 3.0
复制代码 ( F, Y; g p. w9 I* e/ I* s
! |1 A$ {- o- k, z5 X3 x
math.e 表示一个常量
8 u' W" Q5 f( i# z. d5 `- |- #表示一个常量
; T4 d) y. m: w6 j- Y1 v# G, B - >>> math.e
' C. m5 R* k& j% N( A9 ~ - 2.718281828459045
复制代码 2 h/ Y; g3 ?' G
math.pi 数字常量,圆周率1 j6 w$ A9 C/ {' m+ O' Y6 j) a: ]! v
- #数字常量,圆周率
. j. i' L2 q# C5 Y' L0 y - >>> print(math.pi)) G$ ?9 D% ]' R. e( K0 v; I4 n
- 3.141592653589793
复制代码 $ U. y1 b$ t. B$ O# Z- H4 w
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
# H* a5 A5 B7 |9 c. h- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
( }8 N9 g7 H0 }; e, ]; y* L% s - ceil(x)
. N. h0 h1 l) J0 \ - Return the ceiling of x as an int.
9 V4 R( O, }0 q3 ~2 y8 `: E" z* }, i, ~ - This is the smallest integral value >= x., S% F; n( F$ g( U1 `# m( |
0 b$ C* B& F0 R" F t2 `' q" ^- >>> math.ceil(4.01)2 N. R3 m$ \# K: x! c/ {5 c
- 54 N& L6 y5 C s1 ?' d, K* B
- >>> math.ceil(4.99), E" A4 k: N# @4 s' k
- 5
, p. ^. ?2 L; U1 R1 V; D6 b3 l - >>> math.ceil(-3.99)
4 F+ `5 [6 P1 H+ U0 X - -3
+ e. e1 ?4 `# A) I' j - >>> math.ceil(-3.01)
5 {" t v, R( R7 }) l - -3
复制代码 5 s" ^3 Y# _2 S7 @! ]# ^5 A1 W
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身# y. e% X2 T) N
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
`8 F+ O* H9 G$ E( r1 Z - floor(x)
7 w. \5 {. Y& q9 j) E6 W- A* a - Return the floor of x as an int.3 E ~8 k* _* l
- This is the largest integral value <= x.& x' @% C' Z5 {9 ]/ i
- >>> math.floor(4.1)- |* H6 W$ \, h6 }3 n
- 4( X" k' h7 H0 o
- >>> math.floor(4.999)
# x) C8 q/ {8 R9 |# ?8 z' t* B - 4% F j$ S# |* V2 m; M* m3 L1 S0 u+ h. x
- >>> math.floor(-4.999)4 d: X8 r! U) I3 v3 E+ M6 g4 e
- -5+ M8 W( A$ l0 F( C( A$ v7 B& }
- >>> math.floor(-4.01)
$ S; s& |1 k" C/ Y - -5
复制代码 ( }/ o' v! i: R$ o+ {. p4 Z
math.pow(x,y) 返回x的y次方,即x**y5 S1 M ^ ~& x6 s8 j6 S# y
- #返回x的y次方,即x**y7 t- B( z3 t3 M/ B) O& a7 x
- pow(x, y)
# u# P) k* {) y/ v - Return x**y (x to the power of y).
# c- Y0 l4 K- y4 l - >>> math.pow(3,4)
: A5 b \) d0 L' z - 81.0& b' w, g4 `" ?/ T* X) r* Z$ j; B
- >>> $ ?# ?; I. G6 I0 _$ H" i
- >>> math.pow(2,7)& R6 N! U% G8 j$ g1 R
- 128.0
复制代码
/ J+ ^1 {* M$ gmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 p ?5 h. U- c* C- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). ~ O4 D8 |* H. |4 t
- log(x[, base])
+ M7 p4 f( w$ |' a1 f o( w - Return the logarithm of x to the given base. W: X! k% O$ f% I
- If the base not specified, returns the natural logarithm (base e) of x.
/ R( y; R" u8 U* Y% y# J* F - >>> math.log(10)3 i8 a- z- |3 b) o: f
- 2.302585092994046
. k0 \- h: @; Z" s - >>> math.log(11), v* b* T/ | H3 M
- 2.3978952727983707
, X/ m7 [1 J6 J5 L$ V2 i - >>> math.log(20)
% F. v* s3 O; w& K" p) G - 2.995732273553991
复制代码 / H% R* a: `+ [" C6 N, z
math.sin(x) 求x(x为弧度)的正弦值
8 P$ P |3 g' h. X9 O' h2 r4 e- #求x(x为弧度)的正弦值0 k; [! ~. M: s9 m' F6 P$ M
- sin(x)$ K2 k% P, x `' p; z2 O/ W
- Return the sine of x (measured in radians).
4 n9 N2 \/ d* N) t$ r - >>> math.sin(math.pi/4)
1 ^: s; Z V1 {$ s0 {7 S; ]- E - 0.7071067811865475
8 D% o2 s) K) t6 O8 L& n - >>> math.sin(math.pi/2)! C0 s/ u0 E; c7 l: j3 c r
- 1.0
& K4 m) D" D4 b - >>> math.sin(math.pi/3)
* i0 D4 m1 R# O! x! U - 0.8660254037844386
复制代码
8 m2 w, m* O. ~; w | xmath.cos(x) 求x的余弦,x必须是弧度
1 |4 O# U* g: E* z6 T) H) A/ o9 i7 L. q- #求x的余弦,x必须是弧度
# H3 E6 {6 d' N5 Z4 L - cos(x)
9 E2 K! n4 @$ l - Return the cosine of x (measured in radians).
6 v7 ]- ^8 ^ f: A& \+ g - #math.pi/4表示弧度,转换成角度为45度
) y; d8 o0 o* k- p5 j Y - >>> math.cos(math.pi/4)
; w9 f$ F" z0 [/ C - 0.7071067811865476
& D1 R* O1 K$ ]$ ~) w/ K - math.pi/3表示弧度,转换成角度为60度$ X, | h0 j( p1 E9 y
- >>> math.cos(math.pi/3)
; N6 B" i" H1 c& N9 C - 0.5000000000000001
" @! |9 R5 a0 x5 ^* I Z - math.pi/6表示弧度,转换成角度为30度
2 l( R6 ?6 [2 t - >>> math.cos(math.pi/6)
5 T3 n8 |+ K$ R0 ?- Q2 t$ }+ W - 0.8660254037844387
复制代码
- v7 S( F4 k$ a5 Jmath.tan(x) 返回x(x为弧度)的正切值' Q3 ~& O3 U* n5 w: N. B8 m
- #返回x(x为弧度)的正切值% ~2 ?% S( R2 {9 h P- ]* [
- tan(x)6 ~+ q0 d/ f" y# \
- Return the tangent of x (measured in radians).
* X& h; \' l: B/ |5 q# B: L - >>> math.tan(math.pi/4)+ E ]$ k9 e# ?2 l
- 0.9999999999999999
3 s& |( [8 D1 J- E: I! o8 g - >>> math.tan(math.pi/6)- p. I# I7 W6 i6 _5 [0 P
- 0.57735026918962574 m8 p! M8 S9 l. S- y5 t* G
- >>> math.tan(math.pi/3)2 s0 u6 C# Q& ?5 Q+ @
- 1.7320508075688767
复制代码 ' R8 O: \$ m9 W& l( N6 O" ^6 G9 w' B4 v
math.degrees(x) 把x从弧度转换成角度
1 S5 J2 a; O( q) J- #把x从弧度转换成角度
! o5 c0 a/ _" t2 Q - degrees(x)/ Q. o6 U% M6 ?4 A5 e' f5 C% f9 d
- Convert angle x from radians to degrees.
& ~3 r; A1 }+ X8 n$ D, E
- J' @3 B$ U8 E* H5 T/ u0 n0 c, O- >>> math.degrees(math.pi/4)
* n+ H+ e& U M2 p* i! ` - 45.0
: n' ]( D! Z6 h6 Y' q" n - >>> math.degrees(math.pi)
1 G! \9 G( c% ^) N) ^7 R" x) v - 180.0
3 R l s. l% t7 \3 P5 {' {# d - >>> math.degrees(math.pi/6)
) A$ A$ }5 J) e" m E - 29.999999999999996: Z/ H0 B7 j, h0 [2 b
- >>> math.degrees(math.pi/3)
# `- i9 M9 `: P2 y" E$ l - 59.99999999999999
复制代码
2 ?8 s6 `1 _7 G* Fmath.radians(x) 把角度x转换成弧度8 E9 B2 a4 s9 K; X
- #把角度x转换成弧度
7 C: N$ N# O u; j! l. X - radians(x)
4 O( }9 H& K; x- m2 ?2 v' | - Convert angle x from degrees to radians.
3 O6 C0 e) Q* P5 Q/ `8 @ - >>> math.radians(45)* V/ `3 Y. k, R
- 0.7853981633974483
6 U3 G0 C& M& d" `4 c& l5 | - >>> math.radians(60)
" l1 z2 R4 r. o; y: D, a! y - 1.0471975511965976
复制代码
a" _! Q- \. r, B G5 y/ { nmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
2 R) h: [9 [( s6 t, C- #把y的正负号加到x前面,可以使用0* H, C" U+ m) y v+ d0 ?( D4 O
- copysign(x, y)
, p- P, c: v$ L6 J - Return a float with the magnitude (absolute value) of x but the sign
; ]* U; `6 I( i5 N% ^ - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
4 `1 z/ v+ q6 C: ?( I. Q( V2 W - returns -1.0.
5 v7 h' N# K! ~. A
; Z" l2 v* ?) j2 M- I- >>> math.copysign(2,3)' P! E# J. e+ T5 a( H5 D
- 2.0
8 s( H1 ]! ^6 C/ I& U8 w. | - >>> math.copysign(2,-3)
6 c: D4 O. \) w* Z7 `6 c- G - -2.02 ~% u$ `, c! D1 h# [( M- S
- >>> math.copysign(3,8)
( j4 k/ U$ m" A d i3 W! ? - 3.04 a. Y3 ?$ G9 E6 G4 v
- >>> math.copysign(3,-8)9 e0 `- Q1 Y- Z/ C
- -3.0
复制代码 1 f ?$ S: N7 _2 r1 I
math.exp(x) 返回math.e,也就是2.71828的x次方
2 t$ H. E0 r) H# [+ Z- #返回math.e,也就是2.71828的x次方
( v4 q5 q! Z, E* }( g, Q7 a6 m - exp(x)
3 }, |4 q+ O8 [4 n t - Return e raised to the power of x.8 n# W0 p+ d/ y. [5 Q
1 w) X. o% u' ]3 N6 P2 M$ c- >>> math.exp(1)8 k4 I& i& L( Q' A; d
- 2.718281828459045
5 B. V( Q" P& k7 P/ ?. M3 K' \% [1 ] - >>> math.exp(2)
" \+ r) x9 D! g! V - 7.38905609893065# ?0 z# n9 T0 f5 ^: \. H
- >>> math.exp(3)% _( a* E; m; L7 e% O4 V
- 20.085536923187668
复制代码
4 k. w# c3 }$ zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
5 G" x8 i0 m- h, X- #返回math.e的x(其值为2.71828)次方的值减16 |8 n% Y: |' M: x* q7 U
- expm1(x)6 c4 `+ u3 b4 N3 r3 D& Y+ {4 @
- Return exp(x)-1.
1 ]( _# G! I5 ^* n1 @ - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
! m) S0 k' G- Y
; y! \9 Q6 C R* w8 }- >>> math.expm1(1)7 j R4 r, g# [$ D9 h q. s
- 1.718281828459045
H1 d* S7 _3 o( o& Q; k - >>> math.expm1(2)2 b |; H/ o' e9 h( w' E
- 6.389056098930655 s! U+ Q; m. ^, J& v& Y+ q
- >>> math.expm1(3)7 z9 A& T2 S( |3 m F. |
- 19.085536923187668
复制代码 0 `! m4 {" U/ O, \
math.fabs(x) 返回x的绝对值+ A. u3 s; w) h0 Q: ]
- #返回x的绝对值" c0 k+ x; \/ S3 E
- fabs(x)
/ j+ D4 e9 U0 s - Return the absolute value of the float x.
; M5 i9 ?+ Z+ D( _
; D" W% G/ Z) M) I/ e- >>> math.fabs(-0.003)
; N, y) W, b; k9 e( F7 f4 N; e5 | - 0.003( N4 z: W. m) ]8 P
- >>> math.fabs(-110)
8 i1 x/ b9 i+ ~9 ~ - 110.02 ?4 l T/ m" g3 p" Q2 b
- >>> math.fabs(100)
" n/ X8 C5 Z, {7 l - 100.0
复制代码 ! z0 u) {+ H t( z
math.factorial(x) 取x的阶乘的值
+ Z0 s4 I7 j9 ~! h. \7 a- #取x的阶乘的值
4 H4 u) Q' `* N e& ~ - factorial(x) -> Integral
0 F( g1 m1 L |7 l1 ^ - Find x!. Raise a ValueError if x is negative or non-integral.
0 P# z2 G7 h( n& j, g7 B2 A3 n - >>> math.factorial(1)
5 d' Q, i8 N4 j0 M3 l$ G - 1
) j R4 N* D: @: k - >>> math.factorial(2)2 y$ z# x# j% _: { L( b- X d( U; }
- 24 t. o4 e5 u0 A ]- J* X
- >>> math.factorial(3)7 q I& T, c/ w1 J
- 6 s! N: g2 R% `8 [2 B
- >>> math.factorial(5)
o# t/ Q0 t: Y3 M5 U/ I - 120
! t o2 n+ k$ ]0 q - >>> math.factorial(10)( G6 V% f$ w2 B V8 Y4 g
- 3628800
复制代码
8 W6 k( l1 w) Z; E: j! jmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
. t& l! _2 X; H4 v- #得到x/y的余数,其值是一个浮点数2 O6 X- n# l; I& x- R# N
- fmod(x, y)! z. g& Z$ B$ ]
- Return fmod(x, y), according to platform C. x % y may differ.
$ x. v7 c3 d9 t: I7 u - >>> math.fmod(20,3)# t! ?$ k- ? v a9 o! N; @
- 2.0
: R6 e' i. \% J3 J) ~5 B - >>> math.fmod(20,7)' ~1 @; X1 C' k' Z' X7 x* R
- 6.0
复制代码
6 S0 }* X0 [# }math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 Y5 ^" U8 i3 w* q7 r! N- C" x; W- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
0 ?% K% r& ?! V/ ?) U4 i - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值1 M1 D1 i' N# m ~# \7 E1 Y. @0 I- b$ K9 z
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ _! u% N P2 I' |# B% l
- frexp(x)5 u; T' [- k5 V5 X, U
- Return the mantissa and exponent of x, as pair (m, e).
- ?$ Y% o p' W! F - m is a float and e is an int, such that x = m * 2.**e.+ j U4 W0 t' B! F( I: V1 E2 B' u
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.: T' Y+ ~# r r- y0 K
- >>> math.frexp(10)* Q- A8 F; J3 U" Q; @7 C0 q: c
- (0.625, 4)2 r% Q. O0 _0 B0 a) r5 C
- >>> math.frexp(75)) T# J3 ~8 F8 L/ E
- (0.5859375, 7)3 E: Z5 W9 `: {1 M
- >>> math.frexp(-40)
7 U8 H0 m3 L" E7 V& p0 `' s - (-0.625, 6)6 b) d0 n9 H( P: _' ?' t3 r
- >>> math.frexp(-100)$ A) ~7 K! O R. V* d, M
- (-0.78125, 7)
1 {1 h4 c4 F+ t4 q- i7 T - >>> math.frexp(100)+ s; m0 O4 R/ _# k8 H, b! V
- (0.78125, 7)
复制代码 % ~1 B9 i$ |& O' i
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), e3 T; C% s2 d4 v
- #对迭代器里的每个元素进行求和操作; ^1 x" m. D, c
- fsum(iterable)
1 g Q* u7 L$ b1 I% W% b9 ~5 B - Return an accurate floating point sum of values in the iterable.! B- ?( n( e) B3 D" D4 O, @- N
- Assumes IEEE-754 floating point arithmetic.
* M7 b. R5 W$ {. _; r6 C% J5 w - >>> math.fsum([1,2,3,4]) w* T8 D1 R4 T0 N/ f
- 10.03 m; q2 w/ ]4 P7 d" Q3 f! @5 l, n7 O
- >>> math.fsum((1,2,3,4))
( I! Q1 C3 n u4 [3 R& V - 10.0
2 a7 M" ~2 a" V4 c6 e& \% V8 I - >>> math.fsum((-1,-2,-3,-4))
9 Q( I A0 E' o1 @1 ?: p- B' d - -10.0
+ o/ w# r8 _& s$ q - >>> math.fsum([-1,-2,-3,-4])6 o# k6 t7 H) n, j {3 G! g
- -10.0
复制代码 8 a$ A5 X" R2 j4 O
math.gcd(x,y) 返回x和y的最大公约数+ E. I# Z: C; i" u. P
- #返回x和y的最大公约数( [2 ^3 p& a+ n4 n% s
- gcd(x, y) -> int( i. [, B/ q+ u+ K" G
- greatest common divisor of x and y) c( v, I# L+ `7 ^; b0 m! S
- >>> math.gcd(8,6)- T; }" `9 w2 {. m' U0 r) F A
- 2
$ V! l. s) F4 V- `4 u e9 k - >>> math.gcd(40,20)
1 @- t2 c& `# k0 \; u! t( `4 m, ] - 20
6 F7 o2 D5 o2 z( s/ U - >>> math.gcd(8,12)- O9 F6 j7 u! I! w8 ~( R
- 4
复制代码
' |6 G, z+ w* B! d: @math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False9 J" [8 T: Z) |% x% q# h8 G
- #得到(x**2+y**2),平方的值; n- I; i& e& _
- hypot(x, y)7 c3 K% t5 W$ A3 B
- Return the Euclidean distance, sqrt(x*x + y*y).
. {; o% \ {9 t) b1 s: ?) P4 g: I, ^ - >>> math.hypot(3,4)
( N3 _/ y4 X9 h7 U2 ?. y - 5.0
: c6 j9 k: y( k; z5 j$ t0 ^! K* b - >>> math.hypot(6,8)! e* z+ J0 ?' a4 k% m7 Y! j
- 10.0
复制代码
' j4 f( M& j$ l) V: ]+ fmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False; K* U* p% ~$ D6 W: J) M
- #如果x是不是无穷大的数字,则返回True,否则返回False% M+ ?. l) D4 A, P5 @1 G6 H+ c3 C
- isfinite(x) -> bool4 |0 }+ F& k- \; G: r+ Y. W
- Return True if x is neither an infinity nor a NaN, and False otherwise.
6 Z R3 G1 } c! b6 i4 `1 h0 Y - >>> math.isfinite(100)# Y+ M, J4 w+ k+ \
- True
& Z: {2 F6 f- Z* i - >>> math.isfinite(0)! L" B/ b, e5 W# X- [; h' B% ?
- True
) M; a5 `9 a/ Z, M( V: U - >>> math.isfinite(0.1)+ T2 Z) u+ L4 K' y' K( C
- True; s1 M l' K* e q. d
- >>> math.isfinite("a")
1 t r% o& u, D% v! V+ {: Z - >>> math.isfinite(0.0001)7 L* ^! {3 E0 p8 r
- True
复制代码 # i/ _5 y8 e2 }# J5 w1 u8 t
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False/ I! |0 _8 p- v: U- T
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
' ]" B9 N9 i$ ^1 u# n9 }% i - isinf(x) -> bool' ]- s3 X8 T2 C) p
- Return True if x is a positive or negative infinity, and False otherwise., U' H7 P* C# A" Y3 |; `' P
- >>> math.isinf(234)3 j' F. X$ n A- P/ A) B' }/ g
- False, _1 A4 c4 p( g- ~& S: j/ k
- >>> math.isinf(0.1)3 v2 D/ J* L H( i, ~5 v* m1 L) X
- False
复制代码
" a- c( N; e- s# Mmath.isnan(x) 如果x不是数字True,否则返回False3 x, v& h _- P
- #如果x不是数字True,否则返回False6 ]1 G7 L" N: R2 [( [- J5 m
- isnan(x) -> bool
4 N" M) [6 \3 c" m G$ |+ P - Return True if x is a NaN (not a number), and False otherwise.
6 Y& _6 \7 n% G* ]) H - >>> math.isnan(23)! m: W; B% a0 U" k" \
- False* P9 L7 }9 F9 F) h
- >>> math.isnan(0.01)
. T$ c. j3 x, ~/ w2 H - False
复制代码
0 n& o5 J" s* hmath.ldexp(x,i) 返回x*(2**i)的值
" F1 ~7 T) y$ h- #返回x*(2**i)的值9 N# W1 B% Z7 I. l; Y# {1 K6 A9 p2 b
- ldexp(x, i)7 h( f e7 J0 O2 F, A6 @
- Return x * (2**i).
8 k% k3 i: O. A9 J- B Z - >>> math.ldexp(5,5)
3 J( u9 d X! e0 G - 160.0" H8 z1 ~; o. u3 p
- >>> math.ldexp(3,5): o9 j" B- D- A$ t8 H/ ~* y) b/ O
- 96.0
复制代码
) B7 }7 R) a( r1 H# }1 ?math.log10(x) 返回x的以10为底的对数
" A/ o7 z3 z) `# J- #返回x的以10为底的对数
. V+ ?( A! t% b9 Q& ?4 Q, @) x - log10(x)
, s, x+ s( \ l/ A# W - Return the base 10 logarithm of x.
* M' U1 x: {: _7 W7 n. D. p7 w) q7 Z& V - >>> math.log10(10)4 d/ t, z! i6 ~* P2 H* F2 @1 q
- 1.0
( b) E$ b" ?# m; S5 P( j - >>> math.log10(100)( H) ~) K& M* r+ ~) }6 F
- 2.0
- N' J) y5 e5 o; c; w' v' @ - #即10的1.3次方的结果为20
2 ^6 r7 q6 F, d. j9 L - >>> math.log10(20)) ^& U$ B, p) w! }% o' ^3 @ M0 ?
- 1.3010299956639813
复制代码 , S' G0 w6 E6 B0 {& Q$ i. F1 I
math.log1p(x) 返回x+1的自然对数(基数为e)的值3 b6 \+ \3 s n" g# N1 F, U
- #返回x+1的自然对数(基数为e)的值
% S% p& S0 n# t - log1p(x)+ N' w) X. E3 a9 J4 z! h4 f6 P
- Return the natural logarithm of 1+x (base e).
( ]% x& [, K: @, t- U; j0 Y. A, e. E - The result is computed in a way which is accurate for x near zero.$ b" I( G( |7 u; ]( n
- >>> math.log(10)
9 s: }" ? [8 `! `+ M1 | - 2.302585092994046
8 I/ Q9 }1 S, A2 C$ c7 x - >>> math.log1p(10)
$ w! C' H/ e" Q. v - 2.3978952727983707- r# @# k. ~" Q+ E3 D
- >>> math.log(11)
9 l' P: O+ i; _0 n1 _ - 2.3978952727983707
复制代码 . g: [; o9 ]. c+ i
math.log2(x) 返回x的基2对数
9 z5 z# B! g+ V2 M |2 u- #返回x的基2对数, D. ^, ]- S' G' h1 |# K
- log2(x)
( m- j; X7 j) W' L8 x, x - Return the base 2 logarithm of x.
, J4 V3 E" ]8 f Y8 V' |) \ - >>> math.log2(32)! D" j" k% I: h W5 }( J
- 5.0
, o1 K O# Z* X) y* c# K! k - >>> math.log2(20)
: r, I- C% r$ p - 4.321928094887363+ k& d( P$ M! g; S3 d
- >>> math.log2(16)
( {( O) E( o5 `$ K, d+ m - 4.0
复制代码
6 N9 F/ ?0 m& ?, R) s" Qmath.modf(x) 返回由x的小数部分和整数部分组成的元组2 I- i, T- M1 y% C# Q
- #返回由x的小数部分和整数部分组成的元组
; Q$ X3 X) F! Z7 M% ~5 S! U - modf(x)
; s& |8 e" v" | - Return the fractional and integer parts of x. Both results carry the sign7 d4 W9 _% _& M" W8 ]9 t
- of x and are floats. P5 D' i" |5 { Q6 }
- >>> math.modf(math.pi)4 U F( b2 V3 G% b+ f# m
- (0.14159265358979312, 3.0); F) |5 f. M* I6 K1 u; N0 j
- >>> math.modf(12.34)
* S3 Y7 ~- G9 Y' h/ ~3 F - (0.33999999999999986, 12.0)
复制代码 ; V7 T& v) F' e. J$ L8 R
math.sqrt(x) 求x的平方根
7 g. L3 Y4 R6 P3 D; `7 B8 G2 v. B- #求x的平方根7 @; S( q/ u# ^
- sqrt(x)
" {* N2 P6 j9 s5 l( o - Return the square root of x.
. `" l9 C3 ?$ t8 X( j% z+ v! z, F( | - >>> math.sqrt(100)
$ T$ K3 ~9 D" U0 ]; n5 A - 10.0& A: h9 u6 j+ J1 ?( I
- >>> math.sqrt(16)1 h6 g) `: U6 ~! N! ]" J
- 4.0/ F3 O" \3 \- f% t7 T
- >>> math.sqrt(20)6 [$ f% [9 J2 d4 G5 V: h
- 4.47213595499958
复制代码
9 T% h. |; _0 [7 Nmath.trunc(x) 返回x的整数部分- #返回x的整数部分
8 w1 U8 P U' D7 {% J, H! ? - trunc(x:Real) -> Integral& l M- P+ E. R8 p f @" v) f
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
5 n/ W' _& I; J - >>> math.trunc(6.789)
* T5 K e2 p2 D. B1 C - 64 d% i7 V4 ]5 S3 f% T4 k
- >>> math.trunc(math.pi)2 C, U6 k$ _; F2 B8 ], N; H, G
- 3
/ M. D) |5 m6 o4 F7 ^ - >>> math.trunc(2.567)
0 |8 D v: Q% ` - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|