马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- @. q0 K1 N) o/ z( H- @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
4 X! k7 x9 |+ C6 D B$ |9 _* G& h* Q9 U3 i5 V2 e$ i0 A
方法1:
- R' w, C- T+ \3 I7 l% ]6 h- >>> import math
& n1 b- s( o, v+ A$ _# g+ ^9 T. U/ ^ - >>> math.sqrt(9)
. f1 n& J$ j( ]; h& a& x* @5 @ - 3.0
复制代码 方法2:
\8 L/ c6 i) V" Q. ~' a3 x- >>> from math import sqrt# _/ c; C4 y3 j
- >>> sqrt(9)
2 W9 ?* h9 \6 u0 \4 S) N0 C* e' o: Z - 3.0
复制代码 . u; c: a; ]: A3 z2 C* s+ D/ z
7 z a4 ^. f0 v- g
math.e 表示一个常量7 h1 }8 L8 [% a9 Q/ _) J
- #表示一个常量
( [" s3 r1 ]9 h$ o- F5 a* ?- y1 }( k - >>> math.e
. C& Z O3 O& I - 2.718281828459045
复制代码
% F' M6 F9 A0 _7 t3 O; Emath.pi 数字常量,圆周率
$ R# t: [" i J0 g1 g" V* ]- #数字常量,圆周率
; o7 z9 p5 e/ C - >>> print(math.pi)' h2 k! \8 x( @# X
- 3.141592653589793
复制代码 ! _$ U% S" w* ~" t
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
, {% U0 i% h$ w8 c' @" o- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
* @0 w G/ x8 J, @- r* J" I' ~4 _ - ceil(x)
7 }0 A8 u+ [9 o8 _ O; T0 z( p$ g - Return the ceiling of x as an int.& Y8 Y, ]7 j: l& x. H
- This is the smallest integral value >= x.) \0 _# l6 S5 ~ d: ?8 \
2 {1 ]: Y6 t% a9 X, I/ ^- >>> math.ceil(4.01)
. V/ h" a! ?. Q - 5
& X4 z+ V9 I) `/ c; N4 V. C) F - >>> math.ceil(4.99)
2 |1 {9 X* G( s - 5+ n* |6 `( ]4 s3 w/ m3 c1 z
- >>> math.ceil(-3.99)
, m1 h( p I9 `9 U- i- t/ K - -3
6 M4 f9 m: b4 L. C; r - >>> math.ceil(-3.01)% c, f8 H# n/ t6 ]
- -3
复制代码 1 z( G/ c/ X! F1 f$ y
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) H! K/ i6 [' I8 H1 a* D- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
y0 e {+ i$ e& Y0 E - floor(x)/ X9 F/ r( ~0 M
- Return the floor of x as an int.
3 F5 P& K, G( i. Y* d - This is the largest integral value <= x.4 J: {! k; ~2 b) c9 B* D0 ~1 @
- >>> math.floor(4.1)
7 d" y' j. b/ |, R, Y - 42 H& p8 E( t2 w8 \! L: z
- >>> math.floor(4.999)/ d S7 i/ j) x' ?
- 45 w: B0 K; C, I+ R J# W9 ?% \
- >>> math.floor(-4.999): b7 P+ f# m D: K! _; g$ B
- -5$ G2 R, _% B) J4 R
- >>> math.floor(-4.01)3 ]& m: G( x, y! H8 ^8 ^: f, E
- -5
复制代码
# F! @5 ^0 C7 h F8 M/ y$ [math.pow(x,y) 返回x的y次方,即x**y. @$ U2 u: E+ O8 w @! s# Y* l
- #返回x的y次方,即x**y! Y% ?4 ~* g/ V8 R! l
- pow(x, y)
0 M7 o- t, R7 A4 ~3 b& p8 V8 Q - Return x**y (x to the power of y).. @+ T. Z1 s4 h- q* O/ T
- >>> math.pow(3,4); f4 q4 D5 q% A4 P' J
- 81.04 E% r: k1 Z" [7 K( b- I* y
- >>> 5 S% n5 E" S/ q- R# x
- >>> math.pow(2,7)6 a# e" m e' y1 v
- 128.0
复制代码 2 ^0 S# }, d* Q' m$ v" Y$ N
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
* \/ P6 y; N0 h6 @- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: B* @+ j7 x* n& q. m - log(x[, base])
. K' s: ^. h$ f# U/ C2 J - Return the logarithm of x to the given base.
0 u; Y1 \- P9 S' ]( F - If the base not specified, returns the natural logarithm (base e) of x./ y% f+ f% H/ I: o6 O" |4 @. v
- >>> math.log(10)3 G$ d' E; p9 p0 H
- 2.302585092994046
9 Y1 F ?7 k/ k/ r$ p6 H4 |9 F; S - >>> math.log(11)4 B% s) w- T0 C4 H s& Y
- 2.3978952727983707" r$ M5 A" k: z1 T2 c1 f5 D
- >>> math.log(20); o5 e' ]' p& ]9 W, X* T; O
- 2.995732273553991
复制代码 5 E* v. N, l! o: k
math.sin(x) 求x(x为弧度)的正弦值7 o5 Z9 R: n# G
- #求x(x为弧度)的正弦值8 K4 m& |, H- ~+ f. [
- sin(x)0 r0 J F; t, l% t
- Return the sine of x (measured in radians).
; k$ Z. W) r0 w0 R+ o! U0 `. @. p7 o$ F - >>> math.sin(math.pi/4)
# s+ A" i/ r6 a" y$ q7 @ - 0.7071067811865475, ^. _9 M' V; ?: E/ Y1 b
- >>> math.sin(math.pi/2)% p8 q0 H0 n1 {7 z6 }
- 1.0
! N0 ] f( o! ~4 Z. K& |) Y - >>> math.sin(math.pi/3)9 c+ w; L9 a& _1 [7 a8 J( ?
- 0.8660254037844386
复制代码
% ~% |& ~. ]! fmath.cos(x) 求x的余弦,x必须是弧度
- c; {. F! `' j( J) _- #求x的余弦,x必须是弧度9 a/ J3 L. ], _ g6 }9 v! ~. ]$ l
- cos(x)
0 i/ u6 k2 \3 }7 |) `2 ]5 q! Z - Return the cosine of x (measured in radians).4 K5 [, @* E0 X4 l0 L) M, J
- #math.pi/4表示弧度,转换成角度为45度
0 D" P: s7 F |5 t$ P - >>> math.cos(math.pi/4)
1 h. m3 W: m" ~' y- k - 0.7071067811865476
6 `! v+ z+ `, D O - math.pi/3表示弧度,转换成角度为60度
5 K( a9 k% a. V - >>> math.cos(math.pi/3)
$ l% _; G, c: ^; e1 o9 E - 0.5000000000000001
% l0 u' e6 z/ K( H, j* d - math.pi/6表示弧度,转换成角度为30度) A, A S0 d, o
- >>> math.cos(math.pi/6)7 f% P% a" ]; M* C' p8 p
- 0.8660254037844387
复制代码
: }2 |( r1 m% w3 L" l: {& n! {! Mmath.tan(x) 返回x(x为弧度)的正切值
/ X1 J8 {1 H5 b* c% ^* V9 E- #返回x(x为弧度)的正切值
: U9 O. | H" L& T \ - tan(x)
1 |/ K( M$ v! V. u3 C - Return the tangent of x (measured in radians).
# I7 n P: o. E# G - >>> math.tan(math.pi/4)
, v! X: ?- g& N8 s A - 0.99999999999999998 {0 i, E5 [1 P/ r9 _, ^2 E
- >>> math.tan(math.pi/6)( Y) t# x1 p1 |5 E
- 0.5773502691896257+ X3 q! }; H; D+ f
- >>> math.tan(math.pi/3)
' Y: c+ o' E+ a D- T, G2 q - 1.7320508075688767
复制代码 + _- g* P& P: z' \/ N% h
math.degrees(x) 把x从弧度转换成角度1 {/ B, u# V% X
- #把x从弧度转换成角度
' T* ` |# }$ o+ O( T' d - degrees(x)
) ?9 }& A0 }5 ~ - Convert angle x from radians to degrees.5 t7 R, z; [' s, q P! D
- $ @2 |$ x T0 A8 f: z
- >>> math.degrees(math.pi/4)' v/ \# h: i6 r3 e
- 45.02 q/ j) _0 G( h! k: g( \
- >>> math.degrees(math.pi); t+ v* M4 c: n: v9 e5 B0 u
- 180.0
~/ \ ~9 o, J/ B - >>> math.degrees(math.pi/6)/ }) G- s; U$ z# c( A
- 29.999999999999996+ Y0 k7 C; k' C
- >>> math.degrees(math.pi/3)
; y5 W8 L7 L+ G) y - 59.99999999999999
复制代码
/ Y4 C7 t) k: g" umath.radians(x) 把角度x转换成弧度
! r2 z! ]# Z G$ r- #把角度x转换成弧度5 \) G3 M7 E* B, r4 r
- radians(x)! n/ n5 J/ e( h+ q' `
- Convert angle x from degrees to radians.* n) B- A: y( j) d/ B0 S
- >>> math.radians(45)
' L6 V ^# G/ H: V$ h3 [ - 0.7853981633974483
+ S- r2 J: i! c! B - >>> math.radians(60)
: |8 R% {7 t: E, P3 b" V) B - 1.0471975511965976
复制代码
7 X9 z' ?2 m7 H& D1 F& l9 bmath.copysign(x,y) 把y的正负号加到x前面,可以使用0/ G0 D* [6 d7 R9 Q+ t
- #把y的正负号加到x前面,可以使用0
! K# r) v% J0 g2 \* R M9 p" j: L - copysign(x, y)
9 x- H: [. s g- n! J/ E - Return a float with the magnitude (absolute value) of x but the sign $ G- g3 \8 D! L8 d+ f! n' w+ a
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & y8 V( j2 V7 \/ b+ F/ {
- returns -1.0.; l; `& I6 D0 }# R+ i
# V7 J; c' M, F2 d- >>> math.copysign(2,3)
( w& e. w8 l% k; r% N - 2.0- y& }0 ?- ]6 V1 e3 v5 u
- >>> math.copysign(2,-3)) p7 X& L% w @ e6 H @
- -2.0! Z. ~0 [; I( J* U
- >>> math.copysign(3,8)
# k* k8 @# \/ e7 y( G - 3.05 |! ]) i. L1 X
- >>> math.copysign(3,-8)
- x+ o. i5 w @7 k - -3.0
复制代码
' A8 F5 U- B" v: ^' C5 ~math.exp(x) 返回math.e,也就是2.71828的x次方
- D4 R1 s, D4 f: u# P+ H& L) x- #返回math.e,也就是2.71828的x次方
( C. I: x: M; R* X - exp(x) @8 j* B/ ^( z
- Return e raised to the power of x." B0 g# J! M8 _9 t3 l& W; {
- % h1 U* e8 c$ r4 P3 R9 x! @
- >>> math.exp(1)
& Z8 b) d y1 J8 D$ e- W" Y - 2.718281828459045
$ ]3 Y0 Z1 N) a& u6 L - >>> math.exp(2)0 ~% G5 i3 o: i% x8 w) {
- 7.38905609893065
, ~0 m0 a, ?. s, X- M& S* g - >>> math.exp(3)/ I5 J. W5 o7 |$ j# |
- 20.085536923187668
复制代码
& k0 Q7 Q; O) m: Dmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( w' A0 f! E% M7 k* W! n- [
- #返回math.e的x(其值为2.71828)次方的值减1, l( V& l* n( J* e& b) J
- expm1(x)
: e2 L: y! i* {: ` - Return exp(x)-1." n! w' @: f7 X
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
7 C9 \, z, ?' ~
9 \2 m. Z' f& q! u- >>> math.expm1(1)# M' |* g0 G7 L# N5 Y0 k6 {4 r
- 1.718281828459045
/ s1 e6 s% Y8 ]+ l# I - >>> math.expm1(2)2 V% x/ c2 O. C$ z7 Y1 d
- 6.389056098930652 r( Q: v! Z8 S x+ ?, D
- >>> math.expm1(3)
8 }8 ?1 B7 _: i# W+ V+ K5 p, R - 19.085536923187668
复制代码 " G# T/ q% R* a
math.fabs(x) 返回x的绝对值
0 h1 g0 H( a" g) A- S- o- #返回x的绝对值7 y% N0 {- ], N
- fabs(x)8 G+ y" q% J. ~- |3 Q' i3 S. P
- Return the absolute value of the float x.
# \. C- U4 W/ n5 s* ^2 p
! f' B% h9 ]7 @! Z4 Y- >>> math.fabs(-0.003)
" J3 j+ J2 s9 X+ I - 0.003
* F% r" D4 W) Z. u/ `9 ` - >>> math.fabs(-110)
5 [: X+ `( Y) T+ s5 F% y$ v - 110.01 G; @" P9 U% {- Z1 c) b
- >>> math.fabs(100)
3 S: ?9 B% U% @$ J - 100.0
复制代码 7 Y% e! k( i: f6 L% c
math.factorial(x) 取x的阶乘的值
& Q1 }2 d5 s$ O# T7 ?# k, ~2 f% ~- #取x的阶乘的值
$ Z$ \+ J3 X1 z; W3 L1 E3 C - factorial(x) -> Integral
! a) x" D- F Z' \ - Find x!. Raise a ValueError if x is negative or non-integral.
6 {/ B! G7 K9 g9 d% r - >>> math.factorial(1)4 }5 V* v5 u: r
- 17 k8 @ R, f: ]2 @2 ~9 ?
- >>> math.factorial(2)& O& p3 T: P3 |% `& x
- 2
, n9 D C& G% B5 Q! \' d - >>> math.factorial(3)
" `: e! H8 C. r! t" w: x - 6. o# U8 w, c# @- x
- >>> math.factorial(5)
* T: S2 m& x! g+ W5 O0 n% l+ y - 120
4 H/ I) r# \# L4 E( e - >>> math.factorial(10)
# |7 ~6 `, R" Y E. x8 _ - 3628800
复制代码
* D. H2 r5 l) |0 D& k2 N1 xmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数+ p$ i% c) Q! d# {% v1 z: k% \
- #得到x/y的余数,其值是一个浮点数
* }9 V2 c; }3 N - fmod(x, y)0 ^7 v1 v2 ]/ V
- Return fmod(x, y), according to platform C. x % y may differ.
) }& Z6 M/ A" E2 q" u3 N8 n, F$ C - >>> math.fmod(20,3)
0 f: o4 d) T$ r/ y( `8 K- m - 2.0
8 ?9 ]# V7 @1 {2 L9 V- q - >>> math.fmod(20,7)
7 H& q( D5 J: \! W0 { - 6.0
复制代码 9 D$ L) o9 U) n# L# h8 x: \% Y2 T* {
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2 C: \1 G' @ A9 N0 z* K! _2 P- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, v) \, i m7 Q
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
6 y6 C1 \, m9 A6 r8 g - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
/ U2 B: Q/ W# M/ h [ - frexp(x)# _# b0 p. d. ?' C! A
- Return the mantissa and exponent of x, as pair (m, e).
2 ], u3 k% j; F* [ - m is a float and e is an int, such that x = m * 2.**e.
+ y; _8 w7 b5 c) X- L3 _9 b" f - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 Q% e4 s( c/ `9 l
- >>> math.frexp(10)
, h! ?" |3 i% J$ z# m! [# M; C0 R- F5 P - (0.625, 4)/ M+ }& m" `+ n" p! A/ ~
- >>> math.frexp(75)3 I2 B4 z5 O3 @5 a
- (0.5859375, 7)5 l' \# X6 D% L
- >>> math.frexp(-40)
# x1 F& o! `% y$ w1 ? - (-0.625, 6)
. o" @' ]8 w, E- p1 Z - >>> math.frexp(-100)' T$ I% m) V) t
- (-0.78125, 7)
. _+ D4 y) G* N) ~' _ - >>> math.frexp(100)
~" s/ |; E' n( a- o/ k( H4 [ - (0.78125, 7)
复制代码
9 C' ?: d- o$ \; u2 {math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)0 q: R+ [+ k9 ?( v; S+ E% F
- #对迭代器里的每个元素进行求和操作( a- i n. q( I/ l. G4 C1 T
- fsum(iterable)# B: q" C( D1 X
- Return an accurate floating point sum of values in the iterable.% k" M4 c. a3 `0 ]* z* D2 u
- Assumes IEEE-754 floating point arithmetic.4 ^2 s3 E _; Q
- >>> math.fsum([1,2,3,4])
* w; C) G1 R, e* ^ - 10.0+ p) e6 e6 S: a3 f+ R4 n
- >>> math.fsum((1,2,3,4))
9 L; S+ [6 x2 D; q - 10.0# R) w9 l2 n+ i% n v" h4 V% T
- >>> math.fsum((-1,-2,-3,-4))
' b6 T8 @1 D! ? - -10.08 I3 ^$ y7 U2 w; ?( X
- >>> math.fsum([-1,-2,-3,-4])+ @1 b- d) F( D' X% y
- -10.0
复制代码 & I: t# O7 X, D% t* ^1 [! q
math.gcd(x,y) 返回x和y的最大公约数
$ g5 ^: Y) Z2 n" r7 I- #返回x和y的最大公约数
0 m! ^ k- L( Y3 U h - gcd(x, y) -> int
1 Z, z2 F$ h5 a# V! d: |9 x - greatest common divisor of x and y( m! _" n! S( Z
- >>> math.gcd(8,6)
4 z9 B. L& i, b- Y- Z7 X - 2' h O, S8 G4 S. C6 V
- >>> math.gcd(40,20)
( J0 G" A" t3 }" r - 20
$ p8 x% L& z: [! @: i6 _3 g) d - >>> math.gcd(8,12)& d, J% U9 ~ F, V1 [* z$ a
- 4
复制代码
# ] S' t, _4 D$ z3 v" m7 F& rmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
8 ?4 t) j7 m1 I$ ~2 S9 }/ ~- #得到(x**2+y**2),平方的值
6 D6 D+ y5 f- E* k! J! D - hypot(x, y)
& x, i" B# ]6 q4 A2 C$ f. X. A0 G - Return the Euclidean distance, sqrt(x*x + y*y).
0 x5 X: r. Z6 i% _5 ? - >>> math.hypot(3,4)
6 L2 z, w) B0 L$ W - 5.09 P$ x2 r l2 m/ n" d9 Q1 u! Y4 ?
- >>> math.hypot(6,8)
- t/ y" m. k1 j - 10.0
复制代码
; t1 _+ J" l3 G G Tmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 y+ g" c4 k3 K, W& ~- #如果x是不是无穷大的数字,则返回True,否则返回False
3 A6 I4 _4 q; x P4 y - isfinite(x) -> bool
* z2 e- N. [7 U& S - Return True if x is neither an infinity nor a NaN, and False otherwise.1 _ u4 `4 ^. ]5 V. a
- >>> math.isfinite(100)
+ A- w1 t. p o& b% Z8 P - True
. _5 l7 m, H4 O" D - >>> math.isfinite(0), z$ s2 h7 y+ t
- True$ v z3 K/ s* l( ]' ~( k
- >>> math.isfinite(0.1); \. ^7 d: m0 ~$ q! }
- True
d" O: T0 b2 G ? B3 k - >>> math.isfinite("a")# ~6 { U: ~" ~( }5 w% Z
- >>> math.isfinite(0.0001)
# ]- U( r' ] V" U8 { - True
复制代码
5 V8 @" y/ F/ G+ R5 }math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
; T' _$ \ P, r" J: m) Z g: V- #如果x是正无穷大或负无穷大,则返回True,否则返回False
! O7 h; o( t) Y. M! k! S" C - isinf(x) -> bool# @2 t, R0 }+ n! p8 s
- Return True if x is a positive or negative infinity, and False otherwise.
' o# ^/ H- n) n* ^: c8 ~, b: S - >>> math.isinf(234)
( `5 J7 i; L @, s$ q* q - False4 n0 U, i/ `- b6 I" g
- >>> math.isinf(0.1)
# U5 @1 T- i1 r) ` - False
复制代码 8 m/ {. V5 e" p/ g( X5 e
math.isnan(x) 如果x不是数字True,否则返回False
& ]3 S3 v3 @! `6 g* U3 |+ c/ x' {- #如果x不是数字True,否则返回False
' l, L; R8 @+ }) n3 z0 z- i% I - isnan(x) -> bool
, K' h# Z, B2 d/ B* } - Return True if x is a NaN (not a number), and False otherwise.
2 D6 H2 A1 R- q; q/ b - >>> math.isnan(23)
' E- u" t: m; j( g - False- J9 G- w% h( ?, ]) f% P
- >>> math.isnan(0.01)
! @. ]* O+ o: { - False
复制代码
! k2 z% ]7 f% u* {) k( P' q0 l* F$ Nmath.ldexp(x,i) 返回x*(2**i)的值7 w1 i+ U' C3 V: Q
- #返回x*(2**i)的值
@3 Q' e7 G2 T - ldexp(x, i)
: j* e+ K9 g2 h* g( k - Return x * (2**i).9 [7 e y/ q! r4 [. R
- >>> math.ldexp(5,5), @$ X6 ]) z9 H% L) U* V
- 160.0( ?) R4 a9 m0 |5 ?+ k
- >>> math.ldexp(3,5)
6 g; {) q3 F) _, r0 {7 B - 96.0
复制代码 c* Q0 y0 Y6 O
math.log10(x) 返回x的以10为底的对数+ e$ @1 v8 ?) g7 D0 ]! a! r
- #返回x的以10为底的对数
7 a! K$ ]+ a! W1 _/ s - log10(x)8 h: I L F o/ \. A' K. L. V8 a
- Return the base 10 logarithm of x.& o* h; ~0 Y# q T4 b) }; V/ S
- >>> math.log10(10)( E; y, N& W1 h* ^, ^3 V$ f8 z
- 1.0- Z0 O2 y" T6 G$ H
- >>> math.log10(100)
* Z Q9 t }, C' [+ E - 2.08 p0 L2 D) a! |3 q# M5 p; E/ m
- #即10的1.3次方的结果为20$ u5 z; K% s% n7 W5 B: G2 w
- >>> math.log10(20)+ b, s# U3 ~! s3 v% A
- 1.3010299956639813
复制代码 8 I1 k" K0 E% b; n" }& F
math.log1p(x) 返回x+1的自然对数(基数为e)的值7 I0 Y) r, S7 w( o& f! P
- #返回x+1的自然对数(基数为e)的值7 f! N& K8 h% a' [0 D" ?' }% g
- log1p(x)
; P5 Q/ h/ I, _7 I8 C2 u - Return the natural logarithm of 1+x (base e).
3 c2 z+ z, ?, }6 w* ` - The result is computed in a way which is accurate for x near zero.
0 ^3 [# p" u+ \9 y) F - >>> math.log(10)
9 E# m9 v; l6 n6 o - 2.3025850929940464 |0 G" @/ c* @ l; `
- >>> math.log1p(10): F% m, T0 G* P' z" P% Z, A
- 2.39789527279837075 G' Z; Q- t3 G: s& j$ @
- >>> math.log(11)
6 a% j4 m% D, u. t - 2.3978952727983707
复制代码 + R0 V) ~ T7 f: G- A6 s
math.log2(x) 返回x的基2对数$ X# C' k' ]# |( ^* b9 d& v
- #返回x的基2对数
) P/ ]- k! f/ m* v+ h - log2(x)
, r( O* I" G5 R/ R - Return the base 2 logarithm of x.; G) [/ K2 R. \9 o: e7 ?
- >>> math.log2(32)
7 y; {7 U- C6 N- c$ [& D - 5.0' Y* d& X! t% E Q" c4 Z( a7 h
- >>> math.log2(20)
4 ^& w+ A; S7 `& t - 4.3219280948873637 U' _- p/ [/ `) [) r
- >>> math.log2(16): g$ M) z' q% Y6 z6 u% i e
- 4.0
复制代码
9 o3 {" R# y! F& G1 h6 Omath.modf(x) 返回由x的小数部分和整数部分组成的元组) h' x8 A0 L+ O2 q1 d4 [
- #返回由x的小数部分和整数部分组成的元组; u: j, C2 N' n) _2 g
- modf(x)+ ~+ p- n" z. E4 Y' l- Z
- Return the fractional and integer parts of x. Both results carry the sign4 s1 q' F2 ~4 l" _8 S2 P* l
- of x and are floats.8 h; q% v; l6 P4 l# A# k
- >>> math.modf(math.pi)
" ` Y( U; x: {) E9 _ - (0.14159265358979312, 3.0)1 g4 m1 H7 e/ z$ L+ Z3 S
- >>> math.modf(12.34)
$ y* V8 s4 ^: k6 J+ w - (0.33999999999999986, 12.0)
复制代码 & z+ h$ A* [# R9 y
math.sqrt(x) 求x的平方根
8 G# K# E" C0 @5 T8 b- #求x的平方根# m& q" R; P8 C, @
- sqrt(x)
8 d: n! Q5 A1 x+ l2 ^, O - Return the square root of x.
5 E+ {, v6 b7 p2 i" W - >>> math.sqrt(100)# S: k7 i- l/ v" N8 _4 e
- 10.03 T% @6 _1 H# u6 l5 q( X4 |
- >>> math.sqrt(16)4 ]: J! G: N0 @
- 4.0
2 {, z" t3 R3 y6 n) p F* g C" O1 \ - >>> math.sqrt(20)" e8 q2 l! b" P3 a
- 4.47213595499958
复制代码 / u' Q1 P6 m9 G! a" H9 K! u
math.trunc(x) 返回x的整数部分- #返回x的整数部分3 @, n+ l$ g+ R9 F$ c$ K! E- \6 G
- trunc(x:Real) -> Integral
) L7 d. g! \! u1 e C& x - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
& o; _. d. t# u: b% m7 @ - >>> math.trunc(6.789)
' j% z0 I* w( u) i# Z - 6$ F$ S9 s6 m; a, B" ]5 p* t
- >>> math.trunc(math.pi)3 X8 d2 B# f* ?& K
- 3& a2 I3 P9 C$ y' p2 T
- >>> math.trunc(2.567) k2 L( h" x" y* [
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|