马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- C0 q+ C) G: q* a: h6 X V' N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
8 U1 t* ~) x2 Y6 ?- A) s
3 k0 A" U5 O# K- p7 _4 _方法1:% v, @+ }( G. c. X
- >>> import math
2 ~$ Q! Q o+ y# n8 Z0 D# S - >>> math.sqrt(9)
$ a! U3 h5 F$ V2 ^" h" `# x6 k( D - 3.0
复制代码 方法2:* h; a; _; L, d; Q
- >>> from math import sqrt
5 v" k* H- \' G7 Y% V- B/ ?- ?3 ? - >>> sqrt(9)
1 o8 `! D$ z6 r$ d" ^8 \ - 3.0
复制代码 3 P$ @! k# O- q5 s
# c7 m% T( K; p2 ^5 Amath.e 表示一个常量
, N. X% t! S5 y- #表示一个常量; I( [0 R2 j. g
- >>> math.e
( Q# P; Z0 _& s. H& F8 _9 u+ M - 2.718281828459045
复制代码
0 t, } }8 Y4 t2 \" i8 emath.pi 数字常量,圆周率( c& B' G$ C6 x" u+ ~/ V
- #数字常量,圆周率6 r9 d* {" K) V4 G2 f/ T
- >>> print(math.pi)
, ?0 m! [2 }( } - 3.141592653589793
复制代码
! u( m9 g0 D! O9 O. Q9 Mmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x( X* y) Q0 @1 D1 T8 B, `
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 s- k- E3 A- ? - ceil(x)
& ?1 l" @5 X5 c# U( \, Q - Return the ceiling of x as an int.: T$ B: l- Q1 V- \6 ^6 `
- This is the smallest integral value >= x.- r0 F1 e4 G& \8 |' t# p( a/ T8 s
. |0 |/ D" B0 |7 s0 A- >>> math.ceil(4.01)
( S& Y0 x) W5 Z- T( d - 52 I+ E+ k2 S7 D7 t" e' J
- >>> math.ceil(4.99), N/ p9 M& l/ N3 p# N' a- Q5 ~3 @
- 5
7 S- A: T5 C8 H6 J5 B- s- M - >>> math.ceil(-3.99)
! |9 t, \: k, l - -3
; |: ~7 i% H; _- q0 c* x - >>> math.ceil(-3.01)
2 B/ _8 b$ |( f4 ^( I9 ^ - -3
复制代码 0 m0 A/ { [& W$ F% Y9 a
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 f- p- G4 Y( Z5 C* ^
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ C3 ?3 ?1 a4 M - floor(x)
; x7 G: Q/ S( {3 o4 H - Return the floor of x as an int./ r! x; d4 s7 x% ?
- This is the largest integral value <= x.
# ^/ E; U5 q- g A7 m - >>> math.floor(4.1)
' D: V6 U- H5 L9 T& i5 e - 4
3 u6 g0 c" }1 O" N! J+ ]; i# v - >>> math.floor(4.999). h4 m% `6 k' W3 k3 e4 h8 S
- 4
" H( k9 O. j2 M# j - >>> math.floor(-4.999)/ W" b6 U. }' V/ e
- -5
$ O. p7 b8 W3 h: P# X% F - >>> math.floor(-4.01)
" k+ V: r: \ P6 \" c. i' z9 Y - -5
复制代码
5 j0 R9 X5 Q1 v. A; V8 dmath.pow(x,y) 返回x的y次方,即x**y) _( M; H8 `% A5 G. H9 p8 w
- #返回x的y次方,即x**y
* h6 e6 a) o4 H f" I, J - pow(x, y)
- N1 L+ `9 H5 p- d; [ - Return x**y (x to the power of y). U4 ~/ `# M/ Z" }4 f0 u4 P
- >>> math.pow(3,4)
( C- [' l8 V+ s" i% E" _ - 81.0
% h4 g! W2 i9 l - >>> ( `: f$ n' w3 m8 W
- >>> math.pow(2,7)
: G, ~0 d( Z) r - 128.0
复制代码
; e3 @& @, J* o+ v' }; ]* t- E! }math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 \% q |( P% I+ c) [- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' r+ m9 ^& p6 b
- log(x[, base])
/ @4 a* f3 J h! a' M! f - Return the logarithm of x to the given base.
9 ~, y6 M$ G/ u* b - If the base not specified, returns the natural logarithm (base e) of x. `/ c* h6 |1 u
- >>> math.log(10), N0 `. }" p0 k9 n' |2 Q! q% N
- 2.302585092994046% p5 Y& q6 \' o
- >>> math.log(11)& g/ Y& a% d! i- i5 A
- 2.3978952727983707
3 Q/ ]6 G" t6 I {2 v+ G* x - >>> math.log(20)
% T/ B/ _4 g" e) {, t1 f) {0 r - 2.995732273553991
复制代码
7 Z' a# O! ]! y' Q1 P* A0 s- qmath.sin(x) 求x(x为弧度)的正弦值8 v$ b0 c1 {# e6 G d; i* R
- #求x(x为弧度)的正弦值$ i4 |; l# k4 D! r' A( a' u; z4 @" @
- sin(x)
5 J4 J$ J2 C3 A o - Return the sine of x (measured in radians).( B3 h8 F# D. }7 o2 R
- >>> math.sin(math.pi/4)
& n/ ^4 f7 S1 f# G6 [. q6 z2 F - 0.7071067811865475: J* Q6 E4 o" j& `
- >>> math.sin(math.pi/2)
5 ?3 L" ~7 B; C; E - 1.0
0 I, e& \* T6 x8 p - >>> math.sin(math.pi/3)3 |( m6 W z3 K& B) i6 U- _$ M
- 0.8660254037844386
复制代码
x( P5 Q: N( n- j# [% \& l* s0 I) S; smath.cos(x) 求x的余弦,x必须是弧度8 u5 Y& O( h8 V' `' q5 x
- #求x的余弦,x必须是弧度* G% ^; p% `- I, P1 ~
- cos(x)
: \4 W" p% v$ A! U9 R - Return the cosine of x (measured in radians).' ` n% d. T K. Q0 S4 Z
- #math.pi/4表示弧度,转换成角度为45度
- ], n4 \. }3 V" r - >>> math.cos(math.pi/4)
9 u4 n: D$ x; d - 0.7071067811865476
0 d- a! Z+ C# p( x2 c - math.pi/3表示弧度,转换成角度为60度
$ O5 T8 z0 \; I+ Q8 b - >>> math.cos(math.pi/3)
" T( Y T/ s$ K# h1 l+ x& t9 t - 0.5000000000000001
2 g6 v! x$ X& ] - math.pi/6表示弧度,转换成角度为30度
$ H, S0 D$ F" t! j5 n8 X0 H - >>> math.cos(math.pi/6), ]! g s5 k& b9 F, E: }1 p
- 0.8660254037844387
复制代码 I+ Z: L" k4 P1 M4 S3 L/ `
math.tan(x) 返回x(x为弧度)的正切值; I/ t# s4 t: e7 ~" }& b
- #返回x(x为弧度)的正切值$ ~% V. u( Z& L/ B& y% I
- tan(x)( q$ y- a, f Y" t$ E S- c8 h5 c$ z
- Return the tangent of x (measured in radians).
: P, t, F5 v5 k T) L9 i" S+ C0 o a) k - >>> math.tan(math.pi/4)
. K! p5 Z% G! W1 b! r) [ f - 0.9999999999999999; N9 p5 F( L- I/ h. I
- >>> math.tan(math.pi/6)
& p+ W; b1 n5 H6 d" F+ l - 0.5773502691896257' o2 \9 V$ m: t( [6 {9 _7 O
- >>> math.tan(math.pi/3)
5 Y/ X" ~# N# r S* a/ U& R0 ]% P3 ~ - 1.7320508075688767
复制代码 1 J& \2 X% w7 a. v# i
math.degrees(x) 把x从弧度转换成角度+ j% K! I2 G+ k1 B) a
- #把x从弧度转换成角度* d/ S$ _) s/ k5 _/ S, A
- degrees(x)9 V4 E( w# P8 E/ p
- Convert angle x from radians to degrees.
# q% V q3 N+ @* v
6 O& F: ~/ i* x: n+ q5 r3 [! k. f- >>> math.degrees(math.pi/4)
& g+ C+ ~. u9 _2 o0 T0 r - 45.0
3 n2 {* N9 P4 b+ w - >>> math.degrees(math.pi)
' [0 T6 A6 z& M- K' R - 180.04 ]4 f& z1 z) Y$ C3 |
- >>> math.degrees(math.pi/6)
, Y9 y' P. C: o) H4 _) g9 r - 29.9999999999999962 O7 P U/ i% w" m. i( J3 Z( I( D
- >>> math.degrees(math.pi/3)
( T4 Q% t, T# y) G: V; d - 59.99999999999999
复制代码
' r8 U8 A' ]$ O% q) vmath.radians(x) 把角度x转换成弧度: S' I. G" f; C* d Q. T
- #把角度x转换成弧度
4 K3 Z% U" C2 t/ w8 _8 J; x+ ? - radians(x); y: J% I6 s3 c. E: }
- Convert angle x from degrees to radians.
( P8 i8 ]! I7 B2 w* a+ M - >>> math.radians(45)
5 V5 w# N* a4 c8 ?+ @3 T5 _0 h - 0.7853981633974483: V: o& M+ y' W: K" u1 Q
- >>> math.radians(60) h5 ?; M. s' ]: a
- 1.0471975511965976
复制代码
' r$ u3 b, Y' [/ Vmath.copysign(x,y) 把y的正负号加到x前面,可以使用0* Y C* K0 F4 J: p
- #把y的正负号加到x前面,可以使用0+ [3 d& H3 z5 G: l% P& d! Y
- copysign(x, y), K ~, `% L1 a; \! Z+ r. R
- Return a float with the magnitude (absolute value) of x but the sign , v$ h# J" H# q
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 l: K+ S( J) W
- returns -1.0.
8 [7 `4 [( X) _! j; j
0 a1 V& i5 }* r- >>> math.copysign(2,3)
2 y! d! A4 Y/ L, h( l - 2.0
9 G) Y0 H9 u$ x6 U% K - >>> math.copysign(2,-3)
: e# f1 e" r1 n1 L% `9 x0 N$ } - -2.0' s4 d r/ U6 n5 Z9 p# K
- >>> math.copysign(3,8)% C- \( }1 t8 T2 J$ w) a
- 3.0, p. y9 ]( G7 A5 f" X; _4 b
- >>> math.copysign(3,-8)
, p9 }4 x Y( \$ Z% e! U1 G/ `' r - -3.0
复制代码 8 c& C5 `; B* n4 z: g1 P/ L
math.exp(x) 返回math.e,也就是2.71828的x次方- X; n& A3 y1 S2 ]/ T j7 j$ t
- #返回math.e,也就是2.71828的x次方
2 |& d' V7 M) x2 S - exp(x)
: J5 r1 R" l9 a, |' Q( J' g h6 t; g - Return e raised to the power of x.% c( y, ]( o- d
( d: e3 N( ~1 h2 ^" m. B4 B( S- >>> math.exp(1)5 V2 M V {$ K0 t) p
- 2.718281828459045
# D+ G4 Z0 `( r c6 A - >>> math.exp(2) b. {, V2 j/ {, }, ]
- 7.38905609893065
, g2 C, I O. S: t& C - >>> math.exp(3)
8 `( E. I) _' }- A% D - 20.085536923187668
复制代码
( q% m4 M+ J1 ]( M& D& umath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
) ~ a- q! F7 y3 T `! Z* d( V- #返回math.e的x(其值为2.71828)次方的值减1
: }- C. H; F. i+ U* X+ X - expm1(x)1 s, B1 A: @. K6 |! d
- Return exp(x)-1.- e& N6 w: c( k! @! T" H# H
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.3 ^/ w5 e7 ? v" c: b' D% n; S
- . S9 Y$ u- A4 }* U r' q# C
- >>> math.expm1(1)
# ~8 ^$ G" V9 }5 M3 _/ O - 1.7182818284590451 h( f. w' g6 [) h
- >>> math.expm1(2)
9 j, S( `! ]1 B! D$ o - 6.38905609893065
5 Z6 O9 ?& J8 j/ O4 n9 \ - >>> math.expm1(3)
) L& A; F- V5 q; F4 \ - 19.085536923187668
复制代码
, g* B$ R' l) `2 m8 B$ A zmath.fabs(x) 返回x的绝对值
7 l& K L M/ H! I7 k: i- #返回x的绝对值
2 j8 x7 [5 s1 k2 Q' ~, [ - fabs(x). z2 c% {$ i4 q# _# g- Y4 D0 a" t7 W1 v
- Return the absolute value of the float x.
- _! \% n/ }1 Z0 J/ w* s. r
4 e6 Q4 Q, W0 o5 i3 Y5 P, D9 ?- >>> math.fabs(-0.003)
; e# P9 |* x6 s! [( A8 e* D, Y: l - 0.003, }+ B, ^' o# L. ~
- >>> math.fabs(-110). z' t3 F) {0 x* S6 r; @3 l% T
- 110.0
5 T1 H, N9 |- @& b. j - >>> math.fabs(100)* o' P& X. x6 s" U+ o
- 100.0
复制代码 ) S( J5 K* G% i3 T1 |; q
math.factorial(x) 取x的阶乘的值( G2 o) u2 h2 [/ H
- #取x的阶乘的值
2 D8 E) ^8 m5 O) {8 u - factorial(x) -> Integral
$ ~ ~. c# ^ Y* u - Find x!. Raise a ValueError if x is negative or non-integral.9 c. Z* F" [- T: j0 \' {% w
- >>> math.factorial(1)
. j; o/ Y; l& K/ E - 1( B( ]2 r+ E) G
- >>> math.factorial(2)( L- m* y) u. L9 i
- 22 |! g$ d9 u# f# u g/ M0 x) r
- >>> math.factorial(3)3 m. W3 G* |& c9 ^8 {* S$ g7 l1 \8 b
- 6
* g8 k8 M* _9 X, \- U5 X - >>> math.factorial(5). C% F3 l# _ P; z8 s! A
- 120
) y- T: C. a0 Q - >>> math.factorial(10)
^9 E7 b, D( J. T4 w' \! a+ z3 { - 3628800
复制代码
/ v5 R) A, y4 {3 f: u+ r# }7 ymath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
~2 Y' b: l2 V8 O) ?: z3 _- #得到x/y的余数,其值是一个浮点数
% ^0 i% L& U( L$ } - fmod(x, y): m: u( s* m( P, m j9 |7 S
- Return fmod(x, y), according to platform C. x % y may differ.4 V1 k4 p O! [) z; ~
- >>> math.fmod(20,3)
% A' f, a3 I1 _% R. N: {6 T. W) z& ~7 f - 2.07 O3 Z( h. ~9 |! [5 H B
- >>> math.fmod(20,7)
. p4 M8 H C! `* T. | - 6.0
复制代码 ' e( W& E7 H3 z6 n1 W5 _$ _/ G
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围0 i. L& g1 g% s& a
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, K; k$ _: F! W
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 a; K H1 [* l1 n
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ b7 U& @+ j/ A9 n) Q2 x
- frexp(x)
3 H% y* u2 X$ \, w/ G - Return the mantissa and exponent of x, as pair (m, e).
" ^- b0 S$ k+ F, ] - m is a float and e is an int, such that x = m * 2.**e.- d g' o* E! d
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 S1 C4 y- a( R, N: q/ q7 s
- >>> math.frexp(10)
: V' j& h/ G7 g$ D - (0.625, 4)# a. J7 x, _7 O" f+ {5 q9 |
- >>> math.frexp(75)
% H2 @4 H3 I. J- ~% s, A B- W! } - (0.5859375, 7)
7 ]! c {5 E; f" F# M' s" {8 R* ? - >>> math.frexp(-40)# d' y/ i8 g6 V) g
- (-0.625, 6)- P- d8 q2 u3 P F2 h- b9 }0 w
- >>> math.frexp(-100)5 d7 P! h7 b/ V- l
- (-0.78125, 7)4 n; b! \7 S. s! R* x; ~9 r
- >>> math.frexp(100)! `, }4 v$ E' J+ X- s( G
- (0.78125, 7)
复制代码
6 A% `) C( l( G6 Bmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
3 _9 i, Z+ G( I) } s- #对迭代器里的每个元素进行求和操作
7 I' v6 ~1 T7 Z* }/ d s* ~ - fsum(iterable)
3 ^# L5 s$ A, p8 e6 G9 j - Return an accurate floating point sum of values in the iterable.6 {. X2 a! J1 h+ q
- Assumes IEEE-754 floating point arithmetic.
s" e r5 O2 X% d! |) @ - >>> math.fsum([1,2,3,4]): M0 m* @7 H9 O& [2 ~& [9 E$ {
- 10.0/ X% w: K2 d$ P& I+ @ Y0 X& Z' |
- >>> math.fsum((1,2,3,4))
q! E+ |& n) N; M1 H - 10.0
6 u( _ W" B, t- u% E* [ - >>> math.fsum((-1,-2,-3,-4)). y2 I; i1 Q! p" a: b! F
- -10.0
+ t2 T$ l+ p1 s `1 \# K - >>> math.fsum([-1,-2,-3,-4])
( R6 c4 W" d* Q* c1 y4 k$ O+ d5 U& C - -10.0
复制代码
. X8 |* L I; G2 @& @math.gcd(x,y) 返回x和y的最大公约数2 @/ Y& a5 W. H/ I2 Z& ^% _" d& \0 N
- #返回x和y的最大公约数+ \9 [% ^$ c9 t5 T
- gcd(x, y) -> int! ^- M7 E1 U _ l$ n
- greatest common divisor of x and y
- k: d/ F$ _5 K$ G: `) ], U - >>> math.gcd(8,6)& e& g( I3 J- H8 K) j
- 26 |# M9 e3 s6 y3 f' a1 ]
- >>> math.gcd(40,20)
( }. ]: [' R, w' G" Y( U( f - 20
& ]# o/ L8 }8 N) {( ^: f - >>> math.gcd(8,12)0 T4 S; o5 h X U& I3 @8 R, I
- 4
复制代码 6 |5 {- g4 W1 S+ W; o4 \$ \; ^
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False- W4 g8 [; y* H* S
- #得到(x**2+y**2),平方的值
5 Q9 V; Q/ M* ` Z! M - hypot(x, y)
% W" ^. a+ B/ O6 I - Return the Euclidean distance, sqrt(x*x + y*y).. d: ?! k* `1 R; D% ~! |, u
- >>> math.hypot(3,4)- ?4 b. V& @) m. a
- 5.0
" d1 O* N. |3 R. V* }) g# N$ t A3 ? - >>> math.hypot(6,8)0 H' g' ?; H/ b* r ^ G
- 10.0
复制代码
$ G+ m1 r' }6 v% H. j/ N$ Umath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False1 j' j) w. c0 N- r* V" l( F
- #如果x是不是无穷大的数字,则返回True,否则返回False
8 s5 Z: A) _4 t9 Q - isfinite(x) -> bool4 H, O8 p- P: Y) p* n# S+ G: g% z# r$ o
- Return True if x is neither an infinity nor a NaN, and False otherwise.
0 n, a' r( {% W' R# _+ V4 d - >>> math.isfinite(100)
6 Z( X$ {" T! n$ A( E/ ` - True
- Q0 |, f' s5 g9 H. j5 b4 z" I - >>> math.isfinite(0). J6 a, h' o3 c
- True
# y0 h/ U" ~( H - >>> math.isfinite(0.1)6 v, |! V; I( x) F% P$ E
- True
' z, ?1 u q: v% f4 L - >>> math.isfinite("a")
6 u' m8 N; h: J8 q8 R! ~ - >>> math.isfinite(0.0001)
8 S/ e2 f1 x( _7 |* a - True
复制代码
2 Y4 N! D6 E2 a9 ~8 J8 q/ `math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ g, H/ b: r9 m" x0 B0 q1 X- #如果x是正无穷大或负无穷大,则返回True,否则返回False* G9 |8 I1 {3 k. q% D! Z2 E5 W
- isinf(x) -> bool
5 `5 {: f& u; a- z/ u - Return True if x is a positive or negative infinity, and False otherwise.
m4 D* }$ ?& A+ U9 T7 K5 P - >>> math.isinf(234): ?4 ^- l# v- ]$ \2 S
- False
d0 z0 J& V! R% ]" G( B - >>> math.isinf(0.1)
2 Y" D; w8 z W. Q0 ^6 {. l - False
复制代码
' t5 k2 }4 I5 W8 ]$ E# Vmath.isnan(x) 如果x不是数字True,否则返回False
- V6 O: F5 X8 [8 s" Y% q- #如果x不是数字True,否则返回False
; V5 j% D' N: \* N5 }. a: F - isnan(x) -> bool
4 v9 k( ?- R1 F - Return True if x is a NaN (not a number), and False otherwise.+ ~% c3 s/ q% V/ ^4 G# V
- >>> math.isnan(23)
$ y8 j1 b n/ O. e; Y - False* w" K& C$ q# b( ]
- >>> math.isnan(0.01)# g$ W0 ^& ?- D3 D6 \/ v
- False
复制代码
- m' l0 t! k# l1 i& o2 ] ?+ U3 tmath.ldexp(x,i) 返回x*(2**i)的值+ J. T$ ^9 m) m/ W. P2 C4 Z Q
- #返回x*(2**i)的值/ x5 C+ ]: h; s& b
- ldexp(x, i)+ m# O4 k1 B. i$ f; s; B+ b3 b
- Return x * (2**i).
/ H( Q* D' q9 g6 _( r1 Y8 K - >>> math.ldexp(5,5)$ H5 x/ f3 O2 z, M% S: I
- 160.0- \' M- O% @" k3 N+ n1 E! B
- >>> math.ldexp(3,5)% _( l3 w* s5 Q7 [- m" Q8 G
- 96.0
复制代码 3 W3 D$ @2 y' v2 N
math.log10(x) 返回x的以10为底的对数
- G ^/ l- X+ A: W+ z: s6 W- #返回x的以10为底的对数7 ^& h7 j2 \" t
- log10(x)
: K+ B& I# X' A. h1 K- M: W+ o. [ - Return the base 10 logarithm of x.( P0 m# {5 f! s
- >>> math.log10(10)# c/ M N9 d) t1 O0 q4 |' j9 |1 B: p) n! x3 j
- 1.02 i L' t) ~) }8 W7 i
- >>> math.log10(100)
1 S" N9 a0 e1 g @% ~ - 2.02 H( o9 |7 ~+ a0 h. w
- #即10的1.3次方的结果为20
/ T" c0 O: q3 l - >>> math.log10(20)
/ R5 f: W+ q! s( Z w; z) e - 1.3010299956639813
复制代码
* y) _9 X }$ M5 {6 f! Q V0 fmath.log1p(x) 返回x+1的自然对数(基数为e)的值
$ d5 N( w1 ^% n- #返回x+1的自然对数(基数为e)的值
" Z$ E1 O! g/ s6 v8 J4 u$ B - log1p(x)$ G) d9 e5 T3 l& X" s
- Return the natural logarithm of 1+x (base e).! i5 [7 j: \0 s; b1 a
- The result is computed in a way which is accurate for x near zero.' Q8 T9 U- V4 P% R7 m8 M+ o
- >>> math.log(10)/ a- e6 S- t1 c' k( q# n3 O$ b5 \# I
- 2.302585092994046
% o# L3 e3 a) v& Q* J( P# H% F* Q% h - >>> math.log1p(10)
3 y, X, n7 b" c - 2.3978952727983707
8 D r% j) c& l7 Q$ S! G - >>> math.log(11)/ n3 a# k- o6 a, I9 O+ I
- 2.3978952727983707
复制代码
2 G- }# l4 v7 F; m6 n( O; Wmath.log2(x) 返回x的基2对数
; M8 ~# \/ v+ O7 g. U) |! ]2 U0 c& {- #返回x的基2对数
9 z" ?" B, T4 p. |/ y - log2(x)- }" ^/ U, [5 ]' d. A
- Return the base 2 logarithm of x.
1 m! L6 R K5 g# D - >>> math.log2(32)
$ ^8 k' {1 s* [) B1 o' i - 5.0
' L- p2 y* T) P' }% b1 y3 T# }# P4 w - >>> math.log2(20)
/ H; d! r* l2 t# k7 O - 4.321928094887363
# E4 u0 R8 j, { - >>> math.log2(16)
9 m6 O2 ?' I9 r$ Q6 ` - 4.0
复制代码 6 k8 j% P9 Y( M3 ^; Y
math.modf(x) 返回由x的小数部分和整数部分组成的元组- L( L: M1 I5 k, q' W
- #返回由x的小数部分和整数部分组成的元组
+ y4 X1 B% g5 f - modf(x)
* \4 v) T7 v" ~1 c - Return the fractional and integer parts of x. Both results carry the sign: U: s' t& w) W9 k$ C
- of x and are floats.
: B( G6 @) Y% X( s - >>> math.modf(math.pi). s7 ^6 ]; H6 U/ \) Y6 d+ d
- (0.14159265358979312, 3.0)
$ y: U7 |# K% H5 s! a* y - >>> math.modf(12.34)
. |) E& a& P3 M }- H - (0.33999999999999986, 12.0)
复制代码 / f' V& R/ y* K# I& d
math.sqrt(x) 求x的平方根
; L+ |: m2 M& U- #求x的平方根
0 y% Y; p0 b. a+ q - sqrt(x)
& U, Q7 s+ ~3 _: y6 Q - Return the square root of x.
n: w4 A0 M9 m" y6 Y: u0 q - >>> math.sqrt(100)% Z. l* j" p- S" K
- 10.0
" _/ z8 P( u$ y1 T9 B - >>> math.sqrt(16)
/ ^) q* M! g5 {9 U - 4.0) n1 K$ h6 r: w" _. X) b8 c2 H
- >>> math.sqrt(20)) [, h% I# @) u
- 4.47213595499958
复制代码
: i" ^, x/ E$ ]" V' ymath.trunc(x) 返回x的整数部分- #返回x的整数部分- V Y4 @5 Y, y: t$ H: a' M+ }1 v
- trunc(x:Real) -> Integral; ]- t5 \4 f `- K8 J5 t
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
1 ?; A. F$ _9 U - >>> math.trunc(6.789)+ H- W$ k& z1 I* ^7 V, @7 s. y) p2 z- w
- 65 `* G) Y/ O: w+ |; r2 B2 V" \6 e
- >>> math.trunc(math.pi)
$ ?8 H6 ?: C) C& w. F - 3
7 L7 _. M+ Z* [- a( k; _ - >>> math.trunc(2.567)
6 z; j. u* ^) F5 B+ ^ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|