马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
1 K+ M- t. n2 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
( j' G* ?) @/ @' s7 I! Z- B |+ f* c, ^! A6 M$ f
方法1:
# e: k3 j1 m( b6 h- S8 b M- >>> import math
" a0 n) z V* E: ]. z - >>> math.sqrt(9)0 L0 ~7 `" v6 h. {8 d
- 3.0
复制代码 方法2:+ A; V8 p4 {% m7 v" O; b$ e; I2 I& u
- >>> from math import sqrt
! J9 L0 N7 Y9 Y f - >>> sqrt(9)
^6 i, o! n% @. L, M$ G - 3.0
复制代码 : u! l- ^2 |0 O* x p+ {9 V, M4 s
4 v9 h% G! o2 Y/ c( l) ^# ~/ m
math.e 表示一个常量& N( o# b% J4 w' b8 m9 W% C* R
- #表示一个常量/ i7 @1 g4 b$ t9 V9 k4 }# F
- >>> math.e
/ _5 c3 p8 N8 n5 \- y" f - 2.718281828459045
复制代码
+ C$ p' ?* l N7 f/ {math.pi 数字常量,圆周率+ O2 e; q9 x$ F" N6 N# f8 ~
- #数字常量,圆周率
5 S* J. x& G: h& i - >>> print(math.pi): R0 p3 J+ L3 E" ~( i w, @! s
- 3.141592653589793
复制代码
2 z! m9 h9 O9 e; ^/ v; f# Imath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 Y; P. F) ^$ f$ i: ^+ Y6 _' d- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 O1 U, E7 z- X5 _; q/ Z2 X5 \* ~4 K - ceil(x)
- C3 ~' v2 U" u# H - Return the ceiling of x as an int.. v3 }" z6 y" d9 j. d: s% r6 @
- This is the smallest integral value >= x.
9 A: p+ O. ]4 C5 S
/ H4 |8 c" V) e f2 Z- >>> math.ceil(4.01)
% N5 @" b* d1 p* O7 W4 L - 5- n, p! V7 O/ S! @$ Z
- >>> math.ceil(4.99)4 n G& M j/ S& p+ ?
- 5
6 M2 S& a* |# u q3 s" h" K" O - >>> math.ceil(-3.99)
1 `/ k) w, W0 ^* u$ {' t V( o! d( ] - -30 i# u9 u0 ]2 r3 S9 N; ^7 K0 z4 Y) `7 O
- >>> math.ceil(-3.01)/ E+ r: G, J' A3 {* e X/ [
- -3
复制代码 ! h1 L6 Q" W3 ?6 h5 e! b
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ \' _! ]3 M4 p
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: n8 v" `, p0 S& ]3 R' c4 ~ - floor(x)
, D' ~% Y' o# [7 h1 n% a. _ - Return the floor of x as an int.+ Q W0 d6 R x! p! p
- This is the largest integral value <= x.( G: B6 N- B! R e; l( ]; s. S
- >>> math.floor(4.1)" I; g4 ?# U/ `: N
- 46 [+ M2 J" ~( F: u6 r( p
- >>> math.floor(4.999)1 [4 ^; j/ ]8 K
- 42 @- @0 |& T, @! p- Z
- >>> math.floor(-4.999)
; h& {, ^6 h. p5 d8 E; I: y( w' X - -5
' l3 U" |3 g+ \ - >>> math.floor(-4.01)
# l4 P% h' o' I# v& j# v - -5
复制代码 : X5 ?# y) E0 x0 u3 G% Q3 `3 Q
math.pow(x,y) 返回x的y次方,即x**y
; Z# g' F: b* f: y2 f- #返回x的y次方,即x**y( r }! k# ^ c
- pow(x, y)
) B$ n" O. x9 k: N - Return x**y (x to the power of y).$ F/ u8 j9 |2 h! g/ E; O: ]# @
- >>> math.pow(3,4)
5 u$ O, W' _. j; M- G! J& V5 F - 81.0
" m8 q8 Y! l, U5 |* k) X - >>> % _# w! c) d# n0 k( t2 l
- >>> math.pow(2,7)
% ]6 b& y2 ^+ _) g9 i - 128.0
复制代码 5 N j9 r4 \4 o! K2 u0 X6 i( W& n
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): s |- P: [0 ~7 \2 g4 Q3 j; _
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), `2 J- M8 @$ x
- log(x[, base])
1 \( o1 Q$ W, k7 e' p - Return the logarithm of x to the given base.
' O1 q3 K3 C: [+ h, U: ~: t/ s& u - If the base not specified, returns the natural logarithm (base e) of x.
& Q7 q4 s: ` u* q6 X3 x/ B8 U# q - >>> math.log(10)
* b, _+ C g' O& H2 V5 p - 2.302585092994046
! f+ ]9 ]( u% P% v( C$ L - >>> math.log(11)
/ m# T) T& p1 X" e2 w2 k; k - 2.3978952727983707
! J% n! O: i0 H9 S - >>> math.log(20)
( v1 H0 q' _7 t! ]2 E - 2.995732273553991
复制代码
( j( b) W2 P5 t4 p; n- q2 B8 jmath.sin(x) 求x(x为弧度)的正弦值
' w0 ?* O" l c6 f& Q% i- #求x(x为弧度)的正弦值6 T3 q: C" K' K( k/ v9 O8 `
- sin(x)
# O: a( T9 t- n) ~- P, Z# d9 V - Return the sine of x (measured in radians).
. g- i. L/ a" j: q$ e$ j& B - >>> math.sin(math.pi/4)
X: t$ ]$ V, p! e6 }" [ - 0.70710678118654758 K: C; c4 v5 X
- >>> math.sin(math.pi/2)
& t( T: X# i& T+ B1 {+ k2 B - 1.0
8 Q' |& Z5 E T* j - >>> math.sin(math.pi/3)
/ F2 w/ |+ _+ w) B* f - 0.8660254037844386
复制代码
' k4 i+ O7 _; f9 K9 i" r+ _ L* wmath.cos(x) 求x的余弦,x必须是弧度
' @" P$ B$ X* h q0 T) S1 Y- #求x的余弦,x必须是弧度
^# P2 Q! K' {+ w3 T - cos(x)
# T* Z: O% K' Q. o1 V( a( x% | - Return the cosine of x (measured in radians).7 `( ?5 a* u, V) p4 ?" u; n
- #math.pi/4表示弧度,转换成角度为45度
4 j7 l$ M* }2 K - >>> math.cos(math.pi/4)) R: K- w5 G0 N `/ O# a
- 0.70710678118654767 D \1 \3 l4 `) ?& g7 b' s
- math.pi/3表示弧度,转换成角度为60度3 }$ E- V; E9 P3 [; l% }( y Q
- >>> math.cos(math.pi/3)4 N. P# ~2 n$ P# R
- 0.5000000000000001
0 }& Q, o- F/ W/ D2 B, z - math.pi/6表示弧度,转换成角度为30度
7 X9 H+ \5 I5 p - >>> math.cos(math.pi/6)
" s; c5 y/ c1 R& a2 W: v - 0.8660254037844387
复制代码
& g1 G$ U5 V( N7 A2 `/ umath.tan(x) 返回x(x为弧度)的正切值* d5 B. \; U* O6 D& v1 \+ G+ z
- #返回x(x为弧度)的正切值" O1 N t% Z& o8 B+ }
- tan(x)3 R, @; C/ V% I# S9 S
- Return the tangent of x (measured in radians). ?4 |3 O7 k, A8 W: B" l/ Q
- >>> math.tan(math.pi/4)
2 W+ i# b& M. Z2 Y" a9 b - 0.9999999999999999
5 |6 O4 ?$ _- D& R8 d% s, g; R - >>> math.tan(math.pi/6)! d6 y; P3 J- j( M: R# j
- 0.5773502691896257
. P* s0 Y s8 M - >>> math.tan(math.pi/3)9 ^+ Q1 C- i( d# L- i
- 1.7320508075688767
复制代码
% N/ G0 R6 {% Y: M" B' {8 mmath.degrees(x) 把x从弧度转换成角度
0 S8 E* M/ o: a6 p7 Q3 z ^- #把x从弧度转换成角度: E: }# V% d! G0 a9 {6 ]
- degrees(x)* @/ U, F3 n; ?. k# K' k4 {3 z. I6 O
- Convert angle x from radians to degrees., u. M+ s9 D' n6 m& X2 u8 C9 i
- 8 k* d8 ~. N1 ~
- >>> math.degrees(math.pi/4)- Y% G+ G+ Z3 R R o
- 45.09 E. \( c1 \% S' _0 I
- >>> math.degrees(math.pi)
& y* V7 Q5 w' e0 R" l8 } - 180.02 m4 j6 ^; R- U5 j) \( q1 D7 m0 C
- >>> math.degrees(math.pi/6)9 j% b+ z8 z5 x i4 v
- 29.999999999999996
, x+ F' | ~% z5 p* t/ n - >>> math.degrees(math.pi/3)! i6 f/ H6 s1 x* n. I: G
- 59.99999999999999
复制代码
/ }! P Z l8 ~. b3 w+ N( e; Vmath.radians(x) 把角度x转换成弧度( Q; E& S; [% a% U3 g: S
- #把角度x转换成弧度
2 L# ~" F7 t1 K" F: l' Z2 E - radians(x)( V. a# _2 b3 F$ [3 R. Y, l3 g
- Convert angle x from degrees to radians.: \# E% F0 H7 t0 I8 Z
- >>> math.radians(45)
, x3 z3 |: h3 E/ r2 j. O, O- v1 w - 0.78539816339744832 w8 c1 ^7 d/ O" h& ]4 w5 o& s! f: D
- >>> math.radians(60)
_0 C3 @$ L5 w% t0 j - 1.0471975511965976
复制代码
. O3 W; ^; y8 H7 d1 cmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
6 S/ A1 z2 w: V! L4 G" l, b- #把y的正负号加到x前面,可以使用0
' W+ y' Z/ d( Z. o' c7 S - copysign(x, y)
" ?" u2 N/ K1 ^6 g - Return a float with the magnitude (absolute value) of x but the sign
; e; A4 H/ \, `/ w& h8 Y( G - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 8 s3 K6 _- y' j' g* y; k
- returns -1.0.3 k& {5 {* [: f. J( }: E( M; t
1 K' b. F) j: R, P. f! M5 ?# z; S- >>> math.copysign(2,3)5 L) r! x5 q! Z# T% x) t; `4 g! t
- 2.07 L. {' G- _7 y% K: P$ R
- >>> math.copysign(2,-3)4 @8 h3 ]& m) A( W; ]2 g6 y
- -2.0
. u. ?! S7 n/ z9 D: ~ - >>> math.copysign(3,8): J/ \7 _ M. a
- 3.0
( A% t" g7 D. }. e - >>> math.copysign(3,-8)5 ]0 w+ S+ }- Q0 ~9 i
- -3.0
复制代码 4 R$ `4 B- I0 Y& V8 W9 i- u
math.exp(x) 返回math.e,也就是2.71828的x次方
8 Z8 T- C$ w) G+ ]2 Q0 d" q- #返回math.e,也就是2.71828的x次方
' g8 q0 a4 L2 J; J* p - exp(x)
* w! L. d e+ y, X - Return e raised to the power of x.
9 Z6 v. ?4 U3 l; X - : r4 T" P9 h2 }2 I' v( Q4 }5 A
- >>> math.exp(1)9 F) ~% A6 ?8 k, t4 c, K m" m9 I
- 2.718281828459045
k% s% h2 W5 E7 n) |+ ~ - >>> math.exp(2)
- Q2 B: a# M, @: F5 V. j) _ - 7.38905609893065
, c3 k5 {( A; X3 i - >>> math.exp(3)
$ X, V& \+ A% l. s; i, Q3 d - 20.085536923187668
复制代码 3 b; `& [3 a I: S
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1& J! M! Y7 h5 G& v1 S7 U2 u
- #返回math.e的x(其值为2.71828)次方的值减1
- n; f; ?4 Q0 j9 u - expm1(x); Y# K, F6 ~- v3 k2 v. E$ m) U
- Return exp(x)-1.% W/ R: N7 N: l, { x/ J R2 m
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" T& h6 l8 W6 h+ ~ - 6 e' _1 O9 b) ]% U# [& b$ |
- >>> math.expm1(1)# ^2 _* A: \2 U3 x3 H4 l
- 1.718281828459045, m) j) p# ?8 `+ T6 Q g
- >>> math.expm1(2)
8 M( @/ S1 A5 w; V - 6.38905609893065
0 L- f, ~4 U9 b+ f9 f- Z$ y - >>> math.expm1(3)
2 ]4 f4 H1 e. ~# E# H! w0 j, h - 19.085536923187668
复制代码
$ v9 n: d( j2 K0 d |8 l' o7 _math.fabs(x) 返回x的绝对值! D2 k( T; q" I
- #返回x的绝对值
/ A4 G% U: p* t+ P8 u1 |' S - fabs(x)3 T7 Q! \$ u0 |
- Return the absolute value of the float x.( K: a$ e* Y9 d+ N4 T
- 9 s; ]: y! n- J5 T4 B
- >>> math.fabs(-0.003)
# b8 |( N7 Y& J - 0.003
% e9 D% S: }* e0 v. d - >>> math.fabs(-110)
& [0 M) ]( K: Q+ S - 110.0
: ~, x/ j* r. n8 y# V$ Q - >>> math.fabs(100)+ j$ _7 r/ S+ A& V& l
- 100.0
复制代码
$ v, S$ _% E" w2 Smath.factorial(x) 取x的阶乘的值4 I' Z+ x/ h, X# w4 l4 b
- #取x的阶乘的值
2 p, `2 l$ ]& k1 F; `7 k - factorial(x) -> Integral, @% r! m2 L$ f+ S u0 H" @% Q( i
- Find x!. Raise a ValueError if x is negative or non-integral.& Y$ t# y1 S3 P- n x
- >>> math.factorial(1)
9 I! k1 h5 H+ ` - 1
7 \8 J- X! o0 L" g5 g O' } - >>> math.factorial(2)
: P2 f4 L2 u4 M8 z k* A( }( S( ? - 2
: v* v2 g; [7 o; C+ h - >>> math.factorial(3)! J% c' ]# m% Z! O% \* N# x
- 60 a- w% O( m N/ k' e
- >>> math.factorial(5)
4 ^/ _3 Q: b H3 N( B3 h& z8 \3 l& t - 120* [2 ~, p! s& \4 F# E) O! D
- >>> math.factorial(10)
# r4 @" U" m R* n$ f- q - 3628800
复制代码
# P9 Z& g& d1 |! L+ bmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
7 x" m6 C/ Z! N- #得到x/y的余数,其值是一个浮点数' d& _. g/ j& [6 v. N
- fmod(x, y)" R& M4 [9 x$ m' ]
- Return fmod(x, y), according to platform C. x % y may differ.
8 r8 V/ l" y# q2 q9 B - >>> math.fmod(20,3)
9 c2 ?% [5 ?0 t - 2.0
7 t! g# t' D6 Q2 H1 \, e - >>> math.fmod(20,7)9 r3 `% K, h6 ~6 L- Q
- 6.0
复制代码
1 B. k1 [% W8 B! Y9 fmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 V t0 _5 o6 S6 Z2 E) p8 b
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' Z7 ], K4 ^) K# u& K" l0 O% ^
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; T H. u8 k: h1 B
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ a8 h$ B; j: \
- frexp(x)
: h5 P1 p- E2 H$ q. Y - Return the mantissa and exponent of x, as pair (m, e).6 {# ] }0 K3 w4 c
- m is a float and e is an int, such that x = m * 2.**e.# ]9 [: t, J; L, I' O% |8 G j2 ?
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.& y, x( E% b/ ]5 e) _9 l
- >>> math.frexp(10)
1 f! }- d* o; | - (0.625, 4)
: _. b S" f2 G& s$ o- [4 o! r - >>> math.frexp(75)
% e+ L [& w% b: R - (0.5859375, 7)
/ j& n6 b$ M+ W3 K# r4 y- r - >>> math.frexp(-40)
1 M0 _; W$ X, }2 w: |/ t( P# F - (-0.625, 6)3 k9 M$ Z% T. n! t
- >>> math.frexp(-100)
4 j% W( M6 |3 [ - (-0.78125, 7)- d9 ] S# [9 J9 N3 b) |8 ?
- >>> math.frexp(100); N: \/ [5 a1 C! ^! V$ q) D: D3 T
- (0.78125, 7)
复制代码
' m) z1 W6 w) j: z6 O* hmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), k- I$ G0 Y" [
- #对迭代器里的每个元素进行求和操作
0 r4 p! t# E- F& d) a- a - fsum(iterable)3 y: Z0 S: m9 S* K) i5 \
- Return an accurate floating point sum of values in the iterable., Z2 N; R- [( h0 q! I& ]$ u4 f( A
- Assumes IEEE-754 floating point arithmetic.+ w: S0 R. s* {. F: u) B/ A/ f) w2 v
- >>> math.fsum([1,2,3,4])
0 J& ?5 p- m5 _ - 10.0
4 r- X. w, M0 i - >>> math.fsum((1,2,3,4))
& I; t$ Y) j0 _7 @3 e - 10.0% }6 X" n2 o" J; R
- >>> math.fsum((-1,-2,-3,-4))) M% T4 g8 r3 C- M
- -10.0* M; N5 Q% H5 i- s1 y+ O/ h l
- >>> math.fsum([-1,-2,-3,-4])
8 c; l1 ~" r' L5 d - -10.0
复制代码 ; o9 ?' u7 I3 V) v) O* ^
math.gcd(x,y) 返回x和y的最大公约数
# @, a% M: n5 i- K; d# w- #返回x和y的最大公约数; d, A, ?9 L) N
- gcd(x, y) -> int7 z, T1 C) U- q- W' z8 h3 j& K. k
- greatest common divisor of x and y
s+ k; B2 w8 B% A- Q3 { - >>> math.gcd(8,6)+ B4 z) e, M) r- ^! P& i; a2 C
- 2
2 \* V6 g, E- N G% g9 j. T& b - >>> math.gcd(40,20)
! q2 }7 C2 N: ~8 v: X- w - 20( b, t. f+ `6 F7 r! A/ [3 U5 r
- >>> math.gcd(8,12)3 R( ?% z' s6 O$ T
- 4
复制代码 8 E& i J3 K3 A9 L
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* l/ x5 u" r: H6 M8 e- v7 C1 `8 ?- #得到(x**2+y**2),平方的值
# @# _' ]0 @: E/ v( t: z - hypot(x, y)
! ~7 }4 E8 M& P- N( G/ Z - Return the Euclidean distance, sqrt(x*x + y*y).- L7 W0 b" O7 T7 p% e. Y9 @' k* l
- >>> math.hypot(3,4)% e( L# w* S1 Z- ]0 {
- 5.0
: S2 G3 s1 c' z" v* W' |, f- i$ U$ a8 b - >>> math.hypot(6,8). J1 c. C( x7 y7 F, ]+ l
- 10.0
复制代码 , y2 p8 T/ e( `: m0 q& d L
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
- Z) a* g. x+ Q( c- #如果x是不是无穷大的数字,则返回True,否则返回False0 M7 Q B4 t) Y1 x
- isfinite(x) -> bool
9 l8 k! w3 G2 f: ]3 d/ j5 U" T' s+ W - Return True if x is neither an infinity nor a NaN, and False otherwise., P0 ]- \0 O4 n! Y% w
- >>> math.isfinite(100)
$ s$ G$ N3 J3 V9 l1 W# B% ?9 u& J8 A - True3 R. x0 B) X& W1 G( s+ z, u
- >>> math.isfinite(0)
4 R8 p$ u' K2 G - True0 {- j$ m. e& H( N
- >>> math.isfinite(0.1) `# B- Y, F9 |% w
- True
% y8 `6 g2 y- M# `; b - >>> math.isfinite("a")
8 g0 F3 j+ t: @9 i - >>> math.isfinite(0.0001)% u( S* Y" y$ W. j" @
- True
复制代码
$ u+ |: ?; e: U6 Omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False: ~9 x3 J, m, _
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
3 H. U2 g2 j8 D# } - isinf(x) -> bool( X8 D( z* K4 k$ }2 P+ \9 a
- Return True if x is a positive or negative infinity, and False otherwise.
- q% }9 X9 \- B7 R1 ] - >>> math.isinf(234)
$ X% Z J3 K" H - False L" a5 }; N. Y- v7 o; t
- >>> math.isinf(0.1)5 }7 [4 d0 s# [, v
- False
复制代码
' O. T" K! p1 O6 a9 M; Dmath.isnan(x) 如果x不是数字True,否则返回False. C- c/ c: x6 T6 q4 Z2 i& o
- #如果x不是数字True,否则返回False
+ S5 G( A+ ^! M/ ~. { - isnan(x) -> bool6 x" Q& s* d5 I' ~) Z0 _! g9 w
- Return True if x is a NaN (not a number), and False otherwise.# J$ D2 X0 G9 Q
- >>> math.isnan(23)' x, v1 T0 q. `4 m2 U. P
- False8 C4 ? X' f$ b- x& e$ c8 m2 T
- >>> math.isnan(0.01)8 W' c* i' r9 u$ x7 ~
- False
复制代码 : y& Y; M* y4 I3 P% H- s" _" l
math.ldexp(x,i) 返回x*(2**i)的值
0 ^" v4 t" l$ ^1 p3 J! o( B7 v( t- #返回x*(2**i)的值+ a1 n, e2 F" n
- ldexp(x, i)
" W( i2 O% S; d$ ] - Return x * (2**i).
" U% M" j- m p. g - >>> math.ldexp(5,5)
- U+ r0 A% T& ^2 U5 C5 I - 160.08 k6 w8 j9 r" l8 w
- >>> math.ldexp(3,5)' o$ U" D3 y+ k
- 96.0
复制代码
& @3 s, \$ q$ Hmath.log10(x) 返回x的以10为底的对数- M3 a: e+ v" H
- #返回x的以10为底的对数
" R- T% d% X7 V9 G ] Y - log10(x)
- k$ H) V; X' @4 a - Return the base 10 logarithm of x., Y4 e$ }% C# {: C% [
- >>> math.log10(10)
2 W! J" W" i7 x' [2 |$ L - 1.0
$ ^, o7 C1 F$ J/ f! `' D - >>> math.log10(100)
' E; N5 G8 a1 h- A5 B0 I - 2.0
4 U, j4 ?3 v" i& Y7 z2 Z - #即10的1.3次方的结果为20
q6 D2 _0 ]- |' M' ? - >>> math.log10(20)
0 W9 J% z1 a$ D6 Y9 E2 t" V0 W8 S - 1.3010299956639813
复制代码 : _' `+ }3 `$ c- q' Q6 i3 T' i
math.log1p(x) 返回x+1的自然对数(基数为e)的值
9 q# z* [6 n( i" d; x$ y& P4 V- #返回x+1的自然对数(基数为e)的值
0 M- A* |4 k: } I% T. i6 ? - log1p(x)1 {, n; |* q- G0 W( h
- Return the natural logarithm of 1+x (base e).; l8 H9 n- A0 T
- The result is computed in a way which is accurate for x near zero.
9 y- E) l4 x5 p! U) N1 w+ P. W1 ^ - >>> math.log(10)7 w. [. L1 x) [9 s' Q' ^- w( E
- 2.302585092994046+ g& A! N/ k! I9 G; J7 S. F( Z
- >>> math.log1p(10)0 _/ d! h2 ~ y- Z" }1 E8 l9 l
- 2.3978952727983707
% e. }8 A# ]7 y - >>> math.log(11)# c/ u" _. j$ b9 H% p- {
- 2.3978952727983707
复制代码
. |! `0 q( I4 d9 h. e5 r- z; Omath.log2(x) 返回x的基2对数
- S7 r' w, `" S2 N. x- #返回x的基2对数7 }% A6 M$ f Q+ Q
- log2(x)8 l- h. \# p; H. b6 _2 ?# R
- Return the base 2 logarithm of x.% m1 c# S) d9 s' M3 o
- >>> math.log2(32)
! {8 S* @8 t9 g& F - 5.0: l8 o) `# d% j- w B; u
- >>> math.log2(20)
8 h% w% Z1 ?# K, I9 j# Q8 y3 ?8 Q - 4.321928094887363
2 H& I3 a6 A5 R% v7 z. N# Z' ^ - >>> math.log2(16)
1 b V2 S, n0 S0 j& h$ a0 Z( k - 4.0
复制代码 9 j3 n/ d* S6 T$ ]. M& r
math.modf(x) 返回由x的小数部分和整数部分组成的元组
: w$ F# a- x# o/ s9 U, X, w# _- #返回由x的小数部分和整数部分组成的元组
/ _+ t0 c6 m( t4 y2 S" `4 b7 q - modf(x); t7 l0 n m8 v
- Return the fractional and integer parts of x. Both results carry the sign
; i2 w5 g% f- l2 Y M$ M, M - of x and are floats.
; d! B. F. A2 N# @0 V" _8 Q' p - >>> math.modf(math.pi)% u% |. V' Q2 b# g
- (0.14159265358979312, 3.0); N. u) k) L+ L* W! {$ V' }
- >>> math.modf(12.34)% N! a0 e+ ]7 Z9 c3 ?
- (0.33999999999999986, 12.0)
复制代码 : `1 v! k4 e& j+ B
math.sqrt(x) 求x的平方根 s) C" J) k+ X& T- g& x& Z
- #求x的平方根
/ [9 W0 _- y' z$ M/ ~" l - sqrt(x)
5 m; ^5 U. b, L) r4 Y4 K' K5 x1 T - Return the square root of x.3 f& Q/ U, ?- k1 w! S, V: W/ m
- >>> math.sqrt(100)
! m& m; T9 E2 B. r - 10.0
. S+ r$ |) S% z$ N# X. A - >>> math.sqrt(16)! y; |" q( p( N, |
- 4.0: {, F& Y/ V- ] G( C
- >>> math.sqrt(20). z G9 I0 a8 S2 K5 S! h
- 4.47213595499958
复制代码
1 J/ \0 `" E( T. G: L4 H) E9 Pmath.trunc(x) 返回x的整数部分- #返回x的整数部分; t2 J, x/ ^1 M
- trunc(x:Real) -> Integral) F' \' v. k5 S2 @
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.6 s6 J, [" _0 J, E% i+ z7 N; k
- >>> math.trunc(6.789)% }9 U' h- j5 q
- 6
/ E3 k8 E w7 `( W - >>> math.trunc(math.pi)
/ a1 Y( r" k6 i" I; h7 P' u - 3
' E2 ^1 V) R, Z6 E+ m. | - >>> math.trunc(2.567)
, {) r4 ] [2 O- I; x1 ]7 A/ j - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|