马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 @. F) g6 {7 Y3 _3 t% F
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
l* q# R( |! L5 J3 o$ d& H- {
* p& Y) d5 o$ L' ^1 i4 Q% t方法1:
9 R% o4 k! i* d, V5 E2 b; P- >>> import math
4 g7 Z2 e# e. P0 N - >>> math.sqrt(9)
( P4 p- k1 M2 Q u: G; \7 s - 3.0
复制代码 方法2:3 Q5 T- f( j7 b! n% p) h
- >>> from math import sqrt) A* Q* x/ J7 G
- >>> sqrt(9)
3 v) E5 ?& V' s - 3.0
复制代码
: T/ j; x. p( r5 q7 r+ Q7 } ; d; K" p) K0 U) T; n
math.e 表示一个常量
% \* @+ i, K1 e: [- a* W! m- #表示一个常量# H) e. K1 D/ y5 K G; \
- >>> math.e
9 z. X7 J3 q4 p: e3 O+ g7 U8 _ - 2.718281828459045
复制代码
/ ], q& V: H) F" S4 V, J5 dmath.pi 数字常量,圆周率
; H4 r; o0 Y3 n, U4 X9 _- #数字常量,圆周率- w4 v( E/ k, ]8 i
- >>> print(math.pi)
/ w4 A8 C; X, y - 3.141592653589793
复制代码 * M# o" h) U3 O# E4 G% \# |
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x0 U5 N3 Y4 A1 W6 R0 A9 `
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 n# I: S6 j7 }) ^# l$ t" `8 Q& w - ceil(x)$ G, C& c' j1 u t
- Return the ceiling of x as an int.
, U* p& |8 h% P* |+ V X; k9 \ - This is the smallest integral value >= x.: N" c0 ~' F4 W [$ I; A
2 C) X+ h6 r6 U) ?- >>> math.ceil(4.01)$ @: o- P) U3 W# M
- 5& T" E8 M0 m) x
- >>> math.ceil(4.99)
5 }9 u$ ]6 R( S - 54 }$ M# E; d; p) ]; G+ q- c
- >>> math.ceil(-3.99), _# b) g6 J$ L% N5 T9 q1 Z% R
- -3
' h' }5 Q" D9 j. Z - >>> math.ceil(-3.01)0 M4 E% q6 J% }& Z
- -3
复制代码
0 s0 Z$ F1 v! P+ `; Z" amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身! n8 ~& E* }- k/ C2 A
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身; G" \; l! P1 q+ S3 O' `: i
- floor(x)
+ I) \6 y: ]( l - Return the floor of x as an int.8 j4 V) D- e# i' x9 r( J
- This is the largest integral value <= x.6 x9 }5 ]/ J4 [
- >>> math.floor(4.1)' a. c' f6 I, D5 _
- 44 |. |* a, U+ G! d
- >>> math.floor(4.999)0 i9 |! ?0 N( V- J9 l. i; T& j
- 4# M& ~5 q$ C2 A# {' ^
- >>> math.floor(-4.999)
# k1 c) O6 v! q - -5: j7 @' k/ Y) N
- >>> math.floor(-4.01)
8 G4 _7 O2 v# Q6 ` - -5
复制代码 / R" \3 e# w! O5 Q: ~2 j/ W8 c; X* @
math.pow(x,y) 返回x的y次方,即x**y2 M- A Q& ?! Q. S' L7 a
- #返回x的y次方,即x**y
- f$ j! {6 M' u Y4 w5 o+ _5 O( F2 \ - pow(x, y)
3 f. w7 R6 w% }% i/ Z; n! } - Return x**y (x to the power of y).
* U' A5 B+ k" A. {5 Q4 r - >>> math.pow(3,4)- J8 I5 T% f- K8 i
- 81.0
. q- n7 @; K' _) A9 N - >>>
, f8 F2 Y2 T! g/ n) I - >>> math.pow(2,7)# S1 z& l9 S: h( j8 Y; n q
- 128.0
复制代码
7 t0 ]4 O6 d% P+ s1 m! e; g; Amath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 n8 r7 k0 J5 }' A9 U/ d% `- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 r" k! ~1 D- y$ |4 j
- log(x[, base])
. _/ ? p' r0 o* J. K# T - Return the logarithm of x to the given base.
. Y' G, ~# y+ ~5 X% B6 ~ - If the base not specified, returns the natural logarithm (base e) of x.
0 F* f) b5 _$ p - >>> math.log(10)0 s; R6 X, F1 O) \3 \8 L
- 2.302585092994046
& T% Y" V& L8 c% y! s - >>> math.log(11)8 |! k+ T% s" z& b! n8 n: \
- 2.3978952727983707
$ [7 d m" M Q3 ]( R m$ F - >>> math.log(20)5 s* b. I( v+ ]' U, r3 k
- 2.995732273553991
复制代码
: a9 h$ T/ E9 f. c4 k$ l8 I8 Kmath.sin(x) 求x(x为弧度)的正弦值
! K* o3 k# ~; D5 w- #求x(x为弧度)的正弦值
! \! T/ U$ u7 A6 [# }' D; V/ W - sin(x)# L0 c. C& w( z1 }& J: F
- Return the sine of x (measured in radians).# N( y9 L2 R5 @( U5 U) U5 o
- >>> math.sin(math.pi/4)/ F1 q0 `' j( u$ X
- 0.7071067811865475$ k9 ?6 y1 L3 }1 G, z6 {6 R7 u
- >>> math.sin(math.pi/2)8 ?# f4 @) q' t0 I( B3 c
- 1.0' ^3 q4 M6 m) a" I! b3 r
- >>> math.sin(math.pi/3)1 k. c6 `( p8 _4 A+ h- T2 |9 @
- 0.8660254037844386
复制代码
9 L9 u! K% `+ K7 omath.cos(x) 求x的余弦,x必须是弧度
) B4 G1 j6 W- a: b4 P- #求x的余弦,x必须是弧度+ g9 x, N" V# R7 m, j. s
- cos(x)- Z" c7 X0 [0 V4 a; f1 E# H8 t ~) x- j
- Return the cosine of x (measured in radians).
( O& s' E2 L6 p5 _! } - #math.pi/4表示弧度,转换成角度为45度1 x) V% @1 W: d( R% }
- >>> math.cos(math.pi/4)
8 [6 Q+ D* _' l* W. ^ - 0.7071067811865476
- I$ Y% C3 x9 v" [ - math.pi/3表示弧度,转换成角度为60度) w h4 d+ \. ?
- >>> math.cos(math.pi/3)9 B7 _ l; Q: }' J' Y0 t
- 0.5000000000000001
# e# o, E2 u& O - math.pi/6表示弧度,转换成角度为30度
' R2 Z% s( H0 s/ t# p - >>> math.cos(math.pi/6): m5 X! O7 L! l6 P2 h# u6 J0 M6 x
- 0.8660254037844387
复制代码
& u/ b6 a9 \. o% a5 _math.tan(x) 返回x(x为弧度)的正切值; e$ S& B5 c5 A) F: y5 H
- #返回x(x为弧度)的正切值+ W3 t9 d) P5 y; ~: M
- tan(x)- w4 a, c2 O2 _ S2 K |
- Return the tangent of x (measured in radians)./ j) r: O* i# L/ X" c
- >>> math.tan(math.pi/4)/ Q3 D" n6 U: G: L9 x. H' A" b
- 0.9999999999999999
3 J# H% J! @. a: {2 |6 ~- o - >>> math.tan(math.pi/6)+ t6 Y( E+ a5 M! p
- 0.5773502691896257
4 J& p) k; Z K' ]% x7 d3 d - >>> math.tan(math.pi/3)2 Z+ n, `* {/ S& C' V& G
- 1.7320508075688767
复制代码
% A: y1 F2 G. A6 [! b3 e# s$ a0 Emath.degrees(x) 把x从弧度转换成角度
8 Q/ i" t" _ p7 M# @0 i- #把x从弧度转换成角度+ t5 @5 T7 y) A& w8 f2 |/ A+ {# Q
- degrees(x)
4 l+ o8 i8 C$ Z7 c - Convert angle x from radians to degrees.
/ t/ c7 h1 h3 A! M- J& P4 [
; _7 y' z4 Z% y- >>> math.degrees(math.pi/4)
, e5 C2 b {$ f) }) ?2 ^' { - 45.0) \- O2 s( z/ H. u1 \7 V
- >>> math.degrees(math.pi)0 M7 Q4 u" U/ n% H( d/ u7 _
- 180.0
+ X7 H1 {% b: { - >>> math.degrees(math.pi/6)
' `2 k1 Q- l. z' B( E) K( G- r - 29.999999999999996- i& ^1 t1 n6 f6 C" Q) _7 ?
- >>> math.degrees(math.pi/3); L% n+ n4 s5 L. o9 ?
- 59.99999999999999
复制代码 ' b9 ^4 ~: m0 T2 U( }9 L6 T9 K
math.radians(x) 把角度x转换成弧度1 b9 v6 t8 `$ X9 ^3 z
- #把角度x转换成弧度
$ P; h! U) [, @9 Z2 g0 Q2 v" p- `) a - radians(x)
. M2 y" o8 Y( \7 ^" s8 N- f) |6 T - Convert angle x from degrees to radians.
" G* ^% u+ m. ^. i$ e( s - >>> math.radians(45)
' Q' L5 @5 X1 y2 R' r% K - 0.78539816339744833 V. R! g" R# s' ~ D( ^2 c3 l7 L
- >>> math.radians(60)+ j2 B' X! V i: s) W
- 1.0471975511965976
复制代码 - b5 ?6 t& G' D: @0 \% G" T1 f4 j9 \( M
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
" I' e' L2 X. P; d. I4 ]- #把y的正负号加到x前面,可以使用0, v- ~- |* |6 q+ ]+ ?) x* U
- copysign(x, y)
+ J) w- [- }! K/ [) D" V - Return a float with the magnitude (absolute value) of x but the sign 7 T% Z; U2 ^' x0 j. v( @5 j
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
5 F# d/ D& G L7 W8 a$ K; T9 r - returns -1.0.% H5 Q: Y8 `! L4 S+ W
1 m/ K+ _8 b5 Y+ `: ~: V2 u- >>> math.copysign(2,3)
; n1 o4 X; n8 K - 2.0
/ ^8 j2 g6 d8 y/ i! W - >>> math.copysign(2,-3)
' B, [8 ]/ O9 i - -2.0
1 z5 w( J! c3 {2 F* ?+ q - >>> math.copysign(3,8)
1 B8 H, r2 D Y [3 ~. ` - 3.0
: ^) _6 N% j* c+ D/ n$ N - >>> math.copysign(3,-8)
4 C: ]3 \% Y. I3 M& N8 h - -3.0
复制代码 / b- `" s: }/ P0 N$ o1 R
math.exp(x) 返回math.e,也就是2.71828的x次方, [+ [0 C1 P0 \$ M6 N" A
- #返回math.e,也就是2.71828的x次方: m0 i; ~) A6 W9 f
- exp(x)$ H3 |" _/ R& t v' D
- Return e raised to the power of x.
. |6 B+ B( r4 t - , q2 N/ B; H) s! v4 w1 ~
- >>> math.exp(1)" ^& H$ z- E/ g2 [: @
- 2.718281828459045; u; O' j/ d8 v$ t: W& |6 [) E
- >>> math.exp(2)
1 f# h( s5 `! ~2 N' e - 7.38905609893065( {- K7 Y9 H% ^5 b0 T
- >>> math.exp(3)6 h. l' s6 G. f6 E" Q$ ^
- 20.085536923187668
复制代码
{# t+ `; I+ F1 z2 V9 Z5 V Lmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
- D0 p5 h) {6 A: K2 c- #返回math.e的x(其值为2.71828)次方的值减1- u% ^* ?4 @% E
- expm1(x)8 U4 q& P! a7 J0 ]$ C' m1 u
- Return exp(x)-1.# K3 D8 i8 b0 k$ z, Y J& Y% w
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
5 w* Q7 S/ b, P- p- z
& d9 X5 ^: Q+ T! F- ^6 z/ o- >>> math.expm1(1)8 v0 N- t& E/ `( {! e: y! s/ p
- 1.718281828459045
: @ T$ J# C: N7 M" h# y - >>> math.expm1(2)
5 M$ u/ P6 q7 V' {/ E. s# l, _; A0 R - 6.38905609893065/ Y( E1 g* M- o4 A6 C2 i' \
- >>> math.expm1(3)
! H4 S' k) X6 [( Y& F4 X' q - 19.085536923187668
复制代码 ! B; }% R# \6 [( L& O6 p8 k: o
math.fabs(x) 返回x的绝对值
# T }5 j" Z) S8 h) A4 Q% d- n, W- #返回x的绝对值
- w$ I& S7 [9 T - fabs(x)
" h: O1 ~+ a6 H* u; I3 w - Return the absolute value of the float x.% U6 Z. \# o) j6 n9 \
- 0 ^9 ~, u6 `+ v3 u) N7 Z @7 v
- >>> math.fabs(-0.003)
; ~9 x4 U8 t! ] - 0.003, L j9 {) f! `) T- x
- >>> math.fabs(-110)/ }2 j: ? |9 v" U$ ?
- 110.0
/ X: ~( _0 |& S - >>> math.fabs(100), n# [# B& x% H5 J% q
- 100.0
复制代码
5 p# ?! W+ H1 h3 K) jmath.factorial(x) 取x的阶乘的值$ T: E2 }" ], ~/ o% p/ g' ]
- #取x的阶乘的值0 F; a9 V2 c2 H4 p0 U- B
- factorial(x) -> Integral
* ?4 M7 y& A( F - Find x!. Raise a ValueError if x is negative or non-integral.
) w4 i- o7 B* C: n+ K+ y - >>> math.factorial(1)
( y E! U; z% h& a( w9 D3 i- u3 q - 1' l$ e4 z' o R3 v/ }
- >>> math.factorial(2)
* j) M* {* a; p - 2
$ k+ `* m7 A" X4 I% f0 p% G' ` - >>> math.factorial(3)6 d4 z# H& r. F8 o5 l
- 60 U7 q' }: s6 L" G4 X, b
- >>> math.factorial(5)% H, N) W! m4 G1 N* E* j; n) l! o( y
- 120& f1 o( Q l' W
- >>> math.factorial(10)9 G* u7 L( E! w
- 3628800
复制代码 5 V$ Q9 k' g+ N/ ^
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
5 |# P; L( @3 P( Z( t8 h2 W* M& h- #得到x/y的余数,其值是一个浮点数- ~* ^6 B, h+ ~2 j
- fmod(x, y)
8 z( u3 ]6 P* C - Return fmod(x, y), according to platform C. x % y may differ.* Y0 K1 K" y9 {2 V/ G7 Q- \/ H. \. g
- >>> math.fmod(20,3)- [9 N8 X9 r. C/ w4 s! Z0 D4 ?
- 2.0$ \; }8 C% {1 { ~
- >>> math.fmod(20,7)& I! c& t0 e# i3 e
- 6.0
复制代码
5 z: N7 Y" E4 w2 c4 i3 @, cmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围( s; F: f6 ~8 Z9 Z7 p# }* L" K( n- H! e
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% w& C/ S I. b
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值# w, t! u: ` C! b/ \
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ s L/ ~. Z$ ]3 c
- frexp(x)2 {7 u! Q; _4 r* D0 Y; z: x! V
- Return the mantissa and exponent of x, as pair (m, e).' P+ ?; W3 R+ G: n) z# g" T
- m is a float and e is an int, such that x = m * 2.**e.
* x5 m8 i) U" C5 g - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.5 [: H1 f9 @" l) L9 w& J
- >>> math.frexp(10)/ l( f E, k# t7 U5 `1 ]3 ^. P
- (0.625, 4)7 d% Q' S* l, G6 Z* }4 d- W
- >>> math.frexp(75)
0 m9 y/ n1 L# r5 ] - (0.5859375, 7)
8 w; U" K; v: v5 a - >>> math.frexp(-40)
+ L; r$ G5 T9 [/ ` } - (-0.625, 6)
* u: M* E/ s1 n" l4 J/ Z4 X - >>> math.frexp(-100)
$ a0 N# g/ S2 [6 M - (-0.78125, 7)2 Z6 R& a' B$ e# U( r( A: [0 w8 n
- >>> math.frexp(100)! N' n7 z( N: H. D: v
- (0.78125, 7)
复制代码
& \' x! v) k$ E. D% {+ F' }math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
$ u# Q9 ~. f+ r2 k9 X- #对迭代器里的每个元素进行求和操作& T0 X5 R1 _+ N, x' n' e
- fsum(iterable)0 C! L* p0 [) t
- Return an accurate floating point sum of values in the iterable.
# U4 G+ b7 h/ _. z& \6 p* C - Assumes IEEE-754 floating point arithmetic.
1 ^1 z z p# z0 Z - >>> math.fsum([1,2,3,4])
# p' K2 N ^% j. d8 \9 g8 a - 10.0/ f& Y8 a5 B4 P. M2 I F/ j
- >>> math.fsum((1,2,3,4))
) u( j) h4 p7 h$ @- h8 L - 10.0
4 Z3 c( P4 Q, k5 v. {$ B - >>> math.fsum((-1,-2,-3,-4))
- Z2 R; N8 p+ f9 w" y% D - -10.0% U, V' ^$ O- P
- >>> math.fsum([-1,-2,-3,-4])& c. k* h/ Q9 ?* W8 k, M# R
- -10.0
复制代码
6 ]& }% J# _. p+ qmath.gcd(x,y) 返回x和y的最大公约数
N4 [" w$ Z. t; J' x9 w0 ]5 H- #返回x和y的最大公约数
7 y- n h3 O8 ~ p - gcd(x, y) -> int
6 X |* G# y0 i5 P - greatest common divisor of x and y
8 _# Y% C4 D' F! T/ W! f - >>> math.gcd(8,6)3 G- E+ V. @- i0 ?3 \; t) L
- 2/ i4 W( D+ j& _4 K# R1 N% w3 k2 y H
- >>> math.gcd(40,20)
8 _' c0 `; |! B9 V8 m5 Q# G - 20
) y* f5 T( O0 m - >>> math.gcd(8,12)
; s! I( [8 j/ z* Q3 [ - 4
复制代码
/ h, z& C0 W' V" ~* w* Xmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
4 O; m3 J' J5 M8 w$ e9 w) p- #得到(x**2+y**2),平方的值) h3 W4 K$ |" B6 C
- hypot(x, y)# y/ P- F) V0 D# b1 W3 y
- Return the Euclidean distance, sqrt(x*x + y*y). P, U& A" d; V! U) \
- >>> math.hypot(3,4)
7 ~0 |) G9 v* K) l - 5.0; R+ _9 q" Z+ s) n# R- S
- >>> math.hypot(6,8)/ ~9 r% |5 C4 I6 K
- 10.0
复制代码
( q2 E* {2 ^* _math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False- v9 p) ^# i1 B+ ]
- #如果x是不是无穷大的数字,则返回True,否则返回False5 }$ G. b/ h% [! A( z! Y
- isfinite(x) -> bool+ t6 R3 j" d# J& E
- Return True if x is neither an infinity nor a NaN, and False otherwise.
/ R) S- T2 v+ ?7 f - >>> math.isfinite(100). x% J& K5 ~9 Q4 C# i9 ~
- True
, B% w/ j K- m l - >>> math.isfinite(0)
6 w/ R# w! z4 O* s' g5 h% D; K - True
0 ~" Y) _# L1 \( b* `8 S - >>> math.isfinite(0.1)
& N/ Q% W! _ z - True
" l* j, ~& @0 y$ e* m. K9 X - >>> math.isfinite("a")
" I6 b8 k. ?3 K& n, g; z* F0 e# E - >>> math.isfinite(0.0001)
$ |; V9 F, t, k* o S+ i1 B - True
复制代码
, v9 l8 [6 v- g0 n' o2 d. Q8 T% Imath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False% X7 G' ~1 i, {; _% k
- #如果x是正无穷大或负无穷大,则返回True,否则返回False' {3 @+ u. L+ N0 \$ K j
- isinf(x) -> bool# e6 y) @& J- o9 ?! d
- Return True if x is a positive or negative infinity, and False otherwise.
0 N' z5 S& i. n1 Q" d$ l% j2 e - >>> math.isinf(234)
# T% f8 \3 T% D6 v+ l: i - False' S. z8 l0 Y* U; U* E* U) p, H
- >>> math.isinf(0.1)" D3 s. Z7 C( J! u$ |
- False
复制代码
+ t7 ^. @0 z F; [; smath.isnan(x) 如果x不是数字True,否则返回False/ C6 X F$ i* \; E" P- W q
- #如果x不是数字True,否则返回False6 n1 F! C: U0 ?8 p. s
- isnan(x) -> bool
; t' L; w# O9 H# v. Z2 _+ b9 G - Return True if x is a NaN (not a number), and False otherwise.. x; Y, z+ D1 [$ |6 |% @' v# n
- >>> math.isnan(23)
, N0 v+ ~$ H2 n0 G - False* u* W# Q7 U r$ s
- >>> math.isnan(0.01)5 E! G4 G5 O* H$ }
- False
复制代码 F: l2 ^! E- H* G% d7 D8 M0 L
math.ldexp(x,i) 返回x*(2**i)的值1 X9 f% j4 [' A6 _1 G+ J
- #返回x*(2**i)的值7 E6 ^* w" o9 x* g1 Q" s
- ldexp(x, i)
6 C. `2 i2 N% u7 L: d: k - Return x * (2**i).
2 q ]. A/ X: ^ - >>> math.ldexp(5,5)
' E- ?% ?# {: o* ~) j - 160.0
1 i* U) A s& ~ - >>> math.ldexp(3,5)
) w* n! J5 m5 T) D9 E8 c# ], D$ r; B - 96.0
复制代码 ! y: {! d" {2 J& z, K7 Z0 l
math.log10(x) 返回x的以10为底的对数+ w7 ?! k' X2 V8 R, V/ i& z
- #返回x的以10为底的对数
; I( U0 ], Y5 `4 N8 N5 h$ V! Z0 s7 R - log10(x)! f1 m9 x P3 @
- Return the base 10 logarithm of x.
) D* ^6 O2 ^5 P( g& Q - >>> math.log10(10)6 q, l, {' j7 c, t
- 1.09 B! ~% r9 r& s |
- >>> math.log10(100)
$ N( N. Z" S3 q - 2.0
* w: L* ~% r K' B8 O6 g, Q4 | - #即10的1.3次方的结果为20
; a5 d# T% X" S% o- A) l7 g9 s - >>> math.log10(20)! e* d1 g8 Z7 k, I( z% s- k
- 1.3010299956639813
复制代码 . K/ e2 \" N) E' R& e0 ?7 Y
math.log1p(x) 返回x+1的自然对数(基数为e)的值2 U1 E0 K' q$ P
- #返回x+1的自然对数(基数为e)的值
% p1 j$ F7 @9 v+ N; } - log1p(x) P7 H t7 N' _6 l! V% z& y
- Return the natural logarithm of 1+x (base e).) K9 h1 D7 R) M+ ]; g: h) m
- The result is computed in a way which is accurate for x near zero.
9 D$ y5 M& l9 h. [0 p - >>> math.log(10)0 b8 k( p, U2 O# [9 L- y% u. b2 R0 q
- 2.302585092994046$ J; ~. e9 ]- G2 K
- >>> math.log1p(10)
% ], W) l7 ~' D) x5 O* z% ?. A; p - 2.3978952727983707
4 f3 ]- D% X# Z+ f* g - >>> math.log(11)
' x8 Y3 F/ f+ o" d+ m - 2.3978952727983707
复制代码 $ e( v; i: H* g
math.log2(x) 返回x的基2对数2 ~- J2 I5 p* Q$ \$ t7 h
- #返回x的基2对数( I' U4 [! y* F4 p" C0 I) B
- log2(x)
4 i# U; R! a0 u3 o* H" e - Return the base 2 logarithm of x.
& P; F. M1 M4 c2 Z/ w& n# g - >>> math.log2(32)
, C p! ]: i4 [2 d* W9 `6 ?5 i - 5.01 A4 T, w# ?/ W& S H
- >>> math.log2(20)! y& u4 k+ W' n S1 @" X
- 4.321928094887363
* s; c& v) Z) J - >>> math.log2(16)+ h& J# p2 c4 ]- ~
- 4.0
复制代码 ' e' g/ s4 g! u+ X+ x
math.modf(x) 返回由x的小数部分和整数部分组成的元组
A7 z) V; P) H- #返回由x的小数部分和整数部分组成的元组- F& T6 L: |# ?/ V" |: f
- modf(x)
& \7 t% Q# i. [4 \; b9 D1 b2 y" r) S - Return the fractional and integer parts of x. Both results carry the sign
4 [9 [, l/ d! ^+ N+ |0 i - of x and are floats.7 |4 W4 o) `- D0 |7 q
- >>> math.modf(math.pi) Q8 T2 w) o6 i4 @ b- n- u
- (0.14159265358979312, 3.0)
! s& |5 ?( q6 T! x( A4 ]& c - >>> math.modf(12.34)' ]3 ~% J; f+ s+ \& r" ?
- (0.33999999999999986, 12.0)
复制代码
* n% J2 @( G" P& Z. ymath.sqrt(x) 求x的平方根# k2 w3 b3 S( }7 B, \5 l6 ~
- #求x的平方根- K- K( U7 b! s" z% A
- sqrt(x): Y5 @! u1 J: x0 j
- Return the square root of x.
! D7 _9 V1 c: E3 k- K4 e6 [ - >>> math.sqrt(100)
7 s0 ]/ E% G# E Z+ ]8 m# [ - 10.0" m+ W/ _6 U2 `
- >>> math.sqrt(16)
9 N) G1 _% V7 k - 4.0
& O7 G5 ^( b1 Y" ? - >>> math.sqrt(20)
8 V6 D- y1 a L, r. I& }3 z - 4.47213595499958
复制代码 , z/ |1 t1 l k
math.trunc(x) 返回x的整数部分- #返回x的整数部分. a/ a% k2 b5 M% b6 N% _
- trunc(x:Real) -> Integral' f8 ^% u5 R1 b/ M+ O! w
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method." [) ]3 d9 c3 P2 k# I* Z5 g6 L
- >>> math.trunc(6.789), l7 J2 L O4 O1 i. W
- 6' |5 E+ ^& f3 k
- >>> math.trunc(math.pi)
9 C. m$ O2 A$ {- o1 E7 L& q! { - 3
, p7 K9 w( P( P( _* Q - >>> math.trunc(2.567); g7 b9 |/ ~5 W5 r/ f) d
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|