马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 I! U0 K1 v) b$ w
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。) F1 ` H% s+ g# x- v! k1 J
/ B4 B" V; y; \/ X方法1:
2 ~1 F2 s/ E" b7 a& v# A- >>> import math
0 a" \* V6 s! q/ V# D6 ? - >>> math.sqrt(9). w0 f2 J) O) ~' ~4 ?" O1 N! n
- 3.0
复制代码 方法2:9 m5 l1 h' E$ j
- >>> from math import sqrt
0 W; f% r* U0 M1 r - >>> sqrt(9)' j+ a1 i5 m0 y9 x
- 3.0
复制代码
% [* k( o* @' r' M " ]6 j# o/ D; H+ r
math.e 表示一个常量6 J' i: c. q2 q5 M8 G9 g
- #表示一个常量
2 Q. O+ I% K& r) z s0 z; T; a - >>> math.e
9 N( L9 i5 |. G# n" s8 o - 2.718281828459045
复制代码
* M, I" J: O! L- Bmath.pi 数字常量,圆周率5 i8 g, a+ D5 C! V' U9 {# U
- #数字常量,圆周率# t3 Q* z' r0 A/ A' p* l
- >>> print(math.pi)
( }* N0 N# U- i+ v4 V+ [2 y - 3.141592653589793
复制代码
3 U" J( K) j0 a$ o0 ]; Zmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x& |' T% U3 P5 ?+ g: P' c. o4 a
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x' L3 m3 I K2 A/ N0 Q2 H
- ceil(x); t5 D9 Q& l$ @3 b* ?3 z i
- Return the ceiling of x as an int.
# ]" e" Q: |" k5 B6 Z+ \) w2 f - This is the smallest integral value >= x.9 b+ K* j. I/ W+ h( L
- & R) O/ L' B; e* q
- >>> math.ceil(4.01)
H% U3 T3 G# _* }& e8 f - 5
& c- l, l; r a. T [1 ~3 }6 Q - >>> math.ceil(4.99)
$ j- m( T9 M0 F1 S+ N, i+ f- A - 58 Y' s, n8 E, C
- >>> math.ceil(-3.99)/ R2 L8 @4 \$ G+ i9 F
- -3
; j' d8 o0 y8 o0 M - >>> math.ceil(-3.01)3 o+ U6 j; _8 G$ P0 B
- -3
复制代码
0 F/ E$ ^9 Q8 ~4 j7 mmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! g2 x' @3 `+ A3 Q. Q9 `" C- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" ]- }+ f: Q0 O2 |1 q8 S - floor(x)
$ d9 w8 z% d6 C( a - Return the floor of x as an int.
8 Y/ m" v1 U) b% \, M - This is the largest integral value <= x.
5 v& j* P- @% Q - >>> math.floor(4.1)5 ] E M( o7 @
- 4
9 g' ]" ]. H& s3 }/ k! T" z1 c - >>> math.floor(4.999)3 b5 y9 M! {. q* ^
- 4/ B1 x' I3 @ o" x1 X o( x
- >>> math.floor(-4.999)
4 T( B4 R3 k( d* G- K( I& z& O - -5
4 q0 ], \$ ?$ g; H! I - >>> math.floor(-4.01)
1 ^8 t/ W: O0 v7 G0 S/ c. n2 u0 E - -5
复制代码 8 P8 Q" X3 o& i/ b/ A! H
math.pow(x,y) 返回x的y次方,即x**y
. }/ x5 v" n5 h- #返回x的y次方,即x**y8 B9 P$ o( y! h3 N7 q
- pow(x, y)
9 r5 U k3 B$ h) {0 z) N - Return x**y (x to the power of y).
" p- A) W- y. _ - >>> math.pow(3,4)
& [$ s ~2 P. A9 V9 J5 N- N. ~ E. s - 81.0* O- r9 O* i g, w& R* h
- >>> A7 b1 @1 a" @7 e
- >>> math.pow(2,7)
5 b! G, w' {# a, }" A* G3 X - 128.0
复制代码 # ?. R$ g' v9 X; Z6 t9 R
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 K) K/ F, o2 H2 ^: R! I
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, a' r9 \2 v/ W6 y( g& k* d# G/ w - log(x[, base])
0 Q: [1 ?' y0 D" L - Return the logarithm of x to the given base.
$ t# \3 [: b; P8 | - If the base not specified, returns the natural logarithm (base e) of x.
- z" @& s* e6 k - >>> math.log(10)
* Q" o. f) S4 D5 ~8 Z - 2.302585092994046! z) V& \9 Y" K7 _! J5 G. [! p
- >>> math.log(11)
1 d1 L8 W( i1 O2 H9 k5 r0 D - 2.3978952727983707
0 @$ N: I8 m- y: a. B+ I - >>> math.log(20)
' J& p P) I/ V& U0 \ - 2.995732273553991
复制代码 . [" j0 U& M3 K
math.sin(x) 求x(x为弧度)的正弦值2 N/ D" T& h" X4 [4 g. {& c
- #求x(x为弧度)的正弦值4 q6 y3 b! [0 c
- sin(x)2 }% V7 c6 z" u" Z. @8 E
- Return the sine of x (measured in radians).
) V: I8 j4 |8 y4 y, u* i - >>> math.sin(math.pi/4)
/ E$ J- \/ o: k2 }# H$ ]0 V - 0.70710678118654759 @+ \ _6 R9 t8 o2 T `4 R S5 B
- >>> math.sin(math.pi/2)/ z- ?1 e/ @, m8 |8 \
- 1.0- K, {4 f" y% _
- >>> math.sin(math.pi/3)
6 `; h- o& o% E - 0.8660254037844386
复制代码
+ Q* e0 |2 S" N( Lmath.cos(x) 求x的余弦,x必须是弧度
& c2 R. R! P: s, b7 Y) ]9 d* r- #求x的余弦,x必须是弧度; J" ~$ c% K$ E
- cos(x)
' W# X4 P$ a9 E( P/ j+ u - Return the cosine of x (measured in radians)." I" b3 p4 B) N4 P8 C
- #math.pi/4表示弧度,转换成角度为45度0 ?% X- S' D+ q: M" M# ~# h
- >>> math.cos(math.pi/4)
: x O5 {; Y' ~* ]) Y - 0.7071067811865476
0 M3 B; n9 `$ E - math.pi/3表示弧度,转换成角度为60度" n2 R5 G6 }1 r2 g/ e2 z) r
- >>> math.cos(math.pi/3). A$ O& i, L& n
- 0.5000000000000001
- v# o; D: |+ R1 i - math.pi/6表示弧度,转换成角度为30度$ q# N2 _9 L5 b+ O- m
- >>> math.cos(math.pi/6)$ L+ |* Q3 k( h# T7 u; _) n) {
- 0.8660254037844387
复制代码
. t% H% L# g) u7 x( v5 g' lmath.tan(x) 返回x(x为弧度)的正切值
5 t3 G; d) Q. n* \7 d- #返回x(x为弧度)的正切值) w" }- u' @/ g
- tan(x)4 c! q: p# a0 s+ }. ` x
- Return the tangent of x (measured in radians).
# {+ K, h# t3 ?" ?! d& K; C$ _: |; e - >>> math.tan(math.pi/4)) i8 k* ~3 \* r) I, f$ V
- 0.9999999999999999
5 O: |6 E" C' ]% f: f - >>> math.tan(math.pi/6)* X* i7 O1 U" ?' J
- 0.5773502691896257
! M0 F- @/ p6 k$ H0 q' G - >>> math.tan(math.pi/3)
4 s8 e7 q3 J2 a( _, I: m9 @# m( U - 1.7320508075688767
复制代码 3 N! z& G; e3 r
math.degrees(x) 把x从弧度转换成角度
! w- A3 B+ t& ^- #把x从弧度转换成角度
% X" ]4 p) S6 J, M$ e5 v, N" m8 \ - degrees(x)
3 [9 e; j; z3 B# [8 O% C - Convert angle x from radians to degrees.1 S, i, p4 j" [' N" ?" e( z
- ! ^1 P H" o1 g
- >>> math.degrees(math.pi/4)% S2 G! M. ~, D* ?: T) _6 k5 Q
- 45.0
- @* I S3 v8 a, g* E; r- @/ ] - >>> math.degrees(math.pi)) \1 T! R4 R2 y& s& ]/ F# ~( _4 `
- 180.0( I) Z; W: ^, @# a- f3 G6 {8 _! H
- >>> math.degrees(math.pi/6)% j2 @. p3 i& [' t1 t" d9 g
- 29.999999999999996
- M1 {9 a: [' Q' l, K0 Z4 E - >>> math.degrees(math.pi/3). O5 y; S5 b9 q9 S/ r! `9 b' X
- 59.99999999999999
复制代码
2 R' n* [1 f: w1 p, e) cmath.radians(x) 把角度x转换成弧度* C( H$ j) m4 i8 K/ {. o
- #把角度x转换成弧度
+ U" Z5 o* h. J# f - radians(x)
2 i$ L3 @% k; z$ } - Convert angle x from degrees to radians.; Y' ^( [" U( @. z3 j7 o
- >>> math.radians(45)0 I; @3 h2 l" V/ o; q
- 0.7853981633974483
' W3 x( G" l/ R! k v$ V - >>> math.radians(60)
" |. Q# n- D8 x( d2 j; ?8 n" K5 f' |- F# H - 1.0471975511965976
复制代码 . a% f/ p, h( s, }4 L
math.copysign(x,y) 把y的正负号加到x前面,可以使用0 i! }3 u( ?3 G7 t
- #把y的正负号加到x前面,可以使用0. Z! ]2 G" K R0 m" o+ K% L
- copysign(x, y)
3 P4 j( a" C' J - Return a float with the magnitude (absolute value) of x but the sign + P+ ^ M; l5 ~7 h9 j) K
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 D; s0 b# k% g5 q7 y
- returns -1.0.+ S9 s( }" ?. Q/ |
- / V+ s& |3 q; t
- >>> math.copysign(2,3)5 L! p3 l# _9 _
- 2.0# P3 N# Z* j5 ]+ B
- >>> math.copysign(2,-3)
^2 p0 ^9 \- h9 c9 B3 Y1 _ - -2.04 U, C! K7 p/ u
- >>> math.copysign(3,8)
& B( _2 _/ g& N# |0 m - 3.0. z: Q6 o2 |' h1 i
- >>> math.copysign(3,-8)
; V Y+ c( I; ~ H/ p* z E3 J - -3.0
复制代码
6 o2 b p! u! @' ~1 k1 ?/ I$ M$ Hmath.exp(x) 返回math.e,也就是2.71828的x次方
6 j4 r" P; d8 n. x5 \0 r- #返回math.e,也就是2.71828的x次方; |) ]0 K' r* @4 }: y H) F
- exp(x)
/ c+ |, C3 a4 x8 T* n; \ - Return e raised to the power of x.# g3 V. e+ ]; [0 X( K7 K; W
- 1 @: c9 n- V" r3 h( N
- >>> math.exp(1)
# N. r! N: o. z$ Y9 G - 2.718281828459045
* ]. }6 Y5 _4 S& {7 ?/ e: _- M$ Z - >>> math.exp(2)5 |" A% O% {' u' s! x0 w
- 7.389056098930659 c4 G$ }3 y% j9 W4 g0 ~, K/ X
- >>> math.exp(3)
' K; `' c: Q2 R8 v - 20.085536923187668
复制代码 # H( L$ S" ]4 J. e2 u6 O+ Q" f
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
) p! `: s2 N7 }4 X) r6 {- #返回math.e的x(其值为2.71828)次方的值减1
7 t9 @5 G+ ^2 J0 P, P* H - expm1(x)
' \, D$ |* A: ?0 J" }: W9 y - Return exp(x)-1.8 H" D0 h. i& t D6 @
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
6 y- }8 o8 B( i( g9 u8 \3 Y8 q - 9 j2 a+ X- E4 B, B: C
- >>> math.expm1(1)7 x6 L) F1 `) O* W
- 1.718281828459045
5 K: u6 j" G/ a, C - >>> math.expm1(2); P4 b; l3 G& i# {: V
- 6.38905609893065
' R, l' l w: ?) M - >>> math.expm1(3)
+ x" e; |( q' \5 } - 19.085536923187668
复制代码 ) f+ g0 ]- R. S& @. \5 d2 Y9 C
math.fabs(x) 返回x的绝对值
' q' \; @/ e0 }% W; m% x, N- #返回x的绝对值/ }7 {( _7 @3 J; G: q. W
- fabs(x)+ K& K% c, C! [ d$ ^+ y
- Return the absolute value of the float x.3 |; u; K( w2 {* j! l) s
- - Q* @9 ~" [: g
- >>> math.fabs(-0.003) m- i# M) ]' l" p* f: y% ^" n
- 0.003, v, y5 `/ F0 s) H/ |# u) ^2 [# f
- >>> math.fabs(-110)# Z& v, d" i6 _8 s( t9 Q O' w
- 110.08 I9 i" E4 `% }. F" G! X: ^
- >>> math.fabs(100)
5 {8 \+ J1 H, G1 F( z$ F - 100.0
复制代码
6 F0 y2 [' T& q1 w4 L- [+ xmath.factorial(x) 取x的阶乘的值
3 q0 e! Z$ R6 g: X. l% _- #取x的阶乘的值
" i8 h, l2 _; @$ I) J - factorial(x) -> Integral' E9 e3 W; ~+ a3 M( N
- Find x!. Raise a ValueError if x is negative or non-integral.
d: P0 s( v/ [ - >>> math.factorial(1)
# V6 t6 r: d6 S - 1
; b" D0 A* K/ ^) Z: m - >>> math.factorial(2)
3 P4 D. S6 _3 {' W - 2
`& l( R( L3 x; {( C# B - >>> math.factorial(3)
% l. U2 {+ ~" }3 P( o# w3 t' u# [& | - 6
/ p* Z/ i/ \& L; L - >>> math.factorial(5)
; t p" \- F/ ^+ }& I( U [6 R - 120- u4 i2 i6 h6 Y2 l# j
- >>> math.factorial(10)- q5 p' \: r6 C
- 3628800
复制代码
" f1 \& u1 w G' Lmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
, o4 e, P g$ O2 g: k; ?6 N0 w- #得到x/y的余数,其值是一个浮点数
2 W- W2 |: r' q; w1 w* e - fmod(x, y)0 S `+ ^) `7 X$ H0 v0 R* s; [1 ~
- Return fmod(x, y), according to platform C. x % y may differ.
: Y6 f2 N" P7 j1 P - >>> math.fmod(20,3). M5 F }5 _# Q7 B6 D; Q, L- J
- 2.0
M: Y5 d4 A; H4 W8 m - >>> math.fmod(20,7)
+ H3 E3 @( c- } - 6.0
复制代码
& d8 W3 W" a6 b9 Qmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: ]% q# _, ?5 `1 z. Q- U- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,, ~% S8 I1 B- t. r2 B& y
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
# | A' _1 Z1 N* ` - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 E6 |2 y; Y4 z7 C( U% {
- frexp(x)) X* M1 }2 y' ]! C
- Return the mantissa and exponent of x, as pair (m, e).5 e7 _+ D# n% \" m/ X, b8 U
- m is a float and e is an int, such that x = m * 2.**e.
! V2 P! w8 F: E$ h - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
3 h) m. D/ ? J( o9 p - >>> math.frexp(10)
8 l& W# {. C' T% T; T, Y - (0.625, 4)4 q$ _5 P+ i% @7 A
- >>> math.frexp(75)
/ ^5 |0 w W3 u: F5 a$ h- B2 [ - (0.5859375, 7)
8 B% s' d1 F3 F - >>> math.frexp(-40); f) H4 i6 G2 N) h$ e4 u. l
- (-0.625, 6); p6 Z. k$ [* R
- >>> math.frexp(-100)/ {+ r, ^$ Y+ H9 }' y% E6 ]# s o
- (-0.78125, 7)1 w* @" m, ~* |0 s+ z, o# L9 B
- >>> math.frexp(100)
2 c2 o' F. l6 n1 ~1 l- I' `. p. Y - (0.78125, 7)
复制代码
4 ^8 P& n$ @1 a. f7 `math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
+ G/ A4 r. m% }- #对迭代器里的每个元素进行求和操作6 I4 K& L/ G; K/ r7 O
- fsum(iterable)
4 P, @% `) J& q- E) J1 q - Return an accurate floating point sum of values in the iterable.8 a$ e8 M/ |3 {1 M( c# s4 q9 K
- Assumes IEEE-754 floating point arithmetic.* N$ N' |; z0 N* M. b. v) s0 q
- >>> math.fsum([1,2,3,4])% l/ B; D1 }' }9 g1 p" k
- 10.0- h" {6 T m k y- n! o
- >>> math.fsum((1,2,3,4)) x! L0 S4 {9 @9 j8 W' e0 @
- 10.0' V1 g- d9 K- g G! w. U
- >>> math.fsum((-1,-2,-3,-4))
! d( {+ b# [. ^" E" L - -10.0
0 k5 N) b) a, g' c& d6 U, ^* k0 W - >>> math.fsum([-1,-2,-3,-4])
' ~0 V+ l8 z( r0 r2 N0 D, c - -10.0
复制代码
; P" d* ?% a( J4 Xmath.gcd(x,y) 返回x和y的最大公约数
6 N: J+ o* l. `/ S- #返回x和y的最大公约数( d4 }& f% V3 x
- gcd(x, y) -> int
S. u- X. m: U - greatest common divisor of x and y& o2 v3 R' O4 P, e# }. K
- >>> math.gcd(8,6)
! \+ d! Y% ` d - 22 H# X {; u# G) t) R; k
- >>> math.gcd(40,20)( } m$ ~9 w8 ^9 \7 X; `
- 20
8 i0 Y" W( Y2 N3 b4 w& \ - >>> math.gcd(8,12)6 v2 K) [4 n& F4 K0 P1 r5 [
- 4
复制代码 & N, ]* ]. i$ m6 q* w
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* d9 @. v- F3 ~2 f2 B7 M( i4 o- #得到(x**2+y**2),平方的值
2 Q5 Q1 D. i( S. { - hypot(x, y)6 i, L3 o/ X# T# ?! K9 ~
- Return the Euclidean distance, sqrt(x*x + y*y).
" |1 J. {0 k5 J2 s- y3 \, Q - >>> math.hypot(3,4)
" I/ a% U' f6 l% |& I7 ] - 5.0- L! e9 M$ y7 G/ Q9 M4 D
- >>> math.hypot(6,8)
v: S1 Q* j7 b - 10.0
复制代码 4 z6 t# k! t0 g! C5 ^# p5 K
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False( ?7 ~% e/ J5 S) s, h
- #如果x是不是无穷大的数字,则返回True,否则返回False
/ r4 s* N* Z5 i) N - isfinite(x) -> bool
- L7 w1 \! F) P3 P& Q+ e% { - Return True if x is neither an infinity nor a NaN, and False otherwise.' K3 _; m8 M, R1 v8 G
- >>> math.isfinite(100)3 n! n# V+ H/ J# w, d: d% A& l
- True. r; _* z. i7 x% A% I Y( T
- >>> math.isfinite(0)+ j" m6 M: Q7 ]* t) m4 U) a
- True- B5 H7 Z3 n' }- E; G
- >>> math.isfinite(0.1)3 Y, E7 d' x3 u6 j
- True! g$ D: i L: O* G+ w+ n2 s
- >>> math.isfinite("a")8 _$ P: y, F" U% W B/ L: \8 b
- >>> math.isfinite(0.0001)
j$ ~& \2 I3 ?, g0 ] - True
复制代码 , W. ~+ O5 \' { g! l
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
3 H# V }- \' h1 M& [- #如果x是正无穷大或负无穷大,则返回True,否则返回False
! ]5 d) t# q& o( ? - isinf(x) -> bool
% G& u9 t- F! o9 a/ |( v5 A - Return True if x is a positive or negative infinity, and False otherwise.& O0 w2 }, \9 C, Q# {: J) U; V
- >>> math.isinf(234)0 `7 f( R2 E+ m# H @7 u7 Y
- False
. K B4 T. W0 O* @& i) v5 q0 u - >>> math.isinf(0.1)
$ m+ D( X) l" S9 V& u3 ? - False
复制代码 4 q+ H3 A7 L8 M
math.isnan(x) 如果x不是数字True,否则返回False
$ k- F9 Q; i- J5 {7 o2 J2 o- #如果x不是数字True,否则返回False
( p9 q" a, x% R, e" X7 f0 V# _ - isnan(x) -> bool
9 H" u9 n, K1 C% ]" v - Return True if x is a NaN (not a number), and False otherwise.0 V6 m/ b, i- f2 u, X
- >>> math.isnan(23)
6 u+ O) w7 B& T7 a - False
3 x4 k: q& z- h - >>> math.isnan(0.01)
* `) D2 W8 i* W2 ]) N - False
复制代码 2 {# I, f6 f, z5 i
math.ldexp(x,i) 返回x*(2**i)的值! ^' L w0 X9 ^7 x/ G
- #返回x*(2**i)的值
( b( l9 X) |6 Q0 Q" l - ldexp(x, i)
7 P/ z6 z0 x! n( ^ - Return x * (2**i).) Z1 K! t3 u# [, \7 _
- >>> math.ldexp(5,5)
, V7 k" @) p6 e$ M - 160.0
* y1 o& Q, |+ D4 L( Y+ L - >>> math.ldexp(3,5)
! j0 T( i: _8 f' Q& h3 h - 96.0
复制代码
1 C5 `2 T6 |4 o0 h: h# Umath.log10(x) 返回x的以10为底的对数: d$ t3 X$ F* X7 A( c4 ?( U
- #返回x的以10为底的对数
9 ]' o1 r# A( J9 L4 m - log10(x)
. G% e% D+ \$ B4 u1 l ^: t. ^ - Return the base 10 logarithm of x.3 F6 E, ~* U- V2 x$ u
- >>> math.log10(10)/ O/ V, T: F$ E' y' O/ R; c( {+ e
- 1.04 K1 t5 Q) X2 E* E
- >>> math.log10(100)& M. Y- ]1 Z+ ~* D# l
- 2.0
* }; S6 F6 a! N6 d; n/ o - #即10的1.3次方的结果为20
9 p- b l; A: n& H7 L) i - >>> math.log10(20)
2 a) z0 Z4 h4 ?/ U, r6 V+ n/ ~ - 1.3010299956639813
复制代码
+ X& N4 ?$ q7 cmath.log1p(x) 返回x+1的自然对数(基数为e)的值* q. V4 G+ y! T! ?7 U! f: A
- #返回x+1的自然对数(基数为e)的值
1 t' N8 D; c7 ^; c5 b( d - log1p(x)
! W p: B+ P! g) ~) _7 O+ Y) ` - Return the natural logarithm of 1+x (base e).
# Y9 V( i8 {8 a) X+ _4 Y$ [" R - The result is computed in a way which is accurate for x near zero.8 W, t3 O( F& m7 c) m% a; U; \
- >>> math.log(10)
9 b+ K1 f. |& t) u7 K - 2.302585092994046
, S3 T; U6 z' N0 b - >>> math.log1p(10)) l/ M: F) q$ [- W
- 2.3978952727983707
4 o* C0 |; t7 | - >>> math.log(11)
; J! ]+ X5 c& n7 ?2 P - 2.3978952727983707
复制代码
W' t; X% c/ _. M- D( P: Rmath.log2(x) 返回x的基2对数! k1 S' }; P5 l+ x: ]- n: B" X
- #返回x的基2对数
l; G/ n/ S% P - log2(x)0 i1 W# i, c3 G, ^$ I9 E- ]0 e
- Return the base 2 logarithm of x.
7 Z/ _# J& d2 }, _ - >>> math.log2(32)
) B S& z8 a; G6 \" U& f - 5.0 U4 l& W! z3 b2 W- \
- >>> math.log2(20)+ {" L8 Q4 ~4 T/ O' D( Z
- 4.321928094887363; M/ o o- i0 T _% @" @
- >>> math.log2(16)
6 R4 Y8 \( @+ A; u9 G - 4.0
复制代码
5 R$ V$ _% H! d5 ]5 ^math.modf(x) 返回由x的小数部分和整数部分组成的元组
" ^/ V+ q+ s3 P* o$ j0 P6 W f8 r# s- #返回由x的小数部分和整数部分组成的元组1 T6 S! N) n$ Y) C
- modf(x)7 m/ ~6 Q: l! x5 E$ [: X
- Return the fractional and integer parts of x. Both results carry the sign9 i$ M: m1 b% T. x
- of x and are floats.
* G, l: ?5 w2 ^) l* E( I( A% N - >>> math.modf(math.pi)
' |0 N7 K- k {9 @; @# N/ O$ T - (0.14159265358979312, 3.0)# _. L- p/ X. q; B
- >>> math.modf(12.34)
9 W Y4 @! \* ?; u* I) }3 k. | - (0.33999999999999986, 12.0)
复制代码
2 |$ i. b/ U/ P3 _6 smath.sqrt(x) 求x的平方根
% T+ j* @' D# W: T, ~" @' i+ Q- #求x的平方根3 n9 D' I: a6 t1 R
- sqrt(x)
! p( `* x; k3 V0 z4 P x - Return the square root of x.
7 d9 @( A3 Y$ L) f8 E2 Y4 g! y1 T - >>> math.sqrt(100)
' P! a5 c. X& _" `8 q) M* f$ r - 10.01 }, `; `, T- h- I
- >>> math.sqrt(16)
+ k+ r# c4 e5 H% ] - 4.08 L. u/ n% |$ I
- >>> math.sqrt(20)9 t# {% C& C0 K# t/ c" {5 }9 [
- 4.47213595499958
复制代码
: Y* T* b! C( S$ _* X* `& cmath.trunc(x) 返回x的整数部分- #返回x的整数部分
1 u Z) B6 I( o% z5 g% \7 Y* h - trunc(x:Real) -> Integral
1 Z. U( |7 N2 Z6 [2 X5 R- ? - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 R3 e( R) E# L. K* U - >>> math.trunc(6.789)8 M$ e" [9 y2 [4 E5 [1 ~, z1 ?1 k# D
- 6; {9 [, m7 ^- U0 _. d3 J( |, a
- >>> math.trunc(math.pi)0 K1 w( Z: [6 u- b' \2 F
- 3
, n0 Q2 x! c# A+ ^' @ - >>> math.trunc(2.567)0 O5 g, A5 J! Q- g7 k x
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|