马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
: i |$ A0 l4 d) t+ r$ _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。, b8 l w6 k: Y3 `; i3 ?
4 F. \9 n4 N; v$ }' t方法1:
) }! M0 h1 Z& \' L" S- >>> import math6 u2 u& j7 r: ]# S/ B
- >>> math.sqrt(9); y, k5 A9 {6 S! J1 u
- 3.0
复制代码 方法2:2 t' u5 l( u# l" d
- >>> from math import sqrt
" l2 E/ f# w% i - >>> sqrt(9)
- P6 p2 @) R0 w; O! V* x9 J - 3.0
复制代码 $ ~, W& M; w2 k! R6 F0 {7 J, w+ e
% ~: r+ ?) _& }& F) e" R6 @math.e 表示一个常量
) W$ }* B* s/ f s8 m* P8 T- #表示一个常量
" k" B' v2 ^1 {, K' `+ R - >>> math.e
" {9 P8 f% `4 M( n" F - 2.718281828459045
复制代码 & z) @ ^2 D) n3 b
math.pi 数字常量,圆周率* o; p0 p8 q4 u- G3 Z
- #数字常量,圆周率4 U7 A5 d5 R+ ^) x; M1 t$ W' `
- >>> print(math.pi)
. z% _) f$ K# E7 P" d" c+ R+ r - 3.141592653589793
复制代码 + h. A( L7 [' [, R: K: x4 _9 V$ a
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x# B5 {" U) A1 W
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 Z ^: t$ L" N Q% s1 \5 d - ceil(x)( {+ x& e( ], V3 a9 I( n
- Return the ceiling of x as an int.
! N3 D; Y0 f- X0 d9 R - This is the smallest integral value >= x.
, A `2 X6 e# ?5 q( J, [7 C! J9 F+ g
* U& _% ~3 I, J. U' C2 ?* y; i' f3 U- >>> math.ceil(4.01)
6 }5 _5 T; p4 A* x6 D9 u o7 ? - 5
2 s8 A5 \$ }0 \. I4 |/ k n& V' v - >>> math.ceil(4.99)
9 ~# t8 O9 u$ a - 5
* k( p; P; F( E- |* y - >>> math.ceil(-3.99)
- M- e2 @. Q$ A) Z6 [+ j, ~, ?2 ^ - -3
7 N+ s. _" ]/ l. A - >>> math.ceil(-3.01)& W8 H$ \/ B: K$ d4 t; |- ~
- -3
复制代码
# ]$ \: K' W: a- xmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 A0 d8 c+ | g* k; m& f5 ?9 I- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 W/ q, w# `, G* U6 B" [
- floor(x)9 t; |% f3 s) ^$ R5 e" E( t
- Return the floor of x as an int.& [1 P7 v- b4 J6 v. f
- This is the largest integral value <= x.
k: @! j5 Z, C3 r9 d: K5 i& h* ^ - >>> math.floor(4.1)
) h0 M+ v0 P6 N+ K9 n' F. j - 4/ t0 s e9 Z( B ?; q
- >>> math.floor(4.999)
- m0 _& l3 x6 u" n1 q" ?; a - 4
: f, H- [, |" J - >>> math.floor(-4.999)
3 Z( [9 I. M; D) H* c5 |6 P2 T, @ - -58 x5 D1 W! o- R+ o. O+ o, j% r0 c
- >>> math.floor(-4.01)
2 G* M. B9 q% a) C+ i& E: T - -5
复制代码 ! ]" K9 `$ Y9 ]. r6 Z& f7 C- F. f4 d
math.pow(x,y) 返回x的y次方,即x**y
) s( Y" A4 P: Y5 ~( Y- #返回x的y次方,即x**y
3 x/ [, W7 R( j5 R1 Z/ [ - pow(x, y)* l: F' q" C1 J5 B
- Return x**y (x to the power of y).9 Y' M. s" l! G Y
- >>> math.pow(3,4)1 k% \' m( Q4 K" p
- 81.00 M5 W* \+ k* }7 O* y5 L
- >>>
# Z* v0 G7 O# j- z - >>> math.pow(2,7)7 b) H0 ~4 n6 v2 W' B, T
- 128.0
复制代码 ( u$ h+ i6 `: I
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: X( o& L! p) n& S1 G- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 \+ }- v! t$ n C
- log(x[, base])1 a( ^1 B) @; f: ?- P& A. }3 u0 [
- Return the logarithm of x to the given base.
! U( w- m1 H" E" g s* e3 a - If the base not specified, returns the natural logarithm (base e) of x.
$ ]+ K5 N+ ~1 s1 i! n& x - >>> math.log(10)' h6 s& g/ [5 t4 I/ M: C
- 2.3025850929940465 E5 Z5 D1 |0 ^5 D& b9 c
- >>> math.log(11)
+ n, s0 T6 s; r1 v. n - 2.3978952727983707
) h: u4 N3 h" N; V& x - >>> math.log(20), a1 r1 q$ k% y$ W& d" x
- 2.995732273553991
复制代码
2 S9 s1 U! b- ~8 rmath.sin(x) 求x(x为弧度)的正弦值
2 b' W+ s: C5 p) N5 D- #求x(x为弧度)的正弦值
" e% l" z8 G/ ?9 t9 s0 r - sin(x)! U3 g* `# h; `8 L
- Return the sine of x (measured in radians).
2 t( T% {* W0 r6 q3 X* i - >>> math.sin(math.pi/4), @7 V9 o! j& G
- 0.7071067811865475, L: h( V% L# J% c7 y
- >>> math.sin(math.pi/2); a- u1 z4 z9 s
- 1.0. x+ B2 U7 Y; e3 ^
- >>> math.sin(math.pi/3)
- k ? u K- l, ^, p0 _1 t$ E' a - 0.8660254037844386
复制代码
+ _9 C9 m) p$ x5 ?7 E: ymath.cos(x) 求x的余弦,x必须是弧度
: v, M; r3 O( W% g0 W2 ~- #求x的余弦,x必须是弧度$ G$ P& w1 |( L. c
- cos(x)
5 N; H1 g3 F4 Q7 t - Return the cosine of x (measured in radians).' b+ a& p: e0 Q2 z
- #math.pi/4表示弧度,转换成角度为45度
3 c4 w0 _0 P" H) @: C5 W - >>> math.cos(math.pi/4) u1 ~4 }8 K( h2 r
- 0.7071067811865476$ y+ b8 |! E& Y4 H! ^8 z
- math.pi/3表示弧度,转换成角度为60度
6 J' |7 ?) J7 h* S% e4 R- M - >>> math.cos(math.pi/3)3 a3 d9 f2 C; z
- 0.5000000000000001 R/ Q; j( S9 U8 L
- math.pi/6表示弧度,转换成角度为30度" ^: _3 ?* I- c, w+ m. k7 A! T
- >>> math.cos(math.pi/6), U* w: U$ S- \ d
- 0.8660254037844387
复制代码 " e2 ?& j6 ?" ]' R
math.tan(x) 返回x(x为弧度)的正切值
4 Y/ C, c9 i% u) S0 M; V z- #返回x(x为弧度)的正切值
! a. ^+ Y, Q( |/ `; ?! l: N6 m - tan(x). D. b6 n' t9 l# z2 C; e2 f) A
- Return the tangent of x (measured in radians).! }% ~# ~9 ?( P( J% a7 [7 Y& f
- >>> math.tan(math.pi/4)3 ^5 @! A7 T8 @6 v
- 0.99999999999999994 Q3 r/ F; Q3 j) \3 X/ d% u; c
- >>> math.tan(math.pi/6)
% X" v* S& W% W& Z1 f% _3 k - 0.5773502691896257
! N' Z/ I a: [2 m - >>> math.tan(math.pi/3)
) Q+ B, _6 S- p t4 R0 c - 1.7320508075688767
复制代码 & r; F! F1 D, X* z% v- ~4 F
math.degrees(x) 把x从弧度转换成角度# w! d% z7 m" s1 X4 d- {; q
- #把x从弧度转换成角度; g% R( ?% `- s: {7 J& p
- degrees(x)" O1 Z; |5 ]/ o" h
- Convert angle x from radians to degrees., w7 `5 r, z; z. {3 c
m8 h, C6 O3 @ S8 `7 X( o- >>> math.degrees(math.pi/4): _% G! W2 _ Q0 D1 ^9 m; b( k
- 45.03 Y# E, @* N! N! B
- >>> math.degrees(math.pi)4 \3 E% Q* n9 U& p
- 180.0
" @- p; X% ^3 L; m - >>> math.degrees(math.pi/6)0 L* ? u7 D) N7 G+ t) j
- 29.999999999999996' r! i0 H; _2 M9 r; H' t
- >>> math.degrees(math.pi/3)/ D# a9 P4 k9 X: N6 l [
- 59.99999999999999
复制代码
) u5 N, o$ M0 s$ O( n$ Pmath.radians(x) 把角度x转换成弧度7 l% \1 [* q! o5 \; _2 U! N
- #把角度x转换成弧度- [' j- I8 `- K. ]6 j
- radians(x)
! W5 M& x% z+ W: g3 W( t - Convert angle x from degrees to radians.
# O. \8 H- G4 y1 ~1 x - >>> math.radians(45)
, z! i6 B2 I8 _$ } - 0.7853981633974483
+ w, O$ X* F4 S/ B, W& C - >>> math.radians(60)
* [9 p* h. ?/ s4 ^$ @ n- u7 k - 1.0471975511965976
复制代码
2 q7 N" l1 s' F+ x; ^math.copysign(x,y) 把y的正负号加到x前面,可以使用0
) u) f+ @, y* m: j: f& ]- #把y的正负号加到x前面,可以使用0
. [5 V) X! p9 J - copysign(x, y)0 S1 f: h0 E" `6 s5 ^
- Return a float with the magnitude (absolute value) of x but the sign 8 S; H! @4 \& o% W
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
5 }5 Y! f5 |! V8 t( G. W' a - returns -1.0.$ |; B( O1 a) z# E
; j( e0 K: u- s5 _- >>> math.copysign(2,3)
1 I% \$ `: `$ [" L! s( u) L% w# ~) ^ - 2.0
% k9 m/ ]# W/ b/ _: t% e' U - >>> math.copysign(2,-3)9 E% h1 b2 G v
- -2.0
" y' k( v- O/ ]2 l - >>> math.copysign(3,8)
: x0 D5 j4 D8 M) A: s/ S; v0 b! U3 h+ W) E - 3.0* V( m9 J/ p- G' p" b; o( J/ z2 A ]
- >>> math.copysign(3,-8)
4 j* O& E% U0 z% [; @ - -3.0
复制代码 ( `& ~' m" g' r: z* O
math.exp(x) 返回math.e,也就是2.71828的x次方
+ U( q" C3 {0 U6 M( y- #返回math.e,也就是2.71828的x次方
8 P, ~7 k# G" g _2 }7 k$ u( D - exp(x)
4 G1 n/ \/ S: D$ K! x6 \& q9 W* ` - Return e raised to the power of x.3 p) V4 N2 V: u e0 o
( U/ z2 C% B0 ? _5 L! |- >>> math.exp(1); b/ X0 _" w/ m7 ?# ^
- 2.718281828459045- ^+ Y# [3 J% n
- >>> math.exp(2)
+ s' |/ V" X$ p- M$ w j6 t9 J - 7.38905609893065, S; L! g6 O/ k- |/ ^& F
- >>> math.exp(3)+ ] K8 H( Z0 @9 r `5 X
- 20.085536923187668
复制代码
5 U6 K& S1 i: c6 c/ {1 x" b6 Ymath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
' a, r' I1 ~2 o" G8 S- #返回math.e的x(其值为2.71828)次方的值减1
/ C4 B4 Q. q( q: ?8 j* a - expm1(x)5 R# p" j0 X! A* Z F9 a
- Return exp(x)-1.
; c0 n- M& o. a - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
' B |9 L3 z5 G: O - ! E! Q$ k: k+ u' l7 h
- >>> math.expm1(1)
6 \2 Y6 `5 y2 e. N# [ - 1.718281828459045
" o" [3 |4 g7 ?& X! ~ - >>> math.expm1(2)
/ I# g% I! ~; A# Y - 6.38905609893065& a Z3 j" l/ r: P7 R$ g R
- >>> math.expm1(3)
/ i& H! K, C# v3 {* W8 ~. v+ p/ o R+ d - 19.085536923187668
复制代码
* A+ ~) n. i$ p/ P4 v C y5 Bmath.fabs(x) 返回x的绝对值
# \# F: u/ t1 u; T- #返回x的绝对值
! c1 a0 x4 n9 p+ X( l4 l3 F - fabs(x)
- i5 @/ q3 ? Z/ N! d) z - Return the absolute value of the float x.
; N6 v, W$ r& ^. o5 y - 8 X) X4 k1 K/ M( k' y3 N
- >>> math.fabs(-0.003)
. d9 q' R c$ C; [ - 0.003" W& ]7 P4 W o8 d- R7 F6 R. ]
- >>> math.fabs(-110). z! t0 W. J% E9 f# S$ ^
- 110.0
1 M4 W5 v. J9 e3 B - >>> math.fabs(100)
1 \* U* d! w, }( G - 100.0
复制代码 - m Z G/ f. v& `
math.factorial(x) 取x的阶乘的值
* L0 }8 R5 T. k/ ~. z. M8 M- #取x的阶乘的值/ @9 u# h* L! B [7 X' U' Z
- factorial(x) -> Integral
5 I6 X1 E9 I3 b1 Z - Find x!. Raise a ValueError if x is negative or non-integral.
3 Y! h! G3 A E- I3 {+ d - >>> math.factorial(1)
0 R1 H, i$ n N# o; R" w, e - 11 m. l0 ^) R7 w9 d$ T$ P
- >>> math.factorial(2)& F5 H7 {4 A; r1 n
- 27 t3 ^+ v9 `$ A0 y) U
- >>> math.factorial(3)
& D) {1 v& ^. j) { - 69 M" o- j3 y- q3 {* S" Z
- >>> math.factorial(5), m# E' U( i3 x P# u
- 120
: U+ l" s' T* J- g0 J' G" d5 w' @/ ` - >>> math.factorial(10)
! _0 l7 ~5 A1 \9 h4 E - 3628800
复制代码
9 c0 z/ z, m1 H1 j& }math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
3 g0 M# ]2 P0 o7 @# {8 h0 v- #得到x/y的余数,其值是一个浮点数$ p' X! T* p- p5 @$ Q; c
- fmod(x, y): `$ G2 d' p2 y3 n/ m9 S% f: k
- Return fmod(x, y), according to platform C. x % y may differ.% @5 `# @* U1 j8 M, s f6 Q- P4 r4 v
- >>> math.fmod(20,3)1 E& _4 i2 w/ Q' Y, }
- 2.0
: A. ^4 {0 p7 K# a! L& ^ - >>> math.fmod(20,7)
! q1 }4 f, n# e8 g5 W/ c8 B$ u5 S - 6.0
复制代码
0 h' T0 O# Z8 N/ f! |3 Smath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! Z4 Z% N( A, n( R$ r6 a* J
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 b8 S# L! h! R: P7 }- B
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: o$ L+ h* O/ e# u \. v
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; ?9 V; l. W! q" n1 C1 v' h
- frexp(x)
) }" b! U) x. z. K. \0 L - Return the mantissa and exponent of x, as pair (m, e).
7 z7 a( a- i: P - m is a float and e is an int, such that x = m * 2.**e.
8 E% d' V: B" o$ b. Y t - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
& ]+ @! U0 V0 x# P - >>> math.frexp(10)
: p1 t& C M) v: e, U - (0.625, 4)
2 @ {: S- n2 k- W3 ^5 @, P3 D6 F - >>> math.frexp(75). B9 g' M9 G5 [; q
- (0.5859375, 7)" _5 O9 S1 H( U& n- |" z1 Y" C
- >>> math.frexp(-40)) q& f( U! A) x4 O9 Q) @2 z; y
- (-0.625, 6)8 D. @: N) B9 t6 S
- >>> math.frexp(-100)
; }( t" `% ]7 A& |3 |* V - (-0.78125, 7)& B3 h4 j( W. X2 j3 @6 m+ a
- >>> math.frexp(100)
3 o H, s: n) f. S - (0.78125, 7)
复制代码 8 Z3 d- \) x& ~: h* S& N. o2 r
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
: i; E1 w$ B. d. _; P) |- #对迭代器里的每个元素进行求和操作; j$ E; t) _7 o8 W I3 d. e
- fsum(iterable)
2 j3 i0 y3 d0 D/ E' _ - Return an accurate floating point sum of values in the iterable.( C8 z5 N: I! x
- Assumes IEEE-754 floating point arithmetic.
$ h% \( g( V2 {8 G1 q/ o- ? - >>> math.fsum([1,2,3,4])
j: c7 O; ?: { - 10.0, U# j9 c( L2 a. [ [; u
- >>> math.fsum((1,2,3,4))% A. ^) S x+ _ i8 ?3 H
- 10.0
$ _4 D l9 v$ `! w7 f/ a - >>> math.fsum((-1,-2,-3,-4))$ y/ i6 z+ y5 f
- -10.0
, \. y, F: ~/ t - >>> math.fsum([-1,-2,-3,-4])
8 a- C/ q8 h' A' @$ s - -10.0
复制代码 1 x: V' C8 m( T9 P
math.gcd(x,y) 返回x和y的最大公约数8 ~( s% Q- h7 O8 @7 k2 f
- #返回x和y的最大公约数
# v. k3 c0 c, i. Y% N# p6 Y - gcd(x, y) -> int
9 Q4 X3 g2 r) I. C% N1 J - greatest common divisor of x and y
6 v# O$ ]" Z9 R4 R - >>> math.gcd(8,6)% j: T: _: ?% `) F0 p
- 21 ?5 k. O0 D8 I3 e! `6 A
- >>> math.gcd(40,20)( Q: z x# Q: N1 Y/ e
- 20
- V0 ^4 k; n# Z0 t! y8 D4 ^ - >>> math.gcd(8,12)0 y3 A! K8 W& ?6 Z) r
- 4
复制代码 2 U h* K/ w/ M
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False' d0 d' S3 z6 u" ~5 h5 O" @
- #得到(x**2+y**2),平方的值
% T, f/ S" E& z. ^7 ~. J- M9 ^ - hypot(x, y)
9 \ C2 T& I$ U - Return the Euclidean distance, sqrt(x*x + y*y).3 n1 f$ P2 t5 V8 c# i
- >>> math.hypot(3,4), d& {: z( i* g
- 5.04 t" G: u1 z0 O
- >>> math.hypot(6,8)+ a% ~* a' P. F! z1 C% q" ~
- 10.0
复制代码 1 i+ P8 y J5 N! v+ P+ \$ ^
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 s. R) C0 [9 s
- #如果x是不是无穷大的数字,则返回True,否则返回False# a" _/ C6 ^3 h8 ^$ S' I8 P! k. j$ Z
- isfinite(x) -> bool5 j5 ?2 }# E% A. m8 U
- Return True if x is neither an infinity nor a NaN, and False otherwise., _. f: m* F* |+ d% F; y
- >>> math.isfinite(100)
, \: ?4 v& p& x' O0 _5 H - True# K/ F4 @" q5 v7 j
- >>> math.isfinite(0)
+ X. s6 \1 H1 e' t - True$ ^+ J& Z+ w! u0 C) F0 B
- >>> math.isfinite(0.1) `! Y3 W$ w+ U3 ^5 ]
- True! X/ J1 q' c- K3 A+ ~/ D8 c# f% I9 G
- >>> math.isfinite("a")6 x+ E( m+ t8 [4 J' x) B
- >>> math.isfinite(0.0001)
8 \* S u1 q9 ^, f4 K - True
复制代码
c) s6 b% ~1 I9 g0 F& u# Emath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False+ o8 a, X5 W" T5 q) q% K
- #如果x是正无穷大或负无穷大,则返回True,否则返回False2 i; t7 i: w4 d2 O
- isinf(x) -> bool
( u2 t# W. K6 @0 K/ o2 g% U( F - Return True if x is a positive or negative infinity, and False otherwise.9 ^( v! x1 `+ g
- >>> math.isinf(234)
* Z9 o$ H: F* m8 |- L2 S - False
% }# j1 v& }/ a- s% N' P8 I - >>> math.isinf(0.1)# z8 [: t5 G; p+ h3 x$ E4 t2 x
- False
复制代码 $ F. O: H$ ~; U. K" W, v+ p! @
math.isnan(x) 如果x不是数字True,否则返回False% b$ C: }% q4 j+ J
- #如果x不是数字True,否则返回False7 ]% S, \9 _; b$ b% u+ b; ?2 k
- isnan(x) -> bool5 e! s7 S5 E6 ]
- Return True if x is a NaN (not a number), and False otherwise.
5 c3 g" `( z' ` - >>> math.isnan(23)
8 L& e( M% I# D' a - False
/ F/ S2 W' f8 f( m0 V- J7 B7 G! W5 ]1 H; ` - >>> math.isnan(0.01)
3 k' M9 p- r3 U8 P+ k - False
复制代码 + y( |4 J" @, D: \( V5 q
math.ldexp(x,i) 返回x*(2**i)的值
# ^: L1 s6 G0 m! V! } @- #返回x*(2**i)的值' z3 x% M/ O8 I- A! w# E& Y% s! ^7 A
- ldexp(x, i)
, s" A6 A( l8 d& M' ? - Return x * (2**i).7 [5 e( O2 G; Y& h" q
- >>> math.ldexp(5,5)
. t6 |2 F, q( X2 P; q2 }8 | - 160.0
, R6 ~% K- c) M! i3 m8 U# e- I - >>> math.ldexp(3,5)
7 m3 R' I }- }$ Z+ r; j# I& e+ N - 96.0
复制代码 ; Y7 o# N C' r/ H7 C
math.log10(x) 返回x的以10为底的对数. ?) ^3 L8 z+ H: N0 Z1 C
- #返回x的以10为底的对数5 Q: V" Z6 Y+ f; U3 v2 L* M
- log10(x)! U# x; D, O! f- x% _' Z
- Return the base 10 logarithm of x.
: D3 ?: O* `, Q' ?+ e - >>> math.log10(10)# _ X4 B, \2 ^9 i0 G5 W
- 1.0
& i+ i. b _5 X! g - >>> math.log10(100). x) p5 A* ~2 f- K- ?$ ~ K' h L
- 2.06 U& r; ^. x9 h( N; u, n$ {+ I
- #即10的1.3次方的结果为20
/ I3 L* F; L* K( S- A% b N - >>> math.log10(20)) d1 p& L" m( {/ h4 a
- 1.3010299956639813
复制代码
+ b8 i( s9 E( k: Z4 C6 Umath.log1p(x) 返回x+1的自然对数(基数为e)的值+ z% q" P! f! {; c7 z g" `
- #返回x+1的自然对数(基数为e)的值
5 L. E9 z- g! X/ q' _/ X& p$ } r - log1p(x)
! Y+ Z4 [9 G) A& ^ - Return the natural logarithm of 1+x (base e).# N" t+ R4 k" C# F" f( ^7 N
- The result is computed in a way which is accurate for x near zero.8 o. } t5 Q7 s6 j$ p* m: b& E4 \
- >>> math.log(10)2 D. V @1 p& c2 D/ t: e
- 2.302585092994046
( n# F' N. I% S# K! F/ ~; P - >>> math.log1p(10)1 v& K4 T% j$ S! C3 `
- 2.3978952727983707% F* G0 W$ @" ?" r* W2 A
- >>> math.log(11)9 h* o C# p" C+ P+ Q* y/ m0 X; U1 I
- 2.3978952727983707
复制代码
" Q- k- G( U3 X. i, d6 hmath.log2(x) 返回x的基2对数
( A$ `1 ~6 _3 h. u9 b* \9 T: ]- #返回x的基2对数! p4 P( D. u: r
- log2(x)
9 w8 T- W) J3 E0 Y8 L/ S) w2 I5 d6 ? - Return the base 2 logarithm of x.
+ z; I. _; _4 n - >>> math.log2(32)
' T* M$ N1 y, }, H9 N$ d - 5.0; x! |) V! f* U
- >>> math.log2(20)
5 D! C( v) m6 `) E5 C: ^ - 4.3219280948873635 N& }0 q! F! O5 |" X6 f# ~
- >>> math.log2(16)
2 B7 v, X" r- o' {8 z3 }4 G) \% z) ] - 4.0
复制代码 7 M- }* |+ |% k g* h5 U( q
math.modf(x) 返回由x的小数部分和整数部分组成的元组
X2 J' b6 ~! f% K2 R- #返回由x的小数部分和整数部分组成的元组
7 `( H5 k$ r. ~/ e: x a - modf(x)
( M& y$ C& N0 C! L5 I/ g - Return the fractional and integer parts of x. Both results carry the sign% }8 _. ]. S B& p! @ E2 L
- of x and are floats.
& y$ D2 S2 B0 t - >>> math.modf(math.pi)# Q' ?1 b9 H4 I( U# r! u
- (0.14159265358979312, 3.0)
8 a. i/ {& |6 {; ]4 }6 Q0 m3 D - >>> math.modf(12.34)1 m* I K6 {- F( V+ y0 V
- (0.33999999999999986, 12.0)
复制代码
( x9 I0 K1 W0 I& u, C& O' Emath.sqrt(x) 求x的平方根' w- P% ?0 i/ U. b0 d
- #求x的平方根
/ R) d+ O2 ]# H' O - sqrt(x)( ~- a4 ^$ C- X( k
- Return the square root of x." i+ }3 h& @; w" G
- >>> math.sqrt(100), d% B* \/ c% ~0 g( e
- 10.0. y8 A% h$ F ?5 J; \( q" H' d
- >>> math.sqrt(16)
9 L* R! G; e6 G: p* p - 4.0
" U/ t0 |" h5 L% d9 }* s - >>> math.sqrt(20)
" R( Z6 i" c4 |/ G3 l - 4.47213595499958
复制代码
( {2 e! [" k9 S3 c7 S9 r3 omath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 U. @( O- v3 o+ l3 j9 {! H# U+ `7 \% h! h - trunc(x:Real) -> Integral
3 f! v) E% M" ] - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
- I- u" @* E- H; `- B3 w$ ]- \ - >>> math.trunc(6.789)
2 Q6 {4 q& O; u& D - 65 T' A' D% g( C: e1 ~! c6 B& O
- >>> math.trunc(math.pi)
% K5 X& s, e& z7 g7 L4 ] - 39 n l( s E' @- i9 A* T: d
- >>> math.trunc(2.567)" l! t% p! ^( p( K7 d
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|