马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 Y7 Q8 z( e: C6 ~( p0 K" S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。 L$ q7 ]$ h, C; U9 k
$ q* Q- P( N6 Z
方法1:
* y, p: J& ~# J- >>> import math
0 [# {. U( M1 I$ z3 v$ Q) t - >>> math.sqrt(9)
; R9 x: u8 V; l; U: N7 L - 3.0
复制代码 方法2:
* I# ]4 m7 n' k6 v! `0 s# r- >>> from math import sqrt$ m2 ?6 }- e6 b$ \6 _/ e1 a5 I
- >>> sqrt(9)# }1 \$ `0 {# T- s- a
- 3.0
复制代码 $ |, m6 S5 ~7 h
: F% t" t# G4 y! t9 w2 H
math.e 表示一个常量$ ]) E6 w2 Y4 l' U/ {3 U; B( p
- #表示一个常量3 ^+ U; U5 Y5 Q. x9 ]; H7 _
- >>> math.e
* ^5 r# C- O! D, X: ~1 `' J' [9 r - 2.718281828459045
复制代码 " _+ t# {4 r0 J! Z3 ~7 a
math.pi 数字常量,圆周率8 e5 `8 \9 C2 O* c9 O
- #数字常量,圆周率
( A+ b; U* d" S3 x9 [ - >>> print(math.pi)* ~% q! b0 I- ^2 R [
- 3.141592653589793
复制代码
! e" f+ X) X0 w# m( F2 w8 h1 ymath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x! Z% n+ Z& [% _! [' K7 X" {; R; a
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
% |! U2 \# x" L+ S1 T9 L - ceil(x)2 Q7 i7 S8 X! W
- Return the ceiling of x as an int.- C+ K5 T% ?/ n. t/ I, O- Z+ q
- This is the smallest integral value >= x.
# R2 d1 Y& Z: y) b( r - / i; _- z4 v7 ^, v3 H6 y& d/ y
- >>> math.ceil(4.01)
; k- `# i: q' e! K8 ~# ?% j0 N+ j - 5: Z0 ~" R+ I* o9 K
- >>> math.ceil(4.99)
% k! a: ~. Q) o3 u+ c' o4 | - 5
# |8 ^- V$ M7 P R5 j3 ^ - >>> math.ceil(-3.99); m' s6 @9 b% f1 E C; c
- -3
. k6 T; S; m0 b; D# g0 z - >>> math.ceil(-3.01)/ B- S% Z4 W0 o* Z* \
- -3
复制代码
, J2 e% z" I- }5 ?1 U1 j+ |math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 |7 @9 E+ |& F7 ^/ g+ M1 Z) i- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
i7 l y3 u* h8 }: s1 ?' a' l - floor(x)2 U; f9 J! Q6 |3 A9 u" @/ n
- Return the floor of x as an int.
, Z( U- i, |& P8 X6 e* B( t3 t - This is the largest integral value <= x.
# t' c7 M* M& [8 L/ ]( { - >>> math.floor(4.1)
* I+ v' a- n. O. w5 ] - 42 `1 G3 R2 v# d# G5 o$ _9 T& e, P
- >>> math.floor(4.999)' P: z: z# }- K- d" p
- 4
/ Z E. ]/ d `& {& Y - >>> math.floor(-4.999)
( P! W6 Q# g0 M0 |! R/ y* d, t/ b - -5
' `2 a# P; r7 s4 U/ m+ R - >>> math.floor(-4.01); u. R! J' W- y% b0 ~
- -5
复制代码 - b" X( }" F5 j+ e, i8 R/ O
math.pow(x,y) 返回x的y次方,即x**y4 n, |# [: C& H1 M! `6 m
- #返回x的y次方,即x**y
- C8 A) D/ ~: P% f, z - pow(x, y). p `- w2 {; r
- Return x**y (x to the power of y).7 u& J. `) t. H! M/ g
- >>> math.pow(3,4)! d0 a3 @# v" D4 b9 u% i
- 81.0$ G& K3 p3 x: M. o
- >>>
$ k/ I0 w& K S- T* R - >>> math.pow(2,7)
3 }" w' u& \# F- r" ?; v - 128.0
复制代码
+ |5 g. o7 s. _& P. mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 |/ ]# U, ~8 J) t& g- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 m2 u( j4 n# {5 ^
- log(x[, base])& d) x+ U$ V, M8 R: x
- Return the logarithm of x to the given base.0 s4 W1 \3 I) I1 B' V4 E6 W
- If the base not specified, returns the natural logarithm (base e) of x.
% n' v V$ q2 a" Y( ^7 A - >>> math.log(10)
1 R9 ^ W; n7 T0 l& o; f6 }& u* Z - 2.302585092994046
- b6 B* W0 p+ E$ \ - >>> math.log(11)) m5 k: E4 {- h Q' H, p, F) T J) C
- 2.39789527279837072 y$ S5 S. w2 O7 U$ Z; G* S; _! C3 k
- >>> math.log(20); y! L* H. K; n7 U
- 2.995732273553991
复制代码 2 J2 e2 O% u- `' d3 U8 I
math.sin(x) 求x(x为弧度)的正弦值
6 M* ?3 h+ ?% W7 J- k9 B- #求x(x为弧度)的正弦值
0 Z! ~4 J/ p0 } - sin(x)
1 M0 K) `& O+ F ~/ l. r" x4 j - Return the sine of x (measured in radians).
+ l" V6 i: K! e6 A2 I9 i/ F& P - >>> math.sin(math.pi/4)6 r; q" ^5 Z# R
- 0.70710678118654756 v# ~9 h( _7 ]$ k# s
- >>> math.sin(math.pi/2)8 l. Q5 f }3 | b$ G; M/ L
- 1.0! W: X6 \) B4 b
- >>> math.sin(math.pi/3)
7 N8 l0 Z- d* r; D; C' t" P - 0.8660254037844386
复制代码
3 I( N; A! n% X" p) G' z& W, k& Tmath.cos(x) 求x的余弦,x必须是弧度" N( I# r8 S0 ]' y2 m
- #求x的余弦,x必须是弧度
: ?9 I+ o. _9 B* |! a - cos(x)
; @, G, v+ |1 \& t/ s' H# f( f2 U6 R - Return the cosine of x (measured in radians).
( X+ i: f2 }5 l: M - #math.pi/4表示弧度,转换成角度为45度
+ E6 c3 V {$ r - >>> math.cos(math.pi/4), ~! a" b4 O6 L6 R2 c5 [+ ~
- 0.7071067811865476
2 V3 P: o! P, R2 ^9 }3 B! H) _ - math.pi/3表示弧度,转换成角度为60度
6 o; x% `' K) b* `" s2 F - >>> math.cos(math.pi/3)5 u1 f1 S0 u6 [
- 0.5000000000000001
3 T0 x7 Y3 Q: F) @% p - math.pi/6表示弧度,转换成角度为30度6 d3 N$ b) H; T( E5 ?) E4 R" s
- >>> math.cos(math.pi/6)* a; x6 N+ i. l& v6 C0 T
- 0.8660254037844387
复制代码
. m" T {8 }# J2 x& H# y+ S5 wmath.tan(x) 返回x(x为弧度)的正切值3 t$ n3 x0 ]4 Z( C
- #返回x(x为弧度)的正切值5 m$ {: r+ u' f, J
- tan(x)4 G4 j n( @" Y$ h
- Return the tangent of x (measured in radians).5 k+ J+ Q0 u! M+ [8 y- u! D# p. F
- >>> math.tan(math.pi/4)
5 z; v3 T1 o: g0 E7 }# ^' C - 0.9999999999999999
' M q ?3 ?! C F( N7 A - >>> math.tan(math.pi/6)% Q4 L4 l" V/ D0 j( X4 P3 x
- 0.5773502691896257
" }7 A, S) k W# Q3 e4 b. o8 D - >>> math.tan(math.pi/3)
% F+ l f' F" K: M2 J5 K - 1.7320508075688767
复制代码
( E, A7 d+ F/ R& J u7 O' Nmath.degrees(x) 把x从弧度转换成角度
+ W9 F1 v; f" A1 w- #把x从弧度转换成角度
; h7 O D) N. u- y - degrees(x)
8 a- m" m3 m. E* s' T8 G+ W" c - Convert angle x from radians to degrees.4 N) x5 W A0 ?3 o/ N
& y6 f' x5 X' S- >>> math.degrees(math.pi/4)
7 Y0 W L4 ` ~. j+ O. {% c+ S: P& E - 45.0
# {& Q: [$ d9 I% ~7 a9 Y - >>> math.degrees(math.pi)9 _ d5 }9 X. Z: y H# q5 B) m
- 180.0
3 J; Y+ z! H2 m8 k$ B: L - >>> math.degrees(math.pi/6)* [3 q, d; v6 i' P. I/ I, L1 r
- 29.999999999999996. @( j; d4 _4 C
- >>> math.degrees(math.pi/3)
0 |/ T6 _! d" f1 G - 59.99999999999999
复制代码 . O5 i; Y9 Y. R6 \ k3 A4 y
math.radians(x) 把角度x转换成弧度1 Z6 U" p' Q7 z
- #把角度x转换成弧度+ Z& W% `- _1 C8 q1 @
- radians(x)3 p' \$ R, [4 C* y% q+ }2 j
- Convert angle x from degrees to radians.
' Z9 I/ g1 _1 K& e+ P7 d; y - >>> math.radians(45)9 N# ?; G6 G# E/ J2 n1 c
- 0.7853981633974483
+ y! }6 Z2 U0 o - >>> math.radians(60)
$ C8 w7 Y7 T5 |0 w6 s5 [& [ - 1.0471975511965976
复制代码
+ L9 G1 L4 H- W/ h; s& W0 Cmath.copysign(x,y) 把y的正负号加到x前面,可以使用0: S5 T- [$ D5 c+ b
- #把y的正负号加到x前面,可以使用0/ ?. X# Q( y1 ~' J
- copysign(x, y)
) \3 `3 K9 c1 q N, v- y8 { - Return a float with the magnitude (absolute value) of x but the sign 1 w! q0 Y7 p* L& `8 f
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
( ?: i9 ]# C9 D6 P' D4 s3 T - returns -1.0.
2 E0 o, l7 u* _$ ^
! y5 K! @+ R3 y% Y1 T1 z* o t- >>> math.copysign(2,3)
1 h2 @' L f3 x. F, x - 2.0; E* [5 o2 W' A- \
- >>> math.copysign(2,-3); E) c" R- G, _+ H j
- -2.0# l4 ?: A* q/ v3 Z
- >>> math.copysign(3,8)6 Q/ s! g4 s% w) j
- 3.07 K* w" e/ A, e1 g+ ^
- >>> math.copysign(3,-8)
- l$ u. v: k$ ]; j7 X - -3.0
复制代码
* C& _1 G7 ]' b5 o" M8 K0 t7 ]5 Emath.exp(x) 返回math.e,也就是2.71828的x次方7 t+ @8 `/ Z' `% J8 h) x
- #返回math.e,也就是2.71828的x次方
- e9 A& l ~6 f4 o: q - exp(x)- b( x: t0 }, t v
- Return e raised to the power of x.
: u4 o' T$ ?9 p. ~( e% j
$ {+ }0 |2 g/ x6 l+ U" `. _- >>> math.exp(1)
5 T& D9 R5 l$ K$ V3 `! b* u- z/ y - 2.718281828459045
8 T! S# o/ N; s- I# { - >>> math.exp(2)
A) R( I1 C. k& n J: p9 P - 7.389056098930652 a d) e. m) h. u3 q+ f5 F
- >>> math.exp(3)" R2 O, g% }, F5 r7 _
- 20.085536923187668
复制代码
. Q" o+ x, M) M) Rmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
4 i) h: Q* s# A- #返回math.e的x(其值为2.71828)次方的值减1- o) v( q) o7 x+ E7 T# o
- expm1(x)
( p9 i+ U+ r' U- e, p" K0 l - Return exp(x)-1.
+ n' _% {' {, R+ \- c" \; Y - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ D' z! `8 C3 |
- 9 z7 w D- F: H! t8 T# c
- >>> math.expm1(1)$ ~! z+ N% u2 I5 f7 n0 U
- 1.718281828459045# v( G+ k1 _/ @4 `& v0 U" W9 H
- >>> math.expm1(2)5 o8 a- }- f) y; _/ {- n
- 6.38905609893065
) K& l6 G* W: J - >>> math.expm1(3)
8 s1 C! i# I5 r4 z5 g2 ~ - 19.085536923187668
复制代码 ( h# r7 u/ O0 |8 k
math.fabs(x) 返回x的绝对值& K$ ?7 ~3 |6 R$ H, x
- #返回x的绝对值
: d0 e/ t0 c9 h+ G- _; Q - fabs(x)
- c3 P( |' [; b( }8 F5 W- c - Return the absolute value of the float x.
% p* r, z* N6 e: O) A1 r
; p$ m* W) j! o, O- >>> math.fabs(-0.003)
+ r! J8 q- R! @' P3 t - 0.003
! _" Z2 [ A" H) u - >>> math.fabs(-110)) w8 i V3 p$ Z/ W# y9 K$ s$ [8 ?7 N
- 110.0
$ O* v8 o3 R& x u$ n( c6 z - >>> math.fabs(100)
6 g- H+ S" w" \) i/ g5 a+ \ - 100.0
复制代码 * n: e, q' v+ s
math.factorial(x) 取x的阶乘的值
( m- ^# `( S/ y/ c$ k4 T- #取x的阶乘的值! J3 R8 f' d2 c$ P R8 I- a W
- factorial(x) -> Integral
8 M* ]7 _) i" q9 J" J) D: C4 q - Find x!. Raise a ValueError if x is negative or non-integral.
3 D' E! x8 I' o, X0 {( O - >>> math.factorial(1)6 U* u- N. W, k
- 1- a+ n/ {6 ]: |8 Q. p) n0 X5 G
- >>> math.factorial(2)
: c7 J- x5 l8 F# M5 P! C - 2# s: q O2 {; B# M# n
- >>> math.factorial(3)) a. x5 _% ]2 h6 |% ?& f I
- 6 u3 t/ w1 n* k: D4 X
- >>> math.factorial(5)2 H' d6 \. z1 x
- 120
u' o( n' z% c: e - >>> math.factorial(10)
+ R5 q1 T9 M `* p+ t - 3628800
复制代码
6 D" u( J5 Q! emath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
0 y. _, _0 |8 D! q! k) [- #得到x/y的余数,其值是一个浮点数* f( B; p% Z+ ~6 U$ ]# x0 A
- fmod(x, y)
+ y6 C' H4 O1 k - Return fmod(x, y), according to platform C. x % y may differ.: A7 m$ m) v- K* t) U
- >>> math.fmod(20,3) ]0 X: N2 c% S- C" k! w9 R
- 2.09 _2 [. |7 p! _; U1 f+ v
- >>> math.fmod(20,7)
# c9 J# O2 @9 i3 t+ m* Q" }7 ? - 6.0
复制代码
) B. p' s% @* @2 Emath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
& g K$ F1 c/ s- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- A5 H% |" M B' q' E
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
) `" W9 M0 x. B' o+ ~ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
c5 m) a- M0 ]: F. i g0 L; J4 E8 K - frexp(x)
: x' \9 }2 k* ]" G4 ] - Return the mantissa and exponent of x, as pair (m, e).
" V1 H9 B, u% k6 Y - m is a float and e is an int, such that x = m * 2.**e.2 n# Q4 l+ C, g) Y
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.# w* W/ p$ L' G
- >>> math.frexp(10)
/ _& f/ j* Z- u - (0.625, 4)$ Q" S: B, Q. _( W7 _( z7 @1 {
- >>> math.frexp(75)- ]. i C+ J T: j/ g
- (0.5859375, 7), _) y3 _" J1 s! ]2 b
- >>> math.frexp(-40)' X/ \3 [6 n+ O0 e, F2 k
- (-0.625, 6)0 N) e' c% h5 h+ g' x2 ]7 I/ B
- >>> math.frexp(-100)" v k. z( S, A! j6 M& q
- (-0.78125, 7)+ S$ G X' t; b
- >>> math.frexp(100)4 p) }1 e# H0 t7 ]
- (0.78125, 7)
复制代码
9 Z; Q. n# u& ^* _math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列) j D3 {+ ]$ N* j3 Q/ L. L
- #对迭代器里的每个元素进行求和操作
1 G( r0 J% p5 Q, W7 t- x) w - fsum(iterable)
- }% l) S; I$ l) U - Return an accurate floating point sum of values in the iterable.
: Y2 Z) s8 d" v- v - Assumes IEEE-754 floating point arithmetic.
% D* z& D% I- S, u1 W - >>> math.fsum([1,2,3,4])
* t. O3 B! a8 J% J. _, L- Q6 l+ r - 10.0; p' b% h. q. E
- >>> math.fsum((1,2,3,4))
/ |/ j/ I$ d& o& C# D - 10.0! z8 u+ T: F2 `. r
- >>> math.fsum((-1,-2,-3,-4))- E7 \7 d- ~# Y2 P
- -10.0
9 g3 p) d Q% A1 d1 l4 I0 @ - >>> math.fsum([-1,-2,-3,-4]): l. ]1 L' t% H4 ^
- -10.0
复制代码 $ G3 m3 U) N2 o3 B
math.gcd(x,y) 返回x和y的最大公约数
c5 |% G( }' H& U' }1 X- #返回x和y的最大公约数8 V2 U3 v! {; ~2 R0 Q8 j! ^. K! q0 }
- gcd(x, y) -> int& O& M& a9 s+ k; }
- greatest common divisor of x and y m3 p* H/ m( `
- >>> math.gcd(8,6)
, L( [. @3 e% F - 2' g: Z. h4 C. @! L# S& r5 o L
- >>> math.gcd(40,20)
6 c6 z& ~& o6 ~& U - 20
* `5 D) L1 p) \' J! {3 p+ I; S) o - >>> math.gcd(8,12)
4 u! R$ c, j' j6 b - 4
复制代码
3 j% w/ A5 N* d% `/ hmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
' S, w+ D" N' r( S' N- #得到(x**2+y**2),平方的值
7 X% l" d. I( |, s h1 R9 i - hypot(x, y)
( q9 V" C/ `- y' z5 j1 _5 v" Q - Return the Euclidean distance, sqrt(x*x + y*y).
' \! C6 ?; i/ G$ h- H% b' f) T1 r - >>> math.hypot(3,4)
% E u9 Q# K' `$ t5 k A4 X - 5.0
. K C0 v3 a0 L( j3 \3 G - >>> math.hypot(6,8)% A& V" W! u8 M1 I" T2 v
- 10.0
复制代码
: J. ?* B- @+ }1 u, Q, B% W8 D- q imath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
' H: b0 ~9 S: i- #如果x是不是无穷大的数字,则返回True,否则返回False
2 X4 C; s* n N$ B - isfinite(x) -> bool" E: f0 O4 @% K1 v* u
- Return True if x is neither an infinity nor a NaN, and False otherwise.
: S2 x) n# `9 T4 u9 g8 Z# v/ H) M - >>> math.isfinite(100)" q7 ? f" E0 h0 _/ a. Y' w& k
- True5 x- D, v4 [3 n* X
- >>> math.isfinite(0)
! l. K) n0 H/ ~ - True& W$ ~' N% \7 q+ n a6 R8 p
- >>> math.isfinite(0.1)7 `2 m+ y" K* m' S6 q7 m
- True
" O G/ G) Q1 l% \ - >>> math.isfinite("a")
* y( k$ ?& `' E - >>> math.isfinite(0.0001)
( b+ D4 ~' R9 z, v! _1 Y - True
复制代码 , z+ d( O5 ?5 I1 l, E
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
* o; }1 \9 Y/ Y( L2 j( w& I1 y& {- #如果x是正无穷大或负无穷大,则返回True,否则返回False0 M8 m9 W# J. ?1 X8 y
- isinf(x) -> bool' J# B+ o( ~% _! w- x H
- Return True if x is a positive or negative infinity, and False otherwise.
' U& u8 p* @4 _( ^ - >>> math.isinf(234)
* n$ _6 o( z+ }$ V& b - False
) F o/ B/ S: z# p6 {8 L2 ]* c- X - >>> math.isinf(0.1)
* u9 _% p# t/ J. _& I& ?7 V+ P - False
复制代码
. _2 W: x O$ ~5 A6 Z( ~math.isnan(x) 如果x不是数字True,否则返回False! y$ X4 a/ o3 ~9 o! q0 N% W
- #如果x不是数字True,否则返回False$ G1 H# Y5 ^! G) |
- isnan(x) -> bool
5 x4 O/ I- O+ |* T; y - Return True if x is a NaN (not a number), and False otherwise.
$ J( s: y1 t# }% i" }* T% j - >>> math.isnan(23)9 D* M% y. u/ x. @( k1 Q2 L3 U
- False) {" u1 ]& S D6 U5 y& k
- >>> math.isnan(0.01)' K/ t( v V) I. P
- False
复制代码 0 \" N0 t3 p# [5 t) d" O2 a" S
math.ldexp(x,i) 返回x*(2**i)的值1 H7 |" L* q0 t/ J
- #返回x*(2**i)的值 d0 e4 p0 _6 X- U
- ldexp(x, i)
7 K8 J( V$ k3 x/ J$ V5 r - Return x * (2**i).
7 j& f6 h$ Y, J( _ - >>> math.ldexp(5,5)( P0 t% i" T/ n K& j& J
- 160.01 t# m2 L8 {+ n' F& A
- >>> math.ldexp(3,5)
1 d6 j' S8 s8 N2 I0 g0 c - 96.0
复制代码
* U3 I4 {- ] {7 w2 Fmath.log10(x) 返回x的以10为底的对数
% o, }& j! O' b3 a, C' }- #返回x的以10为底的对数: [" ?/ N o. J) \
- log10(x)
) B0 J& f3 V4 n! s - Return the base 10 logarithm of x.
0 ^: Z8 Z* u I+ y& u6 h# h - >>> math.log10(10)
" ?6 j. |& h; o! [ - 1.08 s% C- ^) F6 L r: D4 z# Y1 I
- >>> math.log10(100)
" R: `- d8 Q# ]4 F; k - 2.0
. }: `- l6 f* s* V( { - #即10的1.3次方的结果为200 w4 r/ O4 X# j* E) o
- >>> math.log10(20)
2 m ~4 e# N% f; p* x c8 A - 1.3010299956639813
复制代码
8 B2 e, F3 _, J9 A% d) u1 d6 r0 I( I7 umath.log1p(x) 返回x+1的自然对数(基数为e)的值
4 ~) M) x6 }7 z1 `& J( h- #返回x+1的自然对数(基数为e)的值
9 I5 r; K1 g0 M - log1p(x)6 f2 D* [9 s1 Q5 p* W: ^$ w
- Return the natural logarithm of 1+x (base e).
5 m, ?- T) b. O7 M9 w) l1 @: \ - The result is computed in a way which is accurate for x near zero., y3 j4 S+ d& ^0 m- T) U
- >>> math.log(10) Y- M( x1 u. Q: }+ a' u
- 2.302585092994046& l, {. ]2 @0 z" O
- >>> math.log1p(10)) O& l5 L* Y7 }4 i
- 2.39789527279837073 e! i. ?4 w( \
- >>> math.log(11)
m! G: i- V' [ k2 v( z! G, T - 2.3978952727983707
复制代码
) r3 i; k' P1 v( F% B4 Pmath.log2(x) 返回x的基2对数
6 K/ r: u) ?" `- #返回x的基2对数2 K R1 x( |$ r6 i# n: S! O
- log2(x)
& r3 ?. V4 m: x* {% n9 N - Return the base 2 logarithm of x.
8 O& u: U! P+ ~4 d, w" o; I - >>> math.log2(32)
' f, Y8 L) K* h; z7 \ - 5.0
& |0 ?0 ^: f5 v. S- }' w$ ?5 M - >>> math.log2(20)" S6 Q; K/ i/ i0 t$ @& V
- 4.321928094887363
. K9 f& D# c% i7 R0 O; q( D - >>> math.log2(16)
& F' p5 A. I1 Z5 p" A0 _' A' S - 4.0
复制代码
9 L/ e4 w) \$ M. `# a% P6 x6 Vmath.modf(x) 返回由x的小数部分和整数部分组成的元组
* i* i7 ?9 q) C5 q) n- #返回由x的小数部分和整数部分组成的元组
" X7 s' r% F1 V, X6 Z" u, x - modf(x): ?- j. g7 H3 P/ Q
- Return the fractional and integer parts of x. Both results carry the sign3 J' z8 g+ \% J) R, m1 t
- of x and are floats.9 O9 I2 j+ N) G' u
- >>> math.modf(math.pi)& K3 f9 J4 f; h9 _2 M$ `( e+ }
- (0.14159265358979312, 3.0)
1 b: _' N1 s3 h: t- X+ F - >>> math.modf(12.34)8 v. {8 _3 u7 n& {: @+ F; S
- (0.33999999999999986, 12.0)
复制代码 3 W2 Q5 J" [9 Q# Q6 L/ {/ H
math.sqrt(x) 求x的平方根
# I. V: h; l0 l b; U9 f- #求x的平方根; E+ H L9 h& Y7 C6 i1 J
- sqrt(x)
/ J' m8 D- X- n - Return the square root of x.
; b9 m" m% J$ c* a* V* o' j - >>> math.sqrt(100)+ A! k2 I) R1 m' |5 _/ K
- 10.0' @( Q. @5 ^7 q2 }% x p
- >>> math.sqrt(16)
! e/ |2 o: M) R" K y, ^ - 4.0
* d, V. H+ h c9 N3 {' I9 J8 r. r* ? - >>> math.sqrt(20)+ w# d1 A0 N- G0 \
- 4.47213595499958
复制代码
! f7 Y3 q, l1 O+ f6 S* y* V6 Mmath.trunc(x) 返回x的整数部分- #返回x的整数部分; ^2 R2 k# F6 I
- trunc(x:Real) -> Integral4 A8 `6 E8 x' C" ^2 X# \
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.8 E7 F$ _! e% |( S5 \5 @
- >>> math.trunc(6.789). ]2 k0 } r$ S \* Y6 @
- 6- b a9 G2 y6 a0 k# ?/ c7 }# B/ H- K
- >>> math.trunc(math.pi)
/ X8 T- E+ F2 p, C% t - 3& c3 @1 q: R! Y+ n
- >>> math.trunc(2.567)
, z7 v( M5 }) a - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|