马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
: x _/ F8 u" }' m( \) R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 u5 l9 x. F+ l/ S+ r+ T
7 O3 K. ?: e! @
方法1:
0 z* D) h2 y% Z T; s- >>> import math& v: z4 O( I3 a# S7 C
- >>> math.sqrt(9)3 O- I- Q s* [4 o
- 3.0
复制代码 方法2:5 ~4 F: Y' Z! y* `7 O
- >>> from math import sqrt
' n& p7 Y: @( H% e) D$ K - >>> sqrt(9)8 S7 H2 @6 a: ^ u8 d7 z g. |/ K2 @& n
- 3.0
复制代码 ; b$ @7 I4 T$ u3 l2 e) J' S. v" t
( R6 G2 T: v1 j; ~$ ^math.e 表示一个常量
7 r; F- [9 Z1 A5 ?3 S% I/ ~' ^- #表示一个常量+ f5 Z+ T" p l; j$ t
- >>> math.e
, {$ D5 Y# N+ @ - 2.718281828459045
复制代码
, j3 h( U. v- X6 v" G. U, r* O& _* Vmath.pi 数字常量,圆周率
) j; h7 |$ l( H$ Z% P( h8 A- #数字常量,圆周率
5 u) }6 `- k! e% ~& p+ y3 g - >>> print(math.pi)
3 e! `* e$ T x$ R! M$ @* B - 3.141592653589793
复制代码 8 [3 p1 |0 N# t3 ]6 H) y% B" g
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
( ?+ Q- ]: ?- P# [, o( y- S- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
+ B6 A& J' Z% c- X$ t/ ?9 z6 X - ceil(x)
9 l; _- ]3 d/ v& { - Return the ceiling of x as an int.& f) K3 e7 y% w- G* y9 U% c4 t+ W
- This is the smallest integral value >= x.* d1 r( {+ E; O. J8 I
- * P% C4 C& P; q+ E2 \( W, }
- >>> math.ceil(4.01)
4 }1 w- |5 ~# X7 X' f* D - 5! q8 S' j8 ?/ ]' ]* ^
- >>> math.ceil(4.99)
1 g# M! K/ M& H, v4 P; Q9 z - 5) U' Y# t+ [" j; l5 ^
- >>> math.ceil(-3.99)
$ e0 z' Z2 O6 C4 y. d7 Y - -30 u0 [: n0 v6 @) k: F) g* L
- >>> math.ceil(-3.01)7 N5 Q Y H7 O9 V" c$ a; b2 H% J
- -3
复制代码
1 c3 r* o1 U* m1 O( U1 B( umath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 Q, o. q6 z- Y2 M; i p4 K8 d# s
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# n- O$ R0 ?, A/ [. w7 N - floor(x)) M, Y5 H, l# N6 k& v
- Return the floor of x as an int.
0 O" t6 A0 f! S2 n - This is the largest integral value <= x.
$ f; e, Y1 o* h6 j7 @* p - >>> math.floor(4.1)' J/ P j1 ~! m9 o' I5 `
- 4, Y# S/ s X& D, C6 E. [. o- I
- >>> math.floor(4.999)
( a8 Z$ W! q3 Y" K" m( E - 4
# W' k8 [4 W1 k' K+ } - >>> math.floor(-4.999)$ J, R; i5 H& [$ S
- -5
: ^4 z1 i/ r5 w% K7 W* S - >>> math.floor(-4.01)- j( J: ~. {( O& @, H/ U8 w; ^
- -5
复制代码
/ T7 H* r" h" E6 \6 vmath.pow(x,y) 返回x的y次方,即x**y2 J" Z: {- P# m6 b6 W& k! ~
- #返回x的y次方,即x**y8 O3 x* r. K' o' v9 S) d
- pow(x, y)4 g8 \4 k* b' g! Z
- Return x**y (x to the power of y).
& B2 x0 u0 h' r1 H' Z - >>> math.pow(3,4)
( }5 ^2 ~( O3 u% B$ r6 L* b - 81.0
7 _; h7 r. H; m1 o: C0 v# Q6 ^ - >>>
% G0 r+ I1 N, R& V: c - >>> math.pow(2,7)
! @/ t+ D G/ }% z, C2 b - 128.0
复制代码
* v+ y, d5 ]8 {2 n# j7 ?& j |9 rmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 k) D9 @- C- c4 y0 F1 w- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 k4 Q# T b5 k( H/ m - log(x[, base])
! c* r h3 j! O* w' g' @- l - Return the logarithm of x to the given base.
2 M! W, s( g) \) o- G - If the base not specified, returns the natural logarithm (base e) of x.$ N) W4 O R$ s: x. H
- >>> math.log(10)
9 g2 p/ L! k8 D/ S* N! e1 B* R - 2.302585092994046, f& h9 A' ~. Y; ^9 v; N+ s1 w$ ]) j
- >>> math.log(11)! O) ^+ h. \- p$ F+ N
- 2.3978952727983707
6 P9 }- h) G3 s U% Q* Y - >>> math.log(20)
: i$ _. s) y$ C( {- `0 V: W9 [ - 2.995732273553991
复制代码 4 B4 [' e, r: B0 a8 b
math.sin(x) 求x(x为弧度)的正弦值
$ G" y+ z7 g' {$ y& P6 j6 j- F! k o- #求x(x为弧度)的正弦值
9 m# G" D4 e3 Y1 q1 H7 | - sin(x)
" f: l" A' x- a4 n' \9 Y3 x6 C8 \0 {& [ - Return the sine of x (measured in radians).$ n$ G- l! V5 q. H& E1 H8 S5 K
- >>> math.sin(math.pi/4)
, V9 ^# i6 t. L) ] - 0.7071067811865475! }0 U5 K( l- X, S+ B# I5 t ]7 L
- >>> math.sin(math.pi/2)
0 z: x4 N1 ?% D+ L - 1.0
$ k; V4 v( r/ m; \4 i - >>> math.sin(math.pi/3)
2 N' N. c& g" }1 f, _; @7 _ - 0.8660254037844386
复制代码 ( R/ q6 S k" i! }( h* g
math.cos(x) 求x的余弦,x必须是弧度, R! @7 B4 j; h
- #求x的余弦,x必须是弧度5 M3 {5 ?( b6 m3 ^: O
- cos(x)7 {8 w# k& {2 Z( j
- Return the cosine of x (measured in radians).. L+ X6 w+ D5 Z, g' G
- #math.pi/4表示弧度,转换成角度为45度8 Q! k- c& \+ m+ O) T- x+ v
- >>> math.cos(math.pi/4)
3 i* G" A6 | K F2 n' {: V) ? - 0.7071067811865476
1 H% s1 z3 a/ E - math.pi/3表示弧度,转换成角度为60度
3 R: ~$ j3 N* N" m - >>> math.cos(math.pi/3), g8 g. I: G+ o) r: N4 v
- 0.5000000000000001' v, x" M' W2 o+ n' {* b v
- math.pi/6表示弧度,转换成角度为30度
5 C& @5 ?; \. J2 d7 [ - >>> math.cos(math.pi/6)
" S% L6 Y( O; a - 0.8660254037844387
复制代码 ; s( e# h4 V# c _! g& l# @
math.tan(x) 返回x(x为弧度)的正切值
* K3 r* `5 @2 g ^; z$ \5 }1 m- #返回x(x为弧度)的正切值0 w8 j; y' i8 a& Z
- tan(x)
! K* b1 q+ H; b6 x" b - Return the tangent of x (measured in radians).
. P3 n, w; l" L# s+ U/ }5 s v - >>> math.tan(math.pi/4)( }' Q) W4 W, X) s3 u: z; ^
- 0.9999999999999999* j% ?" y. m0 V4 r2 p1 d# n4 `
- >>> math.tan(math.pi/6)1 p5 e# Q% k% e. r5 l+ x! W1 J
- 0.5773502691896257
v" }# O5 \7 o; L - >>> math.tan(math.pi/3)
# X) U8 |( K: }* S$ x7 @( g* m - 1.7320508075688767
复制代码
' I$ G2 X2 G0 Y; G* s9 Y8 Omath.degrees(x) 把x从弧度转换成角度! y& S' q( D) P6 g9 Y0 j
- #把x从弧度转换成角度6 q9 d( b; A0 L
- degrees(x)# _6 f* Z, W- d/ I$ q$ Z. G& W
- Convert angle x from radians to degrees.! y" R' i' p9 v% S
+ Y8 E7 f' I" y. Y% F- >>> math.degrees(math.pi/4)# q& T* X/ x/ _4 }$ p+ ^( Y" s
- 45.0* g' U. k( D% H( c5 s$ _1 v, P* H
- >>> math.degrees(math.pi)2 p- L; s4 K; T9 \: U/ o3 {! K
- 180.0
- ^. o f( |) e+ @- n - >>> math.degrees(math.pi/6)
7 z# D# I c+ L* x - 29.999999999999996% E4 ] c; I' b
- >>> math.degrees(math.pi/3)
n6 Z2 Z9 [3 q' W/ V - 59.99999999999999
复制代码 3 w( T: Z: q# m+ k
math.radians(x) 把角度x转换成弧度% k. r& D/ x5 \/ |+ p0 x
- #把角度x转换成弧度
F0 U; A4 ?' d - radians(x)2 {: c0 @; }6 N1 @" h/ y
- Convert angle x from degrees to radians.7 S! A* K/ F! k9 m7 A0 y# {
- >>> math.radians(45)( e& F# y k' a: E8 W0 K
- 0.7853981633974483
- ~9 x8 A1 f q& [' q - >>> math.radians(60)
7 q4 r1 r5 O+ M" C. d+ @8 n - 1.0471975511965976
复制代码
2 K( r- P; F+ z; l' X; h L/ M( A. Dmath.copysign(x,y) 把y的正负号加到x前面,可以使用0: S! Z2 i7 X: S# ?2 B' e. q7 ]) @
- #把y的正负号加到x前面,可以使用0
8 h9 ~" ?5 e9 N$ c% h: ] - copysign(x, y)& K3 c( F9 S! ~7 h& F9 D
- Return a float with the magnitude (absolute value) of x but the sign 0 s3 ~+ A8 S8 Q3 X
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
% X/ ]) K5 u5 c& { - returns -1.0.
$ T0 ~. u( @- z4 z2 n- B1 P0 x$ `: W
4 Q! l+ P% l7 U( {' i- >>> math.copysign(2,3)" u# J; N# x- {; k' M0 r: i/ t
- 2.0- M$ G% ]9 P' {7 M! C& ^
- >>> math.copysign(2,-3)( ?% r& \4 i' G6 `8 P6 U0 O
- -2.0
; g2 @( w" H+ l5 |3 u* X; Q - >>> math.copysign(3,8): R" h& T; a6 e+ Q$ _. l, a P" z
- 3.0
5 |0 Q1 v% ~9 q& P3 k - >>> math.copysign(3,-8)
- w3 _6 A7 h a. F( u8 | - -3.0
复制代码 + n" Q9 X. _' R, O
math.exp(x) 返回math.e,也就是2.71828的x次方# g( N7 }4 A/ p$ a- i( M) J: B, |( `
- #返回math.e,也就是2.71828的x次方
% a2 F) d+ q' M: F1 } - exp(x)- ~6 X. v1 l- Z
- Return e raised to the power of x.: t+ Q% w$ h2 P. k8 }6 E/ e6 ?" L
% D6 D. H/ e% E5 r5 }9 ]- >>> math.exp(1)( C9 S, T/ v) X
- 2.718281828459045& u2 ~2 g* r- {7 ?4 A
- >>> math.exp(2)6 f5 T0 }6 ~( F3 {5 t- ~# @4 W9 D
- 7.38905609893065
% L5 V! F# I& v Q - >>> math.exp(3)
- M9 o( Y# M' h* _( O - 20.085536923187668
复制代码 , E f3 \* z$ e* l* j
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减14 W( ~/ R1 _! u( T# |5 R3 V
- #返回math.e的x(其值为2.71828)次方的值减1" i) ^( {. h: s0 q" Q0 x; A: v2 S3 x
- expm1(x)
1 J+ S! Y0 M: R) T - Return exp(x)-1. q$ Z& C8 `% C) X4 ^. M* m1 S
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.' Z( q/ I9 ?3 b# f
- 7 B+ Q+ |1 }) \& E
- >>> math.expm1(1): J6 m" S/ D/ M3 m6 v3 K) n
- 1.718281828459045
% b$ m' q% Y q* d0 | - >>> math.expm1(2)
; P4 I% a+ \* _: F" { - 6.38905609893065
' d/ X! C8 T( g% U' L! [1 H% V: u - >>> math.expm1(3)- e4 i/ i- D. x) k: J3 d
- 19.085536923187668
复制代码 7 f2 H4 e3 t9 h" j2 F( P* S
math.fabs(x) 返回x的绝对值# X" P( j. q: D- v7 i" [, A
- #返回x的绝对值0 |6 W$ C" o+ i$ K% I0 v
- fabs(x)
; e# h" v! n4 K g - Return the absolute value of the float x.
5 D( q& P7 w/ D9 N* \0 e) b
8 K+ _' a7 G; V# q# P5 y' F' d1 ^4 S- >>> math.fabs(-0.003)
! c3 s1 X0 G0 D7 }5 i& S7 a i - 0.003
( R. l+ Q/ d! [. A2 q - >>> math.fabs(-110)' @, q) V/ e) U6 E0 j _
- 110.0* L% o7 V: z- G2 o( D
- >>> math.fabs(100)
2 g$ z% I# H2 D8 l$ @7 t9 v - 100.0
复制代码 ( M+ H3 X: f' @0 a9 \
math.factorial(x) 取x的阶乘的值 n" t* K H! ^$ r) {
- #取x的阶乘的值( `: D- p. g! T
- factorial(x) -> Integral/ T ^# j5 O* Y1 _
- Find x!. Raise a ValueError if x is negative or non-integral.* v# V% J* q3 T8 F! |9 i8 M: }$ Q
- >>> math.factorial(1)
8 ]5 q$ F5 s. N4 T - 1/ I9 P2 `7 I2 M! q4 P
- >>> math.factorial(2)
, }7 k7 v4 x9 e6 t' a) y7 { - 2
* N2 ~/ a0 N) S - >>> math.factorial(3)* w. L' E0 r1 g9 O8 X
- 6
$ r& t) _# c# Y% j) q) R4 y) ^ - >>> math.factorial(5)- m+ b: ]& Q. i* f* Q# V
- 120
+ [+ u/ E$ n/ F0 W7 Q - >>> math.factorial(10)# B: W- t6 m- P' Q
- 3628800
复制代码 ! P. z1 y- x* N1 }
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
' e- ]: g9 k2 S- #得到x/y的余数,其值是一个浮点数/ h9 m* j6 a9 J1 G9 ?
- fmod(x, y), ]8 K. V; q! u
- Return fmod(x, y), according to platform C. x % y may differ., {3 l, L6 n, O2 ]- T
- >>> math.fmod(20,3)
+ g$ t% V$ ]5 Y { - 2.0: h) \* _3 O- k( c) I
- >>> math.fmod(20,7)
6 m+ U. e, d+ ^ ]! ?* _" t1 K - 6.0
复制代码
8 A s" }) f( B) g7 [math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
" o$ K! j8 ~6 V$ E/ e( j. k- f- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
2 k! K- I' s7 O; e5 a - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
' \0 Q# O7 U2 Z - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 c4 W6 _: E" @2 X- N - frexp(x)
5 J7 Z% k$ W6 [$ @ U/ M: M$ @1 t - Return the mantissa and exponent of x, as pair (m, e).
2 z# ]0 |+ V! s1 j - m is a float and e is an int, such that x = m * 2.**e.
& g1 Y) L a8 W$ C2 k - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
7 n _, O+ k! p+ b$ B - >>> math.frexp(10)
! y( Z2 t8 j: c! p3 c- U - (0.625, 4); U- P! v# I3 d! V* ~6 _4 y: _
- >>> math.frexp(75). G: J: v' s# ^# Z, C6 c( N/ [ B
- (0.5859375, 7). ?7 O- N: _! v2 R8 _' @3 \
- >>> math.frexp(-40)% P8 b# E9 q% K; \6 z C p3 i5 t3 \3 Q
- (-0.625, 6), C* b" `: ?1 ~
- >>> math.frexp(-100)
) \# |/ f# u" [ - (-0.78125, 7)
9 S& F! u3 B2 { - >>> math.frexp(100)
( `5 n2 ?+ Y$ U; z - (0.78125, 7)
复制代码 ( N D. |. y+ i" `
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
! ^/ S7 L& F/ s1 Q5 g+ ^/ m* Z2 } E- #对迭代器里的每个元素进行求和操作- i5 A$ w7 H% u4 O
- fsum(iterable)9 E; ?; \& p( \# W9 S
- Return an accurate floating point sum of values in the iterable.
! A5 e! j4 K7 P$ W. U' m$ O - Assumes IEEE-754 floating point arithmetic.2 n, L, D+ Y p& p* G. N ~
- >>> math.fsum([1,2,3,4])) W' E, S x2 \3 h& o& F% s
- 10.0' }6 K2 _ [% a+ a3 x5 \
- >>> math.fsum((1,2,3,4))
9 v9 ?, x% z! p% d& @+ U; H/ u" { - 10.0; r4 b6 Q" o. n g$ q, I
- >>> math.fsum((-1,-2,-3,-4))
7 s- v' h& C9 c( z; A$ ? - -10.0
2 s7 W, B& ~. @) H! z2 i7 I* q - >>> math.fsum([-1,-2,-3,-4])' t1 y ]' Q5 ^
- -10.0
复制代码
4 K. J# i# s, Q# Ymath.gcd(x,y) 返回x和y的最大公约数- j/ n9 r: q4 g
- #返回x和y的最大公约数
' o4 i' T2 O7 |) Y# i5 X - gcd(x, y) -> int7 r- V: }( F# E/ B- v$ W) w3 k) S
- greatest common divisor of x and y
5 \5 H0 R3 j) t - >>> math.gcd(8,6)
# h* [- B/ t9 d9 }/ X3 g - 2" t6 ~ c2 l! X n( e! q# @
- >>> math.gcd(40,20)
9 l) I% {! T8 K7 R - 20
$ M$ L) p' E$ z! w3 F - >>> math.gcd(8,12)2 u) T9 c2 t) t& c
- 4
复制代码
! ~9 Z5 X0 \7 ^% X. N' F2 W( i$ _+ omath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False% M1 i8 i+ C" Y4 S/ q( {
- #得到(x**2+y**2),平方的值
2 V# i6 k: H# } u' f - hypot(x, y), a1 L6 e* f% H3 v3 @
- Return the Euclidean distance, sqrt(x*x + y*y).& R! Y1 L, c0 d( u2 J, m, R
- >>> math.hypot(3,4)8 l3 x4 t" m0 u) q0 Y5 l
- 5.0
' P0 E! E$ j6 f2 d - >>> math.hypot(6,8)3 c) l1 N' K3 i- p+ d2 u6 d/ ^! y
- 10.0
复制代码
2 G: X% Z7 ~; T6 d( s7 J5 ]1 }% @math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False( B; { c( ?+ {
- #如果x是不是无穷大的数字,则返回True,否则返回False
# H; l! y6 Q! q1 G6 J+ d! Z - isfinite(x) -> bool
b+ [) i4 x% D. U$ @8 o - Return True if x is neither an infinity nor a NaN, and False otherwise.& }# Y" D! S E& T- E5 e& W, p# }
- >>> math.isfinite(100)
' ^3 }5 M# P! m2 R7 y - True7 {$ m6 ]! [- B: [1 [% Q: V
- >>> math.isfinite(0)% ^9 s" I4 U: N5 U
- True) M) E: f: c2 I( K n+ q! ~ O5 h5 e
- >>> math.isfinite(0.1)& {8 k. s4 L \2 s
- True
0 h, w3 f$ G+ Z+ \( |& E# B - >>> math.isfinite("a")# k0 y9 }" ~/ X# W: k" L! M: f
- >>> math.isfinite(0.0001)9 A6 f7 {, O( Z) U" ^
- True
复制代码
) L9 {- ^/ g8 X# Dmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
" x* b6 G& ]+ [+ l2 E8 V- #如果x是正无穷大或负无穷大,则返回True,否则返回False
4 |" [0 c% y4 t% V - isinf(x) -> bool& M. M& t! `/ g7 G& A
- Return True if x is a positive or negative infinity, and False otherwise.
" D) M7 ^0 Q* E( a - >>> math.isinf(234)
# |4 e+ D9 F ^3 \% j* L - False/ {* X# H+ O& `. O- m5 S4 Z
- >>> math.isinf(0.1)+ m ?1 ~% `9 b7 s
- False
复制代码 5 ], e$ { `8 y2 y: b
math.isnan(x) 如果x不是数字True,否则返回False
4 Q8 f8 C7 R) o- }2 t2 y2 R* H3 t5 h- #如果x不是数字True,否则返回False
) N' k( l- L) G - isnan(x) -> bool9 B K, n u( |! e4 I6 U
- Return True if x is a NaN (not a number), and False otherwise.
6 Y8 _; ^. M" T X9 \( {/ f - >>> math.isnan(23)5 M$ w8 ^% K8 A4 t
- False
: Z5 L1 ]. n8 B3 N9 _5 l - >>> math.isnan(0.01)
6 o6 a& u% Y* [# P7 D - False
复制代码
- S* `! L) J5 `* Kmath.ldexp(x,i) 返回x*(2**i)的值9 }) c0 S" z, F1 ], l
- #返回x*(2**i)的值0 F3 K, B. ~& a5 e
- ldexp(x, i)
9 c7 w0 m* i x9 Y) Z - Return x * (2**i).
, ] s, t; _; P' }; [ - >>> math.ldexp(5,5) `) f A/ ]$ s
- 160.0
- `5 X& f# n. H: F - >>> math.ldexp(3,5)
6 D1 U) ]5 o. s- T: E - 96.0
复制代码
, I* j! Y v% gmath.log10(x) 返回x的以10为底的对数1 w- ?, L* W7 ~# c, e2 ?/ P
- #返回x的以10为底的对数
, l. V5 T( z$ l& ?$ [ - log10(x)
0 p3 d- z' ~6 A1 | C# p1 V$ y - Return the base 10 logarithm of x.
; C& m2 M) [ ^# ?; k6 g- t1 @4 v; j - >>> math.log10(10)
# N# J% r: m1 R* Z9 ] - 1.0
( X" Z3 d5 E" l3 K; J1 ^ - >>> math.log10(100)
2 u. b4 j P; \5 ^$ n - 2.0
- X- z* d5 g, F. F n - #即10的1.3次方的结果为20
" x, B4 D) D- `" l8 B. b" B% q+ ` - >>> math.log10(20)
\8 ^: T$ |' F" l; m - 1.3010299956639813
复制代码
8 z9 P( [ q1 \4 B7 Q" K6 Lmath.log1p(x) 返回x+1的自然对数(基数为e)的值# _# t% S# v; g' d- N
- #返回x+1的自然对数(基数为e)的值
1 b9 ]) t' R/ L7 R. y - log1p(x)% V' W4 F9 X1 t8 U# p
- Return the natural logarithm of 1+x (base e).4 Z$ `8 s4 ?0 c; b, T; o
- The result is computed in a way which is accurate for x near zero.
4 N" P9 G5 \- Z% R$ B) w - >>> math.log(10)0 {/ J. x5 j% B3 v
- 2.302585092994046 \6 B- I/ O0 a; W+ w1 C' g
- >>> math.log1p(10)
* F, i; f$ H) }5 D* S$ ]- F" u - 2.3978952727983707
) Q9 Z; [5 [) I+ o - >>> math.log(11)" d1 Z2 L" M9 F7 U
- 2.3978952727983707
复制代码
9 ?) u. j9 i$ l8 ?# K" xmath.log2(x) 返回x的基2对数
/ V1 `0 w8 Y1 U8 u' D6 X$ |& \; x' |- #返回x的基2对数 t1 k& p" z: Q+ K) M7 z; n9 }. o
- log2(x)
. o C1 F/ \* W, c9 ?5 s4 { |! G - Return the base 2 logarithm of x.
/ g- k$ z$ Y9 s9 V; v8 f - >>> math.log2(32)( f& F2 f9 V. K: P3 e1 D* S
- 5.0
, k% V! J7 A: B: n/ d, t) P - >>> math.log2(20)
1 I3 o8 l7 i! v, O7 i - 4.321928094887363' v7 \7 q+ N, q' U1 x% w( h
- >>> math.log2(16)6 E0 p$ N% a# w2 E- H1 O0 d7 P
- 4.0
复制代码 * N; g7 H. a! r9 A
math.modf(x) 返回由x的小数部分和整数部分组成的元组( I \) q* H. `, ^+ C7 a
- #返回由x的小数部分和整数部分组成的元组
: j. i' a4 C% Q. R. W - modf(x)
0 c- l9 r- q F4 `/ a9 L' O - Return the fractional and integer parts of x. Both results carry the sign
1 Q+ _6 M- L5 O1 b - of x and are floats./ c% M, M, F! T. i
- >>> math.modf(math.pi)4 ^0 b, M5 [. E Y0 c/ Z8 q
- (0.14159265358979312, 3.0)# x% K9 `. m: V/ i8 B# ^% m
- >>> math.modf(12.34)6 M1 k p1 t# t$ i( ^
- (0.33999999999999986, 12.0)
复制代码 & q3 g7 _& a$ Z1 C
math.sqrt(x) 求x的平方根
4 c0 d. o& A) j6 V% w! M- #求x的平方根( P$ l. z# g7 Y0 D9 e4 t
- sqrt(x)
$ u& G4 a& X% t5 p6 e: _, R - Return the square root of x.
1 c4 O: |; ?6 @$ C# G7 l - >>> math.sqrt(100)3 z8 E. M- H$ e+ }8 m
- 10.0
/ @: X( Q( I$ U. x - >>> math.sqrt(16)4 Z/ K# B1 ]% p& y4 G! j
- 4.0
! s% k' `. P- L1 K; | - >>> math.sqrt(20)+ K% K! _9 K; i6 l6 y9 n, P- ` a5 F
- 4.47213595499958
复制代码 1 t/ H) t ^" _) ]7 `$ X
math.trunc(x) 返回x的整数部分- #返回x的整数部分
. c# Z/ z2 E+ \9 {+ C5 q9 i - trunc(x:Real) -> Integral
0 M5 _' Y# `* J - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# K: R/ z4 J3 @
- >>> math.trunc(6.789)1 c8 k- ^* B% g5 N( [
- 62 h$ l9 G1 o+ M# @
- >>> math.trunc(math.pi)+ Z: b6 q2 }9 }1 \! ^( i; H
- 3; E" ]; f; L$ E8 r. G( \7 T% V' p* m) y
- >>> math.trunc(2.567)
& j, T( `8 O1 A$ x# I - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|