马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ T* W2 @$ _: a {* s" I' |【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ @8 }) d W. q$ n" i
O" I6 P6 T* Q8 n+ G, z8 \& F f方法1:
' E1 f6 G) S) D8 f3 p- >>> import math5 l9 ^6 w4 d' u( q/ Y
- >>> math.sqrt(9)4 D) b& L) W/ r) @5 e) k
- 3.0
复制代码 方法2:
' v8 _; o [& U' ?* ~1 u5 ^- >>> from math import sqrt9 _+ {+ Z7 W# O1 I4 x6 u
- >>> sqrt(9)
" T0 w0 W, X2 u% G' n' G - 3.0
复制代码
% |- D& \: U! P' R" d. P 6 W; _" F9 |3 y9 W
math.e 表示一个常量! e3 A3 L7 W' l: g1 \9 s
- #表示一个常量+ p s/ w. q/ F3 d( w4 G/ D! w
- >>> math.e+ ?0 T* _* g% U( \2 K
- 2.718281828459045
复制代码
& a/ q, y' j, `. jmath.pi 数字常量,圆周率/ I! {; a0 M& c
- #数字常量,圆周率; [4 g- ^' N+ L1 Q0 T1 j
- >>> print(math.pi)2 f; \8 g3 I; f4 o1 s9 Y
- 3.141592653589793
复制代码
' T9 y8 T/ |( vmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
, c& p2 Q' s* j% f2 g7 D- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
& u8 O4 {6 [+ n - ceil(x)0 {, _: f( T4 s) R* [: i" |
- Return the ceiling of x as an int.
4 \$ P# _+ i, j7 F" z" \1 M; q- K - This is the smallest integral value >= x./ X% n5 o- D2 B. `6 S0 o' L
- 7 \" s) @% x3 q9 C
- >>> math.ceil(4.01)
& M0 p$ J' H1 D" O - 52 \+ p+ S4 d) S1 S% \2 R( B1 W
- >>> math.ceil(4.99)
! H: L( I: i2 ?, s l/ Q - 55 u$ u5 C: X( {6 d t1 m
- >>> math.ceil(-3.99)0 c: s2 ]$ G" C- w" a6 u- |( O3 B
- -3
2 `* U+ M8 M, w5 H1 d - >>> math.ceil(-3.01)
) h) d9 I+ E8 L' g: Y - -3
复制代码
3 I$ w$ `6 L0 z2 m- k1 hmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& F% N$ m$ }2 @6 r- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身; c6 N! N0 A# l/ I! V4 E
- floor(x)/ ~6 J; V% E h1 y! Z2 c
- Return the floor of x as an int.( v' o/ G; w- j* B
- This is the largest integral value <= x.
7 z0 X# l9 p- `; {8 R' a- [' S5 Z4 Q - >>> math.floor(4.1)
p2 c9 ^+ U# e. l+ J t - 4
# W+ L* |( o: C/ v0 s5 m( _ - >>> math.floor(4.999)
4 ?) s* R. r$ g2 A# m. R& X5 U - 4
7 ]# m b, Q8 F% ^ - >>> math.floor(-4.999)
6 e1 x N( Q; p, T' v# j - -57 D8 ~/ k5 h2 U! w
- >>> math.floor(-4.01): @. { B, i- ^9 g
- -5
复制代码 9 l8 c- G- ?5 {+ }
math.pow(x,y) 返回x的y次方,即x**y) x- t4 z# Y! F: n$ T. }
- #返回x的y次方,即x**y
! E- f& E ^- h% r4 s; u1 Q - pow(x, y)
c0 j! i" X) u - Return x**y (x to the power of y).0 V' |7 u3 S4 O. @2 L# B* r' f5 S3 w
- >>> math.pow(3,4)
) W7 E: v9 X5 q4 G; ^$ h) P8 P - 81.0% Z- j! E! ~" w/ X! i
- >>>
, M/ u$ D' ]' e- }! C4 a$ L7 \4 I - >>> math.pow(2,7)% t( s0 J* U* v+ h+ I. S _5 _: `
- 128.0
复制代码 # ~! E S$ z7 ?+ q0 i6 c
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 y+ ^( w* L/ v/ K
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 h( X6 B: _- R0 q% p - log(x[, base])
5 d# E5 e: D K2 K3 X+ ~: g6 R - Return the logarithm of x to the given base.
" S% ]2 i; h" L; X9 V% d) @ - If the base not specified, returns the natural logarithm (base e) of x.* ~% c; x7 M- {! w! F# u! g6 i
- >>> math.log(10)/ ^5 a+ s6 b+ ?1 _8 k# }
- 2.3025850929940464 \$ a. _) J# U4 ?3 h
- >>> math.log(11)
" c5 K0 }8 |& K% L - 2.3978952727983707
1 s Q" c6 l i - >>> math.log(20)# w3 {* }9 T* ]& N( f6 m3 D4 w3 _2 V0 l% e
- 2.995732273553991
复制代码 # d; P2 P, w4 p
math.sin(x) 求x(x为弧度)的正弦值+ V9 m4 Q3 _; N: Y5 q1 A3 @
- #求x(x为弧度)的正弦值
: w9 E' p8 h& r2 r) p6 C - sin(x)
) T+ u& c @) c! Y$ D; ^ - Return the sine of x (measured in radians).
]( t! z, u) v7 n - >>> math.sin(math.pi/4)
d3 \8 q0 }) e( H - 0.7071067811865475
, s2 B7 w2 L: Q Z - >>> math.sin(math.pi/2)/ _1 w0 T \" o9 Z
- 1.0- U' Y9 @6 e3 N6 H+ t; `" G
- >>> math.sin(math.pi/3)
Z3 ~# P, e+ ~ - 0.8660254037844386
复制代码
2 C4 v L. _; G2 C! Dmath.cos(x) 求x的余弦,x必须是弧度
5 Z( }' H, v1 Q/ C) _' C9 B. C, l6 e- #求x的余弦,x必须是弧度+ w1 Y X/ x" h" d/ O; `% p' u( S
- cos(x)
: V" ?* C w* [0 f1 g2 w2 C - Return the cosine of x (measured in radians).
2 Q1 P* l X R1 w( _3 O - #math.pi/4表示弧度,转换成角度为45度4 C- m9 h0 f1 Z! X
- >>> math.cos(math.pi/4)* o' Z* t1 \1 Q% ]: o q
- 0.70710678118654768 b. {- U: ~+ m( O5 ^8 m
- math.pi/3表示弧度,转换成角度为60度
" {7 ~% _: Y2 `+ w# ^, n, a! s; S7 h1 L, E - >>> math.cos(math.pi/3)
6 d. q6 q' X i$ w; Q - 0.50000000000000011 B5 h: @7 ~9 _
- math.pi/6表示弧度,转换成角度为30度
) e* N2 f- I5 ? - >>> math.cos(math.pi/6)
) m n2 v2 C2 M" ]3 i - 0.8660254037844387
复制代码 ! B M4 g; Y7 A3 L
math.tan(x) 返回x(x为弧度)的正切值% c4 W9 ~8 L5 d4 ^ v- ^" C
- #返回x(x为弧度)的正切值" D- _7 R& u2 t. a
- tan(x), _( }: B) N- o2 _, I& Z/ Y
- Return the tangent of x (measured in radians).
9 n# Q8 D2 X: ]+ `. m - >>> math.tan(math.pi/4)+ A4 R$ `7 e e/ R
- 0.99999999999999996 d8 d" h- `, Q% n" v+ y
- >>> math.tan(math.pi/6)/ B! f7 p9 I7 h. O6 l% F8 M
- 0.5773502691896257
1 z Q' l) O' P; c* f" Y - >>> math.tan(math.pi/3)
, T5 W! N6 y, a% E g$ }2 X+ S - 1.7320508075688767
复制代码 ' G7 B6 G& R. Y* H1 L* I
math.degrees(x) 把x从弧度转换成角度
8 u7 z: f7 h% P7 v! E7 Y- G- #把x从弧度转换成角度7 L; v2 A' D) {1 {. K
- degrees(x)( g7 U3 T; J2 ?: _. a
- Convert angle x from radians to degrees.# g$ {) K+ v; k9 ~
Y% c2 w M8 d% m- U- >>> math.degrees(math.pi/4) B1 \5 `1 }3 ^% k9 q* E7 o
- 45.0
- j: l7 D4 {% X; ` - >>> math.degrees(math.pi)8 @2 f% q. @6 m
- 180.0
2 d% N* ]2 f. d6 } - >>> math.degrees(math.pi/6)+ D% A% R6 l' L5 ]) k. p3 t. ~% k
- 29.9999999999999969 S0 ~" e2 ]8 Q' T' H$ S9 K8 H8 O$ w3 Z
- >>> math.degrees(math.pi/3)8 ~4 L8 c0 T3 b, H y3 b% R0 ^+ o* m& l
- 59.99999999999999
复制代码 - X* y* o# T* y6 U$ `4 E
math.radians(x) 把角度x转换成弧度
4 Z `) J. p% f* k9 I- #把角度x转换成弧度
7 \$ F% `+ S7 H+ c3 }6 ~+ \" _ - radians(x)
3 R6 J3 }3 J. D( J: |4 o7 m0 C7 S* A - Convert angle x from degrees to radians.
0 Y( U1 K( Y8 n' N5 U - >>> math.radians(45)! g: q: C1 D/ O* o) n8 V8 e
- 0.7853981633974483
( Z }/ ]' a3 Y. ^' Q0 ] - >>> math.radians(60)
% y4 F4 J& J( k- ]9 ? - 1.0471975511965976
复制代码 ) j8 l, `; `1 s
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
+ K% d7 u: A. O% r" E- #把y的正负号加到x前面,可以使用08 d3 g3 _9 p! g8 A# l
- copysign(x, y)) k5 L( A( l) z+ `+ h, K7 }! M+ D
- Return a float with the magnitude (absolute value) of x but the sign
2 w Y g1 S" Z7 R; a, ] - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' ~0 y8 i* n$ g# T
- returns -1.0.
3 K; R- u8 S$ h
6 z4 ~5 Y6 a8 ~3 f$ r3 ^4 p8 ^- >>> math.copysign(2,3)% p1 X1 f8 a# Y+ q1 G
- 2.0
b8 \% L( B( i - >>> math.copysign(2,-3)
! X; W7 L, K8 ^9 A - -2.0
+ Q1 b+ L% q- l3 ^( f - >>> math.copysign(3,8)
: H7 B) g& D& B - 3.0
& {, L2 W: B0 r& `1 L6 [ - >>> math.copysign(3,-8)+ N, _6 x# r. P7 V* i/ b8 Y
- -3.0
复制代码
& Z$ d' q; Q* H0 @4 M; n) {' Qmath.exp(x) 返回math.e,也就是2.71828的x次方' p3 c% ^1 ^7 j& }/ x# k
- #返回math.e,也就是2.71828的x次方
8 S C8 \6 S2 I9 b - exp(x)
5 M! s0 U0 I+ @0 f+ q& h - Return e raised to the power of x.( j8 ^6 c/ r5 Q; q" f3 \. g
* ]0 e3 r r7 X4 s& L- >>> math.exp(1)
/ b w/ K% g6 W j7 U. ]( o - 2.718281828459045
# |2 [! k: s# H; z - >>> math.exp(2)
' X3 o* q6 b+ D - 7.38905609893065
0 f0 g' y( B" I! x0 O - >>> math.exp(3)8 G" `, [; @& V; @
- 20.085536923187668
复制代码
8 @, z' g4 j- ?math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1: o4 ?; ?' P4 ^4 w
- #返回math.e的x(其值为2.71828)次方的值减16 g2 J& T& {1 f- f
- expm1(x)
7 }$ f+ I( r; L1 ]5 _ [* w+ w4 l - Return exp(x)-1.. b; H- A* f8 i1 i
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 n4 l# V9 E8 D
- * Z F; n9 p5 A p1 S% m
- >>> math.expm1(1)
+ q6 ?# u0 t0 i/ c( [ N - 1.718281828459045
4 p7 l/ U7 N: \ - >>> math.expm1(2)7 I$ F' ]+ ^) }1 ^
- 6.38905609893065
" J* W) K: _* Q/ J6 f1 q - >>> math.expm1(3); B, c- H" r) y! c& y0 N6 n, e& N
- 19.085536923187668
复制代码
0 _" @/ _4 ~; S/ Nmath.fabs(x) 返回x的绝对值; q1 K% j; V$ O' A" V
- #返回x的绝对值
, C5 Z- }# O7 s0 J3 f* { - fabs(x)
. ^: s0 ]9 c' I1 j8 D) F - Return the absolute value of the float x.
- i8 ?+ N9 s( y- h& J0 C( p" ? Q
* @0 S- e7 _! A5 K }% o- >>> math.fabs(-0.003)
3 L P* [, X4 t3 H7 P7 g7 f) k$ u - 0.0034 E- p# B7 C! B
- >>> math.fabs(-110)0 `& ]# e; n: \- R& w
- 110.0, ~6 _: f' F% S N; u
- >>> math.fabs(100)
. F' H' f- w4 b! x - 100.0
复制代码
, ]1 Z* g7 Q( O7 V# Mmath.factorial(x) 取x的阶乘的值
+ c' J: j5 S4 ^2 \5 l- #取x的阶乘的值
6 u0 w) _2 _; G: G$ t - factorial(x) -> Integral, a( G: D4 p1 p
- Find x!. Raise a ValueError if x is negative or non-integral.
$ Y+ D2 v- X& E& t' X - >>> math.factorial(1)! v6 L a$ \. U- u1 l! ?& J
- 17 }$ [2 f; U# y" O7 P: ~" B* I
- >>> math.factorial(2)
6 ]2 A z# Y, N+ w - 2
: W9 o0 I" w/ ~. M' m! K - >>> math.factorial(3)% O% }- u4 E! H. i( T3 s
- 6/ W. G& _" x& U4 a, |
- >>> math.factorial(5)
/ _& g; {! v* b9 G5 } - 120( X2 s4 \+ M7 L1 q- h
- >>> math.factorial(10)
0 y9 w* M) s3 R: z6 e - 3628800
复制代码
j* z- }! U7 K" ymath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! S; T( ]/ z4 Q( E& G& o/ I. n- #得到x/y的余数,其值是一个浮点数) B. i; @! g r. J3 o
- fmod(x, y)" Q X8 @$ S2 S$ X' K8 v; N
- Return fmod(x, y), according to platform C. x % y may differ., f) Z6 B+ C# x8 L$ D3 u* h% l3 U
- >>> math.fmod(20,3)# m5 h1 |( w- C' g# I% {
- 2.0. b, V7 m* e4 @! X" S
- >>> math.fmod(20,7)
0 v& L) r* M; z, i ] - 6.0
复制代码 6 a9 O, d* J5 h+ W$ T, `
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
, E+ Z3 U6 u' k. q% Z- g1 i- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
/ @( `4 C! Q% @1 Q - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 v7 @, E1 L2 |9 C+ A: ~( i
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1 ^4 C. v. E, _) y8 P7 d
- frexp(x)
" f2 L. w# O6 ?" E# o* I - Return the mantissa and exponent of x, as pair (m, e).
: l( e* w- i, d! Q% S - m is a float and e is an int, such that x = m * 2.**e., N) C9 \9 t8 `& [1 i7 Q
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.$ t/ x. X9 B, b1 E8 ^9 k: y, G
- >>> math.frexp(10)
& H4 c, j$ E( R0 N: f/ j | - (0.625, 4)* l5 @ V0 E m- j5 w; p
- >>> math.frexp(75)/ `: l- o& S4 S+ q" h+ d) {
- (0.5859375, 7)
# h, Y+ E& c; t, ~" O6 R - >>> math.frexp(-40)
0 P$ }/ [$ e/ Y# K- P$ e - (-0.625, 6)! G$ T; L, l# f8 H$ V Z, a, y ^
- >>> math.frexp(-100)% s& b9 j" U, R2 Q
- (-0.78125, 7)" h/ B$ U! p# l% W9 N( s* `4 v
- >>> math.frexp(100)
$ N1 K5 k$ i8 y# v7 y - (0.78125, 7)
复制代码
1 O( n9 w( A7 V# Emath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)3 A" W2 _, j& Z. [
- #对迭代器里的每个元素进行求和操作
" C V d( d/ q/ ~ - fsum(iterable)
1 M+ n* q$ t* N6 G3 { - Return an accurate floating point sum of values in the iterable.
" b% ~! }& @5 d - Assumes IEEE-754 floating point arithmetic.
0 ]9 E' K; @; z* r- m - >>> math.fsum([1,2,3,4])
0 J, K0 C. ^$ K% _+ M" x2 y, F - 10.0$ y' Q" u7 E0 U2 [- x
- >>> math.fsum((1,2,3,4))
) [: q8 H' x* a8 M. g' s6 D - 10.0
7 h2 S# d8 }' P* w, r/ ^3 J - >>> math.fsum((-1,-2,-3,-4))
3 n& Q9 L* |6 |) ?' ?5 {5 u - -10.0 i7 D( n+ [! g2 {! Y; B
- >>> math.fsum([-1,-2,-3,-4])) L! b% _( F2 [1 ]
- -10.0
复制代码 5 I0 k8 C2 P& `' w- v
math.gcd(x,y) 返回x和y的最大公约数
8 W: g' i- U" }1 [6 g/ `- #返回x和y的最大公约数
0 d# g c8 x' h - gcd(x, y) -> int2 u9 h8 _) C$ d. K0 t; [' |% `+ N/ c
- greatest common divisor of x and y
+ s' ~- ~; ]" H - >>> math.gcd(8,6)) z, I( U' t: w/ \+ P( {
- 2
" S* q4 m% n$ R$ _- `/ M - >>> math.gcd(40,20)
7 ?( W) J1 P9 o2 f& |2 v - 20 r- d# t2 {' w: F+ h0 }4 V) z# j
- >>> math.gcd(8,12)
+ @7 {, j5 v. t; L) A+ C; u/ O7 U - 4
复制代码
1 l1 n3 r+ ], lmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
# J9 l) P6 _) X) G4 ^- Z% F$ @- #得到(x**2+y**2),平方的值! y% j/ U5 A+ }. e* F1 X
- hypot(x, y)
! l, M- S# u3 b - Return the Euclidean distance, sqrt(x*x + y*y).1 w0 Z, {! s. ?1 r* M2 t( v" J3 a
- >>> math.hypot(3,4), z; f( V0 E( i% k0 }; J6 a: d
- 5.0) ], e9 q0 L# r
- >>> math.hypot(6,8)- h' U, Y3 R$ z. y
- 10.0
复制代码
9 J ?- I5 {: C4 a* f) I4 imath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 r/ u" j# B; a# P0 O8 W# V( D& A- #如果x是不是无穷大的数字,则返回True,否则返回False; G5 u/ E. l$ O+ {! Z7 I) w
- isfinite(x) -> bool
[8 e# P" G# X. J1 R0 M7 U - Return True if x is neither an infinity nor a NaN, and False otherwise.! p4 w# ~8 r! B' Y
- >>> math.isfinite(100)" x v% t. J6 g, i' x' ?+ x
- True: t5 m0 y, T8 S0 d/ W/ a
- >>> math.isfinite(0)
8 R+ }' v0 K, A. L - True2 s/ ]6 _7 R' u5 e8 `% b
- >>> math.isfinite(0.1)
$ D8 {& ]# y# y' t: ?. d - True
1 `5 L9 |0 O7 f$ c5 @' z - >>> math.isfinite("a")6 j+ K* ]/ m) n' q, P
- >>> math.isfinite(0.0001)
2 A7 Q& \& d& S# m* P/ N% g - True
复制代码
9 E2 o ?# G% d/ B0 B; B# fmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
z+ j4 e+ {8 c# b: a% i. S- #如果x是正无穷大或负无穷大,则返回True,否则返回False8 _: N6 i: M! f- \* t
- isinf(x) -> bool7 R9 _% h. E' n1 X
- Return True if x is a positive or negative infinity, and False otherwise.
8 V" }! A) l) F1 W: l - >>> math.isinf(234)7 ^& m+ g$ `. o4 U
- False
/ B8 _; D% r7 h2 q - >>> math.isinf(0.1)0 j: I4 _# O% E# v4 j' r) `0 u
- False
复制代码
! c6 F( A4 T' {; l. imath.isnan(x) 如果x不是数字True,否则返回False9 s3 l# B7 Y `
- #如果x不是数字True,否则返回False: y# a$ [6 {9 P& s% T, N
- isnan(x) -> bool9 u0 x& o! z* K6 R' o
- Return True if x is a NaN (not a number), and False otherwise. t2 R) \0 x' t3 K/ `4 ?
- >>> math.isnan(23)2 a1 k2 N/ \) x7 l8 r& ]
- False
% \" z$ R4 ^/ ~( L - >>> math.isnan(0.01)
7 h5 m, ` D( `- q. P h4 B - False
复制代码
! q8 p) |- O! ]6 _math.ldexp(x,i) 返回x*(2**i)的值
6 K8 i- m/ z! f) c2 G- #返回x*(2**i)的值
* v4 d; ^& @* _4 D# X! E5 p - ldexp(x, i); B2 D- E9 S! ?+ Y( K7 o
- Return x * (2**i).
1 U6 F/ |1 f+ ^ - >>> math.ldexp(5,5)5 u% F% H. M: A8 M1 |; g$ K
- 160.0; O, G, B1 }/ P0 h" H8 ?5 \$ B! Q
- >>> math.ldexp(3,5)! e, Y3 N% u+ c4 y: _
- 96.0
复制代码 # _) {% ^5 D9 Q4 Y" c2 Q- f
math.log10(x) 返回x的以10为底的对数5 U# F P2 O4 _1 g. N& s
- #返回x的以10为底的对数7 F+ s+ C/ R+ n, e h0 @/ o8 z+ H0 X
- log10(x)9 H4 j4 X1 `# P P0 s
- Return the base 10 logarithm of x.
% j% v9 Q- w3 O% F- W9 c* V: F" f - >>> math.log10(10)1 M! i6 r2 s; I3 b1 N
- 1.0: O/ {9 h6 i& d1 n S* R- m* \
- >>> math.log10(100)
1 X* I# b$ O- d - 2.01 M" {; [4 S4 \& { b2 Q$ b5 f
- #即10的1.3次方的结果为20
) E* F) T. M ^4 \/ e - >>> math.log10(20)7 N% w! _/ d, ~9 P2 n
- 1.3010299956639813
复制代码 ' G# Q2 I; i. a4 b3 U0 F
math.log1p(x) 返回x+1的自然对数(基数为e)的值
* x4 @) v: z+ c$ J- #返回x+1的自然对数(基数为e)的值& l8 V8 i& K. U: |! @- L' Q( O
- log1p(x): |' b7 g; i$ q) H/ P1 b% J
- Return the natural logarithm of 1+x (base e).8 Y3 N- t# u: ?$ O& k
- The result is computed in a way which is accurate for x near zero.% t0 U& t& O5 \/ j2 k2 W
- >>> math.log(10)
6 \% c5 d* Q' X; h* Y - 2.302585092994046
" ^% u- b4 _% o9 u: @/ @( y8 h; | - >>> math.log1p(10) S5 y% b& {0 ?: y9 p3 I. O# N
- 2.39789527279837079 V! p8 _4 E. G' [7 l2 j
- >>> math.log(11)
& i6 g( d6 V; E% ^ - 2.3978952727983707
复制代码 ; D6 [/ g3 W% I' q+ A2 O1 R" q V/ Z
math.log2(x) 返回x的基2对数5 y S$ L% {% D
- #返回x的基2对数. L$ Y) j8 M: A9 Y8 F) N. g
- log2(x)8 `# z, F: \3 c$ b j2 @$ B* A( g
- Return the base 2 logarithm of x.
9 \& @# K: b) p1 O, [ - >>> math.log2(32)
* X: D8 d1 u5 A% E - 5.07 l3 G; J' ]8 q
- >>> math.log2(20)2 v' @& O5 n A
- 4.321928094887363
* Z, k0 G1 |3 _$ G; _& h' E - >>> math.log2(16)
& l/ W4 C: e# |) u - 4.0
复制代码 1 a; W! O4 E& y/ \0 r0 `
math.modf(x) 返回由x的小数部分和整数部分组成的元组( S, q. y) ?: w
- #返回由x的小数部分和整数部分组成的元组
5 C! A2 C) S( ^: l: ?' q - modf(x)) h1 V/ W( `6 j$ e3 s3 t
- Return the fractional and integer parts of x. Both results carry the sign
* E3 R6 _) s' j" ` - of x and are floats." e- c9 b3 ]( o( r1 g; Q7 c/ y4 W
- >>> math.modf(math.pi)
% a) g8 `$ c# W - (0.14159265358979312, 3.0)
+ e }; c( g) K V% f( ]5 p - >>> math.modf(12.34)' F) G1 h+ u+ K
- (0.33999999999999986, 12.0)
复制代码
6 Y; d; X' _- K9 v2 _! S# wmath.sqrt(x) 求x的平方根" m; Y4 K- ]/ [; C9 U# Q
- #求x的平方根9 D, H8 ] a( ?1 G V/ V8 g
- sqrt(x). ]+ v7 _* m# g4 [- C& ]: @# O, G
- Return the square root of x.0 f8 V e% A% q) [7 J( J5 V; t
- >>> math.sqrt(100)
0 i8 m) |5 P! m" T; O9 c - 10.0
8 r& z; ?, V" B+ u - >>> math.sqrt(16)
4 n$ h0 d2 o9 r! t/ R4 }5 Z" W4 H - 4.0
5 Q' N- a& K7 l - >>> math.sqrt(20)
+ {9 t% c5 l& h& }& R5 ` @ - 4.47213595499958
复制代码 . q2 a- k; }" `; i. Y( e' x: K' I
math.trunc(x) 返回x的整数部分- #返回x的整数部分2 W; _ H; H+ j0 O/ }; D8 ]
- trunc(x:Real) -> Integral3 k( o: R4 [1 ~
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! K: E/ @/ j8 ]/ t1 U! P4 v8 _
- >>> math.trunc(6.789)5 d. |) T4 @8 {
- 6
2 _# h) L( P1 n9 k1 H( a: _ - >>> math.trunc(math.pi)
: B! } ?0 e' S/ Q5 J7 K6 O% C4 | - 37 [7 _% f, @9 j1 S U- a
- >>> math.trunc(2.567)
* V9 s4 m/ o% ?3 _ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|