马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 A5 o N- J+ q$ g# e+ X' O u1 ?% d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。 C3 b$ i. \& j# Q( I: C/ y
8 h) `7 m/ w: g0 D方法1:0 R3 R& P* K& {6 X
- >>> import math
3 ]1 T9 N5 C; k* e y - >>> math.sqrt(9)
% M2 C6 Z: y$ R% `0 B - 3.0
复制代码 方法2:- t! X) L' I J/ [! ?: @
- >>> from math import sqrt
* I9 y; |" C* B1 D. ^, g$ T: c - >>> sqrt(9)
+ x9 A6 b5 i/ T! Q - 3.0
复制代码 ( o1 O: v* H% L, y% O% k( G$ p& O6 r
2 O! ?1 P6 ]- l( u$ A( y% i
math.e 表示一个常量$ L1 M$ S9 B, w- }& ?+ Y9 d
- #表示一个常量
6 H. V2 O% W- C; l& s - >>> math.e! G" }: u! L3 D. I4 w& m$ J( E
- 2.718281828459045
复制代码
7 z5 z+ k7 C7 a: Tmath.pi 数字常量,圆周率
5 J! }# ~; m+ i/ z2 p3 w: I% W- #数字常量,圆周率
3 p) U: Y9 q ^9 U5 S - >>> print(math.pi)
& l( q' R9 z' t* |8 R& N - 3.141592653589793
复制代码 & V6 ~0 n) f. V' Z) V% A L9 s/ S
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x6 ]! Q V8 U+ |; Q( h" Z$ F
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x- y+ Q* {" E3 g* Z- Y" k
- ceil(x)
Z2 D# h7 U3 K' b5 v+ P3 J - Return the ceiling of x as an int.) ~0 W {1 B1 b
- This is the smallest integral value >= x.
( h" M( G- z& B2 ]$ }% S - ) f5 ~3 r- ~: [! e
- >>> math.ceil(4.01)
% k; X7 y0 Y4 h" ^ - 5: H. T7 b5 `8 e
- >>> math.ceil(4.99)" Z7 i6 I; ]- K" j
- 5
3 B) X$ N" ]% F! K - >>> math.ceil(-3.99)
: [% s% s5 a* a( A2 ^2 ^! _6 m - -3+ D. Q1 @' r% }: J
- >>> math.ceil(-3.01)8 o- B; a4 N8 a- z
- -3
复制代码
2 L5 e% i5 w* G0 o" @" B# B* lmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 t$ T, H0 D) b- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; p) a* ]4 e4 R' E u' u7 u6 v - floor(x)9 w: w$ z9 d* y5 ^) N" b7 g' ^/ n" x- U0 o
- Return the floor of x as an int.
. s, X2 B7 d7 v' y) P* b - This is the largest integral value <= x.
0 B2 u2 _' @8 z - >>> math.floor(4.1)
9 ?( T$ V8 P" W' X3 R - 4
/ M" q/ I0 E: N2 G8 j0 U - >>> math.floor(4.999)
) E: ~( v, p; D/ V5 ^6 T - 4
! d9 s, C; ^; b C6 R5 o: N- P; f - >>> math.floor(-4.999)7 C/ u( \% V" d2 Y" I
- -5# C1 A0 r5 R# W
- >>> math.floor(-4.01)3 p& g3 O4 p! w8 H f7 d
- -5
复制代码
7 F' k+ y+ e1 b% N, O% U9 gmath.pow(x,y) 返回x的y次方,即x**y6 V% B: {: [! ^2 J C: A4 H( O- x
- #返回x的y次方,即x**y
4 w) J- ~! s5 T) P - pow(x, y)' i* v. e2 k$ V w! K) |
- Return x**y (x to the power of y).
8 j' O' K; f! {) W, ^; | - >>> math.pow(3,4)
* @; z7 G/ R* g0 G8 S; a7 L - 81.00 l7 [* N( Q' Z0 i: b, w! F
- >>>
; p# l f/ \1 \& p - >>> math.pow(2,7)
: H" N1 G: _* Q; d - 128.0
复制代码 0 v/ Y+ @; X9 f, P
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' m( b, N* \5 z0 z% ^5 G
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ [% u/ l2 V( X& V+ g; F - log(x[, base])& i# H4 t& v v/ `8 r
- Return the logarithm of x to the given base.
. I4 i, A3 `) n. T | - If the base not specified, returns the natural logarithm (base e) of x.
u3 A F5 e# x8 P2 y. r9 P4 n - >>> math.log(10)
- z" s, n' E G) s, J - 2.302585092994046
- _$ V+ O }9 S- B2 X5 h - >>> math.log(11)- ^- M& H; D2 V: X" e4 O! v
- 2.3978952727983707
5 S: ~# Z* N6 U7 n# ]" y - >>> math.log(20)7 K8 Y( S+ X- h& i/ b' X
- 2.995732273553991
复制代码 0 y: j' X, v% D+ h6 _
math.sin(x) 求x(x为弧度)的正弦值, d" X! z# J0 a: r5 h) t/ U
- #求x(x为弧度)的正弦值7 o- \4 b0 g2 b( b5 z* h& p7 G4 k9 M3 h
- sin(x)
g+ m. t# ^: |1 E) S* y - Return the sine of x (measured in radians). Y+ @( {/ l% a9 V
- >>> math.sin(math.pi/4)
9 I4 B; }! Z' g2 o, F( A9 U - 0.7071067811865475+ I+ l- R; X1 @* { n4 I) f
- >>> math.sin(math.pi/2)
( z+ ~' V. O" [* _/ T - 1.0# ^' O, t/ P# q& m- F
- >>> math.sin(math.pi/3)
( k* e9 c: g3 Y: c$ ]: }- {" h - 0.8660254037844386
复制代码
# G% ]& X+ c: K4 s0 V; ^1 |math.cos(x) 求x的余弦,x必须是弧度+ B C8 H) d: v( \1 k
- #求x的余弦,x必须是弧度' \( J, W% X& S _
- cos(x)* b# z' q: v" z/ _/ a
- Return the cosine of x (measured in radians).
1 F4 H+ v6 u7 F - #math.pi/4表示弧度,转换成角度为45度
" g: r0 s/ F1 \2 p: g+ N" X - >>> math.cos(math.pi/4)
. ?# z* `! G& Y: c7 I- u - 0.70710678118654766 r7 Z" B- n9 d3 Y
- math.pi/3表示弧度,转换成角度为60度
, L' M2 G' i' C/ y+ v7 {. K, A% { P - >>> math.cos(math.pi/3)
0 e1 h" L5 t0 D% P/ i1 M L - 0.5000000000000001
% w/ C$ i1 D+ ]& l: X - math.pi/6表示弧度,转换成角度为30度8 C6 l8 F2 E% K" B k/ A
- >>> math.cos(math.pi/6)
1 z0 n0 ^7 K+ ~ - 0.8660254037844387
复制代码 / X ^% c4 b& g" V. k
math.tan(x) 返回x(x为弧度)的正切值2 k' P% W8 l% @- y% M5 f
- #返回x(x为弧度)的正切值; |# t7 @/ s) i" Y
- tan(x)
" Z; S. s2 K# o7 D% V+ S, ` - Return the tangent of x (measured in radians).7 U2 h, [9 J A7 x' J a
- >>> math.tan(math.pi/4); O5 J" Z. E% u" i8 S$ ^$ F
- 0.9999999999999999
# t3 z8 m, N+ S- h: r - >>> math.tan(math.pi/6)' p4 d7 R* W n5 R8 o
- 0.5773502691896257: ]- Z0 B! N4 p/ e6 a
- >>> math.tan(math.pi/3)
% ^. o: o. t4 P& m: B6 M5 X! e - 1.7320508075688767
复制代码
$ G; w$ K8 a/ B2 g0 Y9 Kmath.degrees(x) 把x从弧度转换成角度$ C! E1 F/ C0 ?+ r- q i
- #把x从弧度转换成角度
, k% k9 Y' M! w* p4 g - degrees(x)
8 B9 [, f* i8 T0 w0 ~ - Convert angle x from radians to degrees.
. t- M* A4 G8 j1 t7 F9 \ - , p& X4 ]; K5 ?- d3 {
- >>> math.degrees(math.pi/4)
2 I2 ]' e# _! N5 t5 m X/ d# r - 45.00 ], N: ~) _5 {. e+ W. ~5 ^8 o
- >>> math.degrees(math.pi)1 ~, D( n( D' R: [
- 180.0
) S0 z) z; f7 y4 R7 K; }9 d8 [ - >>> math.degrees(math.pi/6)
3 S! b- t0 `$ x, d - 29.999999999999996
+ R9 H! }8 w* Q$ c2 d - >>> math.degrees(math.pi/3)0 O% E( {' V A
- 59.99999999999999
复制代码
" A, x# G* ~/ n+ H/ `3 qmath.radians(x) 把角度x转换成弧度
5 |8 z+ k; i# ]! D- #把角度x转换成弧度0 t+ u- _7 I& o' m. ~0 S0 b
- radians(x)! R) v) }. k3 ^. Y/ |
- Convert angle x from degrees to radians./ g' z+ V( F6 y8 p' R, ^1 @
- >>> math.radians(45)
8 b7 f5 t, u4 U, n; d - 0.7853981633974483
$ F! L; l1 }/ w - >>> math.radians(60)
3 @( E( y1 V4 @' Y/ Z0 T - 1.0471975511965976
复制代码
6 ~. X, n5 d) e# I Kmath.copysign(x,y) 把y的正负号加到x前面,可以使用0( X$ c) j w5 K" A% F" e+ G) A
- #把y的正负号加到x前面,可以使用0
; \! X& o1 G% q - copysign(x, y)
2 V# s O" P/ @% a7 ?) U - Return a float with the magnitude (absolute value) of x but the sign ; ?6 ^8 H/ a2 y% T
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 8 K0 s ?8 q- [+ X; D
- returns -1.0.
# A9 m# z' e1 c5 y8 r
5 |- ^4 O0 ]" |2 C9 D8 r- >>> math.copysign(2,3)
6 t# D3 P7 ~% E, ^ ~ - 2.03 d( ?7 I* o+ u/ u* Z" o7 r( o
- >>> math.copysign(2,-3)9 v) M1 W6 k7 ^, a. N
- -2.0* T+ Q* g; g' E9 @8 p4 b
- >>> math.copysign(3,8)+ T) I1 L* M6 C& e! p" b
- 3.0
@8 A* P j/ c" I/ e: g e - >>> math.copysign(3,-8)
2 s' r8 O! K4 W; D9 C7 m - -3.0
复制代码 $ q1 g! i0 I& W
math.exp(x) 返回math.e,也就是2.71828的x次方/ u; [( w4 e8 z. }9 r( h
- #返回math.e,也就是2.71828的x次方
7 |2 t% I3 B+ B* h - exp(x)# [% O, R' y5 a# m* y: k3 W
- Return e raised to the power of x.
0 S5 ]/ H* i6 A" N# e9 o - 4 f- ^$ v$ F3 @
- >>> math.exp(1)( u% ~$ V" }& {9 ^
- 2.718281828459045
% W4 Y8 c5 ~6 s+ |& Z7 I: O - >>> math.exp(2)
( L0 }* _% F4 F3 ~ - 7.38905609893065
0 Q" }. K5 ]7 W) E - >>> math.exp(3)/ _& x( s+ {8 o+ {4 y) t
- 20.085536923187668
复制代码 8 C1 f( r( b8 o' u- K/ O( K5 e: l; A; t
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1% l& F1 q d A n, Q3 U( N
- #返回math.e的x(其值为2.71828)次方的值减1/ w! M+ Q. B1 R6 ^$ [& q
- expm1(x); D+ @) t& B" ]! D
- Return exp(x)-1.
# f9 }1 k# Q0 D0 m5 [# }1 T - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
' A# _% {7 X8 B$ U' F+ } - - H: b- |; k b3 P4 A9 m9 |9 c: [
- >>> math.expm1(1)
3 r) W5 f& R, u- ^6 t4 A$ | - 1.718281828459045
6 a$ z6 c6 A% Z; _+ [8 } - >>> math.expm1(2)
9 D1 q/ d; r$ g8 f o - 6.38905609893065
, ^1 U% n8 q% f5 a5 J) t - >>> math.expm1(3)! g/ I r. |0 b, Z
- 19.085536923187668
复制代码
6 A+ w# x! ~# Y5 W0 Z) Ymath.fabs(x) 返回x的绝对值% s7 J! `, o% i, K/ M
- #返回x的绝对值
) a, P6 O$ A3 S0 R% R o - fabs(x)
: m; V' u* C2 ~ - Return the absolute value of the float x.
* Z M& N1 K) ~; o5 w - ! T( ~6 f# o* [7 c7 d1 D
- >>> math.fabs(-0.003)' H& [( G: V6 _; k& t( r
- 0.003
7 @$ @$ J" \ C, _0 h - >>> math.fabs(-110)( _( q2 Y* l. @- N' ?* K0 f, ]
- 110.00 a8 e* p% U) f* a; m9 n/ ?6 R
- >>> math.fabs(100)
$ l4 q5 q& N/ ~, ~2 Q - 100.0
复制代码 ( W: \4 i3 P7 h/ m
math.factorial(x) 取x的阶乘的值6 U2 F. N- L$ s8 d0 D& U
- #取x的阶乘的值
- \+ \# a2 D* k3 X. s - factorial(x) -> Integral
/ b& t3 d0 F. E) U* ~0 v - Find x!. Raise a ValueError if x is negative or non-integral.
! t. O# F9 s4 l3 I& u2 ?* r - >>> math.factorial(1)
( q8 s( V6 [3 q" T& Y - 18 N1 r1 c, U% P7 b0 H2 n* T
- >>> math.factorial(2)( S" {( c% t2 \, c$ J) v' U' q
- 2
3 R4 ?1 w/ H. O$ `2 y! h, A" O- J9 m - >>> math.factorial(3)' B% J0 G: b' B* }- a8 L% I
- 6
6 u2 K+ j" y% G+ F9 w! r8 { - >>> math.factorial(5)
3 a$ f! X2 @7 S i/ Y4 Y - 120+ @: B; Y/ `7 u7 K* \0 L. a2 \
- >>> math.factorial(10)
) c6 i2 O7 E- l* _ - 3628800
复制代码
2 J3 L; W% F. O |/ b: b/ {math.fmod(x,y) 得到x/y的余数,其值是一个浮点数, L& K8 N1 M& S- Z
- #得到x/y的余数,其值是一个浮点数
4 D* c$ H8 M' B+ {+ o7 a - fmod(x, y). O! }6 I R0 C# s8 r! y+ v1 {7 O
- Return fmod(x, y), according to platform C. x % y may differ.7 u, }! G1 L" A- ^4 O
- >>> math.fmod(20,3)
- H4 ?# v0 `' O9 G! A - 2.0
6 p) z( I* B! D - >>> math.fmod(20,7)
5 \1 T3 h$ j: C o6 F0 ^ - 6.0
复制代码 . B, h( P$ R3 R2 H1 `* h7 t/ s! v
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 P7 A6 b5 O1 \$ ~; d
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
) |1 r! X- o a# }7 j' `4 n - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
1 `8 L7 d3 A: w - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 a1 m+ c' z' K: m$ A7 T- t
- frexp(x)
8 H- v( x+ M) p - Return the mantissa and exponent of x, as pair (m, e).0 J V7 K/ |8 n
- m is a float and e is an int, such that x = m * 2.**e./ v% l6 W9 j7 A
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
% r8 T% u! b7 S) t% ?9 ^; K' N6 {) f - >>> math.frexp(10)
; b* p% C/ t! n# r/ p; \ - (0.625, 4)" j. Q0 D( x6 W! a" ^( t) F; E
- >>> math.frexp(75). `+ U! C- M" l" I% w; ^' x: E
- (0.5859375, 7)
; z [& m2 K, Z2 V/ k/ S+ {# ] - >>> math.frexp(-40)
* w( h( ?: E; ~; s - (-0.625, 6)( G7 I0 R/ [1 R( z) x5 e" e* |
- >>> math.frexp(-100)$ X: j- ?. K7 _. V
- (-0.78125, 7)
6 L% f; t) ^" {9 v - >>> math.frexp(100)6 @0 v* q) c: V q
- (0.78125, 7)
复制代码 0 }! Z) `/ |) `1 i+ V. G
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)% b4 X5 o8 [. D
- #对迭代器里的每个元素进行求和操作4 o4 `& k' F8 O* L9 \3 I& U8 _
- fsum(iterable)' ]4 o. N: Y8 |1 ~! J
- Return an accurate floating point sum of values in the iterable.7 q1 s- F( P$ o. a1 `7 d! w; c
- Assumes IEEE-754 floating point arithmetic.
! }7 j8 H* z5 n6 v. X+ `0 S( s1 a - >>> math.fsum([1,2,3,4]): v/ ]* G! N9 Q. J2 M
- 10.0
' s0 |4 d3 Y' K1 Z" j$ l; b5 R! A - >>> math.fsum((1,2,3,4))
2 a) Y3 K# K: e) X - 10.0, q. J( t8 y; O4 v
- >>> math.fsum((-1,-2,-3,-4))
+ u" H: u2 O6 t. l. l9 }; w - -10.05 c: @, h! ~- C5 y0 `3 z
- >>> math.fsum([-1,-2,-3,-4])
% H( e% Z3 u) f3 b; ^: S. `" D. t5 `: D - -10.0
复制代码 % r7 e, `. G5 E [0 h! c
math.gcd(x,y) 返回x和y的最大公约数0 K2 N) `) B) C8 z
- #返回x和y的最大公约数
) x$ o: G$ j2 b& q, t - gcd(x, y) -> int$ z7 ?9 ~5 D S9 e# p9 |( _5 X
- greatest common divisor of x and y
' L" E% T. P0 U q# C! ^( U5 \ - >>> math.gcd(8,6). o2 c- [9 H. j8 w1 l# W
- 2
; k) j) |0 \: T; K - >>> math.gcd(40,20)
% J9 `' w, b3 ?: j# i( Y - 20' h; ~' b5 P+ T7 Q& Q
- >>> math.gcd(8,12)/ V. p7 W, [, I; r c
- 4
复制代码
) ]" R' D; j3 F5 X* k4 kmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
3 C# I. r, C/ ?0 w' {$ }. d- #得到(x**2+y**2),平方的值# x, I/ t( E" r" B- G2 E
- hypot(x, y)
; X0 Y, o5 _, ` - Return the Euclidean distance, sqrt(x*x + y*y).
% y& V) f+ K# L; A& D5 u, J - >>> math.hypot(3,4)' R8 }2 L* @8 F% Q7 H
- 5.0; A% _* t) a" R) j5 ~- y
- >>> math.hypot(6,8)) D: f0 W$ f+ R$ u B9 m* Y n
- 10.0
复制代码 # E) i5 v# t1 F: R+ c
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 f/ y! G& C8 {7 u; d5 e. k- #如果x是不是无穷大的数字,则返回True,否则返回False
- X* A+ W' V( d: Y! @ - isfinite(x) -> bool$ @/ _5 x0 _ P. a$ ]% z/ K
- Return True if x is neither an infinity nor a NaN, and False otherwise.
- w4 N4 C: h8 r2 m0 Z0 A3 f - >>> math.isfinite(100)0 D z! M" B5 m9 W
- True# a9 G1 W) F5 j
- >>> math.isfinite(0), G% [5 }. l, X. t
- True
, N; {3 W# w: W6 Y/ B! P, z( | - >>> math.isfinite(0.1)
- Y( m3 h! w# O# X! ]/ k: u - True" B( F+ ^" ]0 t9 A4 c5 x
- >>> math.isfinite("a")
$ p8 J! C8 `9 }( ^1 i - >>> math.isfinite(0.0001)
% M+ d2 W. j, O) z7 d" A - True
复制代码
0 O; b9 z# \! B- b2 ]# Omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False: E3 b" b6 k( R& p: Z% V( {1 [
- #如果x是正无穷大或负无穷大,则返回True,否则返回False, @6 Z4 s# r+ V# Q9 C: z
- isinf(x) -> bool
+ U" W" C' v) i, i* g - Return True if x is a positive or negative infinity, and False otherwise.# e5 ` P& @3 [% H
- >>> math.isinf(234)- K# X- E8 T: A9 T/ c6 {8 j
- False
$ P/ G8 o- P9 f, I - >>> math.isinf(0.1)
$ _2 ?8 E) g& q - False
复制代码 . f2 h; Q) A4 N: x/ j' c
math.isnan(x) 如果x不是数字True,否则返回False) H0 l; U& }# u8 H Z& x
- #如果x不是数字True,否则返回False
8 ?" D+ Q, S) q" Y3 u - isnan(x) -> bool
# S# G8 A1 p& f - Return True if x is a NaN (not a number), and False otherwise./ R9 h! u: U0 A+ \
- >>> math.isnan(23)2 o# W; f, [$ M" g
- False
9 C% _6 m0 @1 d# s! \ - >>> math.isnan(0.01)% @; X# P8 f i4 W& T( K( _; T4 {
- False
复制代码
+ F @5 q+ d2 F: W. y2 Tmath.ldexp(x,i) 返回x*(2**i)的值
o2 z) o' t0 D/ _- #返回x*(2**i)的值
; H$ Z2 b. i; w+ W9 K3 s - ldexp(x, i)
) g- g/ {4 u* h6 j$ Z5 H/ I - Return x * (2**i).) z) s( ^8 @, f0 K0 P
- >>> math.ldexp(5,5)8 H7 U6 Q3 P+ O0 w
- 160.0- Z3 j9 v8 z& | E( B
- >>> math.ldexp(3,5)/ \4 f) ]* d( K6 Y+ u+ O
- 96.0
复制代码
; h9 S' x% w2 Z4 d! U( ymath.log10(x) 返回x的以10为底的对数6 p0 U- P/ K& [6 k% a
- #返回x的以10为底的对数
4 Q; n4 |* r0 n& q - log10(x)' r1 q9 G. a* N x5 _
- Return the base 10 logarithm of x.1 a4 [4 N4 G( }2 b5 C9 ^' O
- >>> math.log10(10)
0 a# X7 v4 D5 T - 1.0( e7 d# C _2 g. z* w
- >>> math.log10(100) N$ X7 O# H) k' J
- 2.0
( F; M1 B: C! x4 L8 C6 T d - #即10的1.3次方的结果为20
% [7 C2 K! L' C0 z @ - >>> math.log10(20)1 w2 |2 J+ u% P" v/ J) ^' u
- 1.3010299956639813
复制代码
/ h0 ]0 M0 w, f! Y# S$ g; t- q; mmath.log1p(x) 返回x+1的自然对数(基数为e)的值
3 P- r# y( g2 _6 a: V) L' z- #返回x+1的自然对数(基数为e)的值; e2 h5 a3 Y1 v5 W. I9 k+ y. ~
- log1p(x)
! E% J7 i5 x0 T$ \, T0 L - Return the natural logarithm of 1+x (base e).
# j3 _+ u$ I) |% r, y - The result is computed in a way which is accurate for x near zero.9 p( p( F1 s4 ?$ t7 x, L( x
- >>> math.log(10)
. x) P7 B2 Q# p) i, _; W - 2.302585092994046+ s/ D: c3 }# |" o1 k
- >>> math.log1p(10)/ t( }. K* @) m3 B& R8 P" [, ]
- 2.39789527279837072 U1 N8 M$ E- B8 {5 b: |
- >>> math.log(11)
4 \+ r6 e: v D8 h - 2.3978952727983707
复制代码
7 ] e" B& i4 g8 n$ Hmath.log2(x) 返回x的基2对数
" p3 p7 E4 n9 |) y2 T5 \$ D# B ~- #返回x的基2对数
1 T" O4 \. l. P/ i/ L- K - log2(x)
1 i' U8 P; D0 m& b. v; ^ - Return the base 2 logarithm of x.% V* y( j6 O! A2 @' i
- >>> math.log2(32)2 ^4 g) J+ D& \9 w
- 5.03 `% T* l0 w5 k l8 g8 g1 q
- >>> math.log2(20)$ F! k; t+ m0 I4 Z
- 4.3219280948873637 ^% I6 s, A: f; _ N8 g; h. P9 \! p
- >>> math.log2(16)
/ D$ U- y j$ m1 D( E% y4 K - 4.0
复制代码
5 o; ~* [' @4 a* ~5 T( _% s# W9 cmath.modf(x) 返回由x的小数部分和整数部分组成的元组) d' q/ A- V! A$ k
- #返回由x的小数部分和整数部分组成的元组
7 ?& f0 m, o( ] - modf(x)) f: x2 y( W: Z2 p9 }
- Return the fractional and integer parts of x. Both results carry the sign
3 M7 A) f7 l9 O - of x and are floats.
x- a D3 k6 ~7 X4 E3 F - >>> math.modf(math.pi). o+ [; [+ U; f. E5 a
- (0.14159265358979312, 3.0)1 F6 h6 a# n- K6 Y+ |. @) S
- >>> math.modf(12.34)9 z2 Z! E5 X5 |# R% ~0 X, V+ H
- (0.33999999999999986, 12.0)
复制代码
2 a8 O1 Y8 v# k$ b7 v1 nmath.sqrt(x) 求x的平方根
+ Y/ l& h1 Z# e" _0 N1 p- #求x的平方根: S% v+ s8 P! Y$ C+ s8 q- r9 e9 N
- sqrt(x)
1 f* s1 {$ E* h8 S( }1 l1 b2 l3 R - Return the square root of x.
9 A3 U/ V+ J6 A; y6 m+ @ - >>> math.sqrt(100)- o& P0 Z* t4 D
- 10.0
, q* d7 L$ H9 @: g - >>> math.sqrt(16)6 H/ @8 q0 ~9 N4 @9 |# o
- 4.00 V, c6 i9 g8 Y- |& [1 n
- >>> math.sqrt(20)
" P( x1 ~: i/ e - 4.47213595499958
复制代码
1 K* G% W% _. S% V+ C8 |2 Y wmath.trunc(x) 返回x的整数部分- #返回x的整数部分
# _- ?6 J; H7 g- v - trunc(x:Real) -> Integral
! ~9 @/ C( s3 g% x( q/ r% y& o - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: B, D( O v7 K* D1 B0 ~/ a
- >>> math.trunc(6.789)1 Z( y U: x9 ], @6 V5 Y6 f
- 6' d$ R0 L2 F, B& |+ P4 q& F x. x$ B+ _
- >>> math.trunc(math.pi)
) z4 V" j r( S k' {4 h - 3' n; F' F% a$ l7 x
- >>> math.trunc(2.567)
& Z2 l& y5 Z2 H: j - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|