马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
. g/ E9 K) O9 {' Z" ~. M. G
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# R% X4 N- g8 E0 e6 n2 z- H
* w2 f! t2 [6 Y* _3 }/ q) [1 C
方法1:
" x' ]) C3 \, T5 C- >>> import math0 y+ ]% y' \& w+ |, O% B/ j* P
- >>> math.sqrt(9)
# `% x5 M9 ^' ~- `/ @2 J& Z W: p - 3.0
复制代码 方法2:
4 T3 j0 h# U( l) r" w0 r- >>> from math import sqrt! Y! ^: V, F, V9 } x- x
- >>> sqrt(9) B* u& Y0 R, ? ^; g$ A1 N
- 3.0
复制代码 $ U7 X) m/ C$ c. w) K& d# T. A
! r2 T3 Y. A S* C
math.e 表示一个常量" @, E2 ~" J. ^
- #表示一个常量% r& J' G. ^: ?( X5 ?2 u3 ]& @
- >>> math.e8 o2 p! C: M) a9 |5 n
- 2.718281828459045
复制代码
) Y; q; {/ w+ l/ {/ |math.pi 数字常量,圆周率
6 U, c* L* p: h* ~6 T- #数字常量,圆周率1 x; u0 h; c/ V$ ~9 q0 T* P% b
- >>> print(math.pi)4 f* I- X& T0 g r# }
- 3.141592653589793
复制代码 5 @7 H- D" V0 ]
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x( ^3 J# @* q4 g/ P; Y
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 z! W+ k' u0 K6 M; r( V" b3 s. ? - ceil(x), r) P5 n0 r. G6 R& X, R" t
- Return the ceiling of x as an int.) h4 ~+ ]( [( M3 a% t) h( i T
- This is the smallest integral value >= x.( ~9 l& w3 ~# _
- 8 z5 s' t! n' s
- >>> math.ceil(4.01)
0 y: i ~! v" b! N - 5& }# K3 W3 i* U, A* }/ K7 B
- >>> math.ceil(4.99)
2 n( F9 F( a, L- \ - 5
2 C+ m9 g! I) S - >>> math.ceil(-3.99)
! M1 h+ a* d" P - -31 K; Z. i- j' ~
- >>> math.ceil(-3.01)! x& F: v; ~6 w6 k$ x
- -3
复制代码
8 X4 @9 }4 j! n6 e4 n/ P* l. Lmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
* y7 X: i. I. c& e+ }- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身 ]) s2 f Y2 n7 g0 E' Y: N( K; \, l
- floor(x)1 r! }! { q" n
- Return the floor of x as an int.
4 s% K& V* }4 [8 u+ j - This is the largest integral value <= x.
5 \% H0 W/ Z q5 K* C - >>> math.floor(4.1), x, ~; Z" A; a5 [7 X, g" x. d* c
- 4) \! s7 X- [' a' j o" k
- >>> math.floor(4.999)4 G8 ]; l* v4 {, Y( {0 X
- 4
, _5 G1 W A9 E6 r& G0 f V - >>> math.floor(-4.999)# e" @% f- e# p' |; j2 ]& m% \
- -5
- P( b* p3 e/ F# {# ^ - >>> math.floor(-4.01)* ]$ d* Z! `. P
- -5
复制代码 8 ` w4 u0 D: ~' w: e. J
math.pow(x,y) 返回x的y次方,即x**y
6 W! @+ A# I; B: }6 x7 {$ @- #返回x的y次方,即x**y
. s0 _' t! F5 F& C3 r( e5 X - pow(x, y)' R9 L' Q3 P4 I! A; b0 _; |0 V
- Return x**y (x to the power of y).2 a2 M- [7 s. M) z' `
- >>> math.pow(3,4)# Z$ T# w/ x! G
- 81.05 a, _7 M: V7 G9 g) _$ e
- >>>
4 J* f; N3 M N* B3 w2 R' r1 A7 x: I - >>> math.pow(2,7)
: S: x8 x1 `9 U: B8 f - 128.0
复制代码
, ^: B+ F8 _& M- K4 ?- @. {" wmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ F/ D x, ~6 n7 f0 R
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) b! S! m* u3 [
- log(x[, base])
2 S8 L' Q! J) W. C - Return the logarithm of x to the given base.
# h! h! c: z0 |. [/ Z- E - If the base not specified, returns the natural logarithm (base e) of x.! \2 u; u" v. I, s1 Y* o2 \$ r
- >>> math.log(10)
}: u& }5 C) N; R7 p/ A& Y- q - 2.302585092994046
$ ]) C: _& d6 e3 f2 N. c4 j) f- _& e - >>> math.log(11)+ N2 l! v$ k& m8 q3 i. n6 w
- 2.3978952727983707
" L. E0 |8 e+ I( M - >>> math.log(20)$ O+ |: \. S8 A& `, {! P; ]
- 2.995732273553991
复制代码 0 K6 O5 q! h6 D1 ~- C/ R
math.sin(x) 求x(x为弧度)的正弦值4 W0 j! c6 f7 D. n: i+ S, _
- #求x(x为弧度)的正弦值
0 Q, Y- j- r/ g) }2 K - sin(x)% S/ t2 F9 d: ~8 p1 k
- Return the sine of x (measured in radians).2 O7 w; r% m( ^ Y8 K) @! [/ f
- >>> math.sin(math.pi/4)
: j' t7 y* D# ~0 G, R* C - 0.7071067811865475$ J5 y3 N5 z5 [
- >>> math.sin(math.pi/2)0 q9 H3 |3 F: @# a+ Y' T" b( K/ T
- 1.0: _/ v/ C2 Y, Q, ^
- >>> math.sin(math.pi/3)- C0 n# D) f0 S5 u! D9 U
- 0.8660254037844386
复制代码
: C. C" E! O m; |( n1 bmath.cos(x) 求x的余弦,x必须是弧度
7 \. ]7 h: X+ ]6 K2 j( d, w- #求x的余弦,x必须是弧度
" R6 I: w1 e+ U" ]& n( B: U - cos(x)
n; z/ S. @- C4 x8 a3 B - Return the cosine of x (measured in radians).& u1 I# K* O1 ^' d
- #math.pi/4表示弧度,转换成角度为45度2 Y* \3 V/ Z2 e- k
- >>> math.cos(math.pi/4); D; | Y: x5 i/ Q& M- F9 o
- 0.7071067811865476
2 C% o9 b: J, C2 M; V - math.pi/3表示弧度,转换成角度为60度5 z% c& J+ d- y+ @
- >>> math.cos(math.pi/3)
8 t) t9 V5 c W+ V8 T& c0 M, I3 g - 0.5000000000000001
) O( [% {" R5 H4 z! P - math.pi/6表示弧度,转换成角度为30度 M, y8 L1 T) {, C6 Q8 k3 M
- >>> math.cos(math.pi/6). |) ?7 t6 m7 }8 ^7 |
- 0.8660254037844387
复制代码
; b( {1 \" l* F0 b( M5 ~math.tan(x) 返回x(x为弧度)的正切值4 E' U' x0 A* j. h1 `; O/ Z
- #返回x(x为弧度)的正切值
, E- {: ]9 g+ y - tan(x)
( k0 c. \& O4 _* q; o+ C4 S; n - Return the tangent of x (measured in radians).1 I% W- _5 {7 ?3 {! n5 m
- >>> math.tan(math.pi/4) Y/ G, S# b) Z& n: S3 r4 }
- 0.9999999999999999- R Y8 y4 B+ ^) S( B1 i
- >>> math.tan(math.pi/6)
# ~$ [1 S4 q( O/ K; y/ A - 0.5773502691896257
- Y( Q# e6 k4 c+ f: W# H+ v/ ~ - >>> math.tan(math.pi/3)4 N' K m) K1 d) M" m+ k! r
- 1.7320508075688767
复制代码 J( n9 Z- E: _$ B
math.degrees(x) 把x从弧度转换成角度
2 S$ T* }) L3 F& p! R8 C& W% T- #把x从弧度转换成角度
5 ]0 D, a" M+ G4 @2 X - degrees(x)
0 R [! I. P/ s4 J. A0 [8 Y' i+ _ - Convert angle x from radians to degrees.
8 Z" Q7 |9 \. Q3 H9 O5 Z9 \) a - 3 [0 |5 c2 e7 `: \/ K4 i; @5 x
- >>> math.degrees(math.pi/4)" F1 `+ g9 H" T8 T P
- 45.0
! b" U* H* ?0 w9 f - >>> math.degrees(math.pi)8 Z. S4 ~$ K4 Y* G- q2 ` B
- 180.0# P5 i0 ?2 q T! _
- >>> math.degrees(math.pi/6)6 M/ d r4 ^+ ]4 l0 w1 p
- 29.999999999999996
: M2 U$ S+ y- B) @$ \* e - >>> math.degrees(math.pi/3)# h6 H) y9 H! [3 X) r7 u
- 59.99999999999999
复制代码
3 }4 d) l ]% N% z6 umath.radians(x) 把角度x转换成弧度1 V: y* X: T# M. f
- #把角度x转换成弧度7 I: ^& q z* V6 H; y7 z# l4 S
- radians(x)
5 F, q% e0 C7 Z' h$ P E. k' C8 ^) e z - Convert angle x from degrees to radians.
8 N2 `+ V7 J @9 U& t. F - >>> math.radians(45)' x% h2 P( \3 f0 y6 \6 b8 D; z/ u
- 0.7853981633974483& e* W9 Y7 {( {8 U6 M' A, P) [
- >>> math.radians(60)' B# _2 M( z; O# _! X b4 o
- 1.0471975511965976
复制代码 ( F! A, k( E$ U) t! |
math.copysign(x,y) 把y的正负号加到x前面,可以使用0: o4 B1 g1 {& X* E! r* e6 f
- #把y的正负号加到x前面,可以使用0
: f& d0 x* H6 e( p# Q' O7 a - copysign(x, y)# k! F, P% u) a# y* z' N& e" \
- Return a float with the magnitude (absolute value) of x but the sign 0 i& g. ^* i+ }! z. e% G* U( Z7 I
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
" g4 }% Z9 l4 q, W. j7 M - returns -1.0.
4 u" T4 _7 B( L) n8 [! N - ( i! G( P6 Z2 l( v+ Y; s
- >>> math.copysign(2,3)2 E; ~- C9 k/ Y& ` g. |' w' P
- 2.0
# e2 k0 T) E5 u8 y* [4 c2 w - >>> math.copysign(2,-3)# ]& [5 z1 Y/ @$ R5 S$ l
- -2.0
" ?8 g: S- G8 s, T - >>> math.copysign(3,8)! k$ U! {; q9 _, K' E& u
- 3.03 F5 c4 `. e" C& @) R& O( L
- >>> math.copysign(3,-8)) w: P% Q& w% t1 G
- -3.0
复制代码 / ^# s& ?! ?! n# u: k; ^- N; C
math.exp(x) 返回math.e,也就是2.71828的x次方
8 b) f. Q- n% c( s7 Q- #返回math.e,也就是2.71828的x次方( l; T4 v: h! L) ^
- exp(x)8 N" T/ Y u1 X+ p+ G: c
- Return e raised to the power of x.
5 i2 V h S/ r* K - 5 A# t$ ~0 ~: b2 t8 A
- >>> math.exp(1); I' |- p9 E R1 u/ I/ v
- 2.7182818284590452 g4 w( W1 H% C4 N
- >>> math.exp(2)
. F( x4 y8 E( p! D& j9 L( m" S# @ - 7.38905609893065" q+ d3 n' _) z2 Q
- >>> math.exp(3)% |; `) N4 y) p+ u
- 20.085536923187668
复制代码 , e! J5 w: |! w. n
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1* _$ Q4 t9 B% U9 ~9 }/ W
- #返回math.e的x(其值为2.71828)次方的值减18 N$ v4 ?9 P' q! M" K" n
- expm1(x)7 Q' N! B, H1 [6 }2 h' X O
- Return exp(x)-1.$ V6 C% X( O+ t
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." V" I7 n3 b' a3 s7 @% E, M
; U5 s: n- P- O0 ^4 \2 L9 O- >>> math.expm1(1)$ [/ a& }# g: {6 H: r8 x! F% [
- 1.718281828459045+ A! a3 T$ Z% k. L9 M
- >>> math.expm1(2)- O' ~, R+ R$ K
- 6.38905609893065
% w* c( ^/ x. `% a9 b0 v! G# T - >>> math.expm1(3)+ y, u, H! ^+ C' a1 m6 _" C
- 19.085536923187668
复制代码 3 e7 A. x! `" R' c- s0 Z- C7 V
math.fabs(x) 返回x的绝对值9 M; ^" J) `& q- o* @7 s% ~4 G
- #返回x的绝对值; S3 `! C* i7 _% A" I0 ^: L' u
- fabs(x)
$ G) n! A, O% B- ?0 C& a9 H - Return the absolute value of the float x.. X; N# |- J) w2 s& Z- L
- ' }# S( B4 Q1 }% }7 Z1 V
- >>> math.fabs(-0.003)
& J' F F0 R M - 0.003
8 N: b4 D$ H9 s$ c: ~ - >>> math.fabs(-110)% ^; ?0 K0 ?; c( n
- 110.06 |$ J: u5 I7 L/ j3 ^( r1 g2 a
- >>> math.fabs(100)* e# j2 O; a4 [+ v: ~1 F
- 100.0
复制代码 " _4 p, s: o" f' a4 P
math.factorial(x) 取x的阶乘的值
/ E: l$ Y$ |# M- #取x的阶乘的值0 i- Y+ o! m4 T
- factorial(x) -> Integral) l( A3 R& F e( d+ @3 Q- V, d
- Find x!. Raise a ValueError if x is negative or non-integral.- a0 n; D- Q4 V5 J
- >>> math.factorial(1)' r" V, V5 @; y, @6 S0 t
- 1& O6 o2 V* u: H. c# E% H
- >>> math.factorial(2)4 J/ K9 O/ L2 }$ y* G1 H
- 23 S+ M0 }" e4 d% P7 w: x2 b
- >>> math.factorial(3)3 q+ ?+ `* u# y- q4 U
- 6( T/ q1 A3 Q+ c4 ~5 k- f
- >>> math.factorial(5)
/ h4 K! i. U) A - 120* S; g- j4 b+ N' |$ S
- >>> math.factorial(10)
4 v# H# v8 w. A# f! e- {9 R - 3628800
复制代码
* H4 r$ o2 J7 m1 z5 A1 ]math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
6 U, `3 x6 v* Q" r c' z \- #得到x/y的余数,其值是一个浮点数, d7 ~ n1 v. u( }" C$ y
- fmod(x, y)/ R/ p7 M+ e4 j9 Y
- Return fmod(x, y), according to platform C. x % y may differ.5 y+ a$ J0 F, J; _! b
- >>> math.fmod(20,3)
8 {/ k' D& n8 q - 2.0# x: b! |* P) h7 C3 h( N0 X: P' S
- >>> math.fmod(20,7)
6 N0 F7 l3 o J" z( r - 6.0
复制代码
, m( C8 ^. i+ hmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; m8 K8 B$ I! m% b# E- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' |4 h6 M9 o L
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
O) o/ {9 X6 o' W) @ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, o, Q* N5 [: L# x. }2 ?
- frexp(x)4 C9 b- J6 w: O( ]- i# v9 R
- Return the mantissa and exponent of x, as pair (m, e).3 C6 C5 A* ?! q L/ k* E8 X
- m is a float and e is an int, such that x = m * 2.**e.
+ a" @4 ]8 R% N. H A0 a# S- p: k - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 b4 Q7 l( a6 D& D# N& e5 [' ]
- >>> math.frexp(10)( M9 d! F7 R& ], D- |. E
- (0.625, 4)0 p/ L) u! |! U. h/ W6 x
- >>> math.frexp(75)1 [* k6 Y* r7 c: _" F8 \
- (0.5859375, 7)
: I! L6 R' l+ n - >>> math.frexp(-40)3 z( z$ Z( V7 z3 X$ c% j' S
- (-0.625, 6)
! E5 b. g2 ^1 f' |4 U, h - >>> math.frexp(-100)! g/ m. O' f4 E k& ?
- (-0.78125, 7)4 k' \" k5 I; m! U; v5 E, }4 w
- >>> math.frexp(100)9 v K" f! X2 [- b$ t
- (0.78125, 7)
复制代码 , S: ?. x. g. y2 V( `! q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)' X7 {. k' |* F3 ~
- #对迭代器里的每个元素进行求和操作5 m8 c3 B) o0 C" L0 q
- fsum(iterable)
0 C# \; r. b: ^' X7 u - Return an accurate floating point sum of values in the iterable.
) H$ T) b0 t0 Q( H& l - Assumes IEEE-754 floating point arithmetic.6 R b3 N! | K. f% [
- >>> math.fsum([1,2,3,4])8 F5 B" K, k3 k7 w! g+ L
- 10.0
# x( {( x @7 b. Z; g! Y - >>> math.fsum((1,2,3,4))1 I1 g# Y6 ~0 l* z4 G- U4 z
- 10.0
$ B6 U0 ^# e9 P* u5 b - >>> math.fsum((-1,-2,-3,-4))
. T2 I7 h2 x. e1 k - -10.0
1 r# D3 q+ r3 l s P' Q - >>> math.fsum([-1,-2,-3,-4])
/ _% R& Z9 ], y0 k: ? - -10.0
复制代码
8 g4 \; H' I) v e* G4 [/ Amath.gcd(x,y) 返回x和y的最大公约数
/ s9 ?( i8 m: ?- #返回x和y的最大公约数
; |, G( k/ m5 Q7 w' ` - gcd(x, y) -> int; n1 _: i6 L5 P; E* T+ h
- greatest common divisor of x and y5 Y) `0 B$ A, c. ^! |, u
- >>> math.gcd(8,6)
6 q! J% ^* B6 e- G. p$ V - 2# L6 K! O7 _* D7 b/ |( M
- >>> math.gcd(40,20)' {) A1 m! S) j1 Y" P4 @+ m
- 20
z1 k" |& l8 E( G$ b - >>> math.gcd(8,12)0 c) p o3 S3 A' a d& n
- 4
复制代码 9 ~2 L* P: o; h1 A! A
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False( H: E% q# m% w
- #得到(x**2+y**2),平方的值
1 n% N3 P$ O r- M% P' ~ - hypot(x, y)
; }7 ^! e7 z' S( t, D - Return the Euclidean distance, sqrt(x*x + y*y).
5 T7 H& `) v' D8 G: D# Z' Y8 @/ }7 | - >>> math.hypot(3,4)
- T# o6 o' p/ s! ]& h - 5.0. n" A4 v& c5 L" Q8 W
- >>> math.hypot(6,8): j) l; |% k0 m
- 10.0
复制代码 , x- }. l# t) \6 J
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False1 ^; p: J0 V; J7 [
- #如果x是不是无穷大的数字,则返回True,否则返回False2 {6 Y c$ \" s3 `/ c- S
- isfinite(x) -> bool7 T9 M- {3 T0 E8 j0 U) c
- Return True if x is neither an infinity nor a NaN, and False otherwise.2 m, \. f1 }+ }. l' `2 D2 I
- >>> math.isfinite(100)% E* ~9 }- o& v; k* S* R1 W
- True+ T; H6 r7 B/ H/ a
- >>> math.isfinite(0)
) a9 D$ _5 U' z8 Y: } - True
7 X J* F8 N% e, W - >>> math.isfinite(0.1)3 R. A2 l" c2 y+ o# U0 h
- True
! l Q( l" P' [% l9 J - >>> math.isfinite("a")* s2 A' T7 p+ D1 P1 }
- >>> math.isfinite(0.0001)
* o) y: k% y3 ^% D' | - True
复制代码
7 D2 O: J# n( ?& Mmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False! j+ ]- `' q3 B( U: a
- #如果x是正无穷大或负无穷大,则返回True,否则返回False) Z- ~' h2 a8 P9 S4 K3 z" `) @7 M: l
- isinf(x) -> bool
8 a* C/ i: ` ` - Return True if x is a positive or negative infinity, and False otherwise.
+ I* Q* S8 ~& k" W1 j6 ^ - >>> math.isinf(234)
# x- |) g2 O( D8 x& n S - False% F' W& l6 N& J" W5 S% E+ S% y! D
- >>> math.isinf(0.1)
$ q# U* K9 ]: Q% O" Q) H - False
复制代码 6 f' l# I7 e5 A Q/ q, \
math.isnan(x) 如果x不是数字True,否则返回False
' c5 Q0 {; S* H7 t1 D- #如果x不是数字True,否则返回False
* t! L9 G: v% J) M h9 P - isnan(x) -> bool! L) h) t+ I$ y- \6 r2 v3 Z, t
- Return True if x is a NaN (not a number), and False otherwise.
( t2 H/ l% u* W# R; q7 S - >>> math.isnan(23)- J; j4 \. h5 @- Y1 x" D% i0 P
- False
4 Z, ]- y' _/ e/ d5 S. Y - >>> math.isnan(0.01). i1 {4 J" d: [6 _" a
- False
复制代码 ! T6 ], t' d1 D2 d; b
math.ldexp(x,i) 返回x*(2**i)的值! `5 D3 N# L- T% _) O
- #返回x*(2**i)的值
0 d# b9 G. Z3 {0 H0 H - ldexp(x, i)
3 _' R" G' H! B% \ - Return x * (2**i).
7 P( Q- x9 A' m5 j9 ^; J- r" [+ A. E - >>> math.ldexp(5,5)
& R& x% q0 a: l( E9 G, i- I. V: u _1 N - 160.01 S2 ~9 m1 V- o2 T
- >>> math.ldexp(3,5): F0 {' X" G- z; D$ ?
- 96.0
复制代码 * W/ ^. w: h/ w' s7 T' ]& G1 U
math.log10(x) 返回x的以10为底的对数
% |+ F, E& t. Y0 N3 E4 Y. z- #返回x的以10为底的对数
: A1 Q+ h' Q+ C" n( Y - log10(x)
! p7 j4 e( J) a2 \6 } - Return the base 10 logarithm of x.
# L# g4 m3 u9 }5 n, D - >>> math.log10(10)5 B0 B: T' O1 s; \! J$ f) C
- 1.08 K; [' U' X1 J0 H- J
- >>> math.log10(100)
3 O0 {5 }; e- F0 W ^# S9 I - 2.0
) n! h: n. m2 M9 s' R5 |3 i" p J - #即10的1.3次方的结果为20- ?* ^2 _% q# t8 a2 ~! s% G
- >>> math.log10(20)" H. e! q: I7 u: G
- 1.3010299956639813
复制代码 ! t/ L' f% ?3 w9 T3 e
math.log1p(x) 返回x+1的自然对数(基数为e)的值
5 g% N3 o* O( y- #返回x+1的自然对数(基数为e)的值
6 d! U6 V* F& ~ C - log1p(x)0 U! D% S, z+ Z% h( |; U2 N! s @5 ]
- Return the natural logarithm of 1+x (base e).1 k/ V. W, d) `0 O3 n7 |0 { |7 m
- The result is computed in a way which is accurate for x near zero.6 }$ j! R# d: {; |: a
- >>> math.log(10)
# S' D7 `7 u% p# k, R# h; Z - 2.302585092994046
0 k5 l' ]/ o/ ?7 {6 s* S) r - >>> math.log1p(10)) Y1 I: Q; ~9 B) l& |% x
- 2.3978952727983707
2 {/ Q, K6 o p: G- y - >>> math.log(11)" Z5 L# I7 ^) ~8 n5 T6 F
- 2.3978952727983707
复制代码 + @3 e- U! S+ o* }5 B( n
math.log2(x) 返回x的基2对数# i7 A/ o6 @2 x( V( D
- #返回x的基2对数
) X' g* B9 d7 W4 o5 z2 ]: P" f6 l - log2(x); V2 x3 m- ^, q) c( g1 j7 X
- Return the base 2 logarithm of x." u6 K* c8 L4 M$ |3 }! A9 W# W. b
- >>> math.log2(32)
4 V$ l# U' X ] - 5.0+ K) d2 C3 M4 } g9 Z2 R
- >>> math.log2(20)) C# p# T( d0 ^8 X0 d$ {% C' i
- 4.321928094887363
1 h$ A( k$ L/ h u+ c; P - >>> math.log2(16)4 G2 B- D) R$ m, _/ f9 U# a
- 4.0
复制代码
# S& J0 w" Z% W) r; K; T5 ?5 fmath.modf(x) 返回由x的小数部分和整数部分组成的元组
+ \& y. O8 f8 p- #返回由x的小数部分和整数部分组成的元组: x5 X( S, m' k$ l, S- }8 g, F
- modf(x)
! a x* c, \- Z) }% H1 `% e! e6 T - Return the fractional and integer parts of x. Both results carry the sign
& b3 l+ O+ _- J - of x and are floats.9 T! T1 h1 ?& X
- >>> math.modf(math.pi)2 s9 M- n2 \/ t* Y' L
- (0.14159265358979312, 3.0)+ A+ r% ]6 T) P. R
- >>> math.modf(12.34)
+ H& _! l, z: g. {: G - (0.33999999999999986, 12.0)
复制代码
+ V/ R8 o4 q+ }/ w9 Bmath.sqrt(x) 求x的平方根
m+ \* X/ W* L% o( f, ?- #求x的平方根
" `, w, z9 G8 d5 f: b- g* J - sqrt(x)$ l/ o$ b0 |7 a1 i( v) m
- Return the square root of x.: u# \( O# h" Z, |' s; Q4 s
- >>> math.sqrt(100)
! i1 o- V M( h; E' R - 10.0
+ C5 F# o7 ]4 F& B8 w: j - >>> math.sqrt(16)
# o$ ]) V. m3 c - 4.0& ]5 f3 G% p/ u7 Z* ~
- >>> math.sqrt(20)
$ W7 N1 L4 ?8 _0 g2 g9 u - 4.47213595499958
复制代码
. T! g! x% A( P+ ^" Emath.trunc(x) 返回x的整数部分- #返回x的整数部分5 ^' t0 w0 u* b$ c+ r3 }* q
- trunc(x:Real) -> Integral! R' w+ C9 w2 T, Y
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 M- `) X" y; I! O
- >>> math.trunc(6.789)
; l' }: | m; d" _+ l6 k2 E. i - 6% ?1 R2 d5 @3 ~6 U8 j: Q
- >>> math.trunc(math.pi)
& G- P" w! [ H! \ - 3: C: `: x! N' u- V
- >>> math.trunc(2.567)6 ~* g0 k$ O( n0 e8 @3 ~
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|