马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# l! X, ~& B: b& @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 `' ]; \( {; o& }" b* l3 l5 u5 r$ F( Y" ]
方法1:
/ T- d0 J* U. E r1 k- >>> import math2 W, D2 e9 I" d4 a
- >>> math.sqrt(9)8 E5 W/ P( ~2 N) o1 F/ t1 o
- 3.0
复制代码 方法2:- G2 `1 h! {! p* w
- >>> from math import sqrt
( ~6 Z g/ `: _, D5 v9 C - >>> sqrt(9)
" w; T) ~1 U, V; I& x - 3.0
复制代码
0 Q3 T: F( u8 P7 ~5 c
; F5 a- }7 h/ p- N5 tmath.e 表示一个常量
2 p+ _ e3 m0 H* \- #表示一个常量6 m; I* M9 u F) s
- >>> math.e
$ t& a: C' A) ^% R6 x - 2.718281828459045
复制代码 ) J" q6 F1 F& e/ |8 c
math.pi 数字常量,圆周率
4 m# o6 T8 c4 u- #数字常量,圆周率0 I0 H, [5 i1 s% e
- >>> print(math.pi)& [$ j; E4 ~0 _' z6 s3 u
- 3.141592653589793
复制代码 0 _+ X2 i) Q" S% x7 E
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x9 |% F3 {) f# J% K6 t" r
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 l4 J* H( b6 S: E6 T - ceil(x)+ ?5 _. z }4 K8 W- u0 o8 a0 _1 k
- Return the ceiling of x as an int.
+ J( f9 s7 m4 C4 c7 O7 ]0 }5 s) \ - This is the smallest integral value >= x.
) z; ^4 V. V* M7 l
, o3 N# d# O9 _0 f8 B- >>> math.ceil(4.01): i4 X G" v$ h$ n( [7 C
- 5/ M, E! J& x2 P8 W/ j' F; M2 b# C
- >>> math.ceil(4.99)
# }( a3 v) _* e( h - 5. g0 F; `: k8 }; o$ R# L9 N" \/ p
- >>> math.ceil(-3.99); r, I9 R E/ G8 f T* r* y4 Q
- -3
$ }/ U" p# [4 `" Q - >>> math.ceil(-3.01)* p$ \7 F6 _! V$ O
- -3
复制代码
. T% G6 O- L. c* Umath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 p* z8 \. ^9 Y- L( V" k- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 T" P% B! y4 r3 z9 E - floor(x)0 v: ^- t. |# h- h
- Return the floor of x as an int.
: E6 b4 ^9 p% J( W9 V: [ - This is the largest integral value <= x.0 y: e9 M! D k" }' d! l6 j3 ?; V7 ?
- >>> math.floor(4.1)+ K7 `2 n8 q& @. ^+ j6 a
- 4- R* ?' Z k% G$ b3 K: @
- >>> math.floor(4.999)
; j( b. X/ v0 n9 q2 _& W8 j/ ^. E - 4- q9 @: o3 t1 u$ u
- >>> math.floor(-4.999); m7 O& k: y5 o6 \3 [; L8 i; m, A
- -5% v2 @0 q+ D, `. ]. c0 s
- >>> math.floor(-4.01): v4 f" K/ ] A( O H) k7 z
- -5
复制代码 : N+ p! E& o3 `1 F+ S" a5 x
math.pow(x,y) 返回x的y次方,即x**y. M n. k9 P& r9 G) ^
- #返回x的y次方,即x**y
* c q9 h) j: g$ _ - pow(x, y)" T8 v- R; P$ J$ X6 D3 ~7 l3 R2 R0 o
- Return x**y (x to the power of y).
) x9 i2 W3 v7 G& e; ` - >>> math.pow(3,4)
; Z( n) P; b4 P! Z& L4 b5 { - 81.04 V W; ~& C1 u$ P: w1 L* C# X
- >>>
3 M9 b& G) F1 \ - >>> math.pow(2,7)
: S* R$ ^* p6 h& x6 v' Q$ p" t - 128.0
复制代码
+ ~1 t' f+ x1 Umath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" ~0 g% M# p$ a& |0 E- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) C l: j% Y* h' z Q, L" {
- log(x[, base]): M3 a$ T" @% Y B7 C3 S
- Return the logarithm of x to the given base.9 n7 K# n1 V0 o
- If the base not specified, returns the natural logarithm (base e) of x.9 }9 G& u; g! o) `1 K$ P1 l
- >>> math.log(10)
2 Q U# r4 F2 R: m5 u6 { - 2.302585092994046 u+ S5 C0 n! {; ~% U
- >>> math.log(11)
3 r3 n6 {! G( g) i8 l2 l. r# L0 o+ M1 Q - 2.3978952727983707
W0 y0 `& ]# f! r# O0 p$ }4 O+ k - >>> math.log(20)
" T, i9 ~# ]- D - 2.995732273553991
复制代码 * Q( m1 n$ R4 I4 u; O
math.sin(x) 求x(x为弧度)的正弦值6 X: @* H9 }* [- a1 }
- #求x(x为弧度)的正弦值
+ P6 v9 g7 ? E& D7 M - sin(x). w0 P4 l7 A. H2 G E2 p N
- Return the sine of x (measured in radians).
9 y% _' S3 Z# |: _6 q( Y* N - >>> math.sin(math.pi/4), Y' Z& R0 U) c' Q# ]) t' K
- 0.7071067811865475
, { Y0 @/ l+ J - >>> math.sin(math.pi/2)
/ ?4 N2 c% B: w b l/ ^" W; @ - 1.0
/ q9 s7 Z, H6 }* g8 o( ^ - >>> math.sin(math.pi/3)
, I i" h$ d+ }3 w c - 0.8660254037844386
复制代码 : m7 d' c6 X0 A U0 @0 v4 w2 ~
math.cos(x) 求x的余弦,x必须是弧度
9 D- z m7 s" [8 U7 o- #求x的余弦,x必须是弧度7 o v) N9 n9 d& m5 y8 A
- cos(x)
& s* _ ?" B- v) N" V) ~0 b - Return the cosine of x (measured in radians).
. L R* K- n: _6 c5 j - #math.pi/4表示弧度,转换成角度为45度8 {+ ^" T" r4 q+ L4 {1 W
- >>> math.cos(math.pi/4)
7 W$ u5 g1 L( z' o& Q - 0.70710678118654767 v. O) ]% J3 O
- math.pi/3表示弧度,转换成角度为60度9 ~+ i+ Q! ^2 k3 \" k2 B% Z
- >>> math.cos(math.pi/3), R9 j( O$ D9 |0 I
- 0.5000000000000001
: B- N, W6 l% q& C! F - math.pi/6表示弧度,转换成角度为30度* D2 P2 X( q. {. x/ ^
- >>> math.cos(math.pi/6)
% V, W5 @7 [6 [4 x - 0.8660254037844387
复制代码
6 z6 d2 i' |& j" L" F( v# Nmath.tan(x) 返回x(x为弧度)的正切值
. |( V3 C/ K+ D! `8 x, X! G- #返回x(x为弧度)的正切值+ d) _3 y: d8 w& ^
- tan(x)9 ]: u7 a1 ^% O8 N# T' }7 C5 A
- Return the tangent of x (measured in radians).& X# n. |' s: X5 F& J1 i/ U: Y
- >>> math.tan(math.pi/4)+ a8 S# [- p6 p. X) t, \0 N
- 0.9999999999999999" n. ~5 _* j+ X
- >>> math.tan(math.pi/6)
. [- L- M4 v% h0 J: P - 0.5773502691896257
2 R/ u; j8 A# C3 H- t( M - >>> math.tan(math.pi/3)
$ P1 c2 ?' v8 d6 P' T7 p - 1.7320508075688767
复制代码
" l2 b& G2 r# n9 d# @math.degrees(x) 把x从弧度转换成角度* `+ K4 \, C. V" l$ O
- #把x从弧度转换成角度
+ F+ l: P* Q2 c7 l1 Q. s" V - degrees(x)6 _/ g6 B2 K& ~3 V# o" D
- Convert angle x from radians to degrees.6 U4 a: h2 v& {' K: I0 f+ q
' P) S, U! P0 m) ~) e+ p1 `- >>> math.degrees(math.pi/4)+ b, u' M- m/ f: U
- 45.0. l. ?) O/ j& I4 V
- >>> math.degrees(math.pi)
0 N/ R6 f* I6 v/ H - 180.0" ^2 [: s; k8 J* ?% z
- >>> math.degrees(math.pi/6)+ \1 ` p6 s1 ^# N, D( R; r) G
- 29.999999999999996) K& P6 }, [. u& e
- >>> math.degrees(math.pi/3)
5 y w( j/ k' a. j& F1 c" ~, ^, g# j - 59.99999999999999
复制代码 3 w% G8 o: q4 \7 ^+ \# ~/ S3 H1 b
math.radians(x) 把角度x转换成弧度
2 F0 y2 E( T8 G/ o* Y* N6 n. R- #把角度x转换成弧度
9 y% g7 \$ e, B+ h1 w - radians(x)
8 b, c4 j/ c3 ?* M2 R$ ?3 A - Convert angle x from degrees to radians.! l+ s. t$ A, V% g
- >>> math.radians(45), g" F- k# [; a9 M7 y( M' }; x
- 0.7853981633974483/ z7 r, V3 G( l" Y4 K
- >>> math.radians(60)
- R5 g' _& r# ]7 I0 g1 s - 1.0471975511965976
复制代码
3 Z1 Q' l8 h6 Q6 {math.copysign(x,y) 把y的正负号加到x前面,可以使用0
9 U! G4 O# K- I, |- #把y的正负号加到x前面,可以使用0, `1 R/ S0 V3 w0 R" H5 w7 n" A8 d4 C
- copysign(x, y)
% ?2 T8 M$ d# O# Q- m3 E2 A9 S - Return a float with the magnitude (absolute value) of x but the sign 8 f8 x; o W9 r( O6 D( J
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) # r! {% g3 {4 ~9 g0 r; [" `, N
- returns -1.0.! g) u% r, r. o) G4 J! @: [9 z% g2 C
/ Z. H( |0 F8 f. _1 J" T& \; U; M- >>> math.copysign(2,3)
^$ l0 k! M* b9 c& K( B+ O - 2.02 i& P4 W! t$ @! U1 c
- >>> math.copysign(2,-3)
0 J6 W9 d5 y" Y- G8 J# O2 u5 u - -2.00 l* C& C Q0 r9 p
- >>> math.copysign(3,8)
) X, |+ P! W+ f5 t+ C( B, ] - 3.0# ]& W1 Y" R8 ^& q: U; _; e
- >>> math.copysign(3,-8)
0 S; h9 t, J4 a - -3.0
复制代码 : m5 q/ t6 l: j
math.exp(x) 返回math.e,也就是2.71828的x次方
6 F' Z A" ^# F- #返回math.e,也就是2.71828的x次方
6 W Q" j) s" g( J - exp(x)
6 t# g; |7 u) s, ^* o3 s/ p - Return e raised to the power of x.$ R0 }+ x& r3 K$ h2 ~
# r$ w2 h2 `1 ?/ {0 D9 J. K4 t$ G/ U* A- >>> math.exp(1)
# _4 P" L+ A/ f0 Y. Y0 J- ~0 V# b - 2.7182818284590454 t7 n. [: v- p* R4 J' d
- >>> math.exp(2)! E% T) Y2 t3 w9 r( k
- 7.389056098930653 \8 Q2 U( c& k+ {
- >>> math.exp(3)
* k" t& d" ^& y2 g" I - 20.085536923187668
复制代码 , D( Z; I1 |; ?" s7 L1 }
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
9 q* B% t0 S4 C9 y; T2 o- #返回math.e的x(其值为2.71828)次方的值减14 h5 U8 w9 I* n% q) O
- expm1(x)
/ g5 h& z$ t) c- T: Y7 @ - Return exp(x)-1.
* I: e4 |0 t4 R8 J - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
7 Q/ ]1 j' Q, G; j' s3 K - ' Z6 J# _/ K9 | R* y7 }6 `
- >>> math.expm1(1)$ g# r5 H4 q' z4 K9 p
- 1.718281828459045
+ O r1 n. u0 ~: B% z' P - >>> math.expm1(2)
% ^) ^9 T! p* e N2 j - 6.38905609893065
' z; r9 G: s- O( V; g4 J0 h! r - >>> math.expm1(3)9 C% p( Q: B* `# R% j: y+ {' [, d: d
- 19.085536923187668
复制代码
. m( ]' `" K" W& G& gmath.fabs(x) 返回x的绝对值2 @7 A' B4 t1 j
- #返回x的绝对值
" n T4 L; Z$ i9 @$ j$ j - fabs(x)
+ u# e- t2 v6 P @0 t0 @& Q - Return the absolute value of the float x.
& n: r& T! d5 g: V; B& W - $ a0 R: H( y. {3 g* w: b
- >>> math.fabs(-0.003)
# V k9 O6 \; X0 `+ j) K* t- m. J - 0.003% I& {4 L' H {- \, R2 m
- >>> math.fabs(-110)
3 m& W& E- C; B6 H, o( P0 O5 a - 110.00 C: c a1 a+ C. ~
- >>> math.fabs(100)7 k$ d1 m2 W9 J0 V% g
- 100.0
复制代码
+ m# O: ]: \& L( E+ W. h0 Ymath.factorial(x) 取x的阶乘的值
& \/ @0 t, E0 i; ^$ F- #取x的阶乘的值
8 U2 I9 n" T2 U0 h! L - factorial(x) -> Integral
3 M8 V( ~% l: M6 @& k - Find x!. Raise a ValueError if x is negative or non-integral., X' h* X$ I) j/ H" k S- U
- >>> math.factorial(1)
0 h& X5 s* @8 T7 z/ ~ - 19 N! Q4 m2 L q% a
- >>> math.factorial(2)
% `5 I- A5 p2 ^) C& c- R - 21 R. d3 C' G. ^1 [; {. |
- >>> math.factorial(3)" |0 @( D, F1 L5 i+ h/ g
- 64 u/ w! k6 A9 F8 K0 { H
- >>> math.factorial(5)
; ^/ G- \% Y( z3 r8 r( d' f - 120% A3 ?$ j6 E3 h$ M0 I0 J
- >>> math.factorial(10)3 X7 ~" z7 d: j$ }1 Q
- 3628800
复制代码 ! E# m9 O; ~9 Z! C0 G- I
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数3 |" n8 h- f5 u& N& [
- #得到x/y的余数,其值是一个浮点数& _5 y6 E7 f* s# m6 ?
- fmod(x, y)" _" P7 f0 U( j- _
- Return fmod(x, y), according to platform C. x % y may differ.. Y- y7 ^* X$ F
- >>> math.fmod(20,3)
( T( K4 }% p9 [! W+ E! { - 2.0
! |2 ?8 Z. C+ }9 K! q. C" O - >>> math.fmod(20,7)
7 ^/ n6 Q0 ?1 F$ N5 i# r - 6.0
复制代码 1 Y. v$ J. e" y# d, p9 m
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围5 _# M0 `* r; X
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ Q$ Z7 f3 U+ m B
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
1 n) G$ A% d* L8 y1 r) G5 I' r - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
' t% n* Y) ]* c2 E. |; W2 O - frexp(x)5 N$ d' o: C/ G) k; p1 ^ a: f/ q
- Return the mantissa and exponent of x, as pair (m, e).
8 ~4 Q' [0 f! F3 q - m is a float and e is an int, such that x = m * 2.**e.
* t/ k; z' a- _7 ^ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.& d! J- Y& u3 `% Q9 F c5 `: l
- >>> math.frexp(10)
5 M; ?! ]2 a, G( I; q! \2 z - (0.625, 4)
- f/ r) o: H* J" ^- r, [ m - >>> math.frexp(75)- Q) `+ @: M+ y% ^' M6 u
- (0.5859375, 7)
0 X4 T- S- e% H, z9 I4 F - >>> math.frexp(-40)
& ^; f3 s2 }6 }: u7 A* Z - (-0.625, 6)
0 ]2 f, |: H, O2 X - >>> math.frexp(-100)6 s R+ }: ~! Q7 u' j- t, L' S
- (-0.78125, 7)
0 N3 p. w6 Q& Y* g - >>> math.frexp(100)1 ?' C. j+ r3 \& B) g5 }
- (0.78125, 7)
复制代码 0 o. R/ a9 w4 ]$ S# p* e- ?3 i& n% N2 p. Z
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
: E! w/ O$ `. q5 ]- #对迭代器里的每个元素进行求和操作
: p: u1 g, T- o9 q - fsum(iterable)
5 F6 |2 z8 C2 _( n& J% a" J - Return an accurate floating point sum of values in the iterable.
# r b5 \9 [6 c - Assumes IEEE-754 floating point arithmetic.
- `% n' r4 g+ F7 J - >>> math.fsum([1,2,3,4])* q9 z. A' P6 A. l0 }1 E/ X; _
- 10.0
0 o' e+ M& |1 s" q9 z9 G, C - >>> math.fsum((1,2,3,4))% J( F" ?5 J3 x) ^) Q y% ^
- 10.0
" R0 e2 X5 e3 j5 k4 P! e% v. g/ c. P - >>> math.fsum((-1,-2,-3,-4))+ _, S1 b4 c d3 Q/ W/ k
- -10.04 @! i: J9 N$ [- ~" j- ]* v
- >>> math.fsum([-1,-2,-3,-4])
4 t& O4 r$ d$ ]' u E" W - -10.0
复制代码
2 V- ]! h+ ~; qmath.gcd(x,y) 返回x和y的最大公约数
- f/ v# ~4 M- t u8 p+ R- #返回x和y的最大公约数3 p( M$ l$ t& p+ G# z
- gcd(x, y) -> int+ m. k5 B2 H5 U$ c& V
- greatest common divisor of x and y0 i5 w, ^; X: P% r9 M/ O
- >>> math.gcd(8,6)
" h- s( T" H, g5 Z" x1 P% z - 2
. d& M% M D3 C" {4 ~ - >>> math.gcd(40,20)$ l& w z- U+ F0 ^# \8 s7 L
- 202 W4 m% Y- A; o W
- >>> math.gcd(8,12)
P+ [; u$ I7 A% y' V8 h - 4
复制代码
; S+ y9 c. i& ?+ C; h) emath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
2 @8 ], Q# I) z( M* b- #得到(x**2+y**2),平方的值+ E/ l) N* e3 U* {3 r' R/ O
- hypot(x, y)
) f( r# l6 X! C% ^& v! J - Return the Euclidean distance, sqrt(x*x + y*y).
6 z1 v% Q2 T" D: b! q' I ~+ {! c - >>> math.hypot(3,4)
" m" C2 A% |: @& |- K$ f2 e - 5.0
$ D G' M- R+ b# X1 ~ - >>> math.hypot(6,8)- R9 V3 n* N& r4 Q4 J7 Q2 z7 K
- 10.0
复制代码 8 x1 ]8 H f0 ^$ s$ s3 u+ G' O
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
4 `& C; o ^3 ? z- #如果x是不是无穷大的数字,则返回True,否则返回False
- \, s, O9 W/ v, I& d) u5 \7 o - isfinite(x) -> bool# E, f6 i( ]9 _6 ]4 d6 s( n& f" j
- Return True if x is neither an infinity nor a NaN, and False otherwise.
; E5 x8 R6 |6 A- M/ c/ s8 d2 y) b - >>> math.isfinite(100)
$ I0 C4 o: L. t5 Q( S) x4 S - True |9 m5 P7 d* q' {& l2 Z* R) N1 {* Y! `
- >>> math.isfinite(0)
; k ^& v G( E - True! m4 }6 G8 o% J; D% d
- >>> math.isfinite(0.1)
. {5 N+ B: g( a l' ^2 s4 L5 W - True2 g0 R* R2 [# [, q2 ]' S: \
- >>> math.isfinite("a")' z7 [. e2 _* E: h1 P( D- D9 q
- >>> math.isfinite(0.0001)- w0 N: `, m. D
- True
复制代码
; @7 O( `/ r0 `+ v# Imath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False L9 a7 p! |9 m* f
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
- f- \. X; b2 Z - isinf(x) -> bool* N- P# X+ ]3 ~# h: V
- Return True if x is a positive or negative infinity, and False otherwise.
* j0 N ]2 Q4 i& x. P. u. v - >>> math.isinf(234)
5 L9 Q, d0 z' v0 G" @ - False
! I% l& r( _6 `8 B1 p0 h( C - >>> math.isinf(0.1)
! ~/ L0 Y$ |1 p- g - False
复制代码
; V9 z' h% [( f, H6 P- cmath.isnan(x) 如果x不是数字True,否则返回False
/ {) @" `* O4 l! b- #如果x不是数字True,否则返回False- i7 d- y$ {( l% v% d
- isnan(x) -> bool
+ \# ^/ b1 A" Z- w% b - Return True if x is a NaN (not a number), and False otherwise.1 |9 Q/ {+ J5 u0 }- ~$ n* K8 W
- >>> math.isnan(23)- u, l1 J- a! q/ K3 M i& W1 K
- False) H& B* e1 E9 q
- >>> math.isnan(0.01)
: |/ Y, @/ A9 h. W - False
复制代码 : Z: L$ D5 Q3 k
math.ldexp(x,i) 返回x*(2**i)的值1 q: @) ~" q% V. w8 J) d
- #返回x*(2**i)的值8 |1 e& e8 l& F5 l0 y( c
- ldexp(x, i)
* s" W* ?; @4 m1 M" O - Return x * (2**i).
, l# K/ X+ @; q. ?) ?6 h8 \ - >>> math.ldexp(5,5)
- s" B/ X3 T6 K/ N$ P - 160.0
/ }1 b7 @$ M: @; [9 P - >>> math.ldexp(3,5)' N2 h1 ^2 v$ B7 F8 Q: _
- 96.0
复制代码 8 V" U- I( b) N. k/ Z7 F
math.log10(x) 返回x的以10为底的对数* e/ K3 N4 e5 H. d2 q
- #返回x的以10为底的对数 C1 f1 Y* ~2 U7 n& s- W
- log10(x)
2 G9 T& v# m5 V9 c - Return the base 10 logarithm of x.: S+ G1 b- j3 o% g( ?" R6 L
- >>> math.log10(10)
9 l1 l, L+ w: A7 F. F - 1.0( }0 \" {$ T1 [# ^
- >>> math.log10(100)
( J, ~+ ?% @: p9 I0 ` - 2.0
% h2 ^/ T$ `( D4 E p - #即10的1.3次方的结果为20$ Y: }- \7 t7 S0 u. k+ _3 y& ~
- >>> math.log10(20)
0 `, f1 `. s+ I+ |7 k - 1.3010299956639813
复制代码 " A. ?8 o: J2 H+ X/ k6 P% r% o
math.log1p(x) 返回x+1的自然对数(基数为e)的值
' g9 |$ z% l( J! i, M; Q- #返回x+1的自然对数(基数为e)的值
& z" C0 l w, G% _6 T - log1p(x)) C& R2 |( m; L- @/ R! E0 \' }
- Return the natural logarithm of 1+x (base e).4 {- g/ j7 _) U9 ~ |
- The result is computed in a way which is accurate for x near zero.( H2 M5 [. \9 E: c
- >>> math.log(10)
$ m; ~ x6 J' R x3 } G+ m' T - 2.302585092994046
& n3 P( D8 A; s& M( [2 c, M/ U - >>> math.log1p(10)
: e, a7 T. F+ n - 2.3978952727983707
2 v" h5 ` X! `: r. S4 t8 ^. [ - >>> math.log(11)5 p: T# E2 u3 E; S. O E
- 2.3978952727983707
复制代码
. e# k: ~$ T4 r5 Umath.log2(x) 返回x的基2对数7 M6 z) c- b5 G1 r. r
- #返回x的基2对数- G! g% C: H. h1 \5 j* p v+ D
- log2(x)7 u: m9 n; [8 C8 Z' N5 i* v
- Return the base 2 logarithm of x.
; Z; A, x8 k5 B - >>> math.log2(32)
3 L* s+ B, H* d/ ^- Z% H' B) }1 N - 5.0
: X/ k, I8 j2 `" F4 U$ r& L/ T - >>> math.log2(20)
8 l8 U# K9 F2 `% @8 { - 4.321928094887363
# ^8 o# Y. `% c& ^+ b% O, h - >>> math.log2(16)
% K; b7 _2 I& g1 A- h( P6 b9 t - 4.0
复制代码
. K7 c& \( w1 Q3 y o" ?8 I& xmath.modf(x) 返回由x的小数部分和整数部分组成的元组# q% _. |# P# k6 R" J9 L
- #返回由x的小数部分和整数部分组成的元组 X9 k, Q$ T# z
- modf(x)- X' B! ^) G$ c4 d6 l& P3 H }
- Return the fractional and integer parts of x. Both results carry the sign4 j! u) R6 V) f! M% y4 j8 d( B9 p
- of x and are floats.0 v8 \! p# b1 H7 e4 \- O( D
- >>> math.modf(math.pi)
+ x6 y' @/ a1 u4 Z) ? - (0.14159265358979312, 3.0)
/ {6 X8 ?5 i5 A/ b7 A - >>> math.modf(12.34)
$ e- P4 ?4 ^% w. q3 ?6 F6 Q - (0.33999999999999986, 12.0)
复制代码
9 v1 b0 a4 X* ~math.sqrt(x) 求x的平方根
1 h4 M, q" `8 @% W- #求x的平方根
! J5 F! \$ R1 J) y' w - sqrt(x)$ t* ]* c. r% l) k2 Z x
- Return the square root of x.$ _* P+ z4 Q3 G0 ~8 s* Q2 p D
- >>> math.sqrt(100)
5 [1 ]# R* w- w' p) }; ?* m - 10.0
. [5 @7 p% K \5 z2 b9 C - >>> math.sqrt(16)9 J* f4 a* D5 z$ k
- 4.0% K1 ^6 |- z& F
- >>> math.sqrt(20)
2 |, h% F+ A9 {+ K - 4.47213595499958
复制代码
, ?6 {) v. g! H2 N1 l0 vmath.trunc(x) 返回x的整数部分- #返回x的整数部分
% O3 t' j( S: Z8 j4 m6 M. { - trunc(x:Real) -> Integral1 J8 ~7 A$ q$ c z; X
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
4 p- j5 i' t# M5 Q9 K. b& Z - >>> math.trunc(6.789)2 u; ~* s' u( z: s5 t# {
- 6( C: A. o5 c4 g
- >>> math.trunc(math.pi)
% G& t% w7 C1 | - 3
9 r) [5 `* V" ~3 O) o, h2 I6 c - >>> math.trunc(2.567); R/ [5 i2 y& _+ u' R* v
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|