马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
, c, a8 S5 [ R: S) ^! ~9 p! K
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 U' |. B' m& \4 R5 {" [
) ^" C O. I; s I8 d
方法1:1 _7 V# L! T5 m% q* G" m0 n' Q
- >>> import math; l$ q9 I2 X" s* [8 R* x! g8 P& U
- >>> math.sqrt(9)
! R3 N& m' Z' x3 n A/ h3 a - 3.0
复制代码 方法2:+ f8 a; }( d g2 W- N$ d; w
- >>> from math import sqrt2 Y& X5 _/ L9 S
- >>> sqrt(9)
& ?- u+ l& j- _# E) C4 z+ S - 3.0
复制代码
; x- L& M: Q9 |. J6 Y3 @. `
3 A+ ^: Z% Q$ B3 ymath.e 表示一个常量8 y- b* b8 i9 c0 [$ k
- #表示一个常量
( `# F7 B) u! L# }2 T3 ^ - >>> math.e7 V" o* n# I; D K/ ~8 a; x
- 2.718281828459045
复制代码
2 L2 S1 t5 ~, W4 H. T/ k' ]( {math.pi 数字常量,圆周率
9 x9 m' m2 j- H( E- #数字常量,圆周率
+ Y1 s$ @8 r9 w7 y' [- l$ s - >>> print(math.pi)4 ^) L5 t, f" [
- 3.141592653589793
复制代码
1 a7 h# b( G G+ K. s; hmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, o0 I1 `: _( ]1 v
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 b! A6 B" `, K M! m - ceil(x)6 u; K; F, i4 t* {; e; i6 Q
- Return the ceiling of x as an int.
* S( O6 l' K/ k0 ^% W$ i - This is the smallest integral value >= x.
8 r2 m8 V" G/ v. h7 Y7 Y
6 H: ~2 [8 _5 I6 ]( ]( b; y% d+ q- >>> math.ceil(4.01)( h) z+ |2 O: S" g# q5 E
- 5) ^/ `! |) Y. A! C! I( v
- >>> math.ceil(4.99)
1 e) k" x) c0 G* A% v7 N% ` - 5
1 q( r* q9 `7 [7 c! V - >>> math.ceil(-3.99)0 V% Y4 |. u, S% E; D
- -3
6 y& |4 f$ R- B" J) i& i5 \5 f( \9 a - >>> math.ceil(-3.01)
/ c4 K- h: X* s) T' V5 _ - -3
复制代码 % _. ^6 ^; Q1 b* @4 F! P
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) a( [1 _/ {% L, g& c6 n: g( t- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 a$ x6 Y. S- y B
- floor(x)
+ k6 Y( B1 w% O8 |: Z - Return the floor of x as an int.
9 y a z. K( Q- u7 U. s - This is the largest integral value <= x.) v/ d7 x) P- `6 |5 B Z4 ^
- >>> math.floor(4.1)% O5 \. i/ q; f- T9 h
- 4
! {/ q+ t8 b: M0 K - >>> math.floor(4.999)
# ~4 W3 G8 h; ]! X% L& c- m' X: q! Z - 4
+ i1 ]4 m0 P7 ]/ b9 i0 _) n - >>> math.floor(-4.999)
0 D& M& P5 m8 e/ {7 Y F/ X5 z7 C7 W - -5( M3 G7 p$ s7 ?9 }2 N' m8 n# _
- >>> math.floor(-4.01)2 R# z2 a& l+ c2 I# }
- -5
复制代码 & L" k2 J4 a6 w7 I! y) H
math.pow(x,y) 返回x的y次方,即x**y
1 x) q3 R9 h6 {9 f. y- #返回x的y次方,即x**y! J1 ]* ~+ F; ~- |
- pow(x, y)
" U; i+ @0 v0 C [7 N- ?# [7 W - Return x**y (x to the power of y).
( d6 f( A+ \! D! M e+ `7 b - >>> math.pow(3,4)
4 b+ F5 ]# t* k7 H - 81.0 n4 c2 f) a7 _" o2 o0 E0 L
- >>> 6 S7 A2 k: P# E' c
- >>> math.pow(2,7)+ ]5 H( L+ t* Y- C; E2 |8 w+ {) x
- 128.0
复制代码 ! n/ P2 s4 o) b' ]6 Z
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' u" T4 _( K% H7 X
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 ? @1 M% O5 L - log(x[, base])- M2 [% A! r( j7 t* p
- Return the logarithm of x to the given base.- H! n+ U, T+ u- ?+ k) M" _
- If the base not specified, returns the natural logarithm (base e) of x.
! x* m7 }* z( x+ c& F" F - >>> math.log(10)
/ C- A ^+ {; w$ a: K - 2.302585092994046
2 P8 @5 y2 ], E - >>> math.log(11)
1 u) a) K) H z3 x0 ]% h% t" g, _ - 2.3978952727983707 u E8 A! @: s! H" c0 X' b
- >>> math.log(20)- u3 h4 v1 f, ~, l! G# ^
- 2.995732273553991
复制代码
2 y: h |9 Q4 O# Umath.sin(x) 求x(x为弧度)的正弦值
" a/ u; K9 e n0 B# |0 P- #求x(x为弧度)的正弦值" ^2 m; \4 C) h5 B2 b
- sin(x)
9 t1 R/ ?2 H( Q - Return the sine of x (measured in radians).
; o/ N. v- Z7 R - >>> math.sin(math.pi/4)
) a% Q7 O, s$ f - 0.7071067811865475
. E: U E; A' K1 l; t* ^$ ? - >>> math.sin(math.pi/2)
( W0 ~( U5 y; U5 d - 1.0$ {7 B/ X7 }, a O7 F& G
- >>> math.sin(math.pi/3)
* o/ j, k) ?+ e9 W$ j - 0.8660254037844386
复制代码
( n8 s2 u4 Z; D7 imath.cos(x) 求x的余弦,x必须是弧度# n3 `% H+ L! O
- #求x的余弦,x必须是弧度0 E1 v; ?5 Q+ R' l3 N/ h$ i
- cos(x)
) u M2 Y6 `! L p) N+ M4 u# f' } - Return the cosine of x (measured in radians).1 [" F" t" y5 \+ e. i7 }' B
- #math.pi/4表示弧度,转换成角度为45度2 l' U. |) @" o
- >>> math.cos(math.pi/4)
$ L. T) i0 H5 o - 0.70710678118654763 r; d$ M [! H1 o
- math.pi/3表示弧度,转换成角度为60度% c/ Y8 \; b& R4 u; V7 w# a
- >>> math.cos(math.pi/3)
! o9 [7 D7 o* s+ _' A" }9 @ - 0.50000000000000011 G6 L$ [/ ~% E
- math.pi/6表示弧度,转换成角度为30度+ |; k: ?: X$ b5 {# h" t3 J4 O
- >>> math.cos(math.pi/6)
9 p9 i3 I4 l2 p2 F$ h* I' g - 0.8660254037844387
复制代码 $ ]1 `( k$ [9 l* ~, t. b
math.tan(x) 返回x(x为弧度)的正切值
$ @, M' v4 }& s* N6 B- #返回x(x为弧度)的正切值+ ~- r$ N0 Q. s. _2 P4 E r& S4 V
- tan(x)
# r3 V$ }6 y; n G9 {" I - Return the tangent of x (measured in radians).
& ?' ?! } F; g+ }! w - >>> math.tan(math.pi/4)3 C' S/ P/ i# F
- 0.9999999999999999
8 ]- f) p3 q* l" F - >>> math.tan(math.pi/6)' V4 G* v* b- d- t$ _
- 0.57735026918962576 f' J) E# P5 A: G; a. ]: G
- >>> math.tan(math.pi/3), H# p8 Y) J( M, g9 n( o
- 1.7320508075688767
复制代码
/ ^& W* O/ g" D: G2 ]! G. }math.degrees(x) 把x从弧度转换成角度
" }2 ?% \& u- W0 C; S- #把x从弧度转换成角度
# e$ p8 z8 D* p) I7 r$ j- u) P - degrees(x)
7 m2 d5 z0 m8 \ - Convert angle x from radians to degrees.) ~" m. p! d6 z+ E: W1 a
/ Z! b5 P) X, p8 m- >>> math.degrees(math.pi/4), V/ V s# K* g
- 45.0. Y# `- R' g8 P4 O4 D" s/ q
- >>> math.degrees(math.pi)
1 D+ F9 l& c- Q0 }( d! Y! Q& X! N" d - 180.0$ Y& x* f1 \0 V( T
- >>> math.degrees(math.pi/6)( Q. z( W/ l, f: c$ z6 {
- 29.999999999999996
Z! W4 q c6 N$ Y - >>> math.degrees(math.pi/3)
% ?# x+ i0 L; V0 n8 E" r - 59.99999999999999
复制代码
+ u' ~3 d9 c3 D6 lmath.radians(x) 把角度x转换成弧度$ m# K3 m5 x+ \& |! W
- #把角度x转换成弧度5 c4 o1 n0 k' F% f$ o Z- ^
- radians(x)
- _2 s. Q9 l0 w* V6 o: }4 p* E' ]4 l - Convert angle x from degrees to radians.
! P9 ]5 D5 m4 w* c' @; O( N" V - >>> math.radians(45)+ d$ ?' J0 _+ F
- 0.7853981633974483& w/ I* I& M9 J, y- Y( p
- >>> math.radians(60)6 R5 D. B! Q3 D, J+ v- r0 H
- 1.0471975511965976
复制代码 % H, h" n3 |; A/ v; D( d
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
T2 V; u! T* A( [/ E W, f- #把y的正负号加到x前面,可以使用0 z9 V5 I2 F. h7 h6 l4 \6 ]9 i
- copysign(x, y)
0 T' U5 {" G4 j3 k$ d - Return a float with the magnitude (absolute value) of x but the sign
+ r3 q- N( }/ B: J - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
* G$ h( k; e9 ?4 ^; z - returns -1.0.
! A) {8 Z% R2 {4 e' T% f9 V" B/ V) E - * r; S; Q6 \+ s) i" N6 s+ N6 A
- >>> math.copysign(2,3)9 _+ Y2 z2 k" m! |! M) Y
- 2.0
8 A: \* c5 \: M o8 Y - >>> math.copysign(2,-3)
1 G: o" g. [ n0 U$ D - -2.0& l, g8 e9 w$ K+ e. {
- >>> math.copysign(3,8)& |: A+ @* _: j, z* x2 s5 `% d: H
- 3.0
" \1 ^3 s/ J9 a7 ~8 t! S O! V% O - >>> math.copysign(3,-8)2 Z: E+ ~, y4 V! F
- -3.0
复制代码 + |% O# Y u' J$ ~: n* l$ \
math.exp(x) 返回math.e,也就是2.71828的x次方+ m: q9 H. C; |( u. ?
- #返回math.e,也就是2.71828的x次方+ G, T9 ^4 h1 C7 W0 Q1 ]: j
- exp(x)
3 y* ?' O4 ~0 Y n9 M, j& ~: Y - Return e raised to the power of x.) n$ m7 Z% W- M1 ~
- 1 O9 R6 x! n6 W- N+ B
- >>> math.exp(1)* }8 Y3 _+ l8 p* O
- 2.7182818284590450 U1 V' m% Z; e' |9 e9 A! ?2 A
- >>> math.exp(2)
; e9 b$ ~) ?( J- Y9 B, K7 { - 7.389056098930650 ~* u. V0 g: ^- p0 b
- >>> math.exp(3)0 I7 c4 u8 N' x% {4 A$ [* R! w
- 20.085536923187668
复制代码 ; i* G( H# o! O' |% K
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
8 a* s/ B4 c* U+ y E) D$ ]- #返回math.e的x(其值为2.71828)次方的值减14 y6 T* l4 H7 p* ]. |, R! l. S
- expm1(x)
$ ]+ X, j3 x5 z - Return exp(x)-1.
" F. ?1 ?5 Z' j - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
$ e1 G( G( S- s - : ?" ]" T' S( Z1 T7 b5 U9 n0 L8 a
- >>> math.expm1(1)
# q8 {3 g3 o2 P1 g& v3 B" P% J# p - 1.718281828459045
) E0 d. d# Y: ?: t/ `9 ~- e - >>> math.expm1(2)/ T6 ^8 Q r- }9 A5 V
- 6.38905609893065
' W1 P6 h! q* f& v) S3 x - >>> math.expm1(3)! [# N1 Z0 g/ f7 ?2 o( V
- 19.085536923187668
复制代码 3 [% ^; N8 T$ m+ G, Z3 q |
math.fabs(x) 返回x的绝对值% a( Y% J( ^! u6 [8 V
- #返回x的绝对值" B3 G6 B$ t0 F" M
- fabs(x)* Y# y) ~! Q1 m: U K! |: d$ h3 U. n
- Return the absolute value of the float x.
# }# q# ?4 Z8 V$ E9 W% j - # o( {7 v# j" B* A$ y8 B
- >>> math.fabs(-0.003)( C4 A0 a, N9 j% _% k9 F8 Z" @* ?
- 0.003
! ~' j6 \7 b* I- B. u/ B! s1 J% Q - >>> math.fabs(-110). t( L+ P# s: x+ O$ S8 Y
- 110.0
# o8 ^& Y- K% y' H0 o9 l) [ - >>> math.fabs(100)4 P3 f+ K% X' ]1 Y2 k* [" v. s
- 100.0
复制代码 7 H0 @4 M& X; D/ N2 R& D
math.factorial(x) 取x的阶乘的值 P5 h9 A' I/ A+ w, i2 F+ y( e% w
- #取x的阶乘的值
% W+ z3 I w" X* S' d; z - factorial(x) -> Integral: h2 `: l7 c: w4 l
- Find x!. Raise a ValueError if x is negative or non-integral." o+ v" @2 I$ K9 P: b* a) N/ U
- >>> math.factorial(1)
3 I* ?* Q2 ]( |$ w - 14 h/ q, ~) v$ A" i% C/ d' M
- >>> math.factorial(2)( z2 L8 G# M% o2 }% Q" n
- 2+ P* Z- u% e! Q. o
- >>> math.factorial(3)7 ]! B9 A# h' I+ I0 b& ?
- 6. O$ ?# o/ q/ H% ~" ~, |( q& d# T
- >>> math.factorial(5)
1 N o. w6 e0 t- J8 f - 120
" }+ y9 m3 B- C q - >>> math.factorial(10)
( J4 s) n# }# G! Q) ^+ o( I% J4 ? - 3628800
复制代码
' O/ W7 `& X' z: v( F! omath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
4 j; ?" _( R8 J5 y& K1 ^) n- #得到x/y的余数,其值是一个浮点数/ t# K( M8 C9 O8 r5 K
- fmod(x, y); a' y4 v. i# d3 X7 o2 B
- Return fmod(x, y), according to platform C. x % y may differ.9 g1 g$ g: R8 I) y% W8 c K
- >>> math.fmod(20,3)6 M$ f) m& U6 f' y4 S
- 2.0 l4 k/ r( k# N$ V* b( ]0 A( C
- >>> math.fmod(20,7)( e; H4 l6 W. n4 S9 I
- 6.0
复制代码
; k4 O I, _7 ~* h( J; \9 y, umath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& t, O) L9 W8 c1 s
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. |$ E, ]+ F- z8 L4 b% t
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值# `; n, W0 l2 y# M2 Y
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1+ n/ ]+ i0 g6 a' g7 \- \7 C( L8 |
- frexp(x)
2 ~6 c8 T* y& m8 U - Return the mantissa and exponent of x, as pair (m, e).
3 B$ X# i' M' Z" k - m is a float and e is an int, such that x = m * 2.**e.: V+ Q0 D$ T" B/ J0 h$ w, h0 A
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
: w1 k n+ O$ q - >>> math.frexp(10); L' S. d0 _, ]' f' o
- (0.625, 4)$ \4 B+ u4 J4 Z0 |* Q; W+ [' P" ~
- >>> math.frexp(75). K# E; i) [7 d$ L7 j
- (0.5859375, 7)0 e, ?7 y( g, D
- >>> math.frexp(-40), d8 g$ Z- \, _& V+ }( y
- (-0.625, 6)
r" b+ ]. `' E* V( j) K+ B) g( y - >>> math.frexp(-100)) f4 G$ D: w. Y" H9 F
- (-0.78125, 7)
1 a. m8 v M* v- f - >>> math.frexp(100)& f6 \' a1 |3 u
- (0.78125, 7)
复制代码 + t. B6 M) @" A: a9 F; h1 B
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
. s( ], X7 V9 `+ X6 s: Z% V- #对迭代器里的每个元素进行求和操作
% V6 R6 `& i3 I0 c% [8 ~& G - fsum(iterable): r# r f W# {- Y0 Q9 `; N. ]$ i2 l% {. X
- Return an accurate floating point sum of values in the iterable., N4 t8 m5 G$ r4 `! L! @: P; T' @
- Assumes IEEE-754 floating point arithmetic.
0 N: W+ }+ e1 I+ x - >>> math.fsum([1,2,3,4])
, L& ^* y T P7 M) y7 h1 M- k7 a - 10.0
& W0 |3 z8 S( A - >>> math.fsum((1,2,3,4))
! w+ q% l! z$ }; u! b - 10.0
2 ]: F( F0 q+ ? - >>> math.fsum((-1,-2,-3,-4))+ o# k; T2 \/ k2 _) S3 p0 `
- -10.02 s- {" n, z$ P) E; w- U
- >>> math.fsum([-1,-2,-3,-4])
+ w+ B+ T/ C. X8 i8 h4 _8 H - -10.0
复制代码
* x I( A+ m% V# n' o$ mmath.gcd(x,y) 返回x和y的最大公约数' L; D7 k& f. f1 p' ~
- #返回x和y的最大公约数
: I1 [' H8 B0 _( [+ r9 s& J - gcd(x, y) -> int
) l- M: p' Y/ u& @$ s+ W1 b" _! F - greatest common divisor of x and y
: ^4 R$ [( F" q2 q - >>> math.gcd(8,6)/ b& @! @- Y0 E) P6 y, X) y: X* B
- 24 N; ]& |8 C% g; j' W
- >>> math.gcd(40,20)
: M- l5 a$ j6 y+ M+ R4 g, R/ w% q; N - 20* A( w$ q2 L5 {7 | f
- >>> math.gcd(8,12)
) s1 o* ]1 b9 D; \$ w, b - 4
复制代码 $ r; |; t" \1 l
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 Y; X. R) r- X7 P5 w$ _
- #得到(x**2+y**2),平方的值% ?- q$ p4 i) @9 l7 s* z: |
- hypot(x, y)
9 e @0 E* g. F - Return the Euclidean distance, sqrt(x*x + y*y).& M2 n2 a* Y w: F
- >>> math.hypot(3,4)9 ~2 z N% I' n" W. c$ c
- 5.0) _ J/ s( `! {- V
- >>> math.hypot(6,8)
3 ~5 ^1 l' h. x) V7 V. j - 10.0
复制代码 + u( T& D; J# |8 S8 D0 ?! g
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False: s8 {" b+ t8 Y1 K( \5 V
- #如果x是不是无穷大的数字,则返回True,否则返回False
, C$ q" {0 v0 Z) R0 l - isfinite(x) -> bool8 x; S2 u2 c+ J4 p7 @& |
- Return True if x is neither an infinity nor a NaN, and False otherwise.
& ~" X0 h9 y( t5 o - >>> math.isfinite(100)
" a: `' i* G1 ~3 z - True
* b) F+ T! B% y( `+ R5 w& F3 E1 r - >>> math.isfinite(0)
* I; R/ W# _8 \9 \ - True
8 d- q; h& P) w- D* `5 `1 O0 t - >>> math.isfinite(0.1)
# J9 G6 S2 {; K - True
7 U2 u' L3 M/ }( {: w. G - >>> math.isfinite("a")
$ _/ a5 I6 `4 i8 | - >>> math.isfinite(0.0001)
H, G+ V; L: v0 `6 y - True
复制代码
( `7 J9 l" V5 j2 n# hmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
9 X/ ~ s" Y4 s! \, h: T b- #如果x是正无穷大或负无穷大,则返回True,否则返回False
/ @+ G1 c3 X8 b) s/ ]8 Q - isinf(x) -> bool9 \1 X3 e* ?1 U3 I' S) B9 Q. a+ ]1 m
- Return True if x is a positive or negative infinity, and False otherwise.
6 K" A5 \7 v9 G9 N5 o - >>> math.isinf(234)7 v, |5 ~6 X; O3 Y3 n9 b% P
- False; M4 I# Y& o5 E* I/ J' c) j
- >>> math.isinf(0.1)3 i) |, |& |8 c: ^* G+ `( C
- False
复制代码
/ {/ J2 j1 n' N4 z) J7 g" l9 bmath.isnan(x) 如果x不是数字True,否则返回False4 h: _3 j! r ]/ j H# m
- #如果x不是数字True,否则返回False; w! C& \6 V4 Z) H$ i- p2 m6 a) `
- isnan(x) -> bool
! U* _- i% L$ d* ^ - Return True if x is a NaN (not a number), and False otherwise., f! h$ G9 j5 C7 i2 G
- >>> math.isnan(23)
, ^, K2 |" s6 [/ e. |3 v/ ` - False
: X# ^% X6 E; h5 j `- u! v7 ] - >>> math.isnan(0.01). _8 [7 k7 L/ R, V* I$ Q
- False
复制代码 6 u( F: p$ r( ~, }
math.ldexp(x,i) 返回x*(2**i)的值0 D3 z9 S6 Z2 ~$ @9 m) V P
- #返回x*(2**i)的值
1 J& ~+ Z4 F9 w2 G3 t) Z& f - ldexp(x, i)# s! ]; `& z; x6 H
- Return x * (2**i).
5 w3 `; n) Q/ j/ v5 y7 M" M, B- Z - >>> math.ldexp(5,5)5 D8 a0 e, ] s' N9 q/ B" M
- 160.0
+ S/ J) `- E1 _& Y, X" k' \& m - >>> math.ldexp(3,5)
% n6 g$ d3 w6 A0 E8 \ - 96.0
复制代码
: V5 r4 s1 Q5 Omath.log10(x) 返回x的以10为底的对数
9 k" l/ X% B. S' @0 R L$ G- #返回x的以10为底的对数
) Q5 M y/ p/ u. e; [# S5 y - log10(x)
# O$ i7 F4 V/ _7 J0 t) K/ R - Return the base 10 logarithm of x.
# P/ c$ A1 f/ s8 S O5 ]! p - >>> math.log10(10)5 G1 d; b! K$ _# j- e( `$ x/ @0 V
- 1.0- q. X- H, p5 k" O( Y: I6 V7 [
- >>> math.log10(100)
o* G& [/ c! p - 2.0
2 D8 l- a; [5 p' c - #即10的1.3次方的结果为20 W7 v) S. d" m# G4 W
- >>> math.log10(20)7 ~0 M0 d7 L. X& P; x
- 1.3010299956639813
复制代码 3 \9 D/ H+ C# X' n( @( E9 R0 H
math.log1p(x) 返回x+1的自然对数(基数为e)的值
* s) B5 i; p7 l2 g* b, U) u- #返回x+1的自然对数(基数为e)的值
+ U9 f! i/ S! B# j& z, X& w% D1 a - log1p(x) ?( C1 _# O7 N& j, D: ~
- Return the natural logarithm of 1+x (base e).+ b9 @0 T( N% M# o" {
- The result is computed in a way which is accurate for x near zero.
# v- W' Z7 k3 S; q2 i _ - >>> math.log(10)1 b) e# t! E5 P, r7 i
- 2.302585092994046
" p; D* s1 t9 ~/ M - >>> math.log1p(10)
; W! n9 Y7 b% ^& i* n - 2.3978952727983707; N! V& H7 C- X( U4 T% N
- >>> math.log(11)
4 B# T* p* \0 R# k) C - 2.3978952727983707
复制代码 ; V; t# H* C2 C& [
math.log2(x) 返回x的基2对数4 s' o7 r h% G- d
- #返回x的基2对数
( {3 Y- C' O/ U - log2(x)
) `7 R8 F0 J$ |2 s) u; f - Return the base 2 logarithm of x.
% W! w' m- f2 P. o& | - >>> math.log2(32)
$ j# n# W) B5 l3 X9 s - 5.07 M) s$ ]) E7 K9 W2 D
- >>> math.log2(20)
5 h. c3 z! {4 T. S$ | - 4.321928094887363. `4 A( K* D' c P9 c
- >>> math.log2(16)
5 ?$ N5 m0 F7 [6 { - 4.0
复制代码
( R7 a7 Z$ t/ w! i; @ b5 z" z, ?math.modf(x) 返回由x的小数部分和整数部分组成的元组
3 c9 H( s; T4 a5 Z2 h9 z- #返回由x的小数部分和整数部分组成的元组
" o. j; B* W7 D" e - modf(x)( r) ~! C1 w5 x8 M/ Q
- Return the fractional and integer parts of x. Both results carry the sign
7 \, S; q! l; K4 h - of x and are floats.
! O4 P# n O! \( [ - >>> math.modf(math.pi)- S* @" O0 j' r: m* R6 ^
- (0.14159265358979312, 3.0)
. d1 Z8 x! [; a3 } - >>> math.modf(12.34)
* A% n1 }( ~5 Q8 \: Z( u - (0.33999999999999986, 12.0)
复制代码 8 O+ c2 |/ R" M8 _8 T- }
math.sqrt(x) 求x的平方根) `3 {; J B4 M |6 A
- #求x的平方根
; Z2 \' L4 t4 a0 h! b% X% o2 t - sqrt(x)( X$ J: o! E* |2 p. C
- Return the square root of x.
& n0 z; T) Y& O: m - >>> math.sqrt(100)
- s' x' q3 C- Y& t' k6 H - 10.0
) @" Q5 V R( R" }! k$ V - >>> math.sqrt(16)+ T# N0 x4 b* a* g) _5 f
- 4.04 _7 B0 l& s! ~% d" w+ E; {8 C, K
- >>> math.sqrt(20)9 ^- t. s" y! K2 z( o
- 4.47213595499958
复制代码
; W4 \! \% ?0 Q6 d+ J+ {7 Dmath.trunc(x) 返回x的整数部分- #返回x的整数部分
; U0 p7 \! y# E/ R0 u - trunc(x:Real) -> Integral
* |, v. s! b8 i, X E, {+ v - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
% {/ P6 j! V' ]" C - >>> math.trunc(6.789)) v! S7 c+ k2 l b7 Y' n, N
- 6' W5 n' I4 P, o/ Y
- >>> math.trunc(math.pi)7 n+ K5 k& p, C3 q, E3 Q/ ~) [
- 3
+ _$ B: G" q3 ] - >>> math.trunc(2.567)# N: m% Q. ?! y* Z3 o! N
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|