马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" `0 K$ W) ~% s' x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 U6 p. k4 i! y' ?
7 D4 [+ W/ `9 {# L" B
方法1:5 {5 S! ]1 {/ }7 c O
- >>> import math& R% \5 {" b- A6 W: T6 J/ x8 {
- >>> math.sqrt(9)
% M( j( }; L) w' o - 3.0
复制代码 方法2:
0 {9 ?8 \; W* R4 O- >>> from math import sqrt
1 \/ j- N2 T5 Q. } - >>> sqrt(9)
1 c8 J4 ?6 c7 m/ Z - 3.0
复制代码 & D3 f1 O6 o, q4 B$ C5 e4 `3 S0 P0 J
. u! w' J% R$ e A# q' Rmath.e 表示一个常量
: E" a5 ]1 o2 I4 x- #表示一个常量' F9 @+ t6 }# W5 J- ~% q
- >>> math.e
6 N) d1 x1 ?8 g# z: K - 2.718281828459045
复制代码 3 _" J9 b7 x1 g/ K3 n
math.pi 数字常量,圆周率
$ l- q/ G* _+ a" D$ a0 e2 x% q- #数字常量,圆周率7 l: E$ o& w$ J( N. [
- >>> print(math.pi)
& i' b1 f |1 n - 3.141592653589793
复制代码
" C+ Y9 }! t# o" V! o5 Fmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x A9 Z: O/ P$ F: e$ V7 Q! R9 \; f# P
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x7 A, c( H4 z4 |0 @7 l
- ceil(x)* }5 C2 H! `2 f& r0 U' \! }' z6 X* I
- Return the ceiling of x as an int. d3 s* F- L: ^% y" J
- This is the smallest integral value >= x.
% M* q& k( G1 l/ X* U2 ^3 h8 o - : K) V' d9 b8 n( k
- >>> math.ceil(4.01)( Y! l t8 }* E: F
- 5
/ m7 B/ K |, d. L, c3 b/ }) t T - >>> math.ceil(4.99)- n; _+ ? N" ?1 ?$ E
- 50 @5 S7 t! P% i3 ?, }9 n. u
- >>> math.ceil(-3.99)8 u4 ]! c% s2 E# B, [! C
- -3
; l9 {% Z* H9 M5 d7 y' m; l, T: F - >>> math.ceil(-3.01)
0 g1 _7 F9 F( Q, f# @0 J - -3
复制代码 ( }$ {/ l9 S7 U
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 X0 G" _& `+ s O( b; ~
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( R G1 k$ m7 {# y0 A4 E$ K p - floor(x)# E( R6 W- g/ G4 n
- Return the floor of x as an int.) S$ M$ ?( {9 b2 v5 M
- This is the largest integral value <= x.
& h4 e- U( H; o" B; w5 R: G) Y+ h - >>> math.floor(4.1)& h, m+ ]% p2 P* N. Q9 ^9 C3 Q! Z
- 4
$ G; q( ~1 p8 q' x - >>> math.floor(4.999)3 I' N4 H) d1 S% v
- 4
/ H. r+ K# `# j' J- i, e - >>> math.floor(-4.999)
* m$ q6 T1 }% W7 r - -55 q4 u) a, ?4 }7 f4 G6 c' E* n: [
- >>> math.floor(-4.01)9 P% s0 r( F# |; z+ z
- -5
复制代码 * `% N9 D# W1 K) w* _
math.pow(x,y) 返回x的y次方,即x**y/ Q! d; d, i' H
- #返回x的y次方,即x**y
3 ?: e8 ]0 ]1 J) X" I1 I) h - pow(x, y)/ s) J( |0 G$ W1 ]" y
- Return x**y (x to the power of y).
# b; ?. X" }- `* I: h1 w - >>> math.pow(3,4)( P" [8 t9 D) L9 h2 I
- 81.0
) j6 n U. f/ S0 D, d1 S - >>>
7 s: g" j- [2 ^' ^# I) q# `- e - >>> math.pow(2,7) t) K+ q. g, q) q# P
- 128.0
复制代码 ! t5 N( s a' {5 v# ~# E
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
5 C3 D+ B. C3 r4 s z& k& Q- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 X7 V5 z @- o. D$ \9 q; s# [
- log(x[, base])- R' X! e2 U1 H/ }: v: A
- Return the logarithm of x to the given base.4 r3 \7 |& _) y
- If the base not specified, returns the natural logarithm (base e) of x.5 N: d, U T% K% B5 W) P0 C- I
- >>> math.log(10); D1 u" Z! i3 N* E) Y- x
- 2.3025850929940465 j, W! V0 \' D5 B, q2 f1 q0 T& V
- >>> math.log(11)
% D! A% }/ @# f- q - 2.3978952727983707
* f6 u" k5 r! @5 b/ [4 J) y - >>> math.log(20)
/ Z( ^% S; C1 P+ K+ z x) V9 M - 2.995732273553991
复制代码
3 N: t) P$ [8 F o7 S+ Q! fmath.sin(x) 求x(x为弧度)的正弦值
9 E+ e1 D4 z- {' T- H+ R( C- #求x(x为弧度)的正弦值
5 J Q" s$ h7 N: M6 j% x0 L& H2 j - sin(x)/ G8 C+ R U r4 T
- Return the sine of x (measured in radians).
6 I+ v: ?& Q, g$ ^4 n - >>> math.sin(math.pi/4)
0 A( m6 K5 ?6 o% g$ I% w' x. r - 0.70710678118654752 B& \6 {2 |+ g: G
- >>> math.sin(math.pi/2)3 T8 a9 [. ^9 e! g* G9 f+ U7 z
- 1.0( C4 I# g+ M+ q& w, \6 ~
- >>> math.sin(math.pi/3)3 D' k+ C+ u8 E; w3 [
- 0.8660254037844386
复制代码
: w' i {" ]: J* v0 Z$ Bmath.cos(x) 求x的余弦,x必须是弧度
3 n2 B* e4 z4 r4 h1 G0 }( T" P- #求x的余弦,x必须是弧度
8 ^$ F6 J4 [! \0 A+ U; v - cos(x)
+ P* S* X, c- a( i1 u: g* U - Return the cosine of x (measured in radians).- b. m7 k# m3 [( S
- #math.pi/4表示弧度,转换成角度为45度8 a% N. Z- d) _ A7 H( N
- >>> math.cos(math.pi/4)# R5 K/ l# j+ t; P4 z% y
- 0.7071067811865476) {. B2 g& ]1 X. f; i* p
- math.pi/3表示弧度,转换成角度为60度
. K) h M+ R; r) S- y% }3 R, c - >>> math.cos(math.pi/3)
- ~0 Y' n1 L5 {5 v - 0.5000000000000001 V. r7 l' W+ N+ ]# h: ?0 a
- math.pi/6表示弧度,转换成角度为30度- |& b# a' O* d8 M- |
- >>> math.cos(math.pi/6)
8 K1 F6 s* A, g, [3 I7 k* h - 0.8660254037844387
复制代码 & m5 c4 h; f5 N9 h: F4 K
math.tan(x) 返回x(x为弧度)的正切值% F8 P8 ~- m' A; C- y1 t
- #返回x(x为弧度)的正切值
. q) K: i' ~' B - tan(x)
7 C8 O: Y/ ^; J9 ^+ N% r - Return the tangent of x (measured in radians)., l+ z( W8 c* M$ `1 y
- >>> math.tan(math.pi/4)
+ b% h0 O" {- h/ M8 T9 u* ]1 b - 0.9999999999999999
- X; ?3 A5 _1 c8 @; x' s; D5 k% L - >>> math.tan(math.pi/6)
; `2 B6 f C, \. J- s - 0.5773502691896257
7 l* k4 h: T5 ^, e - >>> math.tan(math.pi/3)2 n- D& I1 e. C6 q% j: R
- 1.7320508075688767
复制代码 0 C7 T, t8 N n9 o! H
math.degrees(x) 把x从弧度转换成角度
4 U; P8 k8 U: F% T# u- #把x从弧度转换成角度5 {7 j; z: D; F) Y \. ]- c
- degrees(x)
' ^1 w2 v. Z" n4 r5 g* C0 d - Convert angle x from radians to degrees.; W4 Q! c& J; d
2 F2 M$ C, ]6 N. X- >>> math.degrees(math.pi/4)
/ @& P+ p5 q, I+ L - 45.0
3 Y h2 d0 J% B g2 D: ] - >>> math.degrees(math.pi)3 h9 P$ B" R+ i& y% O
- 180.0
: M% V; B; u3 `0 u - >>> math.degrees(math.pi/6)
4 w% A# t' |- [3 A - 29.999999999999996
! x/ c- a+ K% i. m! B& J/ U - >>> math.degrees(math.pi/3)
9 F" d2 G' U% \! i: N; a - 59.99999999999999
复制代码
( S) T7 O' P# t1 E B) E# Pmath.radians(x) 把角度x转换成弧度6 m+ Y. e6 m; h$ r, P' a# J8 k
- #把角度x转换成弧度
# a5 n; O! `7 M. m - radians(x), Q$ p! r0 z! l( W n7 @
- Convert angle x from degrees to radians.5 z3 S/ P4 l: u" \. N
- >>> math.radians(45)/ k7 m/ w% G* J/ |
- 0.7853981633974483
2 O1 W/ n- J# b/ E4 I4 u% G - >>> math.radians(60)& Y% t4 ?, |. P4 B! |. g1 }& a- e
- 1.0471975511965976
复制代码 - B, y6 s+ F- F) d
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
0 Y" y5 e$ Q/ W4 @) a# l5 P- #把y的正负号加到x前面,可以使用0
2 H, @2 T$ z) m( N - copysign(x, y)4 F! Z% K" Z( E4 K
- Return a float with the magnitude (absolute value) of x but the sign
7 C) W: Y Z2 h - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 3 J( Z7 I( h% t
- returns -1.0.4 {% O- O x: D4 |( B4 ~6 k( P* d
- ' o% ~! Y6 s4 S4 p1 \0 ]' @- C; f
- >>> math.copysign(2,3)
& @; O1 W$ q8 K) Z4 L0 u - 2.0* \; l; v( W3 z/ h
- >>> math.copysign(2,-3). M R8 e5 w# G/ ]( m' e; i
- -2.0/ F! |! S% v' t/ y5 n9 C
- >>> math.copysign(3,8)
% a& m" H& E( S; s3 i0 V - 3.0+ }& F) r- V" E6 J
- >>> math.copysign(3,-8)
2 ~, o3 B0 m' ]6 g& k/ X - -3.0
复制代码 % X5 b* B, ~. X4 S
math.exp(x) 返回math.e,也就是2.71828的x次方
+ N j- U) w# T& ]- x- #返回math.e,也就是2.71828的x次方2 f5 w- |' B7 O
- exp(x)% E9 E( a& \% U' j" w0 I. { b
- Return e raised to the power of x.$ {0 l6 X: x7 @
$ A1 ?- \; W4 Q1 r1 U2 E4 b- >>> math.exp(1)6 W1 Y2 _. G4 ?) ]1 g+ L
- 2.718281828459045
. L5 P6 R+ V7 `0 B# v - >>> math.exp(2)- _3 X, {: }( T+ F7 M1 G* W) P
- 7.38905609893065
5 U8 ~/ Z- |" D) s$ m/ M% E% u - >>> math.exp(3)+ J! y; g5 ^: A
- 20.085536923187668
复制代码 $ Y. H/ _/ }; A: z
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1 z6 ?, `3 \- |
- #返回math.e的x(其值为2.71828)次方的值减1
; K/ w4 P" O8 h+ T8 s6 q - expm1(x)
" [4 X$ D5 l3 h6 k - Return exp(x)-1.4 f4 U5 ]6 O* O
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ p2 c. K n# Q, w) t2 ?
* q/ v( \& W8 i7 f" L) Q) E- >>> math.expm1(1)$ V3 `$ z, B6 h% [
- 1.718281828459045
2 B* f1 ^1 l0 P% e8 \8 ?! z9 A - >>> math.expm1(2); N# c1 P2 i j( V2 c1 \
- 6.38905609893065" G# K) X2 }5 `( M$ F2 M/ R
- >>> math.expm1(3)
w5 @1 d; d/ k4 Z; j - 19.085536923187668
复制代码
& D: `, f7 _7 k& h3 A3 U! |math.fabs(x) 返回x的绝对值- n" ? G+ I( X0 q1 ?- P( u
- #返回x的绝对值
8 C/ B i# X. H$ V2 G, A6 Y) f - fabs(x)" D+ W b, X* T3 i
- Return the absolute value of the float x.
4 b. G2 {: A' r* ?) s1 w- i6 B - ! t+ z4 `% v! V5 o4 [ f
- >>> math.fabs(-0.003)% D$ r7 R1 ^; T7 x0 X% y
- 0.003
& g! m, D+ M6 j1 M& m - >>> math.fabs(-110)
- \- q; _$ v' W3 s6 g - 110.0
/ C1 `, S2 A, Q D/ t - >>> math.fabs(100)8 ^- Q+ b% i: F) g
- 100.0
复制代码
4 a) v9 c! Q% z1 ?math.factorial(x) 取x的阶乘的值9 {5 L6 t( R( d4 R! F- \+ p
- #取x的阶乘的值
1 A2 r. _$ u+ w6 _8 M8 R% `) _ - factorial(x) -> Integral( j1 ?! s1 f1 I) V# E3 [, |/ i
- Find x!. Raise a ValueError if x is negative or non-integral./ k9 _; |9 Q2 D
- >>> math.factorial(1)6 j0 W9 `9 I9 l) A6 b# J' h& k) d2 t% h
- 11 E0 I) ]+ g' z' O! t% m
- >>> math.factorial(2)
/ I) [: _- @9 f! T( Q - 2
* D4 n7 z$ I5 B- Z4 s! W7 K0 N* j - >>> math.factorial(3)
% r/ l( K8 O8 e$ I( L+ @1 J3 W - 6/ ]+ I8 J: s# e5 f( [( e
- >>> math.factorial(5)
$ ~9 d' N: F1 i( L - 120
, W/ M. P/ A1 c% x8 r: z - >>> math.factorial(10)
- F3 Q- L$ { ?( \5 U# Z - 3628800
复制代码 0 o3 C) d# l! H! \
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数* J4 {: F7 N# N3 _. u" c, ^7 p
- #得到x/y的余数,其值是一个浮点数
$ g _5 @+ F+ Y' o - fmod(x, y)
( L9 J5 J% |/ O. m3 W4 D6 w9 j8 Y - Return fmod(x, y), according to platform C. x % y may differ.
) z2 M- o* v: f - >>> math.fmod(20,3)& X9 O3 i1 K( Y
- 2.04 z1 | k& c6 f# p+ ?. p
- >>> math.fmod(20,7)# h. w9 W v- Z9 b- Y0 |
- 6.0
复制代码
( a# J7 S+ u* n6 l/ s& O- tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) ]. S% E( K5 |8 p( u- y) K1 O9 B$ U
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 L0 K7 k5 B$ M# F5 c z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
5 y9 V" v2 X* e$ u - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ ]( W" E! d/ j$ K4 o
- frexp(x)
) A) M. o( V5 f2 U - Return the mantissa and exponent of x, as pair (m, e).
2 V, h% X+ }9 I7 A& g: `% K - m is a float and e is an int, such that x = m * 2.**e.
8 |1 t& T7 D, I3 \ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0./ j6 s2 E- F5 k$ x
- >>> math.frexp(10)# P8 r1 q0 Q& s2 T
- (0.625, 4)
W' O8 H$ L6 c - >>> math.frexp(75)
# A5 {" E8 N% L3 m! P& o - (0.5859375, 7)
* |3 n; T6 y' |+ \0 M; C) r - >>> math.frexp(-40)0 |# m) S6 L0 ?; E5 n
- (-0.625, 6)2 ~2 G t, z7 q/ w
- >>> math.frexp(-100)+ N! M! [6 H3 N' o- l7 `
- (-0.78125, 7)$ q$ v! @; j4 d4 U) ~- y
- >>> math.frexp(100)# b, u( n {4 {* m) }' M
- (0.78125, 7)
复制代码
$ U) u& F C Y& Q5 z: j4 Vmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)5 D# A7 U7 N( |0 B _# @' `' ^3 Y* L
- #对迭代器里的每个元素进行求和操作6 a p. W7 J5 z* q
- fsum(iterable)- V, D, b) s4 \1 k8 L6 B# P/ L
- Return an accurate floating point sum of values in the iterable.' [3 _4 D. i3 Q t" N; K/ Z
- Assumes IEEE-754 floating point arithmetic.
6 k( | A5 b0 |' a ^9 ]) P - >>> math.fsum([1,2,3,4]); A, Z; c+ Q% b7 `; D% O
- 10.0
( X; M2 I# D( U1 {- U( ` - >>> math.fsum((1,2,3,4))7 V3 j9 D: G- \ z
- 10.0
0 p8 ]& ]9 i& x! X9 R - >>> math.fsum((-1,-2,-3,-4))6 T8 n2 c+ \: L& Z. }9 j+ J! ^
- -10.0' j1 o! j0 H- B/ U! @8 X- Z
- >>> math.fsum([-1,-2,-3,-4])
6 n) h+ I4 o, q2 r! p: l+ P - -10.0
复制代码 ; V: t! Y+ d' O8 e# b' y
math.gcd(x,y) 返回x和y的最大公约数, P' i3 h- a, |! f" d3 f
- #返回x和y的最大公约数. l6 s: h6 H+ Z0 ?. G& }! Y
- gcd(x, y) -> int) O& Y" k5 x% z1 z) Q
- greatest common divisor of x and y
# ~; |& d& }. ]/ u; ?. b4 j - >>> math.gcd(8,6)
- @5 e3 ^% ^2 W9 }$ V9 c+ `$ f4 I3 P! n - 2
* Z& Q* E* d9 w: Y |) p - >>> math.gcd(40,20)
x' i$ Y1 }: D0 O2 e - 20/ e1 Z2 Q: M# a4 _
- >>> math.gcd(8,12)
f+ Z, n1 _! t! O - 4
复制代码 1 l. I3 {) F/ n& _2 h4 F
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False |& y" k2 u+ E& S* u9 ]
- #得到(x**2+y**2),平方的值
9 `7 q" ]6 {) N6 e - hypot(x, y)
, R8 k/ E e4 ~0 L4 O' i - Return the Euclidean distance, sqrt(x*x + y*y).7 n) l& H3 N) m' g& c( s
- >>> math.hypot(3,4)
! Y0 r( V$ {2 X( M - 5.0
: } d. Z% b1 B7 {8 _$ U) Y, P0 L - >>> math.hypot(6,8); I* a0 t ] y% Y+ u* A$ d
- 10.0
复制代码 , Q& ?0 I8 B6 j% S n; \
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 S6 H3 v6 k) }7 _, E- #如果x是不是无穷大的数字,则返回True,否则返回False
% u4 w2 G7 Q. X) y - isfinite(x) -> bool5 i! H( v8 b0 A' R( Q: w& `/ I$ F# p
- Return True if x is neither an infinity nor a NaN, and False otherwise.
& U3 Y4 q$ c; d7 c - >>> math.isfinite(100)
( a X- T" n! X" g/ m - True4 U: z+ O$ H O! x5 g' w, g
- >>> math.isfinite(0)
- C7 @- R8 B! G0 r - True Z$ A# E& h( F
- >>> math.isfinite(0.1), C4 }) ?: H8 Y$ [
- True
" O. c: t& w5 x6 ~# Y% F9 J - >>> math.isfinite("a")* t! r. B# T0 A
- >>> math.isfinite(0.0001)
7 U7 o- L2 s+ w* g( o8 n) m/ K - True
复制代码 & \' { d: N" e) g' j
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
) r `4 j) H g: l3 ]3 I! S- #如果x是正无穷大或负无穷大,则返回True,否则返回False8 E' Q6 `0 l2 A& c; T
- isinf(x) -> bool ^" ^1 V5 J/ ^6 F% o$ ?: y
- Return True if x is a positive or negative infinity, and False otherwise.
" i! t% y. [" C: N! o0 h - >>> math.isinf(234), t6 C, T" X9 C( \' @ @( p R
- False/ @( k3 _+ P- j: {
- >>> math.isinf(0.1)' |! T3 S2 L. K' D4 @5 i- O
- False
复制代码
, h$ x8 s" k, q7 U# ]4 B( xmath.isnan(x) 如果x不是数字True,否则返回False
; m/ M* L: o/ j$ R+ R- #如果x不是数字True,否则返回False" [$ g( ?. B# i+ ~2 z
- isnan(x) -> bool; @1 F$ a5 o; W' g/ D8 ]+ v
- Return True if x is a NaN (not a number), and False otherwise.% A! d8 M1 O0 O( S
- >>> math.isnan(23)
1 M) T+ Z8 V/ X' F - False
, P% c+ W- E& k- a: j. q - >>> math.isnan(0.01)9 [8 p5 _, W! p6 I
- False
复制代码 4 A+ r5 }% ]4 w- d Q
math.ldexp(x,i) 返回x*(2**i)的值
7 H5 l. s! ^) Z' Z6 ^0 ]- #返回x*(2**i)的值; o% y) B7 b) ^. _- I' m% O( J7 E
- ldexp(x, i)+ X, z4 G/ [' q) O6 S2 J- p
- Return x * (2**i).+ H- P! u7 F5 B9 b+ Z' W M" ?. s
- >>> math.ldexp(5,5)! ~& z% r/ e# ^. k" C* T) A
- 160.0
& N5 \& U( n# O j: M! D$ O - >>> math.ldexp(3,5)
* { |; V3 Z6 W9 X+ v - 96.0
复制代码
: U4 u. D# e$ H' ?; Jmath.log10(x) 返回x的以10为底的对数# N/ N, L* F% @( A P6 I# ~! K
- #返回x的以10为底的对数$ E3 t+ o) S7 Y- X# q
- log10(x)
1 y3 y/ o* _+ H, Z5 C J0 r% E - Return the base 10 logarithm of x.
& T, [3 m* a9 N* } - >>> math.log10(10)
3 B y' }0 C2 o6 S. a - 1.0
& r+ U6 Z+ V5 l$ T - >>> math.log10(100)
8 W2 k2 V9 O" X - 2.02 S$ a# [0 c* U4 k! \3 S
- #即10的1.3次方的结果为20$ w3 N) x" I/ G# s3 n9 C
- >>> math.log10(20)1 J7 F/ n: x) o7 h
- 1.3010299956639813
复制代码 + P# u! W% V' d3 h/ w; i( C4 x
math.log1p(x) 返回x+1的自然对数(基数为e)的值3 N+ F+ P0 y9 i, \" J
- #返回x+1的自然对数(基数为e)的值9 D7 I. d+ ]- i) w# a! P
- log1p(x)- Q8 y: E* D8 |" q
- Return the natural logarithm of 1+x (base e).: c2 ~$ r1 _5 r7 a; W7 b& o
- The result is computed in a way which is accurate for x near zero.+ T* m9 r# e3 W9 [
- >>> math.log(10)6 Y% f) i: J, ~9 b5 H' R
- 2.302585092994046
; A& k) @/ u' P7 j - >>> math.log1p(10)3 V$ C$ s! `) b- X" w
- 2.3978952727983707
& w# q# h& ?& i, J - >>> math.log(11): R) ~' Z0 |# q5 d
- 2.3978952727983707
复制代码 " n; g# @1 Q& ~, v
math.log2(x) 返回x的基2对数
7 r* J4 c! O! l- #返回x的基2对数
8 e( i: X7 \" O, ? - log2(x)
6 N3 G0 N$ q. W& [) x/ y5 U - Return the base 2 logarithm of x.
* {* Z: v0 r6 g+ Q7 X - >>> math.log2(32); A3 Y# p+ @* w9 C
- 5.0
, q, }) _! m, U( [ - >>> math.log2(20)
1 o. v- X H- J. \ - 4.321928094887363# {$ E/ J j; k/ }4 C3 v: `: m# a
- >>> math.log2(16)
3 s2 L% L1 X' H5 U I, C( [ - 4.0
复制代码
) o4 M3 c: M4 ^$ ^: T: jmath.modf(x) 返回由x的小数部分和整数部分组成的元组
( D( h, C: u- ?2 H* }2 h5 A/ t- #返回由x的小数部分和整数部分组成的元组
) J; F, H% r' V. D' H1 M3 g; d - modf(x)3 M R9 s v5 D( o# X
- Return the fractional and integer parts of x. Both results carry the sign
8 l8 k2 \# H/ {) j9 x - of x and are floats.
7 x! U! X4 d/ n - >>> math.modf(math.pi)+ x+ u0 Z" Y3 e! _& _
- (0.14159265358979312, 3.0)# e) Z& p) Y' k3 o
- >>> math.modf(12.34)
, H, m9 l# i5 K1 z' y" q/ w D - (0.33999999999999986, 12.0)
复制代码 ; t3 i/ o& c R% l
math.sqrt(x) 求x的平方根
2 q. K& V- F1 Z( [9 o- #求x的平方根
9 ]+ o! ] Y0 [( k; c - sqrt(x)
8 `4 S& ]4 q$ v& y4 v8 A5 p" h, R - Return the square root of x.$ |* S0 p4 U" U; [% _2 D. F9 z& F* k8 @
- >>> math.sqrt(100)* I( W9 l n+ T1 E8 [* y
- 10.0. }; E5 a. I3 U, p q
- >>> math.sqrt(16)( l6 u8 Y7 B9 y3 x
- 4.0: q7 q* Z. s) t Y3 G+ r
- >>> math.sqrt(20)+ S. s g1 }/ y" |" v7 M- g$ u
- 4.47213595499958
复制代码
2 g y4 E* b& H6 I3 |math.trunc(x) 返回x的整数部分- #返回x的整数部分. i, j4 V/ @9 O% |- ]' g
- trunc(x:Real) -> Integral/ e! k U1 ?7 a
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 b9 D, }; x# z7 o
- >>> math.trunc(6.789)$ I$ P7 ^0 B& t) f
- 6( a3 d# G! S8 s6 u: w4 j
- >>> math.trunc(math.pi)
! [: p: Q, |5 j7 h5 @- H - 3
; B( M+ c& F9 A& _! K" z' t - >>> math.trunc(2.567)% Q* o0 D% V) R- n7 D
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|