马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! {+ W3 ^& }8 ~3 E4 [【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" _7 X: u. k8 \# p3 w4 m8 L9 ]* [. N* B/ @' z9 C4 g W
方法1:
1 c3 E0 o: P/ N5 e- >>> import math
% |( q7 e, T8 f - >>> math.sqrt(9)7 L4 \% T, _4 f8 s
- 3.0
复制代码 方法2:
/ ^, P* @7 p* v% _1 m- >>> from math import sqrt
! m& k5 v8 y9 x7 H- u - >>> sqrt(9)+ l8 x; ~4 p; y( P+ H" h- d
- 3.0
复制代码
3 d9 z- q5 n* h8 Q& q; b% ] O
5 ]" ^$ \9 M8 I% x! f: K0 zmath.e 表示一个常量
2 Z. w! K5 p4 O* z% [6 }- #表示一个常量+ o7 x- w, Q% }7 x- p% k
- >>> math.e
7 i0 k: o" \$ j2 `6 M# U& [! H - 2.718281828459045
复制代码 ! R# r5 S. o% M$ a/ ^' s/ Q( t2 C) v" X
math.pi 数字常量,圆周率" M) p/ y9 r, \+ D$ R' p' H4 W
- #数字常量,圆周率
& [7 |1 ]0 y, I+ a - >>> print(math.pi)
" m1 O8 s( o: ]% z) E7 W0 y y - 3.141592653589793
复制代码 , a0 O, V w: J+ G+ K
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x% p1 o$ F$ D4 `- B% x$ l3 _$ A1 d+ M8 G
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 H6 ]& q1 R$ P* N5 Q - ceil(x)
8 ~( V5 v/ _3 `' r9 C - Return the ceiling of x as an int.
% w! x/ [( [. E* F, x - This is the smallest integral value >= x.
" H: o" {5 e! }& @ - & \- c: V$ |% `8 @
- >>> math.ceil(4.01)$ W4 N. U% L K2 I1 A8 W; T
- 5
T$ S* R; r u - >>> math.ceil(4.99)
- {2 n$ [3 h5 F) M - 5
9 @: b- s# b8 Z9 ~* d* _! L - >>> math.ceil(-3.99)9 b4 t. D( G; E% ^5 J
- -36 T) L4 U3 h4 [7 \; a5 {
- >>> math.ceil(-3.01)
1 I$ H! }' J( ^- E! | - -3
复制代码
# O; s: O' A/ B3 q: kmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 X) ~. {0 ?1 ]7 M v- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# u/ }2 B7 J% G9 ^$ b - floor(x)$ `3 x+ o% P7 W9 w2 M4 Z
- Return the floor of x as an int.
& e \" p) r) `2 Y1 \) Q - This is the largest integral value <= x.' s. L8 ~; J V! I/ C4 h5 Y1 |: z
- >>> math.floor(4.1)6 j( r/ ], v& n0 M, |9 q/ d* u$ y
- 4
6 {7 O/ l6 ~( M# L6 }% F1 _ - >>> math.floor(4.999)9 c. q% Q# J% L, n" G
- 4) L6 W! Y- k; T5 z" W
- >>> math.floor(-4.999)
; D, T% k! }5 n! _. v2 b2 Z8 V* ~ - -5# H; g0 b) L$ q; R; p' {
- >>> math.floor(-4.01)9 d( j3 e2 ~& k. Y
- -5
复制代码 : n4 Z' I6 H. j" h
math.pow(x,y) 返回x的y次方,即x**y* K9 |* V7 F9 P: f( ~1 ^9 Z9 Z
- #返回x的y次方,即x**y
0 B& Z5 M% t' ?# g5 f - pow(x, y): o: V. Y" B0 B) R5 H* x
- Return x**y (x to the power of y).
& Z# U- N& v1 Z: j/ M* W - >>> math.pow(3,4)
. \& r& u" Z9 ]- i! Y% L - 81.04 h# z: u) R1 t
- >>>
/ K% X; z! {7 ]3 S6 f - >>> math.pow(2,7)8 a* L! @5 H7 z% G' B0 ?5 l
- 128.0
复制代码 i' i' ~! j: ]( }- ?
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' a7 s3 W" ^4 }/ U5 f- ?9 @' _
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ S* W B. e$ W, j0 [# Q: B - log(x[, base])+ f* f* k9 j8 x- f$ M7 S; f
- Return the logarithm of x to the given base.4 a6 l' P6 G, {/ o9 @( i/ F0 I
- If the base not specified, returns the natural logarithm (base e) of x.
0 v8 {! @ z: d, p6 j( f3 I5 } - >>> math.log(10)
9 `# E% ?6 [+ E7 s3 ~ I - 2.302585092994046
1 r5 u& C( h3 F. c# U( q+ L0 Q: v - >>> math.log(11)% K( Z7 ]7 @% C r8 f/ o
- 2.3978952727983707
: {! e8 w- I/ b) _- @# ^3 g - >>> math.log(20)0 x; C& s* @3 {8 ^
- 2.995732273553991
复制代码
& H6 W; o X8 [& ?8 lmath.sin(x) 求x(x为弧度)的正弦值
% J3 N. I" L6 }- #求x(x为弧度)的正弦值* C; ~2 c( |$ L; W0 ?9 k i# m
- sin(x)2 U4 W1 d3 B* f) {; D5 [) d
- Return the sine of x (measured in radians).
, s' d( D" y2 m ^* B - >>> math.sin(math.pi/4)! w, p) Y4 _, p, U. Z; u
- 0.7071067811865475
7 _3 R& O8 H+ | - >>> math.sin(math.pi/2)
1 h$ x# u( m. @- P8 [6 o0 |( [ - 1.04 d5 s! Z4 F" v: y
- >>> math.sin(math.pi/3)& ?- q$ J' L0 g: [. P
- 0.8660254037844386
复制代码 4 s5 L; u0 z2 e
math.cos(x) 求x的余弦,x必须是弧度3 o; K$ V3 ?% n! `! ], t
- #求x的余弦,x必须是弧度
( o1 B/ D% v# o; a - cos(x)
: }* [! z r+ X1 B0 z t - Return the cosine of x (measured in radians).3 I/ u9 u5 x4 Q; j9 R
- #math.pi/4表示弧度,转换成角度为45度
8 t3 O3 Z- a4 k j; q# F8 A# D& k - >>> math.cos(math.pi/4)
% f: x; O4 a% z( q$ B - 0.70710678118654764 H- m3 e- S2 G/ G
- math.pi/3表示弧度,转换成角度为60度
5 Z) w' @6 H* a9 ~$ Q- O - >>> math.cos(math.pi/3)# r8 M% k2 \9 X% V j
- 0.5000000000000001# J" [, F. w5 g' |5 ]$ ]
- math.pi/6表示弧度,转换成角度为30度
8 P0 L( W# ~( N( d7 m1 { _ - >>> math.cos(math.pi/6)
( }3 e# V* R% d/ ~8 } - 0.8660254037844387
复制代码 # X2 P: \3 O+ P9 w! Y1 ^
math.tan(x) 返回x(x为弧度)的正切值 h$ l/ Q" I7 Y2 ^
- #返回x(x为弧度)的正切值/ X% z& N' l$ ~! m
- tan(x)
( i7 w/ I g: B& v4 u2 i - Return the tangent of x (measured in radians).
4 l2 ]0 ]8 | c8 k' R' V0 y - >>> math.tan(math.pi/4)# V5 Z" c1 ]* h" B/ o; ?
- 0.9999999999999999
8 Z8 A2 M2 k$ u+ C3 h( c0 H0 C - >>> math.tan(math.pi/6)
: n' g' q9 O" B- D - 0.5773502691896257" S, F) C/ E5 X0 x m
- >>> math.tan(math.pi/3)
6 R- a. g3 H+ z - 1.7320508075688767
复制代码 7 q1 q9 k# s7 Q, v. H( X7 b& c
math.degrees(x) 把x从弧度转换成角度
& v% F; w2 I& G+ [( F! m0 U! \; ^- #把x从弧度转换成角度
; c' d% C) P- ~8 \ F+ t# L8 i - degrees(x)
+ S( u' l; r; ]0 ^4 u1 b - Convert angle x from radians to degrees.
$ ?* M# f, t5 y6 {1 U, j - & \9 A: C( u* t5 {. ~) E
- >>> math.degrees(math.pi/4)6 v" w- C& L! L) G8 r3 a: z
- 45.08 |; k8 K+ u" D; G2 U$ V- f$ g) M
- >>> math.degrees(math.pi)
( f2 a$ [! K r& y- E3 f7 |9 ? - 180.0
" j) A# a X+ ] - >>> math.degrees(math.pi/6)
: y1 |0 v2 X6 i0 F - 29.999999999999996 I* n$ m' G; n% [/ ~* {" \
- >>> math.degrees(math.pi/3)* D* C. q- Z S
- 59.99999999999999
复制代码
% q- q) n0 C. @4 j; bmath.radians(x) 把角度x转换成弧度
1 a* b' d# q f) [( Y% ?* L; J- #把角度x转换成弧度9 w7 K( b5 x& q6 f* L
- radians(x). u( o. P% y4 f2 N9 @& f7 w4 T
- Convert angle x from degrees to radians.
0 P6 k8 q8 D2 B# [ d; O. `$ }- g - >>> math.radians(45)
1 d# k+ f9 ?8 e9 s - 0.7853981633974483
. M. Z5 k6 j- e$ x2 `& P4 T - >>> math.radians(60)
2 X* a2 [! ?+ ^# o - 1.0471975511965976
复制代码
$ f7 _6 m9 q8 O1 tmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
l/ f1 L; V! ]5 t2 z- #把y的正负号加到x前面,可以使用08 W& i) o$ D" g9 T
- copysign(x, y)' ]* I! Q$ K5 A d% O' Q# N
- Return a float with the magnitude (absolute value) of x but the sign
$ E* O/ x, N9 r" _+ {& T% E - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
/ S2 @+ p- o5 ]% |7 [ - returns -1.0.
5 z& B A( P" N& q( Z - 5 \# A# |# Y; Q% f
- >>> math.copysign(2,3)
7 b; T4 i& F* t5 r - 2.06 t4 E3 z" l& {* q: r, C
- >>> math.copysign(2,-3)' Y& W# x9 {+ E" o" ] g
- -2.0
% A ]$ c9 B. r+ O5 x - >>> math.copysign(3,8)2 A0 w4 t' m$ m& I( A+ u
- 3.0
: T. B8 |+ X _) J) F$ z- ^+ X - >>> math.copysign(3,-8)! l; o F+ B1 i; v& ^, d
- -3.0
复制代码
: u4 n1 L4 @: f% q; a' r* Vmath.exp(x) 返回math.e,也就是2.71828的x次方
+ \: o+ U% T- F+ P; K4 F0 ~) k3 Z- #返回math.e,也就是2.71828的x次方
% u# n% `7 d& W3 Y - exp(x)" \; w7 F: \$ {: s4 b
- Return e raised to the power of x.
+ n5 y: U% }8 |3 P/ m1 U* b7 t - ^( n% R5 v/ F
- >>> math.exp(1)
* W$ }2 i; v7 t! e - 2.7182818284590456 B& n( L' n6 [* w
- >>> math.exp(2)6 C# t& o5 @: \) d1 R
- 7.38905609893065
- N- ~4 _; V4 w8 _1 x - >>> math.exp(3)5 l) S# l% {$ j/ e2 r! R
- 20.085536923187668
复制代码 4 i" t/ [' x2 k. ]; n1 Y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1, i5 ~2 b0 s, j* {9 v h/ L
- #返回math.e的x(其值为2.71828)次方的值减13 V: G) o5 J) o- }$ c0 O
- expm1(x)+ C% }$ {0 m5 ~
- Return exp(x)-1.' u* H( @: Z( [& A( \. E
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 _1 a" D4 B* Y6 r5 E
7 C! L+ j# S& ^- >>> math.expm1(1)) O8 G W0 B" C6 m7 g# m8 q
- 1.718281828459045
3 W% l, U' D1 Y - >>> math.expm1(2)8 z, U X! B& s' c- g
- 6.389056098930656 A8 V9 @. l) ?2 h- J, i9 \& X
- >>> math.expm1(3)3 P% o( Z; Y/ E9 t% _2 n
- 19.085536923187668
复制代码
# {8 G- X" P/ Z6 t9 Umath.fabs(x) 返回x的绝对值! ~8 s1 r) r/ C( O
- #返回x的绝对值8 Y0 X, R( a& q" I. D
- fabs(x)" t5 I7 {" g) ^9 h- D
- Return the absolute value of the float x.
+ @ {/ [: L r6 w' X) G5 ^- D - 5 s7 n& s: w) w, P, {5 x
- >>> math.fabs(-0.003)
: t4 K/ V% N$ } - 0.003
# l0 r5 D7 ~3 x9 a - >>> math.fabs(-110)6 p( f" K9 _& j' l
- 110.0/ g: Y `; H' n5 Y2 T; R" N
- >>> math.fabs(100)) x" o! x h; d: i: y% {& }# J
- 100.0
复制代码
3 C ~- A( B5 V8 vmath.factorial(x) 取x的阶乘的值7 _' |, P% j; N" h2 V# D
- #取x的阶乘的值% L* z+ N8 |" F0 c0 U, ]
- factorial(x) -> Integral
* y1 d0 Y* X1 f# H* W/ n& g% W- ~ - Find x!. Raise a ValueError if x is negative or non-integral." ]; u8 G7 J- ?% g
- >>> math.factorial(1)
) p; m0 `4 D7 S- P - 1; V+ T' L( e5 K& l/ Q& w' y3 v4 _
- >>> math.factorial(2)2 W; J* i* I0 R8 K, E I
- 2! e9 k: { b+ P' |
- >>> math.factorial(3)
2 y! {+ @& }2 k+ \, o; `- r$ Y) I - 6! k6 ~0 \9 {8 g) t; |% r
- >>> math.factorial(5)$ ~" K. A: s1 _2 g, E- x
- 120
* n+ T1 [% O1 J9 m S$ I - >>> math.factorial(10). V9 E4 Y/ M# l) {7 V; u) o
- 3628800
复制代码 % N, g& e+ r( {# |
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
' D n8 L, c4 u2 b! T- #得到x/y的余数,其值是一个浮点数; `6 F, g* `- g% s0 o
- fmod(x, y)
' O+ h4 A7 |! h5 j' f; K - Return fmod(x, y), according to platform C. x % y may differ.
3 q# N' Q8 |$ ?+ c+ ?: E - >>> math.fmod(20,3)& I, D. u% F2 u P; F9 y
- 2.0
, z8 m0 ^# |) N% ~0 F - >>> math.fmod(20,7)
- ]0 m4 ]0 ~- i( L - 6.0
复制代码
K$ T7 S: J5 y3 ]4 @+ D" imath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围6 D. ^ {; m# y4 \
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
5 f+ d1 P8 n$ ?0 a - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ K% F; x, h# ]9 R. s# `2 x
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
1 `& x7 J' Y$ w2 s! W - frexp(x)
% w/ r/ ] o% C$ M) E6 x" e6 u - Return the mantissa and exponent of x, as pair (m, e).& q! E; p& W* ^' X- Y; }
- m is a float and e is an int, such that x = m * 2.**e.6 B5 H- ]* |7 O2 p7 r
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0., b, E7 V" ^, T8 M+ @
- >>> math.frexp(10): s3 V/ y' q c
- (0.625, 4): q1 y+ V) c, r( [& K: ]; n
- >>> math.frexp(75)2 z! W$ d: a( s/ j5 y
- (0.5859375, 7)
w% g4 b4 r4 b; D1 N - >>> math.frexp(-40)) K0 a1 B5 D D" w8 c% g; }
- (-0.625, 6)
d' b% e9 E; e% o1 e' x - >>> math.frexp(-100)
6 a8 O. [! ?7 f3 \* M - (-0.78125, 7)4 B& h# h% \9 {2 b8 y* I
- >>> math.frexp(100)( I7 Y! ]6 O2 t# D5 E* S/ l
- (0.78125, 7)
复制代码
+ `6 F6 w8 s7 u% emath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
* u9 C) e0 _, m- o0 h/ o- #对迭代器里的每个元素进行求和操作
. f! c/ w H. {$ S. W - fsum(iterable)
/ \& G$ a+ {$ x8 N5 t, G) m \+ P - Return an accurate floating point sum of values in the iterable.. i* m w" b3 j. p- s: K# d
- Assumes IEEE-754 floating point arithmetic.- e( I3 F# w2 M% [' K, P3 _
- >>> math.fsum([1,2,3,4])
0 c" M3 }6 E$ |+ P T - 10.07 `9 U; f$ y* Q3 @- {. B& U
- >>> math.fsum((1,2,3,4))
! g T8 ]' v& W- G$ P - 10.0. x/ ~1 H) j% C8 j
- >>> math.fsum((-1,-2,-3,-4))8 v9 p. l" l- u1 _2 w
- -10.0
+ |% M4 i7 q! i( O6 X. r1 Y' ] - >>> math.fsum([-1,-2,-3,-4])
2 p- u5 A# J) m% _ W4 V - -10.0
复制代码
- _! R& f- j" I$ o% o2 @1 Imath.gcd(x,y) 返回x和y的最大公约数
; Y$ R; T! U5 O9 @4 V& r- #返回x和y的最大公约数2 B% ~; w" Q4 K# c
- gcd(x, y) -> int
5 a# {0 {) j3 S1 ` - greatest common divisor of x and y& R$ w5 k2 c7 q, {
- >>> math.gcd(8,6)
4 f# _: {! a+ F) t2 S7 N - 2
% R0 O6 c3 Q, g - >>> math.gcd(40,20)
" a/ T* K# y6 M" _ - 20
3 c8 J% ?# R1 [ - >>> math.gcd(8,12)
$ ?0 W: U. U5 U' s0 w - 4
复制代码 # F; g& z+ R1 ~& X; e) m
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
0 e( s# Z/ W) R6 p- #得到(x**2+y**2),平方的值
$ j) V, R6 |5 d - hypot(x, y)
' U& {. h, S8 v - Return the Euclidean distance, sqrt(x*x + y*y)." G% T- c1 x7 j, V: Q) {1 I$ Z
- >>> math.hypot(3,4)( c. o) H0 @: m$ ?( c
- 5.0
1 ~) [' z: q; e - >>> math.hypot(6,8)0 @8 g1 X K$ _1 r
- 10.0
复制代码 6 a# x) r- n) S8 p2 T0 e2 u- L
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False5 _* i' s F% b4 ^
- #如果x是不是无穷大的数字,则返回True,否则返回False9 {# g$ a4 h% i- I
- isfinite(x) -> bool7 O+ O# {2 V6 K: D8 \9 [6 h
- Return True if x is neither an infinity nor a NaN, and False otherwise." u+ }0 k F& C8 e# k
- >>> math.isfinite(100)+ q/ u. W$ C; b) ?3 M
- True0 l; S$ h a* L5 E! Q$ m0 D
- >>> math.isfinite(0)
# H- w0 C0 R* V9 I" b4 _ - True
- t3 n7 v) }/ `4 ~" L! i& _: ^; M - >>> math.isfinite(0.1)4 K6 w: F0 P& x* Y4 y! Y1 }7 m
- True! P: q/ ~/ [) @# J; t
- >>> math.isfinite("a")( j# ^% d5 c6 _# F- j; r
- >>> math.isfinite(0.0001)
1 n+ {; Y; v, ^! \ - True
复制代码
: k" v# B6 @4 P4 z2 R' y' f4 xmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
* x! ?/ _+ r% k- #如果x是正无穷大或负无穷大,则返回True,否则返回False% v! @1 T" d0 y3 |0 S, ?: p3 H1 |
- isinf(x) -> bool
7 c" M7 X, C: n/ `0 [) H/ z% V - Return True if x is a positive or negative infinity, and False otherwise.( H3 h& K, w; I. l
- >>> math.isinf(234)5 S1 O% b# l4 x, c
- False f8 h" J; ~' t4 m- x' R" s' Z" b: T
- >>> math.isinf(0.1)) k1 }0 @! g. N) F$ Y8 t
- False
复制代码 9 z3 H' ?, o/ p* J7 L+ l+ }
math.isnan(x) 如果x不是数字True,否则返回False$ V5 S9 d! b$ _# f2 s
- #如果x不是数字True,否则返回False
' e1 E+ ]7 M* x5 D# Q6 l# _4 |8 W( e - isnan(x) -> bool
: [% U3 [' m1 K3 }2 | - Return True if x is a NaN (not a number), and False otherwise.! t( A! j% `9 E: t! \; w: D0 S
- >>> math.isnan(23)
' B9 b: O, t3 Y- t) |; j5 A- A - False; w0 L& s$ t1 T7 s
- >>> math.isnan(0.01)* m4 Y ^( H) G* @& a9 |
- False
复制代码 ' W) C. b+ k& C/ w: X5 o
math.ldexp(x,i) 返回x*(2**i)的值4 a# E% ]# R& c: r1 L! z% U
- #返回x*(2**i)的值! l2 P) j8 z+ v( d; [& y/ m
- ldexp(x, i)( e" N8 F$ m1 k
- Return x * (2**i).4 B6 B& @' N9 V
- >>> math.ldexp(5,5)( N0 f! H D# P) I( r3 C
- 160.0
7 j: i2 Y5 h8 p/ e6 Y2 s8 S' X5 O - >>> math.ldexp(3,5)
7 o* M# `, [+ E" N# V - 96.0
复制代码 k- y* q# ~7 d# r
math.log10(x) 返回x的以10为底的对数! M3 {" \6 E5 ~- b( L$ B0 f
- #返回x的以10为底的对数& {( U- r% x: X: `5 J# i
- log10(x)
k6 c% N% B) d7 Z2 j4 c - Return the base 10 logarithm of x.
7 R V9 V3 p5 Z. y1 r& ?3 g# j - >>> math.log10(10)) U3 O/ X* N" g: k& C
- 1.0; `# ~5 Y$ j: `' B
- >>> math.log10(100)( `, j% T9 q3 R/ u) ]3 p0 k
- 2.04 U- m1 ^8 I7 B
- #即10的1.3次方的结果为20
; e, w4 `+ D# g$ c - >>> math.log10(20): k% n" W- e3 Q& O! m/ z# E ^( ~
- 1.3010299956639813
复制代码 : p$ z# M- F( I: E$ m
math.log1p(x) 返回x+1的自然对数(基数为e)的值
0 d& o: p! G/ _% p" w" q- #返回x+1的自然对数(基数为e)的值
: A; q% J5 i$ E# u# x' N - log1p(x)
) [4 T% {1 _$ B$ P5 q* n - Return the natural logarithm of 1+x (base e).: x& s4 } U1 ]
- The result is computed in a way which is accurate for x near zero.- n& B% l# _8 Y4 k' P7 D1 a
- >>> math.log(10)
( s! r5 _+ D3 L3 G$ L6 l i# }: g" j2 j - 2.302585092994046$ R. z: H6 q+ \# r) ^2 W6 ?
- >>> math.log1p(10)
* D! s/ z8 _6 @! ~2 O9 t - 2.3978952727983707) o, W, B; d$ a% y2 {' j
- >>> math.log(11)
: m9 f5 m \6 ~) q1 w1 _0 t - 2.3978952727983707
复制代码
1 w( E1 H4 y9 F/ P; I! Fmath.log2(x) 返回x的基2对数5 ?& a4 A. B1 P/ l3 s
- #返回x的基2对数
$ S o; w7 R( @! ^5 n, V - log2(x) M, M/ s5 |3 ]& i6 O( P4 C
- Return the base 2 logarithm of x.
. w1 b* W8 |, z: T - >>> math.log2(32)6 O7 `! }/ ^9 s6 | {. M
- 5.0& |- ^1 B6 e% ^) M% B
- >>> math.log2(20)6 `6 U5 C) D5 a ]
- 4.321928094887363, X& G& p; m) c. L% O/ {
- >>> math.log2(16)7 m" p$ C+ |: ^7 [6 ]' R+ @4 E+ e
- 4.0
复制代码
" f# a: @; T! S2 g2 A+ Ymath.modf(x) 返回由x的小数部分和整数部分组成的元组0 K& P$ G2 s$ l
- #返回由x的小数部分和整数部分组成的元组: b' H" A2 P7 ]- A5 t M4 ^% R
- modf(x)
4 S4 r2 W- A* ]* { x' W - Return the fractional and integer parts of x. Both results carry the sign
s* o3 y% M7 Z( f; g; o: B/ n: y - of x and are floats.
4 ?, u: ~, _$ f+ J0 p4 n - >>> math.modf(math.pi)
' [9 A5 p) d$ _, v. O! A - (0.14159265358979312, 3.0)2 t! i" b( S% E: x
- >>> math.modf(12.34)
/ [) S5 J3 l+ C, R+ m - (0.33999999999999986, 12.0)
复制代码
6 w0 ~/ @( }8 P; I: |7 ?! Gmath.sqrt(x) 求x的平方根
' r7 a7 U) K& s# j# M( v: ^- #求x的平方根) X, |' g' Y# u, n$ C+ `* h* y
- sqrt(x)/ Z4 U# K1 z8 r5 y
- Return the square root of x., Y! ~' F: N( i" b' z. g
- >>> math.sqrt(100)/ w, a& v @( F3 S2 S5 D; v8 s3 z- C
- 10.00 O& Z9 a/ y7 K$ M' p5 y1 m l
- >>> math.sqrt(16)
/ [+ r7 B7 n" G7 V, P - 4.0* m( Q8 o- f Q$ O8 z9 T1 ~
- >>> math.sqrt(20)( o: G( g. c7 ^* g0 r
- 4.47213595499958
复制代码 9 D3 z$ B! q% p( N6 t$ B l( i5 l+ v
math.trunc(x) 返回x的整数部分- #返回x的整数部分
6 \# P: N0 O- m/ c/ x j5 W5 @ - trunc(x:Real) -> Integral7 q" V: T6 z, s6 n( Z1 m
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
9 V4 Z5 e/ ]+ u0 i: p6 I5 U7 R - >>> math.trunc(6.789)& x- g4 q3 K+ t' c7 Z
- 6
" ^; _ p, w, E/ v6 J5 N0 J! l - >>> math.trunc(math.pi)+ y1 T* ~ n: u3 j& D
- 3+ Y* ^3 ] p7 r0 y9 \
- >>> math.trunc(2.567)
5 i- o: M: v6 Y$ C* O - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|