马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
. Y9 t) Y. e, b! B$ [9 C/ q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。8 m+ K$ w. D2 t7 \, k) q! f
: r( E. d/ u7 c
方法1:8 c# r' c# g/ R+ x% M) ?
- >>> import math, H8 a* V! j4 ^! b8 S. B
- >>> math.sqrt(9)) ^1 U( L0 M* |" [! U6 Y
- 3.0
复制代码 方法2:
, k$ `, `6 k/ A; k8 y1 Y- >>> from math import sqrt. g! W' ? b; `! F- Z2 y
- >>> sqrt(9)+ W8 f% Q# A7 z2 z
- 3.0
复制代码
+ }# ^" E( b/ C- g& m0 I3 f
( p4 w- N# t7 L3 ^math.e 表示一个常量
( z) W1 I/ v3 ?5 v9 U- #表示一个常量
I& |9 t) p) D+ S4 B4 M - >>> math.e' M2 @. T/ Z( F) S' _- A8 [8 z
- 2.718281828459045
复制代码
5 s9 n8 Z9 o& g$ f: v8 F* n6 xmath.pi 数字常量,圆周率+ Y' N! a: [( R7 q
- #数字常量,圆周率
/ K3 n4 Z/ `# r) V& ^6 S. O - >>> print(math.pi)
# I3 u2 x8 Y, e( m, c - 3.141592653589793
复制代码
& _6 P) k- p& L# h1 M5 hmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 R8 K/ b* K: ?5 @- #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 M N/ m9 H7 a1 ?
- ceil(x)' ~* J1 ?4 L9 M, d
- Return the ceiling of x as an int.
6 `! C, m- X4 Q, k - This is the smallest integral value >= x.
& W5 K8 k3 x. x - ! C. Q6 ?- X2 K1 m- {6 Y( b
- >>> math.ceil(4.01)% a7 E. g% h7 |9 C
- 5" c/ r- s) i% i' b5 z1 z Z/ B2 f
- >>> math.ceil(4.99)
7 n7 I" v" `: y: i2 f0 | - 5. v: w$ f. T) s {0 E+ n
- >>> math.ceil(-3.99)
$ z$ L$ H9 {* _$ M/ J6 L - -3+ V2 N* g7 K1 v# u' N! _
- >>> math.ceil(-3.01)" o5 C3 b# Y. B( p m' n
- -3
复制代码
Y; b( h% t" X# umath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% d' p! O% ^. y6 m' d) I- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* s/ k8 l* f6 c
- floor(x) u( i# w! n" U4 T. k& w8 d- o4 I, R
- Return the floor of x as an int.7 Z6 R4 s' G/ `/ S$ Y
- This is the largest integral value <= x.4 b, y' ^( A [
- >>> math.floor(4.1)
) L( U o1 V# k V9 T$ s/ C) n$ ? - 47 f' ~3 p% C7 s5 k& n/ B
- >>> math.floor(4.999)
" i/ @$ V& Q% d* M - 4! y. X) w& Z, ]7 J: J# [
- >>> math.floor(-4.999)8 N+ }% ^' t" b8 h
- -5
. R! R: S/ ^4 R7 y - >>> math.floor(-4.01); Z: j1 a5 P, M9 S, L$ Z8 I w' g4 |
- -5
复制代码 % c4 J8 g* b! T( W( O5 f
math.pow(x,y) 返回x的y次方,即x**y5 g$ p# s8 {3 g4 F, y8 R
- #返回x的y次方,即x**y3 g8 P: a/ ]! r! {) u' s
- pow(x, y)7 p5 Y5 g# W8 j9 v) L
- Return x**y (x to the power of y).
! x2 T S4 ~2 F - >>> math.pow(3,4)0 Q- l0 W V4 ^& l( n: V# S
- 81.0
/ j. S; y0 l' q; z3 k6 ]% h0 s - >>>
% C( Q4 W9 R6 N* b" `$ m5 W - >>> math.pow(2,7) L* e7 _/ ]) k0 R$ @
- 128.0
复制代码 ) Z& O* G9 B* x7 Q. }. a) K
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' X$ @2 E$ K- e3 T: s/ T
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" t6 l: Y& C! P% o3 h& V( W" h - log(x[, base])- p6 U5 h+ d0 r
- Return the logarithm of x to the given base.
" [- \9 i1 x! b4 z) F' P( ?: u - If the base not specified, returns the natural logarithm (base e) of x.; Z2 B5 O% x: s+ P
- >>> math.log(10)
' F3 W' r9 |4 M8 g, y. O - 2.302585092994046
6 T$ n+ u9 Z* i; j) {0 V( @( `3 l+ t; h4 P - >>> math.log(11); o3 ?5 W- o& S1 i
- 2.39789527279837070 N' F4 S# ~& G
- >>> math.log(20)- |% s2 x# N" r9 u) S( K( C
- 2.995732273553991
复制代码 , ]' |' ~, q# |& C
math.sin(x) 求x(x为弧度)的正弦值3 |9 \" V/ y& b' Y
- #求x(x为弧度)的正弦值, Z1 r2 T. r4 }
- sin(x)
, R6 u6 l$ n% f - Return the sine of x (measured in radians).
# h' K) Q4 n" G - >>> math.sin(math.pi/4)0 V0 A, O- {8 L1 _
- 0.7071067811865475
* b! P2 b* C% ] - >>> math.sin(math.pi/2)$ c0 q0 _ \& m/ l# w
- 1.0" L. c, _1 Q7 I! a
- >>> math.sin(math.pi/3)
% N5 ^9 s5 S) \* n7 i9 s( i - 0.8660254037844386
复制代码
; l4 @5 \' y5 i h% f5 D6 I8 |8 v. Xmath.cos(x) 求x的余弦,x必须是弧度
6 \0 N! \, e0 S8 I: A; x- #求x的余弦,x必须是弧度
% B0 E8 U5 D0 C# }8 X }6 F3 D( b - cos(x)2 B) X" E+ w& ^5 L' n
- Return the cosine of x (measured in radians).
7 A) B+ V1 D3 B' [8 y$ V - #math.pi/4表示弧度,转换成角度为45度
+ q+ d7 s K% H" z - >>> math.cos(math.pi/4). k! D( [5 i* l/ o ?
- 0.70710678118654769 [* r$ ?0 S y( _0 J# w
- math.pi/3表示弧度,转换成角度为60度) N2 Z3 ]9 \+ y6 m) A5 \
- >>> math.cos(math.pi/3)
0 G6 H; h t( |5 B* P - 0.5000000000000001' ?; T- h! g) ?3 P% G
- math.pi/6表示弧度,转换成角度为30度+ z0 O3 g/ [5 {" s" q
- >>> math.cos(math.pi/6)
9 t+ b3 F" J) w2 D - 0.8660254037844387
复制代码
, L/ M1 }; M1 fmath.tan(x) 返回x(x为弧度)的正切值
! M& M7 |' }: W- #返回x(x为弧度)的正切值! L0 ^% {* A1 H: d6 X# K
- tan(x)
! c6 F; E; o6 h3 w& }. l# X# f- Z - Return the tangent of x (measured in radians).
5 r& c# S# \7 ]' s - >>> math.tan(math.pi/4)+ k4 G, a. V# f+ S/ ]
- 0.9999999999999999- c% j E$ K5 N: d
- >>> math.tan(math.pi/6)
$ O8 h9 N3 c* A! j' } - 0.5773502691896257! r! I) D/ y i4 J
- >>> math.tan(math.pi/3)6 b0 W% d" ^1 Z9 o, U
- 1.7320508075688767
复制代码
4 @. Y6 n; N6 Q0 H$ U5 Qmath.degrees(x) 把x从弧度转换成角度 h( F: O( c3 `1 M/ x8 s5 H9 c" B
- #把x从弧度转换成角度7 H* m! i. A o
- degrees(x)
% J! J6 Y& w3 c; k' y, {5 W - Convert angle x from radians to degrees.1 ?; E7 t' t- V: j
$ e' p1 I! f% _- >>> math.degrees(math.pi/4)
0 W/ I( C5 r/ u, j* W9 t5 {" e - 45.0( F2 A8 N) x3 Q
- >>> math.degrees(math.pi)
0 Y) q# [7 i% ^% ?1 c" ? - 180.0
: k: |+ y$ Y! v) r+ d, @: ? - >>> math.degrees(math.pi/6)
6 i* K1 c. F7 U3 v8 d1 s( I/ G - 29.999999999999996! T: t1 l% K1 K9 Q( l1 D
- >>> math.degrees(math.pi/3)3 Q& U6 D5 _! N7 L5 q: b
- 59.99999999999999
复制代码
2 Y4 {4 u- p+ A6 R$ _4 cmath.radians(x) 把角度x转换成弧度8 t( r: a, B0 u; x# `* q+ d
- #把角度x转换成弧度- F: R8 b( W: x) d4 d1 |
- radians(x)
' y0 d' J- s* _ - Convert angle x from degrees to radians.0 b& u1 O* H1 {' V& H
- >>> math.radians(45)
4 J9 l6 ]& Y" G8 \ - 0.78539816339744835 u4 y2 \* }6 V' c: f* p
- >>> math.radians(60)& }5 O- [( U( \4 a: O
- 1.0471975511965976
复制代码 : C |" I# e; g3 ]. e
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
3 F/ v! z( q0 r- #把y的正负号加到x前面,可以使用0
+ r3 T/ O" |$ L7 K+ Q2 ] - copysign(x, y), c: l9 z$ E% Z# j4 Z$ F C" s3 n
- Return a float with the magnitude (absolute value) of x but the sign
5 F5 U5 |) E4 g$ J3 q - of y. On platforms that support signed zeros, copysign(1.0, -0.0) * i" F& C. X5 U# R, g8 U/ s+ T& m) ]
- returns -1.0.
1 G+ c c3 O/ A5 P/ h/ e( O0 b
' l. I/ s# y x( K7 w8 [" M) _- >>> math.copysign(2,3)
7 J, o1 v+ \& V2 f; I9 K5 r - 2.0; L: s; }% K/ C% T( k7 r) d/ p
- >>> math.copysign(2,-3)
3 O$ d) Y3 y: W2 r$ y - -2.0
+ d% h1 K3 `4 S7 p( s - >>> math.copysign(3,8)3 v6 _+ W. j a4 G+ {4 s
- 3.0
' {3 a/ Y5 \( j2 @( f3 \& L( { - >>> math.copysign(3,-8)$ c# {. ~" z/ ^ a# A% d
- -3.0
复制代码
1 f. A4 ^, G& f0 ?+ _math.exp(x) 返回math.e,也就是2.71828的x次方
( B$ W5 ^* ?( i$ A A- #返回math.e,也就是2.71828的x次方6 p- o1 U+ h( W
- exp(x)
8 T- r8 `! t; _5 S) V - Return e raised to the power of x." N' u8 c. b! e* O5 F
- - I* ]$ J2 N4 }
- >>> math.exp(1)' [' U/ U# Q- l* l; a2 f
- 2.718281828459045+ y5 u$ `5 N8 @$ {
- >>> math.exp(2)
* h B& }. Z8 \) u - 7.38905609893065" |6 g8 C" {4 ]' E4 |& q+ L0 [
- >>> math.exp(3)
+ `" ]0 r3 ?! O; F7 J0 H - 20.085536923187668
复制代码 ! `# n5 [, O( f3 w1 h/ T7 V
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减16 V) U. U3 i% J. W7 \ G' V1 W! U7 \: O
- #返回math.e的x(其值为2.71828)次方的值减1
) {5 p0 u- |9 y3 o& F1 a - expm1(x)7 p% [! S; A2 u
- Return exp(x)-1.
- u2 R9 n4 x* t: c - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
' U) L3 t7 M" p' I3 O) e - 5 x5 n# O! g( e. ?: ~7 [, C7 E2 p; L
- >>> math.expm1(1)+ R" k* k' i5 o
- 1.718281828459045$ \5 u; e+ V: o/ w2 |
- >>> math.expm1(2)
o$ m1 |, e% T, j9 z - 6.389056098930652 D; p2 s! j+ F9 a- v8 Z2 y
- >>> math.expm1(3)
* L- _8 V, w m9 j( N - 19.085536923187668
复制代码 6 J6 H4 |6 a0 w2 n' \
math.fabs(x) 返回x的绝对值: ]% `# J4 h- U
- #返回x的绝对值
5 \% J$ G0 J( }: k - fabs(x)
2 a: x0 j) @0 i9 z/ ~' c - Return the absolute value of the float x.
" O8 G# T: O% U1 _, C' H' s' u' @
$ o4 s: M9 K! c7 @$ u8 z- >>> math.fabs(-0.003): \5 P+ E& g' S
- 0.003
4 \9 K, s3 y! P - >>> math.fabs(-110)0 c a. P1 y1 m( P* `2 t
- 110.0
( u6 A7 e9 B, x$ z: a - >>> math.fabs(100)
% R$ y' G7 Y+ p0 m( P5 E3 f - 100.0
复制代码
& x9 Z) s+ S1 }math.factorial(x) 取x的阶乘的值
$ {$ T+ \- c7 C% V- #取x的阶乘的值0 e6 A7 S6 M) c1 G) `" X
- factorial(x) -> Integral4 k! |. k, m' U/ E
- Find x!. Raise a ValueError if x is negative or non-integral.
1 x" {8 w3 S5 t" r# v - >>> math.factorial(1)
' }" M: t2 X+ {1 ~ `3 F - 10 o2 B/ r" {: r& N+ p4 |
- >>> math.factorial(2)
v% S1 G. h* l9 L) N; H3 K - 2
$ V- f# P: s3 o d - >>> math.factorial(3)& B, n) U5 L; {& L/ \3 d
- 6
; A2 L+ a2 S0 c) I2 y9 M! d - >>> math.factorial(5)) U7 b2 I. X6 |
- 120$ E4 F) X- C9 q" a
- >>> math.factorial(10), ]. a0 t' T& l9 P
- 3628800
复制代码
4 I- n# {, }/ _5 g; r+ f, Rmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数: {# f& U' {* {2 [8 `; u
- #得到x/y的余数,其值是一个浮点数9 D; s+ [8 v% O+ h, w: S0 D, V
- fmod(x, y)
) a3 }# }2 r3 z- _ - Return fmod(x, y), according to platform C. x % y may differ.
) H2 T: J0 N. t# i4 _" a; K* T6 Q X - >>> math.fmod(20,3)! N4 W2 d3 v, _- p5 r2 N% U
- 2.0
/ f5 u& e2 r; }0 z3 c - >>> math.fmod(20,7)) i; s3 Q8 \1 @* A
- 6.0
复制代码 : ?( R& C% C4 f3 q z: }/ m
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) i$ h$ B9 }' N' H/ |, B
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; ]. \1 K/ K6 e
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
2 a) I( q8 I; [5 j - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, a+ H, e P% r$ T& a
- frexp(x)+ k4 c: c5 S! j" s' Q
- Return the mantissa and exponent of x, as pair (m, e)., }1 x, Z- l" I7 L
- m is a float and e is an int, such that x = m * 2.**e.5 A1 t- O- ]$ X
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.' {7 K( \( _& ~) k3 J
- >>> math.frexp(10)
/ z" R7 e1 [5 h' X" ~3 O6 Z4 h - (0.625, 4)
' E$ x# q3 W, g) t - >>> math.frexp(75)4 Y0 i& a% R5 s4 S# S
- (0.5859375, 7)
) W/ U) [2 m% R) a0 Y5 h& Y: I - >>> math.frexp(-40)
- q+ g4 Y. I/ _2 l; g" S! ~ - (-0.625, 6)) R7 g$ m3 A6 C6 B- r1 d
- >>> math.frexp(-100)
9 J! v. U* Q: ?- X: o( {8 v - (-0.78125, 7)
. w4 V) k5 h1 _. z: s0 p - >>> math.frexp(100)
" g3 h9 y, ?# _1 j. c - (0.78125, 7)
复制代码
! Z: L, d8 |+ _/ U2 V# X4 D! omath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& Z( [$ X' m) n1 _2 ?# H- t- #对迭代器里的每个元素进行求和操作
- \4 S- ~. K+ c- t! C3 U2 E - fsum(iterable)
- c2 H* m9 }2 `" x, M4 Q( S - Return an accurate floating point sum of values in the iterable.
4 Q2 L5 t. w1 d; r. t' f - Assumes IEEE-754 floating point arithmetic.; Z) \6 I& H( n+ z3 O) I, U
- >>> math.fsum([1,2,3,4])
; ]( a+ B8 p9 N# g+ x5 n - 10.0; M7 }1 o1 m/ G
- >>> math.fsum((1,2,3,4))3 j5 {; V$ \- k L
- 10.0
/ Z& q# O8 \+ v, R& O1 a) f5 v - >>> math.fsum((-1,-2,-3,-4))
4 _; _7 C, R' B6 m4 o - -10.07 J* ?! E$ ~8 w5 n
- >>> math.fsum([-1,-2,-3,-4]). ]7 @3 I0 I; A" t2 |* Z0 M2 I
- -10.0
复制代码 7 t5 B+ L% S+ {. p8 f2 h
math.gcd(x,y) 返回x和y的最大公约数- y2 K: R1 w3 U( |- {
- #返回x和y的最大公约数
' z9 \9 @6 ^* L7 v' ]2 b - gcd(x, y) -> int
3 L: I5 w: ^& Q' K/ U/ _ - greatest common divisor of x and y
2 U6 a0 Y& g9 Z - >>> math.gcd(8,6)
5 S: [" ?% [. }) ]! S6 ?5 }3 l8 N% G' t - 2: k/ F6 f1 Z, u, _3 n) k
- >>> math.gcd(40,20)# u# S1 V# B0 u# ^) [6 a
- 200 _/ d% l8 Q8 R( y& ]! a; g
- >>> math.gcd(8,12)" S! Z! k/ t9 X1 P7 p9 j8 b+ m% ?
- 4
复制代码
; ]3 g7 }% V; Q% Z2 nmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 X' @. t7 P3 X' ^1 ^+ n* _- #得到(x**2+y**2),平方的值
' B1 V4 ?/ z/ q& T - hypot(x, y)
" v* w u) q' N - Return the Euclidean distance, sqrt(x*x + y*y).
2 _6 Y3 c: S, p9 t [ - >>> math.hypot(3,4)
2 P0 k" p4 d, d: \2 k: t - 5.0
* i! h( `- j/ U; p6 P, x5 Y: Z - >>> math.hypot(6,8)- |; O6 N, R* D
- 10.0
复制代码 # W+ ^2 @- s( z; G
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False0 R' Q" L( }/ q( I$ K# t
- #如果x是不是无穷大的数字,则返回True,否则返回False
9 k/ M8 O! J% i9 J - isfinite(x) -> bool
7 p, p7 r! t* U( N - Return True if x is neither an infinity nor a NaN, and False otherwise.8 F- x! m% a( x/ Q4 _
- >>> math.isfinite(100)& K0 h) J* k2 X7 C e/ J( f0 Q* \
- True
% H6 w5 C- M& W2 m8 o; u$ e - >>> math.isfinite(0)$ ?4 ^# L) i/ \
- True. T5 P; q, r0 [! `+ M
- >>> math.isfinite(0.1)
2 T, P: i9 |4 {) k# B4 h* b - True
, u; g' X) O& E% r! Y8 [2 z; U0 Y7 u2 h - >>> math.isfinite("a")
7 z# r% l1 Y( d$ i% x0 d - >>> math.isfinite(0.0001)
* I& F4 |# m# m' e! q4 W - True
复制代码
7 K: I6 H* z9 c# f, nmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
& n: V6 A! X6 y. q6 @, P9 a- #如果x是正无穷大或负无穷大,则返回True,否则返回False
( t) ?9 }7 M* U9 P0 j' D- B( Z2 E - isinf(x) -> bool
# F" v2 K9 Y2 E6 \- b- H - Return True if x is a positive or negative infinity, and False otherwise.6 a5 A+ ]6 @! H9 x
- >>> math.isinf(234)8 w. a# W& @, z- Z7 m0 s7 Y S
- False
$ O& Q) ], j: x$ h - >>> math.isinf(0.1)8 z- M4 ^0 l) J$ F
- False
复制代码 2 d3 u9 n, V8 p6 H1 d
math.isnan(x) 如果x不是数字True,否则返回False$ m# y3 {& O+ ]8 j, M$ Q4 _& e0 e; p
- #如果x不是数字True,否则返回False3 O: e$ P& j- Y3 H* n/ w/ |! W
- isnan(x) -> bool
1 J8 s0 i, k4 ~1 x4 O - Return True if x is a NaN (not a number), and False otherwise.' H5 D7 M) j( [- g1 U6 W4 _! Q& m+ M
- >>> math.isnan(23)
0 U: ~# x. k! ?6 k7 v0 a: F4 u+ h - False
/ r0 ?3 H! c( ~& w' O( b# ] - >>> math.isnan(0.01)
6 M1 @% e5 |) u- W, V3 i1 r" F - False
复制代码 , X2 |4 C& H- J% d
math.ldexp(x,i) 返回x*(2**i)的值1 |8 z$ R! U; H
- #返回x*(2**i)的值8 g2 B0 q7 g7 A" q* u, l' M
- ldexp(x, i)6 h) x8 i- X5 m6 ?
- Return x * (2**i).
3 W# a% T. M) j& ? - >>> math.ldexp(5,5)8 M0 T4 ~$ q% D; S8 [ C( ^4 \
- 160.0& c0 K0 U" Y# \- G* S
- >>> math.ldexp(3,5) c# T+ B1 x' D) U
- 96.0
复制代码
' b, Q; I' b3 h, Z+ {math.log10(x) 返回x的以10为底的对数
9 {7 @% {: R, t& X; t- #返回x的以10为底的对数
& ?; \$ G# X; }/ `' e - log10(x)
/ ]# ^' p$ W" `$ Z& Y* R - Return the base 10 logarithm of x.
# F4 f5 H6 |" g" ]8 C - >>> math.log10(10)
4 K6 n2 A: w1 r5 |2 a( Z6 ` - 1.0
7 g/ I8 ]' P, J; Y7 j& S - >>> math.log10(100): B8 i0 `* L5 W. @
- 2.0$ e, h7 ~5 ?6 q. L2 F
- #即10的1.3次方的结果为20/ W4 G6 W$ t6 y
- >>> math.log10(20)( H0 I* U3 J# \* n' O2 r
- 1.3010299956639813
复制代码 ' J `+ i" u: q6 }4 L% R
math.log1p(x) 返回x+1的自然对数(基数为e)的值' |# W9 Y3 \* K p- S5 F6 ?' q! \$ b
- #返回x+1的自然对数(基数为e)的值
+ V6 a* p7 r. g8 o) A; G" E - log1p(x)
/ M1 o( S! P9 w. Y6 b5 X. Q - Return the natural logarithm of 1+x (base e).
) n7 S7 \2 _% x' M& D$ \0 L - The result is computed in a way which is accurate for x near zero.5 N. k, R) a/ K# c0 I5 O4 h
- >>> math.log(10)
) K4 ^2 ?# D2 {' Q4 d0 C - 2.3025850929940465 W+ s0 T& ?7 h7 c2 s3 E4 k& P: B
- >>> math.log1p(10)8 h. j2 q8 a# i# X
- 2.3978952727983707
& \3 L) P) A0 B( S9 _ - >>> math.log(11). Y! R7 S0 W; F4 G1 _" D% X! N
- 2.3978952727983707
复制代码 N6 p# G( b3 H+ L. B
math.log2(x) 返回x的基2对数7 I6 ^: ]' e7 i
- #返回x的基2对数
( D: V9 N5 c1 h J/ f( ^# P: H& W2 l - log2(x)
+ y* l7 J$ z2 u1 Y8 @ - Return the base 2 logarithm of x.
* y. f9 p! ~* _( i( f% ^ - >>> math.log2(32)& y& ?7 f9 p: m x, n
- 5.0
( I$ Y7 ~& s/ n! x X - >>> math.log2(20)8 C: Z E' R+ T* T7 d
- 4.321928094887363% d. m/ v0 n1 [/ x* [0 p
- >>> math.log2(16)- W) N* B5 z T& M% y8 F4 I q; Y
- 4.0
复制代码 1 Z2 b" b3 G+ _$ }3 P r) e! S
math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ y1 p2 S/ D. b4 N* j% a7 _- #返回由x的小数部分和整数部分组成的元组
- c5 ]: x" u7 ]/ K I- A# C7 y - modf(x)* u) z" s" L6 M; G; g! P; y5 g
- Return the fractional and integer parts of x. Both results carry the sign1 u* B+ Q9 s( w3 T4 j/ ~9 X6 E7 `
- of x and are floats.
, W; I, @( t0 P5 E" w - >>> math.modf(math.pi)7 h q& z& r: w1 p7 N% K
- (0.14159265358979312, 3.0)7 L* b- c- S# B: B
- >>> math.modf(12.34)9 E: r( ?3 B- b m9 r
- (0.33999999999999986, 12.0)
复制代码 " q+ b: y4 t9 v9 a+ h5 B
math.sqrt(x) 求x的平方根
: [ x% R' \4 g! O5 F5 T- #求x的平方根* f+ V: \- k4 r" k& A, B
- sqrt(x)/ t: b2 B8 b2 D* }8 |
- Return the square root of x.) B* a- u5 G# ]9 H: A- R+ }
- >>> math.sqrt(100)2 G+ G) I `" ?2 I% k4 G4 r: C% |
- 10.0
5 N D D1 T9 ?% O - >>> math.sqrt(16)1 I( N* \* ^0 g+ h! L: H( _- S2 Z
- 4.0
3 U! x5 S7 V7 x: L# { L7 U- G - >>> math.sqrt(20)
# y$ U6 l+ H; M2 { i V3 M, J - 4.47213595499958
复制代码
1 u( B3 h$ I1 E5 |7 P, Pmath.trunc(x) 返回x的整数部分- #返回x的整数部分
1 X7 G' b. {3 P - trunc(x:Real) -> Integral
- d3 @- g" S& r l- i5 p, j - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# b9 x9 W" a0 j5 c+ m - >>> math.trunc(6.789)9 d y6 L4 j7 m7 r$ R
- 67 G4 }+ S" ]) Z7 }1 H3 u
- >>> math.trunc(math.pi)
1 G9 e7 z2 q+ Z - 3
: u+ \. @2 m' h$ R - >>> math.trunc(2.567)% O! n0 w; }/ s0 D2 B N" t
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|