马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
o, T) ^$ Z- v6 q' c9 M
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ z7 F5 S+ b B4 A( x" O/ B( V6 q4 c" ~; I" Q6 V2 \
方法1:1 D" c, g5 |% r6 B" Q
- >>> import math( f# j; o5 F3 C+ ]' q, C: l: t0 B
- >>> math.sqrt(9)
f3 i5 y- e* B - 3.0
复制代码 方法2:
$ B, ]# ~) v5 L( h" a5 I3 a- >>> from math import sqrt2 G' L/ y' a3 D+ l9 ~
- >>> sqrt(9)$ D( U) l& s! A; s' o& m
- 3.0
复制代码
: T$ ~7 k# i* M
E0 A8 ^( |, s% lmath.e 表示一个常量
. p* P9 }% X3 b5 m- a- #表示一个常量# W/ b. ]5 Q$ S) w+ q
- >>> math.e
9 R) m# w0 I/ p' a9 g: z8 t - 2.718281828459045
复制代码
% d7 v+ y1 \2 l; ~: W! qmath.pi 数字常量,圆周率0 [+ i4 I j$ V
- #数字常量,圆周率$ y/ i* d @2 Z
- >>> print(math.pi)/ G2 L* Y% _6 @* j N
- 3.141592653589793
复制代码 9 u# d7 c( [3 ^# @7 ?# g
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, q b- b" b4 A# d9 r5 \
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
, j) N; r4 P+ ]- G z9 {" n; v' R' q - ceil(x)0 O! @5 v8 r# W- n. \, u4 z
- Return the ceiling of x as an int.
: H8 y- z5 ^1 c) [8 y& [/ R! n* K6 s- N" {9 q - This is the smallest integral value >= x.
! O7 t% G5 m! |+ y - 4 M7 s+ x2 B5 z4 Z" [% g5 v8 I
- >>> math.ceil(4.01)
" r8 j+ d e+ s8 e/ @ - 56 y1 \* c! _% | R# d0 Y8 E
- >>> math.ceil(4.99)6 t ?% r1 a4 z0 _" Z
- 5
& `& g% a$ {, | - >>> math.ceil(-3.99)
# p/ k) G& G! D0 F - -3. z: p/ K; t* }0 ]( o
- >>> math.ceil(-3.01)
2 l3 K i1 ?* O) m6 s3 b) t F) B - -3
复制代码
7 S0 j7 m: L/ o& jmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 z x3 \7 G' ~3 Q3 W9 w6 |
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' X% t+ o w z U3 V - floor(x), ~8 H. x {/ G' o
- Return the floor of x as an int.1 W' p/ V$ Z4 p5 k
- This is the largest integral value <= x.0 e6 \+ ?7 W+ t
- >>> math.floor(4.1)
) `9 f8 f2 Q D& t - 4) M5 P- z5 Z. ~4 E( \
- >>> math.floor(4.999). t2 }- @2 H6 ]( U( z) V
- 4: \: S0 ~+ q3 g
- >>> math.floor(-4.999)
& r9 a' z* U% E' ` k - -5
& h6 @) q, x/ m0 W; y' p/ m - >>> math.floor(-4.01)2 e! A# d8 m! O, f, T. u8 t
- -5
复制代码
. F+ U" e) f# `* j* Bmath.pow(x,y) 返回x的y次方,即x**y
0 M Q3 d# A0 K1 |/ m" A5 _- #返回x的y次方,即x**y; R1 F, |' } P8 L1 }
- pow(x, y) c" ~3 Y# @& Q: Y
- Return x**y (x to the power of y).8 O4 c$ Y# C8 A( D* Q
- >>> math.pow(3,4)
7 t. V A7 i2 u9 v$ @" \ - 81.01 ]8 x6 V5 l( \) i+ b" c) S
- >>> * g3 s) l' B0 k. ]# n9 j
- >>> math.pow(2,7)9 `% n4 ^, d0 j% f7 y7 |
- 128.0
复制代码 ; @/ q8 f- g. |
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
) K7 S: M4 O. u$ E5 @% w0 t- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). M/ j9 B% W8 f7 z
- log(x[, base])# [- Y7 {) W7 p; {) G
- Return the logarithm of x to the given base.
1 l$ r: V6 F9 |( e - If the base not specified, returns the natural logarithm (base e) of x.5 Q- b3 O8 J; Z0 V
- >>> math.log(10)1 |8 `& C! E0 \" p
- 2.3025850929940460 P5 k3 U* D$ H7 T9 h
- >>> math.log(11). Z$ y1 |2 l3 \8 s+ u
- 2.39789527279837077 t6 B# ?( i, X" ?0 t0 [
- >>> math.log(20); n5 L0 u& B: E9 {$ d$ M1 p* h
- 2.995732273553991
复制代码
K9 |- Z; z8 \( Emath.sin(x) 求x(x为弧度)的正弦值
2 g! w5 T# D, i( b" I/ [- #求x(x为弧度)的正弦值" l3 I; H% _$ n9 V, e/ K
- sin(x)
, W: B1 a0 V3 g - Return the sine of x (measured in radians).& U+ N0 k; Y# r8 W4 P
- >>> math.sin(math.pi/4)
- x0 B2 a0 Y$ ]6 S/ S - 0.7071067811865475
L& j- V \0 C8 m* y" {( o - >>> math.sin(math.pi/2)1 M) X5 _$ O: ^8 @3 M( d! t
- 1.0
9 b! ^: `! p- B: i5 Q - >>> math.sin(math.pi/3)& v- k7 b3 J e7 X
- 0.8660254037844386
复制代码
2 d4 N6 o; O; Y/ |; o9 m' I# w0 Qmath.cos(x) 求x的余弦,x必须是弧度2 V& f: k' J" ^/ t" D& j
- #求x的余弦,x必须是弧度
. F' U' O* h& {1 J - cos(x)
3 B9 ^' e* ^; H7 g; H M& i - Return the cosine of x (measured in radians).4 y- C5 \# \8 W B
- #math.pi/4表示弧度,转换成角度为45度: a0 j, v2 n1 n1 g9 ~2 |
- >>> math.cos(math.pi/4)
" }6 Y" s- B9 t - 0.7071067811865476$ X% X: g. A, y+ O7 |
- math.pi/3表示弧度,转换成角度为60度
; E n0 @( `( K: Q8 A; m+ j - >>> math.cos(math.pi/3) R G) N" t7 ?, J: [
- 0.50000000000000016 a. J+ y+ R! Z' j& [, ^, Y. y
- math.pi/6表示弧度,转换成角度为30度
7 `$ V, P$ l% U- ? p - >>> math.cos(math.pi/6)
! h4 p: ^3 ]3 Z5 ~" L - 0.8660254037844387
复制代码 + j1 {$ w! ~7 e- T- s
math.tan(x) 返回x(x为弧度)的正切值
- e! h2 p, u; C% J2 ^1 B- #返回x(x为弧度)的正切值; P. \& P' K: D) F) v3 g6 C
- tan(x)0 q) E+ `# e* _! |6 \: { [
- Return the tangent of x (measured in radians).
- e2 y3 j: m D! \" m; _9 o - >>> math.tan(math.pi/4)
\* b2 l _2 z& |/ I& @ - 0.9999999999999999
& @* a3 [' O4 X3 f' i - >>> math.tan(math.pi/6)" C; Z% P b7 L2 D I( m5 f
- 0.5773502691896257# Z8 g/ B$ v% R% }( q
- >>> math.tan(math.pi/3); ~9 Q" z$ ]$ M2 N3 J
- 1.7320508075688767
复制代码
1 n) ?! k+ W' Wmath.degrees(x) 把x从弧度转换成角度
% y6 ^/ N8 Z. V6 n" C; l* C2 w9 k- #把x从弧度转换成角度0 K( ` @- N- f. _( h+ @6 Y
- degrees(x)4 M+ E$ S F# S2 @( _* w
- Convert angle x from radians to degrees.* D2 N# w6 \3 v8 U3 @& u! q4 w
- : f& L. L" @5 [
- >>> math.degrees(math.pi/4)" P" X, S" G" C0 z( F6 X
- 45.0* c' m' H4 N* E8 ^# @& ^; ?% ~
- >>> math.degrees(math.pi)
' R, @0 n& o+ Y" ^2 w+ T' u- Q - 180.0
! v+ d5 l( v0 o3 h9 x4 Q# H3 I - >>> math.degrees(math.pi/6)1 i! @/ j6 }6 K
- 29.999999999999996
8 x: ~$ D7 k. C4 @: H! X, o7 Z - >>> math.degrees(math.pi/3)
9 }! j) z8 R( {+ B - 59.99999999999999
复制代码 # C( h3 E7 t$ v7 O
math.radians(x) 把角度x转换成弧度8 W @3 }5 ^4 |5 s. }5 k
- #把角度x转换成弧度
3 P! G) v# r- ?9 v5 p" ] - radians(x)4 L% Y( E3 R9 }; D) o
- Convert angle x from degrees to radians.
+ D: B. A: S9 j' W% h0 B - >>> math.radians(45)
! c! R4 p0 k% q: \ E9 B - 0.78539816339744834 D' w5 R4 b- K6 ~
- >>> math.radians(60)
3 l8 `1 M, ]' e7 x - 1.0471975511965976
复制代码
% S3 Y' _) w; a Tmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
' Y, |1 @) `7 k; Y- #把y的正负号加到x前面,可以使用0
! ?6 f- `% F0 N* g, X) E - copysign(x, y)
, M ] o: h) ^8 K6 [8 N - Return a float with the magnitude (absolute value) of x but the sign " n6 y) d5 T5 C" S1 |# y
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
4 C6 b: \. @3 J3 F- J - returns -1.0.$ h) W6 `+ n1 D2 B6 u( Z6 _" G
- , j0 r* s- e7 q, b5 g. v
- >>> math.copysign(2,3)
4 x+ }" C3 {) ?; k' _7 h - 2.0- G( k7 \& H/ S" n; ]8 p2 [$ I0 P
- >>> math.copysign(2,-3)/ N, R' S; H' t6 m
- -2.0' `4 K- M: ^( p8 Q
- >>> math.copysign(3,8)
& L) ~1 P! n- P1 ~ - 3.00 V: K" f& O& a* T- x
- >>> math.copysign(3,-8)
6 ] J( y$ \' x& g. ` - -3.0
复制代码
1 B# B3 s& I: [, U$ r% U1 p3 t" Hmath.exp(x) 返回math.e,也就是2.71828的x次方3 K2 T. n+ ?( l- e8 r" N" P8 s$ x
- #返回math.e,也就是2.71828的x次方+ s6 K3 W3 w8 i/ l
- exp(x)2 ]3 S8 G4 C0 [- ?3 s6 V
- Return e raised to the power of x.5 [& R1 V9 x0 X2 y/ v
( H, E H/ Z) p- >>> math.exp(1)8 o3 F8 Q2 b/ w. q) i% {" f6 H t$ R
- 2.718281828459045/ z* O8 I& f& t# H, e
- >>> math.exp(2)
, M% z% x8 ]. g% S9 A, m+ o - 7.38905609893065/ ] Q3 t$ e% M1 e% a
- >>> math.exp(3)
7 w8 I: c3 O7 O% r - 20.085536923187668
复制代码 5 o( O4 {7 _/ C. d/ L' m6 a
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) ?# M% W2 R! Z) K" s
- #返回math.e的x(其值为2.71828)次方的值减1
3 z3 u& c6 y, K$ u4 G) { - expm1(x)- t. t$ X y; e: i; ]. z7 M2 U) ?8 ]
- Return exp(x)-1.- [2 u! X- e- ~. y: y4 I3 \- i3 s
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
! N4 a/ U, T7 @' E( b* N& l" f: U' Z - 7 a5 p" s" Q8 r6 S ?4 u
- >>> math.expm1(1); @$ l( F6 T' q) ~( `
- 1.718281828459045% J! ~& K" M/ f3 j/ s/ X6 O9 u
- >>> math.expm1(2)
0 y. q+ A3 N7 W4 I/ K ~ K0 o7 L - 6.38905609893065
2 g. P. a( ~5 W! x; w - >>> math.expm1(3)
& D$ ~. ]8 Z+ G! |6 a3 N) e - 19.085536923187668
复制代码 " I" L1 v: n M1 ?) x1 j: G
math.fabs(x) 返回x的绝对值
7 i$ [+ A; u. w- #返回x的绝对值
3 R, |. A7 t" R. ~ - fabs(x)6 m+ v9 b5 M% f6 ?2 k" M( `
- Return the absolute value of the float x.8 b8 L5 q0 p7 C9 J, q
" G5 Z! ^ k9 s* o7 @* s, O. x6 i- >>> math.fabs(-0.003). P5 t/ N. ~' E) J8 z' `
- 0.003! ~0 y1 V+ {& H4 X4 i K' r! W
- >>> math.fabs(-110)
' e1 }( h2 X/ d7 P9 e1 w; y - 110.0, t+ k$ R! I2 {
- >>> math.fabs(100)# ]: P0 u: n8 e; ]9 T0 ~
- 100.0
复制代码 ( Z H S6 k" L6 k4 ?& [
math.factorial(x) 取x的阶乘的值
' S) A1 F- n5 _( ]8 h+ e6 ~9 S- #取x的阶乘的值# s9 R {% l7 h! I
- factorial(x) -> Integral, v% f2 t2 k, c9 ?) z9 ^9 I
- Find x!. Raise a ValueError if x is negative or non-integral.
1 t* o; E* r' C; m( ^" J* E: l - >>> math.factorial(1)
8 [3 V- D# O- p7 B2 V" ?/ S - 1
; \" t# p0 o8 q5 F: ^4 [9 j - >>> math.factorial(2)
/ I0 L4 p0 E- M( Q3 z! G1 o - 2$ T2 o! p4 S0 }) \% \8 t8 h" P
- >>> math.factorial(3)
- Q# \8 I! y$ O2 P" u- ~% ]- Q - 6
m# e- S$ B6 z, B - >>> math.factorial(5)
& t2 X1 Q- x3 \" Z. U - 120
) k7 E1 G+ x: p+ I, [) D& d# `' e - >>> math.factorial(10)
% C2 g1 S' D+ b - 3628800
复制代码 % I/ J- _& `( D# A
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! b& d' r, e7 j9 i- #得到x/y的余数,其值是一个浮点数$ u7 d8 G6 P7 `. l7 U
- fmod(x, y)
6 D: E' b9 G* y8 K: B- V - Return fmod(x, y), according to platform C. x % y may differ.
+ Q+ B8 v5 {, R+ G+ I) i' M' U9 _4 _ - >>> math.fmod(20,3)
7 d0 @8 S/ ?4 A, N# u L - 2.0
! _: g5 P% r y# B* O - >>> math.fmod(20,7)
3 Y* C$ a4 D1 h6 s- p! H - 6.0
复制代码 ( S$ h9 y% e" \+ ^1 a8 s
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) v6 U5 Y) e ~$ N. `- w
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; |4 D, Q/ Y6 X( x% f
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
4 i$ q c1 Z9 c$ r# j0 @( L9 K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
& B9 k: ~& k" h# q0 J( E- O - frexp(x)
) K* v- w, X" t; P7 R" s; F: t - Return the mantissa and exponent of x, as pair (m, e).- B+ Z8 Y- Q2 a- b: y4 R
- m is a float and e is an int, such that x = m * 2.**e.% g3 c7 S; l2 c+ |4 }; |! l5 e
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.( x9 e0 w4 R7 T0 w, }5 W* i3 F( {; G
- >>> math.frexp(10)
9 }5 v( }! E* m; `; K# D% w - (0.625, 4)$ d& z' E6 R1 E- L" A& W% Z
- >>> math.frexp(75)3 R$ L v0 ~; ^# D
- (0.5859375, 7)6 ^' X+ C, W( L _2 f* E
- >>> math.frexp(-40)6 p6 t3 \* \/ n0 f& t1 L5 b
- (-0.625, 6) ]9 s6 N" g; i- S4 C a+ }
- >>> math.frexp(-100): j. L. M6 `, P( `4 o
- (-0.78125, 7)/ [# M h8 n* I4 }' {& v
- >>> math.frexp(100)7 Z2 W( j/ m* o) G
- (0.78125, 7)
复制代码 # t: ]) ^. ]' h! |
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), F& F: d9 g% G! V
- #对迭代器里的每个元素进行求和操作; n- x: o, a9 f# J7 I3 U# y4 `
- fsum(iterable)
! Y7 w3 E+ F/ b$ F$ [% r* x - Return an accurate floating point sum of values in the iterable., N% P |, A) `( |1 l7 o
- Assumes IEEE-754 floating point arithmetic.6 L' T& r5 {; w+ y/ l) S( S' d. ~6 n6 f
- >>> math.fsum([1,2,3,4])9 q6 E4 }6 V5 k; B+ {9 |0 b' U; \
- 10.0
, ?! l9 l/ y* r - >>> math.fsum((1,2,3,4))
4 l2 @; W0 K/ U) ?$ c# s f; O4 [ - 10.08 y+ P0 S, Y/ M# a) |7 O
- >>> math.fsum((-1,-2,-3,-4))( {% L8 N5 ~4 N( N% [, [7 P4 i& W5 e
- -10.0
" m9 ^/ e( l6 U8 p3 N5 e# i - >>> math.fsum([-1,-2,-3,-4])8 N2 C3 B! |+ i- D% l
- -10.0
复制代码 ; [4 Y5 ?; V7 b9 `
math.gcd(x,y) 返回x和y的最大公约数
6 }; D# ^1 n( x2 c# J- #返回x和y的最大公约数
" n; T& B$ U4 F7 Q! R - gcd(x, y) -> int- h9 b3 V; P S- v% l2 d
- greatest common divisor of x and y$ O3 X W, |# {) V5 z
- >>> math.gcd(8,6)* ?9 j- n! Q) R; [( X* x. t- q
- 2
+ ?0 v2 d, d/ }, v - >>> math.gcd(40,20)
# }7 K1 Q5 p: A1 R - 20
7 o. E' A( J! F) _* `5 p - >>> math.gcd(8,12)- ^; @. R, b8 s7 T
- 4
复制代码
' p- R& P! M( Y6 Tmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
# o7 ~$ F1 x: F3 I9 G: T- #得到(x**2+y**2),平方的值
+ {' b1 t# o7 F- l. S2 Y# z3 w - hypot(x, y)) x) k8 M0 \' I0 N- Y5 V+ m! |
- Return the Euclidean distance, sqrt(x*x + y*y).
( O9 Q" Y* E1 ] R8 k4 K* J* ~ - >>> math.hypot(3,4)
$ s& Z( ~. U2 E! ^2 h4 A/ Y- T* A- Y/ m - 5.07 n! z( G, f- r5 C! O+ s0 L
- >>> math.hypot(6,8)% Z( F+ Z) O+ L2 B
- 10.0
复制代码
, ~6 j; r( F0 cmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
1 D) S8 { Z5 i2 X' a- #如果x是不是无穷大的数字,则返回True,否则返回False8 l- R+ K! J/ k9 m- i. o3 U7 e
- isfinite(x) -> bool
& F$ g+ T) |/ _; ~ - Return True if x is neither an infinity nor a NaN, and False otherwise.; e5 E9 Z. O a2 M* D4 d; H
- >>> math.isfinite(100)
" m2 m9 T1 W1 r3 T - True
% {8 X9 e1 m6 ^' t% e7 S) F5 D - >>> math.isfinite(0)
. g' [6 p& p+ z _5 C- y - True3 e1 f* ^: R* z6 ^
- >>> math.isfinite(0.1)$ U: ~6 F: w% Y& q7 C# j6 E
- True
9 P2 s3 p0 E" Z0 u - >>> math.isfinite("a")
/ W" k1 T, h$ Y0 \; V2 O4 r' w) i - >>> math.isfinite(0.0001)" l: K0 z" g/ u; J4 N; M3 m
- True
复制代码 , p7 b/ O/ z7 _2 Z, i
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
6 P4 p! T% t) ^* W# q- \- #如果x是正无穷大或负无穷大,则返回True,否则返回False
3 b; `7 [' H* p! @" w }- z6 Q8 y; }& _ - isinf(x) -> bool
! {; ?$ ^& P ~# [8 _- W- I5 _! l- Z - Return True if x is a positive or negative infinity, and False otherwise.
# r2 x) X) O- ` - >>> math.isinf(234); v" s7 [$ H& z9 n0 i
- False8 N- k# ]- n3 n7 w2 m" ^
- >>> math.isinf(0.1)
( G7 G( e9 Q) `5 \" W6 p' o0 A, j9 r - False
复制代码 3 a& A; r2 i' s9 |" Y; P9 U
math.isnan(x) 如果x不是数字True,否则返回False2 n# P4 u7 h. h! G5 g$ k7 q
- #如果x不是数字True,否则返回False
( v N5 [ D) N, l - isnan(x) -> bool
3 L- O' @6 g [, r, B0 X* |4 j& U - Return True if x is a NaN (not a number), and False otherwise.6 Q6 ^$ b& Z( `7 Y, Y
- >>> math.isnan(23)
3 H) l2 Q6 M! N$ O( V5 _ - False' O5 I" ^4 Z: B. ?4 F
- >>> math.isnan(0.01)
% }9 x& M; g* B4 d% D* y7 U - False
复制代码
b8 J& ~* P# e9 y- smath.ldexp(x,i) 返回x*(2**i)的值
; r6 l6 l7 n0 C* x" o- #返回x*(2**i)的值; A$ {/ o8 m O
- ldexp(x, i)
% B) O9 n: N9 l# H" L - Return x * (2**i).
, g7 M0 S2 }3 [ - >>> math.ldexp(5,5)0 Z5 C# `$ w2 d2 b0 p
- 160.0
1 ^3 E/ t" |4 k; d: @$ ~0 ]# T - >>> math.ldexp(3,5)
5 P8 z6 |/ L1 w2 P& J - 96.0
复制代码 ! X! B7 d D& e* a6 }) C
math.log10(x) 返回x的以10为底的对数
2 s k, u& g' \! [3 e+ V* i- #返回x的以10为底的对数
/ ^6 e F. p$ m - log10(x): W: P) d; F' H1 v) M% A4 c
- Return the base 10 logarithm of x.
8 O* @3 m5 F3 x - >>> math.log10(10)7 F4 N/ I1 R0 K0 g5 `5 F
- 1.0
0 p3 i1 x- i: Z# D+ q+ E4 d - >>> math.log10(100)5 `8 z. Y3 A* X. V; a3 S+ d
- 2.01 O/ F4 e( }, ~ X
- #即10的1.3次方的结果为20
# d; b0 o- T/ }+ }# T5 a' u5 ^ - >>> math.log10(20)
7 m2 d7 J% J+ R4 f; [6 ^1 r" x5 Z) A - 1.3010299956639813
复制代码
) b+ Q* n0 ^8 S9 R3 R# V, q5 tmath.log1p(x) 返回x+1的自然对数(基数为e)的值
. o2 X/ n! Z0 e9 }. H/ ] ^3 f. l- #返回x+1的自然对数(基数为e)的值
2 e/ ~8 \; }- ] - log1p(x)
0 E& @/ P' T4 R - Return the natural logarithm of 1+x (base e).
. \/ |. {" W; \5 Y0 W+ I2 j - The result is computed in a way which is accurate for x near zero.) W% ^# t* n& T) T H( u( q9 Y/ Y+ P
- >>> math.log(10)1 k) E% t* a2 a( X8 |4 ^
- 2.302585092994046
0 i3 H/ e) i( P3 W& }6 k2 x - >>> math.log1p(10)
7 V- Z% }8 T5 z3 `8 P1 V - 2.39789527279837072 o, d4 e; s* C9 L8 l
- >>> math.log(11): Z& V5 @3 I, p; b' P- e
- 2.3978952727983707
复制代码 7 W9 u" N6 K3 g
math.log2(x) 返回x的基2对数, J4 y) q, ?5 u$ C4 Y+ Q4 r( s
- #返回x的基2对数
: B# u3 z: {$ u0 f' } - log2(x)- \4 ~1 ]! A! E( e* M& P
- Return the base 2 logarithm of x.7 |5 t! E6 U1 P5 C, k
- >>> math.log2(32)
% ^* f+ J( M) h/ O3 W: P - 5.0
5 t o' W, ?: L- C) V% X - >>> math.log2(20)
- G8 e0 t( f7 g% o - 4.321928094887363% T/ p5 v% V. I6 m1 {- x1 q9 I
- >>> math.log2(16)1 N" z: T# J* D, H1 v/ T
- 4.0
复制代码
0 J' \! s) M3 }7 ?& ]math.modf(x) 返回由x的小数部分和整数部分组成的元组
5 X6 q2 y" ^$ M2 j3 d( c0 c- #返回由x的小数部分和整数部分组成的元组
7 z% j4 y* g) k - modf(x)) I1 P$ M* W8 N- e0 c2 _
- Return the fractional and integer parts of x. Both results carry the sign& F9 l' E. `) c* v0 r3 [
- of x and are floats.
2 S3 ]: w) ^6 ^" h& T" U/ M. m: z - >>> math.modf(math.pi)
3 _+ W6 O2 {, t7 K" d - (0.14159265358979312, 3.0)" b3 z2 i+ B' k
- >>> math.modf(12.34)
4 r9 }. S9 v7 v: j - (0.33999999999999986, 12.0)
复制代码
" L0 b9 S) j# p8 _! Q8 qmath.sqrt(x) 求x的平方根' O- Q9 [5 M7 T/ Y# f
- #求x的平方根
% ]: \- y- D7 l - sqrt(x)4 ~( V# d0 E% j% X5 X" ]1 ?
- Return the square root of x.+ G) B" A4 ^; L* r" p& _) ^
- >>> math.sqrt(100)0 @; I! f" f# H
- 10.0" K |0 D6 b5 j
- >>> math.sqrt(16)) \) H1 i# i$ n: R. ?$ b" s
- 4.02 ?3 V3 }1 I9 c/ g n4 u* Y
- >>> math.sqrt(20)
) g9 U' K/ _& V8 } ` - 4.47213595499958
复制代码 {$ `9 S! V$ G( u
math.trunc(x) 返回x的整数部分- #返回x的整数部分, m7 V+ Z9 f' E! h7 o, O
- trunc(x:Real) -> Integral3 U, f* `8 j5 V( Z
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
0 z5 d6 F' N& ^8 m; q - >>> math.trunc(6.789)
* @0 |2 O: _, M* r# x2 s - 6
# P0 o8 U" T! t5 S+ S/ v9 Y5 H. O - >>> math.trunc(math.pi)7 b( e& k) J3 G5 }
- 3. w3 f' o5 N+ i7 \+ d, g
- >>> math.trunc(2.567)7 @5 ^2 q: y" L. b
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|