马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 M0 l' }+ |9 ^. ]) c+ d0 t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& d' T C: S. C# P; Z- r% D+ Q j) l$ ?7 D
方法1:7 K; Q; w$ @/ ~' v8 U& S, N6 x
- >>> import math8 |$ }2 [+ ?2 V; p
- >>> math.sqrt(9)
, Y3 P+ t/ H1 ~& c7 _) h( F - 3.0
复制代码 方法2:, C0 L5 E# K% a) n9 Q
- >>> from math import sqrt) q' g! b5 K- k9 k3 {; J; o1 ^3 k' O
- >>> sqrt(9)1 p- M5 l+ O0 W6 o, U
- 3.0
复制代码 6 w P- Z3 {7 a9 \" o
; ^+ i5 C `% x3 j/ F# r" O
math.e 表示一个常量4 b5 n/ ?8 Q- R2 R1 g( s
- #表示一个常量, W! S- ~9 `! d& H
- >>> math.e, ]( n* F0 F) S- v
- 2.718281828459045
复制代码 H# n. F. T# E
math.pi 数字常量,圆周率
. ]3 |: E! Z2 u0 L* e- #数字常量,圆周率
4 m/ p# h9 r' L# M; i - >>> print(math.pi)1 X4 _) V6 S: A! Z4 Z' K
- 3.141592653589793
复制代码 4 q( M+ t4 U- O* C$ U3 j. H- A
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x& p2 x+ L: L( i$ u ~
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
# ^% c% B: i$ M& x - ceil(x). u5 O5 w" n$ t$ ^- P- a
- Return the ceiling of x as an int.
' ^% W' D4 T H" W) t+ u4 T - This is the smallest integral value >= x., h* E9 D3 W5 s( g
- G: B! T3 L' i7 T$ G% {- >>> math.ceil(4.01)
+ W* j" j7 ^! U5 X" B7 p - 5
; O: N, ]' I8 s' C7 |2 M+ t - >>> math.ceil(4.99)
4 f2 q/ j- n9 {9 _ - 5
& R! d4 c8 x: W: P - >>> math.ceil(-3.99)
" ^6 [ o. ?3 m" E - -3
# t" d" q2 W, H+ A - >>> math.ceil(-3.01)7 \( t' D8 d) ?. z+ p3 b
- -3
复制代码 - ?5 p! J# `: X5 Z" b2 @* J0 d
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 i& Z- f" D9 S7 U
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. V4 U- d/ K# u; h - floor(x)3 u2 I3 E" W" p' m6 d1 P5 x
- Return the floor of x as an int.
2 @* y; o) c& M; r4 x/ A - This is the largest integral value <= x.
) V" i) x) U) J+ m - >>> math.floor(4.1)
2 A. |, k e# g* F - 45 Z3 w5 v _$ m( _( }
- >>> math.floor(4.999) r! N4 X% g7 _0 X& u/ F0 H
- 4
- U7 r* v- G. A2 w/ n6 a4 c- u - >>> math.floor(-4.999)
' g" {" r& F' s3 A) d( i - -52 ]5 h0 E! P- z5 m; p
- >>> math.floor(-4.01)6 ]* F* K6 r3 D0 H5 ^3 g" w
- -5
复制代码 6 Y' K9 {! O" ^7 v
math.pow(x,y) 返回x的y次方,即x**y$ x3 Y( H6 Q3 Q
- #返回x的y次方,即x**y: U0 k. y+ \/ a0 v
- pow(x, y)- s7 z8 r; Z3 B0 q
- Return x**y (x to the power of y).
$ D, n) C0 \- x6 f$ _( X - >>> math.pow(3,4)
4 y! D8 x% U$ l5 w' T; @4 ]0 ^1 n - 81.0$ M9 Q, r3 m! z' t% j- Z
- >>> # X7 ]& C) B/ |3 k: I$ ~$ k V$ I: \
- >>> math.pow(2,7)
3 e5 {7 O/ j! e+ F* e+ ]3 w( x - 128.0
复制代码 ) B9 h( }7 Q% L% F U( i( h/ x/ X3 W
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 p6 o; V/ k' Q& y5 n# z- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
* v' d e/ \3 Q/ z7 C - log(x[, base])( Z: J9 \/ i) \
- Return the logarithm of x to the given base.
5 {& {: m5 }$ J8 D6 y0 f - If the base not specified, returns the natural logarithm (base e) of x.
# w9 d+ e" g* R - >>> math.log(10)
4 g# U' O0 r4 b" s1 d" m" F - 2.302585092994046
0 `# Z0 H- a# k* y; z - >>> math.log(11)4 n7 ]( `6 T* h' H( X2 \
- 2.39789527279837075 T4 z- W% A2 r) G8 U! M( X
- >>> math.log(20)
9 z& ~" I) M7 G7 K - 2.995732273553991
复制代码 ( @: x9 g6 u- ^
math.sin(x) 求x(x为弧度)的正弦值0 O/ F% V7 ^& \3 p9 N
- #求x(x为弧度)的正弦值+ A6 ~6 n: A& o" ^8 x2 n5 `; g" j
- sin(x)5 T: w9 P* o1 |& c, ~. _
- Return the sine of x (measured in radians).
$ N* G* ^6 N0 [$ M" h/ D( Q) U9 L' L8 K- ] - >>> math.sin(math.pi/4)
0 r. K4 k0 ]8 A - 0.7071067811865475
. ~5 d% _4 _9 o) \ K9 ?! ? - >>> math.sin(math.pi/2)
. ~0 o# U' z6 Z - 1.0
& }# G' |3 ]5 ~) Q - >>> math.sin(math.pi/3)) F' M( k9 Q3 \# w
- 0.8660254037844386
复制代码 , N& A9 b% ~5 ^' {! N2 y
math.cos(x) 求x的余弦,x必须是弧度' h' o! g t6 i4 ]5 K0 g- Z- x/ e/ Y
- #求x的余弦,x必须是弧度. o! j5 R5 l# z/ ?3 t" q/ c6 s+ `
- cos(x)
, K$ \+ a R9 W8 ` - Return the cosine of x (measured in radians).
% V V6 ]5 Y" A! E1 _- p - #math.pi/4表示弧度,转换成角度为45度
- Q/ ~* `, P$ O9 D/ i8 m - >>> math.cos(math.pi/4)# [7 @& B( O, n+ d/ |
- 0.7071067811865476; z. m/ l; b# U- P$ Y
- math.pi/3表示弧度,转换成角度为60度
! R4 e) m7 Z' T9 A) h - >>> math.cos(math.pi/3)+ l. H+ K1 Q, w* T
- 0.5000000000000001& \9 O" K+ y9 k4 [, m
- math.pi/6表示弧度,转换成角度为30度4 W0 q0 ?9 F) g
- >>> math.cos(math.pi/6)& J0 H+ |2 y: ]+ `0 O! `
- 0.8660254037844387
复制代码 ' y+ ~0 {% J) c; c5 r# A
math.tan(x) 返回x(x为弧度)的正切值7 A0 R1 c& M& m) ~& d c7 i) u# \
- #返回x(x为弧度)的正切值
, A3 L4 |5 }$ F2 I- E: E x - tan(x)/ w; T2 N8 @6 O: M" B0 }3 S
- Return the tangent of x (measured in radians).3 k; g2 S' W4 E! O- P2 @
- >>> math.tan(math.pi/4)7 X7 U) ?& w; `% o5 J" ^, j6 G
- 0.9999999999999999
& i0 O9 x5 d7 V8 P4 @5 y - >>> math.tan(math.pi/6)
& T ~& Q: J) y8 Z: r! l - 0.5773502691896257
$ p3 Z; W' r3 }' h4 d2 ^ - >>> math.tan(math.pi/3)
% I/ c4 N" r9 N7 ?+ W - 1.7320508075688767
复制代码 6 ]/ J% Q6 X) ]0 Z+ m0 T% ^! F( k
math.degrees(x) 把x从弧度转换成角度" z$ l! r. a3 @8 |
- #把x从弧度转换成角度
/ C! J' U4 ^% Z2 w1 d! q1 I1 u7 l - degrees(x)- h; ?, R: @' R& ^
- Convert angle x from radians to degrees.
, q, n6 O6 S+ X. P - , p+ w0 ]% V1 v, e8 `% e9 G6 R
- >>> math.degrees(math.pi/4)
1 y; c$ @5 l$ m3 I6 ?2 _2 w - 45.08 T2 y5 S# {3 U
- >>> math.degrees(math.pi)1 b" A" r2 @5 n( \4 k/ O6 w
- 180.0& B" J- m. H- N( F
- >>> math.degrees(math.pi/6)
9 P/ |0 y: k7 O1 y& \ - 29.999999999999996& `2 `* T! J2 x0 Z, d
- >>> math.degrees(math.pi/3)# p# t H$ i6 g' A0 t0 C$ i
- 59.99999999999999
复制代码
A+ u; \" F+ gmath.radians(x) 把角度x转换成弧度
+ g4 r: G6 @7 }" ?5 n7 v- #把角度x转换成弧度
( D( V- b9 I O6 M - radians(x)
8 c& e& y; p8 `5 @ - Convert angle x from degrees to radians.
1 Q3 j% E% A2 \/ a! y - >>> math.radians(45)
2 c4 T, l/ A: ~+ a$ j4 Y) r0 K - 0.7853981633974483
9 Y# x% }1 S# v2 }+ ~2 n2 y - >>> math.radians(60)
- q2 E" B% E4 x) D9 [4 n - 1.0471975511965976
复制代码
% ^5 n. o' Y" g% g! }1 O. c- dmath.copysign(x,y) 把y的正负号加到x前面,可以使用0, R$ G% }6 v$ o8 L8 i4 G
- #把y的正负号加到x前面,可以使用0
- A, ^ P" i. U- b, D) m - copysign(x, y)" z7 ^" E/ t" i5 f- a% C! A1 P7 l
- Return a float with the magnitude (absolute value) of x but the sign
6 y4 R, `; V- B) s! A! q - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
% B0 P: A, X( E/ j1 g, H/ t - returns -1.0.1 X& \/ w, |! W8 s
- ) W; ?" p. j1 X' n/ t% a
- >>> math.copysign(2,3)9 G& w' [' a; Z( c) @( R- |/ T
- 2.0- T# O. y7 A# e/ ]7 k, U; a
- >>> math.copysign(2,-3)5 \- P# Y3 Q' {
- -2.0
5 y3 P J" b1 c f9 e% U w3 B* G - >>> math.copysign(3,8); v# l: i) p' [6 s) G: A% A: M- D
- 3.0
4 f6 |, U: ? w: w$ T7 k - >>> math.copysign(3,-8)
3 D- b1 z# V5 |6 P9 r - -3.0
复制代码
( H3 a$ P" |% [math.exp(x) 返回math.e,也就是2.71828的x次方
1 Z) U! E% d$ \& |7 H! ] y/ K- #返回math.e,也就是2.71828的x次方
/ r2 q' |: V3 a. p - exp(x)
$ B% x* w; {' W7 V2 d - Return e raised to the power of x.
: |% ], B0 R+ H' m - 6 Q/ H( @* l! s$ C
- >>> math.exp(1)
+ W. x, G7 g. ]4 I+ y. e7 U - 2.718281828459045. K2 R L" Q- Q0 k3 q7 L
- >>> math.exp(2)
2 r! k1 P/ s; U0 _! j8 e - 7.38905609893065
* i1 g* p/ O* T3 F) k - >>> math.exp(3)- [7 K; I) O7 p6 g
- 20.085536923187668
复制代码 ' ~& u; k+ R, X& _$ w
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( f4 n' C4 U" y& Z+ U
- #返回math.e的x(其值为2.71828)次方的值减1# G, d$ _- ]( ~
- expm1(x)
6 `" c, M- b6 u \0 a4 ?9 Y - Return exp(x)-1.) k7 ]- @1 h# w; A- O
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.# Y }, S, c6 P) ]
$ c6 [* }! J1 b0 ~# R6 n! N- >>> math.expm1(1)( X$ b! d8 u" H% I6 ^" Q# n$ u
- 1.718281828459045. t* Q. L& h3 d# L8 }& T
- >>> math.expm1(2)' B8 J _7 Y6 W& V
- 6.38905609893065
$ E- N4 J7 d) C! w$ ] - >>> math.expm1(3), h7 G* x! @* R( g' }( G5 J$ b
- 19.085536923187668
复制代码
8 c- _ W0 }5 r( ^$ G7 Z7 F& rmath.fabs(x) 返回x的绝对值
. h5 Z6 e9 @" v- #返回x的绝对值/ Y0 ]( ]9 _# B ?
- fabs(x)
# i" i5 P6 b6 R3 V& L - Return the absolute value of the float x.( O2 Q* B8 Z) v: o9 j# J
1 I0 B: _9 R7 ~9 }9 y- >>> math.fabs(-0.003)
6 e# f5 R0 z4 ] - 0.003
* e! T8 f* x$ `. ?; H" ^. D - >>> math.fabs(-110)
& j0 Z. G9 l8 E' M$ C - 110.0
. s6 |6 K* r. A; t - >>> math.fabs(100)- T: w4 e1 ]6 ?
- 100.0
复制代码 ! F& d3 _* a1 n# d) X
math.factorial(x) 取x的阶乘的值
. H3 x% q/ o7 p- #取x的阶乘的值
_8 _/ E, a; u - factorial(x) -> Integral# M; O! B* O' s( T
- Find x!. Raise a ValueError if x is negative or non-integral.3 z/ n) Q% L$ s( @
- >>> math.factorial(1)
# a3 s Y( \! Z B6 p - 1
3 Q" f' ?6 ?3 O( S3 U. O - >>> math.factorial(2)
; w, M6 S A# d: h0 f; q3 @ - 22 `; O4 A# a v( ?/ U p& n
- >>> math.factorial(3)( M3 C6 L5 h& J, B4 O5 J
- 6
1 w0 q) S& x( r, r, i. Q - >>> math.factorial(5)
& j1 F! q1 i6 ?: j - 120. o) L. y# s9 W" Z( }3 m
- >>> math.factorial(10)
0 b: c" V# |7 Y/ x/ \ - 3628800
复制代码
$ r% O. O" U0 U% `2 bmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
1 @6 _( }" o' q- #得到x/y的余数,其值是一个浮点数
* {) Y' |; Y7 o/ d4 x y - fmod(x, y)! l) L: ?2 W3 O7 }: d
- Return fmod(x, y), according to platform C. x % y may differ.. @% Y; [5 d& n/ y0 T
- >>> math.fmod(20,3)8 P+ f9 J& r+ Q* u% m! C8 N/ M
- 2.0' x3 P* m2 P# l/ m; F, ~2 E
- >>> math.fmod(20,7)
# [: X, d9 Z* \' o - 6.0
复制代码 8 y0 O# A. z) e, z8 y/ l
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; N0 z! Q; i! a& T3 r3 [( S- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
( e! P& R8 L* u: a3 f - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
* l2 U5 D4 |( r7 w( ] - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
; h) q0 k: a+ T" ~. z - frexp(x)
+ x ^7 s. i1 D/ A$ W: X3 D) m - Return the mantissa and exponent of x, as pair (m, e).
( Z/ z9 ^# f8 B& N, F - m is a float and e is an int, such that x = m * 2.**e.
" f9 k! u5 g7 j - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
! ^5 F6 D- [& }2 x: c - >>> math.frexp(10). ] N4 y' q2 v5 `( `, ^
- (0.625, 4)( N6 u% N; ^. a; h( F
- >>> math.frexp(75)
8 }8 W" A. F _- p - (0.5859375, 7)2 ` U6 n1 W, F" G/ A5 l: n
- >>> math.frexp(-40)
! E4 G: P! w% z3 V% i8 |; Q9 L - (-0.625, 6)
8 \) r$ i6 L6 ?# ]' o; \9 R: n - >>> math.frexp(-100)
9 `% a |" S- I5 W$ X) q - (-0.78125, 7); a1 U% o) p z0 U0 o. C0 Y- I3 c
- >>> math.frexp(100)
8 X; L1 L% t- H g% ~ - (0.78125, 7)
复制代码
$ h5 K; T$ F7 p$ c3 kmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
' B# o: z8 e) i2 t0 |; J r, k- #对迭代器里的每个元素进行求和操作
5 Y. A1 K. Q: Y: h - fsum(iterable)& |8 P2 a( D6 C$ ] w. n
- Return an accurate floating point sum of values in the iterable.
4 d; ]% |( T* ~9 H5 n - Assumes IEEE-754 floating point arithmetic.
4 X& _0 [" \+ Y: j# c - >>> math.fsum([1,2,3,4])' Z5 P* ^; z& z# {# f( o
- 10.0
+ O1 W( d5 w6 L6 l1 T - >>> math.fsum((1,2,3,4))
% K# B. [8 Z0 A5 K1 { - 10.0
0 v3 W) n# s% H- z) r% t3 |. ` } - >>> math.fsum((-1,-2,-3,-4))
" D: N9 L, _5 p( J" t3 m - -10.0) V3 r- H1 U# d7 g3 h
- >>> math.fsum([-1,-2,-3,-4])! n5 P$ l9 A( D7 n( H1 ~2 \
- -10.0
复制代码 # K4 n4 `9 v# [2 C
math.gcd(x,y) 返回x和y的最大公约数. ^, W: v/ s6 Z
- #返回x和y的最大公约数
. ?2 o2 ]9 `6 u1 g - gcd(x, y) -> int0 M ], e" j% M# G4 n
- greatest common divisor of x and y1 m) D( z( ~( o& R o5 x- A: s
- >>> math.gcd(8,6)) {6 q) R4 W% W+ p4 s3 \
- 2/ T) V! F$ X6 @# R6 ]1 u+ u
- >>> math.gcd(40,20)
8 q9 h) n7 u5 A$ S+ H* A# r - 200 T2 g, X# q: Z
- >>> math.gcd(8,12)- H) M' |& a; q+ h5 W: k
- 4
复制代码 & n- ?" g+ {7 m8 O$ \
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False; M& m8 q# v3 f1 \
- #得到(x**2+y**2),平方的值! i+ v% [" S5 `5 s5 L" @) G5 S9 Q
- hypot(x, y)
- B$ P* ~ a- s# a" p) b - Return the Euclidean distance, sqrt(x*x + y*y).
! {. n! R$ p" A" U - >>> math.hypot(3,4)# |) [* u! @3 U& ~- K: s# \/ U9 v o9 O
- 5.0
/ N2 H6 F, V: U( q* b0 N, u - >>> math.hypot(6,8)
, ]& i, [0 H4 [' z9 A7 ]) z, F - 10.0
复制代码 ! k5 g( T+ h; X, o1 b0 A6 R7 L. }; @
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
- l( Q) J+ U* _6 C# I, ]' G( Z$ ?- #如果x是不是无穷大的数字,则返回True,否则返回False7 b3 D6 @. T6 U+ c! O" U
- isfinite(x) -> bool9 y. V. U. W% v1 g+ s0 R
- Return True if x is neither an infinity nor a NaN, and False otherwise.
) Q& \9 K$ S/ P) O - >>> math.isfinite(100)
& r4 A0 }9 k7 ?+ r0 r9 U5 k - True3 j: x+ N/ }, R. T9 w
- >>> math.isfinite(0)! m" D7 ~( b6 }9 q1 O
- True. I: O/ H# C5 {: e& m6 j/ h3 p- h
- >>> math.isfinite(0.1)
) i5 J$ H# M3 j& S8 B - True( m t Y& o3 E0 K. E2 Q/ ^, E
- >>> math.isfinite("a")) ^& `8 o; Z* u8 \
- >>> math.isfinite(0.0001)! \$ W$ ]2 P \+ I: u" B0 m
- True
复制代码
% k" D& e2 \ [1 f9 _* s4 Bmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 I7 h& \7 {; C( }5 H+ L+ d- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ Y5 U5 `9 ^* y$ x k - isinf(x) -> bool6 e! w* c( Q0 y O% r% C
- Return True if x is a positive or negative infinity, and False otherwise.
8 \# e u( C6 V1 t - >>> math.isinf(234)
& C. _0 F! r p& f, D# Q - False
* w! W% t8 N$ ~* q3 M' |4 I - >>> math.isinf(0.1)
( H3 Q9 [* \* t7 [& L - False
复制代码 0 \9 X; V @/ ?; M* }/ T
math.isnan(x) 如果x不是数字True,否则返回False2 q' f7 Q4 u! r# [9 i* q" G
- #如果x不是数字True,否则返回False
* l' t: O9 a9 ~9 T' P! t& k - isnan(x) -> bool- H* d/ _. a5 {2 _, `& Z
- Return True if x is a NaN (not a number), and False otherwise.
, ~9 O& \" a8 [$ T; T: X5 A - >>> math.isnan(23)0 X. k7 H7 `) N3 R" u0 f5 B
- False
: t9 D6 N! }' s9 K8 X& a - >>> math.isnan(0.01)
@# i) v" Y% g - False
复制代码 ; u9 i0 J0 S; Q1 a
math.ldexp(x,i) 返回x*(2**i)的值
1 {) W1 c" I# _* F9 G- #返回x*(2**i)的值6 m) c" q1 E/ ?% {* e
- ldexp(x, i)
5 n% K6 q% [' L% }% f3 i! ^ - Return x * (2**i).) c! t) j3 F# G+ r6 H
- >>> math.ldexp(5,5)( B: F6 Y0 \) H3 K
- 160.0
6 G) {5 d- R2 u5 K - >>> math.ldexp(3,5)2 w* E% @+ D, {
- 96.0
复制代码
! H8 \. ?- A! f( l' Zmath.log10(x) 返回x的以10为底的对数
1 }' f o( \5 l" T- #返回x的以10为底的对数
) F6 q2 |2 i4 Q: {1 ^ z - log10(x)3 ]- a( `- W1 Z2 q; v
- Return the base 10 logarithm of x.. H, ]* ~0 t# e8 b: r! D
- >>> math.log10(10)
4 L5 Y, g, L8 K5 E% `! k - 1.0+ J! _; k& v! o4 j, n' {2 \2 @8 h( j
- >>> math.log10(100)
/ b5 @# C5 S. H3 I5 {: ^! K/ b8 w7 } - 2.0
" T! @+ U* S6 C& E6 |: ^ - #即10的1.3次方的结果为20
* E% L/ k5 v9 o2 ~+ r9 ^( }& K - >>> math.log10(20), c4 I" `+ `$ Z
- 1.3010299956639813
复制代码
! K$ e) \* X A' V5 o+ V& mmath.log1p(x) 返回x+1的自然对数(基数为e)的值
* e' S2 W& X! g- e$ i0 X- #返回x+1的自然对数(基数为e)的值+ |/ s9 k* a0 w0 B, V' m# v+ D
- log1p(x)# E2 _ e! w( x2 |5 d+ u$ R# L
- Return the natural logarithm of 1+x (base e).
5 c) S, `1 c+ T+ {& ^0 z e - The result is computed in a way which is accurate for x near zero.; ?6 } C" ?/ y; c/ K
- >>> math.log(10)
3 z) j- C* ?' p) h+ A3 V f - 2.3025850929940466 @) ?) \# b: t0 o4 j
- >>> math.log1p(10)* q2 n1 f+ H5 E8 V- W! V
- 2.3978952727983707; t# e4 j( K, x- z, k
- >>> math.log(11)
0 X6 ^3 L& B+ _/ I5 A- v - 2.3978952727983707
复制代码
- n9 [# T5 S6 wmath.log2(x) 返回x的基2对数: J1 w5 R( _- \0 n6 F
- #返回x的基2对数5 J- c' M, f" w: w
- log2(x)
; P I( q+ Q1 S$ r! C# |9 G - Return the base 2 logarithm of x. |; m8 u- g' i9 M& G
- >>> math.log2(32)# ]7 C- Z2 h* ^: A! W
- 5.0
0 {' y. P4 }* w8 B - >>> math.log2(20)
3 j! f2 D% C) U2 I: X+ q - 4.321928094887363
2 b+ Z, [7 G3 U* l - >>> math.log2(16)+ Q9 |5 _) X) u! g9 c. y5 V9 Q
- 4.0
复制代码
$ W$ m: P, G7 K% v7 r4 Q8 Umath.modf(x) 返回由x的小数部分和整数部分组成的元组8 C1 M$ F+ U6 n9 Z
- #返回由x的小数部分和整数部分组成的元组4 p7 ^% k! j% g5 L
- modf(x). B2 K& \9 t7 i- q( ~
- Return the fractional and integer parts of x. Both results carry the sign
+ o# s7 E9 D6 U( x& h W6 B- g - of x and are floats./ R; ]7 e3 n# f) y7 r3 z
- >>> math.modf(math.pi)) w! {" d* E% s6 j" l0 U7 g4 q
- (0.14159265358979312, 3.0)
* {2 V" J* r$ K3 X3 p' j# s - >>> math.modf(12.34)4 Z! q, r% h! Z" k8 } z
- (0.33999999999999986, 12.0)
复制代码 ! b9 H; ^/ K4 \
math.sqrt(x) 求x的平方根8 l' Y- U$ p7 q O8 N2 P2 a0 O$ `- i
- #求x的平方根
) H& x5 m1 @/ }: y$ X. ~$ {+ C - sqrt(x)2 \+ _) s2 \4 D) T4 B% q" I) }1 m
- Return the square root of x.
& `8 u5 a4 L! X& u: N4 L3 } - >>> math.sqrt(100) F0 f/ m! o8 J" y# X2 p1 {/ g
- 10.0
5 R, e6 s' m1 l7 d. Y - >>> math.sqrt(16)" E% d/ v# o7 H7 o
- 4.0
3 u7 ?; v& b% Z: R+ } - >>> math.sqrt(20)+ D& O: B7 R( j- Q) X! h+ ~
- 4.47213595499958
复制代码 ( X' n" S7 w! z) P. R
math.trunc(x) 返回x的整数部分- #返回x的整数部分
5 T3 U7 s' Z7 [% |. x+ t3 N - trunc(x:Real) -> Integral7 e1 i7 P/ g) B, {5 ~8 H& d
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: r/ }) ~# E! R- X. ~/ c4 ~
- >>> math.trunc(6.789)6 h8 S# }5 f- Z4 l$ W; h
- 6
5 | m: i3 X7 ^* u - >>> math.trunc(math.pi)
+ w3 T. y$ U% n) R! s - 3
" E7 K" S: ]6 _5 m - >>> math.trunc(2.567) h; R! \# f' |4 Y
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|