马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) r3 H, c _9 s& q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。) g% x* G) \4 {" j
0 O# h6 j7 J5 T6 M
方法1:
% C% \+ M t: b- >>> import math6 x6 H: I" k5 G, n
- >>> math.sqrt(9)) d- w) B( ^. b! {- q$ \, \
- 3.0
复制代码 方法2:3 h+ D6 d, V. r$ z/ K
- >>> from math import sqrt6 X- g8 |& b) T( z( ]0 }
- >>> sqrt(9)
# l, S. Z4 e* z) s3 y/ Z - 3.0
复制代码 ( q; h$ Y b) S1 {4 w' Y* r# ^
# } ^: T& v3 Omath.e 表示一个常量$ X) x6 }4 z' I4 ?3 R
- #表示一个常量
% q+ z* o+ n+ p% } - >>> math.e |# y; J! P; W i, t
- 2.718281828459045
复制代码 ; g3 w1 S" x& ?0 w; J8 c6 D1 K8 e
math.pi 数字常量,圆周率; }5 I( E& z- { Q8 m. z" k
- #数字常量,圆周率
( V" `% P+ e: S8 l* u+ f& V4 q, k - >>> print(math.pi)
( l& j. h: a: [2 F7 W/ J+ @ - 3.141592653589793
复制代码 $ Z( N% L$ _ M' H5 t
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x; ]- \, {! M- \" I9 O
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x" J! _3 k0 c- N/ r- T: U
- ceil(x)( i, N: [1 F6 i3 ^: ~. Z
- Return the ceiling of x as an int.
3 |4 J7 `/ R! i+ }/ P5 E - This is the smallest integral value >= x.
* ]5 G6 y7 L8 v; f) a
0 C( h0 C9 ?& h) @0 `8 t) D- >>> math.ceil(4.01)
: {( W: p! m2 N$ y9 U( T - 5
: y0 l7 t0 z0 h4 [ f6 Q - >>> math.ceil(4.99) i' ]$ `* h; P2 A. j- u0 [0 Z
- 5
. Z A+ }6 ^4 H - >>> math.ceil(-3.99)
% y( N7 L. Y, I" e( s$ | - -3
: m: P* e; E& i% b% _/ @4 M3 n - >>> math.ceil(-3.01)
" c: t1 O# R$ F8 X( x5 B, X# n - -3
复制代码
3 `- F% B9 E9 ]( g4 |* a/ ^& k& Umath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 }0 z+ v0 H6 a
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 ]5 ?( Z+ ]4 U9 T/ W0 }+ R
- floor(x)' e! Y3 d$ ?0 F8 c; [# m
- Return the floor of x as an int.
! B3 |2 z$ p: n - This is the largest integral value <= x.# M5 b& j3 x7 M7 k
- >>> math.floor(4.1)
7 _7 i7 F( o, N* ?; [7 o0 F2 k - 4
4 O" I. @9 p) D8 c7 d - >>> math.floor(4.999)
9 X t* G" s& i% p - 4
& f0 V; T% Q( n7 K6 v- G - >>> math.floor(-4.999): o4 g% K$ r3 V8 U# p* s$ y( K$ k# U
- -5
$ c# m e" c, D- O - >>> math.floor(-4.01)
" x3 q% f4 S5 i& {3 Z6 O - -5
复制代码
9 x2 F1 ]. H4 M* _- } D' O Emath.pow(x,y) 返回x的y次方,即x**y
/ E( M6 S: Q* y: e4 k: q) j- #返回x的y次方,即x**y0 i# } H; k9 Q8 K, s
- pow(x, y)
" z' y$ `/ M/ ~. H& P" F k - Return x**y (x to the power of y).5 k. |) E$ p5 `0 Q b
- >>> math.pow(3,4)
3 P: n @! o* Z* r' _. ` - 81.0, q6 P+ y8 f2 C" u- d
- >>>
( V- m9 |# n/ [5 Y - >>> math.pow(2,7)$ }+ m h, Z( J& k: o! w7 b
- 128.0
复制代码 , S0 i! r! ?1 R$ Y: u
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! n$ p v8 }# R0 P" Z! N$ x8 T6 O
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): s; o$ y' U/ k1 J, S; i" U* {1 K
- log(x[, base])
7 L* L& Q0 G+ M' _$ J4 C7 l% n - Return the logarithm of x to the given base.
& G2 K: h8 ~0 p& i: m( |: v% c - If the base not specified, returns the natural logarithm (base e) of x." R5 G0 f: L" \; O$ L
- >>> math.log(10)
& o5 Z* ~* `: D a - 2.302585092994046# Q" d; R% P. ^& q. L, |2 Z
- >>> math.log(11)
3 |2 c6 D5 m- `# ]1 V8 U - 2.3978952727983707
7 j+ B2 L/ s/ [! p; R - >>> math.log(20)3 x1 c6 ^3 p5 o/ `, e- U
- 2.995732273553991
复制代码
4 l3 C3 Q+ L) k) F+ Y9 cmath.sin(x) 求x(x为弧度)的正弦值- U, k/ Y. N1 s: F* |
- #求x(x为弧度)的正弦值
1 g+ N: P9 A6 Y* N, Q& P% S, p - sin(x)2 ?. W# X, Z2 l8 r
- Return the sine of x (measured in radians).4 T2 o. i' ^: P1 F! A- Y0 @% m
- >>> math.sin(math.pi/4)
7 I& Y! s$ L0 w9 \0 B4 f - 0.70710678118654750 A6 k# D1 S; v3 L6 Y2 L7 M4 r
- >>> math.sin(math.pi/2); O5 W4 d4 i5 O, }2 l8 e8 E
- 1.07 e$ C0 } `9 Y+ c P6 N1 a& G
- >>> math.sin(math.pi/3). L4 I* P; e* J# V
- 0.8660254037844386
复制代码
, v; z# W" H! L5 G: H+ jmath.cos(x) 求x的余弦,x必须是弧度1 S' _; R6 S' Y% P( m- T* `
- #求x的余弦,x必须是弧度' Y u+ _% ^+ I& T: ~4 P+ V5 b2 z. ^
- cos(x)! x% I& M+ i/ t. q# k5 n' L+ v3 p
- Return the cosine of x (measured in radians).# u/ U, i7 `' d4 N% W9 F) N
- #math.pi/4表示弧度,转换成角度为45度- F; d' }+ h6 \* g. T4 q1 H0 p
- >>> math.cos(math.pi/4)% u" @- u/ l& T, \7 ~& N# K, X8 d
- 0.7071067811865476
4 X+ a- d/ n. N! O1 T* K; Q3 b - math.pi/3表示弧度,转换成角度为60度( W% s6 I( g% d6 T% d4 g* I4 E$ V9 c
- >>> math.cos(math.pi/3)
p' k7 B0 n6 @" j' a - 0.5000000000000001
8 U9 q: Y( e4 W; H. Q( J6 d u - math.pi/6表示弧度,转换成角度为30度
1 Y4 ^' V& w4 T' _" Z - >>> math.cos(math.pi/6)& I: ]8 h9 X5 K* H
- 0.8660254037844387
复制代码 4 T& ?; h. f. t5 [
math.tan(x) 返回x(x为弧度)的正切值
1 k% E7 y4 Q8 H4 |: i3 d8 v- #返回x(x为弧度)的正切值8 K. D% T+ G; W; L$ J- K0 x. \
- tan(x)
9 F5 D0 z; q0 f2 M, J! P - Return the tangent of x (measured in radians).
- A3 [2 X# s- Q' |/ }+ V0 ` - >>> math.tan(math.pi/4)
' Q( M, b4 q2 {8 r3 a - 0.99999999999999993 @: E" [, \, p3 }& a
- >>> math.tan(math.pi/6)
1 f# w/ ^+ H ^! Z: T, y - 0.5773502691896257- `, M& `4 R: r$ f
- >>> math.tan(math.pi/3)7 v0 o6 k( [( h2 i5 S% p. |" M
- 1.7320508075688767
复制代码 ( X# y9 h Z8 d+ I( Q
math.degrees(x) 把x从弧度转换成角度$ a2 A& I+ y0 e# v: U, }' H1 @) X+ Z
- #把x从弧度转换成角度9 e6 b% c: F* h3 S) r |4 {
- degrees(x)
1 J. I3 ~$ M. L- _7 W0 |# C. q - Convert angle x from radians to degrees.
7 w0 O+ [& `$ t: E1 j' }. |- x7 x' Q9 y - - v' P5 H0 S/ O7 S' N$ M; w1 L
- >>> math.degrees(math.pi/4)
* H0 _. x2 d7 ~2 S* x3 r+ E - 45.0
0 ?# b! d# N& J$ g2 ?3 p - >>> math.degrees(math.pi)( U' m: G. C: f* y; j8 }
- 180.0
$ @( e2 y5 c, O- { - >>> math.degrees(math.pi/6)
/ X" i2 g( } t( O) M - 29.999999999999996
6 S0 u2 e- I/ o h' \ - >>> math.degrees(math.pi/3)% X4 W# y: J: s/ f
- 59.99999999999999
复制代码
8 V$ q4 U( J8 E) Q, X. \math.radians(x) 把角度x转换成弧度
5 q3 V* e7 j, j! P- #把角度x转换成弧度. @0 i: B6 m2 D8 t7 K. {- ~9 _, B
- radians(x)
8 A$ K* k( d+ O+ z- v2 k9 @7 v+ ^ - Convert angle x from degrees to radians.) d# {# B/ b( w% N4 h
- >>> math.radians(45)9 Y: Q% T! n1 |. m1 [
- 0.7853981633974483& x! F$ o9 t8 ?! i) ~2 ~- J- L+ v
- >>> math.radians(60): l1 b0 Y/ p3 U$ e& L% V9 b
- 1.0471975511965976
复制代码
1 C- y; K# ~( J$ P0 Ymath.copysign(x,y) 把y的正负号加到x前面,可以使用00 r. Z7 A3 I" p
- #把y的正负号加到x前面,可以使用0
* g- h' Z4 Q4 U9 ^; ~ - copysign(x, y)
' H, |4 W1 ^+ J$ e# [& N0 |, f3 ?. A/ h - Return a float with the magnitude (absolute value) of x but the sign
3 ~- B8 D! k. c5 K! L6 h. g/ H - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 4 l! `' ^9 P/ _" d& R l
- returns -1.0.
* Y- i( R% t/ r8 ^+ k1 J
2 E7 \* G2 T" z" X- >>> math.copysign(2,3)
9 p: {6 U/ U8 }$ |! k - 2.0. [# p4 L3 d. t* o7 f
- >>> math.copysign(2,-3)
8 A3 q% m; {9 e$ ~7 s- p! a - -2.0
% {' ?/ A* h3 x; P/ h( i - >>> math.copysign(3,8)
* D% e: K6 U) L) `7 H - 3.0% D" l( m% M7 R3 Z" h$ o
- >>> math.copysign(3,-8); ?* l9 [# m. D# k4 M! G8 y
- -3.0
复制代码
* U) W8 i9 m* y! Bmath.exp(x) 返回math.e,也就是2.71828的x次方8 _, T. E( _6 ^' a6 ?
- #返回math.e,也就是2.71828的x次方
, a+ j" M; @( L" h- f - exp(x)
) C) P- D. H+ e M - Return e raised to the power of x.
3 c+ x# d( @) ], z' H - + f2 m/ \1 ]% f' o. o
- >>> math.exp(1): r" b( r- m; L6 d# @* F z
- 2.7182818284590452 K8 _. K1 G1 x1 h
- >>> math.exp(2)0 g3 X/ a H$ h3 q0 I+ \( m0 X
- 7.389056098930656 u( ]; F! s- W1 l. F
- >>> math.exp(3)2 {7 X! s9 m& m
- 20.085536923187668
复制代码
, u* W7 \4 I4 ?/ imath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1: A2 p* O/ O) A |) | m! x
- #返回math.e的x(其值为2.71828)次方的值减1
4 @8 O+ H2 }; ?! B - expm1(x)# Z6 L9 C7 ]& U9 M( S
- Return exp(x)-1.
% j2 [2 S; N1 L# N2 n0 H' Q - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
5 q( _9 Y: s- S9 m
, r' [: {1 r+ ^- O \/ M3 Q+ ]7 @- >>> math.expm1(1)6 J# m/ S; x, i8 j+ m. u/ q
- 1.7182818284590456 m9 E' W6 i# Z. g" S1 o
- >>> math.expm1(2)
" |, e2 m% L7 r - 6.38905609893065) }0 w) o( }6 T+ N6 L: B8 I
- >>> math.expm1(3) v3 G. H7 t9 W: M7 S+ F
- 19.085536923187668
复制代码
; U$ P/ \7 n8 {- S" nmath.fabs(x) 返回x的绝对值
- O! R! f- c7 i/ }& |) x- #返回x的绝对值7 k" J* Z% L1 L" h
- fabs(x)6 Y& |4 }& B, _; D8 r. q. A
- Return the absolute value of the float x.+ K5 ]% a8 H2 T: P2 b5 i
- / Z& g" O O' w/ v: W
- >>> math.fabs(-0.003)6 J) c' Q( J1 }
- 0.003
' c/ C; v% K1 P5 f; l+ c - >>> math.fabs(-110)- d7 N. m% W/ P: p
- 110.0$ n9 o! j7 A2 [( Y# E8 j, p. | P
- >>> math.fabs(100)
{, C* F4 r5 V. z+ X- q5 t5 z - 100.0
复制代码 6 O1 Z; K T, ` M) P) l8 B; ^
math.factorial(x) 取x的阶乘的值1 v5 ]2 ^4 y6 I* p: w
- #取x的阶乘的值
0 ~' s3 ] m6 u - factorial(x) -> Integral% g! e9 K0 e+ S! I5 i6 O( D
- Find x!. Raise a ValueError if x is negative or non-integral./ ~" Q0 c' X) _5 Z
- >>> math.factorial(1)& s M5 w5 _6 A) e3 h& r' A/ x
- 15 ^' q6 p7 C5 {
- >>> math.factorial(2)
, C% g' s: z# E3 ?' t' b - 2/ g0 M% F0 ^; `( G
- >>> math.factorial(3)) v( @# k/ m0 Q2 J
- 6
! S/ I2 S5 F/ Z/ h, f/ U0 r' R - >>> math.factorial(5)
% J1 w; x# T3 H - 1206 z( X$ d% m+ C1 Y, V3 I( h
- >>> math.factorial(10)7 X# ~# t1 T7 y, t2 r
- 3628800
复制代码
" V/ A' n- e+ @! Z! P2 l! H& M5 cmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
[& P2 K9 n5 }3 _# Q. d- #得到x/y的余数,其值是一个浮点数% W& a' Q$ g. ]1 A6 H9 [' v/ |
- fmod(x, y)4 e, l$ G! n0 d7 h0 d
- Return fmod(x, y), according to platform C. x % y may differ.# X2 e: }; j3 Z
- >>> math.fmod(20,3)
8 c" \1 A# y5 a9 _$ |6 | - 2.0& }. A$ E2 n8 R0 O" U# c: `& v E
- >>> math.fmod(20,7)
. `. e% u6 z! E9 | - 6.0
复制代码 ) X) t* d# n7 t+ R: u9 d
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 U6 ~. q; V4 ^, u) l7 \3 n1 u3 u0 L$ E- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
* U+ D( o$ i* a: S - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
4 }8 H1 I" V4 ^ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1+ b5 ~5 G8 E: ~8 ?( y+ e# ~
- frexp(x); w! {( c* H; v
- Return the mantissa and exponent of x, as pair (m, e).2 R0 u& B5 R3 o% N& }/ x0 x' z
- m is a float and e is an int, such that x = m * 2.**e.# I" \& b, F: f) T
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.: g* v& r( }9 i% E
- >>> math.frexp(10). b* ]0 \1 p, g% d @- l
- (0.625, 4)
$ ~) r. N/ [4 o' } [9 [+ h - >>> math.frexp(75)4 k4 Y" i. B( _6 v! Q
- (0.5859375, 7)8 ]2 X( Q) t) ?# d
- >>> math.frexp(-40)) |, k$ g! {7 g8 j* p6 C, t8 b
- (-0.625, 6)* N% l: I( u6 @5 h9 b& I# l( b# B( N
- >>> math.frexp(-100)0 M5 _& N+ @6 F
- (-0.78125, 7)
1 b. C4 X6 l2 R - >>> math.frexp(100)6 e' I5 T& }8 i/ _1 e
- (0.78125, 7)
复制代码
" b/ n: j( Q/ J# I% ~5 y6 i( smath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
3 c8 f- L/ _1 a/ ^- #对迭代器里的每个元素进行求和操作+ y6 ]/ L/ ]6 V
- fsum(iterable); x0 _5 O/ t1 B+ ^' j$ N
- Return an accurate floating point sum of values in the iterable.# l0 s9 |; o. d
- Assumes IEEE-754 floating point arithmetic., t: I$ j# h! l$ _3 ~, G$ p
- >>> math.fsum([1,2,3,4])
& [- @( ?, e; Q; A, k5 c; O+ e1 f - 10.07 ?- j. q* \7 m; l1 W
- >>> math.fsum((1,2,3,4)). b' D; I6 r# x! \1 t
- 10.0# z. \. D6 T' l2 L% y
- >>> math.fsum((-1,-2,-3,-4))
# J$ R# N$ R+ l! e - -10.0
" a( Y+ _$ k" I& `& T) o) M: b - >>> math.fsum([-1,-2,-3,-4])
6 s' O; u, A6 d/ L1 E& P0 ? - -10.0
复制代码 2 a* G8 X7 [1 a W4 X+ c
math.gcd(x,y) 返回x和y的最大公约数; f9 x6 Z9 d, P
- #返回x和y的最大公约数
- y! `. G" W2 _5 W3 h5 @) Y - gcd(x, y) -> int9 u4 M( a/ O7 J" {% F8 T6 ?
- greatest common divisor of x and y
1 a* z7 K. ]1 {& q% ^; b - >>> math.gcd(8,6)
0 H4 e2 z$ p* j" C# {: f8 ?# f4 r - 2/ Z$ U2 k- d' x! E' ]9 I% S
- >>> math.gcd(40,20)
4 w' w2 y- b4 G) T: h/ c - 207 P9 {! s; G/ M1 N" U5 n
- >>> math.gcd(8,12)+ L3 l J! y) ~' s( ]# r
- 4
复制代码
" A2 b2 H( s8 @' s3 S" ?math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
% q, v9 |0 K L/ G+ I) C( ?- #得到(x**2+y**2),平方的值
6 @3 }5 k6 U% w7 k4 P - hypot(x, y). M3 N+ C0 e. d& c }* x/ p
- Return the Euclidean distance, sqrt(x*x + y*y).; l9 m* t4 b1 g3 {) C
- >>> math.hypot(3,4)
) j. V, I0 p& }1 B5 S. d/ a; W - 5.0$ D: b5 Y( X2 I) I" e9 _( z- j; Q( Y2 d0 G
- >>> math.hypot(6,8)
: { @7 T0 x! }4 k& \8 z w5 f - 10.0
复制代码 7 x8 D( u: O( h3 t
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False- X: C, c% K& n$ L7 J; u" n, ]6 I
- #如果x是不是无穷大的数字,则返回True,否则返回False
2 e P( `( s/ M1 s' S4 v6 a" a3 _ - isfinite(x) -> bool) F! m( k/ Y- w, H; ^: I$ ~
- Return True if x is neither an infinity nor a NaN, and False otherwise.
6 H Y( ]# f' [+ G# l - >>> math.isfinite(100). Y8 C" C. _4 V8 i3 s! x
- True O% _: o* n1 F; C+ r1 T+ z. [4 u
- >>> math.isfinite(0)
/ E" v: p* f& n( f" j - True( P: C' m* w* k( g& k
- >>> math.isfinite(0.1)5 s4 {+ o5 D7 o1 T
- True
0 A; @: C) B+ Y1 P2 Z' N7 z; w - >>> math.isfinite("a")
`$ c9 s" c9 z- x- G5 l/ R: g3 z; V - >>> math.isfinite(0.0001): W# c; [0 F( C0 @) `- ?' z
- True
复制代码 % F7 o( ?: K4 X* j( S' X$ P6 a
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False* l3 S1 B6 M7 @. G/ t2 V) F
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
6 K$ t7 F: R2 }' m: M. G' e - isinf(x) -> bool9 ^6 p- x$ j4 L& b& H, r! [
- Return True if x is a positive or negative infinity, and False otherwise.
2 s3 F$ U- P7 [# J: i - >>> math.isinf(234)
/ c) e9 R2 |8 M1 d* b' k5 t - False6 ~6 i. U' E4 a) k' O6 A; Z+ o
- >>> math.isinf(0.1)& R5 S% ^& V. V5 p u, B
- False
复制代码 * u0 N. G: V+ e) q5 @) q! v
math.isnan(x) 如果x不是数字True,否则返回False# g W/ K% q# Y: {1 |. j! p
- #如果x不是数字True,否则返回False% O5 N* g2 w2 p% C$ S1 J; K
- isnan(x) -> bool
& {/ [+ n2 U7 l* o) M) G - Return True if x is a NaN (not a number), and False otherwise.
" O! L9 o" u: q W - >>> math.isnan(23)
4 ]- `5 |. }6 ?& H; I - False0 I. ] |6 X' [' n! z; W5 ]* s
- >>> math.isnan(0.01)3 X+ K( X) F1 @% B' i/ V
- False
复制代码 $ @* a3 L; w! t8 P' P$ @ H- D9 \
math.ldexp(x,i) 返回x*(2**i)的值
v- {; i# x% l$ x6 P- #返回x*(2**i)的值( ^7 D- _6 h9 j
- ldexp(x, i)
4 z. z& O e" }, T9 w! l4 H6 y* ]$ x - Return x * (2**i).
+ x4 g" s$ o0 C, t$ F. c ~ - >>> math.ldexp(5,5)) E) R5 K/ ]: ?" n( g( F9 U! ~
- 160.0
& _+ R/ d$ u3 [/ M3 j' d* C1 a( e) ^ - >>> math.ldexp(3,5)
5 A! e3 P$ a' M8 ~7 ^. M" D - 96.0
复制代码 ' J) f6 q, W# J# p, C2 k
math.log10(x) 返回x的以10为底的对数% U) v J/ e' H, a
- #返回x的以10为底的对数
3 I& E) U8 s9 p - log10(x)
! ]( Y( j8 C, G- S% J - Return the base 10 logarithm of x., u5 r- t3 C: }; y8 P5 Q
- >>> math.log10(10); k* Q3 I' S! M! p5 L# x
- 1.0, R6 o; h; V. O# ^! k9 x1 V3 d% I- ]
- >>> math.log10(100)
* W/ o& s( D8 J6 e# }: N - 2.0
, B* F4 n5 \9 M. @. k2 w+ q0 k4 T - #即10的1.3次方的结果为20: d! C, M# t. L& Y) @2 S
- >>> math.log10(20)& h# d5 k% _. |1 K# e
- 1.3010299956639813
复制代码 / r2 o7 e& D) Q f
math.log1p(x) 返回x+1的自然对数(基数为e)的值
( _& [* Z& V. n) [' E A* T' A- #返回x+1的自然对数(基数为e)的值( X' _/ J6 y; ^7 j1 K B
- log1p(x)
# L7 @# L. X3 L ? - Return the natural logarithm of 1+x (base e).: x7 r; [' J0 a& c
- The result is computed in a way which is accurate for x near zero." U9 {( R! E& C2 \; Z5 e; s; j
- >>> math.log(10)* G* U: `& f7 F. U. B
- 2.3025850929940468 i2 i- \9 p- K4 l( Y0 T# Z
- >>> math.log1p(10)
4 ?; Y6 t/ V; _ p8 u a - 2.3978952727983707: M! A" q; p8 M9 K
- >>> math.log(11)
- j: V: ^: ` h8 l" x j - 2.3978952727983707
复制代码
+ ~2 U: J7 H0 D& L0 zmath.log2(x) 返回x的基2对数
$ {+ e0 G' N6 e1 W& U- #返回x的基2对数
" \- F2 g6 n1 U& G* A - log2(x)" U" q& S: l; I" f$ [" n7 v7 r- t
- Return the base 2 logarithm of x.
9 b) u3 b9 u: | - >>> math.log2(32)% |. ?: j2 X9 L$ d( f% x
- 5.0
6 A1 W6 l9 p3 D - >>> math.log2(20)
5 S4 J, Q/ e- |) R9 f' x$ t - 4.321928094887363. _* z1 S$ A- _- Q( s4 P
- >>> math.log2(16)
$ Q# a" s; v I. K" A7 o( s* x - 4.0
复制代码 + q3 {3 s4 I% c5 ]- c
math.modf(x) 返回由x的小数部分和整数部分组成的元组
1 D- l! q! F; h9 S- #返回由x的小数部分和整数部分组成的元组
/ q6 o! \. T" L# H" U Y4 x - modf(x)
6 b4 _" [4 C7 V& p5 x( ? - Return the fractional and integer parts of x. Both results carry the sign6 N. Q& ?: v/ R+ Q; B
- of x and are floats.
/ I- l* L. `, ^2 X - >>> math.modf(math.pi)* b4 I9 z C8 N
- (0.14159265358979312, 3.0)
0 M# K8 v' d9 n9 A/ c5 V - >>> math.modf(12.34)8 a6 S. B! T" @9 \- y. @1 x
- (0.33999999999999986, 12.0)
复制代码
) C$ }) A( s2 N! X$ Z; Rmath.sqrt(x) 求x的平方根
$ w1 G2 F9 s0 r, @& E1 n- #求x的平方根; ?$ k' c: u; E" }5 w- d
- sqrt(x); }6 H; B1 N& t( v' i& W3 o" |! w
- Return the square root of x./ I4 M8 k( i0 \
- >>> math.sqrt(100)
8 K: `0 q. M; e6 o! A" m; S# w5 _ - 10.0: D6 P* f6 F0 M" J& j
- >>> math.sqrt(16)- ]/ R% h0 l- |# h) V
- 4.0& \* w7 {/ p* K1 l" B& f' I
- >>> math.sqrt(20)
9 X% X5 R) p; Z. O2 z2 v( o: n5 v - 4.47213595499958
复制代码 ) j( u( e* E6 o2 C5 r
math.trunc(x) 返回x的整数部分- #返回x的整数部分
/ f8 q5 F! D7 i; I, ~ - trunc(x:Real) -> Integral# l+ h! c4 z2 ^8 G$ }& I" v/ t
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 s! H8 H9 ]7 A1 y% D. u1 g
- >>> math.trunc(6.789)
) R) m+ p O7 `: P9 B - 6
0 A: O* ^! T8 X6 C! X - >>> math.trunc(math.pi)8 U" B7 p6 w2 r
- 3
# q! S, c P" u/ b% X - >>> math.trunc(2.567)6 l9 x6 U# a# t x
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|