马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ |' m/ H; [3 J# E+ Q6 b
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 r0 e% Q1 ?/ j$ O2 }. R& Y" B
" M9 H& a [, C8 t- J, ]
方法1:
# {3 a8 H, n2 Q- >>> import math
# {6 v3 Q7 ^$ I- S# h7 a* p - >>> math.sqrt(9)
7 i5 @1 b. t3 F; V( { - 3.0
复制代码 方法2:7 m6 a; J) ~; l( t' U# ^6 X
- >>> from math import sqrt- `+ F# Q, s, i/ `) x
- >>> sqrt(9)8 l! Z" y1 j$ ~3 s& m
- 3.0
复制代码 3 F9 ], H0 b, a0 F% i2 m
/ |2 i" p P3 w+ `8 M* b1 W
math.e 表示一个常量
# S% C- O# J# z, |( u+ p; \% O) q- ^; b- #表示一个常量% ?* h" \4 H* E) e4 P& t0 E
- >>> math.e: d% d* B- ?9 _* B8 m
- 2.718281828459045
复制代码
7 h! l8 g1 k7 r' z+ bmath.pi 数字常量,圆周率" ]# T% s6 p F- h- M- n# g
- #数字常量,圆周率' Z. F% {2 F' e: a7 q9 l/ ?2 w
- >>> print(math.pi)/ X0 s$ g j) f* d8 f, l8 O& J$ H
- 3.141592653589793
复制代码
1 k8 I9 A* v- G0 w9 mmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
" S* I1 B) j7 X! t0 a- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ a8 p$ ]8 P4 k: Y! T - ceil(x)6 v+ f8 n) m+ q
- Return the ceiling of x as an int.
: c X$ ]' J" W - This is the smallest integral value >= x.
2 A/ e- x) B2 o. L+ E - $ E+ f& z0 M- ^2 p6 X
- >>> math.ceil(4.01)0 A4 W, y- f. F4 \7 l1 q
- 5
5 D2 H0 v* T; L9 P+ a$ \ - >>> math.ceil(4.99)
+ H7 L3 u5 F- X. C: [. Q6 L - 5: J5 e. ]+ g: P7 e3 l: s9 ?
- >>> math.ceil(-3.99)" ]- N, a# `, K s% C( Y
- -3
9 s6 O; J9 L; j, k- k% i& @$ W2 D - >>> math.ceil(-3.01)) z7 c) H6 _ u% @0 d" b
- -3
复制代码 : b6 g. ?( ^! S6 ?) p9 N+ \
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, U, |8 f) }2 _: G2 o- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& |+ `- ?: }8 s! U - floor(x)' z; e) W2 W& o' b. R) Y
- Return the floor of x as an int.
2 V& u% o# d2 |) v% r% n - This is the largest integral value <= x.
: y" W, s' |# r/ ? - >>> math.floor(4.1)# N- V( S) Z4 r' U$ c
- 4$ H& E/ k, _6 |0 W) p
- >>> math.floor(4.999)
; j, B, ?% S# R. M" ^ - 4
3 S! J3 z8 a; P' @9 l4 v) e - >>> math.floor(-4.999)/ l' _7 ?+ T* o( b
- -59 z2 l$ t+ N8 @# h* q
- >>> math.floor(-4.01)
% J$ ]7 x4 K. \ - -5
复制代码
" h6 ]( j5 p) \math.pow(x,y) 返回x的y次方,即x**y
6 l1 T* r q6 J9 y# a/ p- #返回x的y次方,即x**y
1 e* R: n. \8 y. \6 s - pow(x, y), E9 k2 p9 W) P$ K U' Z+ X) T
- Return x**y (x to the power of y).$ J6 o1 J" s7 m3 J8 q) N
- >>> math.pow(3,4)
) z3 ^! J$ Z, [" b- | \2 \- V - 81.0 s* i3 V' {+ G% ?1 w B; p+ c
- >>>
: w X0 B# z& w% Q - >>> math.pow(2,7)
, T6 U! D2 C6 V1 K, n! I - 128.0
复制代码 ( @( C# x$ ]3 V! a8 Q) r. i- \
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' O6 q2 q. Y' S3 `$ b) R- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 R j, [- |1 ?1 M9 |3 z8 G l - log(x[, base])+ f. }/ ~1 J3 o: c2 Q
- Return the logarithm of x to the given base.
1 Q4 l. E5 J$ ^' I4 ]) H! g - If the base not specified, returns the natural logarithm (base e) of x.; c f4 b/ `7 q5 k6 y$ i& O5 A3 o
- >>> math.log(10)
1 r0 [; g8 ^2 V! d3 B2 y - 2.302585092994046$ ]: x+ E% @, ~, k" J
- >>> math.log(11)
( O z ~ Z3 R( B, n2 k7 o - 2.3978952727983707
; y8 f0 w7 C9 x# e; B% P6 V* z - >>> math.log(20)$ I |6 M2 Q8 H, m1 Z/ C& l
- 2.995732273553991
复制代码
* y* |* I0 R! t# {5 p1 _$ |1 D( Hmath.sin(x) 求x(x为弧度)的正弦值
8 {; c0 g. T7 p- #求x(x为弧度)的正弦值
4 O2 G+ s; Q1 F2 Q" O1 }* o4 A; c - sin(x)
+ V) y0 _* f" Z/ i8 } - Return the sine of x (measured in radians).2 c$ C- Q. @& {$ m& o
- >>> math.sin(math.pi/4)
+ V' h2 m$ Q s/ } @ - 0.70710678118654755 k! u; K) Z( j2 n. e4 U7 R0 t i
- >>> math.sin(math.pi/2)4 u& ^1 q' k9 A4 t' \
- 1.0* `8 Z1 G0 }( w! h
- >>> math.sin(math.pi/3)5 j1 g) @2 _* ~* j k5 O- y
- 0.8660254037844386
复制代码
( A; r% L6 _1 c7 C* Y; X& W. R- Tmath.cos(x) 求x的余弦,x必须是弧度/ D/ y& P- i! ?" e
- #求x的余弦,x必须是弧度
9 E5 l9 m" H" V - cos(x)
6 E( W& ?* y5 Y- q - Return the cosine of x (measured in radians).
/ x$ g4 W+ ^7 k7 M" x2 g - #math.pi/4表示弧度,转换成角度为45度
# e6 n' `3 V5 x9 z. v - >>> math.cos(math.pi/4)5 W( X3 \9 w! t
- 0.7071067811865476& @ W7 x* I% _
- math.pi/3表示弧度,转换成角度为60度
! l$ h/ r5 O4 t5 t) O; T - >>> math.cos(math.pi/3)
3 D. i; i4 k7 D' e+ S g0 T1 J - 0.5000000000000001
6 U9 q- N9 G2 h2 }+ u4 y1 ] - math.pi/6表示弧度,转换成角度为30度4 O3 U [2 ]7 _! q3 J
- >>> math.cos(math.pi/6)
3 I8 ]3 |$ K2 j' I! Q* x% w4 M - 0.8660254037844387
复制代码
9 t! v: \' a7 V0 b6 |math.tan(x) 返回x(x为弧度)的正切值# ^& R7 f; m5 v2 e
- #返回x(x为弧度)的正切值& g' g5 N" E4 Y. [* h) e; E
- tan(x)" {0 O! |/ `8 v: K! z
- Return the tangent of x (measured in radians). ]4 d9 ~; O+ W
- >>> math.tan(math.pi/4)
7 L6 C u0 t7 y+ C) Y6 d - 0.9999999999999999
+ d* A0 ?& G# |7 {# w$ H8 Y - >>> math.tan(math.pi/6)
- z4 V7 G0 m6 i, @ - 0.5773502691896257" @' _+ O4 E& T5 V5 {: T5 X
- >>> math.tan(math.pi/3)8 W! J5 f* k% i9 r! a
- 1.7320508075688767
复制代码 " |$ F1 ]2 X0 k( ?: X
math.degrees(x) 把x从弧度转换成角度8 s' a) `/ ^7 [! I; X% ~0 S3 p M! Q
- #把x从弧度转换成角度
+ H) z" e) p% N4 t - degrees(x)* E: S1 N& W w: B) Z, X0 U% S: h
- Convert angle x from radians to degrees.
1 X8 w! W+ @3 k. t - ( N T* {4 a. X. k$ |5 u9 J6 C$ e
- >>> math.degrees(math.pi/4)4 r0 w2 a+ c( E- h5 L$ Y( W
- 45.0" g" C! g9 G: X. l4 n$ V! g
- >>> math.degrees(math.pi)- i! E4 }1 p+ t" B7 z* m0 N" {4 \
- 180.0
# l: m) \) z! C/ O9 |5 \ - >>> math.degrees(math.pi/6)1 j4 f% C( f' c6 L
- 29.999999999999996, b' Z" W7 e( n; y2 y* l
- >>> math.degrees(math.pi/3)' f5 x! u {4 J. _5 S8 Z d/ E! [
- 59.99999999999999
复制代码 5 z5 p3 K; l! w
math.radians(x) 把角度x转换成弧度: i* M; a# k+ E' s& {2 \ g
- #把角度x转换成弧度3 M$ }$ r2 ~4 a; a, ~ O: N
- radians(x): H( q; \' Y4 ~- e5 j3 {2 E& ]
- Convert angle x from degrees to radians.
$ U, [' V* p" V6 Z6 f! S1 z - >>> math.radians(45)
* j( m. q. P* \ - 0.7853981633974483
5 Z) a2 g* i( x& W. U" [ - >>> math.radians(60)) T2 h7 B+ Y4 e* `
- 1.0471975511965976
复制代码 2 G5 r r0 E8 Y9 ]: Q
math.copysign(x,y) 把y的正负号加到x前面,可以使用0/ @# k8 y( Z B. m) o% D
- #把y的正负号加到x前面,可以使用00 x& \+ @5 h9 Z/ T" l2 \ |* h R
- copysign(x, y)( w9 H& k$ h4 _: }
- Return a float with the magnitude (absolute value) of x but the sign 3 i# \6 w0 j/ }* s4 C
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
" x: D. k) w+ Z; i0 y - returns -1.0.2 M0 h* z& s: T. G
`8 d# x I/ j% \" Z2 E, f5 x- >>> math.copysign(2,3)
7 A5 G, A% b8 y9 u6 _- v: A - 2.08 l" U' |: q! z) I! r
- >>> math.copysign(2,-3)0 o, n8 m* r3 w9 b- W+ }4 h
- -2.0
1 t$ J l t: Y. R/ q5 e# |* m - >>> math.copysign(3,8) p3 p/ l4 m" e4 E* V2 B
- 3.0
8 _8 o' ] T8 I6 G$ j0 I2 j - >>> math.copysign(3,-8)8 u1 b) c- Y/ ^
- -3.0
复制代码 6 ^1 {* V! T0 _8 A
math.exp(x) 返回math.e,也就是2.71828的x次方
2 E+ w$ Y$ c- }0 |$ E- #返回math.e,也就是2.71828的x次方# P" K! P8 l% H- j
- exp(x)
% |) V3 a4 N2 d* H - Return e raised to the power of x.
3 o" I& c( |, @ r9 @( H2 @6 K4 j - 4 k) _( K' w, a
- >>> math.exp(1), T& m; k8 K! e2 _' J9 Q% G2 p: N
- 2.718281828459045
) v' C/ J" h; t7 h7 {1 t - >>> math.exp(2)
$ \* u) I) Z# a, V - 7.38905609893065
3 q. U5 T* w- i. a# {. i" ] - >>> math.exp(3)( I: T( d1 M3 t+ m1 U" ^
- 20.085536923187668
复制代码 ; L7 o; A0 }% s# P2 d5 x
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# `& U' r$ E; C- L( I- #返回math.e的x(其值为2.71828)次方的值减1& R% t4 `0 H H
- expm1(x)
, A' d' M0 ~. e4 ], M* ?% p - Return exp(x)-1.
1 a+ h2 N/ P- z' i% n0 K5 E - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 i4 R# I+ K% `
- # k+ q4 _) O) M N
- >>> math.expm1(1)
$ h/ ~% D' [. `1 R - 1.718281828459045* V" w/ e" R( t2 r& C+ m
- >>> math.expm1(2)- E* J; [2 R. R# E3 u; H
- 6.38905609893065& W! [% a, [; _! S% T5 H
- >>> math.expm1(3)
* Y* j( z: t6 [3 D0 B* d - 19.085536923187668
复制代码
: T1 p) H1 z3 S X( L" Z* v) Nmath.fabs(x) 返回x的绝对值, G5 y" ]7 B7 E2 z
- #返回x的绝对值
& B0 X; x: N8 g - fabs(x)+ i( p$ l9 v3 {
- Return the absolute value of the float x.
4 G8 o. I$ N# N
" _" {: }' Z! b8 C( w( F8 x: E$ ~- >>> math.fabs(-0.003)
3 v; d3 g4 ^6 F/ K T - 0.003
; I7 v; ?- [9 j, A - >>> math.fabs(-110), z0 V: n: x$ w% \9 q! o
- 110.0
8 l- |3 ]/ C/ s' E - >>> math.fabs(100) ]3 U& s# \) r) O3 O, |0 t0 j8 i
- 100.0
复制代码 ; g/ e+ h0 l0 F* i. w& m) s M1 Q% k
math.factorial(x) 取x的阶乘的值5 Q+ \" Q2 E5 J. f( S
- #取x的阶乘的值( Y; X3 C6 E G0 y- j1 Q1 W, o. G
- factorial(x) -> Integral
8 c$ _9 Z* o% \: E% H1 `1 z9 s - Find x!. Raise a ValueError if x is negative or non-integral.
- A/ r7 x5 x' b, I+ y - >>> math.factorial(1)
+ r( p! {* z7 v( Q* r' y. e' E/ \ - 1
/ O% z1 \6 w# f$ Y1 T. l - >>> math.factorial(2)
9 W$ Q: ^; g/ r5 H - 2
5 n, u% m9 `- J - >>> math.factorial(3)
% B- t; f7 E1 B - 6
) x5 N) Q1 l8 Q! Z3 p - >>> math.factorial(5)
$ f p W. R" h - 120
3 d) Q- R, m; m2 u- q* I - >>> math.factorial(10)
6 f" L5 S0 N. @! \6 L' _, ? - 3628800
复制代码
8 s+ B- B, k5 V9 l3 B9 y5 Zmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
0 z- e* o0 V' V* h ~$ O- #得到x/y的余数,其值是一个浮点数
6 M0 }7 \: n. K5 v5 l4 ]2 g4 Q - fmod(x, y)
b% Z$ P* ?& Q/ w' {8 O4 H - Return fmod(x, y), according to platform C. x % y may differ.
* x) U4 B$ H1 d- g - >>> math.fmod(20,3)
0 K) K- ]' u7 A - 2.0" X& B! j2 z9 X
- >>> math.fmod(20,7): T4 u1 d' \) u5 Z$ U
- 6.0
复制代码
! ?5 A2 k, a) ]0 m @math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
' c" p- G7 d( Q# { u; Z- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' R/ N. o- U# d
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
# J5 F3 C( C" \/ W1 W5 K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
5 I7 r X, w8 w/ [% s3 i - frexp(x)
: _0 Z, m$ D; o8 m, ?; P - Return the mantissa and exponent of x, as pair (m, e).
! R+ _/ s" h! }3 B- @ - m is a float and e is an int, such that x = m * 2.**e.
5 n1 I; O3 [8 D0 l/ C. K' T - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
2 \, H. @& ]& x0 w" s - >>> math.frexp(10)
& A8 Z) F6 R- A) \: N# ] - (0.625, 4)
+ `) d! ?! W& M: T3 k - >>> math.frexp(75). Q) N% K8 }' T: Y* H' i6 S* b
- (0.5859375, 7)8 B7 z; K: L! L
- >>> math.frexp(-40)) G1 U- I& a5 ~+ F) ]
- (-0.625, 6)9 o+ K6 ]5 q s( ]" ~7 a
- >>> math.frexp(-100)# d( G$ l* g/ R, I* C& P8 k
- (-0.78125, 7)5 @) j8 k: C5 I& X* e0 t
- >>> math.frexp(100)9 g* h6 `0 _8 \: |0 {- K
- (0.78125, 7)
复制代码 6 y7 w* x) }; M/ D( t0 b
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
5 ], P6 ~( z6 v4 U- J3 h3 S0 v- #对迭代器里的每个元素进行求和操作2 x% q8 @7 W& o, k/ v
- fsum(iterable)& a( C$ P7 K2 f
- Return an accurate floating point sum of values in the iterable.
& i! j9 i1 K) u L - Assumes IEEE-754 floating point arithmetic.) L# J& G8 Y4 b1 P& i
- >>> math.fsum([1,2,3,4])! j: H4 l" R3 C9 b( P/ s
- 10.0
; s5 F* a: i% K# Z5 W4 g; R - >>> math.fsum((1,2,3,4))
. A2 X7 ], X3 ^! g - 10.0
# D* D* x0 F/ t. @/ p0 }% ` - >>> math.fsum((-1,-2,-3,-4)); N8 e% m* k. k+ e
- -10.0
7 ?+ `; z9 O. Y" p9 l; d1 U8 j - >>> math.fsum([-1,-2,-3,-4])" @4 \1 b7 U$ O) p4 z: m
- -10.0
复制代码
, |" G/ ^5 ]5 y1 p; L5 |1 u7 C" Omath.gcd(x,y) 返回x和y的最大公约数. [9 O; X6 X) [) @8 ? P
- #返回x和y的最大公约数
1 N$ p- A% M4 Y. S [; e$ i; a0 U - gcd(x, y) -> int% P8 k e+ G* h5 {; P6 U: w( a
- greatest common divisor of x and y6 i2 o- M8 J6 B4 t# f
- >>> math.gcd(8,6)
2 `# r2 g4 x. Z8 O4 q, Y: @ - 2; A& g9 k" q; M, e( s: ]6 v9 y
- >>> math.gcd(40,20)
6 S+ `$ L7 i3 L1 Y @& q: J - 20
7 X3 {2 v) G1 e* _ - >>> math.gcd(8,12)
1 g" F" P7 a) Q# {9 a - 4
复制代码 2 \! h3 P3 l1 t0 s6 M
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False' H* J/ r5 C- p4 V
- #得到(x**2+y**2),平方的值
4 R' V1 L2 \" t2 {% y% z6 J - hypot(x, y)1 f5 @0 [ k4 o1 G) o$ e* B2 {
- Return the Euclidean distance, sqrt(x*x + y*y).5 ?. g9 K7 e2 l$ B3 g7 T1 e
- >>> math.hypot(3,4)4 N2 B% q. k2 Z. V/ ^! i
- 5.02 F5 d7 p% }* c9 _- }
- >>> math.hypot(6,8)
5 f& H6 L2 S0 S( F; n/ a - 10.0
复制代码
8 l) i3 g9 p3 j" r/ A, B7 }6 o- rmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False+ w( C, T, O( O( F
- #如果x是不是无穷大的数字,则返回True,否则返回False
( V! ^" t) x$ k& X% P - isfinite(x) -> bool& z, i e8 q! B8 \' P4 C
- Return True if x is neither an infinity nor a NaN, and False otherwise.
4 u' a! G- n3 g R: e - >>> math.isfinite(100)
! U- {, B5 O+ T7 R( p: [ - True
5 G+ ~: Y! }" y0 @2 B1 G0 A - >>> math.isfinite(0)0 g# m$ u6 R8 {, a6 s
- True
) W1 Z, l4 S- n - >>> math.isfinite(0.1): C! j7 m T1 i V* A* l7 f) f
- True
$ I$ Q) U* `! P C3 S' G' H - >>> math.isfinite("a")0 W( V) _4 d+ @, _" P$ M! d
- >>> math.isfinite(0.0001); D! g1 R) [8 x, ]2 N4 o8 T
- True
复制代码 ( {9 d8 C# v# {2 Y9 Q
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False0 v z8 l9 A) |5 I# C
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
" y, v( i' X3 [. q7 t3 o0 O3 m) k - isinf(x) -> bool$ D- q- g4 u& t( r
- Return True if x is a positive or negative infinity, and False otherwise.9 p5 E+ \) z2 }
- >>> math.isinf(234)
1 n9 j% H. K" o* u/ R, o. l% b - False$ R) b1 V+ l1 M0 H
- >>> math.isinf(0.1)8 }# y# g! d7 O; z1 ]7 ^5 d0 r
- False
复制代码
, F; k- \! o+ L# p" Smath.isnan(x) 如果x不是数字True,否则返回False
8 k% w( O7 G6 ~! H- M: I- T- #如果x不是数字True,否则返回False
$ Q5 i4 k9 q0 v7 e/ ^ - isnan(x) -> bool/ h5 q5 d6 S7 X' T; H
- Return True if x is a NaN (not a number), and False otherwise.5 i$ g$ N6 n( Z! B9 K: q$ |: q
- >>> math.isnan(23)
! n N3 { h' }, b& o - False* z& R! K6 J/ _
- >>> math.isnan(0.01)+ `" n! }- a( ~8 @. ~# a
- False
复制代码
5 J! O. J& r7 Y& `+ _math.ldexp(x,i) 返回x*(2**i)的值
$ U9 D! P) E$ h0 ^9 x- #返回x*(2**i)的值
% q& i! T4 a# l* {/ v0 d W - ldexp(x, i)8 j, Y8 Z ]1 D6 o* |
- Return x * (2**i).1 K1 c$ n+ i6 x( x9 ~
- >>> math.ldexp(5,5): f* W: s' A! w+ s
- 160.01 y E" d/ F) U( C" K0 [
- >>> math.ldexp(3,5)
% ]4 F: w: B5 l* \) D% b - 96.0
复制代码
8 @9 }% e8 B. Z9 W; u7 c& h6 tmath.log10(x) 返回x的以10为底的对数. b: ~, E- b& }7 l, z3 Q0 ?% D) W
- #返回x的以10为底的对数. l: C, l6 W' U; E7 x5 ^6 S
- log10(x)
. x3 g" o, `; [# K# g9 O - Return the base 10 logarithm of x.# [; w8 t, s% s T2 C- ~
- >>> math.log10(10)
9 @, A& T9 @% M6 f8 R) p3 L, Y* q% y - 1.0
2 F" T1 F8 n; B& E, R- r | - >>> math.log10(100)( d: D0 c$ C8 D1 d* J* T
- 2.0& a4 f8 W* Z+ Q
- #即10的1.3次方的结果为202 U+ N8 ]$ h: }( e* L2 a( J* g
- >>> math.log10(20)
9 q% o V) W" r3 S% d8 ^( P, G; F - 1.3010299956639813
复制代码 , T" C$ C1 W9 H/ l
math.log1p(x) 返回x+1的自然对数(基数为e)的值
. }# i% ?. z3 \; c& B- #返回x+1的自然对数(基数为e)的值; N! F+ B% s2 w! _
- log1p(x)
5 T" `6 ^: B8 T9 z7 C - Return the natural logarithm of 1+x (base e).
% V h9 q; @1 J, {7 W; x! p - The result is computed in a way which is accurate for x near zero.
/ w9 e% o: R# n8 i. ^8 L - >>> math.log(10)
0 t* m: T$ m! K - 2.302585092994046# Q& D- u! P- x) L, E/ q
- >>> math.log1p(10)
9 R1 Y0 a$ V( u( V9 |0 o - 2.3978952727983707
j" |5 I/ T0 y" M; Q, v+ n1 R - >>> math.log(11). z6 l \; i# U" O
- 2.3978952727983707
复制代码 ( E7 r. o- s& D# r
math.log2(x) 返回x的基2对数
6 s4 o' J( S2 w3 e- #返回x的基2对数% _" h+ ~" T3 P$ N, {! f1 N4 {
- log2(x)
& a& ?/ \- \3 y6 h: ?, o) m# A; h$ O - Return the base 2 logarithm of x.
6 l4 [5 Z% k5 \ - >>> math.log2(32)+ F' d x' k. m
- 5.04 g ]/ s; ?" _4 A1 o3 l4 \
- >>> math.log2(20)
2 o' ?, m, i; O T5 F+ U3 g9 ~# _ - 4.321928094887363
! E" p8 @3 {, M7 l' T3 _7 F) q" ]( H - >>> math.log2(16)0 m( p' x2 f- w' s, k6 _0 z; \
- 4.0
复制代码 ' m% |7 M* b. j5 d) `% _( e0 S
math.modf(x) 返回由x的小数部分和整数部分组成的元组
6 \2 |, h0 o+ E/ c0 _- #返回由x的小数部分和整数部分组成的元组
% q7 N6 s9 F) g5 j# u, P! q5 _& N& u - modf(x) ]% i. R- f( m
- Return the fractional and integer parts of x. Both results carry the sign: t6 z! h" k6 x) g V: Z& ]
- of x and are floats.
" a+ J' [( {+ b$ M5 f% B - >>> math.modf(math.pi)
- \5 B% b0 R( {& {0 @7 S6 H - (0.14159265358979312, 3.0)
) S) x( ]* V. o5 _ - >>> math.modf(12.34)1 p q% T; u# K# p
- (0.33999999999999986, 12.0)
复制代码
/ ?% d" q; k: C% O7 Bmath.sqrt(x) 求x的平方根2 `% U, Q( M6 }2 {8 o
- #求x的平方根
# s8 r% Z. a' E! W1 @4 D7 j - sqrt(x)
* ~9 a9 v- Y9 h - Return the square root of x.4 E3 Y3 Z' v3 }9 p' e
- >>> math.sqrt(100)
1 T/ d! ^) z( q w6 P& i - 10.05 W: I4 U9 v' d& r
- >>> math.sqrt(16)
& R4 u4 V3 L( y3 O2 J - 4.0
# D$ m/ B- j: b% K6 C7 i - >>> math.sqrt(20)
. i$ t5 T; k: f - 4.47213595499958
复制代码 2 E: a+ v, D# g4 F" c) W& E
math.trunc(x) 返回x的整数部分- #返回x的整数部分/ y' R8 Q4 V7 y r7 }* V
- trunc(x:Real) -> Integral. n( Z. _8 F9 X$ v# x! H Y3 E3 \
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
" z- L# k3 Q) D% u! j - >>> math.trunc(6.789)9 _0 K& y& ^* w2 n% O& `8 t* k
- 6
$ s& v! X$ c& j) h - >>> math.trunc(math.pi)' a9 e: D Y! n, Q# d
- 36 P) s$ ?8 x7 k
- >>> math.trunc(2.567)
0 u4 w# W: w$ a% P) E6 n/ _ M - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|