马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
& ^) \6 Y0 [) o+ U0 s" R9 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
: d' t I) Z, ^7 D. Z( O+ L/ g2 E
8 D" G2 g1 G+ T! I, W: _% J方法1:5 x( i5 u9 ^0 i0 I. Q$ t) Q) c7 s
- >>> import math; }+ S, [* p6 ~5 o
- >>> math.sqrt(9)
2 K. Q8 h, K2 W/ O5 x' |3 Y. O' T - 3.0
复制代码 方法2:* Q6 {8 _ p3 N, r5 y# ]) {/ q/ E
- >>> from math import sqrt
; r# ~% k' m9 z' N# j. Y& V - >>> sqrt(9)3 A% G) y! U. K- V; i
- 3.0
复制代码 2 E; \! x; M! f, U
: W" a$ F- S) Z. u
math.e 表示一个常量
+ L" [. m1 `4 z! \7 |7 D) V- #表示一个常量
5 a3 F9 \+ s- Y. c. Q5 }- c2 c - >>> math.e- }* } U/ v9 O
- 2.718281828459045
复制代码
& x, B E5 W- a, Bmath.pi 数字常量,圆周率
9 B" P: `6 J9 Y% _0 U6 I- #数字常量,圆周率0 R A, H8 |% I
- >>> print(math.pi)
# k& j6 I& N+ H6 L" ^" T/ V - 3.141592653589793
复制代码
( D3 Q- m5 w6 ^# _! |9 Z& c( z. Mmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x: J* Z; c) W$ n0 G; z# O2 w
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 J, u+ e9 b# l
- ceil(x)0 U& N* X8 E/ ~. {+ b
- Return the ceiling of x as an int.: g1 V$ k+ f% W
- This is the smallest integral value >= x.
! K' P, q3 e. O$ x! n5 x2 } - 8 v' k0 z3 X8 S9 C/ X! t( |
- >>> math.ceil(4.01)
; ?2 k, R+ v$ k+ U - 5
( o6 n% P/ R! x( H) |( P - >>> math.ceil(4.99)
2 [1 A9 u+ H9 }% {, w - 5+ t3 |3 U g+ N4 {. ^1 n
- >>> math.ceil(-3.99)/ H, T1 E$ Y% q4 q4 Q1 a5 ^/ P$ Q7 B
- -3
! R. _: V: a' j% X% ? - >>> math.ceil(-3.01)
6 e% e. X0 L, ^- G' M# H2 Z - -3
复制代码 9 }4 G0 o& }' R* d9 v% L! P
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身% g7 u8 @# f* @& ^* S `
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身, ~, l* Z" Z+ A
- floor(x)
& j z4 I- Z, h" X6 y0 f - Return the floor of x as an int.3 n+ C; \/ N7 L7 V7 R
- This is the largest integral value <= x. X4 G4 C- L0 s
- >>> math.floor(4.1). N0 X$ K% b! M+ w# T7 @
- 44 N- R9 K' Y: F7 R* Y/ J t# Q
- >>> math.floor(4.999)4 N, e, a% G5 n; `5 D' D
- 4! G2 `( _. K$ p, k
- >>> math.floor(-4.999)2 L7 M7 g2 v4 o! y# g/ |
- -5* u) ?# W8 c/ L5 z& d. v9 O) J' S
- >>> math.floor(-4.01)
- P" ^+ u- {# @; x7 h5 a - -5
复制代码 2 N" P* J( G% V/ e" }9 ?4 n
math.pow(x,y) 返回x的y次方,即x**y7 \! y2 ^6 F8 W9 W u1 X
- #返回x的y次方,即x**y
' |" |; |5 A8 {1 T8 v+ C - pow(x, y)
+ G/ ^: K2 s3 I( Z- s. M4 s1 [& w - Return x**y (x to the power of y).
3 ]% q8 @1 Z6 u/ ^) } - >>> math.pow(3,4)
7 e+ E* L4 E c* N, p% q( }; w - 81.03 n# d. h: i: l+ N! Q; Y
- >>>
, l8 I: f2 D) A' R - >>> math.pow(2,7); d7 Z. W# l' @% H- n
- 128.0
复制代码 : H' y! ?' y% m
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' ]) i: c: C6 a5 O, @/ K6 J; }
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( O% \. \* m% U - log(x[, base]): z" M$ `( }) a, B; r* j# Q
- Return the logarithm of x to the given base.
. n' U0 L# s+ ^4 y8 I - If the base not specified, returns the natural logarithm (base e) of x.
# U2 F1 _5 G% n. U& ~5 Z - >>> math.log(10)
6 ~- r. J9 b3 D/ C - 2.302585092994046
. b- Y a) D' J7 ` - >>> math.log(11)
( f. r [7 X2 x& |; e( C- r6 U; |1 e - 2.3978952727983707
7 I7 r. V6 o# i/ S& R \: k, n1 F - >>> math.log(20)* y# D( e! r+ k$ K7 R( e9 X
- 2.995732273553991
复制代码
- a) N0 \) k0 F2 y; r$ ]! Mmath.sin(x) 求x(x为弧度)的正弦值( m: x- L( y* M- ?
- #求x(x为弧度)的正弦值
. b* A8 v% t7 M4 x ?8 v+ n. ` - sin(x)& K8 ]5 I* h- `) X) p
- Return the sine of x (measured in radians).& u6 Z1 Z' H. _6 N: ^* q0 B
- >>> math.sin(math.pi/4)/ v% i+ p# H" }; N: t
- 0.7071067811865475
" a7 U, s( Z1 \: n. T& y - >>> math.sin(math.pi/2)2 b; o3 i: _9 M' A( L4 l T
- 1.0
`' {' ]" C$ A7 Z- B. D - >>> math.sin(math.pi/3)
' k/ |2 v V, s6 F" { s4 L - 0.8660254037844386
复制代码
- U4 [7 t% w1 |0 |math.cos(x) 求x的余弦,x必须是弧度
; {; _+ L( L/ P% u- #求x的余弦,x必须是弧度
4 }* w/ i% d8 X g - cos(x)' ]+ ]* x2 b8 q/ L
- Return the cosine of x (measured in radians).- [# `3 ? L2 `* K& W' u) E' |: a
- #math.pi/4表示弧度,转换成角度为45度, o9 V5 t0 l; G% X) ^0 v% E
- >>> math.cos(math.pi/4)
8 m8 c$ P) |( A( N# m( {6 k - 0.70710678118654767 ?; d" z3 w9 w0 y. ~
- math.pi/3表示弧度,转换成角度为60度
4 {. T2 _0 {* N/ m) C: `9 D0 u - >>> math.cos(math.pi/3)
, a+ H7 o) C% l! Q$ B - 0.5000000000000001
7 d; w8 c. Q( i8 p, R( } - math.pi/6表示弧度,转换成角度为30度
! X4 a7 b8 F3 I6 J6 B5 ~ - >>> math.cos(math.pi/6)7 J3 J8 M9 n" O. H0 y2 T4 ~
- 0.8660254037844387
复制代码
7 c/ G, ~* ^; B# Q1 ]* y3 d: Cmath.tan(x) 返回x(x为弧度)的正切值
. Q: A. b" D3 v9 D' G6 T7 Y& |* G- #返回x(x为弧度)的正切值0 _5 s+ L. m. X3 K
- tan(x)$ K2 K4 b2 y$ e2 S& M' n7 ?
- Return the tangent of x (measured in radians).
- @3 e! u0 D$ g5 r5 d% Q - >>> math.tan(math.pi/4)
# j, L5 z! D. m" G2 W! o - 0.99999999999999993 ?, o) ]" t" L- K+ w
- >>> math.tan(math.pi/6)
" \8 l. [+ V5 I$ Y: P6 J - 0.57735026918962572 s( W4 R$ x& M5 t; B8 }, z
- >>> math.tan(math.pi/3)
; j- }! S# w7 S6 U8 `6 B2 V8 l/ E - 1.7320508075688767
复制代码
- ~. l% ?- M5 X' ^math.degrees(x) 把x从弧度转换成角度 s# H( M8 L# G' P
- #把x从弧度转换成角度# ?! E: V1 M( [3 A; {# v3 _
- degrees(x). L6 N C3 r" t) Z' \5 I
- Convert angle x from radians to degrees.
/ b' }; s6 y, g/ U1 [/ x
0 T/ T; ^, ~$ [7 S/ k" a, W9 @- >>> math.degrees(math.pi/4)
& u# i6 u/ g v+ z2 e2 O! w - 45.0
# B, ]+ c# v4 x& `4 K" x; J - >>> math.degrees(math.pi)
; F4 E. b& p- u: c& U z4 b8 O - 180.0% x2 E/ p5 V) W0 I0 S* A- l
- >>> math.degrees(math.pi/6)2 x Z( e1 F7 G3 ]/ `$ P$ `- L
- 29.9999999999999966 ^) [) M7 J- J t8 m
- >>> math.degrees(math.pi/3)
2 h/ C* y0 L. L1 |! |# W9 H4 ^2 C - 59.99999999999999
复制代码 ; h; y5 Z! c- s3 j0 `
math.radians(x) 把角度x转换成弧度
# t7 ~% Y( S0 l- #把角度x转换成弧度
& n+ ~/ Y* j$ W. }* Y) G9 N6 ` - radians(x)( D) B% a. @2 g! C3 C
- Convert angle x from degrees to radians.
, T9 o9 K6 X! t. ^8 I& d - >>> math.radians(45)
" I3 g2 a8 N7 T - 0.7853981633974483
' H/ ]" R. q; ~- W9 k: o ` - >>> math.radians(60)" ?# Y- i0 E; [6 I
- 1.0471975511965976
复制代码 ' ^+ b" p5 i) W( @6 n( M( ^
math.copysign(x,y) 把y的正负号加到x前面,可以使用07 t' S! L1 m& j; K
- #把y的正负号加到x前面,可以使用0
2 i2 K8 \% b" B - copysign(x, y)2 n8 I4 G9 p) J3 h+ \" }3 b% M
- Return a float with the magnitude (absolute value) of x but the sign
0 O4 e3 [; y( R% E9 r _ - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 L; v& P0 }5 ]# p4 ^+ s3 x - returns -1.0.% `- b9 \2 ?% x* H9 ~
9 ?# J; `7 e. h9 I- M- >>> math.copysign(2,3)+ }* K$ }: L; ~
- 2.0
0 I/ Q) V. B. }% h @5 n( I7 l - >>> math.copysign(2,-3)
9 \: h- [6 R" k9 V7 L# b - -2.05 B: y# | r3 I
- >>> math.copysign(3,8)' r) g5 A9 d4 s2 R1 k
- 3.0" g* S+ j2 H: V$ E$ l
- >>> math.copysign(3,-8)
5 O6 n! b9 m0 G+ X5 K( D - -3.0
复制代码 / T4 P% T5 Q6 u/ N
math.exp(x) 返回math.e,也就是2.71828的x次方, a$ w- q* s" X- R4 c/ L% {* S
- #返回math.e,也就是2.71828的x次方6 n- V2 ^$ t! @2 d8 y+ k
- exp(x)
- c. B8 Q4 B0 G- | - Return e raised to the power of x.3 J6 A; J P! ?6 v
) E8 {; i2 R' y+ D3 U# Q- >>> math.exp(1)
- J* D$ E. u8 O9 x$ T" A/ d - 2.718281828459045
. Q# i% l- W0 G - >>> math.exp(2): b B/ G( J. j( f" K; \
- 7.38905609893065
5 V4 i2 z) S0 ?# {$ x7 k; c - >>> math.exp(3)* e% n. v6 b Q7 T4 K! c
- 20.085536923187668
复制代码 y' a! K8 d" M: C+ ?) M7 t
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1- ^1 G5 q. F5 i& u! Y
- #返回math.e的x(其值为2.71828)次方的值减1
7 x" ~& t d$ F" ` - expm1(x)
$ r* [: Z1 a J1 ^/ e$ ? - Return exp(x)-1.
9 U% ]# B* l" R - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.: N$ k# s2 U( U/ r: I
& h& [. Q& R( x- >>> math.expm1(1)7 a0 p- d& H6 R9 G% ?" Q; V8 c, b* S( q
- 1.718281828459045/ P& t0 f8 C- Q
- >>> math.expm1(2)4 p3 P" K' X8 w& `/ g
- 6.38905609893065, Z% ~7 q8 A- w! _" C
- >>> math.expm1(3)4 ]& G8 N' y! e$ w- T' F& B
- 19.085536923187668
复制代码
" g( o7 ~) K1 v- Dmath.fabs(x) 返回x的绝对值& A# q. O/ i6 @( f
- #返回x的绝对值3 H9 c9 C% Y& L) A
- fabs(x)1 N' {# T9 Y" p1 X
- Return the absolute value of the float x.9 ~4 l: c4 @, m
. V( M$ R# [9 {( z' F* p8 R4 Y, o& u- >>> math.fabs(-0.003)" |7 n! A& |* _, h, n. I" L
- 0.003) q0 R0 a# T! z( c" I4 \3 e
- >>> math.fabs(-110)
9 W) ]& n+ y% @ - 110.0
( I0 ^8 l1 c } - >>> math.fabs(100)% d: i3 R/ z) a3 A
- 100.0
复制代码
, k2 K4 l H5 a3 F7 S* S" omath.factorial(x) 取x的阶乘的值
& d) m/ a9 }: I% D( `% R+ S- #取x的阶乘的值2 G( E6 L5 i- ^% d" I; e
- factorial(x) -> Integral
2 F8 ~. m0 D/ Q - Find x!. Raise a ValueError if x is negative or non-integral.
8 q) F+ t- a p - >>> math.factorial(1)
0 B& t' F& ]* ]* F% c& n* o% i% p7 C7 O - 1
. L# M/ M' f+ A" M$ K& Y - >>> math.factorial(2)0 s' X! N. l( I3 z" U9 Y% F
- 2( t9 L2 _3 @( }6 t. F
- >>> math.factorial(3)
* v) H* w, ~5 t$ r6 E0 w+ ? - 6
# n4 a7 h+ H( |& Z' i3 I# P- @7 y) S - >>> math.factorial(5)
* n$ A' i& j1 @8 f4 Z1 d$ t - 1203 ~0 Q; Z0 U( ~
- >>> math.factorial(10)& a: R1 p9 q9 V) r5 B( Y) _
- 3628800
复制代码 * i: k% f5 o" ~# u" G
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
5 ^& Q5 w0 I$ J2 k+ E# ~- #得到x/y的余数,其值是一个浮点数
$ z+ C, U0 f" n+ ], m2 ? - fmod(x, y)
0 N% o' S8 E/ \: _3 O4 K! h - Return fmod(x, y), according to platform C. x % y may differ.- \6 t) N1 b0 c8 h: X+ e
- >>> math.fmod(20,3)
4 q1 C, W; ]$ K# D# |5 [- s. E - 2.0
* I% ~2 l! R4 @: `& K. Y - >>> math.fmod(20,7)
6 Q# V8 d: |4 E - 6.0
复制代码 . j; r% X7 ^8 D: i( C; l) @' [7 H! c
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围 Z; _# E5 X9 N/ S' | P& r
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* r$ B: x! P F' w
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' D- u" }: n- c' @3 ]0 D
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 H) q5 T. S m! [% ^; Y- J
- frexp(x)
4 ]" V6 j9 ~) d- c - Return the mantissa and exponent of x, as pair (m, e).
' |: M+ j k, _& H5 L Q - m is a float and e is an int, such that x = m * 2.**e.( N* @. | c8 b. d
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
v/ K' _, f0 f# e) h2 E/ m - >>> math.frexp(10)
( E) T3 y I, `0 m - (0.625, 4)
]0 N; Z0 j* v* l3 {: D8 O$ T - >>> math.frexp(75): p; W2 T. P4 G
- (0.5859375, 7)
- \2 l( D: s* E, W& _ - >>> math.frexp(-40)4 u5 c9 t# e! K& B
- (-0.625, 6)* n- [8 t2 t% B
- >>> math.frexp(-100)% k7 s# ]! Z4 l7 `+ E9 W+ Y" T8 J
- (-0.78125, 7)7 v5 `1 j: Y1 a( A" [3 v- e
- >>> math.frexp(100)
9 f8 a) @2 b( \) Q - (0.78125, 7)
复制代码
" J) l9 p+ _7 @0 Omath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), ]; z7 u# ^5 }! n$ o5 V( A$ ?8 ]
- #对迭代器里的每个元素进行求和操作& T& u) D" M1 i! o( T7 j9 f
- fsum(iterable)6 f2 L9 Z k8 E4 `0 K) f0 j
- Return an accurate floating point sum of values in the iterable.' d y K6 n- f2 ~& |$ z* ^
- Assumes IEEE-754 floating point arithmetic.0 }" n- C2 }& p" ~+ ^$ y
- >>> math.fsum([1,2,3,4])9 v7 M* R) S2 A# K( o' x' \- D0 D
- 10.08 o$ x" C9 h0 I6 N
- >>> math.fsum((1,2,3,4))" b. V1 H9 f8 H. Y2 G" v! e7 Q& O$ {: n
- 10.02 \* }9 l ~1 ^: |8 ]7 c) K
- >>> math.fsum((-1,-2,-3,-4))
: Y1 D& p/ i& T. X/ b. [9 \ - -10.0
6 `; n- x4 w) c - >>> math.fsum([-1,-2,-3,-4])9 T- p- G" \* s" Z. v! _4 k
- -10.0
复制代码
: Z2 M. N4 X- z) A" Emath.gcd(x,y) 返回x和y的最大公约数
- L: ?2 U( U, I" |! m- #返回x和y的最大公约数
# S0 o$ h- l; |$ c2 y9 y - gcd(x, y) -> int
* L* D5 d: `3 ]( M! P - greatest common divisor of x and y
3 c+ L! G i6 S4 I - >>> math.gcd(8,6)& p- D2 S, T. z8 y- v
- 2
0 G! U( Y0 ~1 I, C/ v - >>> math.gcd(40,20)
?- V: R2 L" g4 l - 208 {" r# x: u8 e8 J
- >>> math.gcd(8,12)6 d+ V3 T8 v4 d. s# x
- 4
复制代码 ( x) g, \; m/ b5 C9 f( d
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
, f+ M) [: t4 L: I- #得到(x**2+y**2),平方的值# |! V4 Z5 }7 D9 Y
- hypot(x, y)
. e0 w r- Y" \9 B/ X - Return the Euclidean distance, sqrt(x*x + y*y).& v8 t& j' p7 d- s/ u# k. N
- >>> math.hypot(3,4)
) R) M& B9 P. S- } { - 5.0
9 w0 w( Q1 S, G }+ t& O - >>> math.hypot(6,8). w+ k5 O/ s2 L' C6 Z
- 10.0
复制代码
; D' h4 B: U# d+ n% S$ u" E- f Tmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
4 c8 o- k- w: Q& b1 Q. Y- E- #如果x是不是无穷大的数字,则返回True,否则返回False
& ?% \8 d _- q1 t/ p - isfinite(x) -> bool- d) [; M' ~9 ?5 R) m M6 X
- Return True if x is neither an infinity nor a NaN, and False otherwise.6 C6 ^1 |) X8 k4 i
- >>> math.isfinite(100)
1 k8 B5 x' I. W ?( }5 H - True& S- |, k9 N1 [- K5 q% N
- >>> math.isfinite(0)
/ d" T/ }' a" J9 S/ ^: K! k& p4 A5 B - True
7 w* a5 Z9 X7 B# X - >>> math.isfinite(0.1)( z" j2 I- I) q2 u, w
- True
( `7 m! F$ C5 D5 q% [4 A; Z - >>> math.isfinite("a")+ f9 N( h+ n* U- a. Z, s6 D
- >>> math.isfinite(0.0001)
1 u/ g; e, E" Y - True
复制代码 [, K# l9 M4 @# i; J7 H D) Y
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False- s# @, k4 k/ m& ~* [. A. u
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
! k. n7 z& Z% X P1 f. F - isinf(x) -> bool( h- X4 b8 H' P) ~; }+ r
- Return True if x is a positive or negative infinity, and False otherwise.
0 \- z1 o% I$ u2 F k - >>> math.isinf(234)
1 ^8 Y$ K# S, e; z - False) F* V" d. E* ~( m$ I$ F
- >>> math.isinf(0.1)
. Z" @" g6 S. G E( {( j - False
复制代码 ! ~: ]3 w' l9 k2 Q# |
math.isnan(x) 如果x不是数字True,否则返回False' c; X/ [) F! E0 c4 u9 c5 a, q
- #如果x不是数字True,否则返回False/ d3 c8 U, H R, Q! i% j, V
- isnan(x) -> bool6 j$ j1 `! H. M+ r
- Return True if x is a NaN (not a number), and False otherwise.
# [6 w" r) @2 |) j( N - >>> math.isnan(23)
7 Y( h ^2 Y/ t y - False6 [ T( a- v1 Z) Z7 Y
- >>> math.isnan(0.01)2 ]' ?! k* V3 J
- False
复制代码
Z+ ]3 v) L8 i6 D/ ^6 ]5 _math.ldexp(x,i) 返回x*(2**i)的值2 x6 e2 X# Y: r! i: k6 W, G9 H
- #返回x*(2**i)的值8 {, \! i+ _- Z8 e; I: H3 M
- ldexp(x, i)- _3 o+ y" R) ^; \! k
- Return x * (2**i).
& H8 B) B* y/ j3 g! S# A: @ - >>> math.ldexp(5,5)6 l2 k8 C) C' f, s4 k
- 160.0* i3 B( N4 J3 i) b4 g" c
- >>> math.ldexp(3,5)
& x' q+ J; F8 _# Y - 96.0
复制代码 0 n- d" R* H. _8 ^. J' O, E
math.log10(x) 返回x的以10为底的对数. Z5 f2 q, V1 z6 u) Y( C
- #返回x的以10为底的对数+ \" T2 Q3 t4 w& Y4 m
- log10(x)4 H L' j6 t) r$ v8 n F8 M
- Return the base 10 logarithm of x.
( V G# w3 O; l+ X6 ]. O' [ - >>> math.log10(10)
; ^4 k! S4 W$ h) O. t/ v3 o- ] - 1.0
# b( p2 ]0 p! p3 r) _' F - >>> math.log10(100)
- P0 {% z' a. Y- d9 R; H5 o - 2.0
2 T: H" X6 F8 P4 F6 a - #即10的1.3次方的结果为20
$ C4 R- e7 ?- | - >>> math.log10(20)
4 t2 t' i( D# d: } - 1.3010299956639813
复制代码
1 y4 q5 G8 b) r5 e5 N+ `. vmath.log1p(x) 返回x+1的自然对数(基数为e)的值
' p2 c. R3 a0 N- #返回x+1的自然对数(基数为e)的值
6 i1 S6 ~* A0 y% Z. Y - log1p(x)4 P+ E: N6 P6 U: W
- Return the natural logarithm of 1+x (base e).1 E0 n9 a/ b4 f5 Z+ h0 M2 R5 z
- The result is computed in a way which is accurate for x near zero.+ f X+ Z3 v1 q- z' }
- >>> math.log(10)% H- T- @) i" M
- 2.302585092994046
! \( \! C/ S( Y ~7 S$ U# Q - >>> math.log1p(10)
2 g) u9 B, d0 a5 b4 ]5 Z - 2.3978952727983707: d8 Q1 Z% `7 r: s, \
- >>> math.log(11)1 m4 x4 q8 P2 [: r$ W
- 2.3978952727983707
复制代码 - B! k, |8 s! F' x( z
math.log2(x) 返回x的基2对数& H1 t F3 K+ }: Q; ~
- #返回x的基2对数
* S# [& u `0 Y) T - log2(x)
7 r: A4 T" |1 H4 ] - Return the base 2 logarithm of x.
2 J0 ~3 N; F6 @( [, ^ - >>> math.log2(32)
3 w7 d/ @* S0 ?: o8 v, W) ` - 5.0 ]* O2 V A% v$ x& Y0 S& p
- >>> math.log2(20)
R3 ~: V0 K$ x+ C- ` M ~# K9 c2 i - 4.321928094887363: O G$ Z+ v3 d- ^% o" _( V
- >>> math.log2(16)
h- ]; a- I: i! b3 A6 H' N9 U - 4.0
复制代码
- _4 V/ B: G0 d0 r5 _5 Cmath.modf(x) 返回由x的小数部分和整数部分组成的元组
4 B% F$ X3 z1 I" r+ |* O# g- #返回由x的小数部分和整数部分组成的元组2 x. z/ Q* Z$ C4 C! d
- modf(x)% d) k4 ] e+ t) `/ `; k
- Return the fractional and integer parts of x. Both results carry the sign
6 ^6 D3 T. e' t3 C, a5 o4 d - of x and are floats.! K4 k, S4 X: v, N; V4 r. U
- >>> math.modf(math.pi)( i$ o3 W; z9 ]: B2 F- p9 ^4 g
- (0.14159265358979312, 3.0) {) ~' e0 ~- j: A0 K5 p
- >>> math.modf(12.34)
" m+ @3 _3 h3 u1 c( k - (0.33999999999999986, 12.0)
复制代码 4 ~: j4 c" i% H3 g( x, a
math.sqrt(x) 求x的平方根* X8 @8 z5 \6 s, Y' _8 f
- #求x的平方根
+ Z; s* d K( U* ^8 n - sqrt(x)+ J* x8 q/ R, J9 ]# j6 O
- Return the square root of x.
% q; q( v- ` m& f - >>> math.sqrt(100)+ I t, ~; i. z( s6 b
- 10.0/ e+ ~0 P& E$ F. O+ n
- >>> math.sqrt(16)) C1 x8 D3 m- M/ s! ?
- 4.0; D! n ]) o% S
- >>> math.sqrt(20)3 ~6 o& E( s9 o, V5 X
- 4.47213595499958
复制代码
# m# R& U0 u; V% l& y5 N. [8 e# bmath.trunc(x) 返回x的整数部分- #返回x的整数部分# F, w& [7 D$ ^% m4 t x6 y
- trunc(x:Real) -> Integral
$ k5 L6 }' p; r4 o+ n - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.1 v' X* C6 g# x4 X* ]
- >>> math.trunc(6.789)& Z0 E* v5 b/ \: n8 h
- 6
! C8 o1 [+ ^1 G - >>> math.trunc(math.pi)
8 Z: P5 [; g! |- h' W$ _/ S - 3
" e) u: y1 N8 t3 y7 l4 D" e - >>> math.trunc(2.567)
/ b0 ^2 v4 }3 C$ G a N! z - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|