马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ r6 o" A* P; F* Q. Z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。4 h) \- M# X* z0 n, @, b& T
) l" _, l9 T0 a3 d9 Y. j$ @
方法1:
* t4 S) M0 p/ ?/ w9 l# \- >>> import math
/ G3 V5 a' L$ r+ e+ X, ` - >>> math.sqrt(9)
o( v# E9 d; X5 @ - 3.0
复制代码 方法2:
* f' B) R0 b8 }/ r9 X- >>> from math import sqrt* S( I @6 S, i5 W; y7 K: ~
- >>> sqrt(9). P3 Q# { _. g+ A) t) x6 \' Z3 q
- 3.0
复制代码 & f7 u) |) n& u1 P" f; `
7 l, ^ n/ P( Dmath.e 表示一个常量. O: x! T1 B3 W0 p. i
- #表示一个常量
6 S% ~# R% G/ M+ d6 B - >>> math.e. T$ ]# k3 h* a) r
- 2.718281828459045
复制代码
: a1 ~- L- J4 [! Tmath.pi 数字常量,圆周率; z% z0 O" l; E3 \1 e" W( P
- #数字常量,圆周率
) ]% U J, B: ^" y# @ - >>> print(math.pi)7 _# C' b2 A1 ?& B9 B
- 3.141592653589793
复制代码 - L/ c) l/ I. C8 T1 x% Y4 F
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 d4 l* s0 G. r: M- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 C, }( W/ E, O9 o2 w$ d - ceil(x)
$ o. O5 |% z) Q - Return the ceiling of x as an int.
H! B. O7 g* A4 s6 d - This is the smallest integral value >= x.
6 \" {7 t5 F7 H+ _0 k3 o7 K2 ]; F: x
5 X' w% G; I# `3 `- O6 H/ A* `# [- >>> math.ceil(4.01)1 F9 z/ A2 n0 P
- 51 Z+ @. w5 X8 M( {+ i" m. M
- >>> math.ceil(4.99)2 j1 j; q6 S+ I# p
- 54 r& s+ X7 [8 L: E9 j
- >>> math.ceil(-3.99)
# X5 G+ u* ^ z7 R - -3
$ R( x: L/ c) q1 E2 [4 P. P - >>> math.ceil(-3.01)
6 c9 P: }' E( P( D$ ~% W - -3
复制代码 + I6 E6 C) D0 Z* A8 l( M
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ E7 Y2 p6 ~& Z/ t- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" a- s" A& R5 ?, G2 W - floor(x)' I7 l: ?- u" V* t5 c1 \
- Return the floor of x as an int.
& _7 Y; N* [0 s0 C& M - This is the largest integral value <= x.8 _3 U* c1 n# M' k, g
- >>> math.floor(4.1)( t1 l) S. k8 @$ l! [5 M
- 4& h8 j+ {; |; U+ y
- >>> math.floor(4.999)% O) X& K* @6 g. p! h' d6 W% }" i
- 4
: m7 U0 W/ d5 q+ b' ?, N- G* C6 v3 r - >>> math.floor(-4.999)+ ?! {* l* {9 |3 J5 u4 A
- -5
6 L/ _. G; n0 b9 @6 L - >>> math.floor(-4.01)
) Z1 Q% p3 y3 ]- C& ~* I - -5
复制代码 . E8 W' R( u& P! i& J
math.pow(x,y) 返回x的y次方,即x**y
' q7 I V2 i" ^. V7 A" s7 p1 N- #返回x的y次方,即x**y
^- U, U- L) T: z3 J$ S - pow(x, y)
. s# ?0 a& B: X1 c0 J+ x) B - Return x**y (x to the power of y).
- D+ `' E* _! L1 h- i- T, B4 ^ - >>> math.pow(3,4)
: K/ g/ X2 X! d& F I - 81.0
# \! }) v2 A: H1 l$ @1 J- Q - >>>
9 Q) J8 w" F0 B7 J - >>> math.pow(2,7)
( B8 w9 v3 d6 E& C - 128.0
复制代码 ' h' n3 a7 s% I
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); }' Q) J' R% r, f" K9 D) e5 T8 x
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 `& a: t" U/ m# n" u
- log(x[, base])
) u* F3 d/ E- R) v( B( h - Return the logarithm of x to the given base.% \; }" f; l8 P! u+ A/ p4 p, N
- If the base not specified, returns the natural logarithm (base e) of x.
/ T% \$ G# E2 Y+ m - >>> math.log(10)8 d# { }* c% M# a8 t
- 2.302585092994046 p3 j' O/ u- i! g' x
- >>> math.log(11)$ O7 ?5 Z5 y: R# v X* h6 ^$ o( ?2 e
- 2.39789527279837079 m! ^) P9 \) V* d$ x; i
- >>> math.log(20)( ]+ \/ b, U/ d, c) V" f% M
- 2.995732273553991
复制代码
" B4 t5 Y* i) }$ {; Lmath.sin(x) 求x(x为弧度)的正弦值
, A) W3 P* g! w9 |6 l8 C6 \- #求x(x为弧度)的正弦值
* E- c; I* N/ C# C }- m. ] - sin(x)
6 ^( w6 w' ^/ ^8 K0 l$ p- I - Return the sine of x (measured in radians).1 }' g1 q! T2 O$ T
- >>> math.sin(math.pi/4)
4 J3 X9 i* v% N; a$ R - 0.7071067811865475
2 _& B" z. W4 H" ?) F( k - >>> math.sin(math.pi/2)4 q% @/ Q3 d( X7 d% ?2 b, ~
- 1.0
2 ?: k6 `6 `) c* l3 H2 s7 L, t - >>> math.sin(math.pi/3)! Z# C! x" P4 d7 V2 x# c. g Y `! I
- 0.8660254037844386
复制代码 5 {4 \: [9 Y0 q) A+ h9 `
math.cos(x) 求x的余弦,x必须是弧度8 H* G5 \1 ^# X3 s
- #求x的余弦,x必须是弧度; Z0 p' B( T0 P7 j
- cos(x)- D7 u* \: M7 B
- Return the cosine of x (measured in radians).
0 `- [8 h7 E6 p) \; |$ c8 @4 q$ c5 \ - #math.pi/4表示弧度,转换成角度为45度
5 h' H8 v' C/ @2 `# ]) ]1 D# Y/ f# v1 Z - >>> math.cos(math.pi/4)
; l7 z" i, I' W% e3 s$ f+ S+ \! |% E, h& r - 0.7071067811865476
1 p: _+ D% J/ j. S. T9 @2 p5 C% S - math.pi/3表示弧度,转换成角度为60度
2 ?( ^/ ?" P0 X) u/ q4 j v; |, U+ F, n - >>> math.cos(math.pi/3)
% e6 ]1 f5 }% m8 T8 j' | - 0.5000000000000001
3 l. O# E" J2 q1 T2 x3 o# A6 h7 B - math.pi/6表示弧度,转换成角度为30度
/ S* ^, t" O! h0 L& F, m% G - >>> math.cos(math.pi/6)
3 r8 X# F4 s+ @( q% w- O2 P - 0.8660254037844387
复制代码
2 k+ W2 o/ d( a6 C% _math.tan(x) 返回x(x为弧度)的正切值* U2 \8 m6 L9 n
- #返回x(x为弧度)的正切值/ E. D( s9 m/ i/ y$ Q6 c/ g) P7 k- p) O
- tan(x)" N' k1 F+ Y+ r8 ]: `
- Return the tangent of x (measured in radians).
( ?2 \& K$ h- E! I - >>> math.tan(math.pi/4)7 q. h# V& f) q1 \
- 0.9999999999999999- w3 I: K2 `6 \( X
- >>> math.tan(math.pi/6)
3 e1 e( E: Q! g% u# g5 Z- \9 H - 0.5773502691896257) r/ i$ {+ O1 E9 O
- >>> math.tan(math.pi/3)+ n8 I- D) L- ]7 b
- 1.7320508075688767
复制代码
+ h: k& c& [8 p: g$ m) a) p" Rmath.degrees(x) 把x从弧度转换成角度) ^+ y0 ^, G/ I* a
- #把x从弧度转换成角度, `; F1 f" k0 M5 q
- degrees(x)
. F Z2 v c. z( }, F9 f - Convert angle x from radians to degrees.
6 d8 _ ~- s0 ~3 w
0 m6 }* H+ e1 Q/ u7 z8 ~- >>> math.degrees(math.pi/4)
# ?' `. Z/ Q- @" \+ { - 45.0
! }$ o, |" \* g& v; M - >>> math.degrees(math.pi)
, S; }/ V$ H! g4 N6 q7 o - 180.0
9 k- {$ Q8 w% `1 C3 _ - >>> math.degrees(math.pi/6)$ |- u) P D2 k/ c$ R- ~
- 29.999999999999996
/ y& Z1 l. H( c) e* O - >>> math.degrees(math.pi/3)
6 n. |2 p. `/ [3 R - 59.99999999999999
复制代码
# }2 K0 a5 ~6 B0 P1 @6 [8 u$ cmath.radians(x) 把角度x转换成弧度' {0 ]' J9 j6 k0 O
- #把角度x转换成弧度. U7 @( b7 k' \' r5 s
- radians(x)
8 x+ {* D* P2 J" H6 J6 @ - Convert angle x from degrees to radians.
$ ]1 Q$ R- T# m# c$ }4 Z5 ` - >>> math.radians(45)2 ^2 _% u" D( o+ ]. w3 x. K
- 0.78539816339744835 O9 f4 T _9 G* {2 u A/ D
- >>> math.radians(60)" Y3 }# L0 y! c8 B: t+ s6 q% a
- 1.0471975511965976
复制代码 2 ^; o% A, l! V- {& D( Y# z
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
+ W7 c$ |& i) w% p- #把y的正负号加到x前面,可以使用0+ G' o5 d- x& u E$ d3 ^ ]
- copysign(x, y)# y0 g+ U2 N* [* {% O2 J" N2 T: R
- Return a float with the magnitude (absolute value) of x but the sign - a0 ^. E) ~ Z( I: K6 p
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ; N8 E" j) X% z6 n( O
- returns -1.0.
9 ?8 B \; h+ c - 6 V' t1 K: J4 O9 E5 N
- >>> math.copysign(2,3)1 o" }+ {0 @8 |- H5 c% T
- 2.0
4 ?9 A0 R; K' T4 B9 k% b - >>> math.copysign(2,-3)6 I l! N. D6 F# I b# g
- -2.0, T6 P) _' E4 _" N2 g
- >>> math.copysign(3,8)! W" D2 M0 @+ M5 j* _1 T
- 3.0
2 I: D& B5 E$ @- Q: k7 T3 F# y5 Z7 e - >>> math.copysign(3,-8)
/ X( d, v) O' A+ M8 S$ R - -3.0
复制代码 . p" Z! ~# U% T1 l6 C. Z4 K4 ` n
math.exp(x) 返回math.e,也就是2.71828的x次方- z! y9 j2 t' S2 a1 M" j6 w
- #返回math.e,也就是2.71828的x次方% y3 e, Y! i) e. \$ W7 L
- exp(x)
6 F" Z" d; R. o2 l0 n4 B/ F - Return e raised to the power of x.
2 \( J% p* Z4 N& s( C" V; n3 `/ M3 B - . ]/ E( b0 |9 t
- >>> math.exp(1)
) ?& T: D/ l( ^' `! N9 \- X - 2.718281828459045
* V L5 q6 N" i( x5 T: G6 ~ - >>> math.exp(2)
$ f( D! ^8 m, x+ C! ~5 O - 7.38905609893065
5 x+ X$ t3 U e) N0 ~ - >>> math.exp(3)
3 U1 F8 k- p l7 W - 20.085536923187668
复制代码
, c3 y8 y9 g; `9 `" T& zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1$ R w) P8 X3 z. ~8 D2 e# V
- #返回math.e的x(其值为2.71828)次方的值减19 o; z( Q; G- W- v/ V% o. h
- expm1(x)
. s$ o; [ s6 }9 G: g7 d - Return exp(x)-1.
9 v5 L1 R7 E. j. L - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
+ I2 b8 T" s, ]/ {- F
J; k' H9 `8 F- P6 R/ C5 y- >>> math.expm1(1)2 {: q5 Y( }' p# [8 N2 n7 v) ^ f
- 1.718281828459045" f2 Y) E9 O* h. D; G5 J6 D
- >>> math.expm1(2)) x+ @* c" ~3 z1 D; _5 L( A4 F
- 6.38905609893065
) V9 J6 m( L) Z' ^7 g e - >>> math.expm1(3)
) | w6 B, @9 p/ D1 ? - 19.085536923187668
复制代码
* Q& F% {- I! _6 C6 s& dmath.fabs(x) 返回x的绝对值
# n4 ~; G" W1 l% ~7 ]- #返回x的绝对值
- D1 V7 F. ] v. N+ n - fabs(x)
3 `8 F! K' [& Z9 e - Return the absolute value of the float x.$ {( B6 E* @% M$ {
. S- d8 C( c) ~5 S( m- K2 P- >>> math.fabs(-0.003)
% s+ W1 A7 _, [$ U$ ^# h, c - 0.0037 o3 w/ }# W' R" q
- >>> math.fabs(-110) {& n. L3 \! f% b. z1 [/ O5 y& z
- 110.0 m% h$ B4 s* }
- >>> math.fabs(100)# @" \3 a/ l* k7 h6 Z- n; N
- 100.0
复制代码
' q9 d% L6 K: e! y/ rmath.factorial(x) 取x的阶乘的值! b( O5 m4 d5 n% Z% J: S
- #取x的阶乘的值
( x+ g: B% R8 b- n$ t4 O) g1 z: o - factorial(x) -> Integral% b+ v1 p8 `: D: `% F+ B
- Find x!. Raise a ValueError if x is negative or non-integral.9 S l2 ^7 ~' \8 ?) v1 A
- >>> math.factorial(1)
4 ~. h7 w) ?/ B* X P9 i- n, R! g - 1
; S/ X8 }& @) x' U1 X" \ - >>> math.factorial(2)
8 K0 Z2 i3 u; I- h# K; Q - 2
9 d3 k- a4 g; q; F( Y - >>> math.factorial(3), J" t7 ]/ D) |, n
- 6
* Y% M# L6 M; H8 L0 ^6 G - >>> math.factorial(5)
0 G1 L k4 U( T9 f; j9 Q0 H - 120
' O& a6 |- h1 o# M" Y0 |1 l - >>> math.factorial(10)2 o* ?; S/ a1 f; M: F/ c
- 3628800
复制代码
* A! g1 c% D) j) h2 s/ Zmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数$ ~4 a% f U8 b, }( D: u
- #得到x/y的余数,其值是一个浮点数2 y+ s) d. p5 {& p& A
- fmod(x, y)& c9 `9 {6 j6 Z4 u1 p! Y2 J
- Return fmod(x, y), according to platform C. x % y may differ.
# h3 W1 Z5 j/ Q) a, Y - >>> math.fmod(20,3)
5 D) m& F* Q- _& D+ t# L - 2.0( E! B- y" k# I! |
- >>> math.fmod(20,7)
2 c, u, V' o! d ?2 T$ | - 6.0
复制代码
5 g( l* c& U/ r, }math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 d; t! m. S8 @) @& x
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 }- d8 G8 E2 O3 T* | - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 Q* e$ D% k" G; M- o% x. j
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! _& s" Q& B7 U$ {6 @
- frexp(x)/ G7 e1 `8 D1 B) S: Y# q# ?) e
- Return the mantissa and exponent of x, as pair (m, e).
" k$ c" q% U, n3 p( m+ \/ t - m is a float and e is an int, such that x = m * 2.**e.
, [# w, z! l' }( ?/ ~( D - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
9 f4 l, n t' `) Y% o. I - >>> math.frexp(10)
" d; H- c1 O+ @. ^ - (0.625, 4): I1 P1 f' C8 o$ Y
- >>> math.frexp(75)0 k5 P2 ?% H& B* w4 _ ^
- (0.5859375, 7). w8 x2 K) Q. T) C1 B
- >>> math.frexp(-40)
* e5 ~. x7 s/ ~+ @( C: r - (-0.625, 6); X& r! q1 e1 G
- >>> math.frexp(-100)
! D4 z- g L) G: z2 s( Y, |: k7 ^ - (-0.78125, 7), R# A$ r2 n; a
- >>> math.frexp(100)* U0 L& q* W5 O' T
- (0.78125, 7)
复制代码 - Q% M) \2 r2 D& z) _6 i" Z! M n
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)3 f7 V7 @8 v/ D; V2 p
- #对迭代器里的每个元素进行求和操作" n4 _: O t g$ E
- fsum(iterable)( N8 u5 t& K! Z \ C
- Return an accurate floating point sum of values in the iterable.+ `; J! w6 I7 p
- Assumes IEEE-754 floating point arithmetic.
) ^) O: C* [% i: v5 `: ~- ~ - >>> math.fsum([1,2,3,4])- W1 {/ T, H4 ?
- 10.05 T# d( ?0 _/ W0 b
- >>> math.fsum((1,2,3,4))$ ^5 ?/ s# ?$ n0 K" j
- 10.02 E4 j# p% `9 C9 [; s3 U
- >>> math.fsum((-1,-2,-3,-4))" F! o2 J. }6 e$ I
- -10.0 |, B- f8 k3 {' X# I
- >>> math.fsum([-1,-2,-3,-4])
2 g) X/ r" y3 @8 i6 g, E - -10.0
复制代码 4 Y" P) N" q* P# B; J. m/ X
math.gcd(x,y) 返回x和y的最大公约数5 h* ~/ U1 o6 f* q% b3 W" b
- #返回x和y的最大公约数3 \% J$ }3 V: \' u4 H3 J
- gcd(x, y) -> int7 {2 e, B8 p6 }; k$ a9 l5 D
- greatest common divisor of x and y# c' `6 H. {" `8 Q9 p) Q
- >>> math.gcd(8,6)
( a$ z; R! `! j7 v$ m0 `; U9 R - 29 f3 R2 D9 M, r: Z. H
- >>> math.gcd(40,20)
; O/ I+ C& d/ q7 {' D5 i - 20
4 O# C* M! g5 C& I( |; Y! ^6 I7 Q - >>> math.gcd(8,12)2 n# s9 G' P- j
- 4
复制代码 1 d+ [, \) ^3 K1 f6 _; E. I7 r, k
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 }! V9 Q7 j0 W- ?1 Q- #得到(x**2+y**2),平方的值 w% l A# w; ]' X. e
- hypot(x, y)
: k; h: n2 r5 w* p# u; j - Return the Euclidean distance, sqrt(x*x + y*y).- d7 o! [8 K) ?+ [) m9 q- C5 \
- >>> math.hypot(3,4)
' q% T: A; t: S( c' S& _ - 5.06 u$ u/ w, d- y C2 b; L
- >>> math.hypot(6,8)" I7 P+ \8 x O
- 10.0
复制代码
/ f( a# a+ m( O- T! c; Mmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
$ N7 H6 r5 d, \2 {- #如果x是不是无穷大的数字,则返回True,否则返回False
' O7 ^# m" H- x8 y8 z - isfinite(x) -> bool8 t2 V7 O7 n l! w6 j9 `4 P
- Return True if x is neither an infinity nor a NaN, and False otherwise.3 s1 E/ H& R9 T% a8 J& U- g: L
- >>> math.isfinite(100)
& u# R) \+ m5 w& [8 N! y1 ] - True
+ ^- T3 E" Q8 K9 V; l* |! z - >>> math.isfinite(0)
d* B( J4 H# m! \7 j' M$ ]! b - True1 y: Z; d! a( ]8 ^; z' X" e% j+ X. H
- >>> math.isfinite(0.1). z( }: `7 N- Q7 w/ V" a# S, o5 H
- True
6 s6 W% S& n, l- |! \ - >>> math.isfinite("a")
' i8 E4 S+ G+ p* x3 |5 T- \ - >>> math.isfinite(0.0001)
* ~0 i3 M Q6 V. | - True
复制代码
! @' J; S) i" z5 I. s( wmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! s4 _, |. { ~- ?0 w3 A- #如果x是正无穷大或负无穷大,则返回True,否则返回False1 T. c8 b- [% |% O6 y# A
- isinf(x) -> bool- I( w6 ]( S1 Q: P# {
- Return True if x is a positive or negative infinity, and False otherwise.
4 T, K5 C+ W. [1 r: b" } - >>> math.isinf(234)
4 m' P. z" I M4 Y! H: O" n - False/ m/ b" s, D9 ~
- >>> math.isinf(0.1)
. Y+ ]' P0 A+ T' P, v3 t9 o - False
复制代码 - u* {4 J- B! R# x' x. p
math.isnan(x) 如果x不是数字True,否则返回False
2 p" n, h. s& O# O; u( V9 p! Z: {- #如果x不是数字True,否则返回False
: h# Z) ]' }* Q+ e0 Z6 B$ t - isnan(x) -> bool( y% P0 @* g" h9 j& H4 I
- Return True if x is a NaN (not a number), and False otherwise.
9 Y! L* g/ a' Q/ r3 i0 s - >>> math.isnan(23)3 B& Z( t3 J+ w" u) t& k
- False8 _' X5 V ^! X. H9 C2 b: }, |
- >>> math.isnan(0.01)1 w3 H( s: W( r' Y
- False
复制代码
. k2 h/ Z+ v' f# f' H) ^math.ldexp(x,i) 返回x*(2**i)的值
n# e y. X3 J) \4 m( ^6 _ }/ Y; P: ^ {- #返回x*(2**i)的值
5 ] P5 r. H0 M# r% F/ Y9 J - ldexp(x, i)
9 K! V# p6 E. J4 |- p - Return x * (2**i).
- I2 |; y, G: I. l* a7 A: B3 ] - >>> math.ldexp(5,5). [# d+ d: T9 H2 ^4 V
- 160.0; R8 X( A. N- a# U, ~
- >>> math.ldexp(3,5)
" i' l; Y1 |( L+ I4 ~( h9 V - 96.0
复制代码 $ W' @) X8 b$ R/ D2 O! O/ d
math.log10(x) 返回x的以10为底的对数
9 n: i( V$ ~8 m- #返回x的以10为底的对数
1 w- V7 ~6 @: z' \# } - log10(x)
- u( \, k+ E0 G. K7 Q3 O( K - Return the base 10 logarithm of x. D" H7 A" P" }# h( [
- >>> math.log10(10)
1 b8 p X& |& h- t - 1.0
8 I$ e4 c, W5 ?! ~: x6 q! n - >>> math.log10(100)
$ v p+ K# ^2 d1 ?! w - 2.0
* {5 s3 d$ n& E5 Z% E4 b: y - #即10的1.3次方的结果为20! k k) T; F; o# c8 D. x
- >>> math.log10(20)" h$ @: b6 ?9 ~
- 1.3010299956639813
复制代码
% y) Y; l$ J. \math.log1p(x) 返回x+1的自然对数(基数为e)的值
. i- s! g6 j/ V& W6 M* Z- #返回x+1的自然对数(基数为e)的值. P6 w5 V! m# @# T, {! H3 Z3 y
- log1p(x)
8 y' K K/ n5 e: Y- ^7 F - Return the natural logarithm of 1+x (base e).( w2 n0 z: x% ?" o3 w
- The result is computed in a way which is accurate for x near zero.
[% n" d2 ?# | - >>> math.log(10)
1 f+ D3 M" d$ a! `3 ^! }8 Y& ?, J7 z - 2.3025850929940465 ?4 U1 ]* {( \- K2 m( X" C
- >>> math.log1p(10)8 {4 a7 F# O1 v. V( q9 D u
- 2.3978952727983707
0 ~( R" p) @+ ]# }1 L - >>> math.log(11) c3 I K5 V' N" y
- 2.3978952727983707
复制代码
( K. @& O( U! T$ j% `7 Xmath.log2(x) 返回x的基2对数
7 E t# v9 a9 `# x1 {7 ]7 h, S- #返回x的基2对数
! o1 o0 `. ?4 X. W9 x4 z( ]' r - log2(x) b' ^3 I! I5 ], n& G/ R: h6 A
- Return the base 2 logarithm of x.
& g7 X' X: n3 ]! D; `% O, v - >>> math.log2(32)
0 f s! K6 {. w8 y6 ~, Q - 5.0, f! o. z6 _0 G! F& O
- >>> math.log2(20)/ I0 A. A' E" n
- 4.321928094887363
# v3 V' v i( Q - >>> math.log2(16)
2 e+ I2 U) e( _: v - 4.0
复制代码 ' W5 ~) R$ r, x) U
math.modf(x) 返回由x的小数部分和整数部分组成的元组3 F/ | _2 G7 s! g" H& v$ t( T
- #返回由x的小数部分和整数部分组成的元组! |3 u& L( ]. I. T. F
- modf(x)
: T5 y, A% I: u! P7 j! F8 |7 n - Return the fractional and integer parts of x. Both results carry the sign- t% a. A0 ~+ L8 o1 y h$ u
- of x and are floats.
( C1 I( t- P: D - >>> math.modf(math.pi), ]) Y$ O, w6 [
- (0.14159265358979312, 3.0)
8 V4 J- b a. P - >>> math.modf(12.34)
. ^" i7 t# C' B4 L& W6 Y - (0.33999999999999986, 12.0)
复制代码
! L$ y _, v5 g3 bmath.sqrt(x) 求x的平方根 b/ x& S$ `1 b: u
- #求x的平方根
. e0 z3 q9 t: Q* J - sqrt(x)
2 O, A& X( r4 E4 S9 Y - Return the square root of x.* A# W% P1 `3 N# z
- >>> math.sqrt(100)
4 K* r! F' F% }/ e7 j. M - 10.0
' a: ?# s. ]2 E( l - >>> math.sqrt(16)
; B- G D1 v) H8 a1 t2 _ - 4.0
! g& K! x, R* j - >>> math.sqrt(20): ~( l7 f. p. | C
- 4.47213595499958
复制代码
! x+ s8 T6 v" e6 G% X. ~math.trunc(x) 返回x的整数部分- #返回x的整数部分
, }8 R3 r1 U( `& u/ E& ^ - trunc(x:Real) -> Integral
1 S5 Q* A @, R+ n+ P. R$ G - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# F/ ?6 z# L( T5 m, a' P5 ` - >>> math.trunc(6.789)* ?* n6 e/ G; r. [4 Q$ [
- 6
! t% c/ `# E, U% X1 r - >>> math.trunc(math.pi)
0 E' i' Z# S. W - 3. H/ ~2 l$ i) J/ ?
- >>> math.trunc(2.567)
% w o. G7 U8 x9 k# J9 c. Y - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|