马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 u1 z; R4 r2 H0 q5 K/ I【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
# E- R1 m. _& |+ {) B; T4 y
3 N, z0 _0 L$ t3 B6 x方法1:
% P2 G {/ N5 ~/ ^- >>> import math
, j9 f K a2 k8 X& B - >>> math.sqrt(9)# |( _3 n& m& [
- 3.0
复制代码 方法2:
+ X* E+ b: \3 M: B9 a- >>> from math import sqrt7 |! Y" K/ r! @. U+ g: B4 e5 S
- >>> sqrt(9)0 k! Y' {4 L P! Z% `, }4 v, Q; G& a! V
- 3.0
复制代码
: m6 n# Y, x5 y) ~3 p
+ d7 q! `1 d% Z( \) o3 _math.e 表示一个常量 A" ?" D- {8 M0 l. I$ i A8 M
- #表示一个常量
- f7 c% ?. [1 B" N4 i - >>> math.e k2 @8 C2 s1 N
- 2.718281828459045
复制代码 # ^# B; ]6 O- @+ s3 j
math.pi 数字常量,圆周率
* g( i6 b; `% O9 i- #数字常量,圆周率
2 N4 z1 D0 c2 q m. V4 f8 l3 ` m4 O0 j - >>> print(math.pi)
% h( P, g6 `: ]1 U5 K - 3.141592653589793
复制代码 ^% }1 W" m- o* @8 v# ?7 I
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x0 [) C4 j% ~% N6 u" U7 F( B
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
; y- D2 P; ~( ~ O* g1 A - ceil(x)! w ?2 z# a& |' R& R7 b
- Return the ceiling of x as an int.
% _. M" v! J' g7 @9 g& ~ - This is the smallest integral value >= x.0 d* `1 c* B# g
- : w9 k/ q2 s* _9 t5 o9 S$ P
- >>> math.ceil(4.01)
' C- Y2 i" ~, W4 ?, W% a - 5& G" I! O8 O+ R& A9 n
- >>> math.ceil(4.99)! P8 z( y5 W+ s( R5 m
- 5% G* ]0 Z0 C j
- >>> math.ceil(-3.99)& Z3 q; d( x) D8 w- S
- -3
. ^' x# b, j4 b& ~' m - >>> math.ceil(-3.01)
; p/ C. m% f% F$ m5 r - -3
复制代码
# D8 d1 _ X3 u- fmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ N+ r9 X7 S, F2 E
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
- v8 w3 O5 U" S - floor(x)
7 I# T U& g. [5 s7 } - Return the floor of x as an int.
4 B9 U# H! b7 } - This is the largest integral value <= x.* ^6 Y- k O4 U7 V0 o$ `3 n- m0 Y/ y
- >>> math.floor(4.1)
' v6 I. e9 P* x( c2 S8 } - 4
4 O3 P. x' s( ?# q) q5 H - >>> math.floor(4.999) _/ e7 i; d# H X5 T1 j9 O( r6 d
- 4 L: E' d1 J6 t9 F: N
- >>> math.floor(-4.999)
, p. E1 _7 b7 p- s - -54 N0 K# y8 x* ^" Q( I5 f
- >>> math.floor(-4.01)/ z2 m% b3 T# H5 s/ W( Q7 G
- -5
复制代码 2 q9 N7 ]% k0 Z# U* x' k3 v
math.pow(x,y) 返回x的y次方,即x**y
( Z# `/ f. e! H0 N- #返回x的y次方,即x**y, E3 r( r/ c5 e1 y7 U
- pow(x, y)2 N) O1 Z" a$ ]4 U1 B/ s
- Return x**y (x to the power of y).
' m! f4 ?; p7 U7 F8 f - >>> math.pow(3,4)
' ]0 P2 R9 T4 g) @* ` - 81.0
4 S1 P7 t9 M" ~) I8 t8 x - >>> / `. I& @4 k0 _& B& m) o4 M' q
- >>> math.pow(2,7)' j5 Y( s7 r, l3 y, f# _$ }
- 128.0
复制代码 6 m5 D! Q; O3 w3 N
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), y# W+ v% @/ K# s0 R/ z
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 x" X1 ~) C- ^* }; E' q" l) Z
- log(x[, base])
# F# k8 b6 C( S' t5 P9 A& e9 U) Y& H, W2 v - Return the logarithm of x to the given base.& b# S0 w _4 o+ q/ h3 C" b, ?1 g
- If the base not specified, returns the natural logarithm (base e) of x.- Y: d- O: _: |* j. S; l% o
- >>> math.log(10)& L# M- t" g4 f/ _) h) [- o
- 2.302585092994046' R* A+ |) T! {5 I8 i# F
- >>> math.log(11). u; p* @( w: l
- 2.3978952727983707# W) D5 `' `* B+ c& L6 P
- >>> math.log(20), k1 o' g4 r0 e2 R' j
- 2.995732273553991
复制代码
6 I% K/ C0 x+ }( ?, Rmath.sin(x) 求x(x为弧度)的正弦值
9 d& u; j5 F, e( ^1 E2 H6 \# \- #求x(x为弧度)的正弦值; y) ^/ w4 U9 K( U: E6 `7 y
- sin(x)7 L. |, F# u5 ?' v2 J* f$ F
- Return the sine of x (measured in radians).
7 _# D4 V% o" c - >>> math.sin(math.pi/4); A5 j- `3 D4 p/ W
- 0.7071067811865475
/ E7 B K' f% X. R# [ - >>> math.sin(math.pi/2)
+ |3 x4 S; h- I6 Z' G; H - 1.0
* v% F1 U; j, N' V' e - >>> math.sin(math.pi/3)0 ^' a6 ^- w: t8 t. l
- 0.8660254037844386
复制代码
' Y: N: S% W) Y2 j3 i) g- bmath.cos(x) 求x的余弦,x必须是弧度! B7 e, t" W; E l& ^0 S
- #求x的余弦,x必须是弧度
5 S8 k7 K8 R: B- `8 h2 G+ X1 D - cos(x)3 s T* Y2 d8 V4 Y; n
- Return the cosine of x (measured in radians).3 e- r _: u% C
- #math.pi/4表示弧度,转换成角度为45度$ @5 p- }+ a6 E
- >>> math.cos(math.pi/4)# r4 j! _; g( l+ C# H
- 0.7071067811865476' Y" C s/ a8 D' }
- math.pi/3表示弧度,转换成角度为60度( k7 V4 ?- R* a* t
- >>> math.cos(math.pi/3); j" Z4 a! b7 E/ N; u: Z
- 0.5000000000000001
+ d$ p1 Y4 e- Q `" _$ _3 ^% F - math.pi/6表示弧度,转换成角度为30度" n8 B2 U; g* M: G( p( Q/ {
- >>> math.cos(math.pi/6)
0 m' n# G9 r6 C& p1 W - 0.8660254037844387
复制代码 1 ~% _& E5 P8 u! x) x
math.tan(x) 返回x(x为弧度)的正切值
; `0 ?$ Z# b3 l; Q3 F- #返回x(x为弧度)的正切值* m, J* X, Z* p
- tan(x)# Z& `$ ]& P, E0 W2 ^9 C
- Return the tangent of x (measured in radians).
8 J0 `, M0 t3 A" ?: c4 Q - >>> math.tan(math.pi/4)
6 h) M, P8 `2 f! h) J6 a - 0.9999999999999999
3 K- \9 ^! t+ m7 s) L: _) x3 F - >>> math.tan(math.pi/6)
& i" z6 F& w$ s1 n" o/ ^ - 0.5773502691896257 M0 O! E- s7 g, b( K6 X0 k
- >>> math.tan(math.pi/3)
$ {# E9 ]2 S0 z6 ~9 A. C- l - 1.7320508075688767
复制代码
5 x& l t: L5 ?* T1 k% c0 Pmath.degrees(x) 把x从弧度转换成角度, M, G, w" u" k; v4 g9 c: J7 d! r
- #把x从弧度转换成角度
1 \* r0 J- q, B6 E0 |( i - degrees(x)) @- O: K, k, m# W
- Convert angle x from radians to degrees.
9 z8 p; |6 R: M% B) W
9 `! y2 B+ j* ]8 B0 @7 y5 W- >>> math.degrees(math.pi/4)
& L4 E+ }) n! ~3 a$ J - 45.0$ V6 `% s% L$ T f1 v
- >>> math.degrees(math.pi)1 c$ N6 z0 u6 H% R: k/ C+ }; ~
- 180.0# S, W5 b: f/ V n9 d1 b
- >>> math.degrees(math.pi/6)' h: T7 n% y- g0 V- A# P
- 29.999999999999996' S$ B. U& |& R/ |/ X% `5 L
- >>> math.degrees(math.pi/3)1 Y2 b$ W4 F6 v8 u$ w4 q' T
- 59.99999999999999
复制代码 # u4 d5 O9 ^' @( ?2 [, u. m/ n/ K. C
math.radians(x) 把角度x转换成弧度
6 o& W0 D% s0 d A, Q- #把角度x转换成弧度
; C% \& |2 d& p/ r - radians(x)8 y4 [$ h5 S/ Y: H* Q! ~
- Convert angle x from degrees to radians.7 r D3 e2 @7 \* v0 i/ B- R
- >>> math.radians(45)0 g, }5 a2 A- s
- 0.7853981633974483
" O9 c9 \6 r3 B+ p - >>> math.radians(60)
: n2 o' ?, V3 J! H: D2 Q - 1.0471975511965976
复制代码 ) P! R+ F: f6 E/ c. U
math.copysign(x,y) 把y的正负号加到x前面,可以使用07 k( f E- \/ }% }
- #把y的正负号加到x前面,可以使用0
9 T1 [* o' I# W - copysign(x, y)
- r) {+ O. F: g* X5 @! J, h0 z8 [7 M - Return a float with the magnitude (absolute value) of x but the sign 7 m# ^/ j2 i3 m- P6 c' h1 C
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
6 j) Z) N e- A/ I$ S - returns -1.0.. y# m6 U. m5 p9 `# y
8 y# a" w+ B+ v0 s2 R- >>> math.copysign(2,3)+ _/ k7 r2 C+ N, o; K9 F6 f
- 2.0
. b. M) n/ g/ v6 h - >>> math.copysign(2,-3)1 ~: x4 l; H! n4 X/ S( Q- H: ]
- -2.07 ~$ b' X6 U. V% _3 F a
- >>> math.copysign(3,8)/ e- k2 Z* X4 Q- N/ K
- 3.0
, b5 @- ?, Y1 ^ - >>> math.copysign(3,-8)! O, `* A/ e( O7 G& ~- I
- -3.0
复制代码
( f9 I* F% S% j$ F2 B2 ]! wmath.exp(x) 返回math.e,也就是2.71828的x次方1 g, b) b: g% B1 [
- #返回math.e,也就是2.71828的x次方( L+ \7 v' m6 k9 p8 f+ N
- exp(x)
7 l$ C O. S$ e0 F Y- w - Return e raised to the power of x.9 |4 ], ^# `: F% P# d
) E3 G0 _& Z, V- >>> math.exp(1)! a8 H9 p+ t0 I3 X4 B) p
- 2.718281828459045
' A. f, D: ]' N - >>> math.exp(2)3 y$ ?$ r, A" U
- 7.38905609893065
6 A U$ l s8 r+ i5 _ - >>> math.exp(3)
3 D. X+ |, I8 I' G - 20.085536923187668
复制代码
- g% e% A4 W' A1 h# s2 L" cmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1# X: J E. m+ y- ^9 V/ i; t& {( N
- #返回math.e的x(其值为2.71828)次方的值减1* H% E j/ d- g- U6 x' F
- expm1(x)
$ a- _8 Y, P- T' h - Return exp(x)-1.5 v- b6 C! [' H: u$ Q
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.9 e& D0 e( ~" _$ X6 E0 ^/ ^+ Z
+ I% C: x6 w. `: S& l3 o& o1 e- >>> math.expm1(1)
$ d+ t! c7 s1 h% V - 1.7182818284590455 _' r7 i8 r X/ k
- >>> math.expm1(2)7 C# V) q5 M3 Y, D; C
- 6.38905609893065
: w- a! @4 {: Y% } - >>> math.expm1(3)- ^% A- U1 n% ^0 i; H7 U/ [
- 19.085536923187668
复制代码
8 g3 ]- H2 {% o: lmath.fabs(x) 返回x的绝对值! N% h9 m4 L! r% z
- #返回x的绝对值
. r5 A0 y$ h4 z' E - fabs(x)- `3 n+ j% o' W. n
- Return the absolute value of the float x.
+ x; i0 i l* c. v6 |
" b9 ?* h; d0 ~- y4 k' Y8 q- >>> math.fabs(-0.003)
4 H+ a5 y! t- x) X - 0.003$ h4 y8 a1 b$ u+ ?* V$ U T; E" A
- >>> math.fabs(-110)7 u9 C1 `& @7 J. N6 h
- 110.02 t3 [8 x& O. j" \5 `$ x. A# T% b
- >>> math.fabs(100)! @" U) B( J! @: X& E- M) C1 c
- 100.0
复制代码
+ f2 B7 M% D: {) imath.factorial(x) 取x的阶乘的值
$ j8 K7 ?, m# p! z+ B3 P$ p, Z9 `- #取x的阶乘的值
( m2 D: y+ W( k" ] - factorial(x) -> Integral8 v e7 M6 k! M: C f! K0 U
- Find x!. Raise a ValueError if x is negative or non-integral.
; v' o7 [: z) y9 _" f - >>> math.factorial(1)3 l6 W; V- u% \0 ?9 G
- 1% B+ U+ d- j1 w: }# i7 Q! ^
- >>> math.factorial(2)+ X) Y$ W- q9 U
- 2, l: E- ^8 R: L
- >>> math.factorial(3)
/ h, v) t) R( R* J% ~4 h5 L! } - 6& z% f# v) ]9 W( D) O- {8 c3 a$ F
- >>> math.factorial(5)4 K4 t+ t1 U4 \2 A
- 1206 i) O, d1 n2 l; G
- >>> math.factorial(10); X& E8 L" b5 u# U3 z/ j& U1 Q
- 3628800
复制代码 * f4 _3 c2 d! u: s$ b
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数1 e: v- T+ i; V) k9 m) J
- #得到x/y的余数,其值是一个浮点数) r" z/ O' I0 h) w$ e% Z1 z
- fmod(x, y)
7 ~9 g0 P* t# v0 H+ a - Return fmod(x, y), according to platform C. x % y may differ.
% c) v% p/ z- w- u; h3 \ - >>> math.fmod(20,3)
) B) j4 R; w1 }1 Y - 2.0
9 w/ k, v9 \! h9 \0 f1 o. O7 G - >>> math.fmod(20,7)' ]: k+ L) g3 E6 q. E
- 6.0
复制代码
' L1 P( Y2 d/ r! }. fmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 c2 V$ O% w6 C U0 ^6 _1 H- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
" `5 w8 d2 a& a3 N% {- `3 @6 { - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
+ i, J0 ^& i+ O! w3 r# z - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
, G" V) m( P S* y - frexp(x): C: L9 R# E2 f a+ J: v$ b1 _- D
- Return the mantissa and exponent of x, as pair (m, e).) M; I9 ~/ F' a- L
- m is a float and e is an int, such that x = m * 2.**e.$ t$ X$ c2 J _8 H" F
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
. ^ M( w- E; U0 Q" X S+ p - >>> math.frexp(10)
0 B# a J( v+ ^1 Y% K$ @( H- a7 | - (0.625, 4)" a, d6 _- e9 y% y2 H
- >>> math.frexp(75)6 A( e. h$ f: a# \0 n
- (0.5859375, 7)8 I8 s7 c) V4 \+ K( g
- >>> math.frexp(-40)
1 w; ], V' ]8 Y% Z+ {% x - (-0.625, 6)
# p' U% R6 W" q; f. y - >>> math.frexp(-100); ?; k" K+ t3 F1 V9 s4 I
- (-0.78125, 7)
Q4 S* T" Z9 q( k' @, d# c - >>> math.frexp(100)
c$ ]/ Y6 ?5 a1 Z! R; ] - (0.78125, 7)
复制代码 9 b- r) Y: D/ A1 ~5 [) k8 ]/ ~ X
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)1 d- D$ Q' [% `8 B: P d/ C
- #对迭代器里的每个元素进行求和操作% C, p" X0 X6 M+ ^8 P
- fsum(iterable)
1 _+ C; }9 _) B4 k5 A - Return an accurate floating point sum of values in the iterable.
7 X4 K N) z' x - Assumes IEEE-754 floating point arithmetic.
8 J7 X3 t! I* j. Y9 j+ \ - >>> math.fsum([1,2,3,4])
3 o2 f" p! ^7 h - 10.0
# a4 P! \. C" H/ P! y. w. A- i - >>> math.fsum((1,2,3,4))+ B K3 @* x5 ^
- 10.0
/ B' {* i3 X# @4 F' |" C) A. H - >>> math.fsum((-1,-2,-3,-4))
. C5 ?2 a( G8 n9 Q - -10.0
# E N$ U6 f0 X5 G - >>> math.fsum([-1,-2,-3,-4])+ ^% \+ X* Q; y9 `$ i$ i$ c" \4 r
- -10.0
复制代码
2 t: C% f5 K8 P, n' x, s, Zmath.gcd(x,y) 返回x和y的最大公约数
% r/ b8 y& f& q8 a2 r" b* M" e5 i- #返回x和y的最大公约数/ A z: U% s$ }8 x& d" h% Y
- gcd(x, y) -> int
1 s2 |7 g" r1 p8 O3 j - greatest common divisor of x and y
, a/ P+ H5 ?, Z. \/ W - >>> math.gcd(8,6)
4 r$ S: W8 u) p1 y- Z) N! W( @3 h - 2
|! N0 ~ h* B' k/ g - >>> math.gcd(40,20)
4 V8 P r0 W' z6 A8 J* n - 203 C8 E/ k% m: g* k
- >>> math.gcd(8,12)
4 ?0 w+ n2 `9 d6 D - 4
复制代码 * [2 N4 r( ]- S! U+ e/ \4 x
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
0 F- k. l8 |+ ], g# U7 W- #得到(x**2+y**2),平方的值6 q, z" X. }: W2 d/ H/ ?& s2 t
- hypot(x, y)( E, w: P1 F% M: }! ^0 P/ P% j, s6 o
- Return the Euclidean distance, sqrt(x*x + y*y).
, R1 d" Z! o4 b- G* u M - >>> math.hypot(3,4)
, U2 d" @) P9 d- o6 \9 ^- S - 5.00 n/ H! F7 S: f- V) b! b
- >>> math.hypot(6,8)# N% y) m/ d( a4 d. y
- 10.0
复制代码
! h7 G4 u/ W2 D% imath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False, g9 k a+ j( n6 J
- #如果x是不是无穷大的数字,则返回True,否则返回False: E8 F+ L$ R. x: K( ^, `
- isfinite(x) -> bool* e9 ]$ I# {5 f4 D- a2 M! i
- Return True if x is neither an infinity nor a NaN, and False otherwise.
* P8 x% Z. T/ Y5 {; S - >>> math.isfinite(100)
* q2 h6 W: E% D- F6 { - True6 } |7 J8 |. U
- >>> math.isfinite(0)
1 w# C3 I/ |2 K( ] - True) g, `: Y$ }3 \( P' m% h1 @4 A
- >>> math.isfinite(0.1)9 @7 b N u6 c$ j" w; l% Z
- True& d8 u+ L0 R6 ]" n8 S, s8 ~ j
- >>> math.isfinite("a")
) }9 {; A3 V s* s: W# Q0 H - >>> math.isfinite(0.0001)
, x6 G0 v% |* |8 I L6 |2 D - True
复制代码
2 N- ]" }$ \' Qmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
, x! p" D' }- m8 I: C" {7 }9 K- #如果x是正无穷大或负无穷大,则返回True,否则返回False
. E) V1 O* S, P# r# v - isinf(x) -> bool9 h- `. s# d: u
- Return True if x is a positive or negative infinity, and False otherwise.
8 F7 b! ?- g: g4 ` - >>> math.isinf(234)7 R5 D5 n: G: t# D1 R6 H2 T! W
- False
" V( K# Y+ W# \% Q - >>> math.isinf(0.1)
$ l0 Y3 c* i- l+ e4 F) k - False
复制代码
% y$ I5 d6 e/ q3 omath.isnan(x) 如果x不是数字True,否则返回False1 l3 X8 M$ N, X( S
- #如果x不是数字True,否则返回False
6 ]) l4 ^. s6 w- R/ o1 n( ~ - isnan(x) -> bool$ m0 D3 ?3 Q/ ^+ [6 p; g% o
- Return True if x is a NaN (not a number), and False otherwise.$ V/ `6 ?0 Q7 `% ~. B; D
- >>> math.isnan(23)
9 R' t2 C: V0 s) ]# {) L - False
, `* _% Y! k- _1 U* `+ E: ^( K1 ` - >>> math.isnan(0.01)
, h# E4 z a q w8 f$ N, W; c8 H - False
复制代码
* p S7 O1 ^+ o6 p2 `math.ldexp(x,i) 返回x*(2**i)的值
! x- m( h3 p% t+ }. U J- #返回x*(2**i)的值/ x" N" [5 \1 S- S4 m- ?( o
- ldexp(x, i)
- ~% P# X/ B5 C1 l. A, ] - Return x * (2**i).0 x' q J4 f! c5 E |& e( [
- >>> math.ldexp(5,5)1 b9 L* d4 ~4 g! e
- 160.00 a5 {" p: W5 B1 \0 Q q/ _# K
- >>> math.ldexp(3,5): y$ a; {/ \! ?
- 96.0
复制代码 7 L0 Y$ t( O4 O4 N7 i5 ]6 a+ Y4 _
math.log10(x) 返回x的以10为底的对数+ p, _1 ~# G8 Y9 v- u q$ w3 | F
- #返回x的以10为底的对数5 b2 _& M; _( V* x6 U
- log10(x)
% @% c. f, e$ L - Return the base 10 logarithm of x.9 J/ h1 C5 }5 b9 a& W- q8 ^
- >>> math.log10(10)" D5 V. L- g' l
- 1.0: U9 U6 L6 i. U; a4 P( J
- >>> math.log10(100)) g. L3 u! g6 _4 l- o0 n
- 2.0
' T* I9 N& g. w - #即10的1.3次方的结果为20
2 p3 q% c0 h& K) } u1 l% c( d# T - >>> math.log10(20)
1 Q/ i& w7 a1 L# A7 M2 {. [ - 1.3010299956639813
复制代码
$ m3 v k" L# {# b& emath.log1p(x) 返回x+1的自然对数(基数为e)的值% B9 c8 n, b7 ?& E0 Q3 ]( O. \
- #返回x+1的自然对数(基数为e)的值0 ]/ Q2 e9 Z0 _2 R7 D, l, r0 {
- log1p(x)
" o8 {- ?) o7 a A; S2 {, R4 m5 K - Return the natural logarithm of 1+x (base e).5 p( D# F# z9 t& D) n% a
- The result is computed in a way which is accurate for x near zero.- T) U; m' ?/ F' T: m2 N
- >>> math.log(10)9 L% A5 n/ ]+ C: M( |% C$ _
- 2.3025850929940469 y i* v6 l- Q
- >>> math.log1p(10)
& g2 S4 @" y, ^' q1 Q- Y' P - 2.39789527279837075 g5 g) }" t& K1 G5 e
- >>> math.log(11)9 H" f7 `- ^; Y- _$ C# a
- 2.3978952727983707
复制代码 " `8 D/ s- a" H4 g4 ?$ X
math.log2(x) 返回x的基2对数
+ \* k: ~2 p' U- Z/ g( ?# G- #返回x的基2对数
* [( q. _4 d, |* X: X - log2(x)7 G+ `; |0 t7 f m1 C# Q- n" d! p0 R
- Return the base 2 logarithm of x.& u" E9 v! O# I6 U- A2 b/ J
- >>> math.log2(32)
2 J# ]$ v: ~# `, g - 5.0, B& W3 c" p$ }% B( b$ \
- >>> math.log2(20)6 u. i8 j1 J. f4 ]4 S# @7 C& O
- 4.321928094887363
6 S: _9 u# u" g$ @ - >>> math.log2(16)
" O( O+ G( Q$ F8 y8 B! a: Y8 C; `5 [ - 4.0
复制代码 + R% H7 [7 L- K0 V0 [2 W
math.modf(x) 返回由x的小数部分和整数部分组成的元组
X; ^4 Y$ I* N7 X+ o/ E- #返回由x的小数部分和整数部分组成的元组
- s4 [9 }4 \1 L" I1 s) W7 l# l - modf(x)" _/ c( V5 z! W! r/ ]( O& s
- Return the fractional and integer parts of x. Both results carry the sign0 f! u) t+ f" q/ z6 u
- of x and are floats.
. @/ y6 n" a& U+ A q% X$ Y - >>> math.modf(math.pi)
0 \/ M/ C; x& A% }4 F - (0.14159265358979312, 3.0)' \! U' r7 _5 j& J) e
- >>> math.modf(12.34). o2 Q7 ?( O; ~8 \! y6 H5 O
- (0.33999999999999986, 12.0)
复制代码
6 v' Y( x8 j" v& s$ j2 Zmath.sqrt(x) 求x的平方根
) ^1 M& c) f f- #求x的平方根
5 q4 R: I/ j7 e' ]3 O - sqrt(x)5 e. L4 R e4 d
- Return the square root of x. V( @& e- k. _4 b V: h3 g% l; w+ M
- >>> math.sqrt(100)" z" B C2 K/ N
- 10.0 C/ y; g5 p/ p: W$ Y: y3 }
- >>> math.sqrt(16)( e2 M) z" m" [& v5 k% G: w3 }
- 4.0, y4 z: E1 G8 c0 d" P5 |
- >>> math.sqrt(20)
* I G2 ~' {8 Z; }8 u6 O7 b - 4.47213595499958
复制代码
% L' T7 V6 g7 W6 }/ dmath.trunc(x) 返回x的整数部分- #返回x的整数部分5 W4 X; l/ t2 q4 i6 K; T" N, g
- trunc(x:Real) -> Integral
! b4 j. C; X$ z - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
/ Q9 w' O% N' m1 t# b+ Y5 Y - >>> math.trunc(6.789)
0 C S* _7 g G0 } - 6
6 | K' \3 i9 } - >>> math.trunc(math.pi)
3 w7 K, F4 x1 U8 g - 35 X3 I6 b2 P7 @/ L" T8 f4 h- z
- >>> math.trunc(2.567)
- d4 R% E1 u- `! V- p- N2 s - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|