马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" |, o% F1 N6 m0 v【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ [& e9 n4 O+ k; m: n( p5 } s B+ F6 A0 s* n. _$ L ~. G
方法1:/ y* t$ Q. E! K" G8 p
- >>> import math/ e+ D% N& ]* r4 E1 A7 \; I
- >>> math.sqrt(9)
. \( o9 y4 w* z7 C8 a2 A1 `4 X3 g - 3.0
复制代码 方法2:
2 {" e2 R# x- O+ x, e% r- >>> from math import sqrt3 w: w9 |- _. T2 g1 n
- >>> sqrt(9)
) b; B! R9 U8 |0 | - 3.0
复制代码 5 r5 C& w3 ?* J: K- U" q* t2 l3 e
- ~4 L5 V' S/ _4 E$ p9 n' ?' nmath.e 表示一个常量
. P! E3 R& u4 I. y2 w& Y- #表示一个常量
/ F* Q* p' p2 m# i6 d: c1 @" E - >>> math.e' c3 u+ |0 X- j' d, V6 ]1 R
- 2.718281828459045
复制代码
: J1 N3 B0 c8 s* bmath.pi 数字常量,圆周率" W& C5 C% J! c% O
- #数字常量,圆周率1 t1 I% J3 b, r* G
- >>> print(math.pi)
, F7 x& k% }' D7 A' F - 3.141592653589793
复制代码
7 Q8 n% I4 J; Z5 ]+ ~8 l& z, ?& [math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
" I8 e0 Z6 f; `! O8 @$ R+ u" g6 M- #取大于等于x的最小的整数值,如果x是一个整数,则返回x; f7 j* i* h( T8 T
- ceil(x)
% `; n4 ~1 I" V" {$ @) m9 n! @ - Return the ceiling of x as an int.% e1 w5 B/ u" q1 u& R* o7 \- B
- This is the smallest integral value >= x.
+ z T. `. L" _2 ~4 D7 B/ I" S - ' s: A5 ^- W+ p2 N
- >>> math.ceil(4.01)
$ p- t* I, k5 h. n - 5
* ^ ?5 N2 b! E$ N1 h0 m - >>> math.ceil(4.99): K9 x' @ k8 H8 H- r( ]+ {% |
- 5& ^0 T: G5 a4 n
- >>> math.ceil(-3.99)
+ `8 y$ W; u3 E% l" R - -3
+ {6 O7 ]7 q& R) |; W; k" M - >>> math.ceil(-3.01)
$ d" h4 F8 l6 A' V! `5 V - -3
复制代码 % @/ B% j7 p& D9 s6 H6 B
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) N1 X6 d2 K( { v1 {7 Y- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 q' R( U6 f$ z! |, l
- floor(x)4 f3 Q; i, z n1 D3 q7 n* { _
- Return the floor of x as an int.
+ }" j5 n4 ]' O- b - This is the largest integral value <= x.
6 S; X! |! }1 f - >>> math.floor(4.1)
' h. F1 T- }5 }1 T/ \3 Q" U - 4
$ E. o* |; L' [7 @ c - >>> math.floor(4.999)+ G; U; L* ?. d4 d
- 4. i1 `1 l, t( {9 t1 O
- >>> math.floor(-4.999)! i4 F& w0 H c) b( J0 f
- -5
& p1 ~- x) c8 P4 x5 p - >>> math.floor(-4.01)
: S9 u! ]% O: q6 k! J5 w" ~2 k - -5
复制代码 & O% ^: L; k! [$ A
math.pow(x,y) 返回x的y次方,即x**y
0 ^ I4 ?% w8 ~% B. Y. M8 D- #返回x的y次方,即x**y Q) Q! l: `& l$ A- k+ D
- pow(x, y)3 Q2 [4 k% x7 e
- Return x**y (x to the power of y).2 k c+ {" a, \: u8 i6 l+ j: E; t
- >>> math.pow(3,4), |7 [* `% i; H/ ^: r0 q( d4 q8 P
- 81.0
9 R) R9 M- P# G6 Z - >>> , m F# i% A# R1 L9 m+ u: e
- >>> math.pow(2,7)
/ j; C1 v( Y: z) T7 P5 Z& |& _ - 128.0
复制代码 # H- h9 d$ E/ s- q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# B1 O$ Z' B) ^5 J8 Q4 c) J+ I) p- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 U9 ?/ @: r' Y
- log(x[, base])6 W4 a( R/ J, i/ ] P# H; P
- Return the logarithm of x to the given base.( B* Z7 j& Y7 c6 y2 x
- If the base not specified, returns the natural logarithm (base e) of x.
8 w. H% X5 z X0 {$ @; k - >>> math.log(10)
]" w& i3 ?- l8 E2 Q - 2.3025850929940462 G1 [ I- L8 ?( _
- >>> math.log(11)2 b& O+ U; q+ _* i$ V% K. @/ {
- 2.39789527279837074 Q3 \ W5 X. R
- >>> math.log(20)
5 h; l4 w: K7 O) m) O* H - 2.995732273553991
复制代码
[, G: x7 o- ^! E# f' O. h* xmath.sin(x) 求x(x为弧度)的正弦值: u d' X2 D4 |) b+ I( g
- #求x(x为弧度)的正弦值
- {; y) ^, e3 D - sin(x)
i1 f, c, s" g# C& } - Return the sine of x (measured in radians).
! b/ l4 D0 u+ |+ \ - >>> math.sin(math.pi/4)
+ j5 f% Q% ?, Z& R! w. n - 0.7071067811865475
- `- q) V# d; s; P- k - >>> math.sin(math.pi/2)
7 t3 D; |' g5 C9 K, q1 s - 1.0
% i: o9 H; {3 i' ?. o - >>> math.sin(math.pi/3)
$ p: ?3 y( d5 _ - 0.8660254037844386
复制代码 ' F7 |% f4 d3 S
math.cos(x) 求x的余弦,x必须是弧度7 z. k3 A0 C# [
- #求x的余弦,x必须是弧度
. G m; @* G. q. \, R7 r3 W4 k! ] - cos(x); s5 k' F: u6 v* Q1 K5 ]3 s
- Return the cosine of x (measured in radians)./ F/ S9 K! I" B% P2 r
- #math.pi/4表示弧度,转换成角度为45度. y% Y0 @5 W/ R3 ^# X
- >>> math.cos(math.pi/4)
2 {, W7 _- @2 ? - 0.7071067811865476' \2 k+ h' c# r
- math.pi/3表示弧度,转换成角度为60度
6 K( _1 x; R) y, x - >>> math.cos(math.pi/3)
1 A/ y+ X$ h4 p; s5 y) ?) K. ^& | - 0.50000000000000010 [3 \% ~1 N+ k ]4 l: O% ] Z& [
- math.pi/6表示弧度,转换成角度为30度* @+ C9 L W" F( B2 f% g' s
- >>> math.cos(math.pi/6)8 z* M- N; l G" ~
- 0.8660254037844387
复制代码
: O+ e$ }0 l; j) _1 n* N. a* s' Qmath.tan(x) 返回x(x为弧度)的正切值. q2 F9 P# N/ j& G9 ?
- #返回x(x为弧度)的正切值- Y2 A6 K. k J+ C
- tan(x)
1 d! C6 ]9 G/ u( n9 w7 b" l! n# h6 K - Return the tangent of x (measured in radians).
5 \0 V+ m1 k! G0 H, r - >>> math.tan(math.pi/4)/ i6 P* f8 X Q
- 0.9999999999999999
+ M# T, R# H( B$ R. l - >>> math.tan(math.pi/6)
! U% q- Y8 h5 b* U( g - 0.5773502691896257* W) u% Y; _$ @4 M: q: ~. l
- >>> math.tan(math.pi/3)0 c, L- k2 e8 p6 M7 `2 g$ e: W
- 1.7320508075688767
复制代码 + T) ]; @7 d0 _8 [. Y- G( ~
math.degrees(x) 把x从弧度转换成角度; `3 C/ x$ _/ f9 v
- #把x从弧度转换成角度
' i$ {) U. g* y. R - degrees(x)
/ O/ p; c2 W( N" r2 l# t: q7 M( | - Convert angle x from radians to degrees.! X% z) [* ]% \/ w6 F4 t' k
9 j8 k2 z# m( A$ s4 D# M7 {7 h- e- >>> math.degrees(math.pi/4)+ R! B* M- Z+ _3 R
- 45.0
: B$ O! H) A K3 v/ b7 f - >>> math.degrees(math.pi)- D/ m! S0 n' a" I3 c
- 180.0 x( r7 w6 d( U. m
- >>> math.degrees(math.pi/6)( Q# Z0 A( p) M- ~5 A5 O% M
- 29.999999999999996
2 G0 \2 b; i9 B2 ~! t - >>> math.degrees(math.pi/3)
* C1 Y- c1 z `3 v) f+ j7 D0 j5 x - 59.99999999999999
复制代码 $ d+ k) }. i- o' g6 ?
math.radians(x) 把角度x转换成弧度
4 s( T- B; u' k$ ]1 ~- #把角度x转换成弧度
# K% M4 l" ]8 y$ M! ^2 l9 f - radians(x)
4 Z% r7 @8 t3 v$ q. W - Convert angle x from degrees to radians.$ Y) p) O% z L
- >>> math.radians(45)
, d6 _) S1 q6 _4 B. C# A - 0.7853981633974483+ Z8 N( b* j# W) _' u5 {* a
- >>> math.radians(60)
& V7 P3 o/ [& {* F - 1.0471975511965976
复制代码 3 ^/ P$ |5 b' _" l0 Z3 X6 J
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
z- }0 ]! ^1 K- #把y的正负号加到x前面,可以使用0
# y4 \; }( M' l% V! w P$ U: J - copysign(x, y)
- S+ O; m; | k, a/ g$ ~ - Return a float with the magnitude (absolute value) of x but the sign
* |. v9 [% A# [8 Y1 V' V! | - of y. On platforms that support signed zeros, copysign(1.0, -0.0) , G, X/ z3 l- J; V1 e" G2 M
- returns -1.0.7 ]' [, _8 W5 ?5 e; a2 [. Z+ v
, o) _7 ?0 A9 y! ?( {- >>> math.copysign(2,3)
4 z3 J, x. F. V8 R* s8 [ - 2.00 Z, f, _/ M f0 @6 G( Q5 m
- >>> math.copysign(2,-3)
+ D; f8 I4 ^6 s; n - -2.0, E) m8 \' O& c# ~( T2 [
- >>> math.copysign(3,8)
2 x: @8 G( a0 _6 X - 3.05 |9 x: R4 U5 Y$ \1 q+ N5 f
- >>> math.copysign(3,-8)
0 t3 a! P, [# w' x2 H - -3.0
复制代码 ! @. l5 [- g5 m& m
math.exp(x) 返回math.e,也就是2.71828的x次方. y! ]5 {3 L: L- k
- #返回math.e,也就是2.71828的x次方
; H; w# f2 ?" ?( K, l( l4 D2 Z - exp(x) A" R# @6 R7 o/ N$ e# L
- Return e raised to the power of x.
9 F& ^' K) Z: Y - & O& z% H' u' b/ u% b8 Z" x6 w
- >>> math.exp(1)
7 M( n z$ X1 E [, x( p5 ^ - 2.7182818284590451 Y+ T+ S( N1 w/ S2 A- u- b* L5 @
- >>> math.exp(2)9 z/ w* E. S2 i' C6 K4 r$ N
- 7.389056098930655 T* b& s0 L; t( {- T$ @
- >>> math.exp(3)
* I0 m. I( `" e0 l7 @: @6 T$ {8 ], e - 20.085536923187668
复制代码
2 }* n; Z, s/ [math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1- D- x% H: w3 G* P' i8 R
- #返回math.e的x(其值为2.71828)次方的值减1# |5 y- j* Q; I+ D- D
- expm1(x); X) d u- n3 [+ L
- Return exp(x)-1.! E2 L3 [: g& z* P3 z+ G# c- a
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
8 S+ D9 I+ z( p( J T9 b* ? O - ) K3 s' _' M) N- L4 ~) m! x
- >>> math.expm1(1)& q) z P1 l5 n
- 1.718281828459045
, ?4 x7 E7 f0 |( n4 r5 I - >>> math.expm1(2)- O4 O0 @' o" [+ g% V7 e# `
- 6.38905609893065# H- f# y$ {9 W5 m% P) z& h
- >>> math.expm1(3)$ {8 S& X2 t* X
- 19.085536923187668
复制代码 8 k* \; K+ O+ q4 Q! ?
math.fabs(x) 返回x的绝对值
+ U1 S b K* W6 ~( H- |- H- #返回x的绝对值
, a7 t5 Z/ W. d& v' @- p4 D! L - fabs(x)
# X5 D1 Y3 Q- c: g. P - Return the absolute value of the float x.0 ~1 L% I$ |: X, D5 g3 i
6 V+ b3 O+ v' R4 W6 M- >>> math.fabs(-0.003)
0 @* Y! B8 N5 V5 v! ?% f7 ` - 0.003
4 C, X' }; g( _1 X2 U - >>> math.fabs(-110)5 y) X7 X: x [' ]
- 110.0
# @9 t, Q: S# c+ s - >>> math.fabs(100)
0 E2 v7 G5 A" S* y4 Z, g - 100.0
复制代码 & B- @" z! C% d7 ~: ^
math.factorial(x) 取x的阶乘的值; t0 C: i* q( G3 U# Y! T! S$ q! `
- #取x的阶乘的值
! b9 Q6 z2 Z8 D6 j+ a. z - factorial(x) -> Integral- B( `% F- H* \7 `" I' i/ p
- Find x!. Raise a ValueError if x is negative or non-integral.
. m0 |( k8 v4 Q1 x. h( D [ - >>> math.factorial(1)$ O5 ?/ o2 s: U1 m, F. k: K1 a) p
- 1( K H6 B: m7 Z* A, S, m9 b
- >>> math.factorial(2)
* S$ d2 u) g" n0 O+ \6 m$ D - 2$ x% l9 Q7 E6 k+ s. A6 `' J5 C
- >>> math.factorial(3)! P; L4 G0 {- N% {
- 6. Q x2 e& [+ \5 u
- >>> math.factorial(5), \+ R! i$ p& l9 ^# {2 u3 d
- 1201 E$ X* i: u( F4 O% @6 A( r
- >>> math.factorial(10)" U3 z8 b$ L s8 Z
- 3628800
复制代码
5 v1 P: n; t% u$ |math.fmod(x,y) 得到x/y的余数,其值是一个浮点数/ h4 s! t6 W0 q$ D4 j, I
- #得到x/y的余数,其值是一个浮点数
+ H6 i2 D4 A! s9 g0 M) y5 t - fmod(x, y)
1 b* t2 w3 c, j1 w. k1 f/ { - Return fmod(x, y), according to platform C. x % y may differ.' @4 V5 d/ i6 ^* v
- >>> math.fmod(20,3)
7 P0 d$ L& F: W - 2.0
" @3 H! V' @( d+ t' z8 \ g - >>> math.fmod(20,7)/ ] O2 _3 k9 b% C3 O
- 6.0
复制代码 6 i& Z1 `) S4 h4 ]7 k% V
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
D4 V0 E3 |# F1 Z8 t- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( M4 M3 K6 m, X& O% S& x2 t6 S
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
& j9 T6 d2 n$ ^ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
0 |( J$ ^: i; ?+ e0 [ j - frexp(x)
4 \- N3 M9 m2 U - Return the mantissa and exponent of x, as pair (m, e).
3 c% @$ A3 C$ H0 Z - m is a float and e is an int, such that x = m * 2.**e.
2 a5 D0 x4 U' q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
: v3 G) @; i4 s. c' [ - >>> math.frexp(10)
& T3 X' v4 J: x3 h/ K; x1 s& t& T% _ - (0.625, 4)
& _: X- Q3 S5 R; r- ~( f/ ~ X - >>> math.frexp(75)3 Z1 S* Y% Y. F: _5 Y
- (0.5859375, 7)( V; P6 Q; M( m. ^" G; K" c
- >>> math.frexp(-40)& ~9 t7 @, U+ v% q/ r% V& _" Z& o
- (-0.625, 6). d; F5 g0 G, \6 j
- >>> math.frexp(-100)
9 O( q# X# m( |, O: d$ w - (-0.78125, 7)1 S% a6 \5 u1 K) v9 z) P" a
- >>> math.frexp(100)
}0 a! @+ A, k2 l( T - (0.78125, 7)
复制代码 % s( m! h, k* ^# W
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列) o2 t9 ~* w) @3 I4 h( ?
- #对迭代器里的每个元素进行求和操作
2 D/ u$ e3 O( e( b8 x - fsum(iterable)
: M, n! h6 G& A: Y) m; t* L, N - Return an accurate floating point sum of values in the iterable.+ @. g& u. t' x0 \5 J; \. J
- Assumes IEEE-754 floating point arithmetic.5 l. y# i. Y H6 s
- >>> math.fsum([1,2,3,4])
- j5 B+ w5 c7 m) T! n* _. ?( k - 10.0
- T) c- [ [7 n7 F8 C$ @ - >>> math.fsum((1,2,3,4))4 Y; f4 n6 G. Z# T! @, ?' @
- 10.0
+ L$ G4 n ]4 m- |; P - >>> math.fsum((-1,-2,-3,-4))7 f: M% r* g$ J# R6 q* K: p: i
- -10.01 X$ V4 d& ]4 x# A |( D" t: T
- >>> math.fsum([-1,-2,-3,-4])
, ^2 t& E% H4 ^; C2 K - -10.0
复制代码 0 T2 K: t. B1 K1 m, D
math.gcd(x,y) 返回x和y的最大公约数0 H. c' A! E1 {: N. Q
- #返回x和y的最大公约数* z R8 g# A$ x4 F+ ^
- gcd(x, y) -> int
% |% g" A3 T) l/ _! K/ t: H! z7 ^( ~) x - greatest common divisor of x and y
6 b/ [0 [2 C* K1 |9 q - >>> math.gcd(8,6)
, x V% d' f0 t - 2& x0 _: Z) u/ c7 h2 q! ~
- >>> math.gcd(40,20)( m: l5 \: P& t7 O
- 20* t6 d, b( s: f3 s% j4 D! m
- >>> math.gcd(8,12)
R* [1 u/ i; G9 w" T( k [+ {* L - 4
复制代码
7 I2 v" B ^ [6 g$ Wmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
6 O/ s. U& m) O4 Z- #得到(x**2+y**2),平方的值; z! M5 U2 ~4 w, v3 q% }4 `
- hypot(x, y)
$ m1 O) s! E8 j7 `& ~ - Return the Euclidean distance, sqrt(x*x + y*y).
) U7 e6 n8 J1 |2 A - >>> math.hypot(3,4)
: u. W3 R$ ~! e" d - 5.0
# C; X% O& H+ e# T! K - >>> math.hypot(6,8)
* }* E5 _4 |4 E/ W - 10.0
复制代码 , q a6 t# z4 H6 S$ P
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 H' F. ]0 ?" H2 n- #如果x是不是无穷大的数字,则返回True,否则返回False
/ O( ^2 t1 U% D r9 d$ K7 ` - isfinite(x) -> bool3 R8 t8 D/ x* C- ^# E }- l
- Return True if x is neither an infinity nor a NaN, and False otherwise.4 I6 m; O5 U' `6 i2 l
- >>> math.isfinite(100)
6 E. m, h' Q4 G7 a; N - True( ?4 [( n( O: O! T$ C
- >>> math.isfinite(0)
/ o$ H4 }. i3 S! s - True
* B9 I8 Y* i# j - >>> math.isfinite(0.1)
+ P8 q5 T. f9 x, p! Z - True2 I: }* A3 i. o+ P; m5 p/ `
- >>> math.isfinite("a")
$ q8 m7 F+ {, N, o5 n - >>> math.isfinite(0.0001): f, k( l$ F" T) u
- True
复制代码 - W$ m6 }2 f, C2 N
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False$ A, `' [% s# b0 e
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
2 j0 [: `0 I- {/ _" i - isinf(x) -> bool, `* O6 i8 x. i5 w: R5 B$ A/ k
- Return True if x is a positive or negative infinity, and False otherwise.
4 ~' D+ H/ ?. m/ K - >>> math.isinf(234)$ s, | O' l& F) c( \* b
- False
5 v' L; U3 _8 Z3 S/ I) n: V - >>> math.isinf(0.1)7 K% @, u% i0 `9 ~1 X( _
- False
复制代码 2 g! c, ?. I$ S9 ~9 r
math.isnan(x) 如果x不是数字True,否则返回False& ?; b) G2 s# J3 G5 v
- #如果x不是数字True,否则返回False
8 @5 ]/ ?7 z o: X: K* P - isnan(x) -> bool# U4 l/ \- d- J v
- Return True if x is a NaN (not a number), and False otherwise.+ z, Y2 j* x% Y
- >>> math.isnan(23)! _& s$ z& ~! e9 [" _' Q! c7 E
- False
: s4 L) N# a8 Q& d/ |6 Q - >>> math.isnan(0.01)4 F1 H+ [% x. |8 e4 @
- False
复制代码 & v+ W- y. y* F$ M5 E
math.ldexp(x,i) 返回x*(2**i)的值! V5 Z9 x- B1 w: h0 X8 v, P' N
- #返回x*(2**i)的值
! V. M! }) `9 k% f' Y - ldexp(x, i)7 b/ q" ]6 M' {, U. B! B
- Return x * (2**i).
( ~5 p7 B4 f! Z( v7 q( u+ p - >>> math.ldexp(5,5)
( Y. A5 e! @$ d7 I& u, d) }, O( Y - 160.01 U2 r, [ s* N& u$ ]
- >>> math.ldexp(3,5)
$ Y6 c# l+ ^+ l/ o - 96.0
复制代码
/ J! ?6 a6 o9 ?! m, o! Umath.log10(x) 返回x的以10为底的对数* n! O+ D4 M9 p ]! S3 E5 ]4 f
- #返回x的以10为底的对数
2 B1 k9 m6 M: w% S0 ^ - log10(x)* b* I' f6 Z! H% @0 p* B
- Return the base 10 logarithm of x.
8 K0 T* v* w( k' r: [, A - >>> math.log10(10)
" M$ V5 {, N% ` - 1.0
" s5 Z* B" S' s4 ^2 N V- m2 k - >>> math.log10(100)' G: p3 |# u7 @
- 2.0
0 V) E8 @5 v+ D' ^4 I% B& c - #即10的1.3次方的结果为20
* E3 U c7 s+ b# J - >>> math.log10(20)2 t" r8 \8 a2 p. W/ u
- 1.3010299956639813
复制代码
. r; G% K! b' v2 D) y2 Fmath.log1p(x) 返回x+1的自然对数(基数为e)的值8 m$ f, v( r! ^5 x& O
- #返回x+1的自然对数(基数为e)的值
! N+ l9 w! ]' K" w! O0 a) f0 i - log1p(x)
, }( o/ U/ x# z4 z) q/ f - Return the natural logarithm of 1+x (base e).
& n" m8 t) m; v; r' U8 X" R - The result is computed in a way which is accurate for x near zero.9 d9 y) P. R! I9 b2 G7 o' N
- >>> math.log(10)
1 z6 p% C. K9 x9 c6 u" w' N4 h - 2.302585092994046
9 r, l7 E( i! R) t - >>> math.log1p(10)
9 ^3 `" I) K/ @ - 2.39789527279837079 t | r4 m% P( d
- >>> math.log(11)- K7 t$ Z/ v, {# i% Z# Z4 j4 ?
- 2.3978952727983707
复制代码
1 J- r, L2 t& Y2 |9 G4 Vmath.log2(x) 返回x的基2对数* p$ }5 A/ m1 V$ G$ s( u- L9 S; T
- #返回x的基2对数- ?1 @0 Q" P! f0 Y% ~5 Z( P
- log2(x)
! r7 c2 v' P" |" H( @# x - Return the base 2 logarithm of x.
7 Y' m* t( z p9 t - >>> math.log2(32)3 F. o+ Y, }6 ?! S) M+ N
- 5.0: ?0 @. F0 r! {+ z
- >>> math.log2(20)! y! V, C1 o7 B' N8 x" n! n
- 4.321928094887363
; S j$ |" v* u$ D3 v - >>> math.log2(16)
' ]" w' ]# j8 n. M - 4.0
复制代码
; `/ | t: W4 f& C& w# tmath.modf(x) 返回由x的小数部分和整数部分组成的元组
6 y2 p$ e- X& |- #返回由x的小数部分和整数部分组成的元组+ D8 J6 H' _; M3 b, H( U
- modf(x)7 Y3 G$ d+ }# q* s I& N
- Return the fractional and integer parts of x. Both results carry the sign. E. i* {; s4 O3 J
- of x and are floats.
. B7 K0 A) o% L+ A* l0 C* S) O ?+ s - >>> math.modf(math.pi)
) B9 l( T5 ~0 L) J - (0.14159265358979312, 3.0)
' k6 @+ K) d6 o* I - >>> math.modf(12.34)
3 L3 v! O2 E/ S' O8 C5 B - (0.33999999999999986, 12.0)
复制代码 % {" F! o) s* g3 L; x7 O5 p
math.sqrt(x) 求x的平方根+ A! z& \: N9 N. ~1 g
- #求x的平方根$ C% h( ]! Z t1 O
- sqrt(x)4 `' [& ^* h- L- c! g/ w
- Return the square root of x.4 L z6 O' u! @# p) Q7 U& c
- >>> math.sqrt(100)
0 e. Y! L* u8 T& d m C - 10.0' D. S8 F2 l# P- V+ U+ [
- >>> math.sqrt(16)" {, `$ ^# F% m/ g/ d8 u2 z
- 4.0
: p/ A* C3 F; R/ L& d# R- g! b) @ - >>> math.sqrt(20)
7 [: V; @3 w3 H" | - 4.47213595499958
复制代码
/ |* {( v6 t% x' [, z$ h, K, n- U! Bmath.trunc(x) 返回x的整数部分- #返回x的整数部分: r3 y0 A! x, K/ p: C }( J$ t% i
- trunc(x:Real) -> Integral, W* {3 {1 X5 v% r$ ~' Z3 \- d% q
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method., K4 C. v0 Z# J6 W5 |
- >>> math.trunc(6.789)
: a* q6 N6 U$ p - 61 ^$ V# y3 M1 l8 O* R
- >>> math.trunc(math.pi)$ A6 K2 c6 w5 p8 ?: t$ ?
- 3, G+ ^. Z6 s6 X1 Y. O! X
- >>> math.trunc(2.567)% j) o* q" J3 }$ Z
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|