马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! _' K4 Q3 M4 b& y+ P
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 `0 i1 I6 N9 z
1 n; e L( }4 a9 @* a方法1:; d7 V& ~- ?4 @8 w8 @
- >>> import math
9 L( C/ ^2 G/ L8 y - >>> math.sqrt(9)8 s8 Q% s; P* c S5 x
- 3.0
复制代码 方法2:# ?! G+ A1 ~6 s# l/ z
- >>> from math import sqrt+ i1 F/ N( R" E: H7 S/ C( N
- >>> sqrt(9)! p; C! A- |8 A. |4 v" E
- 3.0
复制代码
' o3 O- _# l* W3 V3 R! T& x
1 k; Q5 Q4 H4 e; Y1 ?math.e 表示一个常量
2 c" m7 u ] i" X! n. C" k- #表示一个常量4 ~* m$ O8 u' ?, K; A$ c0 v
- >>> math.e
' g! P6 y# m$ p+ ~( Z/ I! `$ u' V. h - 2.718281828459045
复制代码 " L& |' E0 L3 q2 F0 U
math.pi 数字常量,圆周率
+ V, x; }) G0 A0 d( N- #数字常量,圆周率% O( O) |$ ?9 {4 E% q
- >>> print(math.pi)
e# c6 X8 W0 ]7 P4 U+ B - 3.141592653589793
复制代码 ( x2 w$ z% n$ |8 ^+ p" Z: Z
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! F) l( m/ z+ q2 G& T$ O- #取大于等于x的最小的整数值,如果x是一个整数,则返回x- z5 | o9 W/ z# q1 v0 I/ t
- ceil(x)- M1 b8 g% W0 o/ ^
- Return the ceiling of x as an int.
+ ]- K; K e& ^* K3 Y% Z - This is the smallest integral value >= x., _! y8 R2 }) N" i b; ~
# c2 w' K9 v3 g3 G2 y- >>> math.ceil(4.01)6 K/ r4 C* u9 O7 r
- 5: \# H5 F2 Z2 U2 r3 d
- >>> math.ceil(4.99)3 r: A1 e3 m* Z4 _5 i, x* Q+ s
- 5$ D6 i6 }2 l* E
- >>> math.ceil(-3.99); f" Q2 m2 X( b: w9 D$ G' r* M; R
- -3
3 Z- l3 e$ }! z+ L/ _$ ~8 } - >>> math.ceil(-3.01)
& D) m% X! t& O - -3
复制代码 * S3 A, D7 S% L/ {
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" o7 Z0 k: v) O0 D! U- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* P# d' w2 T0 J+ {3 V
- floor(x)4 G2 {( u7 L, b! \& C0 x
- Return the floor of x as an int.
" u* ~! N" o3 b - This is the largest integral value <= x.
# O6 k) P- P: o - >>> math.floor(4.1)
. G7 ?7 g! a8 P7 I1 z - 48 D# S4 B/ B# } P; C3 ~/ A
- >>> math.floor(4.999)- u- Q4 M3 D1 x; e/ w
- 4
3 S0 t' t7 ]# S" Y - >>> math.floor(-4.999). a2 Y! o# M, G
- -5- Y4 G6 t: i. X/ ]1 E, `: C
- >>> math.floor(-4.01)
: C' x9 [% o$ k - -5
复制代码
* I5 p1 x/ H7 Nmath.pow(x,y) 返回x的y次方,即x**y3 _' H& A+ Q$ [/ p, N
- #返回x的y次方,即x**y
9 ]* S8 b, |* q/ R. O4 {* X9 F1 A4 G - pow(x, y)
1 F8 j4 d- `3 U- {) i - Return x**y (x to the power of y).: F B' P3 v- R7 w8 f4 ]! }; M6 g
- >>> math.pow(3,4)
3 n) t$ j% S* q5 E5 r- V - 81.0
9 Q3 i4 b2 U$ {& [* r7 H - >>> / E8 S) s' w1 q+ e: R1 |! m
- >>> math.pow(2,7). x N; r9 G8 `
- 128.0
复制代码 8 Y, I3 V9 m) _. ], a. I
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% U7 ~) Q9 U: v- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* _6 n. z) A( n
- log(x[, base])$ h0 j5 n/ ?4 @2 z0 L; k$ t
- Return the logarithm of x to the given base., P4 c. n) b( r/ Y6 `: @- W( j
- If the base not specified, returns the natural logarithm (base e) of x.
/ C$ }3 n* `% O& E* D3 L! q- g - >>> math.log(10)/ S! f2 d3 c) I9 T6 {1 v; q3 ^
- 2.302585092994046' F4 V7 G7 a6 e0 @5 t6 }8 b
- >>> math.log(11)9 J& u+ ]/ d0 S0 n
- 2.3978952727983707" q: I& S, u. J. b' r& _5 Q6 |$ j
- >>> math.log(20)4 V. a- O* s9 p4 T2 F
- 2.995732273553991
复制代码 5 y$ K O# u7 j5 k: x
math.sin(x) 求x(x为弧度)的正弦值
1 N/ @+ s$ b+ j8 G$ E6 ^7 d- #求x(x为弧度)的正弦值- Q O9 C4 H0 X
- sin(x); j' ^4 q! u9 w; A' W. A
- Return the sine of x (measured in radians)./ U5 M3 v$ D& k- F* c- L0 g$ W
- >>> math.sin(math.pi/4)) q7 _7 }- N0 U+ i5 m1 H# J
- 0.7071067811865475
& s& D9 V* \( W4 h' l; g - >>> math.sin(math.pi/2) w1 ^& L- n. Z& F/ \0 B
- 1.0
9 b3 _( `! D! _7 l2 ^: R7 u - >>> math.sin(math.pi/3)* o7 `0 G6 k& B- E: K5 M
- 0.8660254037844386
复制代码
$ d: w5 u6 E& \! cmath.cos(x) 求x的余弦,x必须是弧度
3 c% P; Z& q; Y0 J9 I" w6 O# A- #求x的余弦,x必须是弧度: x& R8 ]6 E1 v4 Q1 \
- cos(x)
' z1 `: p- Y" `; \1 p - Return the cosine of x (measured in radians).
7 |. A' n4 X2 v2 ?3 K - #math.pi/4表示弧度,转换成角度为45度) B7 {2 c; L" K- w" r
- >>> math.cos(math.pi/4)) Z4 [7 Y2 N7 F; v, K
- 0.7071067811865476
% S p' \3 X. d) U$ l8 F - math.pi/3表示弧度,转换成角度为60度 _% @4 ^, E8 S& E# |! ?
- >>> math.cos(math.pi/3)" p5 s6 G k% I8 W7 O4 U
- 0.5000000000000001) `1 d1 k; n; Y
- math.pi/6表示弧度,转换成角度为30度& L D" v8 _+ a& H4 R h$ n/ C
- >>> math.cos(math.pi/6)* Y2 a' I' ?7 \" t- S7 }
- 0.8660254037844387
复制代码 , y& g! M; D2 w
math.tan(x) 返回x(x为弧度)的正切值3 D3 c% ]) D. o) R: l
- #返回x(x为弧度)的正切值% m: e' W7 e7 |8 ?
- tan(x)9 ?+ V u' ^! _
- Return the tangent of x (measured in radians).
$ ^7 Q; f5 {2 h - >>> math.tan(math.pi/4)
; e# j, J# }3 U: O8 _$ A* p - 0.9999999999999999
% [7 G& L0 Y; s" E - >>> math.tan(math.pi/6)5 `1 t9 [' }% R3 t" J9 j
- 0.5773502691896257
& V+ j9 s0 b. h Q3 a% h: m - >>> math.tan(math.pi/3); R4 S/ K/ p5 ?) b$ n! B8 w
- 1.7320508075688767
复制代码
. i2 N" {9 r! @3 ]" T9 } }9 s! Hmath.degrees(x) 把x从弧度转换成角度' A' r; l2 D2 b
- #把x从弧度转换成角度
+ l, ^8 }; ]+ ]* U - degrees(x)8 d6 O. t4 L0 L) N0 d- D% U
- Convert angle x from radians to degrees.
5 Y3 i U6 d! X; f
# I5 T% j: i$ g$ m- >>> math.degrees(math.pi/4)" X% r& p0 A. x7 ?! }" u3 B: T& d
- 45.0
6 u2 u$ H# N8 A& H' c - >>> math.degrees(math.pi); {6 p( R' \ L' j3 ]! Z
- 180.09 n4 J; x6 a$ q0 F2 I5 N
- >>> math.degrees(math.pi/6)
$ O# w; `8 y; k+ @" U$ T2 { - 29.999999999999996
2 D0 D" G! T+ j: r - >>> math.degrees(math.pi/3): x# _' \) ]( Q F# H
- 59.99999999999999
复制代码
; L3 h j) H$ d3 ?) mmath.radians(x) 把角度x转换成弧度& q% [1 m, o1 s
- #把角度x转换成弧度
2 F" s1 a$ C+ @- L& H6 G0 b - radians(x)
& @; T% F& P# H4 `8 \) ]5 V7 v1 P - Convert angle x from degrees to radians.0 `( H+ |1 ~8 Z x/ N. m$ K! t. E
- >>> math.radians(45)8 d( r7 V9 C7 h4 |" f1 \6 c. Y0 P
- 0.78539816339744836 c( h8 B& j& K0 ^
- >>> math.radians(60)# I+ B" }1 B- k; F; j5 @
- 1.0471975511965976
复制代码
. }3 k) ?8 I1 Hmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
" v- P h2 ]1 A: L- #把y的正负号加到x前面,可以使用0
# z! E$ X$ K2 q - copysign(x, y)
# ]9 v" G: R! T8 l - Return a float with the magnitude (absolute value) of x but the sign $ F7 G& i+ w; C5 k. k0 [
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
; A1 q; L$ q# n0 j0 m - returns -1.0.
6 G9 b3 c1 ~+ b( F" I0 P* P
y7 [- n \8 |8 X+ O- >>> math.copysign(2,3)! L6 d* Q8 e2 g4 o
- 2.0& S6 a& B/ d8 i0 P) y3 ?3 f
- >>> math.copysign(2,-3)
, f) D+ B2 N4 @$ |+ M& @ - -2.0; b$ Q9 \' R' c1 T! f) @
- >>> math.copysign(3,8)
9 b @$ M, f: a! e& t9 Y, p - 3.0
+ {. l- D0 ?# P2 d* e3 a - >>> math.copysign(3,-8)# @5 J" a( ^/ {" T
- -3.0
复制代码
# B9 J8 C- E( K M( |/ T0 o% ]7 Fmath.exp(x) 返回math.e,也就是2.71828的x次方9 D8 t1 P( k! B" E. v P; h
- #返回math.e,也就是2.71828的x次方
$ K# l7 c6 j" @$ {' B M - exp(x)
2 i: D4 C' y6 X0 l S - Return e raised to the power of x.
+ ^/ D* M5 {2 }7 V; F/ [ J* i
5 g+ n3 b5 j% l- B! G& ?- >>> math.exp(1)% R8 }) ~% |1 l' z
- 2.718281828459045
: S, O8 K/ v+ o) ~& F - >>> math.exp(2). U; z) R4 @ E: E/ v
- 7.38905609893065) f* K, {, [# `( ]8 @$ R3 e6 v5 f
- >>> math.exp(3)1 s3 b2 c, o, `: k
- 20.085536923187668
复制代码
6 Y* E4 l+ V- ^8 X# _$ H$ ~math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) ~# ?3 b3 O1 ]. G$ ]
- #返回math.e的x(其值为2.71828)次方的值减1: B9 W* k# \) f" ^0 x$ K P- Y
- expm1(x); L' O8 Q$ \2 I/ ^
- Return exp(x)-1.
" T4 l' r% p4 o/ m+ D0 n, n - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.9 ?1 b u3 x% A7 F5 }( h& n2 c' |- U) k
- 3 ?# D( M) R T6 q
- >>> math.expm1(1)
8 U# d- H1 G7 Z- C - 1.7182818284590457 l" e5 @% i' Z% _3 n3 z# y7 p6 v
- >>> math.expm1(2)
0 Y; T, ?4 T" R, e/ ~1 K - 6.38905609893065! ?* L0 U) @% ]8 p
- >>> math.expm1(3)
+ n0 m, ^2 a; q1 a" E& F; t - 19.085536923187668
复制代码 9 v. K, u$ g2 R. W0 p, A% W- h
math.fabs(x) 返回x的绝对值+ I! _; E% a; r( ]
- #返回x的绝对值. K- [6 |' F& u! ]. x3 i! t% P; I! }- s
- fabs(x)
4 ^) s9 ]' j. H2 v, }% V1 T - Return the absolute value of the float x.1 U, o" ]/ v; @% F$ S. {% o+ I, g
- : U2 K* t- r9 t, T7 e; ]
- >>> math.fabs(-0.003)
! x5 V! ~9 q" T% J - 0.003
s9 o' W |! e - >>> math.fabs(-110); ~3 r6 `. K5 f6 A" G2 {% S; [0 F5 N& t
- 110.0! n1 J* J+ @* m
- >>> math.fabs(100)
4 f! d" t/ v6 z1 Q( N3 F' B - 100.0
复制代码
& W; j- O0 E; H* E1 t* @math.factorial(x) 取x的阶乘的值0 _3 Q, t/ z& {* O4 D+ v8 Q
- #取x的阶乘的值1 p# p# Z& r# P. ^3 j! T3 S. d7 O. f
- factorial(x) -> Integral3 D* Z; U) v% c9 F% `2 c0 c$ w
- Find x!. Raise a ValueError if x is negative or non-integral.
4 R: y) }0 r) |' f# t - >>> math.factorial(1)
' @1 ~+ m2 N4 _5 ~( k - 1
7 y8 \3 ]; W9 T- ]! v - >>> math.factorial(2)8 L4 `2 W3 S; Y, }
- 2
: O2 V; C. Y. ^8 w6 X( p( k - >>> math.factorial(3)
( x& h. n. c2 {5 S - 69 n1 Q$ U- j6 d2 \ X2 \
- >>> math.factorial(5)* A" X' u" M" Y: @; ]& ^) y/ }
- 120. W6 A8 ? `. c6 ~! s. T
- >>> math.factorial(10)! ? C N2 p3 `: u
- 3628800
复制代码 $ H, b& t7 A0 @$ O9 y; k3 H
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 M- H8 Y6 Y- l5 G
- #得到x/y的余数,其值是一个浮点数! F) A$ C; M4 [1 Y, m' P
- fmod(x, y): n" k1 p, R8 \" u3 S) H
- Return fmod(x, y), according to platform C. x % y may differ.
! A, {- d$ f* x4 l: u& a2 { - >>> math.fmod(20,3)
( I4 h( O# J' T: \; k2 i - 2.0
8 E# J4 P8 F/ M5 @) P - >>> math.fmod(20,7)
6 [' v7 a Z% a K. w: q - 6.0
复制代码
0 v$ p, E+ i: ]" K( M# U2 C9 P8 fmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 S) p, i9 r/ ^* B2 \( ~
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 N3 A) s4 A, w0 S, S W; O3 O
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值$ P0 Q/ ?6 w# k4 B, M; U1 H
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
1 g/ _. C3 Q8 [- S! A - frexp(x)! }: d2 t% ^% A5 X5 D
- Return the mantissa and exponent of x, as pair (m, e).
- u, A' J2 {% |4 X5 O - m is a float and e is an int, such that x = m * 2.**e.! C8 f8 P" d! D: Y6 ^( w$ k. F2 ]
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
* f; E. r2 R5 j, z- Z - >>> math.frexp(10)
) E" M. s2 Y# g3 y6 T - (0.625, 4)# ?0 w( w& W4 D5 T1 ~: @ B$ C& W2 ~
- >>> math.frexp(75)
; H. Z1 L4 C g! e3 V& _6 F - (0.5859375, 7)9 ], V& b! |! t0 c
- >>> math.frexp(-40)
+ V4 |. ^6 ]2 H3 W0 W6 y - (-0.625, 6)3 L$ Q( @7 h5 E5 s4 n- h
- >>> math.frexp(-100)
5 E+ g$ o, s% g - (-0.78125, 7)
4 F' ~* ]3 Q, E0 x! W6 J% T4 J4 {! j - >>> math.frexp(100), t/ j) g. _. d
- (0.78125, 7)
复制代码
3 A! b+ L: H9 lmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)9 I1 x8 _( a' {% x( i
- #对迭代器里的每个元素进行求和操作
7 ^- l, O1 ]. @7 |8 y - fsum(iterable)
1 L- B' V1 u3 a# P/ \1 n - Return an accurate floating point sum of values in the iterable.
4 A3 Y9 f" p: j' L$ D - Assumes IEEE-754 floating point arithmetic.0 L ^% N4 u J2 R2 E# S! z
- >>> math.fsum([1,2,3,4])
( U3 Y3 ~3 s0 @% w' v) {: t% _ - 10.0
; K$ Z1 ]3 a3 Z) m2 D' ]! @" t/ n - >>> math.fsum((1,2,3,4))
Q) c/ V% ^% T: V - 10.0
# E' v7 |3 v0 O4 Y7 q! K0 Q - >>> math.fsum((-1,-2,-3,-4))
* a# A/ v5 [# L( p - -10.0
8 o4 ~+ L, a8 M# f% Q3 c/ G - >>> math.fsum([-1,-2,-3,-4])
3 q9 E2 b2 K8 v, n, l& E - -10.0
复制代码
! o$ Z# N# r" j6 emath.gcd(x,y) 返回x和y的最大公约数
& t4 J1 F7 {, c0 s- #返回x和y的最大公约数
. Z2 n! T! P, B; E - gcd(x, y) -> int
' O9 g ?9 Q& ~1 E4 c3 L: W - greatest common divisor of x and y# c2 j2 o( x& e& [8 Y! B* ?4 v
- >>> math.gcd(8,6), h0 }1 C, ~1 I$ Q8 h4 {
- 2
" I# v- _& T( P& ` z' ~0 O - >>> math.gcd(40,20)
0 Y" K% M/ ~# F5 b& @" k w; C - 205 v/ @# k0 G+ R. O4 Y7 ~
- >>> math.gcd(8,12)+ f1 g% [& ?+ a: d7 l# e
- 4
复制代码
8 [& s0 |1 ?( Y5 d+ {math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False! i. f' {7 B8 \% X Z) q3 s
- #得到(x**2+y**2),平方的值5 k/ n f! @4 K2 J+ ]
- hypot(x, y)
: u( w I2 z/ A! B$ k: G/ C- L! A - Return the Euclidean distance, sqrt(x*x + y*y).
4 V9 Z% k' |! h: z( u! y7 V( Z - >>> math.hypot(3,4)9 f- I1 q0 D$ G! p. r" P
- 5.0) x# ~9 B$ {. \) L7 Q( A! W
- >>> math.hypot(6,8)
: }4 e; u" O: w8 ^/ B" W - 10.0
复制代码 t9 v: n S6 ~9 L* I: D; L
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 ~0 e* S" F+ X# ?- #如果x是不是无穷大的数字,则返回True,否则返回False" U6 Y. Z* g, Y6 X
- isfinite(x) -> bool& G* {: E7 `4 M) h
- Return True if x is neither an infinity nor a NaN, and False otherwise.
, F- K9 M5 I+ f2 t - >>> math.isfinite(100)
5 [7 \4 y& W Z; n& U6 m - True+ N2 q) q5 Y" a
- >>> math.isfinite(0)1 ^* h, A: l* G; m
- True
% l! d! o+ R( ~: a2 [7 o5 X - >>> math.isfinite(0.1)
1 X; F9 a- V' s: T( w5 L1 S q - True
$ F2 h* l/ H r5 a' W - >>> math.isfinite("a")' @6 h" }- Y6 M6 T4 @* D7 @
- >>> math.isfinite(0.0001)
" V* ]" P0 Q/ h3 Q- i- K3 n0 T - True
复制代码
) K+ o( m/ w/ Q5 h! A9 X! cmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False" n5 @) z% D% M* H9 \# v# T
- #如果x是正无穷大或负无穷大,则返回True,否则返回False9 O# V# ^. n- b4 M" p) j' \
- isinf(x) -> bool3 @$ v% t* p; ]7 e L N4 I: Z
- Return True if x is a positive or negative infinity, and False otherwise., m5 G0 M/ d3 B3 V1 i7 x, X( w5 {
- >>> math.isinf(234)/ f# N; y$ w& z) o' K2 p g3 t2 C
- False
! X$ C: ~) o' u8 x9 }7 P2 [. M - >>> math.isinf(0.1)
+ y% t9 y6 p+ [/ V$ h& T - False
复制代码
7 W, m$ C5 S L* fmath.isnan(x) 如果x不是数字True,否则返回False) d8 B# q& r8 @
- #如果x不是数字True,否则返回False' L( x% T# ^( {3 z2 t
- isnan(x) -> bool8 ?# V4 X& C8 O5 n
- Return True if x is a NaN (not a number), and False otherwise.
0 z5 U8 ^0 a! h3 K( l) e - >>> math.isnan(23)
. Y5 i( L. u5 O1 M - False
: Z2 t {0 J7 [. C6 b0 x( V - >>> math.isnan(0.01)
9 @* j+ G1 T {# s0 o - False
复制代码
2 f: a/ v* Q+ Y# v u; |math.ldexp(x,i) 返回x*(2**i)的值
) M1 D: c: I4 r1 Q% q+ y. t( R# i- #返回x*(2**i)的值
; `$ L8 A; z7 p( ^ - ldexp(x, i)# c; e( Q% f) ]; v7 h- z. I& r5 u |+ y
- Return x * (2**i).
H8 [( O6 \4 ^! T0 U9 d - >>> math.ldexp(5,5)# `: G6 z* k8 r3 R H: ?
- 160.0" {5 p: f5 D. H" n4 c
- >>> math.ldexp(3,5)
; F+ f( c V# p; g0 y! f* L7 ] - 96.0
复制代码 ' ^4 R* E' d; n" M+ u7 ~5 ?
math.log10(x) 返回x的以10为底的对数
' [3 B) u! O: c7 U- #返回x的以10为底的对数- [) ^" R/ |! \$ C/ g7 T$ g. F
- log10(x)
" ^# t% e/ a; |9 h) w! d - Return the base 10 logarithm of x." U2 E% Q4 `0 U+ p) B" ]) C# h) _
- >>> math.log10(10)
) j4 t, S2 K- O8 Y! ^ - 1.0* F, M( v; F K% p
- >>> math.log10(100)0 r- ^& y: u* g ]
- 2.0+ _& G3 W2 q R8 ?2 U6 ]
- #即10的1.3次方的结果为20. z# n. c9 k8 n. x3 x6 N3 w" h7 l
- >>> math.log10(20)
D$ q8 z2 Y, L1 e# s1 i+ R7 E - 1.3010299956639813
复制代码
+ Z0 S9 Z* B* `4 i4 H* N' W( }) Kmath.log1p(x) 返回x+1的自然对数(基数为e)的值
- A' |7 b9 ~8 o* z. x- #返回x+1的自然对数(基数为e)的值
) j' o0 t6 W* h; n0 r A - log1p(x)1 J4 g9 l! e4 d! }% L3 S0 N# c
- Return the natural logarithm of 1+x (base e).# c+ ~$ e) f9 }
- The result is computed in a way which is accurate for x near zero.
4 `( E; V5 b- p ^. U8 k - >>> math.log(10)
1 {5 G4 K8 D) S. j Q6 p9 J - 2.302585092994046" z: D6 _ M7 T- `; l, F" \6 U
- >>> math.log1p(10)
7 z+ q Y! q, d$ J% h \ - 2.3978952727983707/ _8 D! Z) n" F1 p4 V7 ^
- >>> math.log(11)
( I6 R2 M# N$ f - 2.3978952727983707
复制代码 ; u! [" W4 p/ i4 v2 j/ j8 t
math.log2(x) 返回x的基2对数" ~( ]0 s# S' ?
- #返回x的基2对数
* B8 M3 g d% u$ [4 g - log2(x)& t( F9 J& t, U! S
- Return the base 2 logarithm of x.9 T1 d& Z, G- {$ s
- >>> math.log2(32)
$ z1 Y0 |9 |+ R( n - 5.0" H. `- w" y' B# o( L# D4 p6 J- Y: H
- >>> math.log2(20) w! d9 `- K! c, d- Z
- 4.321928094887363
. Y" M5 ?* I3 i! q$ m8 s6 C - >>> math.log2(16). E" m7 u6 B: R: P
- 4.0
复制代码 ( e' E0 X8 G. t$ ~, e5 _ }: W
math.modf(x) 返回由x的小数部分和整数部分组成的元组
( ?6 ~' }/ _1 }4 ~& M/ \. o2 J- #返回由x的小数部分和整数部分组成的元组
& j$ v `3 F; Y+ E7 o5 k - modf(x)
# [2 F. S' w, O7 w& _ - Return the fractional and integer parts of x. Both results carry the sign
/ \+ }8 m' \( S1 k5 O( w* | - of x and are floats.
! F( S. l; s, h4 N/ t4 y6 _4 _ - >>> math.modf(math.pi)
0 Z% S7 X5 a p - (0.14159265358979312, 3.0)% @+ L$ p5 f! F6 P( A
- >>> math.modf(12.34)
; K% h0 J2 \ Y, S! k( i - (0.33999999999999986, 12.0)
复制代码
3 k. f3 M# u K1 i& ?$ nmath.sqrt(x) 求x的平方根) b' z$ V4 Q1 {/ F+ \1 ` O
- #求x的平方根
7 q |1 P, X# ~2 v8 Z - sqrt(x)
+ n6 k; A( f I* p! f - Return the square root of x." t! k& L. b' `0 g5 `" f
- >>> math.sqrt(100)
: E9 ^. M2 L7 C+ Q- Z3 U0 w - 10.0, f0 U& }; K3 Y# D$ }: K2 T
- >>> math.sqrt(16)$ s k. `+ E6 D \; f0 m+ `) @" ~
- 4.0- X" [; \- Y6 x& d* O* ~
- >>> math.sqrt(20) U* I4 _- r. \; n
- 4.47213595499958
复制代码
! l: d" D, D8 T2 x' emath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 B6 M+ W: U( P - trunc(x:Real) -> Integral, c) P# S2 \1 `: N* |
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 m+ ]; k: d7 B( A s
- >>> math.trunc(6.789). |' q. Q: W2 o0 c0 ?$ i3 y' T6 G
- 61 p4 f+ \0 L0 l2 w6 p: h
- >>> math.trunc(math.pi)
( ~4 U. u. n6 B u - 30 s% s% {( {3 `7 V. b
- >>> math.trunc(2.567)
* s$ p) I/ V0 r1 ~: M - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|