马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
: l" n% `+ Z! y# z
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。 t6 X E' H' V% [) u; x$ b& r
6 }" @- t0 C# P) x
方法1:
C' |) ?' V8 |3 `5 I; H- >>> import math
8 e. P7 D5 X2 C; Z - >>> math.sqrt(9) A* S' g$ K4 K/ M( o* B& F$ f
- 3.0
复制代码 方法2:" b2 m" _3 R# i0 ]: W
- >>> from math import sqrt1 V9 B0 N0 I: L2 c C/ R, p( p& m" h
- >>> sqrt(9)0 ]7 K" f0 _9 x# z: n
- 3.0
复制代码 0 ~) R7 s' K% o4 a1 ~( T- b* A
1 u9 Z/ z" T* R0 o: Pmath.e 表示一个常量
: ]3 L6 P1 c3 b+ r; o ]- W- #表示一个常量* s) p; v8 o0 U2 } n- I3 l
- >>> math.e
. A$ z) K7 w' b2 K2 v - 2.718281828459045
复制代码
% ]+ t9 I. F# H2 W2 m& Z" l7 tmath.pi 数字常量,圆周率
' B0 d7 t2 V; V% p i8 t- #数字常量,圆周率
( j- K; {. H7 l8 D4 @1 Q7 F/ T - >>> print(math.pi)' D( h# R* ?9 O; }- M" w) U
- 3.141592653589793
复制代码
; Q! ]# _9 i6 r V' a# g# }5 @math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x' ~0 J- r! ^2 Q% S. {# o1 q M
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ b* g& Q/ S' J8 K6 I - ceil(x)1 g3 x# Z+ f; c+ @. M& u/ J
- Return the ceiling of x as an int.0 Z, m2 n. o# d- q- p( I5 C& A
- This is the smallest integral value >= x.
+ R6 F# m' _7 i - ' @- ]8 \+ I" h- }% v$ Y
- >>> math.ceil(4.01)2 ?/ l5 j+ M3 `" `7 p
- 5
1 N" C6 F" G& E" j: k" u - >>> math.ceil(4.99)
( ]4 A% V' y7 q1 q$ G! { - 5
! @" d5 O$ w Q5 M: K% R - >>> math.ceil(-3.99)7 v7 l$ U6 n3 L- x t7 u6 V
- -3: V. T) w4 K8 s" e
- >>> math.ceil(-3.01)
7 m' c$ r0 `8 m8 S - -3
复制代码
! n1 Z, @3 |- s& f8 hmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
7 O' O0 U/ H) f9 u* I. g( z- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% i+ M* M0 p# x; s% N+ @/ l3 E - floor(x)
$ o# X f% ] e+ c - Return the floor of x as an int.) k$ A$ e; w1 H- h) \) ^% n
- This is the largest integral value <= x.
* |' T4 ]8 M, \* \ - >>> math.floor(4.1)
% E" c7 a1 {) N" p - 4
5 f# ?) v v/ k2 E - >>> math.floor(4.999), m% ]: l; q3 T8 A5 m. ~
- 4
. M( h' M. v2 T9 {3 M W! C - >>> math.floor(-4.999)
+ h/ I4 y2 K, P: i3 y5 ]/ j - -59 I6 S1 P$ F! \: z- D4 _ {+ h
- >>> math.floor(-4.01)
$ ~+ O/ H* [9 d( [9 f - -5
复制代码
8 m) H3 o& d2 q, G& h6 E) hmath.pow(x,y) 返回x的y次方,即x**y. }! M" H0 E) j. _
- #返回x的y次方,即x**y
- k0 \/ ~& n' Q8 ?) | - pow(x, y)1 i* w% Y7 R$ O0 C
- Return x**y (x to the power of y).. U- ]) P+ m6 y; u' K
- >>> math.pow(3,4); Z! f8 m8 J8 w( L* P7 w0 Y, v
- 81.04 S; s8 }6 |* q2 M4 {% K3 [
- >>>
7 C" z. `4 ~4 S. K0 \ - >>> math.pow(2,7)
5 e; [- Q8 S5 t' B8 a$ y# c - 128.0
复制代码 $ l. s7 W+ @7 f2 k; t3 M; D. ]! X
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" A' x$ g6 z" J
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, M& X& a9 H- m) p2 z - log(x[, base])
3 Q: Z; o, v, w$ F3 { - Return the logarithm of x to the given base.* B3 V+ Y c/ ^$ Q
- If the base not specified, returns the natural logarithm (base e) of x.. A6 k& t* F0 F. o8 t9 b% U
- >>> math.log(10)9 J) `. @! {# f! D0 L7 v
- 2.302585092994046" q. x0 ^$ h L% p
- >>> math.log(11)5 P7 u+ T! ]5 N* s: c7 }0 Q" t
- 2.3978952727983707% G& n% D" t3 b, R, P# s
- >>> math.log(20)! T! A! ]2 Q7 P5 t$ D* o
- 2.995732273553991
复制代码
" K0 S0 M) E$ i0 W# ~7 I4 wmath.sin(x) 求x(x为弧度)的正弦值8 i7 v2 v4 M3 ^* y
- #求x(x为弧度)的正弦值7 r% G, S L) j0 B e/ e, ~
- sin(x)" d; a+ X! J. x) n1 A: D* t% @8 k
- Return the sine of x (measured in radians).
- M* v0 y+ f, k$ E$ H% v6 P0 n - >>> math.sin(math.pi/4)
' V4 ?9 H8 F1 ^3 ~( { - 0.70710678118654755 G% {% r4 r6 l0 I/ R L* Q2 l' o0 s
- >>> math.sin(math.pi/2)
! h: u2 i( P& I3 \( Q! h% C5 i - 1.0, c' p+ `$ |2 P; M" p, m" d# ?
- >>> math.sin(math.pi/3). @0 Y, a) @3 N7 Z5 @3 N; p4 L B
- 0.8660254037844386
复制代码
2 G: M) E- _0 ~7 V& Y+ nmath.cos(x) 求x的余弦,x必须是弧度& }/ ^4 y( `& `
- #求x的余弦,x必须是弧度* ?5 n7 ^- s' _# h! u! L# Z- q
- cos(x)% X3 Z. H! g$ p: g. Z- e! T
- Return the cosine of x (measured in radians).6 @1 q8 b; g& R8 }4 a+ r- ~
- #math.pi/4表示弧度,转换成角度为45度
G" J+ @% H: O% q- C! l0 }- d( R - >>> math.cos(math.pi/4)
# G, A9 z5 |+ B' y G- u - 0.7071067811865476
; B3 ?1 E- @1 F - math.pi/3表示弧度,转换成角度为60度
* x( _/ M |. K, t - >>> math.cos(math.pi/3)
! L' m. [( P- K7 [$ I3 P - 0.5000000000000001% ?# T! y7 w( b, V+ V
- math.pi/6表示弧度,转换成角度为30度7 ^$ X7 ]5 H" l( z4 x7 W' z0 D# z# y
- >>> math.cos(math.pi/6)
f8 C6 h3 p- v4 f* j) L$ g - 0.8660254037844387
复制代码
9 Z. B. n5 b" C0 h6 Wmath.tan(x) 返回x(x为弧度)的正切值
$ `. B) B# W0 Z# G- #返回x(x为弧度)的正切值
( _- d% f6 R }' V4 }3 e' Q' F1 [ - tan(x) `- m; H# D. e6 q3 ^4 v; T+ k
- Return the tangent of x (measured in radians)./ S Q1 |% q1 d2 B, t( K9 `
- >>> math.tan(math.pi/4)
& v( Y$ U3 C; g6 S7 ~ - 0.99999999999999998 R7 h% `5 i1 ]2 A( f* K4 I
- >>> math.tan(math.pi/6)
9 A0 D. d+ e5 g- m3 g - 0.5773502691896257" Z N F* d, N; w1 D
- >>> math.tan(math.pi/3)
* C3 V0 t2 \3 B - 1.7320508075688767
复制代码 & o% U3 R& o+ i' y" B* e9 y% o
math.degrees(x) 把x从弧度转换成角度
6 X" O2 m4 |6 A9 t- #把x从弧度转换成角度# g' P% H+ l% r; m# J' Z
- degrees(x)
$ g; k& G% _( h9 w3 @. e2 {& G - Convert angle x from radians to degrees." m; r' J3 D9 C/ u
: W, D6 Y5 a. P+ Q3 B- >>> math.degrees(math.pi/4)
- L) a5 z. U* V/ G% n, A! {$ X - 45.0
, m* C+ A8 x, Y - >>> math.degrees(math.pi) W+ Z3 a0 s' n5 r- Q: |
- 180.0( O" A. `- ]: h" O8 S# k2 ]2 V
- >>> math.degrees(math.pi/6)1 ?+ {% D2 z& k0 S! @% r r# c) G
- 29.999999999999996
; x$ N$ Z4 l, {. f- @0 `1 o - >>> math.degrees(math.pi/3)' H: s3 N& P& L; U1 ]
- 59.99999999999999
复制代码 * T1 g8 v6 r' e* I9 F
math.radians(x) 把角度x转换成弧度
' f8 Z) E. Z1 t: v& k4 n- #把角度x转换成弧度
5 ?( O2 ^# z1 X" W/ y - radians(x)& _+ ~& }0 g: h1 t, V/ z
- Convert angle x from degrees to radians.
% w# }1 U: e- p) R - >>> math.radians(45)
% s( Q* i" S; ^ - 0.78539816339744831 @* K. b) X7 o# a/ P6 @. _: S j
- >>> math.radians(60)
# [8 @' `5 L! u - 1.0471975511965976
复制代码
( m. d1 @/ n# Xmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
3 }( ^* V7 J9 P- _$ p- #把y的正负号加到x前面,可以使用0& A' F' b. ~0 g0 H8 u
- copysign(x, y)3 \, f, P. W$ J0 r, D8 G
- Return a float with the magnitude (absolute value) of x but the sign
! C9 z% t$ J! a. U0 Q' z - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 t! `; v1 C. C I4 J$ s - returns -1.0.+ ^% A8 e) y( n7 T! J5 ^2 b+ q
- 2 i& M* ^ d% ~) _+ D# |& T; d$ P/ ~
- >>> math.copysign(2,3)8 T* L% q. y t9 c% `$ h# h
- 2.0% V s* k& z% Z" I# V/ m, U
- >>> math.copysign(2,-3)
- y: q% o9 h7 @3 w2 w) m - -2.0
: @1 M) b( r- Y8 u) W5 t. J5 s: m4 e - >>> math.copysign(3,8)
' b0 _: Q! w0 n$ l" _# K/ T9 H - 3.0/ k) s; a- P; w! }1 |6 a* R
- >>> math.copysign(3,-8)& o/ I C3 A- A& W. }+ m. J# g, Q) D
- -3.0
复制代码
5 j+ v* N5 f4 y6 l$ W3 Rmath.exp(x) 返回math.e,也就是2.71828的x次方
+ A0 B$ o N2 P' ~4 T h, p- #返回math.e,也就是2.71828的x次方' S, U. V% C" w. Q ~8 d% V2 ]
- exp(x)
( f9 n! t+ \0 a1 \0 a - Return e raised to the power of x.
* \8 P+ I% K1 l/ {3 L3 [3 @$ K
u/ b$ V" R4 \) E" v1 E- >>> math.exp(1)
7 ^) @6 x- D/ q) ~* g, o* d - 2.718281828459045
+ u# Y. b' H: \& Q7 ?( { - >>> math.exp(2)0 J, O& \* I6 A; o. }& I
- 7.389056098930650 i6 k7 B+ K ?% ^& U+ d) q2 D
- >>> math.exp(3)1 X8 r# D" h s8 B- }3 A
- 20.085536923187668
复制代码
9 {: e3 l: p0 n) R6 t5 J; ^8 Z/ cmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 b/ m: h8 B0 g! n! o( Z9 V( E; [
- #返回math.e的x(其值为2.71828)次方的值减1
( x. k/ q' R' [ B0 T+ h% | - expm1(x)
! A, h% d. S; E0 \/ K - Return exp(x)-1.; U& q: n$ ~( l0 U( h: O
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
6 u3 A& v4 C' N4 w5 U* L
T0 T2 Z6 ]* K0 Y- >>> math.expm1(1)$ n, i' W/ Y L
- 1.7182818284590450 U! }' _/ T( Q9 e1 k" y
- >>> math.expm1(2). j9 O3 U0 O% `1 [% {) g2 w( r( y+ S8 k
- 6.38905609893065; A ]7 n1 |3 Q6 e
- >>> math.expm1(3)
# l. b* p7 g# |/ w0 r, p+ ` - 19.085536923187668
复制代码
, |, d1 M' y. D: g2 a% dmath.fabs(x) 返回x的绝对值
3 K0 A7 P1 w. g- v$ A% w- #返回x的绝对值3 n4 j1 C. d4 |5 ~4 N0 r2 ?* A
- fabs(x)$ N! ]( g( \3 l+ U; X8 e: ]
- Return the absolute value of the float x.) c( G$ z1 K( o+ p5 n6 {) t
- N% q0 `+ m8 ^- ?8 _& m U# m& }
- >>> math.fabs(-0.003)
" a0 ^. G3 X# a' |3 |6 O0 M - 0.003
: n, }, L, n) P" ?" n - >>> math.fabs(-110)
7 E3 }1 J4 d u% [+ {( n - 110.0! R8 b8 Y) o. t8 h8 d1 c
- >>> math.fabs(100)
7 \: Q( }9 p4 l - 100.0
复制代码 4 [! t/ S5 p0 W! j; p1 D
math.factorial(x) 取x的阶乘的值( t& S `7 D* K1 @& K
- #取x的阶乘的值
4 N& ^$ M. @) N: ~8 @ - factorial(x) -> Integral7 r3 S# |' j9 _+ z1 t
- Find x!. Raise a ValueError if x is negative or non-integral.* C, U$ ^7 \' f) q0 t
- >>> math.factorial(1)7 C5 Y; ?# v5 @* F9 `& X
- 11 M7 n' I( Z7 W- {2 t* B
- >>> math.factorial(2)8 a. k# W/ K: W9 v
- 2: A& R. @' O4 u) z4 H( Z' n, b1 M
- >>> math.factorial(3)
- {9 R" E6 S3 x6 Q/ T, M - 6) x. C# q8 O/ ~) G+ ?+ }
- >>> math.factorial(5)! z1 Z* s2 I8 K6 O" Q6 f
- 120
6 e0 e& L+ E# ^ - >>> math.factorial(10)6 }# \9 z. Y- J, h4 @6 w
- 3628800
复制代码 m4 J% ?7 L; e. Z* J
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 B; U% X# P7 J0 F* e$ b% Y
- #得到x/y的余数,其值是一个浮点数6 C7 {0 o, @: A
- fmod(x, y)
. H, n4 C3 x; L9 s - Return fmod(x, y), according to platform C. x % y may differ.1 R9 H, C$ j ]; U$ ~
- >>> math.fmod(20,3)
$ e; j ^% V" w6 S1 q - 2.0
0 L% H% ~ Q4 w2 `9 w/ p: f - >>> math.fmod(20,7)$ e |, D, F" E/ ?+ e# C
- 6.0
复制代码
+ f6 h {3 E- p. d6 V1 gmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围+ s: }* p5 l) m! X+ @6 P
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
* O4 e: D2 Y5 ?7 d - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
- k2 v8 D& Y( I4 A+ v' P - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
6 ?- R% h2 J2 q8 q: C1 z1 _ - frexp(x)) ?6 ~- T# w0 D- ~. [$ R
- Return the mantissa and exponent of x, as pair (m, e)., u/ ]0 C. k. Y8 S8 ]
- m is a float and e is an int, such that x = m * 2.**e.$ x6 y7 |# S7 i; v7 J# P
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
s, H) V# M6 @1 H5 N0 [6 x8 U+ n+ P - >>> math.frexp(10)
( S+ O7 u: U/ G4 A( A - (0.625, 4)
- b3 I* I' ~+ e f - >>> math.frexp(75)
: r! B+ Y8 n% T4 [3 t* M - (0.5859375, 7)7 j C& T; }4 v+ s
- >>> math.frexp(-40)
$ G4 M3 ^3 }7 d F" T - (-0.625, 6); A" Q& z+ X+ j/ b$ h
- >>> math.frexp(-100)7 S0 H* o0 B; W9 P4 U' g
- (-0.78125, 7)
2 H$ l& z; E1 g$ } - >>> math.frexp(100)
' \1 y: w4 Q/ o - (0.78125, 7)
复制代码 ' B7 ~* ]+ W$ j* b0 q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
( \$ R$ a8 H! J) L) m; b- #对迭代器里的每个元素进行求和操作) K; s" q( Q+ g/ d* j
- fsum(iterable)3 x- g& R' J) F# c* ^ i0 X; P/ b
- Return an accurate floating point sum of values in the iterable.5 p+ V" y m) }: [6 |* c; E' O
- Assumes IEEE-754 floating point arithmetic.( U! _& Z w) L
- >>> math.fsum([1,2,3,4])
8 y* R: g" @5 G4 p' o- c9 G; ? - 10.0
( i, B5 _5 j9 I9 S - >>> math.fsum((1,2,3,4))
6 \5 \) F% o0 r v - 10.04 V! X! K- G; v8 D3 [* j
- >>> math.fsum((-1,-2,-3,-4))- H. D+ s6 X; }4 P! d7 z
- -10.0
4 c9 A9 M7 [ R4 R% G2 l - >>> math.fsum([-1,-2,-3,-4])3 a" j: V+ x* g7 u2 v) W0 N
- -10.0
复制代码
8 o2 R- ]+ n4 V/ \" ?' @( T8 Smath.gcd(x,y) 返回x和y的最大公约数! E2 w% f7 C4 q& T& r( G" `
- #返回x和y的最大公约数
Q- o9 A& J# c - gcd(x, y) -> int2 |3 j2 c3 D5 w+ ~4 M
- greatest common divisor of x and y
3 K4 l% e3 b" z2 c- @ - >>> math.gcd(8,6)
5 a+ y; m8 x# \/ b8 T5 U5 d - 2- s/ D" E5 [5 ? z. ^( p
- >>> math.gcd(40,20): ]( ]6 c$ }% D" n: }; d$ l
- 20* y7 J; u5 @) m# \( }
- >>> math.gcd(8,12)4 \( d. ]; H Q7 V0 R- H
- 4
复制代码
) s; k# W# b8 ]math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False" c3 F: S9 { E7 ~5 |
- #得到(x**2+y**2),平方的值2 P! |5 A- {: \1 x: {9 }
- hypot(x, y)/ O$ I% @7 C5 U3 m! Z- T
- Return the Euclidean distance, sqrt(x*x + y*y).
0 h5 o+ P- Q' E0 G/ {7 @( S - >>> math.hypot(3,4)" d8 Q- Z; I$ R1 _
- 5.0
& t, Q$ B9 }9 d5 B - >>> math.hypot(6,8)$ _0 O' j" ] S+ d; R6 f5 \5 g
- 10.0
复制代码 8 S1 _2 n/ E8 h. r* K3 G# _
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False; ]+ f6 K& ]+ T' }. e7 W
- #如果x是不是无穷大的数字,则返回True,否则返回False9 Q+ i) E9 Q4 g' L7 `
- isfinite(x) -> bool6 D. l" D3 l" M: |
- Return True if x is neither an infinity nor a NaN, and False otherwise.! I2 D7 e- r1 k( i
- >>> math.isfinite(100)
2 p; z% z) E W6 r* ^" @4 z; B2 ^ - True
' u/ [3 V4 ^) b0 |3 d- } - >>> math.isfinite(0)$ U( f8 Z- H7 W) ]9 f
- True
2 g5 F6 U# N c; v U - >>> math.isfinite(0.1)
; I& b: L# x/ F/ G - True
J3 V) B5 c: ?& a - >>> math.isfinite("a")6 `! I4 \# |" |6 F
- >>> math.isfinite(0.0001)
' B: u& l c; z/ f# T" o - True
复制代码
+ }& {8 U! Y) ]: a2 ~5 A- m% [+ omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False/ a! Q3 S) T- F4 F# T! y9 `7 O. w: M
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ R/ s; t/ O8 C- H0 L* R - isinf(x) -> bool
; D# ~: J2 V. w ^ S3 {$ e. r; G - Return True if x is a positive or negative infinity, and False otherwise.4 t# I8 n% M6 U- J+ B$ k
- >>> math.isinf(234)
" q+ t8 ]( t- p9 q$ X - False! a: d+ H$ n8 t% y
- >>> math.isinf(0.1)
6 \+ U, a# G0 J Z/ U- B - False
复制代码
" k" G+ e" e# J, l8 Tmath.isnan(x) 如果x不是数字True,否则返回False
' Z6 R5 F) L& I$ K$ _; `4 F- #如果x不是数字True,否则返回False
k0 W% @) B) E- i - isnan(x) -> bool/ e' W! c4 F" p# Y, j
- Return True if x is a NaN (not a number), and False otherwise.
6 I% E7 w$ o) @. J, F - >>> math.isnan(23)( A/ _1 z$ T9 \5 `- h
- False$ l+ H6 k0 q! I5 _
- >>> math.isnan(0.01)0 g: ]6 `9 I7 C! a% K& L& M3 Q
- False
复制代码 " ^+ w) A6 N9 X5 I
math.ldexp(x,i) 返回x*(2**i)的值
( d6 I8 Z6 h. @' h& o I- x- #返回x*(2**i)的值
& m/ d |5 H0 f& c. `1 ]4 p - ldexp(x, i). u/ F* `" U- }0 K" Z
- Return x * (2**i).
/ v" T p# \) q' X% W1 j - >>> math.ldexp(5,5)- b0 U# q F& U+ k; q
- 160.0
3 H3 U, c* Q/ |4 V- I# ` - >>> math.ldexp(3,5)
/ T- `% n- [6 q/ v* u - 96.0
复制代码 3 ^/ u8 W) X: i' J: j
math.log10(x) 返回x的以10为底的对数
4 j' i C2 d9 y- j4 A* N- #返回x的以10为底的对数: _: g F/ v9 D, {- ?+ X
- log10(x)
2 D4 l9 S. p6 i8 d - Return the base 10 logarithm of x.
! \, Y: I& \! {& Z' R+ E5 h1 J: S - >>> math.log10(10)# M* R! K: y( b' F# R
- 1.04 { q. ?+ Y0 y$ |
- >>> math.log10(100)/ P! D* n7 a' x# }% e
- 2.0/ {5 M. ?0 j0 s
- #即10的1.3次方的结果为20
2 `% }, ^7 O0 ~ - >>> math.log10(20)
' t$ Z* |1 G) |- i9 Z5 T6 o' X - 1.3010299956639813
复制代码
1 J( Z8 S1 c, W0 S# T% A; fmath.log1p(x) 返回x+1的自然对数(基数为e)的值. H. K1 D7 T& O z- R
- #返回x+1的自然对数(基数为e)的值
6 x5 D% s: K8 f, ]1 {$ ` - log1p(x)
' r, r/ S1 m6 f2 I8 m; v - Return the natural logarithm of 1+x (base e).
% j6 |2 _1 `$ X/ q* C6 ?. g) L: V7 b' N - The result is computed in a way which is accurate for x near zero.
! T l2 `0 G; ^- R5 f - >>> math.log(10)7 s- U8 W! D6 a5 v9 K6 d
- 2.302585092994046
& D% x2 c) Y+ G1 R* H - >>> math.log1p(10)9 Z- U2 t. g! K" s$ o5 g5 `+ C2 J
- 2.39789527279837074 l* E$ @5 d4 Q; B1 O! n) [
- >>> math.log(11)% s3 a, v/ U1 g% @ {
- 2.3978952727983707
复制代码
: i$ C0 x! b7 \+ v& Q, R' Qmath.log2(x) 返回x的基2对数
+ \' N: w1 i* `/ H" J3 v h5 J5 c; T/ c- #返回x的基2对数0 W' |) E4 c/ t
- log2(x) y* ~$ Z& @) t( o8 Z# H; x
- Return the base 2 logarithm of x.
# N5 U9 j3 I6 Z$ L: h3 T - >>> math.log2(32): X7 |+ X: N1 D
- 5.0
" C/ o5 A( k* b ^% x( v - >>> math.log2(20)
, ]! G |7 ?7 A - 4.321928094887363
1 G! r% v, s1 Y# M1 G- _ - >>> math.log2(16)7 F& I- k3 t- p
- 4.0
复制代码 ; n' i+ _7 m% G' s
math.modf(x) 返回由x的小数部分和整数部分组成的元组. g+ C3 s2 V1 s8 J
- #返回由x的小数部分和整数部分组成的元组8 \3 T7 s" S! p e8 c) J
- modf(x) G# l4 T% |+ h; u% p% y$ l
- Return the fractional and integer parts of x. Both results carry the sign& |, w5 E0 Z- D# h$ A0 ]2 }) x
- of x and are floats.
# d; r' |2 n% L - >>> math.modf(math.pi). ]! a) k9 e! u4 W5 @" R9 Z
- (0.14159265358979312, 3.0)8 V0 o4 ^) y! x# k9 |( e4 V- c
- >>> math.modf(12.34)
; x# f- I- w1 c6 ]3 y - (0.33999999999999986, 12.0)
复制代码 2 u7 ]) T" P$ n* ^% ?
math.sqrt(x) 求x的平方根6 z; z9 a# a1 J
- #求x的平方根* _; ^! ]6 ]' Y, `
- sqrt(x)3 ^1 p4 O6 H' x: T0 z
- Return the square root of x.( F. k' b* y2 [5 {/ G. [( b( a+ I
- >>> math.sqrt(100)
$ Z" L$ U0 k8 F- g) C) b - 10.01 }9 [0 ^4 e: @3 m8 ^
- >>> math.sqrt(16)/ p. Q% S: e' Z q9 h
- 4.01 r9 j, H/ }( ?/ N9 ~- K( `, c
- >>> math.sqrt(20)
9 c' W- @# `- B. ^' }: I- Q; Y! ~ - 4.47213595499958
复制代码
. p H# x M3 a& Jmath.trunc(x) 返回x的整数部分- #返回x的整数部分( G- B" w0 k0 D$ Y* w0 P: W
- trunc(x:Real) -> Integral0 ^. S, v1 J" g0 c
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: o4 e- _: U4 V# b5 [9 O
- >>> math.trunc(6.789)
9 _8 f% t% t% O% b6 z+ R - 6
+ r& v$ @! E- T8 Q. d; N# z - >>> math.trunc(math.pi)
3 i6 q! _. N9 A2 u i U& s - 3) Q4 E# Q0 Q' X8 @
- >>> math.trunc(2.567)2 H+ H, U- N/ J; r1 V
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|