马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
, U5 G. k7 z4 t7 u6 T2 ~【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! Q6 f' o9 U1 n
2 }* K. J- L9 K+ ~0 U9 h" Z; O
方法1:' D5 B' y0 s C: S* J- P9 H
- >>> import math
; \' l2 m- g r3 E; T/ `' u - >>> math.sqrt(9)* w8 O' ]7 N( D& n) H. j; _
- 3.0
复制代码 方法2:
7 x8 Y% w1 |4 a5 t- >>> from math import sqrt7 G8 T2 a; r3 p; O) b* Q P
- >>> sqrt(9)
: X: L1 M+ r& Z8 A - 3.0
复制代码
9 I: b0 a% U( }& ^* _# p " D, ?/ V' ~) [1 P7 L1 T4 u
math.e 表示一个常量* A# q7 z' `- [4 c" L
- #表示一个常量% q; E# f) P- M4 i n D
- >>> math.e
: C5 N' P4 g; q; S' R- B - 2.718281828459045
复制代码 0 v" B1 i: g" r
math.pi 数字常量,圆周率# ~# `0 _# O2 L" T) F3 y, v
- #数字常量,圆周率
' V4 ~1 l1 y* S2 n/ L+ y* ?7 F - >>> print(math.pi)/ t1 L3 Q9 p, k& s
- 3.141592653589793
复制代码 8 i0 e7 R- H% ^7 V i
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
* r; ^7 Z5 k \# X3 a- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 r R& M$ ?9 S. O X - ceil(x)
) h- l. V. u) C! |! v S - Return the ceiling of x as an int.$ R9 p; k; S' u: s" O0 t. ]
- This is the smallest integral value >= x./ ^+ B, R/ l, f6 t8 Z
- % C) [4 W* P' f$ H- r1 Q; `! o
- >>> math.ceil(4.01). R0 k& b+ K+ L1 ^" W, `
- 5+ s$ ~; X8 b3 \* D- k
- >>> math.ceil(4.99)
# b6 `1 h* M3 B$ q$ x1 z* [7 b - 5
9 _" K" n3 H$ l( P - >>> math.ceil(-3.99), J, n4 ]" C* P9 T
- -3
4 c$ i9 L# k3 d7 Y1 P - >>> math.ceil(-3.01)
, t+ p+ b" i0 L5 \2 V9 ^" _ - -3
复制代码 0 D, x; T" u. F
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身 V6 ?7 [ T: G5 T" \5 p6 E% v
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 s' ]+ c% U; C8 p
- floor(x)
G. v3 G1 j9 e* F1 Q6 L+ U8 H - Return the floor of x as an int.5 i' d: ]1 m9 W; w/ U
- This is the largest integral value <= x.
" F! r' s1 P9 U& j0 Y - >>> math.floor(4.1)
. w2 B$ e& Q& a' \5 Y - 41 K+ N( o% K6 w) A
- >>> math.floor(4.999)- l* R& @+ X- @! E3 D
- 4
* i, S a7 D/ x4 V6 p - >>> math.floor(-4.999)- I3 ]0 t! Y, y, ], ]: A
- -5
! E B# }1 j5 M - >>> math.floor(-4.01)
`* ?. |- J0 j - -5
复制代码
1 D5 w, k. ^( c9 j9 omath.pow(x,y) 返回x的y次方,即x**y# T5 C, [3 ?4 }+ j S
- #返回x的y次方,即x**y9 Q, J/ s6 L/ n' ~. O# b
- pow(x, y)3 _" L4 {9 ? R+ l3 z0 E
- Return x**y (x to the power of y).2 T( }+ F" a. ^/ y8 V/ ~" n: N
- >>> math.pow(3,4)
; A- |6 v2 `: J6 r& ?$ I - 81.0
4 U& F# h* d E7 `- U% L+ V - >>>
8 r# A6 B$ M$ L' A+ p2 g - >>> math.pow(2,7)! ~" e# I6 h6 G! c
- 128.0
复制代码 % x$ s4 ], R/ v) f
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: E5 z- D2 S' w% L- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& O5 G3 I9 T- f- ?: p0 a2 R4 c - log(x[, base])
( ^2 L; u( K7 [) e - Return the logarithm of x to the given base.
3 p# Y- C1 t7 Y- ^ - If the base not specified, returns the natural logarithm (base e) of x.
7 X- `' j2 Y5 G - >>> math.log(10)' P) c- J; P. Y( g I3 M4 j
- 2.302585092994046
' G8 F. c( B$ a8 [) v - >>> math.log(11); P2 ^" E K* i. k( w) V3 W
- 2.3978952727983707
! \, N2 h; _ o; Z: f- ] - >>> math.log(20)
5 a0 k. o( k0 E! |3 P/ g; e, g - 2.995732273553991
复制代码 - Q0 g% [1 N& `/ d7 j8 d
math.sin(x) 求x(x为弧度)的正弦值! u. ?$ Y& Z0 F* l G" J
- #求x(x为弧度)的正弦值+ d: d& e ]) e0 A
- sin(x)
* H' `% |: P' ^6 ~, E4 |: F - Return the sine of x (measured in radians).
+ M2 V0 d: T6 I+ L& m - >>> math.sin(math.pi/4)
& V0 z9 t3 s. {5 Q& m/ v: V - 0.7071067811865475! o5 ?! M5 n! T4 |5 ~% M8 A6 u6 k
- >>> math.sin(math.pi/2)7 ~4 w, ~7 F/ j% n) O
- 1.03 f Q, J/ }. j# N
- >>> math.sin(math.pi/3)
6 b% p# S. ?( p% V9 b- m - 0.8660254037844386
复制代码 8 ^5 K) v5 M, B% F$ ^( G& d
math.cos(x) 求x的余弦,x必须是弧度' f9 I) [, ]% F$ J; _: B8 r
- #求x的余弦,x必须是弧度
9 A; J; k3 l1 S: W5 K* b9 |$ }) } - cos(x)
; T0 B3 l( K) p, R* p - Return the cosine of x (measured in radians)., S9 P4 [" @# C% O C: Z: M, M
- #math.pi/4表示弧度,转换成角度为45度3 z# _3 ?& u5 e: r: u. r- W+ s
- >>> math.cos(math.pi/4)% J% k. \6 f5 M8 T% S v
- 0.7071067811865476
6 ~' h" y5 n. P' B7 g7 j - math.pi/3表示弧度,转换成角度为60度- \0 h0 b$ O* V- g0 x1 h
- >>> math.cos(math.pi/3)
7 e) I" U6 j" a5 b+ ^4 K - 0.5000000000000001. c) z* \( t s. ^$ ~9 T: {
- math.pi/6表示弧度,转换成角度为30度8 n( K0 P) ~4 p2 V; W; ^" \
- >>> math.cos(math.pi/6)* i% T' d$ _3 i
- 0.8660254037844387
复制代码 ! w% R; [3 J, C3 T$ b1 F
math.tan(x) 返回x(x为弧度)的正切值; O" m' x, y/ ], A3 x$ ?- Q
- #返回x(x为弧度)的正切值& |# y1 v* r9 t6 y( Q5 m% G5 a
- tan(x)3 o* {; x$ H7 \4 F
- Return the tangent of x (measured in radians).
, G1 u8 ?4 ?4 n- S6 _, f - >>> math.tan(math.pi/4)
' W: x6 f. M+ R: x: @5 S; b - 0.9999999999999999
& C: u/ E& y# x7 Y- n) R( L - >>> math.tan(math.pi/6). l; c' l8 h3 v' U" E" A
- 0.57735026918962571 j' ]2 r: w) o8 }4 C
- >>> math.tan(math.pi/3)
. G- b0 h5 m Z/ K6 g: N/ i8 H - 1.7320508075688767
复制代码
" K( Z! w) a7 t1 d3 D# Zmath.degrees(x) 把x从弧度转换成角度- n: C* F" K9 V1 ?& ^
- #把x从弧度转换成角度( l3 P$ c( d" v/ }9 Y+ ?
- degrees(x)
3 n7 ]( ]/ j8 h# e7 w \6 b p - Convert angle x from radians to degrees.2 k$ Q$ _% O+ l0 r* ]$ a
5 z) E1 A; v" C! v8 s: E6 V- >>> math.degrees(math.pi/4)
& ^7 n+ \2 c% Q# G' N - 45.0
- g5 @( T0 t3 x* s" e, d4 x - >>> math.degrees(math.pi)+ n0 G8 W$ }! U# [% t; M5 D( o
- 180.0
! f; e; i% w/ j& c4 c& V' P - >>> math.degrees(math.pi/6)" b& h: a; A& `
- 29.999999999999996
. l/ V8 ~/ M- n% k1 `4 M, g - >>> math.degrees(math.pi/3), L; H2 x- K9 K
- 59.99999999999999
复制代码
; c$ N3 U; d! R! k: Hmath.radians(x) 把角度x转换成弧度
5 s9 ]& P) F$ _. X- #把角度x转换成弧度
6 C2 l% N9 h0 r; W! R5 d1 I - radians(x)
2 X' A6 A4 O' | G6 @ - Convert angle x from degrees to radians.
2 b3 J& X- J$ P8 c4 b8 S8 Q- L - >>> math.radians(45)
$ w, W' Z2 b2 { - 0.7853981633974483
' ]& ^7 w/ U) ?; g/ ? - >>> math.radians(60)
- O, g% Y# h$ ^% b. }1 H - 1.0471975511965976
复制代码
* Z- D! y& y( Gmath.copysign(x,y) 把y的正负号加到x前面,可以使用0* @0 V8 C1 P$ Q! h+ E2 m- n
- #把y的正负号加到x前面,可以使用0
/ h0 G% _# b; L - copysign(x, y)
1 q) B! v* d( d8 X* Q9 L# Y - Return a float with the magnitude (absolute value) of x but the sign 9 v6 U& ]+ `: R' h' r& W
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) " P# \- e. |1 ^; r2 e( ]$ A" v
- returns -1.0.: U3 H% G: I7 c/ {( |
& S% L* N1 T) j- >>> math.copysign(2,3)7 N+ O0 S, ], {! M }. J B
- 2.0
* Q) a0 \ _7 c+ S- c7 l+ I - >>> math.copysign(2,-3)) W! K3 _& S0 N: A9 _% O
- -2.0, D6 C. v1 Z, n$ @; n
- >>> math.copysign(3,8): U C6 O; w! Y) f3 g: G" v
- 3.0
4 |1 S0 @6 D- k% B6 }( f' P) g( _ - >>> math.copysign(3,-8)/ x% e9 p% b# i" f: t% y- o2 K4 v
- -3.0
复制代码 0 F( `% O& x* B& [
math.exp(x) 返回math.e,也就是2.71828的x次方/ X6 E' Z+ p6 U. I7 V* l+ S
- #返回math.e,也就是2.71828的x次方
8 I0 q3 r, o& U; l; J - exp(x)' b. ?: K% e# D; B! c
- Return e raised to the power of x.
% d. g: I3 c) W8 B( a* s$ S/ }
" L5 u- e- F5 B1 o7 F2 n- >>> math.exp(1)
- |; L7 I0 h7 \5 B0 L6 T - 2.7182818284590457 j0 d0 @) U9 Y/ B
- >>> math.exp(2)
: a3 m9 }7 x1 w( V9 ] - 7.38905609893065
# \: B) u2 M% r5 h# n) K" y - >>> math.exp(3)
9 z5 q$ ^8 m7 c" q$ L# ?2 z! Z - 20.085536923187668
复制代码 : Q; _$ o$ n! D5 C; P% p
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减13 l- Y* j0 l2 t# J1 \- |0 ~% ^
- #返回math.e的x(其值为2.71828)次方的值减1
3 l4 @4 R9 Y; z' Y6 A - expm1(x)
: i# i' c# k' N - Return exp(x)-1.
0 o Y8 u: A; ^ - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
7 j0 E( ?6 b: z: [" O
5 \" g0 s: V1 Y% Y& ]- >>> math.expm1(1)
0 r8 A0 x' H. \6 ]! P - 1.718281828459045. o& [$ `% f- J: M: W
- >>> math.expm1(2)
/ Y0 G, E4 Z- P; p - 6.38905609893065
( z2 ]4 Z) Q/ }& m% W; i1 \: y - >>> math.expm1(3)6 Z1 ~6 P/ r! S5 P( Q* [# s
- 19.085536923187668
复制代码
, u9 k& P% I: s% Lmath.fabs(x) 返回x的绝对值' @% d6 p, _& V7 \$ X
- #返回x的绝对值
/ ?8 }& g( l3 L+ u% @# X$ U - fabs(x)
6 m: k$ n2 R! J3 b - Return the absolute value of the float x.
: m8 ~4 \2 n" C6 n6 S7 e, a& C8 U - ! Z0 T0 o6 }' O6 R. ?
- >>> math.fabs(-0.003)$ h2 Q) O# {' J8 P5 k' Y1 Q2 }
- 0.003* K1 O3 d+ _6 j3 y
- >>> math.fabs(-110)
; J+ R* c I8 k" \1 L* z - 110.0
7 `- x; j; X0 s+ h. ]3 d - >>> math.fabs(100). h% s- b9 X4 N9 O7 {0 R' P
- 100.0
复制代码 ; W* t* H% ~, o, V. F# r( Y
math.factorial(x) 取x的阶乘的值" p! N! a; m, `! \" { {9 }
- #取x的阶乘的值
, f( Y7 W" l( j3 n( H. ~7 V - factorial(x) -> Integral
1 U% r- T d9 {! ?. n% x! t3 R; V - Find x!. Raise a ValueError if x is negative or non-integral.
6 P9 G* q! D: {6 B3 s3 J - >>> math.factorial(1)
, H, \3 k: l: @ - 1
. N+ o7 L$ _1 T' t - >>> math.factorial(2)$ l1 u$ Z7 H' ?* Z) u3 j
- 2
# U4 H. l) \* Q$ a( M% ] - >>> math.factorial(3)9 H% a7 o. ]# M* a. \
- 6# ]+ Z+ M/ K4 [' B5 C
- >>> math.factorial(5)
! U$ o3 p8 p! H' T3 q* n3 P8 f8 a - 120. E7 D; Q7 z# _* @
- >>> math.factorial(10)
, E( X* ?4 _2 W+ h% Z- z3 e( ~ - 3628800
复制代码 , E+ C# f2 Y" }, W
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数4 j9 u: H+ E3 }7 S, s
- #得到x/y的余数,其值是一个浮点数 i0 a7 r! `% M i5 k4 Z/ ~. o
- fmod(x, y)
8 _1 n) S7 j, \ - Return fmod(x, y), according to platform C. x % y may differ.
/ o! {1 s5 z/ |& Q - >>> math.fmod(20,3)* b- ]2 k" h2 \& _
- 2.0
4 ^9 p: l8 G" H0 e) U7 F - >>> math.fmod(20,7)
2 Z, @: w, J: o. C - 6.0
复制代码
f( e( @5 T. i: pmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围* o$ z' L' d5 m8 i* F
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 d) K" T9 Z% w/ ] - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
0 m" _& G0 a- l# l4 l% L* r9 \5 x - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 ^* z# O* c: C5 L
- frexp(x)) P# Y- |) C1 J/ M7 x) R ]( F
- Return the mantissa and exponent of x, as pair (m, e).
. e' v6 F) r" F z - m is a float and e is an int, such that x = m * 2.**e.5 \6 r* n% `5 w- y9 t
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
8 q) o8 ~2 [/ B5 `+ U A - >>> math.frexp(10)
. K; a* Z+ P5 E' R0 e- l - (0.625, 4)
! d: L+ p/ N& ~ - >>> math.frexp(75)5 z4 ?1 K [) o: f+ \/ s: E/ ~( U" {
- (0.5859375, 7)$ B) Q c3 X8 R4 G, P l
- >>> math.frexp(-40)
. c. a( V8 G+ e# U - (-0.625, 6)
3 J# T1 v. P! c$ E( C" d$ ]& s1 k - >>> math.frexp(-100)- b" N& e1 E# e( r. k. s
- (-0.78125, 7)
2 K) }) I% t8 d2 S6 S& P - >>> math.frexp(100)/ ]! Z; v0 L- e1 K6 a8 D
- (0.78125, 7)
复制代码 ( L/ ?% P% l5 w6 {. {
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
) o) A7 Z5 f& q- #对迭代器里的每个元素进行求和操作
: @/ N! E" K+ d$ l) M) E - fsum(iterable)7 g6 z% a7 t |* o6 \ B/ F# r9 I
- Return an accurate floating point sum of values in the iterable.! N& z6 \; T1 V) Q
- Assumes IEEE-754 floating point arithmetic.8 Z% v% ^ h, x( V/ t, E5 C' [
- >>> math.fsum([1,2,3,4])6 `% A3 ~( x4 U- e
- 10.0
: W8 `, H7 v5 T# K" P - >>> math.fsum((1,2,3,4))
( L7 m4 P% Z) h, o8 l- r - 10.0
* h ?' O4 H2 D' l% s8 [: c4 G - >>> math.fsum((-1,-2,-3,-4))
) t$ _- t' L2 @ a - -10.0( ]$ F/ W! I4 L# b2 \8 f# q
- >>> math.fsum([-1,-2,-3,-4])
& V" s( g+ B8 F( ]; @ - -10.0
复制代码
, C6 j9 E) X- x9 zmath.gcd(x,y) 返回x和y的最大公约数
1 U! A4 p, w, \: X x5 O- #返回x和y的最大公约数
4 P7 e; [; j8 Z8 }( W - gcd(x, y) -> int
8 F* t/ y, L2 k - greatest common divisor of x and y
( h7 {9 M' p5 J/ L( {6 e+ T4 x - >>> math.gcd(8,6)
, G2 g/ b! p/ W6 V" ~* O - 2. s; X+ B* {8 v4 v# k) \+ W
- >>> math.gcd(40,20)
* w1 k3 e4 K8 A! o. z3 T; F. f# Q - 20- E4 I- c' {+ M8 U
- >>> math.gcd(8,12)
, n8 [. x$ F5 L( y: }# n - 4
复制代码
L/ ~2 }" l' A3 O+ jmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 p7 n# Q! |2 a+ |6 `, [. @# c8 i- #得到(x**2+y**2),平方的值% K4 J$ F9 [+ S8 k/ D0 C. `5 L
- hypot(x, y)
, d1 l" a9 ~) t& z8 P6 p - Return the Euclidean distance, sqrt(x*x + y*y).
. D/ I: ]; S; s2 Y. x# H - >>> math.hypot(3,4)
9 B1 Q/ Y6 U" n5 \ - 5.0" }0 r7 `8 Q8 Q, P. [! ]( l8 e
- >>> math.hypot(6,8)& c. L2 ?4 F, F$ w4 s4 L: M
- 10.0
复制代码 % t/ q9 ~/ A8 J/ K" B% r+ S2 c5 w
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False$ u# W6 B5 l/ g! c) n5 [; J# P
- #如果x是不是无穷大的数字,则返回True,否则返回False$ A1 V4 k, m/ |* a. F; M
- isfinite(x) -> bool
- h! g* ]( t4 W2 A+ |2 g1 L' ] - Return True if x is neither an infinity nor a NaN, and False otherwise.+ v+ _0 S c' s$ a
- >>> math.isfinite(100)
: q) a& ]/ n6 O s - True
) z; F, F5 Y' n# E! f - >>> math.isfinite(0)
9 v4 j7 v, B9 ^# p2 X/ E8 s - True
7 @/ M! _7 @' _% w' D- }% G+ K - >>> math.isfinite(0.1)) i+ y5 v2 k- t. z2 a( F
- True
; z! N9 Z/ W* ?' O* E - >>> math.isfinite("a")/ ^- ^; a" T( s, D% _: C7 j
- >>> math.isfinite(0.0001)9 V4 n$ O, ?+ N% W/ T
- True
复制代码 4 C2 ~# ?0 ^: j' q4 N
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
a" s3 w; a, a+ d, k- #如果x是正无穷大或负无穷大,则返回True,否则返回False
7 V; u$ G. {' b7 n& ~1 h5 ?" r - isinf(x) -> bool) h4 N2 X0 c& Q( R" @
- Return True if x is a positive or negative infinity, and False otherwise./ N( z R& x/ d
- >>> math.isinf(234)
# n* ]' m% L7 y; c& s - False6 J- f* e# u2 ]/ o1 \/ j* {/ ?
- >>> math.isinf(0.1)' [; h% A* o E9 G
- False
复制代码
( B& n' D) A7 d4 c; Fmath.isnan(x) 如果x不是数字True,否则返回False/ ?: z4 K+ Y! U; r$ K7 z
- #如果x不是数字True,否则返回False* Q4 x# k5 F. T' P: d8 K: Y; H
- isnan(x) -> bool7 W- \5 ~) e- p( v% ], n# f/ d; Z
- Return True if x is a NaN (not a number), and False otherwise.
# S" I/ K) M% T0 ]6 W - >>> math.isnan(23)
* K4 d" v' V, U8 Y - False& |& Y. u3 j* g/ u
- >>> math.isnan(0.01)3 a6 D7 p `! A3 I2 E. Q, J' L
- False
复制代码
6 L& _ B6 Q7 m1 y8 j2 U" Omath.ldexp(x,i) 返回x*(2**i)的值
5 o% v2 T4 a& Z! ^* Q1 c9 r( c- #返回x*(2**i)的值
4 g+ b- _9 D7 \/ I - ldexp(x, i)
& n6 ^ `# v! g" l0 ^- Z - Return x * (2**i).2 Q: p+ s3 d. h* A
- >>> math.ldexp(5,5)
5 h2 |+ P2 ^7 l7 S8 J' U- T& \, w - 160.0 I5 K! ~0 o; ~& S( Z
- >>> math.ldexp(3,5)" O" a$ i, l2 z0 b& @7 C2 s
- 96.0
复制代码 9 |7 _/ x6 S$ e; M+ o
math.log10(x) 返回x的以10为底的对数
n$ O* G0 X$ u# y) n% s- #返回x的以10为底的对数: T) z2 \4 d+ H
- log10(x)0 ^4 r% ~2 E1 f! Z5 I2 O
- Return the base 10 logarithm of x.$ {. w; n% z4 a+ Z0 m
- >>> math.log10(10)
; K: s( J, c! a; `- g - 1.0
! k9 C% E4 ^/ w5 u - >>> math.log10(100)( h+ E4 c$ k3 x
- 2.03 f( ? y3 n) |* l2 `" H9 L) w) z& z
- #即10的1.3次方的结果为202 J. @6 l" d. J* @
- >>> math.log10(20)
' z( c! p S) D5 O - 1.3010299956639813
复制代码 ( o6 T/ M: {# ^! J3 O% ]) ]
math.log1p(x) 返回x+1的自然对数(基数为e)的值7 h7 Z& x. `$ Z( A4 z" {
- #返回x+1的自然对数(基数为e)的值
( D& X! J ~, g5 @7 D3 k# I - log1p(x)7 H6 g8 H9 U( [3 {& O
- Return the natural logarithm of 1+x (base e).
& @( l" A7 d$ ^5 h9 j - The result is computed in a way which is accurate for x near zero.: K* _& ]2 G- q7 M) `3 o9 A. M. w
- >>> math.log(10)
( b0 @# E Z8 P - 2.302585092994046, u p$ I& n2 q* D4 t* C9 L
- >>> math.log1p(10)
5 p- n+ \! m! H" E4 w6 g3 t - 2.3978952727983707( r4 N5 C! G6 m- b# Y
- >>> math.log(11)
) j, D) ^+ g, {% x - 2.3978952727983707
复制代码 8 x* ?+ |: e* b
math.log2(x) 返回x的基2对数0 f2 L D$ Y) ~) R# A/ u
- #返回x的基2对数' n3 @$ `9 q* v' V1 i0 c& z) v8 |
- log2(x)9 N9 m" t( G# h8 m
- Return the base 2 logarithm of x.4 c; p- P4 f. |5 j# O- t3 u
- >>> math.log2(32)
9 R2 g0 h! Z9 O0 J8 t& N9 B9 ] - 5.0
" _, I7 f4 N: t* E' G+ H - >>> math.log2(20)! l, g9 U6 m: R3 Q
- 4.321928094887363
}& @+ o8 p- R. @+ A - >>> math.log2(16). _5 C" [3 H# [0 G
- 4.0
复制代码
3 r P, J% U0 ~4 t' {9 R1 ymath.modf(x) 返回由x的小数部分和整数部分组成的元组9 O& N, t1 j8 `: U
- #返回由x的小数部分和整数部分组成的元组6 u$ c5 _" G- S3 _
- modf(x)/ }, q- x- E$ Y. x
- Return the fractional and integer parts of x. Both results carry the sign% w) C0 u7 `* C
- of x and are floats.
# U. _7 h; ?$ G- _9 x' L# e0 L! l - >>> math.modf(math.pi)
: J7 C4 N$ @/ S9 w; l& Z - (0.14159265358979312, 3.0)) ?* @4 a# q! q! E! p J
- >>> math.modf(12.34)
* s7 d( N+ }% A/ E1 M- ~ - (0.33999999999999986, 12.0)
复制代码
, _) T% M: g7 |! [- N# \math.sqrt(x) 求x的平方根( Z! I9 r: s4 C, J9 h
- #求x的平方根
+ y. b8 A; }7 `- Y0 d - sqrt(x)0 D! J2 u. k' m9 y8 [3 h3 n
- Return the square root of x.& S( G% a. t! |7 q/ E' Y
- >>> math.sqrt(100) I8 L2 ?: c2 T3 \
- 10.0- z, |/ K c5 S# Z1 N: z
- >>> math.sqrt(16)
. `" M2 n# y; ]0 Q - 4.0& k$ i9 F) ?+ c. i
- >>> math.sqrt(20)
$ Z1 y5 U- ]* {; Q9 C - 4.47213595499958
复制代码
) Q9 N3 W V& I L) `- Omath.trunc(x) 返回x的整数部分- #返回x的整数部分
. m; n( T# }7 A" s( D+ ` - trunc(x:Real) -> Integral! K/ g4 \9 f8 K. w
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* {! `6 u. O! f! U
- >>> math.trunc(6.789)
7 n; M6 h6 Q5 x9 l - 6
& y* t' f4 h) h1 Q1 w# P - >>> math.trunc(math.pi): I4 y+ b4 k* u! `: t8 L3 K
- 3' ]! f; m# P+ T( Y" X, t
- >>> math.trunc(2.567)
! ?- j$ H- L4 l3 k6 Q3 E4 _8 r9 y' F - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|