马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ Q5 n$ X0 y5 Z# {1 n4 V R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
6 [1 _. l5 _/ X; G, P) v3 g, F o$ T; _
方法1:
1 a8 \' T6 B/ i4 |, I- >>> import math2 F; T, M' D7 W5 j& b
- >>> math.sqrt(9) h) l- |/ ^) l8 x
- 3.0
复制代码 方法2:' p% O3 U3 r5 k3 O" f0 f! B* C7 z
- >>> from math import sqrt' q: y+ b. H( Q! E' y+ @
- >>> sqrt(9)3 {% m! n: X; z3 d
- 3.0
复制代码
u; p, z% M: Q3 Y W2 e
; x3 n; p6 Y) Y( I7 G mmath.e 表示一个常量2 v3 y. i1 e7 |/ F. V
- #表示一个常量$ [; a. G. F2 e/ R9 q
- >>> math.e9 `( V0 V$ I$ h( ^7 |5 T1 |
- 2.718281828459045
复制代码
8 \6 c! n* v: d% E# Jmath.pi 数字常量,圆周率$ F$ S9 @; Q0 P' b4 V; h+ w
- #数字常量,圆周率, I, s# C- v, n. L( h. }, P6 O
- >>> print(math.pi)
J, d" j8 P7 @, ~. u - 3.141592653589793
复制代码 / J3 e6 F2 E3 s# Z
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x$ G; U7 ?+ V; ?
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x$ C4 W4 O7 P! V" \4 K& ~# |
- ceil(x)7 T( u' O: e0 Z% H4 U; b8 F
- Return the ceiling of x as an int.+ W" b' }) ~9 V# n5 H/ {
- This is the smallest integral value >= x.8 H7 T: P) e" w# p) F5 x
- 5 ^8 p6 z* _0 K; H& b
- >>> math.ceil(4.01)/ z4 ?! w, O3 _
- 52 w$ G6 h* }. J/ h1 K" L' S! F
- >>> math.ceil(4.99)* q8 f8 K$ u6 t3 D
- 53 J; ]( ~$ n) h" ` Z" P! X) x
- >>> math.ceil(-3.99)- [/ w0 z+ i* {
- -3, L& o7 D2 b1 u/ \: \ q
- >>> math.ceil(-3.01)6 L: n' c& R, ]! Q+ E8 ~
- -3
复制代码
3 c8 p& V9 A5 D) R" r1 [6 Lmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 T0 _: Z* y. L4 u- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# ] V) b5 S0 [! Y8 x. h3 S
- floor(x)
$ A* J+ {! p3 b' | G - Return the floor of x as an int.
1 r6 m2 z$ y" v% S) m - This is the largest integral value <= x.( [0 V% B- S* L8 ]. ?
- >>> math.floor(4.1)9 P+ e% c$ H6 J; } ~; \
- 4 \# t7 ?( K0 ^8 V5 C& Y
- >>> math.floor(4.999)
a+ {1 h$ @7 Q. R: w2 t; s6 L - 4$ u [# P% K- ~' e# n; X6 _2 q
- >>> math.floor(-4.999)
4 L9 t* P: B; f/ J8 {: k8 }( [ - -5' b P' a, f, C# T) l
- >>> math.floor(-4.01)6 |# F- }( Y w9 X4 h# h3 R
- -5
复制代码
2 X3 r k+ f3 f; H7 n6 Bmath.pow(x,y) 返回x的y次方,即x**y
( T& t7 ?' w" X# N$ b8 z! m; i- #返回x的y次方,即x**y3 W( w4 m6 B( l2 [ i* @3 _
- pow(x, y) w% Q) R: U. v
- Return x**y (x to the power of y)., O9 O( ]6 `7 {3 \& q5 ]
- >>> math.pow(3,4)
- p5 f6 {) H' p: ~# k - 81.0
( t' I* S1 T, W" w: C) T9 R7 Y/ {6 f - >>> . l7 D* W2 d9 M: N
- >>> math.pow(2,7)3 w( r4 |) i! a, e- C+ d4 M" X8 h
- 128.0
复制代码 ; p! t- s' d1 }# L$ L+ f8 s
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 P% x5 E! F: W: [% z" J p
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). L9 J* a$ J: u( s
- log(x[, base])
6 I( S( W2 z2 O% J - Return the logarithm of x to the given base.' L2 m; q+ c7 i: P" i; ? y% Q9 `, \
- If the base not specified, returns the natural logarithm (base e) of x.( n1 y& W; R6 U: S6 p" W
- >>> math.log(10)) Q3 R) J( m) F1 _9 P: n
- 2.3025850929940461 o9 Q% W* T& k6 L: G9 @! m
- >>> math.log(11)
' {" W( r4 i6 s' c Y1 O! q - 2.3978952727983707
. K: d7 n# s/ F - >>> math.log(20)
3 z( T4 f; A% Q - 2.995732273553991
复制代码 # w; w; D) B9 J% P6 g: ?8 V
math.sin(x) 求x(x为弧度)的正弦值! ^ D; C+ J9 T
- #求x(x为弧度)的正弦值/ |) W) e; |" ]( g4 U- h
- sin(x)
! B5 i% Q7 z2 z# ?0 s - Return the sine of x (measured in radians).+ A7 g$ s/ o1 a3 Q2 C
- >>> math.sin(math.pi/4)% D0 e; }, P* a# G
- 0.7071067811865475
2 N- ^7 ]8 G( U; f; c9 {& u - >>> math.sin(math.pi/2)
, _0 {# Q$ Q4 S4 P; `+ h+ v$ f - 1.0
# k; x5 G+ v. a! @ - >>> math.sin(math.pi/3)
# T& n" }& N1 A# g3 J1 u4 v& i - 0.8660254037844386
复制代码 ) l" X5 M8 l' V
math.cos(x) 求x的余弦,x必须是弧度
# x- v' n! }+ e, [ O- #求x的余弦,x必须是弧度2 `; v+ u1 W% _
- cos(x)& a: j6 w. K3 |& u1 \% U
- Return the cosine of x (measured in radians).8 p) p! D0 b8 \! d1 ]3 y8 K( g
- #math.pi/4表示弧度,转换成角度为45度) M) e5 o; Z0 \- ]' f
- >>> math.cos(math.pi/4)* O1 f. t; ~3 b- i
- 0.7071067811865476
j4 _8 z9 S8 A- M4 C6 S% i) S+ D - math.pi/3表示弧度,转换成角度为60度
[' q; j, L9 Q2 {6 \1 u( d - >>> math.cos(math.pi/3)4 F- \: ?6 L7 e }8 x6 n0 w
- 0.5000000000000001; ?5 C/ D, B" ~3 {6 Z0 j& l' s' v
- math.pi/6表示弧度,转换成角度为30度
. |1 C7 T& T! o7 V' i3 a# {/ @+ a0 P - >>> math.cos(math.pi/6)+ \6 \2 m9 B$ A+ F( h
- 0.8660254037844387
复制代码
% c, `& N( z7 v" Y2 ^$ o! Nmath.tan(x) 返回x(x为弧度)的正切值
) J( x+ l( n: c8 h8 q- d4 j- #返回x(x为弧度)的正切值
* i) K3 L: |! h# O5 Q' A - tan(x)
+ z+ T! X8 X6 ]6 F* }$ O - Return the tangent of x (measured in radians).4 a5 f j! T. ], t- H0 F# y
- >>> math.tan(math.pi/4); G: _! ^) v: E; E7 S. \
- 0.99999999999999999 e. ?! _6 C4 x) g
- >>> math.tan(math.pi/6)
4 Y6 P; A' B! H/ \1 i8 u( \3 O - 0.57735026918962571 @$ z Y$ l2 J$ ^
- >>> math.tan(math.pi/3)
/ D ?6 {" g- |1 @6 X - 1.7320508075688767
复制代码 6 w G" j* q( E) R
math.degrees(x) 把x从弧度转换成角度8 P' G! |/ w1 D
- #把x从弧度转换成角度
f% B: V7 ~: O$ b - degrees(x)3 C+ }; E2 i' w* h7 G
- Convert angle x from radians to degrees.
3 w) w0 g7 `* W5 O; w, [/ f/ F
: H) [) S( h- K# v- >>> math.degrees(math.pi/4)
4 _0 i$ I" X# o2 ~* b$ B - 45.0
) P/ W) C6 N* e' q - >>> math.degrees(math.pi)& N1 E9 Q X) c I0 r
- 180.0
' ] t( p5 H$ D" U$ w - >>> math.degrees(math.pi/6)
- R; r: J- Z) e' I( |1 h( _6 \, y, M! m - 29.999999999999996
# q% s% g4 J" b/ m# ~7 h5 t5 u - >>> math.degrees(math.pi/3)
5 `, v) n; n' m$ H/ \6 A7 Y - 59.99999999999999
复制代码
9 I4 U& v# S' k8 U! Zmath.radians(x) 把角度x转换成弧度
5 d: e4 i) N: u- #把角度x转换成弧度! T+ a8 R& B% p* v
- radians(x)0 I* N0 Y& k; l6 f
- Convert angle x from degrees to radians.0 |7 b3 V P. z" s8 F% d0 f& E
- >>> math.radians(45)
V% g' t0 J* i a - 0.7853981633974483
9 X2 W# k! s. `# D5 |( Y, _ - >>> math.radians(60)- }( E6 u' o) i) w4 J. y3 G5 R
- 1.0471975511965976
复制代码
5 \4 L/ b" c1 umath.copysign(x,y) 把y的正负号加到x前面,可以使用0
6 u' K3 m3 X- W W) \2 K- #把y的正负号加到x前面,可以使用0
- b) \) q2 M. I2 M3 j( E - copysign(x, y)
& A+ `* j0 X9 k' W/ C, y: ` - Return a float with the magnitude (absolute value) of x but the sign 9 a @: F* ~8 e9 ^5 H
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & \9 O' O' a/ Z1 j8 K/ ^- }
- returns -1.0.
# C, j# C$ `$ ^0 \9 K
$ K; `' }# [) B- >>> math.copysign(2,3)
$ g0 V0 x: V+ Z( N - 2.09 H3 w4 W; h0 E$ F
- >>> math.copysign(2,-3)
! i: p, C& j3 Z9 a - -2.0/ X% u& N. S1 S/ J9 A
- >>> math.copysign(3,8)1 [ s8 ^" X$ Q3 }; ]1 P
- 3.04 G) Y) s' f; A1 f4 W
- >>> math.copysign(3,-8)) Z N k6 P' V3 O! V
- -3.0
复制代码
5 H4 V& A, J* Bmath.exp(x) 返回math.e,也就是2.71828的x次方
' a! b& D9 o" m) o- Y- ^- #返回math.e,也就是2.71828的x次方
/ @9 Z* ~* s- h' R/ X - exp(x)
1 x' ^. {/ W4 G$ o8 d9 d* n - Return e raised to the power of x.
" g8 ~' p. _9 F% T - $ A) P8 A. w( z
- >>> math.exp(1)- [+ p/ a/ e/ \& \- E
- 2.718281828459045# Z, t1 m" y2 M$ R
- >>> math.exp(2)
& G5 b, o% @ ` - 7.38905609893065
' [9 H- `* R' x1 n9 R0 d - >>> math.exp(3)
3 @# [0 B- k8 z2 A) r - 20.085536923187668
复制代码 3 a/ J# t' j, w ^% @1 S
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1, g; t, y# m" {( s
- #返回math.e的x(其值为2.71828)次方的值减14 }( |$ [$ f# g7 J, S
- expm1(x)
0 W' i/ u) L8 L& A, V9 U - Return exp(x)-1.1 C3 c2 p3 j: g+ L& ?& V
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." D# L. u6 l+ r5 n( y5 a! V; Y
5 c- x- T2 v' J. |- >>> math.expm1(1)+ V! f7 P$ k- G4 T& O: H
- 1.718281828459045
! z' \9 e/ e3 }' \" K. ^ p - >>> math.expm1(2)
* O# @* m# @. s9 N5 @' J - 6.38905609893065/ B( S- M' [3 U$ e
- >>> math.expm1(3)) Y6 v: B0 W, M7 B5 G$ o1 o
- 19.085536923187668
复制代码
- g+ |1 t# g& }' v+ hmath.fabs(x) 返回x的绝对值. H- R% @. _7 ]8 \5 u
- #返回x的绝对值5 G* A) x1 I( R1 z
- fabs(x)
7 ?0 i9 A' |1 q* P1 s - Return the absolute value of the float x.+ \! H; V6 M, T, M
, w# D# _. u2 n$ }/ I [- >>> math.fabs(-0.003)* o' ~+ o4 B% o$ N# o
- 0.003
9 `& F4 e) c; r/ a - >>> math.fabs(-110)7 H( _3 G; y, M, @- ]) r& U0 w
- 110.04 H0 B' @! @; X; S g; V5 O; a4 H
- >>> math.fabs(100); l; i3 m( V) h
- 100.0
复制代码
) ~6 i% A5 I" Z) h" X7 I L: H; y: zmath.factorial(x) 取x的阶乘的值
2 k4 ~ Q" g. l0 Q1 b. i- #取x的阶乘的值' ]" Y+ f0 I) e2 ]
- factorial(x) -> Integral* D/ }# _& j" F! w* F& p A
- Find x!. Raise a ValueError if x is negative or non-integral.
/ z% P( b6 Y% l) M ^ - >>> math.factorial(1)* A3 b* c5 o ~, m* _
- 1! d! D* J1 y. D# L
- >>> math.factorial(2)6 ^- [6 f* ]4 t* o
- 2
M' e- n; b+ R3 d - >>> math.factorial(3)
) o, V) L. a( V4 Q0 Y% x - 6
+ I! Q3 r8 e& ]' P( d9 @( a - >>> math.factorial(5)+ z4 M6 O2 E e% g( l
- 120# K2 u! t9 ^ z1 T5 {; e
- >>> math.factorial(10)4 b4 a7 r, Q4 P. |- b; @) W2 t3 ~
- 3628800
复制代码 _( ^% o/ Z2 ]$ W" Q- o
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
% ]5 r0 v( m8 t- #得到x/y的余数,其值是一个浮点数& e- i- I5 z9 f- i& \5 s& H0 l
- fmod(x, y)
1 B, \8 W( i* `; b. Y - Return fmod(x, y), according to platform C. x % y may differ.; D; V7 T5 A) C6 S$ @
- >>> math.fmod(20,3)6 y. H$ g2 Y# F6 y; v) {' p
- 2.0% L; f' Z0 } O' E. L3 b8 E% a
- >>> math.fmod(20,7)# Y `7 i2 U2 F
- 6.0
复制代码 9 s! I( o4 g$ {% Q1 a7 ]3 k
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 S8 X" Y. Z. m1 C
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* f' p( s' M9 a
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
5 t5 i6 {) D; ~+ B& X/ z, K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1# c" ^ }8 ~) N1 r" f6 \% o5 b
- frexp(x)
2 z( ~2 y `7 | C. W6 L1 W - Return the mantissa and exponent of x, as pair (m, e).& y4 ^3 U3 y* G4 \9 O' K' ^3 r, V+ E
- m is a float and e is an int, such that x = m * 2.**e.
+ D9 V0 h3 x: U# ] - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
9 V% _% \+ b8 F, }7 ~2 r0 \ - >>> math.frexp(10)
; V6 t6 @7 c# o7 W& Q5 ]) w6 D! L7 `7 g - (0.625, 4)2 O# Z' e" s3 L) V
- >>> math.frexp(75)/ q5 ]3 q1 l# w2 p' j: K* W1 c
- (0.5859375, 7)
) l. N. @' E' D! r - >>> math.frexp(-40)% p9 H6 A" X O- s: q
- (-0.625, 6): A6 m8 t6 C& T! b
- >>> math.frexp(-100)% s- W! [" G, N: J/ @4 [
- (-0.78125, 7), Y9 ~4 w! W+ N/ x6 \3 u; I/ \
- >>> math.frexp(100)
+ t! D6 y7 g9 P8 f - (0.78125, 7)
复制代码 ; A2 O1 K' t# C8 C/ g6 Z% G
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
" P6 k8 z7 v8 L( D5 l- #对迭代器里的每个元素进行求和操作4 ]8 X. L! m, p9 u: V7 @. l, I
- fsum(iterable)
0 A3 Q. F( D6 Y/ E. j0 a' ? - Return an accurate floating point sum of values in the iterable.
. G/ x5 P# p8 X0 |6 z - Assumes IEEE-754 floating point arithmetic.
8 z5 ~0 s+ H8 v7 g" O6 k1 g - >>> math.fsum([1,2,3,4])
& @: M! n" B, ?: m/ L+ r/ i - 10.0$ O% u3 K, c: J4 \& d/ B
- >>> math.fsum((1,2,3,4))6 ?- l$ N1 F1 \0 y& p! a& j
- 10.0( ~* p% h- n3 T. t
- >>> math.fsum((-1,-2,-3,-4))' N+ o+ W! R# {( S) O# a1 ^3 Y
- -10.0
3 O8 z# }, ^- |0 Q& O2 j/ Z! ?# j' t0 W - >>> math.fsum([-1,-2,-3,-4])$ A1 q5 G; P) ]0 W
- -10.0
复制代码
& g& R: t6 D% o- c2 }" Bmath.gcd(x,y) 返回x和y的最大公约数
, n. ]$ [* E6 m# B! X2 k5 \) m- #返回x和y的最大公约数
, v4 }% e/ v# R5 k - gcd(x, y) -> int' y% K7 n# d( l, @$ U% G* q2 y, T
- greatest common divisor of x and y
# x3 b( |7 `5 f2 K7 } - >>> math.gcd(8,6)- ^6 e8 v9 a* ?8 q3 s. r$ _' `* t% P
- 2
2 ?7 J! x9 P7 P. m - >>> math.gcd(40,20)7 A: g! M# e- P3 z
- 20. w# Z9 g$ g/ p/ J! ~
- >>> math.gcd(8,12)
, Q7 g. e4 q+ r. z. I9 x. G - 4
复制代码
) ?3 @" q; ?, b& Smath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False! M5 }: J& o% _) j7 Q, D# R/ P
- #得到(x**2+y**2),平方的值
' @# I |) {# c2 u" p - hypot(x, y)8 d9 F4 k2 m2 t4 d) q- J- L
- Return the Euclidean distance, sqrt(x*x + y*y).0 h6 M" c) I) c% f& Z
- >>> math.hypot(3,4)3 d* p `- p2 P! M7 d7 _
- 5.0. Y: A9 \( w# l6 X1 |. v
- >>> math.hypot(6,8), m% b1 ]% R# _! F4 F4 m
- 10.0
复制代码
2 u6 n9 R; _( |* g) {1 ^& F: D' `math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! e Y. A$ t5 ^* i- #如果x是不是无穷大的数字,则返回True,否则返回False
8 C8 v9 I- K5 E, W3 r - isfinite(x) -> bool
% Q: r) M% A- |3 e+ i - Return True if x is neither an infinity nor a NaN, and False otherwise.2 }: X8 f+ e* t* K7 L) _" x: G0 @" R. v
- >>> math.isfinite(100)
( O. K9 q. \8 C - True
* l- A" y! {5 r# c( `2 x: V1 l) S - >>> math.isfinite(0)
, J2 i+ O1 X7 }0 I2 C - True1 D( m' T, W: r2 W$ K0 t5 r: ?% u
- >>> math.isfinite(0.1)$ \) P" A# r% j3 v7 X
- True
: x# O4 F+ H) |$ i3 V0 K! z+ B - >>> math.isfinite("a")
( C+ p2 y: L* c7 M. u - >>> math.isfinite(0.0001)
# y8 U( T K5 O - True
复制代码
2 s, E2 V" F& C8 K# E7 [8 nmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
2 D# l3 f9 Y. D1 A# c- #如果x是正无穷大或负无穷大,则返回True,否则返回False
& Y1 a0 H2 }1 E. v0 K9 A - isinf(x) -> bool
3 [6 d9 o$ k, u6 d) |' E& b' j - Return True if x is a positive or negative infinity, and False otherwise.
0 Q8 H; Y+ Z+ g% A. z, U - >>> math.isinf(234)& M, } d7 H4 H
- False' v+ z" Z% j+ Z
- >>> math.isinf(0.1)! d9 Z0 l" M/ D a' {
- False
复制代码 & T; J7 t/ X1 C# x, k; X1 Z
math.isnan(x) 如果x不是数字True,否则返回False; g( s8 {4 x) P% `
- #如果x不是数字True,否则返回False
; H# s! X* P* E - isnan(x) -> bool
4 [# X) U8 q' H - Return True if x is a NaN (not a number), and False otherwise.
" p6 B$ H9 B/ [) h) L - >>> math.isnan(23) q% [5 t/ m: P( A2 ^. v% B
- False
# x5 d, |6 F7 F3 D8 E - >>> math.isnan(0.01)! o$ ]! h: c0 A3 @7 a
- False
复制代码 5 z J4 ]! G! d' p0 N3 s% ^
math.ldexp(x,i) 返回x*(2**i)的值9 H. ]" O, ^/ t. o w' f2 o
- #返回x*(2**i)的值2 u' Y4 z' I- P) f! [6 K2 A0 n
- ldexp(x, i)
# h4 ] {8 W. H F& E: z - Return x * (2**i).4 ~* f3 }" t% O& d9 B
- >>> math.ldexp(5,5)
" h$ F# R$ b/ v/ M( ?. @ - 160.0
) i; u9 t- p T5 y/ c - >>> math.ldexp(3,5)# E2 d9 p' Z6 e0 x) U
- 96.0
复制代码
1 |' b4 M8 q1 G- Umath.log10(x) 返回x的以10为底的对数" y6 W+ [' F8 A2 {! A
- #返回x的以10为底的对数! y" ^3 e# h* g
- log10(x)
1 ` m) i# P: M; | - Return the base 10 logarithm of x.
0 L N2 o4 z" [5 K0 U+ [ - >>> math.log10(10)
0 c. J8 t' c2 v+ E5 Y t; \+ S4 X - 1.0: d4 m, e9 n" i# t2 z7 e3 `
- >>> math.log10(100)
; \' M+ L4 V7 ^4 e/ _# ? - 2.0
) h& y: S0 r6 G/ z+ d2 u - #即10的1.3次方的结果为20
* n r! h7 {# U% ? - >>> math.log10(20)
2 h* _8 C( C* b+ t - 1.3010299956639813
复制代码 ' ~- a' Y/ _6 O& n5 h( C
math.log1p(x) 返回x+1的自然对数(基数为e)的值3 t c2 X4 y! J/ `
- #返回x+1的自然对数(基数为e)的值9 k5 ~4 o3 I' i
- log1p(x)
4 {! B5 h' R V7 w. x - Return the natural logarithm of 1+x (base e).
' ]# z+ b. W) C6 h/ c2 g9 { - The result is computed in a way which is accurate for x near zero.& f$ ]# }: ~5 u
- >>> math.log(10)3 x) _/ i- v5 C9 L& W# L1 p) g
- 2.302585092994046; [1 q( W' ^, A
- >>> math.log1p(10)
5 v( Z' e L# o- T( R! _ - 2.3978952727983707
* }! S+ @' T. X, Z+ w - >>> math.log(11)
. ^% \4 P/ d9 W5 ?9 O5 h7 z - 2.3978952727983707
复制代码
9 ?* ?9 O5 D* ^- `/ h+ Imath.log2(x) 返回x的基2对数/ o1 X7 V' ^2 u. k2 z/ K4 c% O
- #返回x的基2对数1 N: \9 |0 C+ f$ h4 W9 Q/ z
- log2(x)
" N$ l. o0 [! y: J6 V' L - Return the base 2 logarithm of x.
; P$ z5 \* ?8 \" d( g( E' c: p - >>> math.log2(32)
9 s$ Z4 E. Z h7 ~4 U - 5.08 W1 O8 @7 `+ m2 b, ?; t5 ]
- >>> math.log2(20)
& n3 [: E+ w* h8 b - 4.321928094887363
3 _! s; S. d c% c- U - >>> math.log2(16)
6 X# }. w7 U6 n* b5 t - 4.0
复制代码
2 l$ ^) _2 C+ imath.modf(x) 返回由x的小数部分和整数部分组成的元组+ z, L( c4 X4 O" a4 F4 {7 L
- #返回由x的小数部分和整数部分组成的元组% j0 ~( Y y* f M* Q6 i
- modf(x)
( o# L* S7 G4 a! D+ ~( b - Return the fractional and integer parts of x. Both results carry the sign3 c+ A9 s& v3 ?% @8 \2 G
- of x and are floats., l. X3 d7 L( R' |
- >>> math.modf(math.pi)% P3 `! L6 T$ j5 w6 m) `1 p% S
- (0.14159265358979312, 3.0); p2 @/ k: _) u5 ^7 R4 q
- >>> math.modf(12.34) W/ i. t H5 m! t8 s3 z8 x
- (0.33999999999999986, 12.0)
复制代码 1 V- d: G! _2 H3 G3 C
math.sqrt(x) 求x的平方根
' `0 z4 r! U4 _# T/ Z+ ?5 K- #求x的平方根
) K1 p" A1 n+ r6 }9 G - sqrt(x)
0 ]; U- ?8 }! Z$ J. s" ^+ j - Return the square root of x.
( T: R5 a+ ^, Q5 y$ ]0 }! O - >>> math.sqrt(100)
" M2 V i6 t# H/ E - 10.0- U" Q4 _. ~* L
- >>> math.sqrt(16)
! x; h) T; R/ {8 B; I& r - 4.0
8 R2 B( n* Z- i7 \5 }1 c' D$ n1 ?( m - >>> math.sqrt(20)- N% E t4 r2 r* t+ Z) F! T
- 4.47213595499958
复制代码
n4 d8 y. }; v( B0 jmath.trunc(x) 返回x的整数部分- #返回x的整数部分3 _' A" ^$ {4 w, S* O
- trunc(x:Real) -> Integral
7 o; d' b4 k- p! x% h$ \ - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( r0 v2 X' i9 c7 {# r# k
- >>> math.trunc(6.789)1 G- _1 i' ?. I/ L) U9 [/ f5 d
- 6
/ d$ ]% S+ |/ b' u, F - >>> math.trunc(math.pi)
5 C+ P* a# B. n/ v; K) b" M) ]" w - 3
Y) l& \$ _- A4 R' }3 E - >>> math.trunc(2.567)2 W% X: J. k$ m
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|