马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
, u& ^3 ?; Y/ n ]2 g* O. e' U1 L2 g【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% v3 e+ i+ R5 S: r# m# d% O3 i- N
5 |+ l; t# p, f( q% X方法1:- h, U; U3 U3 O
- >>> import math, w( y2 Q: y1 z, o0 q- F% `
- >>> math.sqrt(9)) f. h3 H: l! q* K/ u( j
- 3.0
复制代码 方法2:
j. Q Q' f. W! |* x6 o0 _1 a- >>> from math import sqrt% Y, H* Z8 V; c
- >>> sqrt(9); P3 ^' @3 B0 }4 P K, P
- 3.0
复制代码 ! m8 F/ |2 ?. c* r$ m7 j! U. ~
5 C$ f2 U$ d, ]math.e 表示一个常量
/ Q+ x: X" @! k7 v2 P3 ^) j- #表示一个常量7 m% t- l. o0 K& ?" v7 l
- >>> math.e x# Q" o" |! ]& i8 m3 o+ W
- 2.718281828459045
复制代码 3 m2 C J. [# {3 y
math.pi 数字常量,圆周率
' U$ j5 b" q6 ]. Q" z: x+ v4 z- #数字常量,圆周率" U8 ^ e( y! Z/ _/ N
- >>> print(math.pi)& D$ Y* }: t% h7 D8 K* ?5 N% h$ r
- 3.141592653589793
复制代码 $ p8 U8 N' D4 Y0 B7 h
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x- A/ G2 X5 E' a h' U, t
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x$ r0 U. C& w! R. q! |
- ceil(x)6 f/ ~3 s3 R; m. N0 s2 b
- Return the ceiling of x as an int.
* B* ?$ I7 a5 `% j2 Q - This is the smallest integral value >= x.9 E7 B. [+ q: Y2 h9 n, w( n
- ) H0 J! \5 {9 S- ^9 b
- >>> math.ceil(4.01)2 Y' t; ^4 |; M: V: v4 L7 r
- 5, \& N9 @2 [5 E5 d" [
- >>> math.ceil(4.99)% H4 _. P% F* c; [0 s7 ^
- 5# o" d0 x- a. S4 C! K9 a5 t; c
- >>> math.ceil(-3.99)
5 `9 Y g3 e9 V Q! l' i - -3# Z% O# t0 ]) @/ Y# R
- >>> math.ceil(-3.01): t4 Q; Z2 C. h9 k
- -3
复制代码 # n2 H( m2 m( ]3 V. |. P
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 Z9 |/ t2 X; Y% S. {( i/ t% y- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% z/ f9 U( c) E5 [+ Z6 B7 b - floor(x)
/ X; C; P% L4 |. v6 @+ J - Return the floor of x as an int.
/ ~1 W8 r5 w! r: Z; g2 j" l - This is the largest integral value <= x.
: E, H7 c% w' y! p, m4 i8 ^2 d - >>> math.floor(4.1)2 f% h4 r5 Y2 W0 a. }
- 4
& U: C h& w6 v - >>> math.floor(4.999)
( m0 j2 S5 f; `* g% g/ U - 4
! o W% ?0 s4 a/ E0 P - >>> math.floor(-4.999) S' E- |) l4 D8 o7 t' b: p, V
- -5
: k" G) t0 ~ L6 g* _ m0 L - >>> math.floor(-4.01)+ V* d& Y, e O. j
- -5
复制代码
! v6 O1 y6 B- H- Z+ X, r8 }math.pow(x,y) 返回x的y次方,即x**y
8 L5 j9 J( f8 c4 y- g. E- #返回x的y次方,即x**y
; y+ N6 x- K, s, D+ M - pow(x, y)7 v% Q: D( D6 x3 L4 Z
- Return x**y (x to the power of y).7 O9 M. ], k* g
- >>> math.pow(3,4)4 P$ W" |" W! v8 `* P' M- d
- 81.0
0 C; y) m. q: p% Q5 Z5 E - >>>
0 J) S+ C8 j2 ~" M$ ~1 e4 n% }& q1 r2 b - >>> math.pow(2,7)1 |$ b! b5 q+ C8 F3 ^- O7 r/ Y! i
- 128.0
复制代码 ; G: W+ e* @5 q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 L: d* \ {1 [# {3 k* o8 v) }
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 R# b6 t1 ]$ A - log(x[, base])
: C9 h+ G9 k$ d& Q* D; R' e: C - Return the logarithm of x to the given base.
4 z7 [9 N- E& [4 ` - If the base not specified, returns the natural logarithm (base e) of x.1 ^1 a4 c4 K$ [3 ~
- >>> math.log(10) U' C/ }4 O' C; n
- 2.3025850929940466 |6 t3 O; A: I! [
- >>> math.log(11)
1 g% S8 {7 }5 q1 T- d4 ^# ` - 2.3978952727983707
& d) N4 C5 |" D* a3 u - >>> math.log(20)
' w$ Q6 L) v( W" b - 2.995732273553991
复制代码
- G* g4 J5 w0 }. m6 Rmath.sin(x) 求x(x为弧度)的正弦值2 B: f- Z5 Y3 `/ \& O8 s6 R
- #求x(x为弧度)的正弦值 \' d& K, ^ X1 P+ d" h
- sin(x)9 y8 C; }6 a N: t: c. o3 _* |
- Return the sine of x (measured in radians).
0 p c( U; [, J( ` i) I - >>> math.sin(math.pi/4)
- t# B) V5 `6 w - 0.7071067811865475
& a+ Z; O T" J3 d/ p% ^* F - >>> math.sin(math.pi/2)" Q. f( w& @4 p( d
- 1.05 [$ I/ G- K6 B0 E
- >>> math.sin(math.pi/3)& n. h6 U& ^* G. g- _
- 0.8660254037844386
复制代码
" K+ j+ x4 D% U/ h, ?/ y" Amath.cos(x) 求x的余弦,x必须是弧度
& P+ B) Z/ }3 Q/ o; W8 d8 X* n8 Q( G- #求x的余弦,x必须是弧度/ f% n! q, B, P6 `8 C- Z- v# }* ^7 Q# g
- cos(x)
; o7 X/ S7 _/ c1 X9 ~6 { - Return the cosine of x (measured in radians).. a# @& b- j$ D/ O
- #math.pi/4表示弧度,转换成角度为45度
1 E: }5 y l3 n' z - >>> math.cos(math.pi/4)' q! }; z% J: Q* d$ _* K& O+ O+ J
- 0.7071067811865476
2 g- i8 Z' h% v& ?# k - math.pi/3表示弧度,转换成角度为60度
* y* o. Q' R0 y - >>> math.cos(math.pi/3)
4 \/ X& s5 ` r. \7 E - 0.5000000000000001
+ l" ]+ z, F5 h - math.pi/6表示弧度,转换成角度为30度: G/ Z! R* [. e/ d: A- `
- >>> math.cos(math.pi/6): |* G$ Z7 g8 J0 _' h* Y w: L
- 0.8660254037844387
复制代码
+ N: }& {- e6 t! l4 |+ ymath.tan(x) 返回x(x为弧度)的正切值
* {9 G! B3 s o8 m1 ?- O- #返回x(x为弧度)的正切值- o, ~: f7 C" k) U
- tan(x)0 x3 o1 n: [$ x) {* n q
- Return the tangent of x (measured in radians).+ y: h s/ E W1 |2 F
- >>> math.tan(math.pi/4)
2 a m6 F. E3 C2 J% X - 0.9999999999999999( F. S, n9 P7 P& f6 i
- >>> math.tan(math.pi/6)+ [3 f0 H$ {0 W: ^2 G A
- 0.5773502691896257
+ g7 N; p" m ?6 C/ I( y+ A, q - >>> math.tan(math.pi/3)
9 t; L0 l0 b, V% E - 1.7320508075688767
复制代码 ( `6 A* K7 w3 ]2 b' c
math.degrees(x) 把x从弧度转换成角度4 N4 R8 Z* E+ x( e
- #把x从弧度转换成角度. t9 J3 D- B& `# f7 W9 N* C( O4 P
- degrees(x)
. t7 W3 |4 N2 S: m# X - Convert angle x from radians to degrees.* z6 q. r8 ]( y# A
- ?* a J' Q( b1 y7 W
- >>> math.degrees(math.pi/4)
- n' G5 k1 ]) Z" | - 45.0
7 [9 v# B9 W; g; a1 `) X2 x - >>> math.degrees(math.pi)
; `. P7 T/ d/ |& u) N# J7 q1 T - 180.0 d% o( a; |, S. q% j N
- >>> math.degrees(math.pi/6); } f. W: e5 }
- 29.999999999999996
2 S) @" m4 i7 G& U - >>> math.degrees(math.pi/3)
% v! M' [) ~6 K - 59.99999999999999
复制代码
5 b- W! k* |: o# @math.radians(x) 把角度x转换成弧度
4 b$ l$ d+ j5 J# U6 e1 u- #把角度x转换成弧度: f/ \& @0 {" M! }" e2 `0 t$ s
- radians(x)( ~* |* G' i. S# H7 a, T
- Convert angle x from degrees to radians.7 g8 t7 e0 r$ Y: J; `# t* ^5 \
- >>> math.radians(45)
1 _; u3 r2 v- H6 G" }4 B5 H - 0.7853981633974483 g+ g! j2 Y9 l; d& c8 N
- >>> math.radians(60)! B9 o" X: N7 e. U5 ^
- 1.0471975511965976
复制代码
" G/ u" T# W4 d% |: x( [3 Vmath.copysign(x,y) 把y的正负号加到x前面,可以使用03 p4 `' [6 ]1 C1 l
- #把y的正负号加到x前面,可以使用0' b* B6 ^) b5 ]$ C( _
- copysign(x, y)
% }1 Z! R) Q, L( i* ^ - Return a float with the magnitude (absolute value) of x but the sign
/ [ S& v! K3 c4 f7 j4 E - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
" x: ~/ C8 q/ W/ b - returns -1.0.
3 [! Y& `5 w% ]' P- t; P* D7 \+ H8 ^
' \1 ]" `- M" P1 q- >>> math.copysign(2,3)6 {' B2 y1 M* v8 T1 O
- 2.0- D5 Q4 X$ P) h- `$ o+ j
- >>> math.copysign(2,-3)$ v1 \2 @, i4 l
- -2.0- a0 T6 w( v! U! ^
- >>> math.copysign(3,8)
: ~( v; n. n# T$ i* { - 3.0
8 ]6 y" P5 i) L8 d8 F - >>> math.copysign(3,-8)1 [5 v! G2 h: E: k
- -3.0
复制代码
3 m/ t( v& u3 ~) A smath.exp(x) 返回math.e,也就是2.71828的x次方
0 w, K8 \5 F: j: F- V6 G- #返回math.e,也就是2.71828的x次方/ \' C; d8 [ @1 Y. Y! y
- exp(x)( `; g2 W( E L4 n. u$ G# l! I
- Return e raised to the power of x.! \! N! |+ j& @, ~* d
- 5 l# @/ x0 x# N
- >>> math.exp(1)! H" ~7 d; G' Q( @' l1 k6 x2 h9 ]
- 2.718281828459045
5 [& j8 v' } V5 P% a - >>> math.exp(2). T0 B' K) r+ u6 Y. ]
- 7.38905609893065/ l' q s' {0 n, O
- >>> math.exp(3)' q% X8 |$ L7 T# ~; p3 L
- 20.085536923187668
复制代码 8 ^% a5 j4 G; n1 V# @# e4 s; Y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
' `/ b3 d) I% v; h- #返回math.e的x(其值为2.71828)次方的值减16 l; o7 u% _. @7 J: X
- expm1(x)- t5 h6 _8 s- E1 J# y y- h
- Return exp(x)-1.& M9 f8 m6 M8 b) T
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 l; s- r Q+ `+ q) E
$ }3 C3 g8 d; q# o% \- >>> math.expm1(1)
: Y# }7 @& U1 i! g - 1.718281828459045
) k- ~# H% e G# g - >>> math.expm1(2)* Q2 X( J1 ?0 \* b2 j" d0 S* C
- 6.38905609893065
. j) k- c; O/ {: n) Q, V9 I - >>> math.expm1(3)
2 _: u o, c" }9 s7 W5 N - 19.085536923187668
复制代码 # |& K- A* B; c* H: ^8 U
math.fabs(x) 返回x的绝对值
3 q0 W& p4 |; K. `, X; h, D- #返回x的绝对值
1 b' m [, u, r3 H - fabs(x)7 A& f& e5 O0 x: w, Y7 O+ E
- Return the absolute value of the float x.8 \; t9 O0 u/ P, }/ N7 E9 x
- * O& }5 G F; `1 F
- >>> math.fabs(-0.003)
$ i/ Q) K. d: q/ D: j - 0.003; q* r1 r$ a. [9 w$ @
- >>> math.fabs(-110)
7 I, \( g' O7 ^) {$ d# d P - 110.0+ O L, L2 P# `& m; X0 h) ?# f7 }
- >>> math.fabs(100)
; O0 E4 e2 k" v6 J, x' K1 T- } - 100.0
复制代码
, F2 Z9 x/ P! ^" q& n7 fmath.factorial(x) 取x的阶乘的值
7 j5 a8 B. y- {+ A0 b% a- #取x的阶乘的值! M! J) N4 k) S, @. M7 f7 v
- factorial(x) -> Integral$ e7 h s! P, P- L
- Find x!. Raise a ValueError if x is negative or non-integral.5 a5 \& Y1 _) F& n! Y
- >>> math.factorial(1)
# v$ K3 G7 b. o c( ? - 1' \. I0 K& |. D) S) A
- >>> math.factorial(2)( i2 A5 ~6 @: k/ P
- 2
; ^* {8 w' K( P2 }$ Q7 m - >>> math.factorial(3)+ X: Y5 I! L2 r4 C* C
- 6; p* q7 o' [: n+ B1 i' _
- >>> math.factorial(5)* I. |5 y7 M! i( v: R( h' K
- 120" D; `- T1 U# f
- >>> math.factorial(10)- f; @# Q8 z, z/ ?8 _) } X8 V
- 3628800
复制代码 ) X2 ~: C8 i$ n5 ~; H
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
9 g! P- I$ {2 U, q" `- #得到x/y的余数,其值是一个浮点数- U, D+ J5 @6 `1 ?+ j
- fmod(x, y) `9 O9 H$ o1 F+ A
- Return fmod(x, y), according to platform C. x % y may differ.; ?( e# E2 c9 T2 }6 I0 ^
- >>> math.fmod(20,3)
7 y, d! i7 {" Q7 Y$ E0 F& v0 ?& K - 2.0
7 S" h4 k7 `) {/ P; a$ [5 h/ x& L - >>> math.fmod(20,7)
- m u( @+ ^: H6 r% y2 m3 e - 6.0
复制代码
4 ^# [& A7 L2 v5 C! Gmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
7 q, O2 r$ K$ a. Q5 u8 h5 `9 k- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 V3 c% M+ i* G" t4 G
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" Z% ~' e( i5 e K, s0 G
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
; x" k! W# i8 B- ?7 j - frexp(x)# R+ N- n7 ]; j3 o& _* y
- Return the mantissa and exponent of x, as pair (m, e).
! O' g/ m8 U \/ ~ - m is a float and e is an int, such that x = m * 2.**e.4 M$ h) U. a p. |+ \
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 r [# u- m$ i6 T3 W+ P% N8 V
- >>> math.frexp(10)- S. ]2 ~/ ~" @% \, }
- (0.625, 4)) u5 H. ]1 }1 d$ H3 ^0 s8 A
- >>> math.frexp(75)0 p% z0 B1 [( n+ q3 E" V
- (0.5859375, 7)% J% M% ~9 ^5 v: J
- >>> math.frexp(-40)5 m# x! i( Y$ k: R" }
- (-0.625, 6)
. Z4 R6 B9 y3 M3 e1 [7 q - >>> math.frexp(-100)
( Q4 Y+ R6 t& W9 |7 \ - (-0.78125, 7)
8 S% d$ L" D l7 `3 e! [$ a* y - >>> math.frexp(100) E1 P3 V" o$ c! E+ e) v+ C. v
- (0.78125, 7)
复制代码
, R* F: q) ?- I8 k1 Emath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)8 U0 X% F" U7 o7 w; I/ E) R5 h3 x
- #对迭代器里的每个元素进行求和操作
' j! B' \: s! K( @. T8 X+ A$ N - fsum(iterable)
( }! H& l3 e9 h1 l - Return an accurate floating point sum of values in the iterable.
u6 p) [8 {9 D7 O" g' X( U# s - Assumes IEEE-754 floating point arithmetic.
5 M _( Q$ m7 K; H6 c - >>> math.fsum([1,2,3,4]), l8 w# u. c2 d$ Z9 n) j L
- 10.0
) h' y. F p9 |: Z* G - >>> math.fsum((1,2,3,4))' Q" f3 f* q- ]* x
- 10.02 M- N+ a4 a* q P
- >>> math.fsum((-1,-2,-3,-4)) {" P3 C& r$ x8 k! B% m
- -10.0
3 N' A6 h1 s$ n# z2 P - >>> math.fsum([-1,-2,-3,-4])
2 t: ~8 u% l7 I+ O9 E - -10.0
复制代码 6 L0 J3 B& T9 u1 p K/ p h1 ?
math.gcd(x,y) 返回x和y的最大公约数% R. J0 x# X% f: [$ [; z
- #返回x和y的最大公约数& p+ ?! h3 a* _- o
- gcd(x, y) -> int
+ E% d A/ f1 {; R" @# B4 | - greatest common divisor of x and y" H/ ^! v+ `4 m7 X: ?8 X0 @# \
- >>> math.gcd(8,6): O$ P# ^0 V/ S- S; ]
- 2% x3 V ^2 {# u
- >>> math.gcd(40,20)& s5 H0 {8 b0 ?
- 209 Z# B' J6 x4 s! J3 h
- >>> math.gcd(8,12)' @4 b* s: [% `7 m' w0 P
- 4
复制代码
- Z& X4 N+ y% A/ m% ?math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
) p% o, v# X3 |% ~# Q5 s- #得到(x**2+y**2),平方的值$ j5 N2 z, ?$ j. ]3 n9 P3 {' b1 M
- hypot(x, y)
7 i. L o9 I, M. Z3 A9 h+ x F - Return the Euclidean distance, sqrt(x*x + y*y).7 e; t4 w4 z: R& z8 N5 ~
- >>> math.hypot(3,4)
! M7 I- X& X* `. s1 h - 5.0: l7 Y. x; V" y$ X0 @, A- o& u
- >>> math.hypot(6,8) x9 G5 B3 n6 R x
- 10.0
复制代码 . p: H* ~+ N6 \
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False# V) L) G7 C: O: a3 z
- #如果x是不是无穷大的数字,则返回True,否则返回False
7 Z7 y5 h2 M9 s - isfinite(x) -> bool
) a; G* b# b- [ - Return True if x is neither an infinity nor a NaN, and False otherwise.
+ N% D Q% n# H# X) R - >>> math.isfinite(100)
w7 w; D9 }. {3 T$ t' \ - True8 w+ B8 ^( m( \ ^# \
- >>> math.isfinite(0). c& u; t( }5 V+ T- @5 q# _; x
- True' c- p! {9 S( O7 ]) {9 M# k
- >>> math.isfinite(0.1)5 p0 R# l8 r9 ~
- True* W* L0 H7 w7 O8 m2 e8 W) M7 b
- >>> math.isfinite("a")
2 I* @* x! Q; q3 @! ?3 b4 j1 n1 G0 Y - >>> math.isfinite(0.0001)2 {0 q _7 i) ?8 W9 }& j+ q- J
- True
复制代码
. K1 r8 C t v4 s2 Bmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False5 c" W4 T* \4 ?7 ^6 x0 W: {. w
- #如果x是正无穷大或负无穷大,则返回True,否则返回False1 M3 G& R2 X4 Q, w9 ~" {
- isinf(x) -> bool
% a; q: G0 _7 N. ]9 O - Return True if x is a positive or negative infinity, and False otherwise.# H3 x l9 w2 E
- >>> math.isinf(234)
# J0 `2 [ d; r) k" N9 ]" k: A - False3 \' a0 L, a3 _- I/ U& H, v* \
- >>> math.isinf(0.1)
4 N% U! W8 g+ i: Y - False
复制代码
1 I+ I ?7 M7 I- h7 k- }( amath.isnan(x) 如果x不是数字True,否则返回False" C; L, Z( m& ?3 Q, H
- #如果x不是数字True,否则返回False; @7 O0 E# }5 M* J/ m. |, d& x, j
- isnan(x) -> bool1 B/ r6 A8 N+ P A$ ~
- Return True if x is a NaN (not a number), and False otherwise.
& R* I S+ d( q3 ~ - >>> math.isnan(23)
( W& i; B% g, `+ U7 ~4 F - False$ J; i2 o. g# h
- >>> math.isnan(0.01)
1 j% r! C$ [# b7 d( f/ ]- ]) B* _ - False
复制代码
! N1 b. [9 ?" i) X2 W: }; nmath.ldexp(x,i) 返回x*(2**i)的值
& D! R0 \ S% \. N; ~- #返回x*(2**i)的值0 L" @- V. q( X+ Q# ~
- ldexp(x, i)7 b: K' b& v2 U1 _% U/ V$ V
- Return x * (2**i).
' ?' G6 e: V- ~* j( n. U% C; J s - >>> math.ldexp(5,5)
! ?! s9 e; B% E# L" M - 160.0
# a( d7 n/ b* T$ K, D+ n - >>> math.ldexp(3,5)
3 q/ \( ~/ w$ A# t0 T$ ?- r# f8 ] - 96.0
复制代码 ! c& c/ O6 ?1 Z! h4 M
math.log10(x) 返回x的以10为底的对数
3 n8 J! K& |+ I$ W# r- #返回x的以10为底的对数# O3 f9 b8 o2 V9 m5 O1 F( |
- log10(x)! T+ x! d! v: @( y7 D# k$ X
- Return the base 10 logarithm of x.
& I. A1 V3 }) h+ l - >>> math.log10(10)
5 ?% O; S3 D s. C7 |+ p7 i: @ - 1.00 a; ^- }8 H" q* P3 D2 X) Z2 w
- >>> math.log10(100)
) C* H/ V1 g) @- E6 R4 q; Q" m - 2.0; o2 q% K. v6 _- U
- #即10的1.3次方的结果为20
: r V+ ]. N/ |" O/ C - >>> math.log10(20)
' j7 \7 A; I4 M3 c6 I4 Q; y8 C - 1.3010299956639813
复制代码
% Z4 c/ l& a& m0 O `( b3 gmath.log1p(x) 返回x+1的自然对数(基数为e)的值 u7 E R. \+ m$ e0 D' c" P
- #返回x+1的自然对数(基数为e)的值
# b: {/ x' K/ M! q' f - log1p(x)0 t8 D# B+ C/ z# l. z6 N
- Return the natural logarithm of 1+x (base e).' L$ G0 C/ W! J. j5 J- H3 `0 G
- The result is computed in a way which is accurate for x near zero.
* c$ ?8 A* c R: T2 g1 |- O - >>> math.log(10)7 q; A' ^ R I: ` Z/ y! ]: Q
- 2.302585092994046
: q! d. B2 y# S( j8 E2 | - >>> math.log1p(10)# U c. V! J: P/ _
- 2.39789527279837071 C; y! I& ?+ B( X2 G$ x
- >>> math.log(11)
& }! G) C8 W6 o* F8 f. e - 2.3978952727983707
复制代码
$ P( J( V( ^3 y" p( Q- ~: w) F0 Amath.log2(x) 返回x的基2对数% ?5 O, |+ h% k1 I
- #返回x的基2对数; O5 x# i$ s1 R
- log2(x)% ]/ T i1 y3 L) @
- Return the base 2 logarithm of x.
% M6 W4 k" c5 p! W1 N+ j4 A$ M* A/ s - >>> math.log2(32)
- a- g y) y5 q - 5.09 q: D* U+ n" h) a
- >>> math.log2(20)" S, w8 w! j3 W7 n
- 4.321928094887363
' L* S8 _# i+ q - >>> math.log2(16). x6 Q3 P0 @1 p& z% O
- 4.0
复制代码
, l! _$ ?, D% W1 wmath.modf(x) 返回由x的小数部分和整数部分组成的元组; a- U/ ~, C! Z( X# X
- #返回由x的小数部分和整数部分组成的元组
6 h; G) v; U+ O6 @& w9 r - modf(x)
?: s6 y3 U* F6 K- w; X$ ~. N - Return the fractional and integer parts of x. Both results carry the sign
! \) a8 ]$ r; D. p( D - of x and are floats.8 ]$ `( @# B A' h/ E" E% e
- >>> math.modf(math.pi)
* l& t4 L! A3 E* T) q6 G# y, ~ - (0.14159265358979312, 3.0)9 c0 [+ y3 q3 S- d
- >>> math.modf(12.34)
$ f: q- G/ e8 `" X6 P* n - (0.33999999999999986, 12.0)
复制代码 ( P8 X x* I' V4 }
math.sqrt(x) 求x的平方根" ~( r) K# \0 H4 w
- #求x的平方根, u0 U4 f* `- r$ j/ G; d
- sqrt(x)- @3 o) j8 _ Q2 s5 K [! N/ r$ A
- Return the square root of x.( Y7 Z$ z' ]& K% J
- >>> math.sqrt(100)/ F1 J- W* t- D0 X+ Q% Y
- 10.0 E; {/ n& s# ]* I5 c
- >>> math.sqrt(16)
9 ?% ]7 o& L( ?% }# ?2 F; U - 4.0
; H- \. [4 E* m - >>> math.sqrt(20)
0 L. l" a9 g- W/ K - 4.47213595499958
复制代码
& K7 d6 H7 L( s- Emath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 h4 H4 e: G" b. w% Z - trunc(x:Real) -> Integral ~/ ^( V- e4 E- x; L8 d2 I1 |
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
4 u$ s/ p* q* b, |* x( } - >>> math.trunc(6.789)' n4 b! A& ?. b( S. y" h
- 6
' v1 T" r7 K* D+ Y* s3 t - >>> math.trunc(math.pi)
' n$ F8 [& V6 P! K8 G - 32 v% ^' O0 O7 m+ O' f+ ^- W
- >>> math.trunc(2.567)8 s+ w8 ]& Q4 r/ Z
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|