马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 C$ T+ S- ^& o1 k0 w【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. M+ H* a7 }( u/ o
7 n* s- `9 Y6 u* Z. [/ ~$ J9 H
方法1:
. r) ^& i& Z$ b0 ]/ g' e3 b- >>> import math
1 ]' x; J" o3 h- H& k0 C! R6 s! \ - >>> math.sqrt(9)* v, ?7 b% R) b# K7 \" o+ [
- 3.0
复制代码 方法2:
, f$ [; Y K/ |/ B: d2 l, f- >>> from math import sqrt
5 z2 A- M& ?9 [; E( S/ L5 p" P, c - >>> sqrt(9)$ T8 d/ \" z, S+ S. h) J
- 3.0
复制代码
6 l2 Y1 N; I) p0 x" r
1 e. {0 _7 q8 ?$ Rmath.e 表示一个常量8 ?6 c: Y& ~; B" B
- #表示一个常量
# F0 _- p% p( G$ O - >>> math.e
7 w9 G) S+ c8 o) Y e8 J - 2.718281828459045
复制代码
5 }5 ~; d0 G+ P" W1 omath.pi 数字常量,圆周率
4 c! @5 k) O5 z8 G- #数字常量,圆周率- K3 A0 I6 V0 m6 ~2 e2 E
- >>> print(math.pi)
" ]/ B1 r7 [- S - 3.141592653589793
复制代码
. R7 z# b% U2 A& {math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x/ O! S6 E* _. P. p0 @
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 b% R; e1 L0 [ - ceil(x)
! B: D, L7 j' j - Return the ceiling of x as an int.1 F" _$ s/ m- V+ u+ j
- This is the smallest integral value >= x.
0 M2 _, r' t" z) ~; g U( e- ]
/ p' s# j* [0 {- >>> math.ceil(4.01)0 T3 J1 k1 |9 H
- 5
8 u" q" g( G. X4 _! m0 { - >>> math.ceil(4.99)$ ]* S3 W* |* \$ O4 g
- 5
4 R5 Q& ~. O$ q: S( P; ~3 {) y! N' l - >>> math.ceil(-3.99)% R- |# C) H& Z4 s; q, ^" q
- -3
" ~0 N/ @: B( b2 [, ~7 b - >>> math.ceil(-3.01)
2 l' ]) `! I* |( W8 b - -3
复制代码 , O- ~, Y4 _4 N/ T% ?& J- E: [6 j
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 z6 y- |% M( g# w1 U8 Z
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% t, M G* L+ x6 { - floor(x)
2 o$ J/ A6 x# n9 ]& m- D - Return the floor of x as an int.1 B8 u* V2 ~4 Z R$ h6 l* u
- This is the largest integral value <= x.
r. j* } o+ z. y& [ - >>> math.floor(4.1)1 ]3 k/ k- s. v! r4 n2 w, z
- 4
) L7 `- e5 M* [& O: P2 S! z - >>> math.floor(4.999) c( A0 f: K# S0 c
- 4
: s2 p3 V$ c7 ?7 P7 N& M* U - >>> math.floor(-4.999)1 \ ^5 j' u# }/ @* q0 x
- -56 K( F+ m/ W; @5 {9 ~: @
- >>> math.floor(-4.01)9 `) V2 Q) X/ B' s
- -5
复制代码 : u" W5 I0 C. t9 u) L6 S) O: ^
math.pow(x,y) 返回x的y次方,即x**y
P3 \2 {8 \" _* O- #返回x的y次方,即x**y7 o! J2 e% G) S3 U. k
- pow(x, y)
8 O. |1 O0 a) X% z: X& y - Return x**y (x to the power of y).4 _- |& U# }3 C, `0 } |
- >>> math.pow(3,4)
$ u. \9 _3 t. {2 o - 81.0
, L0 K8 s! k& T/ J* h0 ^ - >>>
y4 e6 M* O* N' V* e+ z) ]- A6 o; U - >>> math.pow(2,7)
& b7 p2 | D4 x! E5 h - 128.0
复制代码
8 T4 v( Z( {# Z( h) b3 E: x5 o9 r! omath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 |8 b8 W) R0 U$ ~6 }2 C" E; E7 P" j- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 h: ^' L9 v; u$ f+ p+ K - log(x[, base])( R1 P/ {- ?2 K4 r9 G* N
- Return the logarithm of x to the given base.
, ~3 U: C" K& Z! f( H( Q2 e - If the base not specified, returns the natural logarithm (base e) of x.' O) L% p' l1 |. ]4 T
- >>> math.log(10)0 L& M# o* k5 D% W5 i; |# h
- 2.302585092994046
# T5 e9 F% o7 n3 a - >>> math.log(11) ]' Z! F2 u, s1 j+ T) C5 @5 v j( j
- 2.3978952727983707
) ^/ _3 {+ M [' ?$ P - >>> math.log(20)
9 F' [7 O4 \2 V I, H5 r+ ]- N - 2.995732273553991
复制代码 ) K9 [, \0 D1 d( {3 C* J8 P; p% I
math.sin(x) 求x(x为弧度)的正弦值; E* ]* \: j+ h
- #求x(x为弧度)的正弦值
+ l0 o; H7 F- `7 O) g- U - sin(x)
% {! T7 u5 Y0 O* f4 l4 \ - Return the sine of x (measured in radians)." d7 Y/ B' r/ K6 S3 N3 K( j6 W$ f
- >>> math.sin(math.pi/4) V/ s% C3 z- `( r
- 0.7071067811865475
3 a9 n( F+ A* v7 z1 e, G - >>> math.sin(math.pi/2)
$ q+ n/ z: X/ T5 } - 1.0
a9 y$ \4 v2 h* `' v3 H - >>> math.sin(math.pi/3)
: B; P1 d+ k0 b9 ?3 J+ F - 0.8660254037844386
复制代码
. L# `2 r% J* S% y% cmath.cos(x) 求x的余弦,x必须是弧度
+ P' q% Y( I2 B" t; ?5 e- #求x的余弦,x必须是弧度: L- u3 A# ]' q
- cos(x)
3 w1 u4 }( a3 p% f3 V. E - Return the cosine of x (measured in radians).# l1 S. x0 ~& H0 v& O
- #math.pi/4表示弧度,转换成角度为45度
8 {' B5 K0 h4 Z+ ~6 v h - >>> math.cos(math.pi/4)9 v6 h) Q9 m& c, R
- 0.7071067811865476
4 h4 Z7 Y8 W( C' }' g: l - math.pi/3表示弧度,转换成角度为60度8 D" S* {9 L" Q! S1 A% K2 I! r
- >>> math.cos(math.pi/3)
' ?2 {( {$ l r - 0.5000000000000001
" Z/ i4 P$ K9 a# ~, n% t" W1 i8 w8 b - math.pi/6表示弧度,转换成角度为30度
7 O% }. C* q( B( L8 m - >>> math.cos(math.pi/6)
1 t) b" ]3 P1 F' g `$ R, a2 q - 0.8660254037844387
复制代码
Y$ i9 u% d& i2 Mmath.tan(x) 返回x(x为弧度)的正切值% ^& |+ V4 J# ^$ f
- #返回x(x为弧度)的正切值
9 R+ L/ o! h' |% L, ~+ s) C - tan(x)
6 ?8 L. a& A. w3 {9 U, }3 U- x - Return the tangent of x (measured in radians).
1 `5 q O$ O- ^- n3 r# c - >>> math.tan(math.pi/4)
T& d8 h- @; f$ e# I - 0.9999999999999999
) u% I2 M3 L: ]7 b - >>> math.tan(math.pi/6)5 ^; D0 _: [) [7 I
- 0.5773502691896257
) H# }$ g% }5 d# ] - >>> math.tan(math.pi/3): Q6 Q- D3 F7 F5 l% y6 \$ u
- 1.7320508075688767
复制代码 0 M+ L. x3 g/ H! O
math.degrees(x) 把x从弧度转换成角度
5 h7 Q8 y! F& d- #把x从弧度转换成角度5 \5 b% q, P6 k" ^) @$ G
- degrees(x)0 f7 Z. |0 J }+ T7 ^) Q7 j9 h
- Convert angle x from radians to degrees.
" `) [, }8 R8 F' t# a* V" }" z5 @
5 D0 E2 V' Q. T- \3 g8 R8 z- >>> math.degrees(math.pi/4), B. {- U/ }, V: Z! g
- 45.0, B# n3 ^9 `4 T5 g, u% |
- >>> math.degrees(math.pi)
/ e& I: t; `% t3 u: N - 180.09 a) u) L5 @; |4 i, ]! C+ f1 c
- >>> math.degrees(math.pi/6)
; b; G2 G: d5 E p: x. \ - 29.9999999999999967 K9 W0 n `0 L& u
- >>> math.degrees(math.pi/3)# n2 l) a0 B P+ A$ F$ v
- 59.99999999999999
复制代码
$ K" R w! t1 ?* x( O+ p( q1 p" A7 p: Gmath.radians(x) 把角度x转换成弧度& h' i5 {% H7 G0 L- T# j/ ?
- #把角度x转换成弧度7 _) s5 M" ^& \7 T! w9 ^. }$ H8 W
- radians(x)
$ z" s4 N' K& @ O - Convert angle x from degrees to radians.
; s; t* K$ ~9 v* y6 L - >>> math.radians(45)
6 n+ J& S; }8 W7 \; [ - 0.7853981633974483
. Q1 F0 J& ]5 |. r, @1 ?7 G - >>> math.radians(60)- Z3 {) P4 A, U& z
- 1.0471975511965976
复制代码
0 v% M* u" d& R3 W& v7 umath.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 C2 o/ q: B: S1 \. ]4 s- #把y的正负号加到x前面,可以使用0
7 |' L0 u+ ]7 w, Z( h3 z% U) Q - copysign(x, y), ~; _( ~) V3 L
- Return a float with the magnitude (absolute value) of x but the sign ; p# W+ C' V$ Q3 `5 {' m
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
. }8 x4 K' j3 K - returns -1.0.5 v7 D( L2 A8 l3 P, e: j
8 E9 @9 |3 p( W* U' A- ^' M3 x5 }- >>> math.copysign(2,3)
) I# ?+ f, T4 E( J+ K: n) t - 2.0; T5 c/ h8 n) g7 g
- >>> math.copysign(2,-3)
; r# l+ ?4 h Q+ q: ~8 O - -2.0" {% ?7 R7 ?0 C; k
- >>> math.copysign(3,8)
1 l4 c# r: Q6 n+ G( J - 3.0
+ B( [2 J2 u! e- M - >>> math.copysign(3,-8)
! G9 Z8 Q' u4 I, T" z# a9 ~. u. k0 {" ^ - -3.0
复制代码 ; b3 U5 S! d6 u* y* Q& x) @
math.exp(x) 返回math.e,也就是2.71828的x次方
- C2 u& v# b1 J- #返回math.e,也就是2.71828的x次方$ p7 e1 p8 ^2 ?5 z6 ^
- exp(x)
/ _! n( F% ~7 U9 C - Return e raised to the power of x.$ z/ H& n, g4 g
/ r/ _$ C5 F/ g$ f9 O- z3 c- >>> math.exp(1), B( x. _" A$ D4 R- D5 E% {
- 2.718281828459045
/ j( `) w4 ? [( u% C$ A - >>> math.exp(2), Y$ N! H6 P0 |1 H# T7 r+ j# k
- 7.389056098930650 N3 G+ p+ P; `; y3 t
- >>> math.exp(3)
# p. s+ y/ ]4 c5 W5 _ - 20.085536923187668
复制代码 * ?6 Q& z) L9 \
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# C/ Z& a+ j% P+ r- #返回math.e的x(其值为2.71828)次方的值减1; J0 Z; ^' z2 m$ G: V. [' x
- expm1(x)7 |5 @+ n# T# ?8 I" X; M
- Return exp(x)-1.! I, q- I" J. M5 \
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
/ C3 t6 V1 r* a7 O& q2 W( J; {% W. t - ' v2 R# Z+ y) E
- >>> math.expm1(1)% _2 I2 J$ ]& v
- 1.718281828459045& y( _5 k/ u% ~8 ^4 L
- >>> math.expm1(2)
2 w5 C R, i. r5 ^# o; j n - 6.38905609893065, @8 h. i) Z3 Q5 e A
- >>> math.expm1(3)9 O% B% ]% {( g" p# n
- 19.085536923187668
复制代码 * q3 E, A- b- L7 f
math.fabs(x) 返回x的绝对值
" `/ u, I% w( n3 ], Z- #返回x的绝对值
; l& }5 F6 s# ^( A% L `& ` - fabs(x)4 j1 { K4 {" t
- Return the absolute value of the float x.# [6 L7 g; y* z% I0 O* h; Y0 j6 L* M
- * p8 r- x1 d3 I1 U% y7 s' a
- >>> math.fabs(-0.003). i3 P6 O7 l# x
- 0.003
) U0 s8 C( N6 Q! L0 Y: e2 a - >>> math.fabs(-110)6 o, ~7 u6 k* N! n0 I" ~+ _; T
- 110.0+ _! f1 B% n9 M- d1 u
- >>> math.fabs(100)) _$ e" K0 N& o5 | ~
- 100.0
复制代码
2 O& ^9 G- E) C) C2 e, J! Vmath.factorial(x) 取x的阶乘的值2 u3 y7 I) ]+ f. B7 o
- #取x的阶乘的值' w! d1 N/ p4 B. |
- factorial(x) -> Integral
" `: c" A5 {# q" J. P* k( l - Find x!. Raise a ValueError if x is negative or non-integral.6 x2 U: Z9 n2 L+ X$ @ P. T
- >>> math.factorial(1)3 W. P D# {4 F8 o
- 1
) ?) x; y, ^5 z- C9 } - >>> math.factorial(2)7 o% ~% f! N a& ?- U
- 2
# f0 {6 M: ^$ K - >>> math.factorial(3)$ O1 p9 M V3 ^1 @) h* [5 a+ s! V
- 63 s: k; Y( {0 d. m3 a
- >>> math.factorial(5)
- p0 H3 D0 P! g _9 a6 ?9 ~ - 120 }7 b' G! i% g v `' X
- >>> math.factorial(10), B, p; I. i# [, g8 y
- 3628800
复制代码 2 U1 f2 n6 {" e! Q0 h
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数' }! r$ e- Q% J1 Q
- #得到x/y的余数,其值是一个浮点数7 ~& r5 L1 @. l& k/ I. G3 v
- fmod(x, y)
+ `6 }1 C- q4 W1 @; M0 T - Return fmod(x, y), according to platform C. x % y may differ." o9 T, f5 o) i, j5 A9 q
- >>> math.fmod(20,3)- }7 h" c+ l& r$ ^" u
- 2.0
) I z4 q9 O0 D% i3 N: Q - >>> math.fmod(20,7)
b @# z( [7 i$ O% e7 i$ {" j* f7 }! \ - 6.0
复制代码 5 m' `, C+ A9 q4 @; K
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ G y1 q2 S' g. u& v, y, K1 y
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* {2 D, z" U2 E5 N L
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
+ O3 S' M" i6 j& O! L! k - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1 s' V# H: z& b9 q3 T
- frexp(x)
, v6 G* u' P- n: e: `- t8 Y - Return the mantissa and exponent of x, as pair (m, e).
7 x I1 F' a* R/ p - m is a float and e is an int, such that x = m * 2.**e.! |, C8 T/ Z' {8 n$ R1 i
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.( F7 E `+ ^% s' _
- >>> math.frexp(10)
+ W4 O3 J }! ]( o, ]0 b - (0.625, 4)1 R$ g' ~) n4 o. _4 @
- >>> math.frexp(75)& a q! x+ P! \4 V n9 B
- (0.5859375, 7), I6 W, Q `" ^1 ^0 p+ I! W5 K3 L U
- >>> math.frexp(-40)
4 j3 M! y; h$ @& z& j$ N7 b - (-0.625, 6), C/ p! U9 c; V/ t- o
- >>> math.frexp(-100)# w2 _) \" T2 r2 c$ s& x; F; ^
- (-0.78125, 7)' V% f4 U( ^" j7 R
- >>> math.frexp(100)+ |8 t8 ~$ {3 _9 _6 n
- (0.78125, 7)
复制代码
$ y3 \4 I+ |8 i$ i. W' p9 d4 v2 Kmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
# q. A6 A7 s. v7 J- #对迭代器里的每个元素进行求和操作- J/ f! Y6 y" }2 c1 l2 O7 o
- fsum(iterable)" x* J3 K9 @6 I+ y% Y! F9 N% T: J
- Return an accurate floating point sum of values in the iterable.
* u7 M8 i* Z5 d8 N; r- K7 U+ C: ? - Assumes IEEE-754 floating point arithmetic.. A- k2 g- O5 s2 S2 ^
- >>> math.fsum([1,2,3,4])/ K, _2 ~8 U5 r( j
- 10.0
/ S) o. R2 n+ g9 E; r- C' @ A, z - >>> math.fsum((1,2,3,4))
5 w T/ n/ S3 ~) o# ~8 Y - 10.0; [3 f5 I c6 @8 X' D- s7 Z
- >>> math.fsum((-1,-2,-3,-4))
; ]/ ~3 s# y9 b - -10.06 Z. ?$ W& ?6 F7 T, ]* j
- >>> math.fsum([-1,-2,-3,-4])1 }' o3 R1 W, A/ ?
- -10.0
复制代码 2 ?3 b2 C2 @8 d1 K/ b" u6 g
math.gcd(x,y) 返回x和y的最大公约数
7 d" Y/ I5 ^/ s. ^- #返回x和y的最大公约数. u: j; \$ b5 B
- gcd(x, y) -> int
" m: l# l* A) O3 Q a* C - greatest common divisor of x and y. E: N9 Y) M) l+ R
- >>> math.gcd(8,6)
. e) l' P$ V! Q" c* C4 c - 2
9 P( q5 g( L! d# G - >>> math.gcd(40,20)
* s) s# w5 O! k( D - 201 ~: I* k# x. o
- >>> math.gcd(8,12): M: w* ]$ t9 j+ d8 c E
- 4
复制代码 / ?+ M4 v7 ?& x7 o" Y
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 q$ }; M% ~3 a
- #得到(x**2+y**2),平方的值1 T) \" A( ]! r
- hypot(x, y)% S5 c$ n1 E/ `; K7 r+ T
- Return the Euclidean distance, sqrt(x*x + y*y).. C# n" L& o& `* Q: z w) i
- >>> math.hypot(3,4)
, \: e: @/ e* z3 ] - 5.02 `7 d$ p5 ]4 F% v7 o* q
- >>> math.hypot(6,8) _7 h7 B+ j; d# n0 G' |- {- X
- 10.0
复制代码 # D, i; a" z: M1 q Y
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
. m( ^' d+ D6 q b- m) x- #如果x是不是无穷大的数字,则返回True,否则返回False2 G, q% `; F3 Q
- isfinite(x) -> bool8 p3 j4 u0 [) L- j0 \! ~% h( t
- Return True if x is neither an infinity nor a NaN, and False otherwise.2 }' N! {' } Q7 ^- a
- >>> math.isfinite(100)
' h% D) t; m8 _8 ~4 C - True. r6 r2 N+ ~; J" {, v
- >>> math.isfinite(0)
: m2 ?, F& f; ]0 [6 H) d* n% I - True: a, t3 s# b; R; }9 V
- >>> math.isfinite(0.1)
0 U! [; [: y" N* q1 m( C - True4 {$ \1 |3 A9 _9 c$ L
- >>> math.isfinite("a")( n2 ^. O# C" g. q
- >>> math.isfinite(0.0001)9 |2 }7 J. q$ u5 `0 G
- True
复制代码 ; e6 U# N; w' C" d2 H/ o& h7 j) U
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 Z, _1 ?1 {/ F& r# p) l- #如果x是正无穷大或负无穷大,则返回True,否则返回False5 Y% i& J% b0 @
- isinf(x) -> bool
( {% h) s( P5 Z ]8 N/ w" e - Return True if x is a positive or negative infinity, and False otherwise.1 R9 o! |4 |0 U6 S
- >>> math.isinf(234)
1 A6 V: a4 ]& ~+ M - False' T" g) N! x! w, D& ]1 C$ A
- >>> math.isinf(0.1)# K6 l9 b- |/ b
- False
复制代码 * g7 S: h! e0 s
math.isnan(x) 如果x不是数字True,否则返回False
! j& { N/ C8 C: Z6 C- #如果x不是数字True,否则返回False8 M$ |. d9 T4 v( b8 d4 v, _
- isnan(x) -> bool D5 w( p* Z; Q" E- F, B
- Return True if x is a NaN (not a number), and False otherwise.
( j% B5 F: ]: n$ ]3 i9 s - >>> math.isnan(23) g' S9 U6 L* P0 Z( M+ G
- False
5 b3 z# o2 C$ v, V4 N* O - >>> math.isnan(0.01)
. M% s) t7 }% ~6 x# y! } - False
复制代码
+ J1 F( n ]+ o% O" Cmath.ldexp(x,i) 返回x*(2**i)的值5 C6 P# ]! ^3 {$ ], z
- #返回x*(2**i)的值" w1 B3 Z2 N8 Z1 H" D
- ldexp(x, i)
& }( ?% W! b# i C8 }. @' n - Return x * (2**i).8 `" n4 ~( g6 x% C9 C& W8 u
- >>> math.ldexp(5,5)
2 r4 r: P' e8 g - 160.03 v" @$ h6 g8 U) F G
- >>> math.ldexp(3,5). ]4 u) Y% V/ _% ?# B# d
- 96.0
复制代码
w$ X' ?' W) O: p* E, Dmath.log10(x) 返回x的以10为底的对数: ], l B+ @4 \4 i& T' q+ ^
- #返回x的以10为底的对数
+ f% S: J p' F4 w4 u' x - log10(x)
$ ~ q7 T. t, y! L7 b2 s% M - Return the base 10 logarithm of x.
9 O3 b, @3 G3 l8 _ R% G - >>> math.log10(10)
a( J2 M( v# n4 A+ c9 g: x' ^ - 1.0
6 C* ]) h5 c0 B. r - >>> math.log10(100)
- X) X% A/ l, F: B* h - 2.0
6 U) s+ {, z# s$ r* j, M - #即10的1.3次方的结果为20; d n- W; t9 x( e( p% |3 l7 K1 S
- >>> math.log10(20)
6 Z6 i- p; p. Y' ] C* x - 1.3010299956639813
复制代码 1 z$ U. E6 F- V6 k, J) ], l
math.log1p(x) 返回x+1的自然对数(基数为e)的值1 V2 D; j P: s8 I8 B! W
- #返回x+1的自然对数(基数为e)的值
8 o% Z# J6 g# P, D - log1p(x)
8 j" L2 X1 e) B! U( ]1 S - Return the natural logarithm of 1+x (base e).
$ G* m) R( M: D - The result is computed in a way which is accurate for x near zero.% j4 |6 W2 }( b& j; n
- >>> math.log(10); X* E* v: U9 F: B, j
- 2.302585092994046
@) L& i, G- U7 I - >>> math.log1p(10)5 M% l. _) q( |$ N8 u* h/ b& B
- 2.3978952727983707
6 c$ x: B& J- J% h2 _% y - >>> math.log(11)
* {+ B8 f( r6 m" g. \' I - 2.3978952727983707
复制代码
4 B, D- J+ k: Dmath.log2(x) 返回x的基2对数
* z- m, q& c* {9 y9 ]- #返回x的基2对数
! u+ W6 d1 n. U U - log2(x); ~# j% ~$ N/ Y! L, J# ^
- Return the base 2 logarithm of x.
1 M9 S4 A- o4 _& W - >>> math.log2(32)& g' b5 x e5 p3 ], }6 S' n
- 5.0; y+ N) V! q% G G0 r
- >>> math.log2(20)
9 K3 ]8 c' r% h4 F4 z - 4.321928094887363+ i, q, ^- p: y6 r; ]0 b+ ?2 N
- >>> math.log2(16)
+ ~8 ^5 v+ j( S* S3 z4 ~ - 4.0
复制代码
$ D# T9 p3 k# s3 Mmath.modf(x) 返回由x的小数部分和整数部分组成的元组( S3 Y1 I/ t/ ^, ?& g# q2 A- t$ V1 C! ?
- #返回由x的小数部分和整数部分组成的元组4 A$ z) u0 m+ P
- modf(x)
; K5 S. I$ `+ Z - Return the fractional and integer parts of x. Both results carry the sign# C. ?2 n3 X6 U/ _
- of x and are floats.7 d" z6 c$ c5 R
- >>> math.modf(math.pi)
' e9 R9 t) b, t( ^% ? l - (0.14159265358979312, 3.0)1 m/ u. W7 m7 `6 J7 j
- >>> math.modf(12.34)3 F7 o6 i' V* r& C( Q. U
- (0.33999999999999986, 12.0)
复制代码 ( Z7 b7 ~9 I+ _% ~0 u
math.sqrt(x) 求x的平方根+ M4 G. v+ a8 G* }
- #求x的平方根
/ D! R; ~' @* a4 N% z - sqrt(x)
# \& A) R& {1 i6 O) `& t - Return the square root of x./ N- C# }4 y( l7 ?
- >>> math.sqrt(100)
, \( W0 k% k) X. b - 10.0
4 Y2 r5 R9 p8 G6 j4 j - >>> math.sqrt(16)
" e4 B$ Q8 E" E8 p+ n4 s( ?3 | - 4.0
4 y% R, T" l# G B1 Z, ]+ _1 w - >>> math.sqrt(20)
0 D4 b% u- {. w0 G" V, M3 h - 4.47213595499958
复制代码
$ N2 U# g4 W0 R# rmath.trunc(x) 返回x的整数部分- #返回x的整数部分; s' W; I _9 {$ d/ \- K9 b
- trunc(x:Real) -> Integral6 d' T3 v7 D. S
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
. f- V5 _6 Y1 z - >>> math.trunc(6.789)
/ R1 E2 d! i( Y - 6
$ o$ U4 [) ?1 u7 y4 F; }# u8 y( I' z - >>> math.trunc(math.pi)9 f- u% E7 Q0 o: \0 D% q" J! q. D6 p$ I
- 3
0 U h- D q5 Q+ W9 L( g/ v( F7 y - >>> math.trunc(2.567): {: G* h" g9 _
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|