马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ c2 x* E5 x0 \5 J【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ q$ k+ Q' V* c: c
3 J1 N! q2 n5 R. S. i0 ~方法1:
4 k0 J% s) s% b- >>> import math. Y0 G0 @7 p, ]. w5 v
- >>> math.sqrt(9)
; Q+ c% r4 y) V9 e! {/ O# z3 H - 3.0
复制代码 方法2:
3 g; Q" D3 d5 N0 n4 n# k/ B5 L- >>> from math import sqrt
% J! b" R" a, t: Z& E - >>> sqrt(9). M! r8 M) H9 Y* N
- 3.0
复制代码
5 }" S% S$ s9 o
- e# y6 D2 {3 C3 L2 ]& n% Umath.e 表示一个常量
) N5 [$ F- ~9 s8 H5 K( M- #表示一个常量
8 p. h6 [ C% a! Z$ F7 ]& x - >>> math.e
& H; b' E& a# K4 b7 S3 U! Z - 2.718281828459045
复制代码 ! z" | j G. m3 u+ H- ~
math.pi 数字常量,圆周率
& n b6 C" M i% V X- #数字常量,圆周率
6 c$ |( o' z" b% G0 [- ^! n - >>> print(math.pi)4 q+ C' m+ T! y& R( E
- 3.141592653589793
复制代码
6 q1 _; p0 I& Nmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 Y y. c4 Z: t `8 ~! y- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ O' P7 T) E1 Z5 Z. v+ d5 q - ceil(x)5 s8 F3 D" h. V
- Return the ceiling of x as an int.
- ?/ t! \0 ~2 | - This is the smallest integral value >= x.
) M6 f$ M0 u" X7 x8 A6 b$ A
+ p v% C! \: Y3 x# r3 r- >>> math.ceil(4.01)
* K7 s# T8 w% W0 c' q4 l% s5 v5 ^ - 5: o7 C: y* |1 x' C4 P
- >>> math.ceil(4.99)
- c. ^- l$ L* _' G5 r4 {- s% ^ - 56 G; K- p0 M" Q1 a8 {- @6 m
- >>> math.ceil(-3.99)
! h& t" T/ a& S - -3
% ~: L$ `0 }; P( b- j" `8 _9 Q - >>> math.ceil(-3.01), g& R+ Q: j- X$ Q
- -3
复制代码 7 q p: q* W% S% H
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身! q7 k, E$ r8 `6 P1 g
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 n T2 X( B6 ^+ T1 _8 I$ V9 ]$ K- \8 j
- floor(x)3 i( g0 X; ]9 c
- Return the floor of x as an int.
8 c0 g& m9 x- P3 p5 D1 t - This is the largest integral value <= x.
! Z; R6 ]1 J% _$ F - >>> math.floor(4.1)' w6 J* _, X& x) v8 F
- 48 [: O! W, k6 v" Y
- >>> math.floor(4.999)9 R$ B4 Q3 j# O# C/ Q2 R7 @ z
- 4
. O$ g1 H2 {: v3 K. e - >>> math.floor(-4.999)6 E C$ G4 w. j
- -5- z( O! A4 U$ w$ l! D& Q2 _7 W) N
- >>> math.floor(-4.01)( k; j) \* C9 J+ P: W0 g- z6 t/ f
- -5
复制代码 3 B. a/ t- u: z8 q
math.pow(x,y) 返回x的y次方,即x**y, M! ~+ j9 i+ l _3 E' B9 P. F' V
- #返回x的y次方,即x**y
+ V6 E! V5 e- z; w% ~# q" U - pow(x, y)0 q) N' w$ x+ V5 J4 ~
- Return x**y (x to the power of y).
! Q% ?. g3 }- C9 O8 G: Y2 ` - >>> math.pow(3,4)
5 M' m/ I+ l9 c" ?3 a8 s" ` - 81.0" J$ t8 e9 X; i8 y
- >>> " n& Y& g. G" }) m2 n$ o
- >>> math.pow(2,7)4 e2 Q# ~' I1 L( c; N
- 128.0
复制代码
; L y6 k" e# B- C3 t/ G6 Pmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 x0 ?( K! M2 y- I" R! s5 n- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 L3 ^+ n6 m' B' x' B - log(x[, base]). Y; g' E4 H ^) d/ T3 u
- Return the logarithm of x to the given base.
* w* c- C3 A, z+ Y( t) [ - If the base not specified, returns the natural logarithm (base e) of x.4 R/ e9 r/ X+ r& E y
- >>> math.log(10)( \$ g7 Q2 m+ Z5 _ p/ Z% W5 u
- 2.302585092994046
: Q' f) Z0 K# N$ j2 r w - >>> math.log(11)
9 h9 F W7 L% @# P$ n - 2.3978952727983707
( f1 b& `8 S/ z6 s8 ^! g: j - >>> math.log(20)
. E$ L" I: |, G6 @# a3 Q5 J8 M - 2.995732273553991
复制代码 ' ?; z Y5 R' b5 m0 W$ F
math.sin(x) 求x(x为弧度)的正弦值& d9 i' Y: B# p5 A% d4 }
- #求x(x为弧度)的正弦值7 D# h* y1 s) M8 k! A" ?
- sin(x)- P- l2 l% h3 Z# Q# \) P/ O. O
- Return the sine of x (measured in radians)." ?$ X: w; z! C
- >>> math.sin(math.pi/4)
' K+ L( K C# h* |- e - 0.7071067811865475
" Z# g( L4 d0 r" O, J - >>> math.sin(math.pi/2)
- [, q$ h7 n# x$ Z5 v6 C) ? - 1.0
4 Z, `2 @$ O* w9 z4 L - >>> math.sin(math.pi/3)
9 F7 m( M9 H z H v8 U - 0.8660254037844386
复制代码
) U1 s( \+ f U, Wmath.cos(x) 求x的余弦,x必须是弧度: j2 T- h: E+ }& Q
- #求x的余弦,x必须是弧度
$ C4 ?8 K0 h4 \- d - cos(x)! z; m* L: n, v( h$ Z" D3 x
- Return the cosine of x (measured in radians).) ]* Q; O2 i5 m+ G# G
- #math.pi/4表示弧度,转换成角度为45度
9 u% |4 n5 P/ }0 T, U5 y% p - >>> math.cos(math.pi/4)
( j4 a4 \0 E7 D0 U% J7 O# } - 0.7071067811865476' V! h, S: }* W. Y
- math.pi/3表示弧度,转换成角度为60度1 E% @2 ]8 q; j; W& i
- >>> math.cos(math.pi/3)
) d+ Q. _& R% P/ [ - 0.5000000000000001
3 n6 B$ {1 b$ P H, c( r6 ^; ? - math.pi/6表示弧度,转换成角度为30度
* j9 }* ^/ e* U - >>> math.cos(math.pi/6)$ ^+ k. Q% o3 ^: a" a" r
- 0.8660254037844387
复制代码 $ }! ^# @$ Z" b1 c: n7 `3 Z
math.tan(x) 返回x(x为弧度)的正切值& |$ E, Z8 q7 A
- #返回x(x为弧度)的正切值6 K. s0 R* u) b K
- tan(x)
5 y% O8 B' v. D& H# o - Return the tangent of x (measured in radians).3 \/ a P2 X7 W9 k1 Y7 [& Z
- >>> math.tan(math.pi/4)
; E: M- c! K2 ~! F8 J% M! C - 0.9999999999999999. r5 J! ]" ~6 r- X2 u2 {
- >>> math.tan(math.pi/6)5 O8 o3 r y5 s
- 0.5773502691896257
; t ?6 Z( C z- A2 b3 S. r, Z8 C - >>> math.tan(math.pi/3), x# x H: Y* F- y; f
- 1.7320508075688767
复制代码
+ W( J* C5 ?9 D" e- M5 W/ wmath.degrees(x) 把x从弧度转换成角度
* w) n" O% Y6 P/ s- #把x从弧度转换成角度
% e, O9 l$ t3 [0 N0 r8 o8 i - degrees(x)
2 x- H( t+ t* V& W. A; R - Convert angle x from radians to degrees.1 ~8 D5 m q4 N$ S! M* F
- 3 L& X( v* I/ K6 {; W) X
- >>> math.degrees(math.pi/4)
0 u/ N* t. ]) V/ t) D7 W2 p' U - 45.0, t3 z& ?2 H, S0 K6 J- ~
- >>> math.degrees(math.pi) G8 A3 d; d+ M: L: H, S3 Y% A
- 180.0! w; Z+ S% {; Y8 x$ a
- >>> math.degrees(math.pi/6)
$ Z% O. U8 `: x ~; k - 29.9999999999999961 E& p: ]4 J% d6 M( a
- >>> math.degrees(math.pi/3)( [' A, _" [3 J8 w& w6 h
- 59.99999999999999
复制代码
6 _$ Y4 [1 k/ T4 N& emath.radians(x) 把角度x转换成弧度
1 C# V. l. q# C3 J5 q- #把角度x转换成弧度- x- {8 Z9 y! E& e1 l
- radians(x)! T, k5 S! p; ~- O
- Convert angle x from degrees to radians.# J2 r5 ^( _9 Q) d l+ u
- >>> math.radians(45)$ r" ^& {& Y0 N& H
- 0.78539816339744839 p' L& V7 [, K& G# l
- >>> math.radians(60): N6 B h) f) ^0 D' {
- 1.0471975511965976
复制代码
? I3 h- v# ~% O( Nmath.copysign(x,y) 把y的正负号加到x前面,可以使用0. ]1 p# v; s: F) {
- #把y的正负号加到x前面,可以使用0
* o9 W! w+ B8 e - copysign(x, y), o5 V- y; r+ K% w
- Return a float with the magnitude (absolute value) of x but the sign 9 T0 E/ {; U9 h w, q% d( m
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( o9 _- K. {$ M& J# A# S/ }+ `" u2 U
- returns -1.0.
; c! v4 A% }% C6 v5 F
. I3 N6 l( Z" S- B7 U/ u- >>> math.copysign(2,3)6 |- B J/ i. ~# k/ m
- 2.0
# p ]7 C2 q+ r6 x: `/ J7 B - >>> math.copysign(2,-3)! x, m% e+ J/ v! e
- -2.0
J3 I* Q, i0 H) e. b- A - >>> math.copysign(3,8)
* t- k5 Y/ ?6 {6 j8 Q2 H2 D i - 3.0
$ z% B3 Y6 G, a2 j - >>> math.copysign(3,-8)
" k O2 n v' R) ~ - -3.0
复制代码 6 S7 e. U0 c) f. y& ?5 Q8 ~
math.exp(x) 返回math.e,也就是2.71828的x次方7 C9 k9 K8 U/ N$ d7 c5 j
- #返回math.e,也就是2.71828的x次方
" B% Q J: q) ^ x! V' D2 [ - exp(x)+ ~2 Q1 O* O2 x2 [( o
- Return e raised to the power of x.% z; a ~% K, \) F$ }1 Z* @
- + _ C' Q( }6 ]* P2 ]+ v
- >>> math.exp(1)
8 H5 ~! a: r% Z5 V7 v5 _ - 2.7182818284590452 E* _( } e$ S& e4 Y$ R) ]7 R
- >>> math.exp(2)
0 B& z* Y4 H4 d, x( i - 7.38905609893065; L' J& }- A0 l$ ]
- >>> math.exp(3)
! g: _; J6 r5 O2 P) Z - 20.085536923187668
复制代码 & a4 _ d! u( }" R3 ~
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减18 T* g& z6 Y. |2 G M# M' m
- #返回math.e的x(其值为2.71828)次方的值减14 n" H( R) G- f8 r* d o- Y
- expm1(x)
! b. H$ t7 r) ]8 i - Return exp(x)-1.
/ P0 W8 r2 [$ E/ H. m1 n - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 c/ m" K& w! ?6 B) c L
- & B; M& Y9 h% j2 H; c" A q/ b
- >>> math.expm1(1)1 S8 N0 T4 |" C0 e* m& T
- 1.718281828459045
; g% h8 o8 n! H) i8 \4 w! t5 V4 s% x - >>> math.expm1(2)- m, ^" v% Q1 m: P$ ~
- 6.38905609893065
9 a9 X! J" Z$ M+ Q - >>> math.expm1(3)3 q4 H. h- u! J& n, l' S
- 19.085536923187668
复制代码 - [% |/ q6 o2 b+ h' j: \3 Q
math.fabs(x) 返回x的绝对值
\5 P( N5 N7 W n. ~- #返回x的绝对值
4 A/ G. S4 A l$ k, \$ | - fabs(x)
& ]2 H$ B$ N1 S - Return the absolute value of the float x.
6 p5 J1 m1 z% l" K4 Y6 I
b; A) ?% N5 G2 B1 s& t- >>> math.fabs(-0.003)
$ c4 Y: X8 Q- X. l) W$ i - 0.003; u0 _* ~% w8 F7 G, d4 Z
- >>> math.fabs(-110)
$ W# R5 X1 h# T - 110.07 u" u& ]7 n4 c: A" n/ y
- >>> math.fabs(100)
3 F* d: ?1 H% `2 r7 }7 d - 100.0
复制代码
) D1 L5 ` ^# y" G* d1 Lmath.factorial(x) 取x的阶乘的值
# ~- q/ o) {/ M* H! V- #取x的阶乘的值
% s; }+ K* h6 y - factorial(x) -> Integral
2 L, J# k' C* ?& L - Find x!. Raise a ValueError if x is negative or non-integral.
- U: K# R+ @ a" S1 s | - >>> math.factorial(1)" h$ N8 v" t. z9 v
- 1
" G6 I# W3 G" Z: @" f# L, ^ - >>> math.factorial(2)/ Z* y/ d$ \, @/ H0 c
- 29 L6 j O2 u2 E% B
- >>> math.factorial(3)( [& X6 R- U7 o8 ?+ y. Y
- 6
/ J- Q# ?$ _5 K3 b& z( j - >>> math.factorial(5). b* Z$ S% _, B; ^. Y' ^6 `6 A
- 1203 C1 d6 j9 U9 u/ V
- >>> math.factorial(10): A9 L' R4 a* w. h* |7 l
- 3628800
复制代码
5 J# ]) T K) X8 zmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
) C$ O. i6 Y. d# c- #得到x/y的余数,其值是一个浮点数1 ^0 v0 } H- Q
- fmod(x, y)
; z9 a9 ]) p! s- N - Return fmod(x, y), according to platform C. x % y may differ.
, A7 I2 A" V3 X @' \/ B - >>> math.fmod(20,3)
- s$ a' n( E: T7 y) [! F - 2.0
% s6 y( g7 i" y' c- J: ^7 o$ F - >>> math.fmod(20,7)
! V! C" z! [2 B* c4 H! Q4 l - 6.0
复制代码
+ D0 B0 M6 z4 J# {math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! R$ J- [& `- H9 l
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 q u2 x2 F3 X1 i; B/ J) { - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' r/ i6 e) s. S
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
5 j$ m- _) ^! x& s - frexp(x)# H( L5 \- z* d5 E" w
- Return the mantissa and exponent of x, as pair (m, e).
4 Y' o! E* F- G1 L: R4 I - m is a float and e is an int, such that x = m * 2.**e.6 `( r8 ~' K7 i% o. h
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.5 N; z k9 S+ o4 N- J
- >>> math.frexp(10)
" d# x% ^6 ^$ k - (0.625, 4). p7 K% V) I, G
- >>> math.frexp(75)
" R4 |: [" x) O9 K9 y4 i; k - (0.5859375, 7)
6 Z1 V8 a! c* j' D# L - >>> math.frexp(-40)
- L. Y: E. e3 ` - (-0.625, 6)
) y; o C# C0 t% X( e" s# e' A - >>> math.frexp(-100)/ e% |* p- T7 x* D" b
- (-0.78125, 7)
" G H: ^" J. H, Q - >>> math.frexp(100); k% ?& Y* c; l3 `$ @% q
- (0.78125, 7)
复制代码
3 l2 z0 [; V, d& N rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)6 i. O9 \% F$ l
- #对迭代器里的每个元素进行求和操作
; E( u& U5 U) ]$ F - fsum(iterable)$ f H: n! E" U# b
- Return an accurate floating point sum of values in the iterable.
, O% M( }8 U4 I - Assumes IEEE-754 floating point arithmetic.. J! n4 C/ P7 X, S. b
- >>> math.fsum([1,2,3,4])
; v4 q) y S/ c - 10.0
( Z0 Q5 G* Q- [* X7 k8 W; O- x% q - >>> math.fsum((1,2,3,4))
& y$ c( @% w' s" c! b. l - 10.0
5 o4 f- p2 j$ l% \ - >>> math.fsum((-1,-2,-3,-4))
- ]- A& } p" u }+ Y - -10.0
- N2 R/ W- h6 \6 w( @% h( |! v1 y$ h - >>> math.fsum([-1,-2,-3,-4])
$ L$ h: E# M& t, d6 I - -10.0
复制代码
2 Q% ]' D" ?1 _3 |math.gcd(x,y) 返回x和y的最大公约数
- }( l1 _& I/ h+ h) N2 {- j- #返回x和y的最大公约数 R6 x9 _: t0 d0 K" Z
- gcd(x, y) -> int
! N0 n" B: ?. l& @+ {+ o9 t - greatest common divisor of x and y
4 p7 f. L3 b6 @7 [) M, @% h* R - >>> math.gcd(8,6)" _2 e. h, f+ [
- 2
: \. E$ B* ?: R - >>> math.gcd(40,20)
( ^, y. _1 o9 z/ o% ~7 E - 20# t( o. R; W: m. g o1 [# w
- >>> math.gcd(8,12)9 N) \1 l5 J$ `$ z0 Y
- 4
复制代码 ; n: a, X+ F" W2 _
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
% [+ I, u0 i+ u4 G* t6 i- #得到(x**2+y**2),平方的值
1 `( v, Y8 t( l9 c - hypot(x, y) z9 t8 {0 F$ R$ u7 J4 Z& a- n
- Return the Euclidean distance, sqrt(x*x + y*y).6 u! o; e, N7 l1 h- q4 d _
- >>> math.hypot(3,4)
) @- D N* v: ^ - 5.09 R* i" c9 D/ X
- >>> math.hypot(6,8)6 a3 d h' }% W$ d$ E
- 10.0
复制代码
7 o; e$ X2 h( n# Gmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
c! X4 r) k' G0 b1 j2 @; z- #如果x是不是无穷大的数字,则返回True,否则返回False
% j3 ?6 h+ t% x8 Y) g! a - isfinite(x) -> bool/ X+ v/ g4 r+ N9 ^4 ~% T& i
- Return True if x is neither an infinity nor a NaN, and False otherwise.5 B! Z g3 ]$ m$ V
- >>> math.isfinite(100)
0 P: M. {) p, r/ b; S% C5 q - True
; i4 i/ J, ]# R: T7 } - >>> math.isfinite(0)
p. z! ~+ g- _ - True
) ~* A3 V" s8 j% Z9 f - >>> math.isfinite(0.1)
/ n) s+ h& s7 D1 h, n/ J" b" ` - True) ?( g& T" N, s$ h* E1 @/ x3 `
- >>> math.isfinite("a")0 L4 f" F3 H, P6 I% H
- >>> math.isfinite(0.0001)* Z; {% d0 \8 g, p1 E: [ @
- True
复制代码 , t( K* n g' P' d
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
: Z6 S/ s! P$ _3 D# @ u% D! g! `- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ y+ a4 r, k F7 G" O! Q - isinf(x) -> bool
$ D$ e$ _. P* X. z - Return True if x is a positive or negative infinity, and False otherwise.
# \1 v" o! l! o: A5 ~% f - >>> math.isinf(234) z! {: |/ k2 M- v2 |3 x" P4 _3 e3 I
- False
4 \# w$ p: g' Z/ o - >>> math.isinf(0.1)
# @8 g9 V9 n B7 s) X" @( ~ - False
复制代码
; Q2 J! l9 B0 `9 smath.isnan(x) 如果x不是数字True,否则返回False, m3 }) i% |$ q. B
- #如果x不是数字True,否则返回False$ l1 v$ H1 A5 T, n
- isnan(x) -> bool
4 z8 w7 L* r1 |. u7 a - Return True if x is a NaN (not a number), and False otherwise.% g' k6 ?9 Z3 e, q5 s- R
- >>> math.isnan(23)
% t1 l; S r8 o/ c" w4 ]6 ^ - False
: k2 Y/ Y- j, q' U& G - >>> math.isnan(0.01)- j) k; w, G; w' ~, N
- False
复制代码
/ @5 g- l! e8 i1 a# v! K* emath.ldexp(x,i) 返回x*(2**i)的值6 [* ~5 a' V& `, d" @' l- h
- #返回x*(2**i)的值
* R, _/ e: t. O9 R* i - ldexp(x, i)) J& M; s) [8 x: w
- Return x * (2**i).6 `! G( p! d" l) |5 G8 v- @& R
- >>> math.ldexp(5,5)
9 \2 e: F; D" z6 l - 160.0
$ V4 U" s" \9 C, I+ D7 J6 t* ~ - >>> math.ldexp(3,5)
8 `6 L; l- _5 L7 T6 D - 96.0
复制代码 0 U, H1 ~; z# M, o; E3 c3 P
math.log10(x) 返回x的以10为底的对数
+ W9 m" w* Z0 W- #返回x的以10为底的对数
5 S4 m# k0 W( b' b6 |; |: | - log10(x)( z. G; l0 \4 Q, U n. ?
- Return the base 10 logarithm of x.1 J' n5 K% ~' v5 o
- >>> math.log10(10)% P* v! r3 ]% p- U" A
- 1.0
& E* X9 a. P5 ]3 A0 ] - >>> math.log10(100)
' u+ w2 A7 }- I: j7 M @ - 2.0
# N" d5 r) n8 \ - #即10的1.3次方的结果为205 z" r& e; ^, G+ E
- >>> math.log10(20)4 m. @- I0 D. T4 M2 L% z" r0 h
- 1.3010299956639813
复制代码
% h0 s: K$ D# }math.log1p(x) 返回x+1的自然对数(基数为e)的值
" m8 a) m7 m9 |- #返回x+1的自然对数(基数为e)的值7 M5 Z. P5 l' T( p3 l
- log1p(x)" n) A* t* y! p' S0 ]5 I
- Return the natural logarithm of 1+x (base e).2 b+ X5 m9 ?/ x: U! p
- The result is computed in a way which is accurate for x near zero.
; W7 h# K' r- m9 ? - >>> math.log(10)9 H& k" e- S1 M
- 2.302585092994046
3 T( y4 R' ~4 ~6 G) l$ y r0 U, y - >>> math.log1p(10)2 d, @9 ?7 m+ z8 G: r; |
- 2.3978952727983707! g( B# g" R3 G1 L
- >>> math.log(11)+ Q+ t0 Y6 B2 b7 F B
- 2.3978952727983707
复制代码 , d; ?8 O X6 o9 G V5 X
math.log2(x) 返回x的基2对数
$ ]/ L" X* j7 n7 r- #返回x的基2对数
+ o3 z. s$ n6 X2 a - log2(x)8 C" i/ g) s* r
- Return the base 2 logarithm of x." b% P5 {" M6 X- ?& n. [4 o9 H
- >>> math.log2(32)
2 ]# ^# E- w7 n2 j% j \+ Q# u - 5.0$ b% K5 ]" L3 {7 X# n' C" Q" y
- >>> math.log2(20)
) }; ~* z% t9 x7 Z" b9 @0 r - 4.321928094887363$ f# B4 j. K O! r$ R' ?# N
- >>> math.log2(16)
# ~" S3 t' s) x6 l - 4.0
复制代码
& a: K. r" v# G( \6 Kmath.modf(x) 返回由x的小数部分和整数部分组成的元组
6 C7 i" I: H0 J$ h0 x k- #返回由x的小数部分和整数部分组成的元组
5 k! j) y' C& U/ t, ~ - modf(x)
7 I* B2 ]% ^" u5 |5 O9 f - Return the fractional and integer parts of x. Both results carry the sign& A; h! ?! t" H2 V
- of x and are floats.. ~: ]% a8 g" K! D, W
- >>> math.modf(math.pi)& U+ G% ^1 g4 G9 X! K
- (0.14159265358979312, 3.0)
. m+ d" j' x5 T9 G - >>> math.modf(12.34)0 {6 D$ B8 K2 t2 _
- (0.33999999999999986, 12.0)
复制代码
; H5 M- s$ c( A9 ^6 B* |9 M9 Wmath.sqrt(x) 求x的平方根& d* `* `" o- T
- #求x的平方根
* L7 i3 _5 X' M+ f# C - sqrt(x). F3 [' E1 h) J5 @
- Return the square root of x.4 H4 B5 }! V6 X
- >>> math.sqrt(100)
5 l: a, T5 y9 w4 h w - 10.0
9 H0 o2 f& }- L; Q# ]+ K - >>> math.sqrt(16)8 L$ z; M. V, \) ?
- 4.0
8 I0 Y5 R$ D. o5 I - >>> math.sqrt(20)
7 ?( Z2 z, n: c. u- K" n - 4.47213595499958
复制代码
- Q1 O1 h, t, V/ A6 a/ Lmath.trunc(x) 返回x的整数部分- #返回x的整数部分1 J' j1 M4 ~+ x4 R: Y) h9 ]
- trunc(x:Real) -> Integral: L4 s: a' j8 w1 v7 r3 Q3 |& U& [. \
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
+ w( _! s% m& r7 E1 w/ B1 X% C - >>> math.trunc(6.789)
6 d) x7 K+ U8 `# d - 6
) v7 v$ _" l4 M( d( ~! J - >>> math.trunc(math.pi)
; u; }+ L2 N. j - 30 U5 [4 u9 o$ S) M, ^) S
- >>> math.trunc(2.567)
/ k( v9 D1 E3 L" `$ k0 U - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|