|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
; B; ?8 q6 E8 n/ J
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' n, c9 G% S2 U7 r ?
* l/ W' {( c. x; p
方法1:2 L1 r$ P& I6 r& m- C
- >>> import math
7 v5 }) f& ^2 } - >>> math.sqrt(9)
5 Q3 ?. w# l/ p$ P+ ? - 3.0
复制代码 方法2:
% k6 S4 u% b' r3 Q1 |9 V# c- >>> from math import sqrt
! i: q Q& L v. V9 d- L - >>> sqrt(9)4 Z8 f, I: C0 `5 M8 A
- 3.0
复制代码
% c3 ~5 ^" @. L9 L6 j# E, S
" r9 Z8 P9 K# S7 fmath.e 表示一个常量
5 r% @9 L2 e$ e- K" U. g2 o/ l Z- #表示一个常量8 P% w% ^; |& J! b
- >>> math.e
/ J5 ]: ^* Y ^6 V1 P- \- b" ~ - 2.718281828459045
复制代码 4 T, }0 R- _& J. z1 Q/ c$ P
math.pi 数字常量,圆周率
/ I9 ?2 [0 s; ~% k5 z- #数字常量,圆周率' h; V+ G: }2 F2 E8 ^) S
- >>> print(math.pi)
0 h0 h, S2 a. }: A% w& b - 3.141592653589793
复制代码
7 D; h! t7 m1 V/ @9 j8 W, B) Amath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x8 W( z- [9 |7 W, x2 b
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x( J7 T0 `7 [6 W2 n/ ]: X1 j
- ceil(x)& R3 t9 q; j& ]* D- J2 i
- Return the ceiling of x as an int.
& J1 C3 s9 [% u1 p5 G! ` - This is the smallest integral value >= x., g0 }4 k# a5 F
4 L" D4 c& r/ b6 x( [9 {- >>> math.ceil(4.01)! k) g0 x$ W6 q% X; V, ^
- 5
2 O+ Z7 e$ u; G - >>> math.ceil(4.99)4 Q/ m7 E8 o) [* J( C2 X- {# ]2 E
- 5
$ R" Q1 W% k. v7 @7 H7 h, Y: N - >>> math.ceil(-3.99)
1 ?; Z4 W# w( t1 D - -3
/ d* ]$ `9 R9 P6 u' s, | - >>> math.ceil(-3.01); V! A. q/ W$ [5 x S2 ~
- -3
复制代码
' |& I5 V' \ ~9 p4 a+ mmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身& c$ r* m7 N9 K; D' I, u7 e
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- d& ^( ^) z, C5 b/ C4 j
- floor(x)
" p$ i* F" P+ F9 O - Return the floor of x as an int.
! N# @. L. m3 V- | - This is the largest integral value <= x.1 M; G" } c: H/ \
- >>> math.floor(4.1)6 I+ f4 l( [: i" J3 |
- 4
" p' @1 R: e/ [' @- D - >>> math.floor(4.999)+ _+ b4 _4 Z1 s9 N2 m) M, N
- 44 Q% Y% k9 I- ^5 x; x) _
- >>> math.floor(-4.999)
; G% i* J6 @5 m. P: F/ t5 w - -5/ n- W' Q. S: p* \
- >>> math.floor(-4.01)
! T2 Y. J7 g# d2 r" O5 V6 t7 e$ u - -5
复制代码
' T8 B6 V. [" bmath.pow(x,y) 返回x的y次方,即x**y
; c& a- w$ {9 Q* q- #返回x的y次方,即x**y
. @9 B) r* W% s" A( i2 P' r5 P# z3 w - pow(x, y)
/ {9 |* E/ x$ k0 J' D' w - Return x**y (x to the power of y).
% K1 z' ^) a% J: ~! L0 k - >>> math.pow(3,4)3 E1 O: }7 w4 G8 V, m
- 81.0
% u+ m. Z' V5 a8 h. F - >>> 3 p7 b8 C. ~, m: i' q' M, b9 m
- >>> math.pow(2,7)
% o1 c! u4 K1 Y9 f, D - 128.0
复制代码 5 P. y$ E2 A1 ]8 g
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% ^: M" R& [ t$ k8 f
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" Y; T6 N7 Y$ I% ]
- log(x[, base])
1 C! i4 _4 H- N- Z - Return the logarithm of x to the given base.
/ [1 |5 P7 x w2 a( T: b4 x - If the base not specified, returns the natural logarithm (base e) of x.4 y+ w1 j0 m' G2 x
- >>> math.log(10)
( q8 _, m* n5 l5 C& v - 2.302585092994046; K! k0 h4 i! V
- >>> math.log(11)
+ L8 r7 p9 v9 k# {2 H6 o6 I - 2.3978952727983707$ f. g" R% e" Q0 \0 j5 Q: @
- >>> math.log(20)) @3 x1 ~( S$ J! M; Y) a# N
- 2.995732273553991
复制代码 " D, L) f9 L& f
math.sin(x) 求x(x为弧度)的正弦值# @1 z4 P" m9 K: m; ^; T O. E4 k
- #求x(x为弧度)的正弦值6 w+ j' {+ H( t$ b3 W( `6 ]+ h
- sin(x)
7 a' @( F7 H. b - Return the sine of x (measured in radians).
: F6 |" N' j# O0 D1 ] - >>> math.sin(math.pi/4)
% r$ W2 R4 F0 a4 A: E - 0.70710678118654756 ^+ B+ u* ?' l
- >>> math.sin(math.pi/2)8 m. d& O C8 ]' ~+ P) W
- 1.0
, i' m3 F" }' w" y/ ], i - >>> math.sin(math.pi/3)2 Z5 J* R3 A) V# B f
- 0.8660254037844386
复制代码 ; s m/ R# Y" c, M
math.cos(x) 求x的余弦,x必须是弧度
8 ?5 I5 w i5 a4 Y* R* ~# a7 y0 O8 B- #求x的余弦,x必须是弧度' s- t2 s+ H. B: {" I
- cos(x)! l% E: T H3 f! w+ O3 Y
- Return the cosine of x (measured in radians).% D" d- M. O9 v# @5 ?. C
- #math.pi/4表示弧度,转换成角度为45度/ c, ?, l5 g8 [; M. R
- >>> math.cos(math.pi/4) W) [4 v, _) r' {9 e7 w* q
- 0.7071067811865476
5 |3 u# L( _. O6 _ - math.pi/3表示弧度,转换成角度为60度
: m) P5 M8 |) ]6 ] - >>> math.cos(math.pi/3)
6 m% M; Y" L; K5 e; ^ - 0.5000000000000001
0 z" k( U( w, v2 c: g+ I4 C7 i - math.pi/6表示弧度,转换成角度为30度
+ T8 j% n8 ^+ G2 H" D - >>> math.cos(math.pi/6)
7 _1 U' a0 q; M - 0.8660254037844387
复制代码 I8 @( J2 C1 H8 ?8 w
math.tan(x) 返回x(x为弧度)的正切值
t9 t% q+ Q# f- #返回x(x为弧度)的正切值; f( R7 ]- e" u/ h$ j. U
- tan(x)
# X+ ^1 Q+ Q" S0 ~+ V/ p0 z8 [ - Return the tangent of x (measured in radians).0 M5 ?$ l' i2 V" M
- >>> math.tan(math.pi/4)
! k* J c$ Q/ ?3 ^* } - 0.9999999999999999
6 O4 Z- F3 a: @: H - >>> math.tan(math.pi/6)2 |* Q2 [' _* ?% H& i- \
- 0.5773502691896257
6 y' E0 f8 L) @/ P3 y+ c {9 X: u - >>> math.tan(math.pi/3)1 Y3 [' T$ U2 p6 B
- 1.7320508075688767
复制代码
4 @. Z3 ^+ i7 V* ]. o1 wmath.degrees(x) 把x从弧度转换成角度& N* }( x5 ?- K9 f# B
- #把x从弧度转换成角度
- ?# A! u2 @4 @" h - degrees(x)
; x& G; l) U& l: C( V - Convert angle x from radians to degrees.8 X: w% c+ Y5 s* {% O* r
- 7 Q2 B* y& ]! ]) q$ w( j
- >>> math.degrees(math.pi/4), E2 H0 z; P& R8 y* b' ]$ e
- 45.0/ l) U, G: o/ g0 ]/ n3 W
- >>> math.degrees(math.pi): a; X& ~: I2 K/ R( ^) I
- 180.06 X G; g) {0 q) I& D5 z
- >>> math.degrees(math.pi/6)
# v) S" j: c, H/ d6 n+ I6 M3 {7 R3 I - 29.999999999999996) ~4 {3 f: @4 y6 f: I' R
- >>> math.degrees(math.pi/3). a$ X: r- n4 Y" Z0 F0 p, K$ g
- 59.99999999999999
复制代码 # H8 |8 j$ |; y! A* ?
math.radians(x) 把角度x转换成弧度
- H1 J: L+ B" A6 E) }- #把角度x转换成弧度
6 X1 H, H+ w* G/ n! P9 p& a - radians(x)
/ n" e% A1 O; r" H - Convert angle x from degrees to radians.) n& {3 z/ z; _3 X. y M
- >>> math.radians(45)
! ^' I& s9 R; ^+ c& [ - 0.7853981633974483
$ m1 t' Q& c9 E* C - >>> math.radians(60)
7 s; `1 y: A5 D7 X2 E - 1.0471975511965976
复制代码
$ z* z3 T# v, t% v8 F2 omath.copysign(x,y) 把y的正负号加到x前面,可以使用0) b0 V0 I9 C" P1 j i1 l
- #把y的正负号加到x前面,可以使用0
) p& A4 }: v% r1 |) x# ^( T - copysign(x, y)
$ T/ D p: D/ ?6 p2 [ - Return a float with the magnitude (absolute value) of x but the sign : Q% z2 K) i; {
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) , y8 i0 Z9 Y2 X N$ ^8 L
- returns -1.0.
3 B, V0 T* V" }3 C
5 C$ t# }. r: | H S: r; ^& `- D( x- >>> math.copysign(2,3)5 S t7 o9 M( y% U; @
- 2.0
2 F) ]& f2 |8 Y" y - >>> math.copysign(2,-3)
8 l0 K/ V5 ]' E. J; @4 u0 S - -2.08 S U E% W$ D9 ^$ b1 W% Q# l
- >>> math.copysign(3,8)6 a# X4 }) h5 M3 T6 _
- 3.01 v5 x9 m+ P( {
- >>> math.copysign(3,-8)
" u% |( [$ y' _* }/ ^, F - -3.0
复制代码 4 X }. a! g0 [ ?5 n
math.exp(x) 返回math.e,也就是2.71828的x次方
* z. d/ t" F! _) U ?$ h3 n- #返回math.e,也就是2.71828的x次方
1 p7 c% d8 p6 R( W% } - exp(x)4 V# j+ r" z( ^/ r7 W
- Return e raised to the power of x.
5 [$ v' A' J3 Y* L ~: U5 k' z8 | - D' ?/ Z @$ r+ O$ q o( S
- >>> math.exp(1)
" |- l& f% Z. C! A5 Y9 S8 l) S - 2.718281828459045; {/ c: E0 E7 u! e
- >>> math.exp(2)6 M/ r3 H2 M& o8 H! [1 r# k
- 7.389056098930653 ~( |7 F {$ [' v9 s& Z
- >>> math.exp(3)
2 s5 b" }+ L- X7 T - 20.085536923187668
复制代码 . F% r& w3 ]& S2 F
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
. w8 }# }5 a$ b: p9 o2 o3 |- #返回math.e的x(其值为2.71828)次方的值减1
' {# u4 z T( u2 W/ V/ X - expm1(x)' n6 h J; m3 [' ^. J% {% D s
- Return exp(x)-1. C5 B' j+ Q4 g4 v3 y# d
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
% \3 ?9 ?( B% Q/ l - 6 I# B, `4 I1 c4 l. N$ Y
- >>> math.expm1(1)
3 a2 t2 W( s/ _6 z - 1.718281828459045
7 j$ n, q) T7 s& D* [9 j - >>> math.expm1(2)% G5 r- K6 H" z5 E: q1 h0 [. K8 I; l
- 6.389056098930654 S, A: x! `1 W, D$ u/ t" q
- >>> math.expm1(3)
\( ~* E9 S4 B/ ` - 19.085536923187668
复制代码 1 o |% S. I9 j* A3 @( Z a
math.fabs(x) 返回x的绝对值
/ _' |5 `. E) u4 z/ N- #返回x的绝对值
! Q6 K j; I# x' N! M6 |+ o - fabs(x)
, n! H; e' F0 z1 O/ X; x - Return the absolute value of the float x.
& w) z5 ~+ m2 p" T4 F6 Z
3 M7 w3 s) x' X2 ?- >>> math.fabs(-0.003)
$ ? n, Q4 g8 c) F( ~* h - 0.003 B9 O. g4 f: J
- >>> math.fabs(-110)
. D/ {' s: M0 p9 w4 g% W9 b - 110.07 Q9 x0 y0 I, O6 ^% G7 | U1 H' Q
- >>> math.fabs(100)
( o2 f- @* N& h& |( L0 R! B# v - 100.0
复制代码
" ~- C6 R: C9 w$ Pmath.factorial(x) 取x的阶乘的值
( d: U$ C0 m; D L& g2 g" {7 a- #取x的阶乘的值- x" {* ], P9 Y) H# v/ x4 u+ S) h
- factorial(x) -> Integral
' H! Q& @7 O+ C# @( H+ P" K1 P: ^; f - Find x!. Raise a ValueError if x is negative or non-integral.0 L6 |+ l$ N# H9 f
- >>> math.factorial(1)
3 @9 L) F/ n4 |" D/ d7 b0 S - 1" K+ g/ G6 }# E& s
- >>> math.factorial(2)
$ A+ t4 y( |& p* v. p4 m1 L: F5 R - 2
3 a6 u9 T) k7 g0 {* }0 X - >>> math.factorial(3)$ ^& A0 S3 o8 X. L. k. }
- 6
% }; U5 S! w. p& [( _' Q) k - >>> math.factorial(5); j/ b( q* z( e4 d
- 120
& @1 o4 t, {2 q7 s+ \ - >>> math.factorial(10). x. @5 }0 w* L. [
- 3628800
复制代码
: a# Q$ { Y- @0 `math.fmod(x,y) 得到x/y的余数,其值是一个浮点数/ q* s( O) N( C& l, Q- N$ {$ F
- #得到x/y的余数,其值是一个浮点数% C$ ]5 n# F5 H* ^; x$ j
- fmod(x, y)
9 J# @# Z2 X! N( ?1 ~ r - Return fmod(x, y), according to platform C. x % y may differ./ [- t# L9 T' H% \9 a1 a
- >>> math.fmod(20,3)( w! j9 H) I# K
- 2.0" |. ^* s# d( |
- >>> math.fmod(20,7)
' u! }' Z& _7 x! p, Z) }, X - 6.0
复制代码 ; `$ j8 b8 b- K2 y3 y) y& P
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围 W5 @9 Z. ~" k# t
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
' o( @, ]8 c; q7 O' L9 C' z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 f$ Q5 q- D V! G, k% F% O
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
& m0 ]9 b& U" S# g0 S! l! u - frexp(x) {% k1 M! A* Z: {# c: X
- Return the mantissa and exponent of x, as pair (m, e).
6 B+ |6 z6 |* U9 ^ - m is a float and e is an int, such that x = m * 2.**e.
- r0 O1 [* _# }$ w( [& H3 l - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
- @& j- m9 ?, r. ~2 s - >>> math.frexp(10)/ n" O6 A/ e7 f/ c- f
- (0.625, 4)1 b- u0 r7 x5 f# x
- >>> math.frexp(75)) n/ w, d6 d- O8 |2 |) l. W9 _
- (0.5859375, 7)2 B4 B* U+ |& a1 m5 h! x. p, Y2 c9 X
- >>> math.frexp(-40)
$ \# R. C5 a" L$ p$ p4 S - (-0.625, 6)' l9 V. [3 f5 g- P6 L0 G3 P& ^
- >>> math.frexp(-100)* D' [+ V' O9 g% x5 X
- (-0.78125, 7)
0 U! b8 X' S% W$ [$ D - >>> math.frexp(100)
( b" z0 p/ F: p+ u& d0 v - (0.78125, 7)
复制代码
/ h+ Y( A" e7 qmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
/ j) t7 U' x( w" j% o- #对迭代器里的每个元素进行求和操作6 [/ ?$ z1 j4 r6 V& f% k9 W, N! U b
- fsum(iterable)
7 h) X X! X. z* [ - Return an accurate floating point sum of values in the iterable.' O8 f O7 z8 h3 U* h5 D6 Q6 M- @
- Assumes IEEE-754 floating point arithmetic.
! k% I( C0 Y; ] - >>> math.fsum([1,2,3,4])3 c; _" A1 a% X" o r! |; T
- 10.00 @$ G* a9 I6 ^5 O' e( g1 V+ a8 d
- >>> math.fsum((1,2,3,4))& a. \" p) Z3 P' ~
- 10.07 b8 r7 l0 N6 g9 R0 N0 S4 b
- >>> math.fsum((-1,-2,-3,-4))
8 |* W- ]9 ~1 @0 W3 F - -10.09 v" _# a0 h" Q( p( v0 z' t2 x9 o
- >>> math.fsum([-1,-2,-3,-4])
! a- J8 V; W) t V, _ - -10.0
复制代码 + x" F$ D5 Y# {9 o& P
math.gcd(x,y) 返回x和y的最大公约数
) a, p2 o' {$ F0 f- #返回x和y的最大公约数; l8 I; w9 P- Z p3 d
- gcd(x, y) -> int1 l4 x3 @+ k" P% m) \0 l. f
- greatest common divisor of x and y1 k. S4 z7 h. R6 N1 s
- >>> math.gcd(8,6)
+ n6 o1 U; c! G) b - 28 ], @7 L- t" p9 x8 L# ?6 @
- >>> math.gcd(40,20)
' x% I( X$ t" y1 N Y O5 I& [ - 20, v: |2 K; q2 L6 g3 l
- >>> math.gcd(8,12)
' O/ Q) l* E3 K7 y( R - 4
复制代码
1 o1 Q6 M$ ^) T: c! ]1 R8 J( Qmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 P C8 o2 ~& K; w/ {
- #得到(x**2+y**2),平方的值
6 ^3 E# y+ `+ O; E# t* N+ K - hypot(x, y)
! G- p8 d; r- W- A" U& [6 p9 Y3 h! Q - Return the Euclidean distance, sqrt(x*x + y*y).! H: e/ d5 F8 [" p5 [2 {$ b) {
- >>> math.hypot(3,4)
2 U% I. G* y$ B% q- _% b - 5.0/ o& Z3 H x% M4 D5 A: r8 @2 r
- >>> math.hypot(6,8)
' f- X0 e1 N- I* p% T% I: E - 10.0
复制代码 ' ]# o9 X3 K; v! X5 m* C
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 G# |7 \: Z9 Y* W$ x( A- #如果x是不是无穷大的数字,则返回True,否则返回False
. P" v: u' `+ l' W* _' p7 G' t# {" W y - isfinite(x) -> bool0 m& O' r& H# l1 W" X2 v, A
- Return True if x is neither an infinity nor a NaN, and False otherwise.
4 ]" z2 s N+ u1 z- G - >>> math.isfinite(100)* ~: o- R6 W0 w* ^2 e6 \
- True
" m0 [7 h/ w2 l* H. K - >>> math.isfinite(0)
2 H$ m: E% s0 y - True
0 N* i* ^) J+ M0 s; j! j% W5 ~ - >>> math.isfinite(0.1)
% }5 U& ]# W, ?; f3 |- u - True$ b- M2 v; T; h1 _5 \8 c
- >>> math.isfinite("a")) T/ s0 ?' O) f, M/ ] e+ o
- >>> math.isfinite(0.0001)
7 N% t) q/ f+ o. E4 h) ] - True
复制代码 ( O; _" Q6 y' `0 s3 T
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, ]7 W' j& g% Z/ X3 N0 m
- #如果x是正无穷大或负无穷大,则返回True,否则返回False7 X! A- U2 s: I5 N, z# L, E
- isinf(x) -> bool
3 [+ r4 t" z+ {& i, f2 s( J - Return True if x is a positive or negative infinity, and False otherwise.( t7 ^. S# B l4 v5 J2 {$ N
- >>> math.isinf(234)
# h$ F" L+ c5 G3 n - False
0 N! d2 p0 s, ?( Q - >>> math.isinf(0.1)% W3 s/ y) `+ J
- False
复制代码 7 \' V, V6 A' d
math.isnan(x) 如果x不是数字True,否则返回False B4 g, L4 I7 b l
- #如果x不是数字True,否则返回False
# G, ^9 a% }6 I! N3 L5 P - isnan(x) -> bool
1 R& N& t4 f$ v% o9 \. l8 l2 P' x - Return True if x is a NaN (not a number), and False otherwise.
: _9 Z) n4 z- o) X: u - >>> math.isnan(23)
$ v5 z. N- t$ ]- U1 e - False
5 R3 ? z( q/ Z) ~7 P - >>> math.isnan(0.01)8 }; v0 f/ {* R
- False
复制代码
" h1 r' \- l; v4 h6 c9 P0 |0 ^math.ldexp(x,i) 返回x*(2**i)的值+ x; F# m# I* p
- #返回x*(2**i)的值
7 k" f" w" K8 ?( m3 q* ?) s - ldexp(x, i)2 a! J" j- d' h/ D% L6 B
- Return x * (2**i).
* \% ^. W4 `. C - >>> math.ldexp(5,5)
& t( L* A# b9 H" y - 160.06 `$ d& r: C1 b+ d% X! c9 ^4 p
- >>> math.ldexp(3,5)1 T% i! m6 R5 s& Q9 X( V
- 96.0
复制代码
7 g) D7 v/ _, ~7 r2 l' d$ X% rmath.log10(x) 返回x的以10为底的对数
/ u" j5 J6 v2 u8 a- #返回x的以10为底的对数5 L8 T1 Z0 ]2 Q" R4 X* }
- log10(x)7 K( o* v) s) G6 t; L8 C
- Return the base 10 logarithm of x." ]/ A/ n2 }, L9 }
- >>> math.log10(10)
( T. }5 ~" ^- k% n& x) v/ h - 1.0- i0 L" N* l+ i0 q
- >>> math.log10(100)
: l2 u3 H3 n2 D/ t' |( S# T2 L - 2.0
* r) r$ c X2 L1 H* y* d6 ^2 Z - #即10的1.3次方的结果为20: b3 T$ Z0 e' }% E) @5 z
- >>> math.log10(20)
5 ~) O/ ^5 d- T* _( t/ O8 h4 d - 1.3010299956639813
复制代码
# I6 F! p% c/ ?) J- q2 P; kmath.log1p(x) 返回x+1的自然对数(基数为e)的值9 S; G( p, U2 n; p i! `
- #返回x+1的自然对数(基数为e)的值
: D; j% g# F, t, J$ S$ j - log1p(x)5 r$ l% I, P$ _5 ?, a$ v
- Return the natural logarithm of 1+x (base e).
$ I$ _1 Q' ?3 Y7 ` - The result is computed in a way which is accurate for x near zero.
. f# \9 Q4 g5 g - >>> math.log(10)
/ H9 Q; u2 _$ X, o: B6 ^ - 2.302585092994046# l( s; C* r K
- >>> math.log1p(10)
, y: R4 g. _; q7 k9 H- G8 q - 2.39789527279837078 a0 L& V7 [+ y4 e6 r
- >>> math.log(11): s. m) W" c4 H- @ h7 s
- 2.3978952727983707
复制代码 * \3 K& S. `0 ~4 u3 q
math.log2(x) 返回x的基2对数; f, k* {, I0 K$ b% E2 \
- #返回x的基2对数 V6 m2 ]- U( {8 f2 Y9 I; c8 S/ ]
- log2(x); J( u) C p7 h8 z
- Return the base 2 logarithm of x.
7 C! J% u) g; A. |0 E' I - >>> math.log2(32)6 Z& h- z7 K, n
- 5.0
1 H5 P$ B$ c* E! \; r. p+ N: O - >>> math.log2(20). m: q% d" @. ~3 i1 o `6 x' m
- 4.321928094887363
5 z/ p2 q0 Q9 V - >>> math.log2(16)
# W& {- y0 K) P6 t$ n I Q4 Z" P - 4.0
复制代码
5 R. w3 k, n1 w( N8 @4 O9 Umath.modf(x) 返回由x的小数部分和整数部分组成的元组( t$ E+ Q& E, J4 j6 R
- #返回由x的小数部分和整数部分组成的元组
# Z8 f C! S; s4 d: A - modf(x)
- R' `' s5 v2 [$ } - Return the fractional and integer parts of x. Both results carry the sign0 j: [' ]* M) z, W8 |5 T
- of x and are floats.
1 G/ Z3 j8 t5 V) m3 Z$ [' ~9 k - >>> math.modf(math.pi)
0 [8 X$ G" M+ I! X5 G; ], O' @ - (0.14159265358979312, 3.0)
s+ K% t1 R# n7 t( ?: Z2 K0 n) E - >>> math.modf(12.34), P2 E: j6 k% k) l. k1 q2 K& Y* ~
- (0.33999999999999986, 12.0)
复制代码 * A6 C5 `* F: n% x; r( o' ]' j& r9 b
math.sqrt(x) 求x的平方根
' a5 q% ?6 m! {- #求x的平方根
8 n2 u; r: p( \ - sqrt(x)2 D4 w6 e* W/ R' R. ?
- Return the square root of x.
. F, z$ j6 X! z. _* h - >>> math.sqrt(100)
@" e" P* J/ i; v2 W9 E, A% B - 10.0( w9 F' C4 |9 o5 j: V
- >>> math.sqrt(16)4 Z# a; L- q5 d3 E2 P2 }. a
- 4.0; e* A6 u2 U j) o' K5 J0 }) ^( v
- >>> math.sqrt(20); T) H* n/ P9 J8 P; S6 I( T
- 4.47213595499958
复制代码
+ p( B* [1 s" K' {math.trunc(x) 返回x的整数部分- #返回x的整数部分) u1 m* C7 ]4 Y; ]2 X9 \' u
- trunc(x:Real) -> Integral
) y, }6 `5 G: \; }' k/ U; U - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.& Y6 b2 k! E/ }0 n. f" o
- >>> math.trunc(6.789)0 A2 g; W* s: T" j" ^! T+ Q
- 60 m% F9 S" ]2 r: J7 H( Z
- >>> math.trunc(math.pi)
3 Y5 E2 d/ X) E( b# Y/ C - 3/ Z8 l7 x; k" v1 z* l
- >>> math.trunc(2.567)
/ S6 f4 y6 ^ q - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|