马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 m9 v; Q$ }4 f) Z( m' R' h【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。$ ]6 T( d% Y2 p% d. j- e
6 C q! T- A. y/ A$ E方法1:
, d% [8 C! a1 Q: ?9 `" {5 g$ y- >>> import math% J# _6 r! G0 i ]% z
- >>> math.sqrt(9)
1 Q8 V2 y, L/ g- B9 X* l - 3.0
复制代码 方法2:
) D% h4 e2 p4 v/ O6 ]9 u2 W- >>> from math import sqrt' D9 s/ C2 C: ]0 n& D. f* f
- >>> sqrt(9)
, n: R/ J" Z# R% Z# n( J& C - 3.0
复制代码
5 l4 T, N# x7 L! ^6 e; ^ $ Y L* W; t8 G- t( d, F" a6 |
math.e 表示一个常量5 T m+ y9 m1 {9 c6 P" h. w/ h
- #表示一个常量& x, \7 V, z# N* J1 P; n% |
- >>> math.e0 ?9 ~. ]1 L7 k
- 2.718281828459045
复制代码 9 {$ d8 O! z0 V; G' u
math.pi 数字常量,圆周率
' h- t. ]; n, `. \: g- Q" o- #数字常量,圆周率
, B7 s0 p1 U- v - >>> print(math.pi)% O# m$ O T: E' _6 P
- 3.141592653589793
复制代码 0 g. S. T- j8 }% q$ @' _5 l
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x+ d9 H7 Y2 z% F
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
- u9 l: r+ a7 x7 B - ceil(x)4 m2 p& d! t! _
- Return the ceiling of x as an int.
& W$ g. u8 O1 z& _& v ?, w - This is the smallest integral value >= x.
3 z3 o" ^& B* D - . N$ l$ n Y% t$ e: d4 Z
- >>> math.ceil(4.01)
! Y7 f' a; M% F# u - 57 x' Y8 k6 z6 y$ l- n' [2 ]
- >>> math.ceil(4.99)0 k4 F5 [" K, d: j/ w
- 56 b: W. `( M. l
- >>> math.ceil(-3.99)
% g6 u. s C; v" A, }! o/ v - -3+ g6 Z! M0 X% S
- >>> math.ceil(-3.01)
; X0 ]& N0 {; x- A+ ?& { - -3
复制代码
& y- K' S9 m: V0 Jmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身. o& Q b# m8 a7 q" U
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& `5 f( F/ r8 S/ t& j5 V - floor(x)9 s( [0 o X3 j. I+ `
- Return the floor of x as an int.
6 L! s: D* d3 x; H# O/ D - This is the largest integral value <= x.- r( ^9 [) v1 t0 z# `
- >>> math.floor(4.1)$ T" e! j# ^) ]* K. L
- 4
5 V; D$ s, ]3 ~ - >>> math.floor(4.999)
- p$ P9 k3 N) C/ X, X, G - 41 N* l" _' z0 y* V& ]
- >>> math.floor(-4.999)
q9 o& j/ v+ v - -5! O6 K0 c" N# m' q( H
- >>> math.floor(-4.01)
9 L6 A; s5 l {4 Z - -5
复制代码 / S3 o& P, a U& ]4 M3 C1 X0 |
math.pow(x,y) 返回x的y次方,即x**y
& N: s9 G* k$ L" |( o1 l9 u- #返回x的y次方,即x**y, W' w) O* }8 X5 E2 L9 z7 k% d
- pow(x, y)
# u; W" N$ \( d* l# ?2 n - Return x**y (x to the power of y).
% e; s( r7 C( z% [* `+ F* b' j - >>> math.pow(3,4)
0 f/ q+ z$ q/ _4 U1 r# H6 w - 81.09 c5 j# J- c4 h# H, t
- >>>
9 o+ c& h2 K* K0 u - >>> math.pow(2,7)
) g' j- I# v+ j! c w: R! ~: x - 128.0
复制代码
4 C5 F& k" z! D9 hmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# Q+ w5 n# U& o, n! _2 R- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" v2 t3 b6 L9 p2 m0 \0 D' m2 G
- log(x[, base])
) v" o! K4 I4 I/ ]! r% W - Return the logarithm of x to the given base./ |% r# ]# l( R7 T" @
- If the base not specified, returns the natural logarithm (base e) of x.
: a# k2 {. G# w: P - >>> math.log(10)
) r9 _' S% ]2 h) e: A( W5 K6 z - 2.3025850929940460 ~+ Y4 x s' j7 k
- >>> math.log(11)
, H( S; J2 B/ k5 Q2 m6 [# o) Y' e - 2.3978952727983707
* i! J: ^0 T$ [ l - >>> math.log(20)
# W9 N. j2 ?- i; G" R. ?& K L - 2.995732273553991
复制代码 , f2 _. k. X% U
math.sin(x) 求x(x为弧度)的正弦值
, E* B+ A" u6 e7 S- #求x(x为弧度)的正弦值$ ^7 U+ T& x* a0 V3 n
- sin(x)( ?( B' _. T0 d6 @& n5 s' w$ k( o* ?
- Return the sine of x (measured in radians).$ w- k9 Q* t) \3 H# ~5 E- H6 v% ]& U y
- >>> math.sin(math.pi/4)
% n) \# I* ]. U' O) }- f1 X" _2 w - 0.70710678118654752 s- r1 V) ^' V8 N: p+ v7 g7 [
- >>> math.sin(math.pi/2)3 P. O# \( e b
- 1.0! {, b+ D% i$ D0 ~* r
- >>> math.sin(math.pi/3). m& g7 x: j3 b3 ]* `) C) l6 @
- 0.8660254037844386
复制代码 + v1 L* I$ u# S: \; C" `. y0 B. e1 d
math.cos(x) 求x的余弦,x必须是弧度9 h- R4 ^+ D) g' p2 W
- #求x的余弦,x必须是弧度' j4 Z: N- a: k2 Y9 |! O
- cos(x)
5 b4 [6 y3 K/ f9 R4 W9 |7 o0 X - Return the cosine of x (measured in radians).
8 K+ Y+ a1 {) U) ^8 D - #math.pi/4表示弧度,转换成角度为45度0 ^; h6 O. Q; B; | z/ ?
- >>> math.cos(math.pi/4)! G, L8 |$ i6 X2 a5 @5 d+ b& z
- 0.7071067811865476
/ y( E1 s0 ]2 |* ]: d' W - math.pi/3表示弧度,转换成角度为60度
3 y. G. o C% z& Z; r - >>> math.cos(math.pi/3)
* g7 _: A- J" \$ _ - 0.5000000000000001& r9 |% [. T V, T3 s# ]" O
- math.pi/6表示弧度,转换成角度为30度; M2 |! ], K4 f$ u$ N
- >>> math.cos(math.pi/6)
) \6 n: I q* u7 q+ j/ I9 ~ - 0.8660254037844387
复制代码
& \/ _( F6 K8 [3 d; @; Q4 Nmath.tan(x) 返回x(x为弧度)的正切值6 T, G) w2 I4 Q7 N Z" _
- #返回x(x为弧度)的正切值
8 ?# A% N9 U+ a+ ` - tan(x)* T3 W: B( \' @; x6 w
- Return the tangent of x (measured in radians).7 W9 o! D: d6 G
- >>> math.tan(math.pi/4)
1 }/ R) K& J. W+ C e; M0 b - 0.9999999999999999
" @3 B. Q5 b- D - >>> math.tan(math.pi/6)/ t# ]( S: ?5 s$ Z/ v
- 0.57735026918962573 s& M2 A- @* X; ~. i9 N# k5 J
- >>> math.tan(math.pi/3)
+ Z5 W3 M5 f/ x: a& G - 1.7320508075688767
复制代码
$ g; W. K/ T% Y' vmath.degrees(x) 把x从弧度转换成角度
, S; f4 m2 Z- q, M/ f! \+ H- #把x从弧度转换成角度7 O4 _$ X- u! H2 d
- degrees(x)- u; `: F/ M: L0 [# U
- Convert angle x from radians to degrees.
0 b7 n( h3 n. A! B: q1 T
) U. D: U+ y' X8 S- >>> math.degrees(math.pi/4)1 X5 l9 z# n% l% C" \! z
- 45.0
8 P1 w* h& K U6 @ - >>> math.degrees(math.pi)
" N9 a& c2 k6 \6 }8 a1 k - 180.0
, I2 a, P8 n' |4 Q0 ~+ n - >>> math.degrees(math.pi/6)
A5 D1 X/ g/ Y& K- ~& G1 } - 29.999999999999996& l* r! O# D+ i6 O) w
- >>> math.degrees(math.pi/3), U. j3 o* F- E& M& _
- 59.99999999999999
复制代码 ' S% N' B% a- d/ s4 z6 Y
math.radians(x) 把角度x转换成弧度
6 n( c/ E% |" B J+ n+ `# X- #把角度x转换成弧度2 u3 p# T7 [3 l! Y: M& v7 s
- radians(x)
G8 A, x/ L& c9 S8 v0 l - Convert angle x from degrees to radians.8 @; G) @1 f5 |& j- x- n7 b7 J
- >>> math.radians(45)
/ l, j8 n) j& j; w - 0.7853981633974483) t: \; p2 W( Y( J3 A5 `! c
- >>> math.radians(60)9 f- H! m$ y4 F8 ?$ y% G9 k) z
- 1.0471975511965976
复制代码
1 c) b8 E8 _& W6 P A# Z8 xmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
' [/ o5 S" g0 p; @- #把y的正负号加到x前面,可以使用0
% P6 _6 M# _* E - copysign(x, y)
, ^- |' |4 f/ e0 v - Return a float with the magnitude (absolute value) of x but the sign
S6 F2 Q: k, f' ^ C2 _" Z; V - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' K8 H0 z) x6 a7 x0 p
- returns -1.0.
* f l/ t. _' r; k( O
7 P4 v( ^' g" ?% r9 u2 x- >>> math.copysign(2,3); b% e* l( R* {: m0 ]
- 2.0
4 i. h& j$ i7 Z; A- x) i( _' }) n) g* n - >>> math.copysign(2,-3)+ N" r' F7 A- b ?$ h O
- -2.07 q4 ?# q, ]% `4 m! v, s
- >>> math.copysign(3,8)
3 }8 N! H, @ U( F - 3.0
2 W- M( U$ K: } - >>> math.copysign(3,-8)+ ~7 T1 m! {3 H. _! o
- -3.0
复制代码
) Q. x+ F) B& I# Y1 ~0 R& [$ rmath.exp(x) 返回math.e,也就是2.71828的x次方) i; X6 ~# w9 v2 Y- R, A0 c3 \) D
- #返回math.e,也就是2.71828的x次方
5 s: }* u% z4 j# f+ n2 ? - exp(x)- y7 e, u* N2 n$ u+ Q
- Return e raised to the power of x.
3 ?: _: i& I0 _8 [
) ?, O9 i2 Y' j$ s- >>> math.exp(1)7 m. W: X `7 w3 a% R0 Q9 g. t
- 2.718281828459045
% B! J4 Y' u3 D9 T - >>> math.exp(2)
9 y$ x3 |4 X% x$ ~* @" v - 7.38905609893065- B/ @7 Q. Z; q6 Q& }2 H8 w4 R
- >>> math.exp(3)' u( X& @3 J8 r, \
- 20.085536923187668
复制代码 ' {% O6 [9 I4 C% p- y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
9 P" W: w% `( s" S- `- #返回math.e的x(其值为2.71828)次方的值减1
/ B9 t0 a3 F( F) M0 @0 y. i% |" _ - expm1(x)
* s0 J4 W* |- q' i5 ]" z) X. }8 ~ - Return exp(x)-1.
$ i+ M4 w/ [7 X! G1 H4 Z - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." f1 A+ t& W- O
( K! k3 H7 p: y$ s+ R6 a, W- >>> math.expm1(1)- D. M4 C* F+ r0 Q! _
- 1.7182818284590455 X/ y9 q7 A7 V3 F5 S
- >>> math.expm1(2)" d: P: L& J$ c9 H
- 6.38905609893065+ u9 c2 S8 }: ~! X" N
- >>> math.expm1(3)
; e' p+ @' n' G: W% Q7 Z - 19.085536923187668
复制代码
- z7 r2 M& V8 z3 W! m! bmath.fabs(x) 返回x的绝对值
7 K4 Z, l1 A* z4 w5 {) y- #返回x的绝对值' @: U. H" z9 ~- @: v& L3 @/ m
- fabs(x)) h8 A$ O! `& p" S/ V
- Return the absolute value of the float x.! e0 H* w i/ R- _* j7 f
" B g. J& B! X$ u0 E: P- >>> math.fabs(-0.003)
6 P: X+ L9 C- ?2 S2 v - 0.003. b2 [- }+ h% y1 S& ?$ L, n* j
- >>> math.fabs(-110)' P" Z2 X) ~5 S$ V( l4 v
- 110.0
& O$ R1 m+ z: b, W6 w! E - >>> math.fabs(100)
9 }/ o+ b+ q4 U& ?" x) _' R- u - 100.0
复制代码
. S' }% Q$ Z- n3 P: n& v$ E. Z. zmath.factorial(x) 取x的阶乘的值% k+ Z, X* H1 |8 t0 G
- #取x的阶乘的值# n" J# L" C+ b. L9 T9 u- D
- factorial(x) -> Integral! P" O% [6 \, Q8 G- @& X5 r
- Find x!. Raise a ValueError if x is negative or non-integral.
; o) p# Q3 E( g6 z* E% V - >>> math.factorial(1)
0 |2 Z' @' h5 A% H7 ` - 1: H8 c% V i& R# J, M
- >>> math.factorial(2)
* g3 v% ]' q" m3 N% v* V - 2( ~2 W! \) |' D" ~$ |) j; B
- >>> math.factorial(3)
* p, H5 Q- j* Y; x! C - 6
0 S, m% ~6 r4 ]/ y# p' X1 h8 T - >>> math.factorial(5)8 C0 G# B% L. S+ w! D; Q A; r
- 120
D# |8 o5 K9 v0 \! O! u - >>> math.factorial(10)
; o0 v( V: W- _# n5 M7 { - 3628800
复制代码
5 A' ~, d( }* V+ r7 ?0 Jmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数# g% Z& K- L. |" O/ i1 K
- #得到x/y的余数,其值是一个浮点数
& w7 G6 Z8 Q3 P% @4 V9 n3 x - fmod(x, y)
4 p1 C" f/ P( ] - Return fmod(x, y), according to platform C. x % y may differ.
' x* z) p; o2 M' S) ?3 Y - >>> math.fmod(20,3)+ |6 b9 f/ r% x2 a4 v
- 2.0& A, V; _8 ]0 V- x1 p/ Y" C
- >>> math.fmod(20,7)
! P* g# ^' h9 ` - 6.0
复制代码 ; M: C/ k/ k% D3 S) p6 T
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 B; q( |1 g( m0 \- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' }! t: ]. c; A/ |* J2 T
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
. D8 t4 ?4 T$ e2 j# T - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
" p3 ~% b; O7 s8 b1 a' E - frexp(x)+ a8 F4 J% m# ?0 Q0 M, {/ ^5 E6 i
- Return the mantissa and exponent of x, as pair (m, e).
- G* Y3 k1 y, Y6 Y0 z7 u; h8 f - m is a float and e is an int, such that x = m * 2.**e.
' ~7 k) v# c8 a; @3 Y% W; Q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
2 h" j0 X1 h1 Y- U" W - >>> math.frexp(10)
; {" K7 Q$ K P2 r - (0.625, 4)
. t4 }. H- P; S0 \ - >>> math.frexp(75)
6 m0 U1 ~8 A8 E; l7 Q - (0.5859375, 7), p* z! a/ y. C) M9 |
- >>> math.frexp(-40)/ ~. u2 Y! f2 Y( i- z
- (-0.625, 6)4 x P1 V& L3 m5 A# g$ Q
- >>> math.frexp(-100)
0 \: V5 W8 ~# T/ K. A0 r - (-0.78125, 7)
: l* y1 F& P! z - >>> math.frexp(100)$ V \6 x% _9 S5 \- \. E! O7 @' B
- (0.78125, 7)
复制代码
' V* P( L' r2 T4 j2 a, ]math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), R" ^" t) \0 n2 l
- #对迭代器里的每个元素进行求和操作
/ `! ~0 H2 a1 e$ T U$ ?/ X - fsum(iterable)
% \$ t) S8 d& c - Return an accurate floating point sum of values in the iterable.+ y7 U; w: z6 w$ r+ T
- Assumes IEEE-754 floating point arithmetic." A" P% }3 ]/ ?
- >>> math.fsum([1,2,3,4])% s+ S" x% `) R7 K
- 10.0
3 r; \( Z3 I; @ - >>> math.fsum((1,2,3,4)), B* w0 C! Q# n, {: a
- 10.05 g; |3 w& d2 x' d! M1 B0 `- i( N0 J
- >>> math.fsum((-1,-2,-3,-4)) v1 l; i5 {/ g/ w3 w
- -10.06 F1 I5 A/ k& a* K4 w! U
- >>> math.fsum([-1,-2,-3,-4])
8 v' ?# T8 c0 J - -10.0
复制代码 ; z# d) O" S: S$ V- J. _, {
math.gcd(x,y) 返回x和y的最大公约数
# s C3 { J! t& L# A* k) ^9 l- #返回x和y的最大公约数
# r Q$ F/ j6 S0 x - gcd(x, y) -> int5 K. W& x7 P5 B# S# z; }% A
- greatest common divisor of x and y9 z- z# ~8 `" r4 m' F
- >>> math.gcd(8,6)
: n$ P' Y% C( W: ^) T1 b+ N, U6 z - 26 [0 _5 }3 ?& B* t8 A
- >>> math.gcd(40,20)2 a' S" g& m' B0 M q
- 20
) }, }# f/ m% R" a* T - >>> math.gcd(8,12); S% l9 G E' u/ v
- 4
复制代码 # u3 `* ^1 |& b. W% s
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
4 ]6 r' J& _, r3 ?- #得到(x**2+y**2),平方的值
+ E- ~) T# |, ]) a" C5 D - hypot(x, y)
% N/ {8 Q( I+ C1 ]% Y0 k - Return the Euclidean distance, sqrt(x*x + y*y).5 R: C+ A! ^8 w% ~
- >>> math.hypot(3,4)
" a: |5 Y; d' |/ A& L- s! d - 5.0% o; R/ S9 Y- M2 _6 F; V
- >>> math.hypot(6,8)
4 s1 `/ F Q( \+ s3 R& ^; u - 10.0
复制代码 $ F& w8 x& o# z. |3 F3 i5 Y1 Y/ J
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 l$ n. q6 ?$ D: g: Q( u# S- #如果x是不是无穷大的数字,则返回True,否则返回False
% U4 A# t, e6 Q- o% t - isfinite(x) -> bool
8 t; _* d. ]& _) F; w - Return True if x is neither an infinity nor a NaN, and False otherwise.1 X8 {0 n% s X8 ?' h: L+ E
- >>> math.isfinite(100)9 H( ]/ D! j* ~' k# v! V, _; }
- True& ~7 k% m' Z+ A
- >>> math.isfinite(0)
% r5 ~' K3 j) y! n' m+ K l - True: ?5 l$ n3 C3 Y8 R- E
- >>> math.isfinite(0.1)
' u) Q" g% @8 c3 _/ a - True8 r5 q+ d8 W ?
- >>> math.isfinite("a"); |. h2 F% p% P
- >>> math.isfinite(0.0001)7 f" @( f3 R! P# J6 z- I2 S
- True
复制代码 ) a5 I4 g) F' D# u3 y# j
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False) T9 }) ?! u( Z$ Z0 U* d) h
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
9 X4 {# g+ i( e1 v - isinf(x) -> bool
+ T1 v; i" y+ g* [* c - Return True if x is a positive or negative infinity, and False otherwise.
; o2 Z" u* A J* _, o - >>> math.isinf(234)
; x4 S! V1 e2 q8 W C' ` - False( m* H4 `3 A" z" Y
- >>> math.isinf(0.1)7 Q' n* c! `# T
- False
复制代码
6 L3 W( Y& W+ gmath.isnan(x) 如果x不是数字True,否则返回False+ {7 C# J* _# I; k
- #如果x不是数字True,否则返回False- ], r, j% o; v0 Y) Q8 Z6 _
- isnan(x) -> bool
2 z/ H- O! q Q: I$ | - Return True if x is a NaN (not a number), and False otherwise.6 Y* ~* ~3 b* P6 W2 P- Q' r b
- >>> math.isnan(23)' p" B/ }+ ]8 B/ ~, e3 U; [
- False
2 k! E# m! Y5 v- L$ |8 y) J - >>> math.isnan(0.01)' U( D( u; Y4 o. a+ H1 ~" L
- False
复制代码
4 u$ K3 K+ e4 hmath.ldexp(x,i) 返回x*(2**i)的值
# \& x; z: ~; g$ v T- #返回x*(2**i)的值
5 a3 }# w: ~6 g! W9 w- t# L - ldexp(x, i)
7 W8 N2 H6 {- h% E9 S! @. ^! o - Return x * (2**i).) P) Z- e' E6 N
- >>> math.ldexp(5,5)
# q% Q7 t/ j$ @$ K% _ - 160.0% t% F5 c# g6 @7 k) W
- >>> math.ldexp(3,5)
3 ^% h1 ^* Y8 z - 96.0
复制代码 . K0 |- }- Y9 C q
math.log10(x) 返回x的以10为底的对数
+ F3 C$ V" Q8 I7 B0 L7 k- #返回x的以10为底的对数
, c1 _$ s0 o4 [, O8 l# y4 p# R' V \/ F - log10(x)
6 Z& K( b% c, o7 p - Return the base 10 logarithm of x.
# z, P+ Q+ ]. f3 Y" r S' p - >>> math.log10(10)
: H( T9 y/ E, o6 r - 1.0% ? b$ G% L$ w8 k
- >>> math.log10(100)2 U; k3 s4 c$ a+ ^ p! X/ t
- 2.0" h" n& F1 X2 A
- #即10的1.3次方的结果为20
. ^0 R/ D- W R3 e6 j: Q' X9 X - >>> math.log10(20)
6 C5 k4 [: e1 R7 i3 g: B/ Z; l B- n - 1.3010299956639813
复制代码
7 w W/ S5 ~4 Q' X& @5 I( ^: M Ymath.log1p(x) 返回x+1的自然对数(基数为e)的值; Y' x) S- S' r- B. G( W
- #返回x+1的自然对数(基数为e)的值7 S( N. D+ l- R, S7 o
- log1p(x)' q C' E3 o8 Z
- Return the natural logarithm of 1+x (base e).- ^* x: b/ B2 O- ~9 L# E4 g4 X0 v
- The result is computed in a way which is accurate for x near zero.9 Q- R' J2 D# `5 @( U* k
- >>> math.log(10)
% \, R8 i# ^7 T% B4 g, ^" ? - 2.302585092994046
, W9 P$ i: R" m& f - >>> math.log1p(10)3 X: {: w8 m( K/ A9 i' t( n5 u
- 2.3978952727983707. ~+ {; f4 F. N. {0 T. p3 ?0 o
- >>> math.log(11)1 @" R& b1 c; p$ i! f7 |- d3 [
- 2.3978952727983707
复制代码
* L# U" m7 v- Q: B' jmath.log2(x) 返回x的基2对数
# F; q8 u0 s5 s. c2 r$ [/ m- #返回x的基2对数4 S3 V5 R: t; S0 A) G
- log2(x)
# T2 O* |$ e+ v) e - Return the base 2 logarithm of x.1 ]' Z2 h; p* G
- >>> math.log2(32)
" C" c5 J3 {8 w - 5.0$ v% @5 l- P# h1 L4 s( Z
- >>> math.log2(20)" g. h$ ^9 L8 ]* D1 |3 W, u( b
- 4.321928094887363
8 g' X- T+ F' S$ f7 i! Z - >>> math.log2(16)
. p! N/ ]3 P7 C! y8 S - 4.0
复制代码 / q3 J+ r; ?/ i1 H5 T% G
math.modf(x) 返回由x的小数部分和整数部分组成的元组9 d6 x2 T ?8 n( M: G! x
- #返回由x的小数部分和整数部分组成的元组' D! h# p( j5 A* \! q. N9 @
- modf(x): |" O% \0 A! y2 \ z5 [9 L3 `% Z
- Return the fractional and integer parts of x. Both results carry the sign8 n" `1 a \4 u' w* \
- of x and are floats.
& A% w" b) D- e/ D - >>> math.modf(math.pi)& [8 z* z' [# q- m
- (0.14159265358979312, 3.0)/ P3 H2 p l$ A; Q* r- }
- >>> math.modf(12.34)
" M1 n! W) {" i; v" m - (0.33999999999999986, 12.0)
复制代码 - \) D* W5 g! O3 K A
math.sqrt(x) 求x的平方根
. q5 ]' O3 j- p) n: D- m- #求x的平方根& c; _* _4 b! V& i
- sqrt(x)4 @$ p) a( \% D) c
- Return the square root of x.: z7 q+ d7 o' p0 q9 B) L/ L
- >>> math.sqrt(100)4 [! ]$ h) h4 ^6 a
- 10.0) g8 K8 f# A) F- g- K+ v0 T, S, \" y8 F/ S
- >>> math.sqrt(16)
1 `, C m) h6 l( a - 4.0
- V9 W& }7 E W - >>> math.sqrt(20)# k5 S7 j. M# i% D
- 4.47213595499958
复制代码 6 ?+ V2 u I' a7 n$ m
math.trunc(x) 返回x的整数部分- #返回x的整数部分) \8 b2 i$ l" q! Z
- trunc(x:Real) -> Integral
% ~4 v& C. I0 ^* q$ m: c, q' M - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
8 w7 _1 s% s) v6 g& W - >>> math.trunc(6.789)) y2 U& g/ j- S4 G8 @4 C
- 6
- m; ^( |' N y i% n - >>> math.trunc(math.pi)- I1 X+ I0 U8 ~- q3 c5 {/ i- t7 _
- 3
: N, V6 m( f1 |1 O, t% X6 _) [4 |9 L, X - >>> math.trunc(2.567)
1 s& U1 V* d. p: M3 I; g% T: _6 q! y - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|