马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 b4 }' Y3 K( i$ |1 E& \9 y2 y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: c0 x. ~* q* V$ G# U
4 I$ f) Q) n* O( W方法1:
0 @+ L; ^! w- H' h* p- >>> import math0 n9 {2 Z3 e2 @' X
- >>> math.sqrt(9)0 X$ M f ]- b3 ^5 ?, s
- 3.0
复制代码 方法2:
" K2 P& k6 J3 ^9 P& M* i0 @, @- >>> from math import sqrt
, s7 a/ T+ \3 P - >>> sqrt(9)
! r+ v' v" F7 E. P% b5 r - 3.0
复制代码 * H0 {, D& E' h9 g$ t; E3 {& k! u; o
) n1 i! k5 l( Wmath.e 表示一个常量3 O( X: u8 K; B( G! Q. g
- #表示一个常量" n) Z/ P q9 c7 Q/ g' g' Q
- >>> math.e
9 z6 M- Y7 T5 v- v8 D* e9 @ - 2.718281828459045
复制代码
! F0 {) L, x0 `0 o! Gmath.pi 数字常量,圆周率3 e1 L' e4 `2 K& B0 r0 Q
- #数字常量,圆周率4 X" [7 S. N) ]. W8 |
- >>> print(math.pi)$ u" b( I3 z2 H0 I
- 3.141592653589793
复制代码
* y! o6 w8 @6 r. G- V9 |math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
& v5 d7 A2 E" s+ f: p# N- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 d( X0 N9 y. s4 G. w8 S2 p - ceil(x)
8 |! r1 I) T: O6 x( n - Return the ceiling of x as an int.
. D1 B9 g) F/ b# C" d6 B( N4 B - This is the smallest integral value >= x.
1 `6 `" g' D! g7 e; R. e
0 ? S F: ]' ~0 y+ a+ w" R S- >>> math.ceil(4.01)
3 C5 }! Q5 ?9 T5 B3 i, ` - 5' m% y5 U2 [/ K9 y; E+ n7 \8 x
- >>> math.ceil(4.99)5 x- w5 _1 q! `; J# T! @; {. w* \
- 52 {4 H6 s5 N: P# E
- >>> math.ceil(-3.99)
/ s2 L$ P- {" Q. K - -3
; A, A' }$ d k+ p& j; H4 B - >>> math.ceil(-3.01)1 e/ K8 t# N$ ?- ~5 Z' J: y
- -3
复制代码
( C! i; g' J9 d" _2 h! K; l# I; amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 n9 V$ ]0 R) f; m' `+ R
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 }4 q$ b: @. b8 X$ D# \( Q
- floor(x)
$ O5 |3 O( B8 ?9 o0 | - Return the floor of x as an int.
1 j- _$ J* i: P9 g# \9 g; B# t: Z - This is the largest integral value <= x./ D6 j+ A z. j& @3 i% m
- >>> math.floor(4.1)- S# M' @' g8 j3 f. N
- 4- C; j8 A0 M4 n* `% L$ s+ U
- >>> math.floor(4.999)
O9 P/ R# k1 v: m8 A) K* M1 q0 N - 43 N5 |! R5 @8 |. ^) m" X! C
- >>> math.floor(-4.999)
6 |1 g$ y+ m4 ]. H! l - -5
4 ~; f+ m$ l4 G: g7 e - >>> math.floor(-4.01)
" z& h/ L( O l+ x# ~2 C - -5
复制代码
) i" V. Y+ n( ^# ~" imath.pow(x,y) 返回x的y次方,即x**y. M# U( b9 l: q9 k# n: _
- #返回x的y次方,即x**y/ V$ g- H: l+ L/ F6 C) C
- pow(x, y)
0 {3 @1 F! D/ d, S# f' z) A - Return x**y (x to the power of y).. ~5 R8 Q" |: ^1 o0 s2 t
- >>> math.pow(3,4)
& j# w) u3 q. P" z; | - 81.0, T0 v2 I& f/ R5 ]6 h5 l; S
- >>> : `* ], W# o& J9 J" |8 b
- >>> math.pow(2,7)
; i5 u# y0 j3 j8 S" \' C3 v9 R - 128.0
复制代码
" I/ N8 ]4 P+ @- x+ T+ zmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# ?9 @. N- z/ Q2 S- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
. B" T8 E: i7 W% `- v. |4 u - log(x[, base])/ _. j# f( r0 K. F- M( P
- Return the logarithm of x to the given base. U1 P; `6 C( c6 n
- If the base not specified, returns the natural logarithm (base e) of x.
. I, Y( K$ w0 F/ z3 ^9 Y! P! x8 V - >>> math.log(10)4 b. s! [8 Q( j* Y
- 2.3025850929940461 q. W3 O! Q7 D& ?6 w
- >>> math.log(11)
% X8 J6 N8 L! m' }3 j; b - 2.3978952727983707
c7 X7 C$ b7 k% u% T7 s - >>> math.log(20)
0 `' V( r2 _6 a+ S6 o' Y - 2.995732273553991
复制代码 ; f5 `) X4 a5 n0 G
math.sin(x) 求x(x为弧度)的正弦值: r( @2 V2 J K& p* r% E# m- Q, [3 W. R
- #求x(x为弧度)的正弦值; m: f% Y6 L, P' e$ ?' u$ f) n: E
- sin(x)
1 S4 L( J/ `, L - Return the sine of x (measured in radians).( k8 ]9 u) v/ g& m+ @
- >>> math.sin(math.pi/4)3 i C' E# |; k' x( c) n
- 0.70710678118654750 G' E- W6 q% X+ R/ U
- >>> math.sin(math.pi/2)
, j0 c$ F. y, I- {. `- t6 w - 1.08 c' u4 y8 h; N! m" j0 K& D
- >>> math.sin(math.pi/3)
. v( z% v% C% g - 0.8660254037844386
复制代码
! r& w9 K9 R- Omath.cos(x) 求x的余弦,x必须是弧度
5 C, u& x5 N' x! u- #求x的余弦,x必须是弧度1 J6 I1 v8 s$ C. F
- cos(x)
- e6 J! |5 }. \: Z1 }- w6 {- j' M - Return the cosine of x (measured in radians).& h/ O6 ?- Q6 J1 p
- #math.pi/4表示弧度,转换成角度为45度
+ Z- I+ ^- y; B2 R; h6 O - >>> math.cos(math.pi/4)! O+ [) t3 _( N# X* t2 J9 {3 O
- 0.7071067811865476
7 Y4 @& l4 F* ] - math.pi/3表示弧度,转换成角度为60度
7 B. X% p/ X! P/ `9 J8 |( } - >>> math.cos(math.pi/3)0 y- _8 e9 v* J |6 n8 C* h
- 0.5000000000000001+ j7 R0 A' |+ f( W/ e- O
- math.pi/6表示弧度,转换成角度为30度
8 H% q0 x' a+ } - >>> math.cos(math.pi/6)
: |8 J% f# q7 D* A# O, ?3 i7 f - 0.8660254037844387
复制代码 ( L) m( h) L$ q# t/ E# n
math.tan(x) 返回x(x为弧度)的正切值
3 N+ e1 m# r( a0 s- #返回x(x为弧度)的正切值4 m- F; I" ^; }6 ^+ H
- tan(x)
$ J8 ~" h, n) a! u% M4 v - Return the tangent of x (measured in radians).
1 ~, b E! N* N7 ]8 ]4 X8 W% A4 p - >>> math.tan(math.pi/4)
0 ]/ r& O; Z* }4 y m, v - 0.9999999999999999
3 ^1 ^9 z' B- p5 [: T K - >>> math.tan(math.pi/6)! b# ~. v; R8 U/ [$ p1 ]3 q
- 0.5773502691896257
8 W" a) y4 ^- C# R. C! h - >>> math.tan(math.pi/3) k* t1 r- R, \' g6 Q* [6 V: m2 ~
- 1.7320508075688767
复制代码 3 {# K9 Y' c# c4 ~9 R
math.degrees(x) 把x从弧度转换成角度/ n0 c) S& y& N3 q+ O6 s
- #把x从弧度转换成角度5 q2 l' C8 P8 d/ G* R; V; t
- degrees(x)
& n, ^" `5 R' J% M# f8 O, i - Convert angle x from radians to degrees.
2 D- i1 X! c( M! {+ U4 [/ R - ' N* W1 C Z' i) l/ O
- >>> math.degrees(math.pi/4)
) m2 A: }; `$ e# X4 ?$ Y! M - 45.0 _9 U" M0 q/ v0 l. W1 E2 f
- >>> math.degrees(math.pi)
q6 _" c0 \( G( ~1 A0 N5 @ - 180.0# r7 f# S7 ~( ~2 n& U. c
- >>> math.degrees(math.pi/6)0 S" x6 r; u! ?* y# @, i1 v- K
- 29.999999999999996( u4 Z# g- M7 A3 B: `* Q; N
- >>> math.degrees(math.pi/3)# D+ \" G. E9 X5 i8 W: V
- 59.99999999999999
复制代码 * N* d1 K3 r5 r0 v
math.radians(x) 把角度x转换成弧度0 O' J+ w- K: S" G
- #把角度x转换成弧度
+ F3 n# [/ m) _; z6 v, ?/ W - radians(x)
0 S- c- d T3 m% W1 d( `6 w - Convert angle x from degrees to radians.
0 V% C4 f& @4 C; R$ A( h - >>> math.radians(45)( Q1 @) w; B1 S b; m5 Z) X9 k* V0 a- @
- 0.7853981633974483
! ^; \: |9 T+ o6 Y - >>> math.radians(60)
- i* w7 O. z; a. `/ F+ q* _0 Z - 1.0471975511965976
复制代码
, H* y, \) _, @4 F9 V1 `- A8 dmath.copysign(x,y) 把y的正负号加到x前面,可以使用00 \2 i3 n8 c# x
- #把y的正负号加到x前面,可以使用07 T9 I% Y" V! @9 R1 }9 R" a
- copysign(x, y)$ X- w+ t* R- ^, o
- Return a float with the magnitude (absolute value) of x but the sign
3 @) U8 I- N) r7 m P; m8 i - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
$ R+ ~1 E$ X3 y6 x$ e1 f& i# U- F - returns -1.0.
) r* {3 y" Q- c* |- q3 e; |# G% | - ; v& W) D' X/ c0 X) r+ D
- >>> math.copysign(2,3)
% S8 Z0 ~, D: ]0 D( p4 U6 u" b - 2.0
- w; X7 x6 a$ t+ \% M% e, ~ h - >>> math.copysign(2,-3)6 T0 I% N. e9 l5 n* X8 R
- -2.0( Q& ^9 J# @% ^' ~# ~
- >>> math.copysign(3,8)
. s" \: W% \3 m- x2 ]6 | - 3.0) q7 P+ Q+ u# D5 T+ w+ T1 y" u* T
- >>> math.copysign(3,-8)
3 H5 f+ N1 Q9 C5 S1 l - -3.0
复制代码
5 d$ _5 R3 Z9 R5 q# E9 P; ~2 E$ Umath.exp(x) 返回math.e,也就是2.71828的x次方
% w# V, ~7 u7 m6 S8 v- #返回math.e,也就是2.71828的x次方
& m% c$ i# v9 o( Y% V( } - exp(x)6 @" B* H) t2 F. D/ U) R
- Return e raised to the power of x.5 I; h8 x6 ]: i; u. J% x
- . I( W: g& e& _/ ~3 a F
- >>> math.exp(1)
: k' r! s* |0 d4 I( K' k9 x. G& K - 2.718281828459045
6 o! J$ G& g+ b6 e, G# {" g$ w - >>> math.exp(2)4 E) X4 e, f. P$ u0 d0 X
- 7.389056098930659 Q& ~. | O W
- >>> math.exp(3)- e9 D9 h: V* @
- 20.085536923187668
复制代码 ! H h4 B) F, b* X
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1 w- f" `9 Y/ e' P! q- n- l
- #返回math.e的x(其值为2.71828)次方的值减1/ C0 `( F; g6 R3 |9 O
- expm1(x)5 R6 k" I. Q1 y
- Return exp(x)-1." ^! j5 K2 D, k/ K; s ]
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, z& s" M5 r, A, x5 u" q, T9 ]" @ - * l5 q8 _) c1 g, [! X% Y
- >>> math.expm1(1)
# k) A& Q0 s* g) i* _ Z - 1.718281828459045. ]1 H, M7 u3 y: v% W
- >>> math.expm1(2)
f$ b( F/ h" P - 6.38905609893065/ p" A9 r0 B) R4 R3 c1 R( w
- >>> math.expm1(3)7 ~/ O2 [% R8 x& {! @ l# r; k1 L* S
- 19.085536923187668
复制代码
% a1 B* ]' ?! \4 @$ tmath.fabs(x) 返回x的绝对值
* q6 `) ~) p3 E: j7 h' l- #返回x的绝对值
1 L, w0 M* _6 k l5 `5 k. Q4 n5 [3 }; C - fabs(x)
& D3 i0 I: K; Q4 v8 H+ L3 X - Return the absolute value of the float x.$ I% b* w- H8 W& L5 D
' Q/ {; p. z' S6 m) Q" ]4 \; m- >>> math.fabs(-0.003)
: s& j2 E: B) j2 g0 e0 R, ]6 I7 a; F - 0.003
( t/ t# Z G+ n" x% U3 o - >>> math.fabs(-110)8 a. ]8 x% ^1 r% B. Z5 ~
- 110.0
5 \4 w. D4 b) Y: K* C; [/ t - >>> math.fabs(100)
7 _2 X8 j" v8 _. J/ l7 o7 t - 100.0
复制代码
0 V8 S1 l2 U% K @6 G5 i# O. Y$ [: \math.factorial(x) 取x的阶乘的值0 t k- `: q. O2 _
- #取x的阶乘的值. k' W9 _* S& M5 s* }$ A& j3 i) Z
- factorial(x) -> Integral( M# h7 r w) V) T
- Find x!. Raise a ValueError if x is negative or non-integral.! i& x9 H4 V+ q2 V1 L3 E
- >>> math.factorial(1)5 @7 ^1 u8 i$ k% p* P
- 1/ k4 ~5 x2 a! v a
- >>> math.factorial(2)+ R1 q ^/ d8 H: s! H. F4 O, s" d+ }$ Y
- 2
# S ?+ H& v! ` - >>> math.factorial(3)
! y8 |8 J9 j2 ]' W% q1 e! E* E - 6
}. S& }6 v$ Y% ]9 ]5 d - >>> math.factorial(5)& M" V+ y$ }) [) Y1 Q C1 ]3 B
- 120( v& y5 c! W% i
- >>> math.factorial(10)
, s& R6 P/ W' C# u5 b2 S% z4 H - 3628800
复制代码
4 v6 `8 D* z# Y3 [' T. zmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数. g' F/ X0 b: F$ x1 s) l( i
- #得到x/y的余数,其值是一个浮点数( \0 [$ b& O* M4 T% C. s4 G
- fmod(x, y)
1 H9 F5 G M; X1 k S - Return fmod(x, y), according to platform C. x % y may differ.4 M2 K4 c2 H: R2 L0 f
- >>> math.fmod(20,3)( l- s D' u v
- 2.0
. Q7 w) q2 W" H; V0 o9 s' u2 v - >>> math.fmod(20,7)2 f$ x) h8 i1 e2 D* W* J; T
- 6.0
复制代码
R0 u. y9 O. s3 j8 M* M( `math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, W. }0 i* W# n, I& Z6 z
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
, _. d. ]! A) d - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
5 |, N/ i0 [ j - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
4 K1 G+ k% y0 L$ [ - frexp(x)3 `8 O# n% V3 @- Y- O6 J$ H" K3 L
- Return the mantissa and exponent of x, as pair (m, e).; r( z5 }( T; A( P
- m is a float and e is an int, such that x = m * 2.**e.2 e& ^6 w% [2 U0 U8 J8 T
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
5 z/ m; S& s( W: w0 v; M6 x - >>> math.frexp(10), j/ R- r' r& F4 [3 x O3 V/ A7 |
- (0.625, 4)6 f+ i* I8 m. B; D
- >>> math.frexp(75)
8 a7 ~4 n9 f3 l- `5 T; K- j - (0.5859375, 7)
' l+ u1 u( U- r, H - >>> math.frexp(-40)
9 [' i+ s( v6 M8 b7 } - (-0.625, 6)5 P$ z3 [. W5 }5 X ~" H! E
- >>> math.frexp(-100)
! o$ U/ p G0 X9 C - (-0.78125, 7)
1 p: v) F0 z; ?$ f - >>> math.frexp(100)8 ^' v- N0 X0 w3 b! M2 [
- (0.78125, 7)
复制代码
t R4 c" q) T, m, smath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)3 A8 \% }' i( S# T8 L w
- #对迭代器里的每个元素进行求和操作% _) a! i# T) T, J
- fsum(iterable)
3 {( I; X7 y* S' N+ i( x$ T) R: d0 }+ g - Return an accurate floating point sum of values in the iterable.; y# l8 u. f9 E6 n3 D. S9 R
- Assumes IEEE-754 floating point arithmetic.9 ]6 V; a, F! G2 T2 c% K/ Q+ w; F
- >>> math.fsum([1,2,3,4])* ?. u5 ~2 Z1 ]' {: O3 e/ B
- 10.0
3 F( w E+ W' Y# P- l6 q - >>> math.fsum((1,2,3,4))
% J3 }% a' G) ]7 u* v - 10.0
' j- [6 Z3 m$ V/ `' O, ^ - >>> math.fsum((-1,-2,-3,-4))- W% ~# q% J$ W. ?: u& ?3 B( D3 B
- -10.0
Z0 z, x! v" R7 R. N1 c - >>> math.fsum([-1,-2,-3,-4])
8 _3 }) B" N6 N/ d& p6 t) K: P- r - -10.0
复制代码 + N1 m* n2 q& e" C
math.gcd(x,y) 返回x和y的最大公约数3 O# X% R0 @' d) S T
- #返回x和y的最大公约数
; W( X; Z2 ~2 Y! A) v7 |; @ - gcd(x, y) -> int8 J/ \( ] ?% j- K
- greatest common divisor of x and y
' y' z5 [8 l5 R0 j C - >>> math.gcd(8,6)
9 I$ X, X% }) Z0 V" h& Z - 29 q, C6 } q0 _/ A, ~5 w, N7 Y" ~
- >>> math.gcd(40,20)
6 q0 n! g& X9 W& A - 208 l- z# ~3 A8 \* E
- >>> math.gcd(8,12)
, T; e* P1 |5 z% \( q. l9 K - 4
复制代码
) _- l9 ?! \, c1 C: rmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
2 _6 J% G( J7 ^2 s3 c% q- #得到(x**2+y**2),平方的值
- s. F# v1 U( J) \5 Q2 Z, |# [ - hypot(x, y)
) v) g3 |, m; S; L6 d1 e% z - Return the Euclidean distance, sqrt(x*x + y*y).* j3 g* y! m+ o$ V% [8 ]4 t0 Z
- >>> math.hypot(3,4)
& P2 M; j: [/ q# V! l - 5.0& [+ k, ]3 B0 {& R% D8 A; J
- >>> math.hypot(6,8)
8 J4 s, l" F2 h& r* ~7 G - 10.0
复制代码 # S7 b& s7 V4 |2 u# `
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False+ W* X) _$ F3 D( Z6 T- a$ Q9 O
- #如果x是不是无穷大的数字,则返回True,否则返回False( r2 @& A9 X8 M! R- V
- isfinite(x) -> bool; }& J/ c0 A& c4 G6 Y- B6 y
- Return True if x is neither an infinity nor a NaN, and False otherwise.
, [7 b( f! p7 j - >>> math.isfinite(100)* p* b& ]2 @5 Z2 F# l( m
- True' n n2 M' b5 }; H% {/ X
- >>> math.isfinite(0)
5 O3 p. W+ B- ?0 z* r0 A - True" x H: U8 ?( e" X8 Q
- >>> math.isfinite(0.1). F" r8 c1 R; w& o
- True f6 n! B2 l% F7 L
- >>> math.isfinite("a")
1 x; h4 I7 O r3 `6 L" j5 v' o+ P7 g - >>> math.isfinite(0.0001)
& j- {4 O" s: W$ ]8 m, [ - True
复制代码
* l( ?/ ~3 E5 Z' I1 m$ y: Amath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 v' R8 X0 Z: P z* Y% `/ G- #如果x是正无穷大或负无穷大,则返回True,否则返回False
( S6 {. n" Y; V, K - isinf(x) -> bool
( g K3 t3 @1 N0 x8 T1 g5 Y& E0 ` - Return True if x is a positive or negative infinity, and False otherwise.
6 A4 T& U- z# n7 ^, h - >>> math.isinf(234)
' N" d8 H7 n9 \6 W2 c0 z M - False0 M3 z& _3 A/ r$ t
- >>> math.isinf(0.1)# _) r$ y+ z! h: B) K& ^
- False
复制代码
5 |. k4 s! ?$ c* o( K: j5 Emath.isnan(x) 如果x不是数字True,否则返回False
4 j: o5 X" I1 R$ j5 N$ n1 [- #如果x不是数字True,否则返回False
1 k/ G4 `, J) @( S5 b) S' \ - isnan(x) -> bool
2 m' s- Z+ a* O" x' c$ o - Return True if x is a NaN (not a number), and False otherwise.
- h5 Z, z. Y' o6 J9 f" [/ Y - >>> math.isnan(23)
( N. r0 H6 a w- x2 ? - False l: @9 F" p; T
- >>> math.isnan(0.01)
3 g9 ? z8 G1 O, K5 K, j3 f - False
复制代码 ) \- [+ B8 e5 b5 j% v+ M$ |
math.ldexp(x,i) 返回x*(2**i)的值4 p4 r7 g' n' _
- #返回x*(2**i)的值4 v- N6 y! @ f! y& b$ ?! @/ x
- ldexp(x, i)4 L! q0 W- F' H0 L* M0 h
- Return x * (2**i).
, x( ]/ f9 t& p- ]( X; r - >>> math.ldexp(5,5)8 M3 K0 Q2 U* F% [
- 160.06 n* V% w6 Y& A. g4 x9 `6 E
- >>> math.ldexp(3,5)7 d+ ~" m4 z1 M( ?% B0 s3 P O. W
- 96.0
复制代码 " V9 _# i/ G8 a! t1 Y
math.log10(x) 返回x的以10为底的对数4 U2 `! z& B. Q# H3 `* y
- #返回x的以10为底的对数3 v( U/ Q5 [6 I% i+ ^' u' t9 r
- log10(x)7 }. ^! r+ x# {8 y% U
- Return the base 10 logarithm of x.
" G8 K. M/ ~- t [ - >>> math.log10(10)2 W) H# [: [- ]2 A" s& H/ \* ]) f
- 1.00 W: M# x& F% ~& K* c* |! {5 ?; M
- >>> math.log10(100)
3 Z9 w" `. e$ g& Y - 2.0; i' y% l# f7 l- n7 t
- #即10的1.3次方的结果为20% ~& Y! |$ ?6 ~. a" _/ T
- >>> math.log10(20)- @. q! j; A9 \7 @
- 1.3010299956639813
复制代码 ( a% y9 R1 j0 [( K! \$ ?
math.log1p(x) 返回x+1的自然对数(基数为e)的值) T; V+ l. t) Y, V
- #返回x+1的自然对数(基数为e)的值
6 g8 {0 ?( S# L4 f! I2 l - log1p(x)
3 L5 @9 j M" c1 l, g* [/ P6 ~ - Return the natural logarithm of 1+x (base e).' \. e r, {% T( C& K5 f+ f( [
- The result is computed in a way which is accurate for x near zero.( W( O% t* W3 Z' N- a! Z
- >>> math.log(10)7 B+ ?. {! m Y/ s: D
- 2.302585092994046 M g/ J3 o3 Z$ @
- >>> math.log1p(10)% ^3 l4 L) H# B8 g2 W0 _6 v
- 2.39789527279837078 V# |- b3 p, W$ L6 K( ?( W- o! p
- >>> math.log(11)
2 a0 |; y4 e2 }6 T, k' s - 2.3978952727983707
复制代码
& R' W3 R* y( R4 L, tmath.log2(x) 返回x的基2对数5 [/ V$ g" B, X6 b# d8 M
- #返回x的基2对数
* W2 c1 [1 \" d( P9 V0 t) ^ - log2(x); ^+ A7 b0 j& z) }
- Return the base 2 logarithm of x." v& \* |+ e) X _8 x+ R
- >>> math.log2(32)
; t: e# x" ?+ e9 d( O% c( ~! N9 A - 5.0% @( ?& G& i3 }6 a3 ?1 C
- >>> math.log2(20); i& H) \; g' W# @5 S: g/ `8 l
- 4.321928094887363
* U! c+ _; Z5 g" c7 ` - >>> math.log2(16)$ C# s8 j7 a' s: X# p
- 4.0
复制代码 / N0 U8 H. x- X; K( R6 V
math.modf(x) 返回由x的小数部分和整数部分组成的元组
8 u4 c6 N G4 K- #返回由x的小数部分和整数部分组成的元组
: C7 a0 x" @5 r - modf(x)& g1 j s, l$ Y4 B/ g
- Return the fractional and integer parts of x. Both results carry the sign) g' s P* G$ d) y5 J
- of x and are floats." V* V$ Y/ H1 F7 `1 H3 k2 f; J% x2 k
- >>> math.modf(math.pi)
0 L, M6 I8 p6 D, } - (0.14159265358979312, 3.0) ?5 l) N7 _& z h e- y
- >>> math.modf(12.34)1 O( q' \/ v; g5 D4 D# v0 B# j6 b
- (0.33999999999999986, 12.0)
复制代码
. k! }* M+ Q7 n1 P# _" q1 m9 Gmath.sqrt(x) 求x的平方根* F, P4 e& p% D' D2 o; ?' i7 f
- #求x的平方根+ V% d$ t8 N! v8 f5 d* N! p7 V2 x
- sqrt(x)
% \6 e* z% B7 a: _6 m1 f7 M8 j - Return the square root of x.8 |) h! W7 a$ v# T: ^
- >>> math.sqrt(100)
# G. r0 t; v, q4 k# H - 10.0' y3 Q7 _: Z2 j; }( @' w+ W3 v/ U
- >>> math.sqrt(16)) M; z1 R$ w: \# @7 _! Q
- 4.0
! J6 C X3 v4 X1 W1 Y3 j - >>> math.sqrt(20)9 [% a9 p3 q& d2 J8 R
- 4.47213595499958
复制代码
, n1 y' [* X$ G# V& h9 x/ rmath.trunc(x) 返回x的整数部分- #返回x的整数部分
' w6 h7 Y L2 u* Q* Y - trunc(x:Real) -> Integral
7 y) c# C' y5 j5 m! {: T w - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.) l- E2 F7 _) N& V
- >>> math.trunc(6.789)# M/ `7 [( K& Z
- 6% q0 v* f, Z3 `7 {
- >>> math.trunc(math.pi), A8 `# @. |% p: a( H! [9 _5 A
- 31 u( Y- L: Y& G M: r/ n
- >>> math.trunc(2.567)
( X/ S! a" Y2 J& ? - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|