马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 H# S) G5 d- u0 T4 k5 v【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 U* I* Z2 o' G/ J
9 l0 i& n6 ^5 C( W% a方法1:
% ?* f3 ~+ }( P/ V2 E1 u- >>> import math
! C. r ?" e) c0 g7 z. P/ a; m% e9 c - >>> math.sqrt(9); n" F. i* W9 x
- 3.0
复制代码 方法2:4 F8 `: e. F: s
- >>> from math import sqrt
9 @6 S) ?! f8 }) C' `$ P - >>> sqrt(9)
! G8 L5 @. R2 v$ d/ s$ ] - 3.0
复制代码 % `* Y1 [& `' u3 f0 t$ c" f L
9 ^6 ]; J7 {/ K+ \+ }+ N. w1 ]
math.e 表示一个常量) Q" x# p9 k; |: z
- #表示一个常量
3 }4 ~! B; e8 B3 u6 O! X; j+ ~ - >>> math.e
+ S- T+ H, }# ]1 [5 k! k" k- h1 x7 R) \ - 2.718281828459045
复制代码 {$ Z; p+ ~! Y9 E' b' a* ~
math.pi 数字常量,圆周率4 i6 g: a: F; L4 i5 {0 t8 h8 {; C$ U
- #数字常量,圆周率
9 w% m* x9 a: O" z% b - >>> print(math.pi)& `: L4 s; A5 A/ C2 ~% [
- 3.141592653589793
复制代码
, M- R4 i; X/ Q' S) Gmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
. J* Y. H( g6 k2 W' X% a9 C0 s& B) \- #取大于等于x的最小的整数值,如果x是一个整数,则返回x" ^7 z1 `" \( E1 s8 ^3 a; x
- ceil(x)2 R3 K: @$ Y6 X& ^
- Return the ceiling of x as an int.
8 Q# N( Q5 o9 E% E) F, ^* C - This is the smallest integral value >= x." i! t0 ~1 E: w( |1 L% O
- 4 {0 `; K' @+ P! z" ?+ C
- >>> math.ceil(4.01), i5 E2 J3 y" @: v9 W8 C [
- 5
1 u# ]# o9 C: B z - >>> math.ceil(4.99)% F0 e4 C% r% ^$ m
- 5, S3 N1 r9 O& A" s/ x8 Q V& g
- >>> math.ceil(-3.99)2 {# r1 V2 E$ E* `. s
- -3/ Z9 T5 w9 `: ?3 o& K9 Q/ k
- >>> math.ceil(-3.01)
Q9 w) u3 G4 k3 d9 ^ - -3
复制代码 0 b% z9 c. m0 F( a
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
- M. N$ L2 [" U6 k- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 t" R% o3 T; K. R - floor(x)4 v$ o( W7 e/ y. [" z/ ^; ]
- Return the floor of x as an int.
# }$ x5 S9 K, Q - This is the largest integral value <= x.
: C2 E# O2 C- a9 l5 j9 h - >>> math.floor(4.1)' J0 o% K; c: g3 j( R) M
- 4 }; }& y) ^- q. U; a- g/ ~
- >>> math.floor(4.999)
) C2 o; B8 v, P! F: e* H - 4
2 L& I7 [6 g8 c2 x6 B' O* A+ i - >>> math.floor(-4.999). f2 a+ a; H" o y
- -5 z* l& z. Z" o# H, l
- >>> math.floor(-4.01)
: d% p5 \* N1 J- ]: u$ m - -5
复制代码
1 R: l7 Q4 K( Q: j0 g* `5 pmath.pow(x,y) 返回x的y次方,即x**y8 `$ z. G+ Q" ~
- #返回x的y次方,即x**y
: X0 A9 J/ r% T) }- e0 f; C - pow(x, y)! _ S- D4 V, |2 d" Z
- Return x**y (x to the power of y).
# m- Z8 n9 Z4 W - >>> math.pow(3,4)0 C+ e$ B% q6 N% t9 I2 S# L8 J/ _
- 81.0
( s1 H3 c% }, I# J8 i( b - >>> . K' q) [6 k& u8 M. L
- >>> math.pow(2,7) B- h" n! Q* }0 O. f
- 128.0
复制代码
6 T8 y! T: j/ _' k% ?5 mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 V7 Q% J& b7 n, C6 j, {$ ~8 b# U- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- Z8 m: A/ P( s
- log(x[, base])( Z- m! ^0 w! L$ ?
- Return the logarithm of x to the given base.
1 f2 M! h8 K1 u& d* L - If the base not specified, returns the natural logarithm (base e) of x.
$ r; Z0 W; T9 {! H$ c - >>> math.log(10)& t9 D5 D3 Q- a' p2 @0 J
- 2.3025850929940461 T! i+ H: o3 N0 s- ^# O
- >>> math.log(11)7 a. l3 l# f4 Q. l, R
- 2.3978952727983707
' v: @" H/ A& o- p - >>> math.log(20)
8 M$ I. n o h. J/ J9 M* e - 2.995732273553991
复制代码
+ n* d9 I" _4 }4 z0 L Dmath.sin(x) 求x(x为弧度)的正弦值
6 b4 e, k q' a- E( Y- #求x(x为弧度)的正弦值8 q: m4 ?- P( p; \: L9 z9 R
- sin(x)
6 T: r# n0 b4 \" S - Return the sine of x (measured in radians).! C: [$ L, v1 n+ C( C w/ o* y. n
- >>> math.sin(math.pi/4) E/ @6 M- z7 S4 A& g3 Z
- 0.7071067811865475, S' a/ y5 {) m8 z
- >>> math.sin(math.pi/2)
9 Z* m) I" _, | - 1.0
& ?4 A8 Z$ k$ F" l* C: G; X2 ` - >>> math.sin(math.pi/3)# T: ]/ {5 |- y5 N$ u! e
- 0.8660254037844386
复制代码
# ? L+ `' w0 t! P% w9 ]: ?- y5 L Umath.cos(x) 求x的余弦,x必须是弧度
0 t, `! w' d+ m4 A. S! `7 f- #求x的余弦,x必须是弧度 s) H# I, C1 w6 s
- cos(x)" ~: t7 @) X* U$ _. ?
- Return the cosine of x (measured in radians).
6 ?& H) z6 B0 O# {, I; j! y - #math.pi/4表示弧度,转换成角度为45度
% F7 Y* ~, K" j9 ]4 ]- N" U - >>> math.cos(math.pi/4)
3 I. {9 S5 m" h9 H3 T+ {! I& _1 G - 0.7071067811865476
* e) F9 N. V3 I5 c$ V5 y0 e - math.pi/3表示弧度,转换成角度为60度
/ |+ K8 Q* E; r. a$ T% [( f - >>> math.cos(math.pi/3)
$ P9 {3 k7 H! L; Y( i - 0.5000000000000001% H7 C) H: d6 M* q o
- math.pi/6表示弧度,转换成角度为30度$ P! c, J: j8 @( I
- >>> math.cos(math.pi/6)
$ ?& P$ U2 _! J/ q M+ f - 0.8660254037844387
复制代码 6 Z8 R; ?) E8 E: U* J ^0 }
math.tan(x) 返回x(x为弧度)的正切值7 O. n i6 v+ H5 _' R) v! y' s
- #返回x(x为弧度)的正切值
4 d3 H z1 n* Q - tan(x)- J+ w7 a) q2 x0 ]
- Return the tangent of x (measured in radians).
2 E! z% W" m8 T$ e$ ~! m - >>> math.tan(math.pi/4)% n9 @: S; }, x5 I- {" e2 \
- 0.99999999999999997 E% H- Q/ B h) K. X, o
- >>> math.tan(math.pi/6): n4 G1 r2 Y, }( D
- 0.57735026918962573 _+ U; |, o; ~' W; H" d
- >>> math.tan(math.pi/3)
0 s( X6 a+ X3 G7 Q: Y- L - 1.7320508075688767
复制代码
2 ?* F+ w* Y5 n- x# M; N. ^math.degrees(x) 把x从弧度转换成角度+ H5 ^( j) b1 H
- #把x从弧度转换成角度
2 a, V0 J/ `/ ]+ m - degrees(x)
# F" a9 N1 P2 i: x7 J - Convert angle x from radians to degrees. m7 D7 p/ }* O: I, _% u% _+ ^4 [
! ^; \: Q: u2 R* J0 B% r9 q- >>> math.degrees(math.pi/4)# f; G% E' f. f$ x4 @
- 45.0/ A; K( r. X) u2 X% e) |
- >>> math.degrees(math.pi)
0 e/ @- L/ z- \ - 180.0
8 H" `+ Q, C6 F2 ^/ ` - >>> math.degrees(math.pi/6)
& ?7 \# X/ ^- b8 J" S1 ?) Y - 29.999999999999996
7 J; y) t e& u* B& k$ d* U - >>> math.degrees(math.pi/3)
: l& y! {+ I* P' o# _ - 59.99999999999999
复制代码 2 B" t. n( Q4 i; i2 \1 F
math.radians(x) 把角度x转换成弧度( e9 ]7 u8 U, @! j( j
- #把角度x转换成弧度
9 W8 w& Z1 N- D* ~ Y9 U - radians(x)
) {0 k7 m* I! N' i6 F. } - Convert angle x from degrees to radians.
1 `# U& g- I/ j4 k3 ^" ?) w - >>> math.radians(45)
& Y+ c" g# D/ b% Y: m# ^ - 0.7853981633974483; x0 w9 t! X* [( ~0 \7 G% z
- >>> math.radians(60)
) B' t0 t1 z. y5 S" ] - 1.0471975511965976
复制代码 : I( t$ y0 _# S4 r! V
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
# e7 T+ b' V' o( K/ r0 z8 P- #把y的正负号加到x前面,可以使用0
. P0 W0 S7 ~3 {0 z. p0 B4 y% S - copysign(x, y)
' Q; y, ]3 ]0 z* A - Return a float with the magnitude (absolute value) of x but the sign ; D0 g. z3 D/ `" y% g
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) h8 V5 ]8 w* w9 o
- returns -1.0.
9 {8 F' n; R6 W, \8 K" S4 p& H - ! x" @( B! s, @9 G$ @
- >>> math.copysign(2,3)
; g" o3 P- {8 _& I" N4 d - 2.0% S3 E% T: j- c) q; i
- >>> math.copysign(2,-3)$ Q3 e& i) V. _; a
- -2.0' {& ^- ], e# {7 @/ ]
- >>> math.copysign(3,8)
; P. l; W _$ O - 3.0; Y$ k' u0 w# T# N8 i- ?' h
- >>> math.copysign(3,-8)
4 q8 Z/ x3 c* k$ E - -3.0
复制代码
9 f9 Q7 ~; |( L" @! [, f2 o1 Pmath.exp(x) 返回math.e,也就是2.71828的x次方$ \( X- s) R; `+ @ _+ }1 \0 G. R
- #返回math.e,也就是2.71828的x次方) P; T- W2 V% L) }3 r! B
- exp(x)
; o3 o% }7 M3 ] - Return e raised to the power of x.
f5 B9 Z8 v5 \8 u8 |" q! z$ S - ; R* E2 f. d! D/ L: g% y: {/ d
- >>> math.exp(1)5 d1 p: F, Y3 A+ P
- 2.718281828459045
# p! F2 d" Q- s8 a - >>> math.exp(2)1 ?, x1 ?% u/ q* B+ C
- 7.38905609893065
9 a3 F- X4 a& x7 ^. I/ z7 [& U - >>> math.exp(3)7 g% r# L, Q. M9 F
- 20.085536923187668
复制代码
: v4 `# I% u3 @' Mmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
* g1 J7 i; c# ]: g& ^1 G- #返回math.e的x(其值为2.71828)次方的值减1
6 D. P/ a% ~8 B5 _9 ^6 G3 G6 d - expm1(x)& j: B$ G' T) v: v2 ~
- Return exp(x)-1.
# x# c0 z1 K3 e7 ~1 j# p - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.% d3 |( c I1 Y
- . U7 S) x2 r3 _- X0 |
- >>> math.expm1(1)
6 H3 H7 B( b9 N# }: t/ ]* q - 1.718281828459045' K9 d4 d6 N% Y( L) L% ^
- >>> math.expm1(2)7 x2 T& h% J0 j4 t8 s
- 6.389056098930657 j0 S0 ^5 w8 y6 L& k1 ?( i* b
- >>> math.expm1(3)
+ M0 i9 h1 \! X2 @; {3 {6 T* h& g - 19.085536923187668
复制代码
3 k$ T4 X2 F! C7 T& W1 G) cmath.fabs(x) 返回x的绝对值
/ o6 k9 n+ Z4 u/ p3 l- #返回x的绝对值: h' [$ q3 k2 u) L; R
- fabs(x)8 e& E. a6 d, B) I6 S4 {
- Return the absolute value of the float x.
7 R- \" C! `5 o5 N - ! }3 I; A: Y; c7 e% C3 T) K
- >>> math.fabs(-0.003)
: C/ ~! G2 J5 l; j! H$ E6 k/ f - 0.003
4 V( E* C! ]* k' b! y4 Z+ q - >>> math.fabs(-110)5 }3 K* D; W: [! w
- 110.0
' \. S; z( Z. I# q H- z5 x - >>> math.fabs(100)' }" q( y# G8 O
- 100.0
复制代码
4 z/ ]4 e8 b' `- L( \5 Pmath.factorial(x) 取x的阶乘的值
1 ]- S) H( n9 {: T5 _- X3 a- #取x的阶乘的值
U9 X. P+ ~* H; j% M! R - factorial(x) -> Integral& v& r/ u- y3 w
- Find x!. Raise a ValueError if x is negative or non-integral.
$ Z/ k0 ]" P: O - >>> math.factorial(1)4 W2 d6 j- L$ o2 r, B" A) f
- 1
0 Z; I# X$ E9 U0 _/ f5 b - >>> math.factorial(2)
5 v) v- ]% {# Y - 2$ a _: B! M S0 l' R
- >>> math.factorial(3)( i5 G8 H8 Y. j$ q4 b8 @5 W
- 6! g3 [: r- r4 Y, \9 n" v; A; F: Z
- >>> math.factorial(5)0 Q9 G9 \6 f7 t; V& U1 n
- 120
4 Z. v1 q, B s1 V0 h - >>> math.factorial(10), ^$ ~$ |& _( @
- 3628800
复制代码 9 b: R2 @' a5 M7 q5 B. b9 K7 E
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
4 H( y5 z$ p) Q Z2 c6 f5 }- #得到x/y的余数,其值是一个浮点数
, d* \& u8 G5 I - fmod(x, y)6 C$ Q* i* G, d1 u2 j1 @9 Q
- Return fmod(x, y), according to platform C. x % y may differ.
; C, c. M2 M( z$ e4 l L - >>> math.fmod(20,3)
( D+ @, `7 G% x g - 2.0- n r' N3 W- t+ b0 ?7 y. x
- >>> math.fmod(20,7)1 }) z. j0 r$ N3 J2 y+ ~
- 6.0
复制代码 * ?' e4 e6 r. |
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) ^1 `2 R0 p$ ~5 ?
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,0 E: m4 Z3 ~ X1 |4 I% ~: q! ^
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 Y U! M) B! X c# b/ }
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
1 L5 l/ F7 b, L- Y3 g4 ~, u, C - frexp(x)
/ w4 C- e6 @4 q, m; V7 v" _9 ~/ | - Return the mantissa and exponent of x, as pair (m, e).% w6 w; f' }9 J- F" M9 Z; r X
- m is a float and e is an int, such that x = m * 2.**e.8 U* B" N+ N" M4 ?
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
# r* l6 z$ p# U8 b1 `! o# w! T/ l - >>> math.frexp(10)8 x7 E. t4 m; E4 f& ~7 G9 B
- (0.625, 4), [( _# v: {# c
- >>> math.frexp(75)4 R/ M5 I1 L& Q; i. @& j W% r; q
- (0.5859375, 7)( y! w, H9 A; K V4 H+ T2 y
- >>> math.frexp(-40)9 m8 l" _% e( Y2 `
- (-0.625, 6)
) v2 `2 G; o- A. y. u - >>> math.frexp(-100)$ w& p4 B" M. ?; m* \7 v2 S
- (-0.78125, 7)
% l- _( P8 P. [% T! ]- z - >>> math.frexp(100)
' n! ]. k* G/ q5 @: y: V - (0.78125, 7)
复制代码
T2 n7 J7 k0 c/ n& Q* q, \$ q9 [math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)1 X4 D% G& I& i
- #对迭代器里的每个元素进行求和操作1 M# W7 W" j* B# x* Y
- fsum(iterable)* ?: @/ Y$ Q" \# L% z
- Return an accurate floating point sum of values in the iterable.4 h2 P; O$ q5 {" S) M
- Assumes IEEE-754 floating point arithmetic.4 a9 x1 p: u) J" n
- >>> math.fsum([1,2,3,4])
6 A, a1 T: ?) @' y! u - 10.0
) J8 {2 [: V( T- O" J - >>> math.fsum((1,2,3,4))
4 S. F4 f* c* l! P2 [ - 10.0( p9 B4 `9 h0 V7 ~' r
- >>> math.fsum((-1,-2,-3,-4)); c* R, q( Y: }% M( y, Z
- -10.03 R7 k; ?+ i V2 A
- >>> math.fsum([-1,-2,-3,-4])8 R+ L$ T: ]+ t+ l4 f% Q: H) ?2 w
- -10.0
复制代码
) q" k9 b3 U) M; jmath.gcd(x,y) 返回x和y的最大公约数
4 G- B7 h( S7 ?1 W- #返回x和y的最大公约数* y8 u( Q9 x! f6 m
- gcd(x, y) -> int# {& P( Q: T/ R$ J ~
- greatest common divisor of x and y9 a3 t5 C* H3 p
- >>> math.gcd(8,6)3 ~* R0 i) g9 ?
- 29 q, h8 E; {; i i6 ?+ q
- >>> math.gcd(40,20)
, ~) D6 l+ C' \7 s2 p! Q - 20
% u" F8 w* X W - >>> math.gcd(8,12)
3 R* _ [1 D& E/ u- k, x M - 4
复制代码 1 g! j, O5 C- E3 y! ^& [
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
, H" k$ s( Y7 ]( t( P8 D; q8 M- #得到(x**2+y**2),平方的值" p: u& B+ L; {) ^7 V) K
- hypot(x, y)2 ?3 A0 T1 D1 ^. a% S
- Return the Euclidean distance, sqrt(x*x + y*y).+ {2 @/ T2 \7 T
- >>> math.hypot(3,4)& v8 V5 a, _ b; y$ M
- 5.0# A$ q2 o) R& m6 f3 w! a
- >>> math.hypot(6,8) ^" X9 M, q$ i) F; j
- 10.0
复制代码
0 y; }$ B7 O$ @math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False, U9 |, r( C5 ?# k9 @
- #如果x是不是无穷大的数字,则返回True,否则返回False) e1 t3 p3 ~/ b, C+ l
- isfinite(x) -> bool
4 R+ T# s& f Z: W* P+ q - Return True if x is neither an infinity nor a NaN, and False otherwise.
+ _% r5 {* Z* t7 j; Z' o' V - >>> math.isfinite(100)! v" g$ p* ]: |! o: f: ^6 v+ w: y* C/ o
- True$ z1 k. s( c2 Q+ Y# S; ^, S
- >>> math.isfinite(0)" J& V; @. j( u, ^: U) f- a
- True
) _! G0 m, @, s/ t - >>> math.isfinite(0.1)
& C' D) W# U% r+ d' `4 A, t - True# ]3 v7 P$ ~& ~2 V% P
- >>> math.isfinite("a")% [* l" F g8 N- v+ s
- >>> math.isfinite(0.0001)! w: e. ~% W) |. q. `
- True
复制代码 : g! X/ h" ?/ c/ G4 L- G7 U& ?
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
9 @ Q3 w' y: |# ?0 I! h- #如果x是正无穷大或负无穷大,则返回True,否则返回False" n8 m D: L( E* ?2 n: S0 s
- isinf(x) -> bool
' J& U' B P5 M! ^$ v% z7 W5 Z - Return True if x is a positive or negative infinity, and False otherwise.1 y- \- K& \, P. \4 \
- >>> math.isinf(234)% D: l) V6 x5 D
- False0 \# Y/ e# f0 m' _( j
- >>> math.isinf(0.1)1 e: _3 @: ^7 n# `8 S
- False
复制代码
^ y. `' q- R2 D4 }math.isnan(x) 如果x不是数字True,否则返回False
) t. M* d: L2 T1 S( t) R- #如果x不是数字True,否则返回False8 }0 t( a: W; H+ P h
- isnan(x) -> bool
0 @3 l+ J& ?, e6 z) U - Return True if x is a NaN (not a number), and False otherwise.: s1 G% w9 c2 G g# m* u
- >>> math.isnan(23)
* s, }7 ?+ P- V+ T6 r: l5 u - False
# U4 @( [# [! i) d* S9 M- o - >>> math.isnan(0.01)
2 G- `3 L, i/ M! x' A8 k$ U - False
复制代码
H5 F& W2 R V4 z9 M- Z2 Lmath.ldexp(x,i) 返回x*(2**i)的值
' G2 E4 W; V# a$ V0 p6 U1 `- #返回x*(2**i)的值* \4 _7 n3 D8 C& q% E6 M
- ldexp(x, i)# {3 S1 _7 @$ g5 J2 j3 Y; Q% G
- Return x * (2**i).5 U) r" m! Q3 x5 o* }
- >>> math.ldexp(5,5)4 e! Z; k6 ^ u: {, O3 y
- 160.0) R# F% [/ C, l8 C4 C
- >>> math.ldexp(3,5)! Y( R$ c% u1 Z/ a4 c' q2 i: Y
- 96.0
复制代码 : {0 w: q3 i0 I `3 N1 u
math.log10(x) 返回x的以10为底的对数' R$ q) Z1 ^8 \
- #返回x的以10为底的对数; R: M& s) S& Z* Q" a) J2 Z
- log10(x)# S( p1 n; o! w! L7 ^# f x3 m' Q6 n
- Return the base 10 logarithm of x." t, e1 T% b6 }. Q0 u+ s
- >>> math.log10(10)7 ^ r& o& f0 s }
- 1.0
) \6 d. z) A) ` G3 V/ `" x3 n: { - >>> math.log10(100)
. ]/ o+ D' K$ a' q' l) ^2 m - 2.0
/ P$ w K' ]- D - #即10的1.3次方的结果为20& O1 s* V( ?6 q: ^
- >>> math.log10(20)$ l: M- L8 f5 ?& U/ d/ J; g
- 1.3010299956639813
复制代码
. L; z# x- K$ f2 _0 e3 {2 t; n/ Bmath.log1p(x) 返回x+1的自然对数(基数为e)的值+ c. M/ H1 ]$ o( G8 O
- #返回x+1的自然对数(基数为e)的值
4 l5 W! n, a0 m2 K2 M7 Z% n2 x - log1p(x)" e& L! J0 ^! w0 t" g" q
- Return the natural logarithm of 1+x (base e).
8 Y2 D; E) m2 Q4 Q" j) k! Y, T - The result is computed in a way which is accurate for x near zero./ D Z' m/ n5 [% J1 i6 T
- >>> math.log(10)4 ~6 b( G5 _6 l! E
- 2.302585092994046
" w- |4 W, U% z6 t/ ~9 x - >>> math.log1p(10)
: m o8 N, O0 O - 2.3978952727983707
- a4 c* K4 a; a3 M4 J1 a - >>> math.log(11)
4 U' Z. p" Y% o! w. y' U - 2.3978952727983707
复制代码 $ ]) o z/ t& h
math.log2(x) 返回x的基2对数" S6 I; L- O, m* S! P+ H0 x
- #返回x的基2对数
% L v. I' X- Q$ O$ X0 s( \9 G - log2(x)
, W- e; e* S) P! k - Return the base 2 logarithm of x.
) Q6 f, A# m7 _! j. \+ S: E% S - >>> math.log2(32)) T+ s" l$ G7 D9 p% W
- 5.0
" Q9 D. N/ \# W1 s! M - >>> math.log2(20)# H3 g* y3 Q* K: n* w
- 4.3219280948873632 | P$ A+ I5 _5 _( i
- >>> math.log2(16)
) I* C- t4 u: l v" \ - 4.0
复制代码
/ I6 v3 T: u) Z/ n: pmath.modf(x) 返回由x的小数部分和整数部分组成的元组
# b& C# F# \) X4 v- #返回由x的小数部分和整数部分组成的元组
1 n& G [) r! H& D9 J - modf(x)% E. k" y' i+ i! Q2 B& s. m5 @; C. _
- Return the fractional and integer parts of x. Both results carry the sign6 d3 V! S. s, {
- of x and are floats.
, O9 J" [! |6 b3 G7 ?- V; m - >>> math.modf(math.pi); \7 ~' y& E4 |6 m/ F7 C
- (0.14159265358979312, 3.0)% j- Y6 w7 T+ {$ S
- >>> math.modf(12.34) j1 i4 c2 S, E7 j5 v# \" }
- (0.33999999999999986, 12.0)
复制代码
% O# w8 H8 D0 B3 [4 F3 \- u x6 U( ^- Wmath.sqrt(x) 求x的平方根
$ c5 e. Y" A7 l6 B) |- #求x的平方根! f7 b8 _0 ?; ?5 t2 V+ j
- sqrt(x)
( w: U- Q% y% Q8 r8 i0 H! y' P - Return the square root of x.7 p* Z+ C5 r2 z4 c9 Z& `$ q' U
- >>> math.sqrt(100)
1 P, x. Q1 }/ p! R; ` - 10.05 ?3 \. O4 O0 ~- X8 g2 l
- >>> math.sqrt(16)+ H7 c& F1 W4 F( C( t( Y* a) }
- 4.0
8 a3 b/ O5 b" B6 X3 Q: \ - >>> math.sqrt(20)
e6 t+ L$ i5 M% q - 4.47213595499958
复制代码 & ^) X3 H- Q2 P. @. d! o1 O8 `
math.trunc(x) 返回x的整数部分- #返回x的整数部分
+ q2 X" [4 y R. r+ u - trunc(x:Real) -> Integral
% j7 w/ `! y9 u: u. n8 Q - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 K r4 Q F) [* j3 h - >>> math.trunc(6.789)" m B C% o' x3 E* T8 d: @
- 6
+ K) }# E) z+ W, Z( l& Q( E. { - >>> math.trunc(math.pi)
* g2 `6 g6 R6 h* {, | - 3
* ]6 X5 J' ] c& T/ J - >>> math.trunc(2.567)
* X9 `+ A0 [8 n6 h - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|