马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 w" A( |0 M. ?3 R: S& p* C【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. h( f# {9 j8 D% ^* j+ {5 i: b' {$ X) [3 \, b
方法1:
& A2 f; I0 v; s$ v0 M4 E- >>> import math0 p$ a4 e E: ^6 {0 x
- >>> math.sqrt(9)
, K: c0 h) T9 Y9 E% p - 3.0
复制代码 方法2:
% [2 T* z+ |, }/ ~7 \4 K- >>> from math import sqrt% b, o; l# z/ s
- >>> sqrt(9)! A8 p1 k. j6 F; J4 t+ I" A" ~
- 3.0
复制代码
( v4 }4 R* _: P2 h 5 s2 F7 o- @( z( w
math.e 表示一个常量0 C! m/ U: a# x2 _" o; x
- #表示一个常量
; F7 l; t$ `5 U& E4 ` - >>> math.e+ y) h8 |# L. R0 m; `' `! v0 r
- 2.718281828459045
复制代码 ) R( \8 _* `) E# D# Y, ~
math.pi 数字常量,圆周率
" q S3 \0 [) U, a6 Y- #数字常量,圆周率% Q8 C5 T& P3 x9 c
- >>> print(math.pi)2 T5 O( C8 }5 v% Y" A! h
- 3.141592653589793
复制代码
* s+ x0 e8 J! u! vmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x9 z# A7 K! ?; K G
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x4 b' t. h/ Y& y; i) j; H* D
- ceil(x)
* o) U& {6 S* v, y. G1 ~6 `) d, ^ - Return the ceiling of x as an int., [ b8 B6 m' z3 a( m2 m
- This is the smallest integral value >= x.- p- Q! ] W+ V8 s' ^' _& j
" a$ t; i. r$ D( [- >>> math.ceil(4.01)# h9 G( @% E& I% m" j1 i0 d
- 5
. `% S+ d6 K: |1 q - >>> math.ceil(4.99)
' P. R0 S8 b4 _: B& I# e - 5
1 x1 d1 W6 N) Q+ J3 o - >>> math.ceil(-3.99)
: _! e' m- ~. g" q/ @ - -3! Y/ Z2 L" U2 b9 f! c
- >>> math.ceil(-3.01)$ }. f% U* ~* G' c4 y
- -3
复制代码
' D( _/ I% ~ d# c: }! j+ s5 |6 Z( y0 \math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身' ]1 h5 _1 H3 T- j" P+ }
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# e& m+ X: G# @2 b
- floor(x)1 X5 Q! M/ T1 F) {' B
- Return the floor of x as an int.: h( t. E' Y5 q+ [& E' Q
- This is the largest integral value <= x.
6 z( G* a! K% v T" V! E" H8 \ - >>> math.floor(4.1)4 a1 b/ t7 }7 H8 n* e. s/ l9 y
- 42 {4 k6 Y- }9 d
- >>> math.floor(4.999)/ d. }! j7 h/ s% e
- 4
4 d8 a0 I6 I P3 }4 J - >>> math.floor(-4.999)
8 D$ o" M+ D' L - -5* m8 A# f# V) ?/ z2 r, p2 [
- >>> math.floor(-4.01)
* O- n! _$ J$ g. Q: B( D: J - -5
复制代码
5 T( _( L: X! s, omath.pow(x,y) 返回x的y次方,即x**y
& I) D, [ z2 P# K, F7 k- #返回x的y次方,即x**y
$ r' Q4 [* R" x4 t7 D - pow(x, y)
6 x0 X- C- h! j* g. q5 H6 C3 B - Return x**y (x to the power of y).
/ w! f; g" f7 m7 l - >>> math.pow(3,4) l. c8 O+ i( N( E; m3 u h
- 81.0
+ X! k3 w: M+ z4 ~0 i - >>>
3 s: V3 y1 c: V - >>> math.pow(2,7)
: [. g$ Q( Y4 J) y - 128.0
复制代码
0 y' [) M( i" i% o, z& o2 Pmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& p3 e1 T7 c! r# R( G- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
* \* ?- ?" |; ? - log(x[, base]); R+ X$ a6 b C0 N% x% ?$ t$ _
- Return the logarithm of x to the given base.7 l# c& n0 p! b+ a. T% s f
- If the base not specified, returns the natural logarithm (base e) of x./ x" ?3 O! }/ {- P! k9 N$ E+ ? R
- >>> math.log(10)4 e1 C- s0 K/ H3 n
- 2.3025850929940468 S8 U" P0 f6 K8 i! H1 E
- >>> math.log(11)7 J6 L. }" t. {" y( t
- 2.3978952727983707, D7 u; p+ b1 z9 G9 C! u
- >>> math.log(20): i3 [. Y* U$ h j: m6 V H3 N. \- w
- 2.995732273553991
复制代码
6 ]* H; w. g# x/ r6 n4 l8 lmath.sin(x) 求x(x为弧度)的正弦值8 I3 c# @% ?* X1 t
- #求x(x为弧度)的正弦值
; d' G9 N6 p' m% U- H* G( F' w - sin(x)
- s8 X$ d+ v; s/ r9 V" {' j& y - Return the sine of x (measured in radians).
- s& c9 @5 g/ P+ }$ _! y - >>> math.sin(math.pi/4)
! z. ]0 u* q3 w0 q - 0.7071067811865475
t% t& d9 U+ l0 f0 ^$ n - >>> math.sin(math.pi/2): H: u+ V2 F( }" U* R1 |' @
- 1.03 d- G, F, l9 j; h6 x: J$ z
- >>> math.sin(math.pi/3)
' Y- V2 h T! @: D1 Z) F" ~3 o& E - 0.8660254037844386
复制代码 + o& |( A6 H* _
math.cos(x) 求x的余弦,x必须是弧度
( ]9 V4 K* R( O$ _/ J- #求x的余弦,x必须是弧度
1 e# x" Z; ]& E1 ^2 j5 o8 ] - cos(x)! P/ J% B6 @9 U4 V! e
- Return the cosine of x (measured in radians).
. w' p2 ? ~# R" q' T& x2 `5 b$ s - #math.pi/4表示弧度,转换成角度为45度
& X% N( U$ J- T2 c; U+ K8 B" f - >>> math.cos(math.pi/4)7 C" o( h% ?( w6 ]+ e
- 0.7071067811865476
6 n0 f9 ~; w3 \% z1 C( { - math.pi/3表示弧度,转换成角度为60度
* J/ W0 E) c k: Z, _ - >>> math.cos(math.pi/3)5 }& p) w* }8 {! ?1 z
- 0.5000000000000001
& W! p U- F* y3 x, y - math.pi/6表示弧度,转换成角度为30度: e2 R3 E1 Z" W- I/ Z9 U
- >>> math.cos(math.pi/6)# d8 X: ]7 h( M/ [9 `6 x
- 0.8660254037844387
复制代码
$ b9 q3 Y1 C' Wmath.tan(x) 返回x(x为弧度)的正切值
/ _; c- F& y( v! L( K- #返回x(x为弧度)的正切值( P+ v: C6 i6 r/ [- z( }
- tan(x)& Z3 B6 H/ o3 ^3 m
- Return the tangent of x (measured in radians).
8 q0 Z V; c2 U& B* Y - >>> math.tan(math.pi/4)3 d* c( @ p6 W" k! Z
- 0.9999999999999999
) N% V- c: M; }. B; C - >>> math.tan(math.pi/6)
3 O' C3 |% F3 o6 C( a# l - 0.57735026918962579 l' J# Z/ R) ~* k0 r
- >>> math.tan(math.pi/3)6 }1 r- K# d! W; u) X/ c
- 1.7320508075688767
复制代码
9 p" N; ~3 \$ p/ l: ~4 W7 m! wmath.degrees(x) 把x从弧度转换成角度7 t; k1 K* r t% R" O
- #把x从弧度转换成角度
: V% I) y# b" U0 e - degrees(x)
* m! R* q" H, H B9 S! P8 t$ k: h - Convert angle x from radians to degrees.& l5 c9 U& [3 R
- % B1 a4 _$ T* W% o) C& v& z
- >>> math.degrees(math.pi/4)
# [2 d. G4 g) j, I' q3 Y) ?" H - 45.0 i: D2 X: y) D* R
- >>> math.degrees(math.pi)- e8 i3 {9 V. U6 Q
- 180.0
' F/ u8 l8 X+ f' w: K F& Y - >>> math.degrees(math.pi/6)! j/ w- n/ z5 F0 C8 P
- 29.999999999999996- J0 J; T- ~* C+ ~- w$ J% ?
- >>> math.degrees(math.pi/3)
_$ B% }' X7 u. n( x8 D) R - 59.99999999999999
复制代码 # \& m% s5 c- @7 E; e
math.radians(x) 把角度x转换成弧度0 R) i$ Q' n% \6 G; N5 m- @
- #把角度x转换成弧度
! b! ]% G. Y5 ]- l! C ] - radians(x)3 v8 m! ]& G2 H' u1 Z
- Convert angle x from degrees to radians.6 e- i; ^! |% I, d" Y
- >>> math.radians(45)
/ o) [* x0 s- u, H4 O - 0.78539816339744838 x/ B9 s. ]- D' h- R: N5 h
- >>> math.radians(60)* h8 f) u( m f) t
- 1.0471975511965976
复制代码 ! D o* E# I9 T& q
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
4 i0 q1 z6 k( H% i4 v0 Q. }! M- #把y的正负号加到x前面,可以使用0' t* ^/ J$ A% M; L R, c6 v
- copysign(x, y)
! T, S! {/ W6 [2 o+ Y; c& |* f - Return a float with the magnitude (absolute value) of x but the sign 0 ]$ m. ^; ^' v+ ~7 |
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 w5 Q6 \. Y3 |- P8 }
- returns -1.0.9 U# ]' n3 C1 o% J, I6 R) b+ O
$ E5 k2 h' F; E4 }- >>> math.copysign(2,3)& b0 X. _; q. S/ ~9 K' V
- 2.0
' c; [3 @6 _4 i1 q4 q8 d! M - >>> math.copysign(2,-3)# [1 ]5 X! [5 z0 B! T
- -2.0- ~+ T+ I# x- S5 ~9 v+ w
- >>> math.copysign(3,8)
- T; X D2 J% l) D - 3.0$ K! H# R1 f/ @% t
- >>> math.copysign(3,-8)
7 \9 P$ \* l& ?3 u; @3 X - -3.0
复制代码 ) E/ C) z- X7 H' X' h
math.exp(x) 返回math.e,也就是2.71828的x次方
+ K( Y* F! s. `! \- #返回math.e,也就是2.71828的x次方& {$ {% p. _: R' f7 J: A3 s
- exp(x)5 K R5 j; @3 U4 p k9 y" u7 C4 f5 r
- Return e raised to the power of x.- D2 d, i; T% g+ N0 J
- # v8 [' S# Q( Q' @& c, A
- >>> math.exp(1)2 b6 m; J, \; v: J3 I6 N
- 2.7182818284590455 l, u, ~- F9 Y% z7 h0 B( D8 H
- >>> math.exp(2)
# {* w. s- G4 q" n" Y% |9 t4 a1 c - 7.38905609893065
- [7 c/ T! [% f' \ - >>> math.exp(3)- r1 U* T) N& E
- 20.085536923187668
复制代码
: P" U& k/ h$ k, @% a( H: J: w+ zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1' l& v# _: \6 F
- #返回math.e的x(其值为2.71828)次方的值减1
) A5 E0 {3 s j5 o- Q, ~ - expm1(x)
! Q, g( T* n$ d8 M2 W7 I' z - Return exp(x)-1.
9 X7 Y+ \5 K! x8 p' H - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 ~# I& m! w' @5 n% `% {& d% h& `
% A) @/ ~" z% ~& B1 i8 d% c( A. s- >>> math.expm1(1)* f: j. F5 q' T0 E4 n
- 1.718281828459045
( t0 K+ z/ a+ u6 k) j( | - >>> math.expm1(2)
% T0 N3 }8 a8 f( u$ u* Y - 6.38905609893065" T7 u; f$ {4 u: k" m. [
- >>> math.expm1(3)& ~* ? L4 X7 @' d5 C/ k
- 19.085536923187668
复制代码 & O' ]+ S. j% B' N: u- _
math.fabs(x) 返回x的绝对值
' }2 [ I6 T+ x, P- #返回x的绝对值$ O! H. S3 {( F" b: ~- y
- fabs(x)
- h+ Z2 t: w# M0 T5 R, T4 p - Return the absolute value of the float x.' u2 j5 r I" g7 Z1 d
- v, C; N3 Z: r2 A. j& c; ^
- >>> math.fabs(-0.003)
& \) a. k. l7 c1 N0 p& Z1 O9 A) _ - 0.0031 s0 G$ _3 v( n# R( q
- >>> math.fabs(-110)) a# ~) O% X' ?( w& U. x
- 110.0
( v5 H; G0 z+ K+ Y! O& \: ~# z - >>> math.fabs(100)
* D9 ?8 O, ?5 @( p - 100.0
复制代码 5 J2 N, i9 v Z0 d# Y# M; V
math.factorial(x) 取x的阶乘的值
& U' ^+ r% k, ?1 k- z: x6 p- #取x的阶乘的值
; X6 W+ ]- v" X3 ]4 K - factorial(x) -> Integral
& G( g6 m$ |5 C+ } - Find x!. Raise a ValueError if x is negative or non-integral.
( H X7 N( S$ R/ }: b1 _ - >>> math.factorial(1)
* r# l: B- U% g, d - 1
$ G9 o* G$ _9 h7 s - >>> math.factorial(2)8 L- O( G3 d1 j* X5 i6 w/ N
- 2( W; H) h* j0 x; ]+ V
- >>> math.factorial(3)
! [ i6 i8 u. n& \ - 64 m" a" ~# V* O
- >>> math.factorial(5)
! Z( e* s y& G - 120
: q$ J7 ? q/ o# s6 a - >>> math.factorial(10)
8 O0 l/ ]* f6 r" t - 3628800
复制代码 7 m4 j2 q0 Y8 i& A9 t
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
2 D) D" G0 y8 d8 z% m- #得到x/y的余数,其值是一个浮点数* J" r8 T4 S3 @+ T( s
- fmod(x, y)8 Z2 Y, T$ r7 j1 P- z
- Return fmod(x, y), according to platform C. x % y may differ.
+ g8 e. W" I0 l$ V; ^ - >>> math.fmod(20,3)
; l' Z% {- @' \' _ - 2.08 E+ ]! H$ w" J: @' n, J% E
- >>> math.fmod(20,7)
; w% a) i1 f" r) v0 P1 K - 6.0
复制代码 6 g0 _: c3 d: C8 _
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
. v t) s; e8 s3 V7 @- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- p* ]1 O- R4 b* ]7 {0 [6 I! E% ~
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
# j: L$ F% Q$ R- E/ o4 S - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 I- o2 M( @" c5 u8 i* s
- frexp(x)
3 M& ]* S3 j- ^ - Return the mantissa and exponent of x, as pair (m, e).* S6 b1 h$ V W8 l5 q" w+ k$ ~4 H, \
- m is a float and e is an int, such that x = m * 2.**e.
. W1 c9 P# l: a" D, a- J - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.: k G5 d7 n( i* s9 u# Y
- >>> math.frexp(10)
1 [! A8 u" f" X - (0.625, 4)' |; {% u7 {& g/ E2 T
- >>> math.frexp(75)$ x3 x. Z' J9 L3 m$ Z/ m- `1 N
- (0.5859375, 7)
) A3 ~# H2 p' Q2 N# P( X, P - >>> math.frexp(-40)
: Y" V: x) ?. `4 Q* ]- ^- x; x, k. r - (-0.625, 6)
4 Q/ d+ ~* s. l( y4 t( i0 B - >>> math.frexp(-100)
7 b3 R& i5 V6 E# I! y7 Y - (-0.78125, 7)
. P j `7 t4 U+ f8 \+ t( l6 s, I - >>> math.frexp(100)
! p; b! N5 g, d/ F) y! \ - (0.78125, 7)
复制代码 2 _+ F D' j% O# t4 @/ Q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
8 f B! T8 |) h3 c' T9 q3 `- #对迭代器里的每个元素进行求和操作% \* A% m# [! n; X+ o
- fsum(iterable)
) b5 \6 A$ D) @; q+ {9 k6 \' r: ^ - Return an accurate floating point sum of values in the iterable.6 l) E C7 H' `& V) b
- Assumes IEEE-754 floating point arithmetic.
0 W4 Q% q- }# y7 _" R) u% B* ?+ g2 m - >>> math.fsum([1,2,3,4])7 Y& e* w9 X. E( k
- 10.0$ U" k' ^; V2 {7 i) n' U
- >>> math.fsum((1,2,3,4))
! L8 b3 c4 ]4 P! K2 } - 10.0( I% u8 H0 X. H# Q& }! m K
- >>> math.fsum((-1,-2,-3,-4))
# M, H5 o6 O1 p3 v8 @ - -10.0# P. e. t# }* J. Q. Z$ O& d
- >>> math.fsum([-1,-2,-3,-4]): n( \' F& F! M& V( @% d0 p8 B; q
- -10.0
复制代码
, g+ y" f# ^* j" ^3 j6 q* v l% Imath.gcd(x,y) 返回x和y的最大公约数
% [4 ]: ^) Y# R- #返回x和y的最大公约数
! m' W5 e# S+ v. E - gcd(x, y) -> int P3 C/ O# p. G" G7 [
- greatest common divisor of x and y
' N) X* v: Y7 k; H6 D - >>> math.gcd(8,6)
6 y4 v) Y: Z2 f+ E8 D1 k - 2
0 {. L- ^7 R H: [5 k. o8 R. a - >>> math.gcd(40,20)6 l0 X3 p8 ]; D' f% b# B
- 20$ s! X6 k# x* t$ D+ }. b5 s- y
- >>> math.gcd(8,12)* P8 g" D# L3 K- h" v
- 4
复制代码
5 t% O- t2 d# d2 h @" `math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
' y; z$ v* g: `3 Q* k- #得到(x**2+y**2),平方的值6 s8 f& n, j( b8 S: j
- hypot(x, y)5 h5 D' X- C1 A( S7 B
- Return the Euclidean distance, sqrt(x*x + y*y).1 V5 J- M5 ?3 J$ k
- >>> math.hypot(3,4)
0 U _* N( I, B - 5.0
! |4 J2 g Q" b2 y' K' G - >>> math.hypot(6,8)9 t, [. C4 Z9 I- L$ r% Y
- 10.0
复制代码
4 O4 z% R7 @" Imath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 Q" w4 K6 V# u8 Q6 H: ?. u' j- #如果x是不是无穷大的数字,则返回True,否则返回False
7 \3 o8 h) I3 u# u - isfinite(x) -> bool. e. B6 t5 H e1 z" ~7 ]# ?# w7 k
- Return True if x is neither an infinity nor a NaN, and False otherwise.
0 f& ^- `) b& K- n2 N; M- Z - >>> math.isfinite(100)4 G- T5 r& o2 U9 R ]2 z3 l
- True9 V& L& A/ k' [) O0 ]4 H0 l5 X
- >>> math.isfinite(0)& [3 l" L/ n2 _# h/ J2 c6 L0 I
- True
* ]+ C: ^/ j& o1 F6 [ - >>> math.isfinite(0.1)1 u9 o: j9 r2 `4 Q8 E0 ^1 X8 \ T
- True. `" U* i* e% H2 }( @) q
- >>> math.isfinite("a")
$ u9 [/ d' y3 A3 P( w% O( x( } - >>> math.isfinite(0.0001)
" J& p4 `1 ?" v4 A7 s - True
复制代码 k4 ~ c( U0 t& A! S
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False7 V3 }8 _& Y( ~/ |
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
7 L- z* q+ \; g }6 g9 o1 i - isinf(x) -> bool0 u: V6 c. z& B* o: \; q- J) a) I
- Return True if x is a positive or negative infinity, and False otherwise.$ E* u( V2 A5 m5 w6 {. z4 d
- >>> math.isinf(234), S0 q6 D7 t2 J0 g: W7 h
- False# C1 H" u" t0 b* n& c% }5 b
- >>> math.isinf(0.1)
9 {9 K0 o4 @+ P/ l& V ?, Z - False
复制代码 3 u! [" L4 e+ U' ?% P, W
math.isnan(x) 如果x不是数字True,否则返回False% w' A* ~/ e" A6 U0 _
- #如果x不是数字True,否则返回False
$ L; h9 Y$ z( Y5 Z* F - isnan(x) -> bool3 l8 Z& }5 e( N- a7 }
- Return True if x is a NaN (not a number), and False otherwise.
* r4 U" K' W1 r$ ?2 Q" U* y- i - >>> math.isnan(23), T( \9 ]9 ^& W# {* w& t$ e/ o, X
- False, t) g3 h( m: B; j& ^) N
- >>> math.isnan(0.01)
$ n! U n0 ?% V- F; w. s" f/ @; X - False
复制代码 / }6 y6 x; i5 n z! Z9 h
math.ldexp(x,i) 返回x*(2**i)的值( ?3 V! T2 q2 J# H* o) _* w
- #返回x*(2**i)的值
$ c2 c/ }, T5 ^% ]: X' T, e - ldexp(x, i)6 G, p0 ?/ Z# O) |+ j/ v
- Return x * (2**i).
% q" Y4 e' T0 [0 l - >>> math.ldexp(5,5)! D! f' g9 \( e) F
- 160.0
, z, |% ^$ Y x. [; ? - >>> math.ldexp(3,5)
/ ~; l1 I6 t2 H. P; b* m - 96.0
复制代码 & n! T/ I& F4 y# V1 x" r
math.log10(x) 返回x的以10为底的对数+ O- M% M a8 Q- N* u8 x4 @: ?
- #返回x的以10为底的对数
4 e1 \3 h& `4 s: Q - log10(x)
: }3 t& o4 n9 Q9 F% W1 @6 g) r/ s - Return the base 10 logarithm of x., g: ]+ e! h8 T6 H1 i
- >>> math.log10(10)
3 j2 E/ c; R/ l7 ?2 c - 1.04 u' A( O8 b) @
- >>> math.log10(100)4 W7 m! Z7 a M [; h4 |) V
- 2.0! X( ?0 w4 A: j$ y1 e1 R$ Z
- #即10的1.3次方的结果为206 H" c: d5 `. l& C0 F! y
- >>> math.log10(20)) S" O- \& F+ @% A6 e+ F. b
- 1.3010299956639813
复制代码 - r1 X: C Y- @' f
math.log1p(x) 返回x+1的自然对数(基数为e)的值
( A9 v, E: K6 r L; x2 @* I- #返回x+1的自然对数(基数为e)的值
9 V6 m& X( R: `0 r - log1p(x)
; E" I7 g4 ?6 ]+ L: j& _# T - Return the natural logarithm of 1+x (base e).
+ T' Y6 p+ O* {. i# _ - The result is computed in a way which is accurate for x near zero.
( b. i" c* P: ~$ o5 Z - >>> math.log(10)
# ~+ i, G: O. ^' \8 m- p# Z. J - 2.302585092994046* Z! e u7 L4 m% t8 r( K2 P. o
- >>> math.log1p(10)
7 `, H8 F* |) I$ c9 Q1 i0 } - 2.3978952727983707
9 b: D8 P" I3 z" A3 H, R* Z - >>> math.log(11)
! p$ [, ^& ]% H3 k( b' |' Q - 2.3978952727983707
复制代码 - X7 F" _" @8 A; a0 \; O. {* T
math.log2(x) 返回x的基2对数
+ _* o4 U0 u \0 ^2 z2 \& |- #返回x的基2对数. @/ P$ V( b9 o1 h( K7 W
- log2(x)3 N5 Z- I9 r7 y8 `( ^. l
- Return the base 2 logarithm of x.
1 @- C! j- ~. \% `( p - >>> math.log2(32)) U; X2 ^" O" r' g- b
- 5.0
) v% y" ~& N% @7 A - >>> math.log2(20)
. w% a( u( g; U4 E* } - 4.321928094887363; X( @" U9 H g& V: u
- >>> math.log2(16)
8 `/ T9 v, I, z9 g6 K I - 4.0
复制代码 5 M! t9 F x6 y Q. h
math.modf(x) 返回由x的小数部分和整数部分组成的元组
! ~4 Y2 q' W/ T- #返回由x的小数部分和整数部分组成的元组
) X& ?% Y9 J V+ g/ B4 u - modf(x)& r }) [* s2 `, s/ K9 D! @7 o
- Return the fractional and integer parts of x. Both results carry the sign
3 G9 D" @( _% f! J- w [ - of x and are floats./ R! E9 _: M/ O3 o! [9 u
- >>> math.modf(math.pi)
9 f. M2 P& }. m* k, M& ] - (0.14159265358979312, 3.0)' @/ S/ x I! s! z; z* w/ t0 K& P
- >>> math.modf(12.34)
' L- }( H; T5 V7 Z* s U, S - (0.33999999999999986, 12.0)
复制代码 9 A3 S5 g- E* k: ^
math.sqrt(x) 求x的平方根
, s0 k* @; H* T" ?- #求x的平方根
* V% M4 Y, L9 r- ~& _' s$ X# | - sqrt(x)
2 |5 S& h; Y x" y/ | ~ - Return the square root of x.8 i4 z' @/ A) o! H* N* P2 s. U
- >>> math.sqrt(100)
$ @3 j c8 P0 `3 S/ d0 k( D - 10.0
& J* u7 i' q% d) K* _ - >>> math.sqrt(16)
- u1 m7 P, i% j& l7 q3 O - 4.02 ] W, a8 P5 D/ L* s$ ]
- >>> math.sqrt(20)9 p6 M# T4 F; v5 ?6 f4 `5 v N- S
- 4.47213595499958
复制代码 , A9 b0 A! [; A0 t( T( H# T" B
math.trunc(x) 返回x的整数部分- #返回x的整数部分
+ ?7 j9 Y3 D' i% |. J - trunc(x:Real) -> Integral
# I' J: ?7 Q5 a4 s' d - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# j0 N8 [) K! O5 Y2 z5 t% O - >>> math.trunc(6.789)
. C5 s4 Z( I- T- T* ]0 N - 64 x& T4 h2 I/ N8 u2 V
- >>> math.trunc(math.pi)$ b) S% O; H8 Y& h7 z/ x: U3 @
- 3
- X+ H6 h- ?. H3 f( |5 M# Z& I - >>> math.trunc(2.567)
2 ]: b* x) A4 Y; N( M* V - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|