马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 R; p7 J) t; g; e
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ N) G: S4 H6 C& Q) ^5 \* h$ ]9 a/ p2 M
方法1:) [: u9 Q) c. O; b: t# P' X
- >>> import math: L2 f% v- ~. a3 I* v& I
- >>> math.sqrt(9)9 I4 h' M$ S- A1 P& \7 q
- 3.0
复制代码 方法2:
9 V2 N, C7 Z w/ X! W# x0 {% C- >>> from math import sqrt
6 t5 Z7 T7 M8 P. N2 X% ? - >>> sqrt(9)1 H7 p6 V7 z, B# ]2 e% U
- 3.0
复制代码 $ _+ e# y& w" S9 Q* z4 @
- n k, R2 V- Q& ?- g% L5 A! R8 a! v
math.e 表示一个常量! J1 t# K+ V9 A$ ~: A) {
- #表示一个常量
: e# ]2 A+ [7 p - >>> math.e
; j3 \' d2 |+ p# I8 \1 F* `+ V" q { - 2.718281828459045
复制代码
5 A( V' h$ Y- h: Wmath.pi 数字常量,圆周率$ L$ j6 z( R# F0 ~1 {
- #数字常量,圆周率
6 u! ^" C) b3 S- s - >>> print(math.pi)
% g/ o8 M" \+ n& M2 t. Y - 3.141592653589793
复制代码 , c5 s+ _' h; g# {+ w( r# S
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x+ B$ K5 {6 r# h. Y n* f4 g6 l
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x. O2 l; a& ]3 C4 g7 z5 f: I. y
- ceil(x)6 X/ A. @7 f: b! A9 f( d) F
- Return the ceiling of x as an int.
5 H, ?" v# R& D {. U3 l. _" c - This is the smallest integral value >= x.7 n* g) E: H" J$ O3 e! Y
- Q* ]. V8 b* d
- >>> math.ceil(4.01)1 c% t( o' y3 x5 e4 u: Q
- 58 k5 A% M) e; _# e* ^5 o6 w
- >>> math.ceil(4.99)
9 s/ n) A: c) s: ~' v! p - 5
2 y% o F% G4 C - >>> math.ceil(-3.99): N5 w- e, C9 M4 B
- -3
5 G7 F) j6 ?2 O7 X. W: u - >>> math.ceil(-3.01)
/ B8 \& |3 n4 d! A, z7 | - -3
复制代码 5 W- z s' l7 j$ B( a$ K7 E& n
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 V% _1 M4 |7 E1 j# q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 s" p2 G4 ]: u5 J9 d( G - floor(x)
0 S2 J' W" f7 a t; t3 p - Return the floor of x as an int.7 ]* W5 b& p$ v$ z f$ [" r
- This is the largest integral value <= x.6 f2 m B- B; ?4 g. z
- >>> math.floor(4.1)/ [% \8 q; G; T
- 43 r r$ @0 [2 G" o; e# A0 X# c
- >>> math.floor(4.999)
/ f' ]% Q/ ?/ R$ P( C# l! K1 N - 4
! C/ K- m! p3 n% k - >>> math.floor(-4.999)
, i$ T& d% v0 b) k. V - -5 }) ~8 M! e4 z& m7 z0 M. \
- >>> math.floor(-4.01)
5 U; v) l( i3 ]7 e0 I! r: X* K - -5
复制代码
( h* o; X1 ~ s7 q, b# s! Emath.pow(x,y) 返回x的y次方,即x**y9 n8 B% b) e% }& m. z2 q
- #返回x的y次方,即x**y7 x5 V7 Q0 m2 L5 w" z
- pow(x, y)9 B9 ^) ?$ m7 e% K- `
- Return x**y (x to the power of y).
, ~6 k7 q9 v: p7 u - >>> math.pow(3,4)
& f. u) b7 j$ m, J - 81.06 y8 x3 s& |& I! x. `) Q
- >>>
# u4 _8 r' v6 B) @ - >>> math.pow(2,7)
$ y G! Y% z7 W - 128.0
复制代码
( ^) |: h$ N4 C" m- L: z8 wmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 V0 J* _6 `1 U) G6 w- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( b% | N2 i, t; c4 j( K - log(x[, base])
# e8 f, o& t/ \% b5 X9 V: e8 A. p - Return the logarithm of x to the given base.
/ [4 H9 Z0 r! u1 `3 u5 D - If the base not specified, returns the natural logarithm (base e) of x.% S4 [& R+ B9 h2 E' [' `: o7 z: p
- >>> math.log(10)- x; h# w) A; X: Q3 L
- 2.302585092994046
5 h$ i" U- X* s# v - >>> math.log(11)2 {3 q# j1 N2 M2 N8 ~. B
- 2.39789527279837072 a% R7 _. _$ i# L
- >>> math.log(20)$ B( t; Z7 h, Y- a) Q7 \. F' m
- 2.995732273553991
复制代码
8 P0 x$ I9 M0 c. pmath.sin(x) 求x(x为弧度)的正弦值
8 C. V. R$ m& `) r/ b. t7 j* R- #求x(x为弧度)的正弦值
+ I! ~( h1 A# S* i% X4 U - sin(x)9 T1 n" [, b4 I2 r! V
- Return the sine of x (measured in radians).
8 g% H$ n s* V$ k - >>> math.sin(math.pi/4)
- J, L$ \9 x5 s- r4 g5 w - 0.7071067811865475 h, o) I$ Y+ Y) _
- >>> math.sin(math.pi/2)
) R$ b8 t2 p5 L# q - 1.0/ w" F& n8 @/ I5 t. I2 |$ r
- >>> math.sin(math.pi/3)- _7 V' l- |) P( ^1 c! a1 y
- 0.8660254037844386
复制代码
o5 o; w7 ?) o* r# h9 Y- I1 Vmath.cos(x) 求x的余弦,x必须是弧度
' F8 ~- n: ~3 h8 d y b- #求x的余弦,x必须是弧度
. }2 r; J) s0 j$ \ |5 k; C - cos(x)# j, K$ P, b3 J) Q* z+ w
- Return the cosine of x (measured in radians).9 _4 I2 l5 |! H
- #math.pi/4表示弧度,转换成角度为45度
6 Q! i4 X$ R9 ?; b( T& C - >>> math.cos(math.pi/4)7 y' _0 h& a, g4 ?# o$ |5 t- y
- 0.7071067811865476
( w4 m# |6 ~$ @2 d) I5 H4 w1 i" Z' H' x7 t - math.pi/3表示弧度,转换成角度为60度4 f8 p& y/ W5 A4 a6 {4 k
- >>> math.cos(math.pi/3)5 y+ [& X: q, V( x0 ]4 R9 x
- 0.5000000000000001/ ~+ I. V, l5 O3 s+ r
- math.pi/6表示弧度,转换成角度为30度
3 k4 R; D- x- y6 J z, k; A - >>> math.cos(math.pi/6)$ _2 `/ V! o' u1 N
- 0.8660254037844387
复制代码 - S4 p8 |% S4 O0 @8 V3 {/ N- l
math.tan(x) 返回x(x为弧度)的正切值
7 U7 B: _$ `" H# B# b0 l6 v( B- #返回x(x为弧度)的正切值
1 ~ b U8 N t& T# i* J0 j* X - tan(x)
( m) O& a( S/ ^4 z1 |$ O3 h - Return the tangent of x (measured in radians).9 x: e% w/ y4 X% [/ k1 {, @
- >>> math.tan(math.pi/4)5 r& W L, l( D1 @8 d
- 0.9999999999999999
. _- \8 _: Y4 z" t9 {, ^7 q8 t - >>> math.tan(math.pi/6)) t4 U/ E6 `2 H+ C/ K
- 0.5773502691896257
@5 C3 F9 o- f. y0 T, w N- d2 t - >>> math.tan(math.pi/3)
3 ^$ {* R' l6 P* {$ k( { - 1.7320508075688767
复制代码 : f, Y! |& R1 }0 U0 J% Q# V( K
math.degrees(x) 把x从弧度转换成角度
1 I9 s' ^& s# {8 `: b- Q- #把x从弧度转换成角度
& X2 s0 x5 A' W1 @# w, j; M - degrees(x)6 f: r* T8 M2 `% W8 A
- Convert angle x from radians to degrees.0 m# L: Q+ _$ b
( @' S& B! H. n) K. z. q" S- >>> math.degrees(math.pi/4)# ?( d. s {7 M6 D; t
- 45.0, c$ R3 m- b- e+ r9 J1 T
- >>> math.degrees(math.pi)
' X" N. ]% y% S0 @6 ~5 M! D - 180.07 e/ h% P: J+ |3 _, J
- >>> math.degrees(math.pi/6)2 P4 z. N! z9 d+ ^2 {" d
- 29.999999999999996
$ H% e0 S: H% B9 ~- ` - >>> math.degrees(math.pi/3)1 B3 p( K- }, ~6 d+ H
- 59.99999999999999
复制代码
* `% a, z5 i4 r" omath.radians(x) 把角度x转换成弧度
; L# e1 K' N$ R" o1 u |% }- #把角度x转换成弧度
. }" X# ]8 q/ w( o' s, ] - radians(x)
: q. D6 |! Y/ x' x2 z - Convert angle x from degrees to radians. P; I) ^( M3 @5 A y5 c3 x6 }# B
- >>> math.radians(45)1 l9 M8 w/ T* b6 @- g* N
- 0.7853981633974483
1 C' _ O* H C - >>> math.radians(60)9 c: M' l1 m+ p( W- f- [
- 1.0471975511965976
复制代码
' X8 T4 N9 a3 Nmath.copysign(x,y) 把y的正负号加到x前面,可以使用0, J. F, b7 k2 R* C
- #把y的正负号加到x前面,可以使用0
; N6 w2 i9 g+ E% c. H - copysign(x, y)
& w9 `5 x' r+ ?6 I: _' m8 c - Return a float with the magnitude (absolute value) of x but the sign 5 D2 C' t# I, b# w# p
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
3 `6 p* [% J4 c% M - returns -1.0.
9 \' v3 g6 d% R- ]: u
3 v$ C7 Q! o2 b- >>> math.copysign(2,3)
9 j! N9 w: b; x - 2.0
5 O! G6 m% I$ j' m; G& j - >>> math.copysign(2,-3)1 I- K- q: l) i/ _ F
- -2.0
7 u: T) k" R4 t- l; @3 \* j/ R" a - >>> math.copysign(3,8)" |) ^1 L4 C4 J
- 3.0
, d" p o0 i6 n. y1 a' a. s - >>> math.copysign(3,-8), @- F- {4 o% k& Q/ ?- @: R% ]& @
- -3.0
复制代码 : W1 c' K( R& F: t. P( w
math.exp(x) 返回math.e,也就是2.71828的x次方; o1 T2 G, v f+ y: o
- #返回math.e,也就是2.71828的x次方
! c$ |6 e. n& i( u* \6 B; i/ v - exp(x)
* I2 [3 h/ m. t1 j: p5 [ - Return e raised to the power of x.
4 p% l" X9 F1 G4 K* Q5 ~& Q - ( L3 M9 a4 v2 t3 s
- >>> math.exp(1)5 E- w; c4 C1 g" F# @$ U7 ~
- 2.718281828459045
9 D5 K1 w; n1 P) l2 d* ~8 b - >>> math.exp(2). ^, [7 g6 q' M1 v
- 7.38905609893065
. u3 u5 U, \0 Y" C - >>> math.exp(3)7 c! Q+ u% d9 I G
- 20.085536923187668
复制代码 6 |: Z3 Q1 A& a& _6 G4 y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( ^+ N, n2 k" `+ e# G+ F
- #返回math.e的x(其值为2.71828)次方的值减1
" b; B+ _$ _8 m# h7 [# J - expm1(x)& M9 b* N' m5 C9 s% T k5 w% u: h
- Return exp(x)-1.) }0 I0 b" U* L: l2 \" g! _& z7 w7 C `
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 S8 H2 Q2 l4 B
- 1 R$ L, P! t( P
- >>> math.expm1(1)4 e7 y# R" D& {* P5 ^( L
- 1.718281828459045
* a, f6 t2 P- p# s% L - >>> math.expm1(2)
* N# _: q( m- w% X) D6 Y# U1 k - 6.38905609893065
5 M/ o0 C1 l# [% o - >>> math.expm1(3)
! d$ {: @5 X" `* | J Y" P* ~5 d - 19.085536923187668
复制代码 ( P1 i; K! |- y3 I) _" w \1 \/ I
math.fabs(x) 返回x的绝对值
/ f9 j. r' S* h4 c b: [% y- #返回x的绝对值$ P5 q4 V& J/ y! C0 `% W+ M7 e
- fabs(x)
( C7 X. @2 \# H; a5 x* f - Return the absolute value of the float x.+ h' Z [0 t! L4 Z1 v0 D
- + s6 m( k$ n. T; W6 H* e* I1 X
- >>> math.fabs(-0.003)
0 ]. z0 H$ n1 }+ d3 Q- d2 Z2 X - 0.003
3 x5 `( k& V& M/ I7 U# p* [ - >>> math.fabs(-110)
* E+ ~$ D! W9 }( G4 e* V. M4 j7 C - 110.0
- Z+ |8 B) S# h s3 B3 D* P! W - >>> math.fabs(100)
" L0 b1 Y7 s- y7 B% R! k. |' ^ - 100.0
复制代码
# a) ]6 A F3 X ?math.factorial(x) 取x的阶乘的值
; l9 R) v) c2 K; V' X6 [- #取x的阶乘的值5 n+ |9 F; _) S8 |
- factorial(x) -> Integral4 F5 z; u" O8 q) O! r
- Find x!. Raise a ValueError if x is negative or non-integral.% E' |3 @% \4 v) W, ~" V
- >>> math.factorial(1)" c0 S! |2 a- X: s
- 1
( _" F! ^# |4 `% p0 [: w - >>> math.factorial(2)! @1 m0 D) A& n+ t' z
- 22 ?9 w; \% s/ c3 O& i$ A$ d$ t& E
- >>> math.factorial(3)9 ?% ^& t( Y8 e% @1 f6 i. a
- 6
6 M2 A0 @' a6 J0 V3 h4 @ - >>> math.factorial(5)
/ O2 W% ~, C) `& M" J. g$ y - 120
2 K7 {- Z/ t- c1 F+ T - >>> math.factorial(10)
( q: K" y1 t2 p% t7 P - 3628800
复制代码 ; I0 M7 @5 m& i3 x. k
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数- z6 p- y1 B' `- r# U
- #得到x/y的余数,其值是一个浮点数
1 O4 o' a" S h7 z, [ - fmod(x, y)
1 j7 c" T, }9 ~9 y! Q - Return fmod(x, y), according to platform C. x % y may differ.! Y% Y* ~' o6 P8 N1 D! Z
- >>> math.fmod(20,3)
4 B$ W0 o4 ^! t% r l. q - 2.0) T5 Q' U% J2 G# M t" a4 B
- >>> math.fmod(20,7)) N# V( N' M% u) v
- 6.0
复制代码 # c" P( _3 ]3 A7 x9 C7 p
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
( W3 R3 s- g/ u- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 N) p5 R8 }1 @$ ^
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 i8 H9 s; }( I
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( k4 {* [, C0 J5 [3 _
- frexp(x)
. |9 a3 M( h" @- P - Return the mantissa and exponent of x, as pair (m, e).
2 ~- k& ]) V9 Y4 O6 v - m is a float and e is an int, such that x = m * 2.**e.
) a0 S u# ^9 M6 l2 }4 }. K' r - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.) Y4 `+ [4 t5 i; L
- >>> math.frexp(10)# B7 `6 Y D6 F
- (0.625, 4)2 c, e" h1 h1 @$ R+ N3 G
- >>> math.frexp(75)) p% G( d) ]" e' G. C4 e
- (0.5859375, 7)2 q# ~! J5 D9 K: }3 c' ?& e8 ]% H, f
- >>> math.frexp(-40)
# ~4 F3 U% Z0 M4 m - (-0.625, 6)
) k8 C9 {/ V* A( S - >>> math.frexp(-100)
% U B/ f5 T% p% y - (-0.78125, 7)4 K4 V* m5 T w s8 V$ U
- >>> math.frexp(100)
0 p* e. o- A: {4 B0 `+ \7 Z - (0.78125, 7)
复制代码
6 i/ s3 |3 Z6 E8 X+ d9 Imath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
9 B8 N) u( Y; K; E( _- #对迭代器里的每个元素进行求和操作2 m, }8 @! t% g! O+ {
- fsum(iterable): p1 \ P9 f# G F. B. e$ O) Q
- Return an accurate floating point sum of values in the iterable.6 ]+ F7 _5 n- t# a% f! }! F
- Assumes IEEE-754 floating point arithmetic.
- Y9 l' F0 X7 c8 ^ - >>> math.fsum([1,2,3,4])
- c6 @+ B( g! j# K - 10.0
+ b$ K5 ?8 p2 ?% d% A7 R - >>> math.fsum((1,2,3,4))
) T% r# e1 [, p* E# s+ m/ ]( A - 10.08 O, v/ G' y/ o
- >>> math.fsum((-1,-2,-3,-4))( Q8 }7 X1 r( }' }5 t, t
- -10.0
$ ]/ h* f% `5 d2 ~* M7 w - >>> math.fsum([-1,-2,-3,-4])
9 A) k( ? e d7 F9 `7 u- v - -10.0
复制代码 8 p' q f' B' x7 I- m3 r. B
math.gcd(x,y) 返回x和y的最大公约数0 q( S2 G7 i @
- #返回x和y的最大公约数. K, p( _7 I" o7 S1 J
- gcd(x, y) -> int
* D+ g" u) _7 O5 Y - greatest common divisor of x and y
& ~) r5 M' N, G& T - >>> math.gcd(8,6)6 \# E) ?9 s; {3 ?, k4 G( j
- 2* [9 I' I; I9 ]% R7 o7 c
- >>> math.gcd(40,20)3 d: ^" ~- D0 }0 p
- 20
) D' K2 B/ g* X0 P& \3 c, w# ^ - >>> math.gcd(8,12)2 J5 J$ f) J4 Y3 @ C
- 4
复制代码
- j) j7 x0 b3 K, T% e$ Hmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
1 E6 r8 o% V7 S, _ E: \$ W- #得到(x**2+y**2),平方的值
- O+ V; \7 }7 Z q$ \/ Y1 ?1 r - hypot(x, y)
7 w# A, K) B: M2 n& \0 ` - Return the Euclidean distance, sqrt(x*x + y*y).& Q$ O' d1 ` c% G3 K
- >>> math.hypot(3,4)! f& i7 s) `) r1 x9 `
- 5.0* I7 V8 W, E% V! s: d+ ~' c
- >>> math.hypot(6,8)
+ m4 a; J+ |5 X - 10.0
复制代码
5 }2 n, j6 v- B7 J- i% `math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! W8 Y$ W4 g% ~3 I, s- #如果x是不是无穷大的数字,则返回True,否则返回False
% }# q1 _' p, }4 U/ X - isfinite(x) -> bool
) _" w, X5 _! \. F# n - Return True if x is neither an infinity nor a NaN, and False otherwise.! ?+ g2 e: A- L# I) y9 v
- >>> math.isfinite(100)
( J' l8 x0 b0 s; [& k3 A- r( p5 P* ~ - True
( }; j* h# `" E/ B - >>> math.isfinite(0)) f. M% J) w/ o# j- [
- True3 T- Y- W4 j+ k3 H& @4 |1 S4 A8 ^
- >>> math.isfinite(0.1). v: z% N7 Q1 F0 X( o
- True; k2 q2 M3 J9 t6 S5 H
- >>> math.isfinite("a") _6 T6 b& j7 k9 ]2 T4 f* S
- >>> math.isfinite(0.0001)5 d) _7 `( `, Z$ q2 M; d, D. E' { W
- True
复制代码 5 s: z1 I1 w4 A+ l
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False) O* O# O3 \' y. N
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
! r5 C- Y! z {- @" F& w - isinf(x) -> bool
) Q O! T- {: \ - Return True if x is a positive or negative infinity, and False otherwise.
0 l+ m, E" X- j# D8 O& ? - >>> math.isinf(234), F! l! U+ `) C4 _+ F
- False
0 l5 s3 c! g: l) c* S/ } - >>> math.isinf(0.1)
+ {* A$ X+ ]* y - False
复制代码 9 I3 j" h0 }6 v
math.isnan(x) 如果x不是数字True,否则返回False4 e' [# y5 l. }. z! p; ]
- #如果x不是数字True,否则返回False- A/ ?- g N4 H. o4 C' |! F
- isnan(x) -> bool( P0 `% c" j5 h" t% j
- Return True if x is a NaN (not a number), and False otherwise.& d: e4 z, ]' I. k: P. [
- >>> math.isnan(23)' g6 q: o3 h) g: S0 o1 J2 K' G
- False
0 m5 ^5 Q. ~' d# y - >>> math.isnan(0.01)
+ F6 n8 ~% Q/ F# d5 m5 Z - False
复制代码 / P& n# K" K3 G8 \+ m5 }. ^% ]
math.ldexp(x,i) 返回x*(2**i)的值
" w% f D* Q: r+ [4 Z0 W- #返回x*(2**i)的值. l" \5 o$ ]# A2 l5 S) @
- ldexp(x, i)' W8 K' i% V/ F. r3 \
- Return x * (2**i).
/ v+ V& J) a( n# Q1 o9 Y - >>> math.ldexp(5,5)
/ ^' o: ]; E% X, [) B - 160.0
( V5 V! p/ Y3 c6 I4 g - >>> math.ldexp(3,5)9 b% n$ J8 O0 W% I
- 96.0
复制代码
6 y, i0 ^9 Q2 v, lmath.log10(x) 返回x的以10为底的对数) u" p' ]4 D# W2 _
- #返回x的以10为底的对数
7 u6 g0 I2 t3 E/ E! q! x - log10(x)
) M: F! _4 w5 [( y - Return the base 10 logarithm of x.
: H4 O) t. a% z$ k3 I- j - >>> math.log10(10)
6 A5 n' l2 |9 ~" Q. d - 1.03 {/ d0 w; M$ T
- >>> math.log10(100)
. C* _* s! z7 A6 r b$ H - 2.09 C* R, H; z; ~& ]
- #即10的1.3次方的结果为20
7 S- N6 k: }+ L8 k) E) J8 V - >>> math.log10(20)
: Z" B' y$ r/ W# K - 1.3010299956639813
复制代码
( n l' I9 p: g% b2 p4 R- vmath.log1p(x) 返回x+1的自然对数(基数为e)的值" P1 e' ^. B- x- |" \; K: s; P; P/ I
- #返回x+1的自然对数(基数为e)的值* a3 u+ H' u- ?! [8 p# w' N
- log1p(x)+ \$ R5 d' d1 Z+ E4 S3 h, ^
- Return the natural logarithm of 1+x (base e).% y5 n1 \9 b" c' s: l# X
- The result is computed in a way which is accurate for x near zero.: y3 s6 O1 b/ t' M
- >>> math.log(10)
* T2 i* C* W3 t3 s8 F - 2.302585092994046
" d0 ^; q7 t& B - >>> math.log1p(10)8 q3 k- y7 w' M4 S
- 2.3978952727983707
8 d5 p6 B8 x/ ~" G9 I - >>> math.log(11)
9 k, e) F& M. J& l# `2 a0 @ - 2.3978952727983707
复制代码 8 T* m4 F) k4 _1 c
math.log2(x) 返回x的基2对数
; [* ^6 k. H1 A( ^- #返回x的基2对数$ l$ p; r, s# O- q2 d; m* l, E
- log2(x)
$ b' e' j& n0 d1 @$ | - Return the base 2 logarithm of x.
- A3 L) T' J& z. |# k& Q Y - >>> math.log2(32)6 _( X( G' W5 c" z* m
- 5.0
& b, R1 R7 w# y+ a1 V$ F - >>> math.log2(20)7 t5 D3 \1 s; r3 E; T
- 4.321928094887363
7 s% q* Q8 Z5 N7 c8 k$ ^ - >>> math.log2(16)
5 K5 ]. N: x9 G( Y% B- ]( ?5 s - 4.0
复制代码 . \/ _3 v8 f k5 ]
math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ W" o" W; X, g- #返回由x的小数部分和整数部分组成的元组
9 S3 V" c6 c! e d- `( G1 V - modf(x)" P5 U1 w# M% p0 E) v- w
- Return the fractional and integer parts of x. Both results carry the sign
m: j" _! O/ t$ Q0 m5 ? - of x and are floats.
# K9 `! H; x( N! U3 J; K - >>> math.modf(math.pi)' j% Q) i( a" E4 s
- (0.14159265358979312, 3.0)
* y, o9 l; L* A; S/ E9 \( w9 z! y - >>> math.modf(12.34)- J! B& ~; J- f/ W; r: {
- (0.33999999999999986, 12.0)
复制代码 0 m$ }- G2 i5 Y z2 t5 Z
math.sqrt(x) 求x的平方根
) c" f, @7 a6 H, k0 S' C8 O- d4 c; Z- #求x的平方根
4 x! |9 Z% l5 b* e* m - sqrt(x)/ \! d2 ?8 i _& V8 o/ e, L
- Return the square root of x.
. l x' m5 ]* J' o - >>> math.sqrt(100). i/ K& P9 }4 E; [; I6 k2 G7 c
- 10.0
. B" h* \$ a& s v - >>> math.sqrt(16)* w/ {8 c# y. ^( g# ]
- 4.0; n( {+ [# I2 I1 B& z
- >>> math.sqrt(20)( G) e6 n; E# H5 G8 _5 N8 ?8 N2 I
- 4.47213595499958
复制代码
1 h' g& @8 e+ v2 ]& T$ Y- t: Hmath.trunc(x) 返回x的整数部分- #返回x的整数部分
! k. ~" r* k$ L/ V* O7 a& I - trunc(x:Real) -> Integral- u8 q' T- Q& Y" _
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# A* V& p) U+ G% T' u& P
- >>> math.trunc(6.789): h* x h& W* H! j; h/ H
- 6
. Q0 p* K, F2 S5 S - >>> math.trunc(math.pi)! a% t" R, _3 w2 C
- 3
: z! T6 g% G6 O5 x - >>> math.trunc(2.567)
( O2 p* d1 I# g% | - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|