|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! H- f- }6 u; {* v: w7 z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。/ _( d9 Y# f8 P0 r: ]
$ I6 J* f7 g, {. ^5 g4 Z9 H+ |5 ^
方法1:
5 ^8 l( A7 ?. o4 U U, Y3 @- >>> import math
" i1 i2 b+ W8 I% o2 J% X$ b - >>> math.sqrt(9)
) z- {( c( ^6 t/ G: D1 {7 Q - 3.0
复制代码 方法2:
2 ?; P/ C2 u$ u5 P% g$ i- >>> from math import sqrt
% h. q. K3 {6 y% M+ j3 Z0 F - >>> sqrt(9)) H7 R: l9 q6 U, l Y* w" n" O+ ^
- 3.0
复制代码 ! H7 T& K7 k @) \1 _
?; `- W8 ?: q% ~9 Q
math.e 表示一个常量# h5 `4 V; j5 _ M) n. D
- #表示一个常量% Y0 R. I; v/ M2 [
- >>> math.e
9 B$ W& x% w4 J% P( I; W - 2.718281828459045
复制代码 - j( c0 {0 F* O5 u6 D
math.pi 数字常量,圆周率* ?' b- D9 i7 }, @2 N3 y
- #数字常量,圆周率
( u& o' v3 D8 R8 h/ J3 M" c! h! H4 O - >>> print(math.pi)' p! }# p/ Y9 c# l4 I- j& _" |
- 3.141592653589793
复制代码 9 k1 T' ^/ Q. y
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 W) @' u: L! K- #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 K' A; Q4 Y8 \- c
- ceil(x)+ U+ \' m* T9 y3 G
- Return the ceiling of x as an int. }% R$ v6 W- R; I2 |
- This is the smallest integral value >= x.) ~% m) ~7 f. V' X! Y
; X7 |) ?9 v N4 ^/ I. [- >>> math.ceil(4.01)
O" U" n! {2 I8 d# s - 5+ c: \' W$ \9 r
- >>> math.ceil(4.99)) B) F. j( Y9 c6 a+ L
- 5$ t8 l# T* f# f$ F% ~
- >>> math.ceil(-3.99)
% k0 ^7 Y5 \$ K# Y' _- x - -3
# T4 }9 ]3 H, U2 J" r4 E - >>> math.ceil(-3.01)& J8 g u; `6 T. `
- -3
复制代码 . z. }/ z+ O: z
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; M, C+ V3 H/ o- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 B+ g* z0 I+ o' |
- floor(x)
0 x% A1 J- t$ P& b - Return the floor of x as an int.4 I9 m% R% g8 ~: t4 _
- This is the largest integral value <= x.
) ~5 g; ]$ Z+ Q. i' F# ~ - >>> math.floor(4.1)
1 x3 ~; ]( a+ [) U- B - 4* s* C7 r0 \8 V& C6 E+ [8 F7 K
- >>> math.floor(4.999)
% A: G% M1 Y( Q2 I8 m - 42 B6 s4 P2 d5 v- U- `
- >>> math.floor(-4.999)
3 D- E. |) q7 x, H! D* c: t - -5$ ^! P- @& i! M
- >>> math.floor(-4.01)
$ ~1 R7 _' d# _+ B9 m - -5
复制代码
' K& @% R2 n: P4 I$ \6 [1 q6 Qmath.pow(x,y) 返回x的y次方,即x**y+ t- ?4 U) M9 m) z5 K
- #返回x的y次方,即x**y- n* X' S) L9 l" q, t2 F r
- pow(x, y)
4 J1 ?% O; k& g& @9 l - Return x**y (x to the power of y).1 }# L2 t0 ~9 F& V& k$ ^# ?" H
- >>> math.pow(3,4)7 J1 A4 N6 t1 d \5 |! l
- 81.0
( k$ ]7 v7 W/ J- u0 s - >>>
$ }7 S- o, s: y2 b: z - >>> math.pow(2,7)
1 P; V9 M7 q8 |5 E; x+ V - 128.0
复制代码
& t/ P0 I- J$ R% i& Q9 Z. {% T- e& fmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' A' N0 y- h: G6 P
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 i. J7 p4 e9 w
- log(x[, base]). u1 H' v# {; \5 C
- Return the logarithm of x to the given base.% X2 E- B# M2 H4 O$ w6 R
- If the base not specified, returns the natural logarithm (base e) of x.; n2 q/ A" R# U8 i; b# o$ Z
- >>> math.log(10)! c" E$ K9 O& y. _' u* X& s
- 2.302585092994046
6 r0 E( t: V4 }* g+ m - >>> math.log(11)
" `! x; {* O& Y$ ^ - 2.39789527279837070 }$ h1 |, x# F& K2 U6 V
- >>> math.log(20)7 Z3 X# G3 O/ N% z0 o z( h0 E
- 2.995732273553991
复制代码
- W' r+ s& h% }math.sin(x) 求x(x为弧度)的正弦值
/ V$ O- _2 G# C: G# Q- #求x(x为弧度)的正弦值% a) n( |/ z+ z) f4 L& g
- sin(x) o7 Z8 s) ^, _: W$ I A- y
- Return the sine of x (measured in radians).* Q4 q0 H. {$ e
- >>> math.sin(math.pi/4)2 T$ F! H2 N- v+ K7 P' I
- 0.70710678118654751 E: n. D% Q3 ?* l) Q
- >>> math.sin(math.pi/2)% m* {! {* R" S P x) P4 t2 V% B
- 1.0% D3 M- t8 P8 }! l) }
- >>> math.sin(math.pi/3)- O; f+ }7 v, W" N0 J4 i
- 0.8660254037844386
复制代码 - {0 n, I( B) i5 w) s- M
math.cos(x) 求x的余弦,x必须是弧度% a0 n7 h/ m6 P, Y r
- #求x的余弦,x必须是弧度3 V" y$ _' p3 x$ ~
- cos(x)0 M/ O' U; [0 u7 A$ Y
- Return the cosine of x (measured in radians).
2 U# P( n, B9 B: {) v - #math.pi/4表示弧度,转换成角度为45度) v# A4 ]* h) d0 ^
- >>> math.cos(math.pi/4)9 k! h6 U, ]2 O, B' Q' v' J
- 0.7071067811865476) r2 a' F* ~; j( r f- e
- math.pi/3表示弧度,转换成角度为60度
1 K* E' g1 Z$ y# S7 ] - >>> math.cos(math.pi/3)" d* A: H# [7 N% i, {4 ^
- 0.50000000000000012 F0 Y% c1 [- U, U4 L
- math.pi/6表示弧度,转换成角度为30度
+ r9 D4 ?+ I( ?% b - >>> math.cos(math.pi/6)& L, G/ r5 j" W% |
- 0.8660254037844387
复制代码 ; q* s4 ~1 m. Y( M" d
math.tan(x) 返回x(x为弧度)的正切值5 s6 e$ M, |- u! G" I1 z. I
- #返回x(x为弧度)的正切值7 h( j9 d8 G+ _
- tan(x)# D: T! J) N7 E& U" ?. N
- Return the tangent of x (measured in radians).$ f+ C* c7 o/ F5 r9 g( A; I
- >>> math.tan(math.pi/4)* Q0 S6 J7 L E- X7 |% w
- 0.9999999999999999
t) Y6 ^% K. I1 g - >>> math.tan(math.pi/6)
4 Z& q8 _( C6 L* N; P - 0.5773502691896257. O4 R# X u1 f5 q% {
- >>> math.tan(math.pi/3)3 c- ^0 Q; p- [+ Q/ l; N; ~
- 1.7320508075688767
复制代码 8 t) G7 u$ d, E0 l, z, O
math.degrees(x) 把x从弧度转换成角度
: H: b# w. d4 w. N/ E1 a, Y- #把x从弧度转换成角度
# T; j( f0 a4 H( x - degrees(x)# F( I% C. d+ c. a- E) R1 P: a
- Convert angle x from radians to degrees.
/ J7 ]6 E' A% f$ v4 ~: s3 T
! e1 o/ D, r. G5 ?& v. J- >>> math.degrees(math.pi/4): Y; }1 o0 d9 }/ l1 I, M) J
- 45.0
+ Z$ X; C. j+ e. @ - >>> math.degrees(math.pi)
A8 z1 s% L5 r- i - 180.0
9 M5 a6 Y( W& j - >>> math.degrees(math.pi/6)7 i% N# E. y5 M8 x) i* K( c
- 29.999999999999996- l5 d+ P! e& q/ l
- >>> math.degrees(math.pi/3)+ x* c+ w; m `3 r
- 59.99999999999999
复制代码 ! o$ f" }5 l) ^$ k$ H7 S- Q% m
math.radians(x) 把角度x转换成弧度
8 N; \4 a* P* j0 ]1 L( J( N4 r- #把角度x转换成弧度6 g/ ~$ p7 R2 |" K8 K n
- radians(x), }' @9 F& H, t# a7 y0 O! s5 o
- Convert angle x from degrees to radians.
* }0 v. K: c$ {& p2 l% x, c/ X - >>> math.radians(45)) w! t! n7 h4 ~
- 0.7853981633974483
3 D* u; J# X% t* C1 m - >>> math.radians(60) O: O2 J8 j3 P0 X. }
- 1.0471975511965976
复制代码
/ Q, \3 D6 U, {; d) r; R, E, E7 Lmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
. u# t3 ~4 S& p3 m- #把y的正负号加到x前面,可以使用03 d# E) w5 v @* E' N
- copysign(x, y)
# {# D8 X6 K) V8 Z - Return a float with the magnitude (absolute value) of x but the sign
. g5 {3 U, _# n3 ]6 }6 | - of y. On platforms that support signed zeros, copysign(1.0, -0.0) & [9 @+ \5 {+ i9 K
- returns -1.0.6 {9 A+ v$ @. J4 B" V; ]# S! j
0 t/ p, A3 P) n0 T. F- >>> math.copysign(2,3)
+ j4 L% l# E' y, I - 2.00 Z N6 r. U y9 j, @$ |% a
- >>> math.copysign(2,-3)4 p- K R1 D7 T6 h4 c
- -2.02 H: a+ @5 u! |& q
- >>> math.copysign(3,8)& d& x( ]' l/ U, m
- 3.05 H1 p6 e8 w, j' E* ~1 ?! [
- >>> math.copysign(3,-8)
& x+ L, n. R- B& O2 Y Y - -3.0
复制代码
) A) } l5 N/ }* o8 e3 r3 rmath.exp(x) 返回math.e,也就是2.71828的x次方/ a. N6 m5 b- S1 h% \" z% k. r
- #返回math.e,也就是2.71828的x次方; ]: Z, f3 y1 K2 {! S- H9 S. v
- exp(x)3 f! k1 j; p$ S" S6 {
- Return e raised to the power of x.5 C" ^1 ]9 ?3 Z
- 2 ] ?6 i! v+ ~; u4 b4 ]
- >>> math.exp(1)
! M: u! O1 [9 E1 j. b/ V; Q - 2.718281828459045
$ J/ C) l, x4 v2 k! j - >>> math.exp(2)7 e! E% x4 k2 j+ b
- 7.389056098930650 Y6 b; ]! s& k: @! H4 I; G
- >>> math.exp(3)
1 R7 K( F G. L6 R3 Z! H - 20.085536923187668
复制代码
4 a3 p7 y q4 E- v- zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
( O" w8 K( o' [- x$ t8 U% v- #返回math.e的x(其值为2.71828)次方的值减19 m |: A& P/ @& B/ o/ ^: Q5 \
- expm1(x)
# x! t' Z: K1 ~- M B - Return exp(x)-1.! i; N0 ^9 J% S/ w. |$ M$ f0 P
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 F! n; T- l% ]
. H( b7 q/ ~& v j9 d- >>> math.expm1(1)
" r3 Q0 w* `1 [! T1 m - 1.718281828459045( e2 c) t5 K8 C$ o9 [! y
- >>> math.expm1(2)+ ?7 r [0 H$ Y" c( K9 v7 k
- 6.38905609893065
0 t$ a0 g- u: m/ a; K; Q - >>> math.expm1(3)8 h# A& r% }% I
- 19.085536923187668
复制代码
" V1 z3 G5 `" ^; S% E$ omath.fabs(x) 返回x的绝对值
5 c; z& p) Y" l- #返回x的绝对值: W* j9 }" u' G. O% y7 H" ?! W3 K
- fabs(x). r8 F$ F+ B: g( [
- Return the absolute value of the float x.2 D, l( R0 P5 O4 b: h
) N3 }& X; @: P' j+ x2 w- >>> math.fabs(-0.003)5 n6 c' g+ ]( [2 ?
- 0.003
0 o/ z8 f1 D$ k; o - >>> math.fabs(-110)
9 e" i5 }3 u' e. ]5 q1 }8 }2 h% E - 110.0" B5 E$ @" k: {/ b. I
- >>> math.fabs(100)
5 z: t, W, O4 D- r# a$ B- P( s/ F - 100.0
复制代码
( _( H! ]8 O( }3 ymath.factorial(x) 取x的阶乘的值
, h8 _5 @& T' S z+ W- #取x的阶乘的值% W7 |$ P: V$ @ N
- factorial(x) -> Integral; m2 D3 q/ G+ ~$ t* ?! i8 }
- Find x!. Raise a ValueError if x is negative or non-integral.+ ^4 y! e5 Z `) U8 w8 \) k( Q) P$ r1 U
- >>> math.factorial(1)
8 e8 M9 b* x5 Q- l - 1
7 [7 T$ r* e8 o7 M+ T6 {) k: n& G9 i - >>> math.factorial(2)
) x! Z6 L1 i, k9 ]* q. v3 a* P* t+ ~ - 2, }4 a& K- c. n& Z! C' _
- >>> math.factorial(3)7 w; J9 P# w1 k# y2 y7 g% X
- 6
( d' N* h% q* Y, R0 S; j - >>> math.factorial(5)
' A" `( \+ x* O0 A: u D6 @ - 1209 Z1 i5 S+ y- m9 Z
- >>> math.factorial(10)/ W+ S' Q u% v* P1 f! y( d7 C% A
- 3628800
复制代码
% }) u8 e) [: Z O( ^" A1 \' ^math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
4 S( f1 Y7 n" ?4 U9 V! E1 z5 [- #得到x/y的余数,其值是一个浮点数
* @" S( a9 g$ k5 T6 z - fmod(x, y)! M' I h! D* H' S3 x) u
- Return fmod(x, y), according to platform C. x % y may differ.3 a: x& K$ t# S( v3 i
- >>> math.fmod(20,3), ^3 J9 C3 M- v0 u
- 2.0" Q& [3 j4 J6 Y/ ^8 P8 G, ^ m2 h
- >>> math.fmod(20,7)
" }- ?( s& s( C - 6.0
复制代码 * A( o1 {: \- S) O- v7 F
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" M6 ?- T, \9 ]
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. F% a2 W- e8 Z8 j7 \
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
' f" @( |6 y5 X - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
) K8 H+ E1 U+ |( ^" E$ ^! | - frexp(x)
4 [& z/ E0 ~# Q - Return the mantissa and exponent of x, as pair (m, e).! c, o% I, T% F7 }( Z4 ^
- m is a float and e is an int, such that x = m * 2.**e.
4 Y( N L: C* u! u R - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 B- `9 `% B2 n8 M( M% q - >>> math.frexp(10)* y5 _+ C- l, ^9 `$ U3 A
- (0.625, 4)
+ @+ Q. U, G3 i% q) l - >>> math.frexp(75)0 n! K) N" ^" L# |
- (0.5859375, 7)
! D' ?" P" y" n y+ f: M0 {. q; `' L - >>> math.frexp(-40)
1 @' O" g- ^+ F* V. n - (-0.625, 6)
4 d' e0 X8 ~6 a0 e, t - >>> math.frexp(-100)
$ Y. w$ ~0 q# S( j# \. n- w( t* p - (-0.78125, 7)! |: P" h1 Z7 X U% t& G
- >>> math.frexp(100)' R* B9 C! g3 ]
- (0.78125, 7)
复制代码 2 W$ k9 F r( z. u0 Q6 m; e+ [. E( h
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)2 m1 k. U" X3 z8 _
- #对迭代器里的每个元素进行求和操作" m# W0 p; V* ~/ A* D' p) Z
- fsum(iterable)
+ r6 _7 p/ w2 D6 b6 u - Return an accurate floating point sum of values in the iterable.! H: W/ l1 g, r
- Assumes IEEE-754 floating point arithmetic.9 d( N1 _/ P) }# J
- >>> math.fsum([1,2,3,4])3 F+ l2 V x8 A5 D B5 m
- 10.0
2 M6 V; W% S. w% j - >>> math.fsum((1,2,3,4)): w) L- B% M" g4 O8 v( Q* q4 l* x9 I
- 10.0
) a0 ?2 Y# Q2 b* q: N2 h v - >>> math.fsum((-1,-2,-3,-4))
' p7 e5 d2 b" O8 ?' X: O: |/ b) ^# T - -10.0: W$ s1 f2 T+ ?! Y6 j7 Y
- >>> math.fsum([-1,-2,-3,-4])
3 Y. B$ D5 K* m) ]: w8 [ - -10.0
复制代码 6 x l3 n2 t+ Z9 C
math.gcd(x,y) 返回x和y的最大公约数2 ]; t8 v3 s/ y# `" @$ o! i
- #返回x和y的最大公约数
. }5 F9 Z+ P5 C - gcd(x, y) -> int3 V# G. U9 Q6 u4 v" x+ v' |" V
- greatest common divisor of x and y5 s2 {; v+ T! j, O! m
- >>> math.gcd(8,6)
4 ?, e- g- T) W0 Q1 E - 2
2 _1 d3 x6 ^+ A5 w2 u( V4 V' C8 h - >>> math.gcd(40,20)
5 t6 z! O5 s% _) F) V' v% i- Y - 20
2 I. D Z" z' z0 _2 t - >>> math.gcd(8,12)
8 v1 u$ l9 {8 f - 4
复制代码 - s) K+ g% T0 i5 r" o
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
' m5 s; \/ h6 O. ~4 w- #得到(x**2+y**2),平方的值
' w) c+ A9 k1 s8 y; g b2 J - hypot(x, y)% T& v) [2 y+ i' }: b2 o
- Return the Euclidean distance, sqrt(x*x + y*y).
1 c: a! v" P ]& G# i6 w - >>> math.hypot(3,4)
* ]; B+ v& z2 F) b f - 5.0
7 F- h& e0 r8 ~5 E* m - >>> math.hypot(6,8)
1 W. L5 v# L6 g3 `2 @ - 10.0
复制代码
0 O+ o s: g( c* i1 x! l+ wmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 @0 Z; R% i9 z
- #如果x是不是无穷大的数字,则返回True,否则返回False5 ^; y, c3 |- u3 v+ E! h0 F4 u* d5 ^ P
- isfinite(x) -> bool) b# N& q9 u4 M3 r
- Return True if x is neither an infinity nor a NaN, and False otherwise.! I: O8 L6 n: |8 X
- >>> math.isfinite(100)7 z( ^2 J/ g; W$ E9 w' N. E) W) [
- True
% X& x+ ~" t0 L! p" y- s4 [ - >>> math.isfinite(0)$ @- u) s" j: T- o' w# T5 v
- True
: w8 X. |4 W! j) Q" z4 f - >>> math.isfinite(0.1) L! n9 X( ]5 L% z: E
- True
" y5 P! P* t5 N1 j3 V2 B O - >>> math.isfinite("a")) j# k2 ~$ I) D6 X: t0 q D9 a% m
- >>> math.isfinite(0.0001)* @& b! V; a0 A: Y5 A
- True
复制代码
! l& B$ b& {3 X' ymath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
# w& c- b4 K! ]9 u* |- #如果x是正无穷大或负无穷大,则返回True,否则返回False
' s' W3 e& M% ^, J+ d9 ]" Z - isinf(x) -> bool
+ y) u. y9 n2 w3 R$ d; J& O8 O+ N; W8 @ - Return True if x is a positive or negative infinity, and False otherwise.0 e: H* e) {2 Z
- >>> math.isinf(234)
) {9 s+ r- ~. C' e - False
6 f7 S7 V, y( W - >>> math.isinf(0.1)
" v, n* |6 B% u6 S8 h) Q - False
复制代码
9 a: m6 H, b' E0 @math.isnan(x) 如果x不是数字True,否则返回False( c* K" m. b& U7 ?& b+ Q
- #如果x不是数字True,否则返回False
" F- d( z" R7 l8 ] - isnan(x) -> bool4 I/ v. X ~3 k: L6 A( W+ X. q, j
- Return True if x is a NaN (not a number), and False otherwise.
: b+ E1 m6 p) k2 t - >>> math.isnan(23)
1 \1 A# _- {" T0 ` - False$ B0 |/ D; Z8 i: M0 [- ^4 m! x( a/ R+ b
- >>> math.isnan(0.01)/ t% c1 {- n6 ?) f/ A$ O
- False
复制代码 , j: x/ ~7 S4 E; J: c
math.ldexp(x,i) 返回x*(2**i)的值
0 \ S2 Q7 m) e% u, T- #返回x*(2**i)的值
# F4 p" f" a5 ?/ g/ N8 o: U' K) b) i H6 P - ldexp(x, i)
+ V0 I2 y2 a f7 Z# f, h - Return x * (2**i).
/ m o' E7 q2 f& ~6 u+ g - >>> math.ldexp(5,5)
. h/ H# X( W, s% O; W7 L8 V3 s - 160.00 w3 h* c5 H, Q3 m0 }" M5 V9 Q
- >>> math.ldexp(3,5)
2 k' E0 x; G& R - 96.0
复制代码 . e$ `& }- H6 o0 n9 v* Q
math.log10(x) 返回x的以10为底的对数8 X: {2 t1 e7 f& ]8 x" Q
- #返回x的以10为底的对数
4 @" v( i- u& t( b+ ? \7 z - log10(x)
6 O! `7 P( e; h% ]4 S - Return the base 10 logarithm of x.& J0 h( P7 u$ G- O& }7 i4 U- Q \, ]
- >>> math.log10(10)
# G' B! }8 L( ~9 u* u1 y - 1.0; H+ S" E9 w* J: Q
- >>> math.log10(100)6 Z- g7 W6 a# @" H
- 2.0% {; a. x" A0 e' x
- #即10的1.3次方的结果为20
# L' h" z6 z9 S5 k' ^3 P - >>> math.log10(20)
) w' P" y; c8 A3 N3 i - 1.3010299956639813
复制代码 - f; A# l, W8 j4 G
math.log1p(x) 返回x+1的自然对数(基数为e)的值& ]& y0 ~- Z! j! p% w, A* }: ?" k
- #返回x+1的自然对数(基数为e)的值
) E4 S% A' c# w7 h2 i9 B" S - log1p(x)
$ `; ]+ C/ u+ O! S - Return the natural logarithm of 1+x (base e)." S/ d2 n2 g; S" z
- The result is computed in a way which is accurate for x near zero.
' w3 ?2 F) V: h) M6 G6 ~) g - >>> math.log(10)
6 B* p* A5 a' y4 q) y ]( o1 a" ^ - 2.302585092994046& W8 ^# a; @; v5 H* M! J E* l) u
- >>> math.log1p(10)% p# ~. V4 O/ {+ P
- 2.39789527279837073 u- b1 z% M+ N" {
- >>> math.log(11)
4 R' n1 r+ ?- c7 W - 2.3978952727983707
复制代码 * r9 @8 J- p" u) H. H$ o
math.log2(x) 返回x的基2对数
! s0 o- e; t! c, z4 i1 {1 R- #返回x的基2对数
& w- S: j$ Y) ]1 i* A& |& c - log2(x) P$ G' ?) Y7 l" o
- Return the base 2 logarithm of x.
x, Q, k9 \, O5 r' x - >>> math.log2(32), ?3 ~ q! }! y* U2 `7 q+ Y& L
- 5.0
! v [7 w" F# _7 j! w8 U$ f7 s; b - >>> math.log2(20)' J8 B. V% J$ ~
- 4.3219280948873633 O2 D1 {4 S7 } j, |
- >>> math.log2(16)
) C" B7 f \+ _8 k; r0 [9 r! @7 j/ d( ~ - 4.0
复制代码
- q7 A! [5 p6 a7 {$ `6 D4 \3 Imath.modf(x) 返回由x的小数部分和整数部分组成的元组; P+ J' [. j& H
- #返回由x的小数部分和整数部分组成的元组
8 C/ o& Z) I, {( z) t7 Q1 G - modf(x)! R5 w) H9 {" g; N* ~" q
- Return the fractional and integer parts of x. Both results carry the sign
) d6 t* q/ c( V- p4 o, z - of x and are floats.) v, D- s! {4 z
- >>> math.modf(math.pi)
7 P& n- }( |4 O- m8 |9 F( L% s3 G - (0.14159265358979312, 3.0)
5 D1 ~0 y$ f% G/ q+ |, d - >>> math.modf(12.34)
1 ]! T1 ?1 w3 c9 \! m5 b% P - (0.33999999999999986, 12.0)
复制代码
* W9 d2 I* S$ {& z6 z; Wmath.sqrt(x) 求x的平方根$ X. g5 ]4 S+ m" o( h. E4 H$ u
- #求x的平方根
$ {6 h' y1 S' N: X4 O. `: z - sqrt(x)+ i; u" v6 L! m+ a2 m8 o
- Return the square root of x./ t% m$ y# W, H! m- C3 O ?" r. I' \* t
- >>> math.sqrt(100)
9 Z+ S4 `; j2 H3 Y2 _ - 10.0! N3 q% T& Y. K# T. j
- >>> math.sqrt(16)4 e. N# w( y& Q% n
- 4.0
9 @7 B, P, j6 O1 ?1 _# Q4 c - >>> math.sqrt(20)' z3 b4 p( ^5 ~
- 4.47213595499958
复制代码 ( Z. y7 \1 s3 I5 |
math.trunc(x) 返回x的整数部分- #返回x的整数部分
& d, c& x+ C7 W: l! N - trunc(x:Real) -> Integral/ o+ F4 [% W/ }; \7 p/ Q6 T4 W
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# @( h: ^3 \2 O% ?
- >>> math.trunc(6.789)( d4 l0 p* o( t0 _$ F
- 6
! P% \% u" G4 r- o - >>> math.trunc(math.pi)
. |8 N, E8 c' I! i3 o - 3# I: L x( ]. v1 D# I& c- d
- >>> math.trunc(2.567)$ m8 z4 \3 B+ ^4 r
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|