马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 W5 Y F% @$ B: t5 |# v# v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 r1 \8 p+ }! C( I0 T8 o9 ]
4 l V' j% u: U. Y5 |方法1:( ]& }6 x0 F9 a0 y: {8 L' n& x
- >>> import math
& t$ v, y% P3 i& ~( c' ~8 {* W) N - >>> math.sqrt(9)
! m0 J! V4 X) e, l# G3 E( o - 3.0
复制代码 方法2:
* M6 u) _# y9 X8 I5 V- >>> from math import sqrt
' W: O+ w( U6 W7 c1 k* \6 i - >>> sqrt(9)
8 C; P1 p$ }4 h F1 l5 N5 j - 3.0
复制代码
" W n+ C* X# U: J
. q) l( ?' V8 G7 cmath.e 表示一个常量+ ~! {# h5 _# P
- #表示一个常量
' y+ {6 N- \( {6 L0 R! [9 ~0 Q - >>> math.e ]# ?6 K3 t& F! O, ^5 C
- 2.718281828459045
复制代码 6 B- c4 ]8 W, L1 m' U: I
math.pi 数字常量,圆周率* B9 w l3 o# v! y# F5 ?9 b
- #数字常量,圆周率8 z) R. H0 ^4 g. x
- >>> print(math.pi)
9 ]7 S/ f- [) v1 ]4 F - 3.141592653589793
复制代码 / y" j/ f% f, p) e) ~
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x6 K! b: U2 j9 C( o6 |& t5 x( a5 v
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ p8 \3 m; [4 U1 j$ B2 c) Z. p - ceil(x)- z. A& W% h. q; f' K/ d J
- Return the ceiling of x as an int.
; p- \, |: p& A$ I) i - This is the smallest integral value >= x.+ N9 }. [3 W+ C6 ~ O
- 2 N6 ?9 R$ z, _! F
- >>> math.ceil(4.01)
2 d9 `. |$ J; G, E$ t! m4 m( O - 5
# S) Y2 q. k8 M, X% K9 S2 k# ~' |$ w - >>> math.ceil(4.99)' b# Z j, H. a: c6 p
- 5' Y- a% @9 K6 @( j3 R: p- W
- >>> math.ceil(-3.99)
7 x+ z* o8 ~" t5 Y0 C - -33 F7 P) V% ]! O
- >>> math.ceil(-3.01)% T/ a, h1 n. m$ {7 \4 Z
- -3
复制代码 8 L& b/ Z% Q2 W$ e0 ?' o- H
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: ]& V* Y2 L' v6 b( P5 J- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- C; a& h. S+ {. W
- floor(x)
- }' ^; s& u( g6 e2 U! K - Return the floor of x as an int.8 N; u$ c0 @/ Y& G& U
- This is the largest integral value <= x.0 E: [ o9 ]" v o
- >>> math.floor(4.1)' z* _2 ~; g; e/ ]4 t- }
- 44 f5 r' k: Z! X, M4 N0 G
- >>> math.floor(4.999)
6 t% I' q- d) D' o - 4( f6 B4 |5 r1 c; S3 K& R9 m# b& L/ `
- >>> math.floor(-4.999)
% T7 V7 c' M+ u t0 } - -5
# z" a! `% R7 G/ V" i' {7 t - >>> math.floor(-4.01)
% |$ a1 ]/ L; |! @/ B - -5
复制代码
5 u! [1 w3 |0 }0 Z7 d# amath.pow(x,y) 返回x的y次方,即x**y
; q- ~# p! X' p" q7 M0 }- #返回x的y次方,即x**y# W I* p( z" g+ h0 |
- pow(x, y)
( J, k2 Q+ b6 _# W4 B - Return x**y (x to the power of y).
; d% \$ l& Q! ?, T( j9 i5 M - >>> math.pow(3,4)
- d! H; u! c; N4 E- s# Y - 81.0* |' Z+ K- n# X& z$ [5 o
- >>> 8 `0 j9 L: N. A5 A/ @4 y2 r
- >>> math.pow(2,7)
C( m8 X8 j9 @& I- f) P: { - 128.0
复制代码
0 |: c# v4 b; G5 z7 Lmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, Y! e9 q$ a& ]: E5 H6 v' N+ k- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 U" O, G! a5 `! l& x# K
- log(x[, base])
( P, Y& a4 L, L - Return the logarithm of x to the given base.% f7 [# a6 n. `9 p9 f
- If the base not specified, returns the natural logarithm (base e) of x.% O4 h$ ^/ ~' X; P
- >>> math.log(10)
0 ?+ q6 u4 h( b7 c - 2.302585092994046# ?+ f6 ?: S7 M& \0 `" D6 C7 ^. L
- >>> math.log(11)
) G2 m4 V$ Y6 D; N) a- j - 2.39789527279837075 s* a0 Y4 h1 h
- >>> math.log(20)
" H" d6 f! |1 C7 x - 2.995732273553991
复制代码
/ v$ h! X8 G$ C/ Wmath.sin(x) 求x(x为弧度)的正弦值8 m/ U2 n7 Z+ P! l! q8 ]
- #求x(x为弧度)的正弦值& _$ p F* k! G4 ?
- sin(x) G: k* [0 I8 @' y: v
- Return the sine of x (measured in radians).
: M- R) q. l/ h; J4 N - >>> math.sin(math.pi/4); I) H" \ ~& j& W- P
- 0.7071067811865475
/ o& v. O0 G4 c' L% ? - >>> math.sin(math.pi/2)
! [4 \5 C& n; X - 1.0" m/ h; C0 V! f; _" a* k
- >>> math.sin(math.pi/3)
$ \1 z9 h% e2 U' G3 n0 `' F - 0.8660254037844386
复制代码
' y* G$ W; {4 |- {3 ?2 amath.cos(x) 求x的余弦,x必须是弧度
% e# E8 ?. E2 \: K( O! {- #求x的余弦,x必须是弧度1 W; R5 [6 ~5 c" {, D
- cos(x)
' H9 R3 r% \& t% }- z - Return the cosine of x (measured in radians).% ]8 n" u, d& A3 ~( _) ]
- #math.pi/4表示弧度,转换成角度为45度
8 L; s0 }! n2 I& u6 [7 c2 _6 a; Z' o - >>> math.cos(math.pi/4)+ D5 _9 t& \6 B* \! _
- 0.7071067811865476. d: d$ ?3 h n2 P
- math.pi/3表示弧度,转换成角度为60度
- r! y6 d( Y! S3 \ - >>> math.cos(math.pi/3)3 j/ K' |8 c, T0 g0 A G3 y
- 0.5000000000000001
6 D \) C; U" F1 \ - math.pi/6表示弧度,转换成角度为30度
* b3 h/ o* g A, X - >>> math.cos(math.pi/6)9 o! [6 _& l" Q: z3 z
- 0.8660254037844387
复制代码 N: b3 h$ d3 e# }! v: c4 ^" K( l
math.tan(x) 返回x(x为弧度)的正切值 r. H% g! o0 P; U' { Q$ _1 J" {
- #返回x(x为弧度)的正切值& M- S2 h4 _; g$ b
- tan(x)
9 p+ X |. Z/ D$ ~; s! I - Return the tangent of x (measured in radians).
" o/ `) ^3 s8 M' u - >>> math.tan(math.pi/4)
$ ~. r; [& ]( l+ ^/ g" q9 l* _3 T - 0.9999999999999999% ^( ^4 y6 S- B
- >>> math.tan(math.pi/6)9 M0 ?8 n+ u* Y/ |% @1 j
- 0.5773502691896257, a ~' R, G" }9 v: L* ]! k
- >>> math.tan(math.pi/3)& R4 l$ A( f3 v8 w- ~- t3 T
- 1.7320508075688767
复制代码
) I7 A/ d1 Y! Y0 _$ R6 p) ]. ymath.degrees(x) 把x从弧度转换成角度
# j* `/ V6 B% r3 t1 }0 _- s- #把x从弧度转换成角度/ @: ?0 Q' ]( u8 m
- degrees(x)
$ [% l. `& y, [ - Convert angle x from radians to degrees.3 S1 B' z9 ?% O$ o9 ~* h
- ( D( b0 {2 l7 M+ G8 \$ H; ?) V
- >>> math.degrees(math.pi/4)
* S4 ]0 P- {: d - 45.0
! p5 h. [: \& k. W8 \ - >>> math.degrees(math.pi)* \/ `* I9 ^/ L% D
- 180.0
5 N7 L8 F2 g: u1 Q0 ` - >>> math.degrees(math.pi/6)2 |# J+ o4 O W7 [: k8 {/ L
- 29.9999999999999968 D( L. H) D' @' P! O9 R0 n
- >>> math.degrees(math.pi/3)1 e0 K- D. a$ \
- 59.99999999999999
复制代码 . o* |3 ~$ V" u
math.radians(x) 把角度x转换成弧度; {' M6 f E# u3 F# G/ d5 {7 K! i
- #把角度x转换成弧度5 t+ {. Y p! Z6 f4 K
- radians(x), p/ e- |, ?/ M: W/ v5 a
- Convert angle x from degrees to radians.
( @' h( w! \2 `5 ^; r% V - >>> math.radians(45)
+ s( P) ?& X, [ V$ l - 0.7853981633974483
3 X! j s; u0 @& O - >>> math.radians(60)
- E" y+ C- U) S! b - 1.0471975511965976
复制代码 h) Z7 ~& {% m( O( v- V' }; Y
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 T/ H( x5 @* k i T. F- #把y的正负号加到x前面,可以使用0$ Z# s+ n- {8 p3 z. D
- copysign(x, y)
; Q( C6 B- O5 ? - Return a float with the magnitude (absolute value) of x but the sign
+ v' A$ y; S# u* c$ j - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) u5 a. N( c$ q& ~0 n
- returns -1.0.
/ {4 [! n/ w% b3 T+ k2 H5 X
y- T( _# y0 c& y @- >>> math.copysign(2,3)
( ?* h; [: e8 u. f& Y0 b' i0 N5 ]- I - 2.0
2 G* @ u3 o2 p! E( A+ w5 g - >>> math.copysign(2,-3)
5 b+ r- e$ E. i: ?! w - -2.0
( b! H0 y% p3 z' G4 a7 G6 z2 J - >>> math.copysign(3,8)0 T8 k5 h1 I" z+ b& h( G w
- 3.02 A; l0 [8 ~$ A5 ], K! ^* Q; a7 l
- >>> math.copysign(3,-8)- u/ [# S* y0 D/ o
- -3.0
复制代码
# a+ V" L4 C* \: h' S1 Emath.exp(x) 返回math.e,也就是2.71828的x次方; ^5 ~" q5 J( y- s \2 l. |- t6 w
- #返回math.e,也就是2.71828的x次方4 ]5 u) \4 O& _9 z% n
- exp(x)
% R; ?' Y& u1 C1 f5 h: U8 I6 m. J/ N - Return e raised to the power of x.
/ }7 z. Z% u+ O
4 ]' S' q% t- C- E5 N2 u& e; Z. B7 ^- >>> math.exp(1)6 B& G& I. h' K) W4 v
- 2.718281828459045, S9 N# O+ ~/ {1 M5 G
- >>> math.exp(2)
1 a7 E. ^- {0 H6 X0 @, K1 S* k# J - 7.38905609893065
0 F" u5 \0 U% d - >>> math.exp(3)1 ^9 ]- K& O5 {2 x" {
- 20.085536923187668
复制代码
" C ]1 I2 o4 W( J' r) Amath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
' Q6 K1 s5 V0 o8 P- #返回math.e的x(其值为2.71828)次方的值减19 S' ^* ~8 X) E. S6 e5 _& ^
- expm1(x)
/ X4 j1 i, m# Y- L: ]% j - Return exp(x)-1.* w. c/ k; t3 l) x7 L
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
4 I5 r7 x/ k& `+ h8 Z& x J0 A - ) `5 \ T# q; w8 A' Q+ i7 c8 D# O
- >>> math.expm1(1)% R; G/ o8 I4 T: p8 s( R; {
- 1.718281828459045" R& \! l6 I7 b$ b6 M
- >>> math.expm1(2)
& g. f7 V: E* f# x# C - 6.38905609893065
4 Z s& Y: U" g- X' }7 g - >>> math.expm1(3)& n+ I5 j. L! c* J) p* t/ A
- 19.085536923187668
复制代码 0 |* {6 b! H2 a, c8 B% R# U6 L
math.fabs(x) 返回x的绝对值% a9 H" q5 ]7 Q$ m3 S5 L
- #返回x的绝对值
. Z: S. F$ r! _# g0 a& q+ P - fabs(x)
: n* d& _% I1 e0 W - Return the absolute value of the float x.
3 @0 Q2 W3 b2 B5 H2 K
3 i5 f" |4 J/ p3 O. p1 q# H- x [9 @- >>> math.fabs(-0.003)
3 v4 f0 _" |% x9 N& g - 0.0034 X7 M* V/ ~0 { m8 y
- >>> math.fabs(-110)/ T e, v( r8 H( P( `8 K" [; Y- m
- 110.0% O" l7 E, C3 V: A3 y' z
- >>> math.fabs(100)
6 s5 l9 x B7 x) H' ? - 100.0
复制代码 % K# u- M- \& [: T7 n3 z# G
math.factorial(x) 取x的阶乘的值
6 Y3 @3 F3 A0 m: I) w. v- #取x的阶乘的值
W6 C' {4 y, _+ @) U" O+ h8 v! q - factorial(x) -> Integral% S2 U$ H2 f6 u0 W# g5 S9 |" \ l
- Find x!. Raise a ValueError if x is negative or non-integral.
1 E: x. F% a6 c8 o - >>> math.factorial(1)2 K& ^7 _, G+ S: E# B ~
- 1) t/ u! \& t" m2 M& O
- >>> math.factorial(2)
6 v8 j% G# @$ @; ]5 P+ H - 2* ]) w; _) W. k5 @7 f; s
- >>> math.factorial(3)8 H* E* J. J6 |+ @
- 6
) |' _3 V- e% ~( `0 f0 c - >>> math.factorial(5)% h/ ~7 T D4 @7 a5 I0 H
- 120( @! i1 |/ V- v) n1 Z4 |
- >>> math.factorial(10)3 q( U7 \* r) l
- 3628800
复制代码
; `4 d |) s" s' b" `* ~: ]' i. Xmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数) }$ C1 f% U3 V3 \$ ~
- #得到x/y的余数,其值是一个浮点数
9 M) I5 Z( D( u* h- d5 \) s - fmod(x, y)
* F# `8 J K. O( Z2 d - Return fmod(x, y), according to platform C. x % y may differ.
1 _0 d2 c* U( J8 z - >>> math.fmod(20,3)
+ k- }% v i- W0 i - 2.0, v1 x3 i9 ` I1 ~7 Z; m, x
- >>> math.fmod(20,7)7 e& w1 m6 V6 F8 [
- 6.0
复制代码
# h: z* C* |, d/ r% J) f6 p, Umath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 _4 F: T! z0 v/ z% s' y* n5 K- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
2 }$ F6 m! ^# H7 v% S" Y - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 W5 z/ s3 W) z1 m
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1* S" F4 V. x. S* r) x7 y* y
- frexp(x)- Z% p, l3 R) V# m
- Return the mantissa and exponent of x, as pair (m, e).
4 y) c; J. d6 D: L4 [ E7 _3 h: Y - m is a float and e is an int, such that x = m * 2.**e.
$ y' w; t1 G' i, ~ u- q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0. C+ J6 ~7 }' D* ?+ s" Y
- >>> math.frexp(10)
" y+ ^" O: b; [- u4 G+ {6 J - (0.625, 4) [4 U% z+ w0 ]0 ]# J6 R5 O @
- >>> math.frexp(75)
! i% t( M9 K4 \8 M5 r# y( J2 k - (0.5859375, 7)$ U2 d! Z- \2 ~ Z* j$ F; O
- >>> math.frexp(-40)7 a. j. e0 r, ^/ y; @. C5 n
- (-0.625, 6)' e" y; s' l3 w4 @ `
- >>> math.frexp(-100)% j/ c# f0 P1 V
- (-0.78125, 7)
9 u: z" ~7 G2 O - >>> math.frexp(100)7 X" s3 T' I3 X4 k9 v
- (0.78125, 7)
复制代码 $ q! a/ m- g1 p1 M% O, F3 y( q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)9 ?$ e1 D- f- V7 F j1 L; U5 c# L7 ?
- #对迭代器里的每个元素进行求和操作: `: |! X$ u( U% h; F% D
- fsum(iterable)
U ]. @% f8 ]/ ~2 A% W - Return an accurate floating point sum of values in the iterable.! m. h. a3 q# {+ L7 r7 Y
- Assumes IEEE-754 floating point arithmetic.8 j. D9 Q9 V# H$ t1 F
- >>> math.fsum([1,2,3,4])
$ o3 m2 _1 n+ q; z$ m - 10.09 ~' s" s+ Q' l6 T) ~+ K$ |/ m) q
- >>> math.fsum((1,2,3,4))6 R0 Q6 `: x$ ~9 U( p' ~
- 10.0
4 A* g, r1 M6 y4 j) f, C% o) M+ l - >>> math.fsum((-1,-2,-3,-4))$ `! L3 W7 N: r7 Z, ~
- -10.0' R" d2 }9 g; P/ @: q, d
- >>> math.fsum([-1,-2,-3,-4])3 I9 p# V$ [8 k: _5 D* A+ z o
- -10.0
复制代码 - l& b& S/ ~4 |! K% R; Y9 }
math.gcd(x,y) 返回x和y的最大公约数* C l! @$ ~; g {
- #返回x和y的最大公约数
6 P" l' r# _% v0 O9 o - gcd(x, y) -> int
! G, ^0 m$ H6 E V7 I( H* c7 I - greatest common divisor of x and y
* _$ c. t3 k4 d- N- J - >>> math.gcd(8,6)- r8 l. S/ h5 n' F8 x6 q) B
- 26 z E8 r7 v0 d2 ]: T
- >>> math.gcd(40,20)
$ Z+ P8 g6 D& Q" e' d - 20
; S- {- H) l2 Z B# {. I/ ] - >>> math.gcd(8,12)
/ h, E& y. H# X - 4
复制代码
$ K5 L |& H3 H9 xmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 [7 g2 m' J! ^5 k* t- #得到(x**2+y**2),平方的值9 u4 r! E; J, I9 {
- hypot(x, y)
: |' h9 v+ q/ G/ \ - Return the Euclidean distance, sqrt(x*x + y*y).* M" n6 E/ u% f/ ?7 l
- >>> math.hypot(3,4)
# B# T" J5 u3 Y! A8 ?5 { - 5.0
" L0 U% C. ~9 a! V% i6 t - >>> math.hypot(6,8)
: t: S0 L5 b1 u - 10.0
复制代码 : S8 Q% b! M7 n4 z) l, j- F
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 u) m2 u% X: { W, k' M1 X6 q- #如果x是不是无穷大的数字,则返回True,否则返回False
5 a- ]& U4 i; l0 i - isfinite(x) -> bool
- O' l2 v0 ^, @5 R6 A - Return True if x is neither an infinity nor a NaN, and False otherwise.
1 x' b: S/ F8 d - >>> math.isfinite(100)6 L F, r" r/ O
- True7 _- w# k- M) C, T6 o/ X
- >>> math.isfinite(0)9 v9 B; ?) J' D) A2 P
- True
# ~, U, _! m) B( b2 E4 M9 C$ ~ - >>> math.isfinite(0.1)
) Z# ~: j! K2 J# t - True. q( g" R% ~" E; N p J
- >>> math.isfinite("a"). ^4 b! k1 B3 V6 [: G/ a% S
- >>> math.isfinite(0.0001)
) y. B( i; }1 ] - True
复制代码 9 V9 _, s2 Z4 L( `# X8 V8 m) q
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, M! C! l9 B( L W" G: E. ?
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
& D7 Z/ i1 b m9 z4 n! a4 E- ]" N - isinf(x) -> bool# x7 ?5 G- `! Q. n! P* v
- Return True if x is a positive or negative infinity, and False otherwise.
0 ~: p' H* \# z9 v - >>> math.isinf(234)- ~ V1 u( x# \) c; {8 n/ R
- False
' Y& A5 Q& d, H4 | - >>> math.isinf(0.1)5 w) F. g' |& d+ N' h
- False
复制代码 , X8 P( u" u5 `+ C* p/ C. i
math.isnan(x) 如果x不是数字True,否则返回False
7 l& _- B/ k v( w* _- #如果x不是数字True,否则返回False& u; p2 C1 A6 d) h
- isnan(x) -> bool, ]- [ i+ k6 I# y9 {7 ?
- Return True if x is a NaN (not a number), and False otherwise.
, g+ e3 k+ Y0 Z+ j8 g# } - >>> math.isnan(23)
( T' D4 w+ @& @3 C$ b6 @4 O - False
' H9 ~9 q9 Z! P ~( ` - >>> math.isnan(0.01)
$ e4 v) k: a7 A4 c - False
复制代码 " m1 b& D1 f3 l& p
math.ldexp(x,i) 返回x*(2**i)的值9 d( `, `% Y3 o& _5 B
- #返回x*(2**i)的值
( @; ]6 _( ?7 D' W - ldexp(x, i)
) [; j9 p% ~2 J9 @" w' }: d - Return x * (2**i).
: a( _& I6 i% k# p; i - >>> math.ldexp(5,5)7 P2 k6 f2 V; L. H3 _/ z* v
- 160.0
* t" x; G/ j! C8 g H2 B - >>> math.ldexp(3,5)
% M/ l( Z, s5 }. f - 96.0
复制代码 t4 m' J3 ]" M
math.log10(x) 返回x的以10为底的对数
~' R3 k, A8 Y2 w# `! a5 l: d9 N$ @- #返回x的以10为底的对数
D9 i& Q* ]; R+ \5 P+ O' u5 d, s - log10(x)
9 Z$ y9 `) \! ^- O - Return the base 10 logarithm of x.* u" F7 [* O( _2 _$ h2 r4 s
- >>> math.log10(10)
9 h0 W2 N6 L! M4 d5 z C+ d - 1.0, a! V" r: ~. S& T' ~4 m
- >>> math.log10(100)8 m' A% q" `, u4 z3 @% z
- 2.0; M& v+ s2 I) W* I G0 D) J. x' c* b
- #即10的1.3次方的结果为204 Z0 j* g6 I2 m- ^. r; P
- >>> math.log10(20)
+ C' T2 ~# c P7 Z- m - 1.3010299956639813
复制代码
" y& r# E( v! B" m+ i2 I1 s* rmath.log1p(x) 返回x+1的自然对数(基数为e)的值
9 O; T7 P, V" J) j0 h+ T5 z- #返回x+1的自然对数(基数为e)的值6 g& a; D& i# d9 H0 C; B6 `* A
- log1p(x)
6 \; t. e3 \: s- b# ^ - Return the natural logarithm of 1+x (base e).* J, _0 |! S: K# W8 i R
- The result is computed in a way which is accurate for x near zero.
3 }" |( P; k% ]; _5 z6 s - >>> math.log(10)* }# O9 @$ _9 j2 c9 \* G+ X
- 2.302585092994046
+ v; R) Q! g4 [6 y - >>> math.log1p(10)# Y; f5 D' Y5 R6 A. e/ o. I
- 2.3978952727983707
1 D' p. [$ a @6 p( d* A4 b { - >>> math.log(11); m" q* F& A7 F2 R. C/ p. @2 H$ J
- 2.3978952727983707
复制代码 8 V6 L, O% G! T
math.log2(x) 返回x的基2对数
/ [7 G: n# C' Z: k- #返回x的基2对数0 E8 m2 D6 V4 z" U4 B0 |
- log2(x)) K4 B. O, W6 U
- Return the base 2 logarithm of x.; ]& S( N4 a R( T
- >>> math.log2(32)
8 Z) Q* a; g$ S) T - 5.0
9 @% Q( N# x! I% d9 |5 o8 X - >>> math.log2(20)
0 G9 ~/ j4 W0 Q; @$ X; g" c - 4.321928094887363
2 O6 \- i( d/ U: ]8 d5 e - >>> math.log2(16); |9 A) A3 [: |3 p! @; g8 y2 N/ Q
- 4.0
复制代码 / b! M/ a$ y- H) s2 K
math.modf(x) 返回由x的小数部分和整数部分组成的元组
( x8 o9 \" I6 Q' p8 T- U. j* b- #返回由x的小数部分和整数部分组成的元组6 t& ~% b# M: _5 `: Y4 f+ B7 k% Y6 l
- modf(x)
2 ?- Q9 H/ ~ e8 q$ v - Return the fractional and integer parts of x. Both results carry the sign. o. \+ D- H! z+ W5 a* l T: C
- of x and are floats.
1 m& E( q: a( O8 X8 }, S; C+ z) T - >>> math.modf(math.pi)4 V0 u- j' j; G
- (0.14159265358979312, 3.0)- d5 T& E4 E0 f
- >>> math.modf(12.34)
; y0 f# ~) k: r4 D' t - (0.33999999999999986, 12.0)
复制代码
+ `3 e t/ \7 P, n: Wmath.sqrt(x) 求x的平方根
1 b" j3 ?; u1 O0 J r( e7 I- #求x的平方根5 f, p1 M9 h! p& a2 B
- sqrt(x)) f3 x) x1 `% r8 M3 |# A& j
- Return the square root of x.6 n: h2 V: w1 f
- >>> math.sqrt(100)! @0 ]% M$ j: J2 E3 Q; \
- 10.0
* L5 `8 i/ p I0 _7 u) G. @) | - >>> math.sqrt(16)8 O6 S% @7 |5 E' G; X: a' `/ y
- 4.0
- q, M8 y4 R+ c2 N: \ - >>> math.sqrt(20)
# b. ^& Z0 B1 O6 g! W5 J - 4.47213595499958
复制代码 / ]( J% c, D, ^ f
math.trunc(x) 返回x的整数部分- #返回x的整数部分
- h1 ^! C0 c7 d - trunc(x:Real) -> Integral+ x6 D3 c! h* a, w) e
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# X3 B, l: V) [0 o
- >>> math.trunc(6.789)
# W1 j$ M- i2 X9 N - 6
/ P1 B; [, j7 G' \. s# `! m$ i8 Y - >>> math.trunc(math.pi)/ N* q7 ]" ~/ y
- 31 g+ r0 J: A' |: {
- >>> math.trunc(2.567)
( ?- k2 t, c) S+ }5 f - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|