马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 q, E# L. p, |3 Q( X, a+ A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. {6 B9 A' A" v1 G
1 @3 e" S/ Q$ S6 a; y) f1 J方法1:
7 y' U' n4 O% |. N) ?5 a- >>> import math
) L3 I: y" _6 N/ p - >>> math.sqrt(9)/ e" ^2 l$ U' v- F8 h/ |1 ?+ a" K
- 3.0
复制代码 方法2:' b B+ p' m( U. H
- >>> from math import sqrt
9 }9 S9 `8 U/ t - >>> sqrt(9)
; w. e7 P+ s$ J; ?/ m - 3.0
复制代码
+ z. j$ n9 E5 y) g7 O2 y- k
( D% f! P4 L! f2 t, i. E) emath.e 表示一个常量2 W% k$ y0 w# v
- #表示一个常量
5 s! a; z- x [8 F& ~7 o% s1 r - >>> math.e2 s- F+ S: b5 w# j0 u6 V/ ?
- 2.718281828459045
复制代码 7 L6 H- N$ Q9 t t( L# J0 O2 j
math.pi 数字常量,圆周率! J# d4 [& k8 F5 z+ D x7 U7 [6 }
- #数字常量,圆周率" M3 H8 X, d, A0 H$ Y" F6 ?' o
- >>> print(math.pi)
! X! A% _1 k: v - 3.141592653589793
复制代码
0 e2 B% k1 u% B2 D* p3 R/ l( Ymath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x* j* m( _: l$ t# F/ v/ @5 F( `+ `
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ K I, s# b9 @! ?# z - ceil(x)
/ @- ^; E% i% {: _7 k, Y( |( D - Return the ceiling of x as an int.0 Y) B Q, G) Q. o+ P4 ?
- This is the smallest integral value >= x.7 m" A7 _6 I; e# v6 G
/ A/ W l8 v8 q/ J. `# Q0 c* K- >>> math.ceil(4.01)) ~& F$ A9 U) k4 i* l: P/ M( K
- 5
7 y, }1 ~' k5 r - >>> math.ceil(4.99)
" Q7 h+ `4 Y# q6 n* I2 Y - 5 N/ q7 [+ B. s
- >>> math.ceil(-3.99)
7 p0 M6 @3 U4 u! m - -3, x! r- k. ~. H& U' p8 M0 S
- >>> math.ceil(-3.01)! q) M! F' r0 B' @* P
- -3
复制代码
) O+ }: i; c# l9 B1 l8 y1 gmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ p9 l* D8 E' Z6 c4 K/ Z; ~
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. s4 g7 c5 g/ k+ { B - floor(x)
% M/ F$ _8 p* ?. H5 a2 V$ j$ M- | - Return the floor of x as an int.9 F) [0 X; P% V% T6 m( O4 g3 M
- This is the largest integral value <= x.' b% ~2 R7 w6 |( H. ~7 c
- >>> math.floor(4.1). ^2 w0 j- H1 {4 D4 E" ^) ~/ Y E
- 4: C) Z' b$ l$ a0 i
- >>> math.floor(4.999)4 x; ^6 W9 K& f2 {/ u' j) {8 Q2 r, j
- 46 v. K. L6 h- n9 Q
- >>> math.floor(-4.999)
: Z4 S4 x% U$ j7 d6 _ - -5
: a+ m4 z J0 N" q0 s+ r; ` - >>> math.floor(-4.01)
* y0 q3 b5 ~" ]' u( u1 I - -5
复制代码 1 l9 m& Q/ k% r) @% U( Q
math.pow(x,y) 返回x的y次方,即x**y
) E7 F8 R: |/ v6 Q# b. g/ g% g- #返回x的y次方,即x**y
" s, f6 K& `% i5 V1 a* j - pow(x, y)
4 E$ g9 p6 a$ m- H! \( R% s. ?4 n1 s - Return x**y (x to the power of y). f. z8 X3 N" p- o2 r( A
- >>> math.pow(3,4)
/ g1 M/ `( \3 {0 J- s/ z; j - 81.0* Z7 g$ |0 }( E+ ]
- >>> 4 Z/ B% O5 d$ ?& {' r- N
- >>> math.pow(2,7)
& x w) v. ]6 q/ U" t/ u9 _: g - 128.0
复制代码 7 A( q. }7 {" }: \+ F. n
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" l# ?) Y6 x! T$ s% x* M# h
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) G& H3 T* X; A; Y3 G% |
- log(x[, base])
M- I5 B2 {; E# c2 @6 N - Return the logarithm of x to the given base.
! G. b& X- l* w0 ~3 E - If the base not specified, returns the natural logarithm (base e) of x.# K6 z- A s) x7 T3 Q2 B
- >>> math.log(10)# J! S( ~. n5 {% S7 B
- 2.302585092994046
' \# B ~7 p7 w) i4 b8 B - >>> math.log(11)6 |' R( Z! r. b- q' A5 H& c, ~0 D
- 2.3978952727983707
& t7 Y! b% k4 P+ D( m - >>> math.log(20)5 }) K$ b6 s+ @7 u% M6 M3 G6 |/ Y
- 2.995732273553991
复制代码
) N% [" r$ c( g+ @1 [& Y9 dmath.sin(x) 求x(x为弧度)的正弦值
, a+ l6 H9 L& b; ^% D% ?* k4 d5 _" M- #求x(x为弧度)的正弦值
6 v% U, R* H. ~6 ]6 J" G5 R - sin(x)1 j3 f+ ^1 x! a; z# b! J
- Return the sine of x (measured in radians).
- E3 Y4 ]$ c* `6 V$ b/ c - >>> math.sin(math.pi/4)
0 w! T; d: u3 o! \ - 0.7071067811865475
: P# s( ~. k" u! [5 H. n - >>> math.sin(math.pi/2)% o- O' k5 [/ E+ R4 l
- 1.0* _ X& i( |, a+ C7 {6 I# x o' Z
- >>> math.sin(math.pi/3)
6 D6 e0 q! y1 r5 Z2 s - 0.8660254037844386
复制代码 8 h6 x. H8 }4 K6 J' @
math.cos(x) 求x的余弦,x必须是弧度
9 a7 d! U9 x- v/ W4 k3 G- #求x的余弦,x必须是弧度
( t, r2 i* V( U. D o - cos(x)
- k0 l* B% ^6 ? - Return the cosine of x (measured in radians).
* ]8 I7 G4 t, Z# ]' ?0 R% S7 C - #math.pi/4表示弧度,转换成角度为45度
# ~/ r% M B4 q7 ^ | A1 N - >>> math.cos(math.pi/4)0 B+ b* X( P6 X4 l4 v! L- M6 ]
- 0.7071067811865476
5 }" w/ \/ P5 Z - math.pi/3表示弧度,转换成角度为60度9 `( i5 q9 N! ~" c X
- >>> math.cos(math.pi/3)
: k8 B* L( C. ~+ L" |1 I B - 0.5000000000000001$ {" a" p- }. u& ? P; M" J
- math.pi/6表示弧度,转换成角度为30度$ ], G5 F) l1 y: d; \
- >>> math.cos(math.pi/6)
9 f7 x$ y* N7 T5 w - 0.8660254037844387
复制代码
' d/ G6 [, b- S* {# u2 w- X7 Qmath.tan(x) 返回x(x为弧度)的正切值
: g, b4 N7 f+ }7 y$ \- #返回x(x为弧度)的正切值
5 z1 a$ I) H2 B# r, J - tan(x)
% e8 r/ ^5 I4 {& v - Return the tangent of x (measured in radians).% o r4 Q) M. o( ~+ z. C7 y
- >>> math.tan(math.pi/4)/ G0 X; O! d0 z# l/ o
- 0.9999999999999999
9 _: T; _$ J. D+ Q1 f - >>> math.tan(math.pi/6) F- |& `) d# m1 ?
- 0.5773502691896257* W( s) |( L" M* N! @
- >>> math.tan(math.pi/3)
: l8 z, ^% v# G% C. p' [ - 1.7320508075688767
复制代码
2 f. _- `' D' nmath.degrees(x) 把x从弧度转换成角度3 C# S1 f ?6 F# A/ l
- #把x从弧度转换成角度/ I- v D7 Y) e
- degrees(x)# u7 |+ ?3 J) ^1 E9 [5 `
- Convert angle x from radians to degrees.1 g* I9 T+ f+ p L) E
) K. d$ f3 H5 m$ I& ]8 s# x% o6 z- >>> math.degrees(math.pi/4)* e: N4 \3 G6 ?+ m
- 45.0& w! v) u J7 L9 ^- [
- >>> math.degrees(math.pi); _4 Q5 U1 x! f% e0 J# w6 [8 b6 X
- 180.0
$ [# |% g2 P$ x0 H - >>> math.degrees(math.pi/6)
/ n! c a% R% I- Q+ t" _ - 29.999999999999996
; ~+ |5 M% f$ T# n2 _1 m - >>> math.degrees(math.pi/3)
8 ]0 ?; e5 [5 A X# r' s2 Q - 59.99999999999999
复制代码
# U& g$ B3 d8 T: C' H. N" |2 Jmath.radians(x) 把角度x转换成弧度# t0 ^! a+ i- w* N% x
- #把角度x转换成弧度) d; k& A2 v2 y, G" ~$ {/ W
- radians(x); m5 z0 n6 b. p$ H: R! a4 B8 ]
- Convert angle x from degrees to radians. h7 V" E% r* a( B! n
- >>> math.radians(45)
& V2 T3 X: e' O+ \% V - 0.7853981633974483
0 t E/ L) \( G7 |* P - >>> math.radians(60)) i6 H/ k' W" _! T) j$ l
- 1.0471975511965976
复制代码
. F: P2 C! W' n/ l" Cmath.copysign(x,y) 把y的正负号加到x前面,可以使用02 H, R; ~: E- |: ? K/ r5 O
- #把y的正负号加到x前面,可以使用0
4 c1 L- t6 z2 ?$ k - copysign(x, y)
: T" ]9 x. u) e$ ?$ t - Return a float with the magnitude (absolute value) of x but the sign ' p, p0 w* Z$ D1 i) c" ?
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( P6 k; g$ {! \- `
- returns -1.0.
) d% ^& b# o7 a1 G - . o- S( E+ g2 G2 y! J
- >>> math.copysign(2,3)) n+ S- J1 o2 e3 @( u) |
- 2.01 B A! W) d1 t
- >>> math.copysign(2,-3)
; E; k( {) Y* @8 B - -2.05 c% w& b' H% Q4 `! x6 [) a/ B
- >>> math.copysign(3,8)
% l0 K6 H* h1 P5 j) j+ b* P - 3.0- \. w- E1 o7 D- J
- >>> math.copysign(3,-8)
: C( H& b# ]. A* ~- Z$ f - -3.0
复制代码 ; W7 l2 \4 p2 f( y
math.exp(x) 返回math.e,也就是2.71828的x次方
, h- u3 H* z; K2 z, O, a9 w- #返回math.e,也就是2.71828的x次方 v4 D" y) I2 k! @3 p( V# ]. t
- exp(x)
9 P" @# ~7 m7 E; ~5 I# M - Return e raised to the power of x.+ O( o- b3 w8 J5 i4 `2 H
- e" I: y: x, M; H: B# A8 m' f! B- >>> math.exp(1)
( O- w6 P( t1 k% i - 2.7182818284590451 P; q) u9 G6 f; \" X% s, W: j$ y
- >>> math.exp(2)
7 I) i1 g) w, Y - 7.38905609893065; Q# ~" ?4 p4 K
- >>> math.exp(3); \9 U Z7 c" q. A) Q9 ]+ K0 H% [
- 20.085536923187668
复制代码
; K' z3 ]" F$ g9 `0 S9 S" fmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
5 K& v- i$ B9 [# f9 _- #返回math.e的x(其值为2.71828)次方的值减1
8 R# {. J' q4 E0 X0 b% C" M0 t$ E4 w - expm1(x) S+ t/ t8 _ q1 a* P
- Return exp(x)-1.
+ K/ f- K |! g* w/ X. N - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
6 z0 h) b4 S/ h5 O# ^/ J
7 B" X9 F0 d( B8 n9 g- >>> math.expm1(1)
: L5 z/ _& b8 h1 c, R9 d" P+ ] - 1.718281828459045
$ k0 t6 j- k e7 o - >>> math.expm1(2)- n, b! |5 v5 h# l4 n
- 6.38905609893065
5 m: J6 |; c- x# u4 c6 N _ - >>> math.expm1(3); Q6 A o0 H# h9 |! o: V
- 19.085536923187668
复制代码 0 g* b6 d3 E" X6 J
math.fabs(x) 返回x的绝对值1 w; u) A, D6 k
- #返回x的绝对值
3 j a0 \+ `8 W4 @7 ? - fabs(x)
2 E% s7 S) c+ U - Return the absolute value of the float x.+ H5 e. X* k) u/ _3 j; T- }
- - q s# ?/ T( L% a! s$ }
- >>> math.fabs(-0.003)
4 ^7 W p8 V# l4 m - 0.003
3 ?7 E/ U& z; K. ~$ G- @- u: b; n+ ]9 a - >>> math.fabs(-110)
; |% B7 M' |$ e% E9 \2 i8 k7 w4 E - 110.0, Z1 Y6 Z$ X4 q2 i# @/ {* L+ i
- >>> math.fabs(100)1 x; M! s6 M, c$ X' i4 _$ E" ~
- 100.0
复制代码 * J* c* a G- F. a" @4 V* `
math.factorial(x) 取x的阶乘的值
# ^" @- r& x2 \: B- #取x的阶乘的值
, F! K; u( G7 [2 ~; M0 g - factorial(x) -> Integral! v' s3 v. H# v& e) i
- Find x!. Raise a ValueError if x is negative or non-integral.% P$ g9 \5 T, a6 \+ h, O
- >>> math.factorial(1)
( o, r, W1 u7 u - 1/ O5 q9 o$ z, L% x. p
- >>> math.factorial(2)" g" v2 j+ m) u/ }0 r# T
- 28 B7 l- |3 ~6 v7 g: c. w- U
- >>> math.factorial(3)
) ?% W; z. S. M& ?* d" V - 6& [( [4 `/ P& e" S. l% r
- >>> math.factorial(5); Y# R8 `' O7 C+ s5 U8 S
- 120
Z# o- | K' o" d+ ] - >>> math.factorial(10)4 k& k. V% F5 ?
- 3628800
复制代码
7 n- n7 m7 ?5 n% \! r8 Y) Kmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数; Z: u5 s& C9 [8 Q. m, l: I
- #得到x/y的余数,其值是一个浮点数
* G+ I5 ^) V8 G' ]" R0 B - fmod(x, y)
7 v6 i* L# ], ]# Y - Return fmod(x, y), according to platform C. x % y may differ.
& H P& n$ B* W - >>> math.fmod(20,3); M3 M: _# U& |5 o: ]1 \
- 2.0! v# Z+ v/ O- r: \5 Y% M
- >>> math.fmod(20,7)
8 z/ T- S* h5 q1 ]7 A1 V - 6.0
复制代码
2 k1 ]) T- n& nmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! {; d8 P7 D7 o% N
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
! A+ k/ W S5 F+ V- V - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
) S9 K8 `. Z- J' G - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ \8 O+ K4 L3 F! E - frexp(x). Y3 P; U& m4 J. a) I
- Return the mantissa and exponent of x, as pair (m, e).* c4 H! Q+ }+ x+ m* o" R* T p
- m is a float and e is an int, such that x = m * 2.**e.
% D& B9 _$ |/ N" J; j - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0./ ]+ J1 m2 V; d y; s; x- {
- >>> math.frexp(10)
5 d- H7 v0 x+ {7 z, { - (0.625, 4)# ~% `. o6 |- C. i1 U0 P- Q# ^2 S2 D% y
- >>> math.frexp(75)( h0 Y7 @% { ~4 ^4 @) P3 ^: {8 q' k) o
- (0.5859375, 7)
# p7 v( | B( [4 i- U4 \ - >>> math.frexp(-40)4 Y; p& ?& S+ o: k
- (-0.625, 6)
1 {* d! g. m: z0 @ - >>> math.frexp(-100)
1 h7 E2 B" s5 |1 Y: [: } - (-0.78125, 7)
7 R% s# g9 L& `$ j: n - >>> math.frexp(100)
]" [$ m2 `* w. L: A) H1 k- Y% y - (0.78125, 7)
复制代码 + p; G% L8 g4 P, H* O* I6 o5 H* L
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)$ W4 R6 x- p. P \3 L _
- #对迭代器里的每个元素进行求和操作
) K/ F; g( {9 c( m3 D& c9 k - fsum(iterable)" l# V# e5 F$ ]% m3 G+ I7 A
- Return an accurate floating point sum of values in the iterable.1 s, U& T6 _) l9 _1 r
- Assumes IEEE-754 floating point arithmetic.
0 J$ P$ Z/ ^1 y7 o/ F. i# N - >>> math.fsum([1,2,3,4]), K7 o( C5 X& D' E% R
- 10.08 R4 y, l. \5 X# j5 V5 a
- >>> math.fsum((1,2,3,4))
# i I! h! T4 a& ]( i6 [ - 10.0
* h9 Q: M0 }/ s5 S - >>> math.fsum((-1,-2,-3,-4))
9 d' K/ j, J5 U - -10.0$ S/ u% B: R7 t5 r! D) E- I# s
- >>> math.fsum([-1,-2,-3,-4])0 d7 v& n8 o' a& R9 U: L
- -10.0
复制代码 % k* g, a! G+ m+ o
math.gcd(x,y) 返回x和y的最大公约数1 i% e2 J; P4 F* ~
- #返回x和y的最大公约数+ ?; [8 e" P( S8 W* |$ s0 r
- gcd(x, y) -> int
: s2 _0 c. @* b k - greatest common divisor of x and y [, ?- t6 D8 \3 \- g% m: R0 V' w* x
- >>> math.gcd(8,6)0 |5 L1 t6 L" f
- 22 G6 G4 o$ ]; O {: z- u/ ]- ]
- >>> math.gcd(40,20)( S' X* G7 M" ` y& o3 x- o$ b
- 209 ^- C- b2 A. h' ~
- >>> math.gcd(8,12)' E4 K( F- _, e8 t
- 4
复制代码 6 t% X- f9 W6 s) C3 J) y1 v' |0 ~
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
4 s2 r, Z5 v/ x2 @( L8 Y8 E- #得到(x**2+y**2),平方的值
: a5 H5 ^: p$ d7 ^6 k" H' m' g - hypot(x, y)
, |- \) F& X8 W- S+ V9 ~ - Return the Euclidean distance, sqrt(x*x + y*y).0 v( n9 s# o( d7 w/ e6 _. j- \
- >>> math.hypot(3,4)& x, w+ z& \2 W; W
- 5.0! H, X$ G( y, F/ C8 N( a4 w0 m% {
- >>> math.hypot(6,8)2 O6 w/ a) X2 S% R
- 10.0
复制代码
. C0 f# ]) W: j6 Y% P+ Xmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
( p* E1 m' l) F! j0 j/ ~( M6 E- #如果x是不是无穷大的数字,则返回True,否则返回False
7 t2 ]/ ?2 P2 F; U - isfinite(x) -> bool
& g- `5 P' z; ?* C! s1 L' Y. Q2 R# K" K - Return True if x is neither an infinity nor a NaN, and False otherwise.
`" C+ t) y, G% b - >>> math.isfinite(100)
' S+ O3 q- [1 x6 ]3 P6 W# K - True
# c" K' r& i" W+ ~1 O6 M! u - >>> math.isfinite(0)
c w/ w& k: w" A" z3 f- ?# _ - True
1 ^1 Z8 J3 w$ B8 P+ c/ c/ x, _' d - >>> math.isfinite(0.1)
p( l0 ~7 x$ B# C: Z$ e) n - True
* q4 T5 V2 d0 V" o - >>> math.isfinite("a")
# t" B& ~; T" ? w0 e - >>> math.isfinite(0.0001)
1 R" ~* m: c5 i+ F - True
复制代码
3 t: [) {+ r$ {+ S$ R! Lmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
+ e* y: C. p% {$ ?* u4 A- #如果x是正无穷大或负无穷大,则返回True,否则返回False
9 O4 @$ {) Z o2 T. @! e - isinf(x) -> bool5 ?! u) N* Q3 b2 \, ~/ ]0 W
- Return True if x is a positive or negative infinity, and False otherwise.% C8 A. l: F+ o: O H+ m$ C
- >>> math.isinf(234)
9 C3 {% ?- O* _& K% ] - False
; z0 x$ a! R# Q$ j2 H+ P - >>> math.isinf(0.1)
- Y a3 W, H8 Z' v- w - False
复制代码
. b0 |( j. G- w' H4 K8 [math.isnan(x) 如果x不是数字True,否则返回False
; O) ]/ M$ m5 O- #如果x不是数字True,否则返回False
( k N; Z* M/ k% H' w - isnan(x) -> bool' l( v7 ], j% Q3 ~/ Q' p8 S
- Return True if x is a NaN (not a number), and False otherwise.
1 X- a0 V0 e( }- n2 Z7 |3 A - >>> math.isnan(23)% _- W. Z# G! d: ~0 \
- False
" {9 Q0 X. w ^5 R4 X6 q - >>> math.isnan(0.01)& R: _2 R7 l( ^* h% S, b* @. X
- False
复制代码 9 D* ^% D4 }/ G- v# k
math.ldexp(x,i) 返回x*(2**i)的值2 `* Z; x4 C! G! V
- #返回x*(2**i)的值
4 _! Q+ W% }# @ - ldexp(x, i) |$ a i. v5 r, R+ U% f, I
- Return x * (2**i).9 h: q# Z; y# t2 \+ ]
- >>> math.ldexp(5,5)
/ @, ~0 U! r, x- v# U - 160.0
7 c) a7 O: d- A# i - >>> math.ldexp(3,5)8 _. b+ K/ U) L0 C! H* ]$ [! P2 ~
- 96.0
复制代码
' ^$ |% ]6 h" m& c {; dmath.log10(x) 返回x的以10为底的对数
1 X. I: H$ D$ Y' F d6 k3 ]- #返回x的以10为底的对数1 f; G6 K9 j9 }/ X
- log10(x)
8 `7 g- ^5 E' x - Return the base 10 logarithm of x.
, D# F# b2 W! f" G - >>> math.log10(10)
5 k& ^) y7 k7 o7 {% I - 1.0
t; k3 ]; I6 a1 q) F4 @2 V - >>> math.log10(100)
( r" c' d6 o! |; B% J; ^) L - 2.04 B; \$ _( c$ b4 W0 P+ h4 I
- #即10的1.3次方的结果为206 Z" |2 O# m! z; s* |; Q
- >>> math.log10(20)! o4 ]' h0 {1 ^% m
- 1.3010299956639813
复制代码
3 p0 y2 ~7 K+ m% r2 hmath.log1p(x) 返回x+1的自然对数(基数为e)的值: C1 A' r0 ^3 K/ W0 f% m- P
- #返回x+1的自然对数(基数为e)的值5 V) a5 S* d0 }! ~. A& F+ {
- log1p(x)
# s3 I& w A% G' L2 ] - Return the natural logarithm of 1+x (base e).$ _+ O( q: ]* f
- The result is computed in a way which is accurate for x near zero.
, V. V5 Y% q1 R - >>> math.log(10)) L; I U7 X: B8 }( B/ h I0 ^2 l. A
- 2.302585092994046& O) d: u1 C( d5 K) A1 P
- >>> math.log1p(10)5 {) {& \, `; X( g% v/ q
- 2.3978952727983707$ L/ {, T1 q$ @$ ^7 Y. {5 T0 g3 s
- >>> math.log(11)$ s% ?, w, A) z$ _) q' f4 a* b
- 2.3978952727983707
复制代码 3 D* _* E8 i+ H/ Z' e; z& v7 P0 a
math.log2(x) 返回x的基2对数
; S5 t9 v) ~. @( @' s1 C0 ]3 V* m- #返回x的基2对数
& P( O4 f. e" h7 S* L/ b - log2(x)
Q6 ^4 g% r% x' B/ W1 d/ | - Return the base 2 logarithm of x.9 e( g; m/ x. g4 R5 s( E
- >>> math.log2(32); F% u" K J+ I8 W m
- 5.0& f: x/ f9 z6 Q
- >>> math.log2(20)
0 Y1 W! h6 G; E% U, K H* B - 4.321928094887363* ~7 h; D0 R( I7 Y
- >>> math.log2(16)
! }/ M3 R+ q0 y) o9 K8 g - 4.0
复制代码
- F3 M% ?7 I, ~" {2 G& Y- Imath.modf(x) 返回由x的小数部分和整数部分组成的元组/ s- }3 \2 q# T1 ]1 `
- #返回由x的小数部分和整数部分组成的元组+ k% J" N0 B- P% e* n+ b7 E+ j5 V
- modf(x)
/ o0 j9 U% O9 [# o7 g. o - Return the fractional and integer parts of x. Both results carry the sign; h' c1 C: J. B
- of x and are floats.
6 a3 r+ T3 r( e6 m - >>> math.modf(math.pi)
, z; ]7 j l! m, a5 l" J - (0.14159265358979312, 3.0)
) g) f9 T# i, y- D - >>> math.modf(12.34)
9 S5 ~5 B* a3 n" t" ?) V9 D; @ - (0.33999999999999986, 12.0)
复制代码
- _% \ T- r0 s0 [& n1 B5 lmath.sqrt(x) 求x的平方根2 \' [5 A, v4 x7 }; ^7 N
- #求x的平方根: L4 c t+ F- |# Y+ x' s+ _# K6 l' v, ~
- sqrt(x)
9 Y6 l; W2 E8 E! C, V! i - Return the square root of x.
4 q- Q, Z+ n6 p5 D- }. _ - >>> math.sqrt(100)
; j! h" n# d* {; j# e3 R0 Z# e - 10.0
8 V6 N: S+ U8 ]$ e, _; u - >>> math.sqrt(16)
X7 Z& t; R& M) G, e4 {2 F - 4.05 a$ D' ~/ P. u. _. }
- >>> math.sqrt(20)5 y* o: U) D4 A' C0 }% g3 B$ `
- 4.47213595499958
复制代码
. o5 p' v! s- n7 a' y A/ r5 Tmath.trunc(x) 返回x的整数部分- #返回x的整数部分
0 s( A" W5 N: E+ c1 g( J4 B - trunc(x:Real) -> Integral" R/ q, s3 ]/ T S
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 T/ r n& C. A u' d/ R% v
- >>> math.trunc(6.789)
/ {3 k" j" f' f6 N - 6
% l* R7 m2 I3 @2 g6 }; h- `! G - >>> math.trunc(math.pi) q5 l% ]$ c) [ l
- 3
% r% m( H- u9 B) z7 m0 l - >>> math.trunc(2.567)
9 M" o6 O/ |- U5 d$ n) k - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|