马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 p) J% |+ E5 a) e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. D) Z: O7 `# [& f/ D1 e- q, n, ^- o4 k( A
方法1:4 t% u0 p6 ?5 K# F l: ~+ {! L
- >>> import math7 d' {. E7 P0 \' k( \
- >>> math.sqrt(9)
/ d J. i8 e6 ]3 x8 u# { - 3.0
复制代码 方法2:# G' ]/ E m) f `3 g; d
- >>> from math import sqrt
2 M- A2 J; A9 J; H+ b - >>> sqrt(9)( q# ]- \5 S4 }0 Z
- 3.0
复制代码 5 j. X4 V, d7 E+ n- d3 O
8 l5 D. y, ^6 X% X, j
math.e 表示一个常量
7 S3 e8 E/ L8 i4 U E- #表示一个常量
/ Z- A9 j% G4 \0 Q. D - >>> math.e* s! Z5 B5 V1 T" C
- 2.718281828459045
复制代码 ; \) ]+ r# C2 a* `! u
math.pi 数字常量,圆周率
: W( G! h& O6 N# A- #数字常量,圆周率
& C& O. {2 o0 V% G2 {/ x6 k6 [: [ @ - >>> print(math.pi)
- I. P5 v7 c$ ~, f0 x7 A' s4 X - 3.141592653589793
复制代码
8 q# y! p: f1 c& Q nmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x/ u9 C+ D7 a2 D; [4 _. {
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ O, X- ?5 l9 ~/ x" @
- ceil(x)) Y: C, }9 I2 U0 C, c3 ~
- Return the ceiling of x as an int., ]3 M1 B8 `$ R5 F% Q/ T& d5 U
- This is the smallest integral value >= x.' G [. t4 ^9 c* n" T
- / T) C! J7 p8 m8 x, D; k
- >>> math.ceil(4.01)
* C( b) U4 t' L* ]9 \ - 5' s# _/ w! }, \
- >>> math.ceil(4.99)1 P, ^. ^8 e4 U
- 5' v: Y1 { F/ k
- >>> math.ceil(-3.99), N( S% N6 y p1 D4 _4 W
- -3
6 ~3 z! U: x( l! L2 U @ - >>> math.ceil(-3.01)
5 Y% R+ e e9 Y) f9 ]4 h, k* C - -3
复制代码 : M+ T' T# k2 L g
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身: X) O$ k9 u, h% K) z8 v
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) n( v( v" s- @/ V# @ - floor(x)# {' ?+ k% `) `7 b- d, F
- Return the floor of x as an int.
0 u0 g; o# s, |6 O6 m2 [3 I - This is the largest integral value <= x.
& q$ E& Y4 d6 M( I7 a0 N - >>> math.floor(4.1)
' S- l) E4 Y& o/ \0 I/ y$ K - 48 u& ?9 @% ]) ]) a
- >>> math.floor(4.999)+ d5 E( {! Z1 ]0 Q
- 46 S' b, y5 S2 T- z( B- o% B L
- >>> math.floor(-4.999)0 b( [2 p* F& n# j* E; ~
- -5* }" d9 i6 n$ w5 w) Z( \2 G
- >>> math.floor(-4.01)+ x3 Y c, ?5 j9 [: S8 P7 l
- -5
复制代码
* D0 Q1 ~) E0 ^( K# V% C% \, ~math.pow(x,y) 返回x的y次方,即x**y
! d$ } Q d2 k* h9 n8 u- #返回x的y次方,即x**y
. s& _7 g/ s; i7 D8 o" |4 J3 o - pow(x, y)
# n; f9 X5 c. [ - Return x**y (x to the power of y).% ?4 W* N% ?! x8 [
- >>> math.pow(3,4)
7 ^8 |5 |% A5 R/ G - 81.0% P& L9 |4 P8 t/ y7 G5 B7 ^$ s
- >>>
# u) d9 W) G4 P$ ] - >>> math.pow(2,7)
9 D9 L; R( n3 l+ x# p! C& P - 128.0
复制代码 7 Q( v& h# z. Z3 W& B( j9 [+ w* q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 k1 Y# _; U9 O# w0 q- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! M# I% a* ~8 h- Y% D - log(x[, base])( }8 ^! r3 Y5 j" N& a" m
- Return the logarithm of x to the given base.
& R, o, O& I7 G( Y - If the base not specified, returns the natural logarithm (base e) of x./ c9 R' I4 p2 g( a
- >>> math.log(10)# S- R- e$ Z/ I; a* ]# J, I: f( f3 z
- 2.302585092994046- G% R- s3 C$ o- B$ i: n# ]4 K
- >>> math.log(11)
' s, {; c( Q2 S2 N, L& _ - 2.3978952727983707
- s$ Z2 ^* d; r8 |! U' ^ - >>> math.log(20)6 f$ Z8 ? ` {" x% ]" Y0 `1 `4 l
- 2.995732273553991
复制代码 " J+ M0 h/ M$ _; Z: a! X
math.sin(x) 求x(x为弧度)的正弦值
, R& C$ d0 S* u! N/ |% L" W- #求x(x为弧度)的正弦值
& t5 T: ~* n/ I - sin(x)
. \1 J8 c$ n" W% [) b* D - Return the sine of x (measured in radians).
$ X/ z1 E) @% h$ a$ G# ]4 D - >>> math.sin(math.pi/4)
+ ]0 B) F+ F5 t9 k, f) d' g# s - 0.7071067811865475
# n \* l" l) j1 X8 s7 ~5 m - >>> math.sin(math.pi/2)' N1 X% d6 M6 ~) H! @
- 1.0/ |/ [0 ~7 r6 p) l
- >>> math.sin(math.pi/3)$ w. [5 {4 d1 Y# d% @
- 0.8660254037844386
复制代码
4 n4 k8 Z+ I$ a8 K* ^math.cos(x) 求x的余弦,x必须是弧度* _2 J5 l, l( |% Z
- #求x的余弦,x必须是弧度
. N( y, w2 o; _& B+ d1 p - cos(x)$ `7 x2 Y7 u: B* d( f- @2 E
- Return the cosine of x (measured in radians).: @( y& ~0 o# ]! @' f! O# f' P# F
- #math.pi/4表示弧度,转换成角度为45度' K! N3 P) Z. v5 Z
- >>> math.cos(math.pi/4)
6 h% P: I7 _ y& w2 _' U - 0.7071067811865476
4 E0 I2 z1 X/ {% X$ K- B6 j - math.pi/3表示弧度,转换成角度为60度
+ f( f& I7 y- m3 @ - >>> math.cos(math.pi/3)
# {% i f3 U* z7 E9 ? - 0.5000000000000001
7 J1 \, ]" v0 ?$ }) L - math.pi/6表示弧度,转换成角度为30度
) n5 A, t# K- Y - >>> math.cos(math.pi/6)
& z% R" A( i8 a; s: q) @, d - 0.8660254037844387
复制代码
2 l9 E3 } m# X7 R6 x4 r, ~math.tan(x) 返回x(x为弧度)的正切值1 N" k- n& y0 u( ^, `
- #返回x(x为弧度)的正切值
9 ^- Z% W0 [: v; V" W# W - tan(x)
6 s8 H# A/ D& X+ o% w$ F - Return the tangent of x (measured in radians).
8 J' `' B {5 b M, J - >>> math.tan(math.pi/4)
" l2 a, w: L8 p% J7 L. F - 0.9999999999999999( \' Y: S# ^# [$ ?
- >>> math.tan(math.pi/6), }3 l0 x, P" z5 S
- 0.57735026918962578 a) W; {: d' d2 x
- >>> math.tan(math.pi/3)
9 I5 v6 X( `/ C+ \5 h - 1.7320508075688767
复制代码 1 W9 ~/ _7 C5 A! R& K* [8 r
math.degrees(x) 把x从弧度转换成角度
/ a/ Y, O4 p; \- #把x从弧度转换成角度
/ b% r& k$ s2 o2 R8 a$ M9 s - degrees(x)
3 @& o3 Y8 x; m - Convert angle x from radians to degrees.! R: {+ K0 x3 M8 \& j( N
- 5 L& w" \$ ]$ m! [: p
- >>> math.degrees(math.pi/4)) c' T. F E" y/ _
- 45.0
, w) j: T' e9 U4 h* l/ l - >>> math.degrees(math.pi)
9 c3 @+ k* J4 l4 \ - 180.0 b ~/ \, S, U; U- T% P, n0 I8 ^: C
- >>> math.degrees(math.pi/6)0 Q. Q4 o. o# V( U% n' M
- 29.999999999999996
; \* h$ `7 U3 k! y; Z - >>> math.degrees(math.pi/3)
3 N% c+ n2 j% x. ]9 r - 59.99999999999999
复制代码
: Q: |0 r& L9 h& f, L" z9 S: bmath.radians(x) 把角度x转换成弧度! P! s% A, a0 k) T8 `2 d
- #把角度x转换成弧度. J2 y7 D% L, G' _
- radians(x). I( t( P! \ t4 u% S: Y) `; t8 C0 \
- Convert angle x from degrees to radians.
( J$ J* x+ \+ D0 ?$ R2 p& F; l - >>> math.radians(45)
# v( ^+ P2 B0 W: ? - 0.7853981633974483& @1 E+ \) w. K4 _2 J" F
- >>> math.radians(60)
2 e( o7 p" y i; p* R7 C3 w - 1.0471975511965976
复制代码 ; Q2 O! C4 p& T5 n
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
" \2 n" f! O6 p% S8 }- #把y的正负号加到x前面,可以使用0
: S5 X- U) l0 @* r0 Q3 T - copysign(x, y)' Z+ \& f' x& c& D$ c$ Y
- Return a float with the magnitude (absolute value) of x but the sign
# I3 W' \$ Z3 u, A& J' C - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
9 `5 Q. U- B0 J* ^- d$ T/ P - returns -1.0. Z& Z- N& q! ?! h
/ n: R4 }" O& U0 A9 |- >>> math.copysign(2,3)
- y% }' {$ t" N0 t - 2.0
) O$ b# k: o3 y! x - >>> math.copysign(2,-3)
( K2 p7 ^, n; \6 k5 j - -2.0
7 f/ a/ I4 I1 Q0 f | - >>> math.copysign(3,8)
) p( D* G$ Q! s; {! I" M( _! n - 3.0
c' k' q6 }* b$ Y" W% C - >>> math.copysign(3,-8)
, b/ U. E9 x0 W - -3.0
复制代码 & P8 ?/ |, q, r, }
math.exp(x) 返回math.e,也就是2.71828的x次方7 ^0 g/ V, q: c0 a& i
- #返回math.e,也就是2.71828的x次方
! V, h7 Q$ W# X2 `) E - exp(x)
' C4 j) F0 y; J - Return e raised to the power of x.+ U. }5 [( N5 t4 t) T5 U/ M4 `
- + s5 ~5 S# j K: h9 F- }& U
- >>> math.exp(1)2 i6 p. P! R# n; v4 Y% r# n; C C
- 2.7182818284590454 ~8 h: _/ a. V% z* ]7 j
- >>> math.exp(2)
* a3 B$ x: b$ B - 7.389056098930650 B1 v) E5 L& ]! j1 U
- >>> math.exp(3)
4 V" C2 p- d" g2 D* S - 20.085536923187668
复制代码 2 O1 \7 A) P% T
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
6 B D5 x4 _+ u. r; T O6 Y5 [! m- #返回math.e的x(其值为2.71828)次方的值减1
- P- ^0 y0 e2 w1 I$ ?* H - expm1(x)- u9 H; n' Q+ _- C$ u5 ~, g6 p
- Return exp(x)-1.
/ r! L0 L h. L3 W- s0 G9 O - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, M) J2 c- A- t" @/ W: Z# l
. C1 T1 H+ u* W& M4 S6 M# @$ S- >>> math.expm1(1)" f! t- j$ L5 G+ {
- 1.718281828459045! x* N: U$ O- ]8 Y6 R [; Z
- >>> math.expm1(2)3 W0 t# v p5 e1 o. }
- 6.38905609893065
7 \( S. ]1 R- u4 [, y, e - >>> math.expm1(3)
/ z" O T; w/ r5 Q3 d$ Q - 19.085536923187668
复制代码
1 s. @4 o2 L4 b# A9 Umath.fabs(x) 返回x的绝对值
# U: m8 a; [, Z9 {3 L1 ~1 L) ^5 B# w- #返回x的绝对值
5 F( Z! j8 m Y) T4 _$ } - fabs(x)3 z# a( G& |" Z0 Y# I' A$ {. v' |
- Return the absolute value of the float x.
0 g$ N! {. d; S+ {) U4 N - ! O/ g$ ?: h1 u" [
- >>> math.fabs(-0.003)
' x1 W. E/ a6 m" s* O - 0.003. Q7 y$ }$ X [6 V
- >>> math.fabs(-110)5 ~+ ~' _9 d3 h: j: M
- 110.0
3 W! V% q) F& C6 Z; M/ Z; ~ - >>> math.fabs(100)
/ `6 r& c, C" {) X( y - 100.0
复制代码
2 ^ M* y( n$ tmath.factorial(x) 取x的阶乘的值, F- y4 a/ B# ?6 a" b7 y# x
- #取x的阶乘的值
. n5 i4 X+ s, I$ d$ m$ t - factorial(x) -> Integral+ U% h0 M( `, L
- Find x!. Raise a ValueError if x is negative or non-integral.1 H8 `, j* Y. q8 B) l
- >>> math.factorial(1)
: d: k" B1 }# U/ d0 ?8 U& {: I, J - 1
! g2 z3 V* ?6 E) ]4 V1 ]; S( x - >>> math.factorial(2)# x4 ~; S( G% m7 Z4 d
- 2
- ]! [1 B: x6 a% q( n' V* H - >>> math.factorial(3)4 u+ c- |7 X7 Z- w: G4 [7 D3 i @) Z
- 6
& [& l& E/ ?- W1 M& \ - >>> math.factorial(5)0 B7 d& D b, ]
- 120
9 o5 u6 C7 q. d* K4 V7 O7 K6 e - >>> math.factorial(10)
0 J' c3 }& H7 w! z7 D# z - 3628800
复制代码
' }5 K; n: _7 Z6 ?# R. Amath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
% q0 L9 ~3 N6 N8 ?: s- #得到x/y的余数,其值是一个浮点数
& _! O' I& f) T4 `: ?! r! r - fmod(x, y)
0 u& D$ n- F, G1 L/ n6 e! S - Return fmod(x, y), according to platform C. x % y may differ.
& f+ @8 M6 ]; Z9 E6 x: q - >>> math.fmod(20,3)
# d$ C5 H2 b3 _5 D0 A" c - 2.0) S: E$ w3 G) n4 e) l, g6 V! X1 z
- >>> math.fmod(20,7)! }: J8 c* x( k# p
- 6.0
复制代码
2 M7 @+ W" w* K. p* Z1 a: d& v4 tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! ?$ d. L5 R& F2 `- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
7 S8 M( V+ j! }: h/ n - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
! c$ V0 v {9 q6 y5 t( E( F0 o: c - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, K7 _& T N; N
- frexp(x)8 d9 s. C1 s* N& B8 d2 L* B! m
- Return the mantissa and exponent of x, as pair (m, e).) W e2 O) }& Y! d* \
- m is a float and e is an int, such that x = m * 2.**e.
0 i# u4 E" ^. Y5 L( ^ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
% q' s5 u; M% _% Q& w2 {$ P - >>> math.frexp(10)! g7 n8 |+ b. r
- (0.625, 4)8 O0 S7 |5 u# C3 K5 G" A0 f
- >>> math.frexp(75)
* ]8 i) X; `+ @" ? - (0.5859375, 7)7 a- p' u% k" Q# D4 x$ j
- >>> math.frexp(-40)
9 i7 n/ Q% Q1 U' T3 u - (-0.625, 6) C) Z' f# R& v4 C
- >>> math.frexp(-100)7 ~+ ^$ M; s$ g& ~; Q n2 k
- (-0.78125, 7)
- n5 ]2 c; d! Z& s% ?0 p% W; n - >>> math.frexp(100)6 f5 H9 T+ q. ]6 s9 H* j1 n
- (0.78125, 7)
复制代码 4 K" K( y+ K/ I _. X9 A5 {
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列). `+ i! X6 c' I6 E: i
- #对迭代器里的每个元素进行求和操作
) X7 c( q) n% i) M3 `8 O+ O - fsum(iterable)1 L: i: V+ p; j5 [3 a3 s3 o
- Return an accurate floating point sum of values in the iterable.
( L" r$ ?; h: [0 ~! D3 J% h - Assumes IEEE-754 floating point arithmetic.5 z' }8 ?/ G' Z
- >>> math.fsum([1,2,3,4])
9 X7 j4 ]7 g/ m3 W: E" A0 p8 y# j - 10.02 v1 _6 k" Y' t1 _9 m# k
- >>> math.fsum((1,2,3,4)) o8 Q, V) u* g' f, Y6 H2 l% X
- 10.0
; O1 Y7 D+ g4 N+ Q+ O( p/ s - >>> math.fsum((-1,-2,-3,-4))0 A: N( i0 I2 _6 D5 c8 A; K( I* ]) W" k
- -10.0! c' ]# l+ S( z) O
- >>> math.fsum([-1,-2,-3,-4]); |# W" @: Q. ]" P5 U) l6 d
- -10.0
复制代码 3 W5 _- r9 g9 Y. h
math.gcd(x,y) 返回x和y的最大公约数/ X; l- W- |$ Z k
- #返回x和y的最大公约数
, J- _/ |, ^1 Q w# h - gcd(x, y) -> int
8 D/ d, C' C; S- V - greatest common divisor of x and y
; B3 G# j, u! Y1 T3 A6 M7 b - >>> math.gcd(8,6)
% X; s& X: B2 ?' O - 2
3 w# Q& n4 x) H$ r6 ^- H/ j" y - >>> math.gcd(40,20)$ v# w; M# w1 |2 i
- 20
, u7 l* n$ V& V+ G - >>> math.gcd(8,12)7 I0 X9 B! V4 ? A; y! M% L5 K
- 4
复制代码
5 f/ q$ ^2 l/ y2 t: l* [8 o/ Omath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
! _1 }* G6 v% n. J- #得到(x**2+y**2),平方的值- l5 v1 m3 p6 W, @/ [- Q* B
- hypot(x, y)
7 y* [3 v+ Q- x+ i - Return the Euclidean distance, sqrt(x*x + y*y).% k" X2 d- f+ f# D
- >>> math.hypot(3,4); ]% L+ ^. U V5 c% h$ t
- 5.0# o% Z' ?+ i- E; i: g
- >>> math.hypot(6,8)0 z/ A$ l5 m9 q) b- B- \ X
- 10.0
复制代码 . l# B" }3 a) y9 r, }
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 @5 N A6 H9 H/ P4 m- #如果x是不是无穷大的数字,则返回True,否则返回False
6 x; Y, D+ F2 i/ b, N - isfinite(x) -> bool
2 d- J. |. m) D3 v: \ - Return True if x is neither an infinity nor a NaN, and False otherwise.
9 E) i3 C. L' P3 F# v - >>> math.isfinite(100)
( D& r4 N1 s1 N2 _4 ^ U - True! D" {2 `1 f5 y1 m2 b
- >>> math.isfinite(0)
9 N# h) u6 T5 F0 x1 T3 P9 P. I& P - True
' S2 H% n. J; [% V0 Z$ u* i - >>> math.isfinite(0.1)
2 p5 [- q4 z: ~8 L. ~2 _7 U - True5 i# e6 U {: R5 v% x+ ] s
- >>> math.isfinite("a")
3 Y( o' C8 X1 S - >>> math.isfinite(0.0001)
4 Z1 Z! r5 V2 R - True
复制代码 1 m9 S$ |% w: _" I
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
u% o$ |+ B% Z W" j' c- #如果x是正无穷大或负无穷大,则返回True,否则返回False
, `0 P ~+ h; Z& r: v - isinf(x) -> bool/ h1 R Z% b' H' K) H
- Return True if x is a positive or negative infinity, and False otherwise.
: `. l; [4 \$ Z8 R' g - >>> math.isinf(234), w6 T! c+ o" ^
- False
+ t+ C- J; B6 z, x# v3 {0 z! z' s - >>> math.isinf(0.1)
+ I3 `4 q5 M) ?% {( J, e5 X - False
复制代码 % h1 c5 z* _5 _, @: K) w8 d# T: n+ a- Z
math.isnan(x) 如果x不是数字True,否则返回False" h9 e9 X; F5 o
- #如果x不是数字True,否则返回False% a+ M- C( X7 C+ o7 I/ u. O
- isnan(x) -> bool
) u3 y6 D+ L( A( a( O9 ] - Return True if x is a NaN (not a number), and False otherwise.: B+ |6 K. I* m4 v8 ~, c. c) M; r' F
- >>> math.isnan(23)& u& V q2 J! M9 @ ?# y: p5 O8 Q
- False
% O7 n( ?0 S8 n2 b7 Q - >>> math.isnan(0.01)
5 a$ \3 @3 g" m, H/ u+ q - False
复制代码
- ] M& z3 c$ ]) \! U8 g: Bmath.ldexp(x,i) 返回x*(2**i)的值+ n/ \0 a' i9 |$ f: x
- #返回x*(2**i)的值
, k$ v% z) I2 z7 M. j, w - ldexp(x, i)( [1 O0 {% ?( p+ j3 x
- Return x * (2**i).0 F7 I0 u6 s6 R) o* w
- >>> math.ldexp(5,5)1 j2 H% l$ o6 A/ `" C
- 160.0
& n4 A- g6 e& `, C4 ~# Q1 v# W - >>> math.ldexp(3,5)
6 \% S5 T% `9 v+ a, o3 [4 j; I6 U - 96.0
复制代码 6 o% @6 e7 P2 v) z' x2 I3 l
math.log10(x) 返回x的以10为底的对数 |8 u d& A: `+ Q. G5 B4 q
- #返回x的以10为底的对数
/ K: J9 g0 o9 y9 v - log10(x)
* L2 m' Z3 i2 O# e8 t - Return the base 10 logarithm of x.% Q; `4 |" G2 N$ s
- >>> math.log10(10)# b7 A# d! t! c
- 1.0' [4 C1 B) W4 {' l: S* H1 I
- >>> math.log10(100)) ^# s k, I& X( ^1 V! I d
- 2.0
2 j1 m' r P8 p/ k9 F0 A - #即10的1.3次方的结果为20
# e" v1 t+ m3 b - >>> math.log10(20)4 ~0 O+ m K9 j' y3 e
- 1.3010299956639813
复制代码 + m1 J9 l5 s2 f* t6 _
math.log1p(x) 返回x+1的自然对数(基数为e)的值
4 _* G9 [/ ?; H7 t- #返回x+1的自然对数(基数为e)的值2 o' h$ l8 o+ x* h9 r+ Z
- log1p(x)
/ \6 B7 ?6 J# `; R! }2 y/ C - Return the natural logarithm of 1+x (base e).
/ q2 `: L% h0 H: s9 V6 l - The result is computed in a way which is accurate for x near zero.! h* p! j+ |; z }* W$ |0 w# T) n
- >>> math.log(10)
# M# s+ c# z! B) B - 2.302585092994046. l' o1 b! z+ [( ]! c q9 h4 q
- >>> math.log1p(10)
7 P" y7 H# ^6 Y9 T! x' ~& O - 2.3978952727983707. m8 C5 ]& |) W5 I! e
- >>> math.log(11)
9 c3 V0 F; v' N" T8 S! g W1 n* d - 2.3978952727983707
复制代码 B$ ?: v6 F1 Z$ q
math.log2(x) 返回x的基2对数
) R2 X8 I" c `: q1 q& _8 q1 L- #返回x的基2对数& p, E$ m9 N# q' T% a( r
- log2(x)0 Y- y7 x- V2 `" d
- Return the base 2 logarithm of x.
% Q9 x. E1 g/ m/ O% \0 l - >>> math.log2(32)' V F% Q% U- h8 r' M
- 5.0
# g+ ?! n, X; V8 J - >>> math.log2(20)1 `' ^* ^, ^% |* B' H8 ?
- 4.321928094887363
; u& h8 K" T- X7 f! z - >>> math.log2(16)# ? b2 R# Q. a: I2 |
- 4.0
复制代码
7 X- Y7 p7 i) V: R3 _, _/ ~' ^math.modf(x) 返回由x的小数部分和整数部分组成的元组' e; R, u$ r" V9 {9 W+ k/ O. [2 p+ o
- #返回由x的小数部分和整数部分组成的元组# C3 W* z' S( }( R0 k/ p
- modf(x)" G8 r; Q* E' B1 f- p- a& T
- Return the fractional and integer parts of x. Both results carry the sign% E* [/ w. X( A2 a
- of x and are floats.1 X0 Y* Y3 G w) I8 P& B$ E* z3 {! \
- >>> math.modf(math.pi)
; X0 P' P/ i. ^: @ - (0.14159265358979312, 3.0)" P/ f. l( e, i3 V
- >>> math.modf(12.34)4 ~3 M2 }3 _4 W4 @0 |
- (0.33999999999999986, 12.0)
复制代码 : A, F0 {9 o: K; w
math.sqrt(x) 求x的平方根
8 I! O1 S( c: R1 y! I& J- #求x的平方根
0 s, O8 i+ ^+ U: |$ N2 w - sqrt(x): I2 Z! B+ t3 o- b- [' {5 t9 U
- Return the square root of x.5 I% X% }9 ? e7 `0 P. j
- >>> math.sqrt(100)- V2 c6 A* O! E
- 10.0
n2 t0 u$ K6 F% Z1 i3 Z/ O - >>> math.sqrt(16)
$ ]) B" L7 R* Y0 E# m5 | - 4.0/ {- ?% W# g9 U" V
- >>> math.sqrt(20)
j7 l& R0 v% i. y B. o x6 j - 4.47213595499958
复制代码
7 v7 m9 R% X& tmath.trunc(x) 返回x的整数部分- #返回x的整数部分' e( _. T% ^6 y
- trunc(x:Real) -> Integral3 {, R' W) i9 c' z8 h" l
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' p1 `+ r; L) v1 ]- C6 O5 \
- >>> math.trunc(6.789)
# R3 V/ E. Y' k" @$ ^: v. z( q# k* k( | - 6; B+ Q3 A, M0 K+ i
- >>> math.trunc(math.pi)/ I1 o: O4 g- h7 h' p3 z
- 3
; d0 p- c5 d8 |4 E( a - >>> math.trunc(2.567)+ R: \, k( ~* X0 i1 W8 ]/ m# I
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|