马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 |6 Z' A# V3 @9 Z% f6 @% E6 \, r
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。* g% i" T& d. q6 Z! j6 v( p
: B: V- J+ A! g/ }) P
方法1:
% b. ^. J& y5 o& O5 m. f- >>> import math) _# k; w3 ]& u, _# z: c
- >>> math.sqrt(9): ^( u2 Z0 R G' M8 V8 t2 v
- 3.0
复制代码 方法2:& v/ e% e- e7 V8 S5 G- X/ R
- >>> from math import sqrt5 I$ Y" G. j& \# B$ P0 j. d+ g5 e9 @
- >>> sqrt(9)
% o- j, V r* \# _: `5 _/ f' f - 3.0
复制代码 6 Z4 ~: }. K' s- _* ^( m; h: ?! f0 c
, K9 K) s f8 X g. E+ nmath.e 表示一个常量
% J7 d! H0 e& U4 L! y7 X& M' x9 k- #表示一个常量
5 `2 m% X1 o/ ]! g8 G - >>> math.e) |- H* p: W: F( l
- 2.718281828459045
复制代码
- t5 C+ A* A: `5 z. u u5 \math.pi 数字常量,圆周率( t& r" n2 O9 K. F$ k8 e
- #数字常量,圆周率# i% ~1 j7 n' G+ \5 {" w7 Z. @9 Y
- >>> print(math.pi)
G6 F( J9 Y2 w( V: u6 ` - 3.141592653589793
复制代码 , ]& N, `. X8 k! s
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
& b5 M) g2 g4 B/ P6 ~' v8 ]) h1 C- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 T7 F/ ^3 a0 l - ceil(x)
# V; k4 e+ s9 K - Return the ceiling of x as an int.
2 @! A. V2 ]/ D$ I5 X - This is the smallest integral value >= x.
5 G/ g: e8 I1 k( N2 w" J - & R7 s% c& q1 _5 y" O( L$ A1 r, |
- >>> math.ceil(4.01)5 I5 ?5 N5 s" ?8 e
- 5
* T/ |9 m( V/ a3 x0 {! d( y - >>> math.ceil(4.99)
' O: R% Q+ g4 F2 X Q- L; r& U. K - 5) s" V$ r* @+ n9 C* O7 ]
- >>> math.ceil(-3.99)
$ ]0 ?$ H/ M2 f6 J - -30 {% B. H5 ]4 b) ~" R
- >>> math.ceil(-3.01)+ F* s: T# T* M3 U
- -3
复制代码
7 {; S7 q8 m! M& y, nmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身% c- E/ n+ L% E" a
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ |: o2 a' {4 h, K+ N
- floor(x)/ t7 j$ |- {/ G4 }( D7 n1 X! {
- Return the floor of x as an int.
) U8 I' c1 c! J3 }4 U& ? - This is the largest integral value <= x.
0 Q. t- T8 l; X" R4 x/ B - >>> math.floor(4.1)
/ k, R. T: [6 V# a - 4
. q: A0 z5 x/ T$ c3 `6 g - >>> math.floor(4.999)
2 V' g4 m5 Z9 Y& [& n# F8 n - 4( T4 S9 [" n' w- j3 H
- >>> math.floor(-4.999)
+ e; J0 _, a( P( I - -5
+ R$ ~. E6 Z3 W% U8 t2 Y - >>> math.floor(-4.01)8 e* Q: Z0 A3 C$ Y
- -5
复制代码
. q0 C' z6 a, E% u/ n$ m* [: v. ]0 Kmath.pow(x,y) 返回x的y次方,即x**y
+ I+ `0 c7 ^) R0 x* Q1 x: Q& F- #返回x的y次方,即x**y
& }9 F- ^5 e. q% o. \2 @ _ - pow(x, y)
$ w- |) {2 R2 G @% n - Return x**y (x to the power of y).+ A5 [ H2 K+ M4 d
- >>> math.pow(3,4)! ^9 q, v/ R+ H3 ]! ]
- 81.0" K4 E$ L" R1 N% M6 r9 v, h
- >>> ! Y4 l; G5 N8 H) @0 P
- >>> math.pow(2,7)
' w5 d6 n$ x- |0 v - 128.0
复制代码 . k& H5 f! a& t3 v2 _
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 N. t! |0 h: y- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, X+ z6 r$ X' a8 ^ - log(x[, base])
8 L- C0 F4 S( l4 v( f9 n' N* ~5 @ - Return the logarithm of x to the given base.
# r: E6 P) v- C8 L- b" }2 z - If the base not specified, returns the natural logarithm (base e) of x.9 ~8 o) H, l0 D) W" T/ Z" x
- >>> math.log(10)) [5 g( @# L8 \7 y5 I/ w
- 2.302585092994046
# A9 D: N/ q Q. |3 m6 e, Q - >>> math.log(11)7 x, S& U/ d! D, H" Y$ U! J
- 2.3978952727983707
9 D! H9 u. v/ }) q - >>> math.log(20)
( D) J' q* o$ w - 2.995732273553991
复制代码 1 h% Q. v% z& e. L& R* X
math.sin(x) 求x(x为弧度)的正弦值9 f7 C5 }0 ^6 i, u! v
- #求x(x为弧度)的正弦值
9 F: n( X$ D, W8 D - sin(x)/ ~ V2 G" ?" o
- Return the sine of x (measured in radians).- ~! h5 y% C, U9 N; m) v _; u
- >>> math.sin(math.pi/4)
1 D5 m- ^; w" `0 d5 C( w - 0.70710678118654757 j0 W8 y5 j* V4 l
- >>> math.sin(math.pi/2)' S4 r0 ~1 p; I& A
- 1.00 J* M9 M# E+ _- V3 Q3 v4 }
- >>> math.sin(math.pi/3)
2 N/ M+ w, [ b7 {$ y N - 0.8660254037844386
复制代码 4 o& a7 P" Y& e: Y
math.cos(x) 求x的余弦,x必须是弧度3 [6 m4 }6 S' W
- #求x的余弦,x必须是弧度
+ h: W C" p+ _5 m+ W - cos(x)
$ Y- R5 [, G6 C2 K0 T3 R/ v, x - Return the cosine of x (measured in radians).2 [9 F6 t1 _) p7 z; W) L" l
- #math.pi/4表示弧度,转换成角度为45度
* M* s3 B, I% V# K0 }$ e8 { - >>> math.cos(math.pi/4)! J( h- u$ `3 H e* `9 ]* T/ @
- 0.7071067811865476
( M7 n3 w$ Q4 E* C# X - math.pi/3表示弧度,转换成角度为60度$ ? I* U U! v3 q4 L
- >>> math.cos(math.pi/3)
) a* J& c8 t5 F# p; L; x - 0.5000000000000001+ F* w7 c2 m1 d8 E. Q
- math.pi/6表示弧度,转换成角度为30度% P, G/ B! Z1 \
- >>> math.cos(math.pi/6)$ U8 z1 ^' A8 I& ?- y* d
- 0.8660254037844387
复制代码
# o% b) P2 K+ I( r: g( }: A* Vmath.tan(x) 返回x(x为弧度)的正切值, M- F6 E$ M% q, j# z
- #返回x(x为弧度)的正切值4 r* [; @. y4 ^; r$ w" W
- tan(x)
! U3 n' x( v- \5 ~ - Return the tangent of x (measured in radians).
- z- e8 {# o' F. v o. B$ t+ x" t - >>> math.tan(math.pi/4)0 j# m0 r* G( E; V- t+ P
- 0.99999999999999994 D0 |1 _/ d4 |4 E- t& `/ z, `, T
- >>> math.tan(math.pi/6)
0 L& X" Q1 o9 I( ^+ B0 I - 0.5773502691896257( z' G( s# F5 L8 l! y- r: N" m
- >>> math.tan(math.pi/3)
) W$ h& k3 K+ B+ G1 g) T; y - 1.7320508075688767
复制代码 0 a7 g! m0 d9 I& }, x4 N
math.degrees(x) 把x从弧度转换成角度2 \' m3 ^3 I3 B& F/ @. h( n$ ?
- #把x从弧度转换成角度- H5 d. G% @, v2 O( V
- degrees(x)% I- s o. S2 z5 q% t/ @
- Convert angle x from radians to degrees.
+ X Q$ X; O6 g6 P! @5 O& e8 q
+ k* l3 |7 ]! P2 s T/ w- >>> math.degrees(math.pi/4)
9 B8 ?1 W, Y. o- i; q - 45.0- T1 `* t- Y! e; x! g
- >>> math.degrees(math.pi)
( j" O6 H% X# j% j - 180.0) ?! E4 f# U5 G4 r+ R0 N
- >>> math.degrees(math.pi/6)
: }) Q) n* B4 s8 Z& u. [ - 29.999999999999996, N: O/ ^5 i% L; t9 h9 _% S1 u
- >>> math.degrees(math.pi/3)8 E( Z F" M' e6 w- w4 R! b3 w1 A
- 59.99999999999999
复制代码
) q) Q4 z7 P# j2 {; K: ymath.radians(x) 把角度x转换成弧度) s$ N# ^5 [. ]' F& |0 _
- #把角度x转换成弧度
: }. J% ^; \, C! N1 Q2 J - radians(x)* |- _3 G* ~- T4 b7 ]/ K7 o
- Convert angle x from degrees to radians./ V1 T- \! ]) S. s1 d
- >>> math.radians(45)
* Q& ]) V) u' \, o - 0.7853981633974483
+ P3 Q0 Y8 V; C2 ?+ P1 x. C - >>> math.radians(60)1 m+ h C8 U! C+ `! x$ Y
- 1.0471975511965976
复制代码 9 J/ G" ~3 h" I/ o3 \+ @4 }
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
/ e' p" g% {$ ]- #把y的正负号加到x前面,可以使用0
! S& p G4 }" J$ | - copysign(x, y)
$ r" G0 Y6 i8 s% N' [4 O - Return a float with the magnitude (absolute value) of x but the sign
t0 o, q/ F7 r - of y. On platforms that support signed zeros, copysign(1.0, -0.0) + K7 ?6 H# K& D( O) _; Q# R0 m
- returns -1.0.
- E1 N2 g7 \- ]7 \ b/ i - 7 m c F8 j. v* Q' a
- >>> math.copysign(2,3)
) T& Y z8 q- A& ^5 d- y - 2.0
7 a( x7 {6 u* u/ [; t$ D0 A - >>> math.copysign(2,-3)
. G. @0 G {3 Z* z3 u ^ - -2.0
6 [1 @# c1 S0 A, T5 p! ~ - >>> math.copysign(3,8), E5 |8 ~: O5 q. }$ o( Y3 q
- 3.0
6 n) Z$ a3 x; v* ~! g1 O- }" n - >>> math.copysign(3,-8)
" X4 z2 F$ _6 { o - -3.0
复制代码
! ]5 ?; C# h7 Z8 }3 q2 I* emath.exp(x) 返回math.e,也就是2.71828的x次方
5 D1 |3 \7 u( ~! m j& T- #返回math.e,也就是2.71828的x次方2 }4 {2 Q* u0 Z# U
- exp(x)
8 y, n; ~% l' X% R - Return e raised to the power of x.. m# R- D+ Y. E9 F6 J6 H, d+ [
- 4 P- q8 ]* ^( E1 }8 Q
- >>> math.exp(1)6 H; K2 b) ?8 G" j, B
- 2.718281828459045
4 D0 j T) u/ k5 t+ u' ~# @ - >>> math.exp(2)0 N+ C" q3 V$ L+ `- [6 _& T+ }
- 7.38905609893065
! U7 J5 I& W8 Z - >>> math.exp(3)) r: m) Y; p/ S6 X& o) _1 O! Y, N
- 20.085536923187668
复制代码 - o9 }, L! r, P; A5 j
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
0 n" o$ Z& S: ^ N0 J8 W9 o' Q- #返回math.e的x(其值为2.71828)次方的值减1
1 I9 M- U4 \% L% r' }- i - expm1(x)& n& ?8 A. k# j a/ p* }3 U0 v
- Return exp(x)-1.6 M' K$ a& f" o0 d; }' V7 b
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ n5 o0 u+ q* O4 X' J E
- # l& I2 j4 ^& f( A
- >>> math.expm1(1)7 D5 ? M: J( S6 Z
- 1.718281828459045! w) K: p' G8 f" E* R9 q/ N
- >>> math.expm1(2)
) {: A; j! T$ b Z - 6.38905609893065' n8 J" k3 r1 R7 a0 c- Y' e ^
- >>> math.expm1(3)
2 {& U: F6 n! i$ { - 19.085536923187668
复制代码
2 |9 q5 ~* E/ M e) y/ h4 _+ umath.fabs(x) 返回x的绝对值
$ p: z Y! E$ H: c- #返回x的绝对值+ k h. H) J( T* U
- fabs(x)
1 p) X7 m1 l/ l; Y' O - Return the absolute value of the float x.
5 v3 B. K4 x& ] ^. M4 p - ' I! Y" D7 [$ d& H+ m+ M" B
- >>> math.fabs(-0.003)4 B! E! u7 S+ E
- 0.0039 ^. a) v0 d( T+ b2 n* z2 J
- >>> math.fabs(-110)
4 T* E8 Z9 }7 q! H G - 110.09 a0 h+ Q9 e s
- >>> math.fabs(100)
/ x: N* |& B) i' I1 |9 D3 h/ g8 v - 100.0
复制代码 5 o: w2 Y# T- q8 R/ J
math.factorial(x) 取x的阶乘的值* w6 J) W2 g0 q, w+ C* h0 z
- #取x的阶乘的值' x6 F) G# u8 R# I4 ~( }: U
- factorial(x) -> Integral
$ w( E2 t5 x1 B( A - Find x!. Raise a ValueError if x is negative or non-integral. X" { i; Y. J. j4 ^/ w/ U
- >>> math.factorial(1)
" @. L. E; N# [0 D) Y - 18 c* L( t0 T, V. f: h0 T
- >>> math.factorial(2)
6 M' e' R/ `# F3 S( ?) l5 ]7 x - 26 T& p) O5 t0 ~
- >>> math.factorial(3)/ v/ T* H, l) @, [8 w1 a) q
- 6! T" q/ N/ H) V3 Q( c+ C2 ~
- >>> math.factorial(5)
$ o& Q9 E" D) r3 A( i" L - 120
2 q/ X! q( ~* u0 H/ U& z - >>> math.factorial(10)
$ T" D* t9 g; M/ q' x - 3628800
复制代码 3 o/ n! k5 R6 ?) q) |
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
. H P6 U' D& d& v' _' G0 L- #得到x/y的余数,其值是一个浮点数3 r" M& F v5 j+ t4 U% O
- fmod(x, y)) W" ?" n4 B. ~. @
- Return fmod(x, y), according to platform C. x % y may differ.
) Q F5 U, h* q8 L, Z' l% L: f - >>> math.fmod(20,3)* z$ a' o7 F1 o- k; r( @
- 2.05 w/ B- D) u* v; a6 v4 s) }: @
- >>> math.fmod(20,7); w; U; j3 |$ G
- 6.0
复制代码
* s- B1 d6 B& J- L% dmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围# S1 r/ c3 c. \# ~$ A S3 @, [
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,# Z, }. ^7 X3 E3 N4 X3 F
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
" {5 \ S9 S$ |; a9 I - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1. t' h" A( f: c- S5 p) Z% X0 F
- frexp(x)
# u& G4 O9 V5 g' p0 |1 ? - Return the mantissa and exponent of x, as pair (m, e).# t3 z' T# w1 P6 i) v
- m is a float and e is an int, such that x = m * 2.**e.
) l( n2 J* q U& x& ~: s. ~ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" w( n2 e% E7 V6 c" k0 Z$ q* M3 S - >>> math.frexp(10)# p2 P4 j/ q# J$ S2 M) \- q
- (0.625, 4)
& {. ], A! q5 s - >>> math.frexp(75). w4 r% L9 ~0 R7 }1 V
- (0.5859375, 7)' |/ W- s; W! {5 }9 T5 w
- >>> math.frexp(-40)
% r' Q- A) T; s* J9 C - (-0.625, 6)
& n+ ?7 @' j% W. m* o, s' x - >>> math.frexp(-100)
3 U/ z8 t* v7 B$ H9 v. M) V - (-0.78125, 7)
( f6 J" U: F/ j" H8 i6 h3 j$ T0 j( h - >>> math.frexp(100)1 ^+ A0 e! \( l$ U, Q/ ^5 Z& W* O
- (0.78125, 7)
复制代码
! ^$ ?+ a: Z q* g7 b. p: \math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
6 J: \; Y- M+ T/ \5 D- #对迭代器里的每个元素进行求和操作& Q/ M8 W7 Y. u% [# w
- fsum(iterable)
7 o( \! M8 G# c- ~3 H3 [ - Return an accurate floating point sum of values in the iterable.2 a7 v, |2 Y9 z5 d. \+ _
- Assumes IEEE-754 floating point arithmetic.3 q. D3 H% U2 T Y6 M |6 ?% u
- >>> math.fsum([1,2,3,4]). [- o( x7 u8 y- i- f: e# b" c
- 10.01 F+ Z! r+ ?; n( S' z
- >>> math.fsum((1,2,3,4))1 F6 Q! `! E% T/ y( c5 ^
- 10.0
Z G+ H& L0 x - >>> math.fsum((-1,-2,-3,-4))
! c$ s, P" ~" {, X: r0 D - -10.0. ?% G. r4 t0 t U" X- T+ A- `6 l
- >>> math.fsum([-1,-2,-3,-4])
4 I+ T- x, q Z% g& i5 } - -10.0
复制代码 ) j- t2 _: s5 n I
math.gcd(x,y) 返回x和y的最大公约数
& P% |# `( Q4 y+ H. x& I. a8 x6 H- #返回x和y的最大公约数
: t7 T% K, L. d - gcd(x, y) -> int! L& g" R0 \3 G' |. P
- greatest common divisor of x and y. ~, K( b$ f, M5 N$ D% y: V
- >>> math.gcd(8,6)0 J' ` e0 [. j6 Q0 u9 `9 U
- 2, d) K/ Y! w- |) @) F7 m, I( c$ p
- >>> math.gcd(40,20)
1 v) ^% P1 b% W& I* k) @: C" `0 B - 20
D2 H9 @& w1 C6 W - >>> math.gcd(8,12)
: {3 W, J+ Z$ Q1 H- \8 i - 4
复制代码 # o- [ U& G% C: }8 a
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False9 h% K7 {* r# B; B5 S
- #得到(x**2+y**2),平方的值$ n& Q! [# b4 Z8 b5 F
- hypot(x, y)
4 l& Y% N L) ^+ |5 n; } - Return the Euclidean distance, sqrt(x*x + y*y)./ F+ h: o& z3 z: g9 @4 p$ O
- >>> math.hypot(3,4)
: p& v0 |/ V I4 T2 g; F8 A6 v - 5.0
% D6 Q/ p1 l) O/ R - >>> math.hypot(6,8)* K6 A) m9 o0 _5 W$ p
- 10.0
复制代码 5 A: V! U! L" ?: j4 \. t9 h
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False9 o0 A! d9 M0 n7 s2 P: v
- #如果x是不是无穷大的数字,则返回True,否则返回False+ U0 ~1 P6 h- O: g
- isfinite(x) -> bool/ ^; V+ t3 M" D+ R& c1 n( F7 u% d
- Return True if x is neither an infinity nor a NaN, and False otherwise.' R" g5 B' D7 I m$ S) \, t
- >>> math.isfinite(100)
* `' b5 k7 c9 h, _1 D - True3 [; z' u5 r# x# u- p
- >>> math.isfinite(0)
5 }* h0 u9 L3 @0 q8 a6 H - True
( Z/ Z1 o; g5 A9 v9 V" z! i$ J - >>> math.isfinite(0.1) B8 |' n: }* y& U3 t5 X
- True
2 w. l3 j5 b# _ - >>> math.isfinite("a")$ b( c5 O0 T7 Y a
- >>> math.isfinite(0.0001)& j0 z9 K* e" A4 ]% K9 V
- True
复制代码
6 a- r7 R5 W" O9 Nmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False3 J0 [% K: ^2 O: Z, g# N) n; [
- #如果x是正无穷大或负无穷大,则返回True,否则返回False, t! g8 }( W L' v0 y2 h+ ?1 }
- isinf(x) -> bool
6 z3 S/ @: r, Q9 G; v; `- Q/ |: N - Return True if x is a positive or negative infinity, and False otherwise.8 g# n S! T! u: H7 I# D7 n/ \1 L
- >>> math.isinf(234)
0 I, i5 l8 d; f6 w" u% s - False+ u4 n3 k& I/ {
- >>> math.isinf(0.1)
- n- i2 Y3 S% O! Z9 G5 e - False
复制代码
% W! _( n, }% }# ~4 k! l( r* Kmath.isnan(x) 如果x不是数字True,否则返回False
( c( B, e! l. G. R6 k- #如果x不是数字True,否则返回False
# U: A( v! Y1 \& \ - isnan(x) -> bool
% j" K, M$ U5 h" ~) S0 W& p) q - Return True if x is a NaN (not a number), and False otherwise.! N( _( Z* `' M# {5 p
- >>> math.isnan(23)
" C! |- u2 B/ ]% H. |- ? - False5 E- W! d# G. Z4 X! l2 I
- >>> math.isnan(0.01)0 ~# Q6 n0 `$ S& @* s
- False
复制代码
% Y6 B3 S4 j3 R8 k' smath.ldexp(x,i) 返回x*(2**i)的值7 y' ]3 s) \. e8 o: o
- #返回x*(2**i)的值
" @9 g5 B' K/ b% g9 u8 s - ldexp(x, i)
3 ]1 U- q8 s8 ~8 [# V7 L" a - Return x * (2**i).
7 A+ b2 z8 j" K( t9 Q4 |: S - >>> math.ldexp(5,5)
/ l y$ O" N# D8 m, _ - 160.0
: P! q- f9 T1 T& [ - >>> math.ldexp(3,5)
* Z6 ~0 t8 A5 f6 W+ S2 J* O - 96.0
复制代码
) K* Q2 Z1 ^5 a0 N- }2 O. Tmath.log10(x) 返回x的以10为底的对数/ W" a' h, U. ?* g
- #返回x的以10为底的对数
. Z/ Z+ C" S' w) K - log10(x)
4 E! S3 b5 r) u: ` - Return the base 10 logarithm of x.
4 W- P5 H, j* {* t* B% z3 K$ G, z, M - >>> math.log10(10)
$ q/ d. ?# M l# } - 1.0
! V/ d, @; m7 c* C' V, K0 c0 Q; K - >>> math.log10(100)( r) {7 r: d$ q7 U' H' D! |
- 2.0+ {" {0 p8 b& F# |: s2 k
- #即10的1.3次方的结果为20! |$ R7 j. z1 p; s
- >>> math.log10(20)+ A. ?; n* q- C8 v9 V! G
- 1.3010299956639813
复制代码
' h& t4 t: s' }) @* n0 z$ l; bmath.log1p(x) 返回x+1的自然对数(基数为e)的值
0 T( o. r/ S- I- C- #返回x+1的自然对数(基数为e)的值, o0 f) r; f9 K+ h, ]5 p: r, ~
- log1p(x)
; t% v+ V! ?' |) F2 B - Return the natural logarithm of 1+x (base e).1 ]: S' p! W# L9 @3 j# F
- The result is computed in a way which is accurate for x near zero.: w0 e) s9 P) g) | v. ^
- >>> math.log(10)
, _$ U; c) a0 @. D3 A - 2.302585092994046) J- Z+ u! G3 T- H4 S+ ~" c) J+ b
- >>> math.log1p(10)
8 f# Q/ I' x( k8 D9 a! l, y - 2.3978952727983707
& D! h9 X/ A3 u; K - >>> math.log(11)
+ e, e6 R1 Y8 R* `9 J0 e - 2.3978952727983707
复制代码 " o# _$ i5 W( X1 b$ x
math.log2(x) 返回x的基2对数4 y6 K% w- I6 J) h
- #返回x的基2对数
3 E% X$ |4 D9 T0 q" X- U _ - log2(x)# k( D2 R0 M7 C4 j
- Return the base 2 logarithm of x.1 s) s; o$ K2 U/ U$ P E _
- >>> math.log2(32)
' J3 f1 c. v. h1 Q- s% ~" S. Y - 5.0
* W) i- J' ~$ }( |1 ]* }) q- f9 \ - >>> math.log2(20)
1 I: Q4 d4 N0 L e - 4.321928094887363& d$ S; t: Y1 n$ Q" R/ n
- >>> math.log2(16)* l3 \3 M1 K$ f7 i `7 \$ z( U
- 4.0
复制代码
, I s7 D W. wmath.modf(x) 返回由x的小数部分和整数部分组成的元组) e3 Q- [1 E( ]: ^; V, u& L: C
- #返回由x的小数部分和整数部分组成的元组
8 {" A" L8 n# d3 S+ J4 [: H: F0 }7 M/ u - modf(x): C- r. |. U' V i" @
- Return the fractional and integer parts of x. Both results carry the sign
5 k, f7 X Q1 W e, Y9 O2 T - of x and are floats.
' d1 C9 F) m1 ~' O5 W1 z - >>> math.modf(math.pi), Z8 f$ s* k4 t$ I& g9 C4 t, z
- (0.14159265358979312, 3.0)5 _6 V# R- O* d) L1 Q( l& p
- >>> math.modf(12.34)/ D' s5 U; n. N3 q+ c0 _3 P) t: B
- (0.33999999999999986, 12.0)
复制代码
s! K6 t' `4 A; r& V1 cmath.sqrt(x) 求x的平方根
3 ?$ j6 X: q+ k; l! z- #求x的平方根, W: Q8 s" B5 P- g! F
- sqrt(x)5 t) m3 n( L' y0 _" \ z
- Return the square root of x.
0 H5 M& E% l8 ~2 @4 C5 d - >>> math.sqrt(100)
( V) Q* c/ ^, n, w! O5 b - 10.0; z" T- \3 c3 c. ~- v
- >>> math.sqrt(16)# e0 k3 I2 B; G) E
- 4.0
+ l5 _1 a/ l: o9 I/ c S# G - >>> math.sqrt(20)
$ h6 Q& q9 a/ d7 B$ a8 I/ ] - 4.47213595499958
复制代码
' D* I; h- C* hmath.trunc(x) 返回x的整数部分- #返回x的整数部分8 h! f( ~2 Q' G
- trunc(x:Real) -> Integral
8 E+ ~+ J- N8 x! c - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method., k) n3 _( R h7 f
- >>> math.trunc(6.789)
6 G5 l6 I- h8 B - 6
# N( b% {" l: f9 R, e$ s, H J$ O - >>> math.trunc(math.pi)( v" _! z$ y4 n; ]
- 3
' a {. D |" u+ N+ U5 B - >>> math.trunc(2.567)
9 a7 c3 a9 C' p" b - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|