马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
' v% A4 G; N7 N. I# p3 S: O/ w% E【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
% k' e, k# S; j$ y- u: k
+ P j0 Z+ g8 P1 H方法1:: G) U/ B+ Q( ^; |9 a
- >>> import math8 v8 c/ z) p& B4 |) p- B% m
- >>> math.sqrt(9)( @1 w# N! F. B+ f- V( Z! Z
- 3.0
复制代码 方法2:
: i7 W9 ^5 D" r$ p. r- >>> from math import sqrt) i9 n7 V# ^; J) F
- >>> sqrt(9)
' k" N: Q1 }7 y - 3.0
复制代码 $ M2 c* {! P s6 X
4 }+ P& L' X2 h3 D4 j X+ a
math.e 表示一个常量. l2 K, e8 ^3 z4 o
- #表示一个常量9 }8 ?! ?' Z" h) `* I) [
- >>> math.e
% Q4 @5 _, F) R3 C% M# A) B2 |9 Q - 2.718281828459045
复制代码 ! t" ~+ L& g- p2 B: G
math.pi 数字常量,圆周率4 J3 q: c7 B2 o) K$ u
- #数字常量,圆周率6 ~$ ~1 }; C5 c7 U
- >>> print(math.pi)- e9 ?, S0 f, t$ {. M' m) }
- 3.141592653589793
复制代码 . X! a2 M, _ c* j6 [1 K; R% h
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x1 D! @5 ^$ _4 _* k* _
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 o5 H; {7 v+ R5 l" y$ D - ceil(x)& c9 q2 Z, I4 J) k% l& I7 B' {% G
- Return the ceiling of x as an int.
. O! ]0 f0 F4 E3 J - This is the smallest integral value >= x.
0 l9 j5 e" D4 u h [
0 u. ~$ k9 o' ?- >>> math.ceil(4.01)4 D" w+ \9 L7 t2 q, M/ d% }
- 5
7 U S4 Z, s0 _0 ] - >>> math.ceil(4.99)
2 C. u$ d) J- M) d. V - 5: H3 y& g! l ?) i0 x( k( t
- >>> math.ceil(-3.99)) z' w O# F. [* r6 t& H
- -3
0 M3 A0 r% Q1 s3 P c8 U - >>> math.ceil(-3.01)' g+ G0 {8 a# l1 H3 H9 p* j# D
- -3
复制代码
6 i; F, e, [/ j# f0 a3 p I* T3 h0 Amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( {$ H5 B# e' e: q, j, _5 A6 W7 J* Y5 y) _- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 h, V5 ~/ u& Z5 `. i3 U0 b5 Y
- floor(x)5 ~2 Z* S* ]& L2 S# q, [5 o/ M
- Return the floor of x as an int.
\9 a K$ a0 O - This is the largest integral value <= x.
% d- _' B* T" U1 ^ - >>> math.floor(4.1); H1 ? ~+ q" ?
- 4
$ g/ a: X' [1 \" i' G. Y - >>> math.floor(4.999)
4 F# l( S5 t" H5 b' k3 i - 4
1 @ |7 l6 {6 d! O) v o - >>> math.floor(-4.999)
I, S' g0 _1 N2 g( q- Y; R% q - -59 f6 i% N' f, l8 D
- >>> math.floor(-4.01)$ |) `8 k+ ~% O2 D7 b
- -5
复制代码 # ?- k( }8 n# k8 M0 b
math.pow(x,y) 返回x的y次方,即x**y
0 c, m" |7 N; L- `) o! s- #返回x的y次方,即x**y. v9 s, D! o( k$ \( U4 a- @/ K# G) U
- pow(x, y)
7 P0 \0 Q% Y% o$ p - Return x**y (x to the power of y).! }- d) x7 P D4 o/ g
- >>> math.pow(3,4)) B; \+ `+ C0 P
- 81.05 }$ n- s+ G4 C
- >>>
- y! I; j: q' U s: ?0 l2 \1 K - >>> math.pow(2,7)
/ W* h: u, f- Y4 U x - 128.0
复制代码
0 e: u! l j9 n+ [7 xmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% H2 f- q, F* }
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 @' _5 |% r, z" E* I - log(x[, base])* M) d7 y2 j/ Z* c
- Return the logarithm of x to the given base.
& l2 [- V- }* R. i' \ - If the base not specified, returns the natural logarithm (base e) of x.5 n% h8 A5 t: ~/ t! v. C+ W" u
- >>> math.log(10)
9 @$ o) c& U( J5 \8 P - 2.302585092994046
! v# X1 H: ]5 z! l - >>> math.log(11)
! u8 j! ~1 V2 a' \& z- M* O - 2.3978952727983707
+ F* o1 ?# `3 N- y - >>> math.log(20)
5 q2 S2 \2 H6 F6 f! X - 2.995732273553991
复制代码
& h1 I! {1 f3 wmath.sin(x) 求x(x为弧度)的正弦值, o! z! t# W5 g0 V" P+ z
- #求x(x为弧度)的正弦值
4 \) ]& ?( k) z" ~ - sin(x)
: d! N) X5 Z) ^" v1 s" X+ k/ o - Return the sine of x (measured in radians).
0 w X# L, i4 w2 `7 y+ [- r - >>> math.sin(math.pi/4)
0 g. v) [* R5 V, R - 0.7071067811865475( E- f$ o) \* r' c: R: B) O& G
- >>> math.sin(math.pi/2)+ C3 h+ B) f" d9 ^
- 1.0
9 s% Z' v J* g/ T* {9 z9 u - >>> math.sin(math.pi/3)
8 U# X+ z. {9 G; M - 0.8660254037844386
复制代码
. d, \9 P- I: L0 y/ ^: ]math.cos(x) 求x的余弦,x必须是弧度
+ j% @1 Q' r( P- #求x的余弦,x必须是弧度$ a& v1 A7 @' B4 ?+ R& H2 B: e+ o2 o) {
- cos(x)
' X# l0 |. ~; ~/ `8 U4 p/ v6 s - Return the cosine of x (measured in radians).
' o% b! c" l( E0 ~( }6 x% x - #math.pi/4表示弧度,转换成角度为45度3 x8 g$ J9 E; Z
- >>> math.cos(math.pi/4)
0 z4 {$ \1 I1 Q" t: |' a - 0.70710678118654767 b; ]( Y" e" D3 ~$ [8 F$ R0 W
- math.pi/3表示弧度,转换成角度为60度
, w: v, B0 t& b - >>> math.cos(math.pi/3)
; g! |* G8 s3 b& \' _ - 0.5000000000000001
+ O' m5 `* F4 `0 E" p2 }& p: K% |2 @ - math.pi/6表示弧度,转换成角度为30度
1 l+ Q7 P4 d1 p' l8 p - >>> math.cos(math.pi/6)* O( m! [# w) o1 M. I0 p% ?
- 0.8660254037844387
复制代码 3 Z/ D% F! M! S6 j) L/ s
math.tan(x) 返回x(x为弧度)的正切值4 j4 N( C/ j& j/ S
- #返回x(x为弧度)的正切值, Q3 b0 O2 O. S" c
- tan(x)' Z5 I- {2 S ^# h" Y+ |7 u$ m
- Return the tangent of x (measured in radians).: ^! o2 o r* a. e+ D6 y/ r
- >>> math.tan(math.pi/4)
$ a* w! z8 y# R+ S% `# x" d- C1 D6 @ - 0.9999999999999999
7 H& c4 f8 V! p8 [4 B3 K5 B - >>> math.tan(math.pi/6)
3 F$ a) g5 P" K% W" D/ `) _ - 0.5773502691896257
3 i; r9 W( a" G A- N - >>> math.tan(math.pi/3)3 k( R6 P% E8 E5 T$ O; |
- 1.7320508075688767
复制代码
; L: {$ e- p8 f8 }9 ]/ V0 c. D3 kmath.degrees(x) 把x从弧度转换成角度
1 ?) y4 k! h3 a9 c# }/ A& R: p7 n3 Q- #把x从弧度转换成角度3 o( b$ H5 f- G$ n$ ?5 ?% U2 q
- degrees(x)+ L/ l9 x6 T8 _6 D
- Convert angle x from radians to degrees.! x8 t" E( @; w
C) ^! U* F2 ~. N9 p2 @9 l0 @- >>> math.degrees(math.pi/4)
; [% D1 u+ B/ G" a/ I - 45.0$ m9 x/ v! Q/ C- A( _5 g
- >>> math.degrees(math.pi)- _7 }% ]' P. C
- 180.0( J% r; u% P& |, ~! {, ^, Z$ B/ u- X
- >>> math.degrees(math.pi/6)+ A; `% R K' Z% @
- 29.999999999999996( v. g( Q1 B! I
- >>> math.degrees(math.pi/3)
* D! ]) H" ]* z+ D - 59.99999999999999
复制代码 4 W/ T' C* J) l4 ^& s4 e5 L3 R
math.radians(x) 把角度x转换成弧度* G/ u! S. U8 T+ Z9 @1 \
- #把角度x转换成弧度
9 z5 k) L K8 B( g5 q" s5 } - radians(x)
: f% ?2 v, h. s - Convert angle x from degrees to radians.: ~6 G4 `) x" w; X
- >>> math.radians(45)
" |6 u" H' u' o( ~; \! ] - 0.7853981633974483
1 Q Q" ?# S ? |9 P7 t& { - >>> math.radians(60)
% T. ?5 z$ L6 u. L, L - 1.0471975511965976
复制代码 2 F8 e: ], {8 ^
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
j; F; v1 `6 p6 w4 ~+ O, G& a/ r9 r& V- #把y的正负号加到x前面,可以使用0
$ P# @4 R9 n" F& ~* [ - copysign(x, y)
/ [. x$ W& X! m3 T, ]' } - Return a float with the magnitude (absolute value) of x but the sign
5 G1 U6 y$ [* L- i. x6 v2 s - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
; f5 V/ p/ @+ b! `8 i - returns -1.0.0 m+ Y* u0 s$ I% N X) s
6 P% m5 j5 c l9 H+ i) ~- >>> math.copysign(2,3)
8 H+ W) v& u: D) v1 H - 2.0
, N- Y5 \( S$ B. ^; a/ _ - >>> math.copysign(2,-3)
; H& C2 c8 A$ ^9 T6 Z9 ? - -2.0
% P3 k9 @6 B& F+ a - >>> math.copysign(3,8)
$ h: }3 A0 G+ w! j$ e - 3.0
1 e/ T% z; d, b0 G p( X; o - >>> math.copysign(3,-8)! W$ {! s) O: [5 F" Q$ J2 B
- -3.0
复制代码 - S6 {8 ]2 a, J5 ?5 ]9 R0 D
math.exp(x) 返回math.e,也就是2.71828的x次方+ _: e) B8 g! V1 m5 l1 A+ C
- #返回math.e,也就是2.71828的x次方& S& c: B6 ]! Z8 | q6 Q
- exp(x)5 L7 }, \* p$ T8 w
- Return e raised to the power of x.
9 m( g) d! G1 [: k6 i b
# X/ c2 E: z: i- >>> math.exp(1)' j7 `2 q* g5 \ J* s
- 2.7182818284590459 m+ Z% V: f# J
- >>> math.exp(2)6 e* ?0 g9 ?. j* U
- 7.389056098930659 @$ [$ j9 n5 s. p
- >>> math.exp(3)
1 J b4 M. S' B$ u: u0 h t - 20.085536923187668
复制代码 5 Y% O% O/ T' F& }* B& y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
& E( ^# p! f* R' P- a# v- #返回math.e的x(其值为2.71828)次方的值减13 Y/ B O. g/ e, h; N/ g8 @5 Y/ j% [2 P
- expm1(x)3 o7 k. c9 p! g
- Return exp(x)-1.
; E/ J7 T1 O$ ` { - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; a% |4 r" ` }* ?
* E: g+ r0 G( }) |- >>> math.expm1(1)
7 f6 x+ s0 ~1 h8 } - 1.718281828459045! h2 D P' z4 Z0 T5 y7 B- M
- >>> math.expm1(2)& Q2 `: s& r5 ]3 z* |5 F/ a/ w
- 6.38905609893065* P+ `2 ]% H' E3 h0 t v9 W) ^
- >>> math.expm1(3)+ S: I; C! _: a0 C7 Q
- 19.085536923187668
复制代码
$ g$ O o+ J4 g w1 k; Pmath.fabs(x) 返回x的绝对值1 A* L4 [! e$ m2 i
- #返回x的绝对值
% ~( \" g# x& W5 S$ M# P' s - fabs(x)
# F" Z. f$ a8 u* A - Return the absolute value of the float x.
r2 u/ ]4 T; q' V- ?
6 f7 V+ x! F3 |! V! A5 N! ?* B) y- >>> math.fabs(-0.003)
# u/ P" T) x3 | - 0.003: g. _7 T! S9 n' T$ y5 T/ a
- >>> math.fabs(-110)& |5 h6 w" M* H+ B
- 110.0
) c) a8 v8 k, m9 `) u3 O - >>> math.fabs(100)$ y; g4 P* f/ g6 A5 W9 J7 r2 n
- 100.0
复制代码 5 m6 ~! _2 K( r e7 J
math.factorial(x) 取x的阶乘的值
, B2 d5 o" d+ B) P& |- #取x的阶乘的值! D4 }) S2 Z- e& i- {, T. o
- factorial(x) -> Integral6 p- n4 o% ~- p( u% y$ ?& x. q
- Find x!. Raise a ValueError if x is negative or non-integral./ X# Z8 m7 m$ I& g6 x( q; g
- >>> math.factorial(1)5 f) S; Z' S/ @7 I \' c2 \
- 1
; ]/ c, r$ ^2 A* e - >>> math.factorial(2)' l e8 X( b6 @4 u+ g: R3 w
- 2. \8 D# _+ {: s. v
- >>> math.factorial(3)* O+ ?7 F* r/ R1 y3 |% \
- 60 B* Z a+ V. \4 Z
- >>> math.factorial(5)3 N" T2 A- Z% q7 d$ z& }
- 120: h) {0 [ F" B @ f B
- >>> math.factorial(10)" r9 s i! |. t1 E9 S% t. t
- 3628800
复制代码
, |. v5 }7 D) Hmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数+ O9 u/ F0 P' u/ a3 W0 R9 n ]
- #得到x/y的余数,其值是一个浮点数' D4 q6 h3 j/ ?) P: o
- fmod(x, y)
8 {- _( ^/ a' R9 |/ p - Return fmod(x, y), according to platform C. x % y may differ.; B: Z; e" y' W
- >>> math.fmod(20,3)& T. Y+ O" L& t2 q5 g
- 2.0
" |4 B2 d4 d( {( h' g - >>> math.fmod(20,7)9 m' g& T1 C/ D: ]" B
- 6.0
复制代码
3 Q9 I- k% P- ]* w: U/ A7 u0 d; bmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
* `0 @! r$ X1 P- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 R" u3 H( \4 g, x
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 Q* }. E, v6 Q# k( o5 n: s% _
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ R+ y6 b' ^0 K+ x7 f
- frexp(x)
; p4 m$ K2 @0 o& `0 U - Return the mantissa and exponent of x, as pair (m, e).
5 `+ x8 u& v& ?* ^+ S! C) z - m is a float and e is an int, such that x = m * 2.**e.5 k$ F$ @3 \# D
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.% M3 {7 N* d3 T& e( }
- >>> math.frexp(10)0 s2 X2 `9 P! p; h. x: N& \4 v
- (0.625, 4)
+ }9 l4 S7 H1 m1 Z' V ?: I - >>> math.frexp(75)
' r l- Q0 D, Q v+ G; K* [ - (0.5859375, 7)
Y' k8 E* B& Z2 c) V4 w/ Y - >>> math.frexp(-40)
# i# M- D! x$ x4 D - (-0.625, 6)1 x3 b G; |0 J; f: U8 S# G! j
- >>> math.frexp(-100)8 @( \* w( y0 W
- (-0.78125, 7)
" {* W5 k; E O/ K+ R% h: r - >>> math.frexp(100)& k) B/ l: K$ M! l K
- (0.78125, 7)
复制代码 + i4 R! g/ r/ I2 v: u# ?
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列): K% V M& ^* ]! ^8 i4 N
- #对迭代器里的每个元素进行求和操作
* i4 m6 A8 x {% o3 c# p5 _. z* u - fsum(iterable)
6 y" e( e1 o8 |3 e$ D y - Return an accurate floating point sum of values in the iterable.- r" s- i& ~: `- |
- Assumes IEEE-754 floating point arithmetic.
& c k; E9 f+ w5 Y9 S/ n7 j - >>> math.fsum([1,2,3,4])
0 L3 u9 q' {" C7 ]7 I. k1 K - 10.0; i" V# A( s% p- Y# B" Y( y
- >>> math.fsum((1,2,3,4))
6 }9 [+ A/ a1 ~% A) k - 10.0
' L# c8 j1 l- C+ w; k - >>> math.fsum((-1,-2,-3,-4))/ D+ K. p$ X4 S' {
- -10.04 c W) H `2 Q0 _* o
- >>> math.fsum([-1,-2,-3,-4])3 H/ k6 @" c& t7 ^: l1 U
- -10.0
复制代码
3 x2 u7 ]7 {7 U: W: W: F. F umath.gcd(x,y) 返回x和y的最大公约数
3 H9 G# j6 z( ]% T6 c: g0 I8 I- #返回x和y的最大公约数2 ?! S% g! Z! h% M: E5 M4 J' m1 o
- gcd(x, y) -> int
}2 q" x5 t5 K& [ - greatest common divisor of x and y1 Z Y) T; L0 k5 T$ ?, \4 u
- >>> math.gcd(8,6)
& f( P* r4 s( A- f5 M3 f - 2
3 J/ B" ]; o1 n3 x" w - >>> math.gcd(40,20)! q5 ]1 R4 }9 f5 F. t& Q1 m
- 20
2 P7 Q- q) U& r, R - >>> math.gcd(8,12): R3 y2 R( w+ D1 I% R
- 4
复制代码
6 ?! M8 q) v! K$ o" J7 C% t: s/ I5 ymath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
) _; e, v9 Q+ \# B6 q- #得到(x**2+y**2),平方的值% S7 u3 q1 r* a9 G# v4 t/ a; P, f
- hypot(x, y)
5 w p) {: q: H: C( |% B( L - Return the Euclidean distance, sqrt(x*x + y*y).
0 X0 V" _7 s0 C - >>> math.hypot(3,4)7 `: V( Q" }% F, a: T1 z8 d
- 5.0
9 ]% y- S( b9 \1 s6 l! v - >>> math.hypot(6,8)
' w% {+ c, o% | d - 10.0
复制代码 0 _4 X* b* K ~1 d% Q. W3 U B6 S/ }
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False( y& {- c4 Y# @ [2 _' Z
- #如果x是不是无穷大的数字,则返回True,否则返回False
$ @7 h% M5 U1 i A* S - isfinite(x) -> bool
) c1 [6 y. }! O5 m- s - Return True if x is neither an infinity nor a NaN, and False otherwise.
6 r" U' p. a" ? k4 a - >>> math.isfinite(100)! ~7 D# m0 V0 m6 e. s* I/ X2 F
- True# v G9 w+ ]; O1 ~' C P' P
- >>> math.isfinite(0)2 ]; z7 s% d7 H+ J* n( X
- True
' ]4 a/ [) Y: i2 V( c' ^7 V2 t - >>> math.isfinite(0.1)
, ?- b8 ]" Q: k: w8 C - True) d# q4 b: j2 J: W F' Y
- >>> math.isfinite("a")( l; {1 n! k" e0 x
- >>> math.isfinite(0.0001)
" B/ w t0 J7 j G% } - True
复制代码
d2 ^2 F+ }0 P$ o- L. p: _. g+ _math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
: |+ Q' u: i0 K8 h- K8 Z @- #如果x是正无穷大或负无穷大,则返回True,否则返回False' u" z& o0 G9 o2 a
- isinf(x) -> bool
0 G- H7 s, R5 E. \. K% ? - Return True if x is a positive or negative infinity, and False otherwise.1 @! w; Z! u1 e5 c" W
- >>> math.isinf(234)+ L3 Z' m7 X5 M0 m V; H1 \$ n- I
- False/ k- Z: K1 z& v! O
- >>> math.isinf(0.1)9 S+ }& z5 F8 R# m# ~
- False
复制代码
7 I( U- G2 Y; I% Z( G Pmath.isnan(x) 如果x不是数字True,否则返回False7 _: Z- I% ]' X, j# H3 o/ U
- #如果x不是数字True,否则返回False; b7 X( `8 o+ w' |2 c/ ?3 }; p( Q
- isnan(x) -> bool
6 A* }& x! ?( L3 o - Return True if x is a NaN (not a number), and False otherwise.
! S! t8 J: y, z( L9 A. O& [ - >>> math.isnan(23)
, q+ G9 S) m: `7 Y - False
! S3 t# X, `& j7 { - >>> math.isnan(0.01)3 k! J2 q f& A' y
- False
复制代码 0 t" k7 S3 r; O9 n7 h
math.ldexp(x,i) 返回x*(2**i)的值
; ?4 R% d* |1 s* m- #返回x*(2**i)的值2 _* V2 w2 A& Q
- ldexp(x, i)
4 D4 N" Q1 F5 ~ - Return x * (2**i).1 D9 A2 v( D7 L0 f! p {7 e
- >>> math.ldexp(5,5)
8 C; H+ P4 O& t/ g - 160.0
8 n4 o' f4 t5 m1 N8 j0 ]6 Z - >>> math.ldexp(3,5)4 z) } W/ E& r. H! O: v
- 96.0
复制代码 0 i; }' v; b' t7 G% L
math.log10(x) 返回x的以10为底的对数: B: V7 G" u7 ~, y4 |
- #返回x的以10为底的对数
4 c4 R. R2 @9 M; }% S4 { - log10(x)- ~/ b. \" o; i
- Return the base 10 logarithm of x.! I: `% ?6 w" E1 U1 i2 f1 H
- >>> math.log10(10)" D' `& W+ T4 r
- 1.0# N0 b3 f R0 @0 x7 x1 B
- >>> math.log10(100)
! P9 {; l' V! @1 O8 W' u - 2.0
* i' i5 u9 y, c1 ?( n; U- K3 O- Q - #即10的1.3次方的结果为20
$ O9 J! K1 K, [ f7 g% ] - >>> math.log10(20)
: @* e6 ~) l0 R) V8 j5 g* y7 ] - 1.3010299956639813
复制代码
8 a% t& h f4 y) ?math.log1p(x) 返回x+1的自然对数(基数为e)的值
! S- y& r7 d# n ?& c, d- #返回x+1的自然对数(基数为e)的值( L7 ~4 K) I2 p! a" X
- log1p(x)
! V) L t2 P3 A6 t1 X - Return the natural logarithm of 1+x (base e).
0 k- i$ ~8 F5 `/ R - The result is computed in a way which is accurate for x near zero.) Y: A& i" a; Z! ^
- >>> math.log(10)5 |* [/ p! D6 w! }. l2 r# Y
- 2.3025850929940468 E9 T @3 j3 p9 @( g/ T
- >>> math.log1p(10)
1 \, `+ |" K( L$ }3 q - 2.39789527279837076 Z5 h) o& \& G i' ]! p6 E2 V
- >>> math.log(11)( k9 T; ^7 X6 F, D8 ^: ?
- 2.3978952727983707
复制代码 9 h0 ^- `! N+ I* W* B
math.log2(x) 返回x的基2对数; o( m: w9 ^2 W ~- n% ^! H
- #返回x的基2对数% }8 ^, [8 L \( Y
- log2(x)
1 J$ w d0 V, V. r$ O" X5 H5 g2 b - Return the base 2 logarithm of x.; F. M( \5 r7 ?* C# P5 i0 W/ @
- >>> math.log2(32)
7 {. o/ e4 g) u7 q+ d - 5.0& A3 k p4 E$ D) P; B6 N ^8 t
- >>> math.log2(20)& [) Y- u" h/ u* [
- 4.3219280948873632 N3 o3 Y5 ~8 S) j$ P6 ?
- >>> math.log2(16)
' [/ V9 _6 H9 G7 U! c - 4.0
复制代码
3 }# C4 x3 g: L2 t$ cmath.modf(x) 返回由x的小数部分和整数部分组成的元组
3 x# k% A* F2 D) y' t3 ]8 C- #返回由x的小数部分和整数部分组成的元组4 r+ l/ h- u* |( J# X, `! R. t
- modf(x)
9 g% p4 Y' |) ?: u7 n- V - Return the fractional and integer parts of x. Both results carry the sign
, ^' g# p9 O* L0 |; [2 B. k - of x and are floats.) `: i4 V$ c2 W) a' O/ A
- >>> math.modf(math.pi)# X$ J& u4 l" [; X8 }. B( K9 |$ G T
- (0.14159265358979312, 3.0)9 G6 @; {, k1 K
- >>> math.modf(12.34)2 d: l# @8 P! o4 U+ O, E
- (0.33999999999999986, 12.0)
复制代码 & a6 w; m% P3 L* _7 Y
math.sqrt(x) 求x的平方根
. o* U" X' @5 |5 K. o- #求x的平方根# @6 U. c* t4 F0 V! ]
- sqrt(x)
% ^- p1 C( P! [1 q( o - Return the square root of x.
! g3 N' G' W- j: @3 g1 n - >>> math.sqrt(100)2 Q4 X o f0 y) Q% E
- 10.0
; E2 M/ M3 E0 U$ r% l - >>> math.sqrt(16)' N( a O, b( c2 j. P" ?
- 4.0
) ^4 f$ G# c8 d" H1 \& F - >>> math.sqrt(20)! R9 m3 L6 v2 @8 l# Q8 s4 p" w, r
- 4.47213595499958
复制代码
0 k! ?8 [9 X/ R$ t" o$ i; ?math.trunc(x) 返回x的整数部分- #返回x的整数部分5 b$ Z! c m6 O7 B
- trunc(x:Real) -> Integral' J6 k% f$ E' b A
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 M4 ?& S U _$ y. [' X0 K
- >>> math.trunc(6.789)2 d7 V& e" {" ~$ ^
- 6
& B/ Q+ O! j- [ - >>> math.trunc(math.pi)
$ u3 h4 M5 ~' {# z/ g' `$ p+ m - 3: `: h. i, K) b& P) M9 k
- >>> math.trunc(2.567)1 {' L8 j) J' I$ v: @- n+ h o
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|