马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# U& j/ c$ [: w9 [0 R) N
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; e" s* S+ `) v7 L% J* C P9 k( \
8 ^9 V& K' Z3 i$ Y) r8 S0 F方法1:
; L! t( S: [6 ~- >>> import math
- D6 |2 F3 Z% z% h2 L9 z7 G( | - >>> math.sqrt(9) { W, p) V+ j( q: s: l
- 3.0
复制代码 方法2:
2 F0 \) i! j; b5 l# ?- >>> from math import sqrt
* i+ t) a0 v$ N, o- S - >>> sqrt(9)- G8 t0 ^' T$ N0 O
- 3.0
复制代码
& B! [1 u: U1 n } - f) \6 O, O0 g, w
math.e 表示一个常量$ V8 S6 F) {' W' k
- #表示一个常量
, D* `4 ?+ Y7 ~/ Q1 @# P - >>> math.e
- s/ M) A7 O- X7 ~ - 2.718281828459045
复制代码 ) C- d8 e' E+ @ n9 p- |: p
math.pi 数字常量,圆周率( Q7 P6 K7 @# v2 v1 S7 h
- #数字常量,圆周率
, }( I8 I! ~5 T6 J, k- I - >>> print(math.pi)7 @% {9 q9 g8 X: P6 {1 ~
- 3.141592653589793
复制代码 ; A1 n) s: Y" m- O3 W% c4 h
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 ?6 g# _: |8 p. j2 Z8 e- #取大于等于x的最小的整数值,如果x是一个整数,则返回x7 R0 O' e6 _0 g; o3 s4 u1 p( B0 K/ J4 q
- ceil(x)+ S) B5 t# u w) }1 I8 I& o6 S
- Return the ceiling of x as an int.
9 w Z; r3 `- T) M4 V7 J4 X - This is the smallest integral value >= x.# X! j4 [( f* V
- $ X- o, t k( V$ I1 }! N. o
- >>> math.ceil(4.01)
' ?' |; N& P* G4 y6 x5 V8 W - 5# I: _; j! [5 e4 B) V! M$ o6 h
- >>> math.ceil(4.99)) O& V+ N" w2 D. m
- 5. d" H, z7 x& s2 \0 ~/ Q& S( I
- >>> math.ceil(-3.99)( M7 i5 G g4 F& w. _
- -3
$ j2 d: S1 m- U& E5 B - >>> math.ceil(-3.01)
( d1 x1 x# M& V7 u/ M - -3
复制代码
8 l2 L7 H2 l& l, A( C0 F- R l6 L+ amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身; V! A4 \. k% ~0 Y+ U& i- y. h
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ `, F0 T! }+ x$ @+ t( I
- floor(x)
6 P C8 ^6 U: J& x! _3 i* w) \ [ - Return the floor of x as an int.
3 X3 L# D) c9 q; o# F6 N - This is the largest integral value <= x.* t6 l7 J U# B1 \
- >>> math.floor(4.1)' M7 u& l5 ^8 Q+ q3 c. G8 ?4 s; l
- 4' S& c* V$ F0 k, k ]( |* Q) @
- >>> math.floor(4.999)5 e# _8 ~7 m9 Y; T. B1 U
- 4
; K* [) H2 P' b) x" E$ K7 e: P6 ^+ M - >>> math.floor(-4.999)
8 K) a, }" A7 s6 u$ @3 \# e+ a3 y - -5
9 z9 R* f5 C0 j8 V - >>> math.floor(-4.01)
4 `: Z+ @2 B( }2 F; [9 H - -5
复制代码
# K2 o& c3 o5 Q# o5 @$ F6 G Kmath.pow(x,y) 返回x的y次方,即x**y
6 P$ ?/ L3 ^! |: Y' |* w- #返回x的y次方,即x**y7 ?' n( W9 s4 P( X. ]9 C
- pow(x, y)
/ X F4 A/ m+ ?& J" ^4 U9 g+ l, F - Return x**y (x to the power of y).! b) b* K l( p& ~8 {9 k
- >>> math.pow(3,4)
8 ?$ [2 k9 b' w+ O/ h4 \* a& k - 81.0- @0 ]: | [/ p) ?' u
- >>> 9 h/ s: c! Q) d) @0 M0 o
- >>> math.pow(2,7): r% D1 Z9 M k* ]( m' r
- 128.0
复制代码
3 q1 d% P% ~# B4 w, {7 x% Imath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). C; l$ n0 T3 o# r3 V1 M6 m _
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
- f/ G, h3 q$ ~/ j' |, U5 U2 T' @ - log(x[, base])
4 ]% ~* ?5 }# J - Return the logarithm of x to the given base.: X/ J! m/ [' _& R% A2 |5 b
- If the base not specified, returns the natural logarithm (base e) of x.
$ b* ~; {* D% o3 ?) V- R- R - >>> math.log(10)* O# c, N' B" t: \1 Q& Q
- 2.302585092994046, f: c$ K) I% [8 X8 P2 J
- >>> math.log(11)
$ W7 H, G R- A* l, o0 E$ j( q - 2.3978952727983707" Q+ p6 X7 a3 y7 e& }0 z
- >>> math.log(20) ^; y- z, K. a. l3 x
- 2.995732273553991
复制代码
; s1 Z0 {% p* i8 T/ pmath.sin(x) 求x(x为弧度)的正弦值
$ } Q7 m) g& V4 V0 k- #求x(x为弧度)的正弦值
9 ? [( r& T% [) }5 i: k+ h, v: d - sin(x)
3 q0 W! S1 { \3 @ - Return the sine of x (measured in radians).
/ \! a7 [6 O: p - >>> math.sin(math.pi/4)
7 }9 `* A4 _8 j, n - 0.7071067811865475
$ M K/ k3 }+ |9 L. D( K1 D - >>> math.sin(math.pi/2)( U8 A; m; a9 o/ x1 Z
- 1.0" q- \7 I3 z- }1 g! T# L* a0 Q
- >>> math.sin(math.pi/3)
( ~# N4 j& y0 s6 v: J# p - 0.8660254037844386
复制代码 a8 A( o9 k `
math.cos(x) 求x的余弦,x必须是弧度 N" V) ~& s4 T* e1 ^ J
- #求x的余弦,x必须是弧度
4 }3 V/ @8 R# a- W4 G+ G4 {9 ~ - cos(x)* _! D/ V0 N" N9 P! R* F
- Return the cosine of x (measured in radians).
/ v' U8 u0 o1 @( r( \ - #math.pi/4表示弧度,转换成角度为45度: i8 f$ a8 h, ^+ R. o5 t2 v7 H" X
- >>> math.cos(math.pi/4)
/ n6 w2 O& Y, F$ [4 n7 O4 [# i \$ e - 0.7071067811865476' G4 o% v5 j% w1 Q
- math.pi/3表示弧度,转换成角度为60度
8 ?5 u6 E, H+ Q* | - >>> math.cos(math.pi/3)
+ c* w5 B- k) Q7 g" }- g - 0.5000000000000001
" @) K( @ y% a+ c" c8 d, b - math.pi/6表示弧度,转换成角度为30度
1 O9 p0 v$ h" a: Y1 a8 H. M. C - >>> math.cos(math.pi/6)3 u5 F0 q, }7 s+ v# \; ?% ~
- 0.8660254037844387
复制代码
6 ?" b6 b) p& s% T; H) V' Nmath.tan(x) 返回x(x为弧度)的正切值
; N+ v. m: `" E' y. d, |- #返回x(x为弧度)的正切值1 p3 k; y' T$ ~6 Y$ x/ k+ O
- tan(x)
, k( G2 |8 w8 t+ ] - Return the tangent of x (measured in radians).4 l5 p/ Z" I9 f6 ?0 M7 B6 }7 s! O
- >>> math.tan(math.pi/4)+ H# t9 ? n* \# |- q' ^, R7 i3 G9 I
- 0.99999999999999999 h. v1 ^- G( O+ y3 a8 S3 O
- >>> math.tan(math.pi/6)% l8 N! ]$ @8 N2 w6 z7 E
- 0.5773502691896257, f6 P) d2 Y; S' R" l
- >>> math.tan(math.pi/3)
( U7 \( t* r$ Q+ j, Q - 1.7320508075688767
复制代码
! R: _9 ^; [1 c. x! f4 Mmath.degrees(x) 把x从弧度转换成角度
+ r6 T0 G* `6 Q4 F- #把x从弧度转换成角度
. H+ y' g, `) L) K% C - degrees(x)
/ b. O- ?9 n& g( M- s - Convert angle x from radians to degrees.
" J1 T: j% f2 @# X( @: ~ - o, Y: C: e; J% a7 C+ I' @0 U
- >>> math.degrees(math.pi/4)
9 b3 v1 W9 e# n - 45.0+ z; B1 Y9 N' \2 W' e) F
- >>> math.degrees(math.pi)# K( l$ G! H* s/ m; W; u0 d
- 180.06 ^# Y$ V _: q; k8 G
- >>> math.degrees(math.pi/6)) u. v4 z/ K5 k) T* h& l
- 29.999999999999996
( {0 d, K5 q# u- G* S* ~6 U0 X7 g0 y - >>> math.degrees(math.pi/3)' L4 R, f* h3 C9 I
- 59.99999999999999
复制代码
. _( P) M0 S% }9 W& Gmath.radians(x) 把角度x转换成弧度
7 `' N% Z1 T/ A, T: F- #把角度x转换成弧度* I5 I( [4 L: g" \
- radians(x): f6 J. U1 u; u3 k( X% j
- Convert angle x from degrees to radians.; h3 y! L+ j* P$ `- B F( {7 V
- >>> math.radians(45)
" i8 l1 j, y/ y+ O$ a - 0.78539816339744837 p) B- t, w4 S- d: z5 S) D
- >>> math.radians(60)% |* B' v/ @/ X* V" c
- 1.0471975511965976
复制代码
4 h* D# N% r4 d: |9 u# J% vmath.copysign(x,y) 把y的正负号加到x前面,可以使用0* y: R# S# P% [4 y" X. W9 B7 }
- #把y的正负号加到x前面,可以使用03 M6 g, @$ n1 w$ |! L1 ^
- copysign(x, y)
: G6 S, \- b, l1 V% s - Return a float with the magnitude (absolute value) of x but the sign * s7 I0 j1 S7 V, ~
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 l R* U0 V: D8 N1 a" C0 \
- returns -1.0.
& B1 D( B* _2 z2 b6 s2 d; ] - 9 P% w3 d: b7 q& h3 H+ a
- >>> math.copysign(2,3)
, b) ^6 b" N! f- ]% i - 2.0
! D) {5 }7 o' h# O1 [' k2 S - >>> math.copysign(2,-3)
" M6 g$ D) V% o# `$ ]# v- ^ - -2.0# d9 ~7 A0 {- r( k$ K6 s( F; V2 L
- >>> math.copysign(3,8)' n! _7 ^) | Y% ^6 V8 u4 X
- 3.0+ B* f! ^: o! t* ]' [
- >>> math.copysign(3,-8)) {& g4 ]* U' R* u$ [
- -3.0
复制代码
% y6 z* n/ a6 x- _7 f' Bmath.exp(x) 返回math.e,也就是2.71828的x次方
* B! {6 B2 W, v- H; T& }! G. q2 d- #返回math.e,也就是2.71828的x次方/ E- s* w8 y# g8 h t' ?! i) c7 X) i
- exp(x)
}; N% }" O Y) s - Return e raised to the power of x./ }% s, G3 k- ^9 X7 `8 G( m
4 G q# b& F( v7 a9 a- >>> math.exp(1)& `0 G' N7 d) z) G
- 2.718281828459045* Y- d1 B! A* {
- >>> math.exp(2)
6 l3 w& \" B" m$ f4 m - 7.38905609893065
3 `$ `! m7 r9 u [) u% \ - >>> math.exp(3)
R. H5 V( L- j: B - 20.085536923187668
复制代码
' \& B1 o D8 o; Z1 j. Wmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) _, R- g% R' f& c T6 s( J
- #返回math.e的x(其值为2.71828)次方的值减1
" ?$ p) ^ @) b" M* k3 {5 Q( m7 n - expm1(x)
# x* n8 t5 b: Z4 U - Return exp(x)-1.9 ], ?0 v a G: x' p
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.3 q: M2 K$ J5 v; J/ n8 M& ?
& l# x! g) N! Y' | y3 K. z5 L, N5 s- >>> math.expm1(1)7 C* M5 t1 a& }' | E+ |& P+ }8 e. q
- 1.718281828459045
, n4 U! r! j/ _5 `0 a, S - >>> math.expm1(2)- p }9 s, \3 E- W
- 6.38905609893065: e0 u- z2 t9 A* f9 U# J9 f
- >>> math.expm1(3)& c7 O0 x. ^5 A, `* b- }7 a$ j
- 19.085536923187668
复制代码
5 N2 K$ F3 ?$ n; _, S9 O7 Rmath.fabs(x) 返回x的绝对值
2 W- e2 F* l w: R) O- #返回x的绝对值
& k% @5 G( c* U# w - fabs(x)
. x# G c& \! M - Return the absolute value of the float x.$ m6 V7 x( w5 W7 q7 E" e9 C
6 S2 j- b8 A! b3 D# Z7 a [5 ]- >>> math.fabs(-0.003)
3 ]' |/ q# |* u) [3 D - 0.003
) j% A3 C v* z6 X8 }3 D - >>> math.fabs(-110)
: C3 [# ?0 b2 F9 n9 Y) M - 110.0
" u' U) }% f/ r9 x. z4 k0 f - >>> math.fabs(100)
" V$ \( C" ]& k - 100.0
复制代码
3 B) t6 R9 O2 K9 R- L4 c, wmath.factorial(x) 取x的阶乘的值( t7 m8 v4 t; T# t0 |! e' m
- #取x的阶乘的值
: Q9 e! p8 r7 J5 P - factorial(x) -> Integral0 L* r" ]; i9 `$ ^
- Find x!. Raise a ValueError if x is negative or non-integral.; l" Y6 R3 o) F$ e
- >>> math.factorial(1)
& I( @' ~- ^1 E& _ - 1
3 U7 `+ _. D) `/ U/ `; Q: m$ A - >>> math.factorial(2)1 u* w, I/ j0 H/ q1 H5 t* a# e
- 2% P: \* j, ?! p' f
- >>> math.factorial(3)
/ A: n: Q9 Q" u1 B$ ] - 6
" w# B# ~+ ~! N! @+ i0 Y - >>> math.factorial(5)
6 b! j) e- J2 v9 z9 f1 d; Q - 120
; z! u& ^0 t ] - >>> math.factorial(10)3 G. v% D% o0 M4 @ v
- 3628800
复制代码 5 Q* r3 l% |9 S5 Z
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! r# N- W) w- q V' Q6 H- #得到x/y的余数,其值是一个浮点数3 i1 O& U2 ^" }0 A O
- fmod(x, y)
% {6 v5 ?# o# z! u" H7 H X - Return fmod(x, y), according to platform C. x % y may differ. t" [' H. k: @& ~
- >>> math.fmod(20,3)
( K% O; Y( k# ^ - 2.0" g! f. F# [3 w/ x3 _
- >>> math.fmod(20,7)' J' I2 X Y# j, W$ B
- 6.0
复制代码
" P# C* x) Q' O' }math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
( V' k: w( r3 T- N2 N- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,! Q0 I4 E; u$ G( o6 X7 t8 y( i4 x1 s
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. ]6 n% \/ m7 u3 d7 n6 ~! P" u1 V3 g
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
+ V% F" J5 e! E& D8 D' T1 S# C, e# N; M - frexp(x)
$ [& E: N+ K1 b: x S' L5 ~ - Return the mantissa and exponent of x, as pair (m, e).
, X, I3 Y L' H" G8 w - m is a float and e is an int, such that x = m * 2.**e.
- X! k4 }2 G' b3 Q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.( p( q9 f% o+ s$ P
- >>> math.frexp(10)
1 Y% k5 L5 Z* b# e8 [% l - (0.625, 4): [2 |9 @! [$ \# @# @3 o* t
- >>> math.frexp(75)
( H% {, F: P9 W/ `1 M( F - (0.5859375, 7)) P' W: [, y) s4 o- O; t4 G
- >>> math.frexp(-40)9 n& f0 |0 N7 D- ]3 o N3 E
- (-0.625, 6)7 P5 R7 c6 t6 T# Q2 ^: [* v
- >>> math.frexp(-100)) N3 q# f) d, `9 {2 n2 F8 u
- (-0.78125, 7)0 K9 I2 A, g5 \: J& D8 L
- >>> math.frexp(100)7 M+ A' M4 y2 L7 ^" @7 d& C
- (0.78125, 7)
复制代码
% |, `; p2 T& Smath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
/ E$ X/ b/ \( Z) F% b$ u1 d1 e* M- #对迭代器里的每个元素进行求和操作
' `5 S1 [6 I: k - fsum(iterable): B' J% z8 S6 ^) j
- Return an accurate floating point sum of values in the iterable.: ]3 e; t# t8 A7 S2 f
- Assumes IEEE-754 floating point arithmetic.
. Q- _/ p4 ^% S0 E; { - >>> math.fsum([1,2,3,4])/ g( q. G# L. A. e6 s- `1 N
- 10.0
+ n7 c y& c# K0 L - >>> math.fsum((1,2,3,4))
. v8 A# o: h! c! s- y - 10.0
7 U/ `% a6 u4 H& }* u - >>> math.fsum((-1,-2,-3,-4))% Z* x* P% x- [, s6 C* f6 r- D
- -10.00 A' M: I% a1 Z, {, J: b0 q
- >>> math.fsum([-1,-2,-3,-4])
3 A" R0 ~2 x+ S6 N; I - -10.0
复制代码
^0 m# @. \8 n9 g5 a k( O3 F2 Umath.gcd(x,y) 返回x和y的最大公约数3 R3 \; p1 V: R, s( }2 ]
- #返回x和y的最大公约数$ r& j' y( T8 i; [% r
- gcd(x, y) -> int$ I) ]) k1 }) D& }/ z
- greatest common divisor of x and y
- u0 I, ^' |# E$ ~& R- t2 Q - >>> math.gcd(8,6)& z% o# `* ?7 g! l
- 2 y! ~ b* R" a, N- q* C' }
- >>> math.gcd(40,20)$ F& m+ m! y3 r3 N9 }$ S& O
- 201 T% C9 ]7 ~1 ?/ F7 V
- >>> math.gcd(8,12)* ?; J5 n( R! i( d% ^! s4 i5 [6 B
- 4
复制代码 9 M" c1 u' F- q u; L
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
9 _, q4 Q( n6 V: K! l- #得到(x**2+y**2),平方的值5 |& M C: a) x+ _: Z, r
- hypot(x, y)
5 r, {9 {" p$ c - Return the Euclidean distance, sqrt(x*x + y*y).
' q4 t' X: O' O( ~7 c% Q - >>> math.hypot(3,4)
K, n* }8 K! l! L - 5.0! _- U( D$ b# O
- >>> math.hypot(6,8)
5 i. r( r0 L+ h! X( C A7 n - 10.0
复制代码 ) H' I- [* ^5 N1 `
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False' f% D, D3 y) P& d3 x5 I4 m3 g
- #如果x是不是无穷大的数字,则返回True,否则返回False
, ?0 R& Y6 V* j( u, m9 \ - isfinite(x) -> bool
7 q) t, B% l2 C% z* k8 g' X# [5 g" Q - Return True if x is neither an infinity nor a NaN, and False otherwise.
! Z& ~$ Y- K9 N - >>> math.isfinite(100)" [( P" M( {) A/ i
- True" F" v- H7 n% h
- >>> math.isfinite(0)0 f/ d# K( R) E' a" y
- True
3 D: z4 E0 x- a& [ - >>> math.isfinite(0.1)
8 s$ N+ D3 g0 e B) m( l0 ]- ~ - True1 z2 c$ L$ s k
- >>> math.isfinite("a")% D& _, `2 {5 j- c
- >>> math.isfinite(0.0001)
6 q/ i( M8 p! u* \, u9 ] - True
复制代码
- B" {& s; U3 d; x: t1 i6 gmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False' x' d g5 B2 r% B* B. O1 d
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
2 N6 k9 m9 L/ h3 H* i1 e5 q1 j - isinf(x) -> bool
) p, ~: l2 x5 a2 m% Y& }7 b - Return True if x is a positive or negative infinity, and False otherwise.
( K6 d- l, }! h; r. R; E& _, n2 n/ J4 s - >>> math.isinf(234)9 c+ V# O+ T: M- @& T5 C
- False
% d, y; V" D8 k$ W* o t8 M: W - >>> math.isinf(0.1)
0 c- ^6 I: Z2 _6 R9 A- c - False
复制代码 ) f8 h. j( P% F4 ? W. B
math.isnan(x) 如果x不是数字True,否则返回False
. `2 W, Y& d7 f5 ` j2 b9 s- #如果x不是数字True,否则返回False
" U7 B& Q- Z9 B+ S( H1 u2 N - isnan(x) -> bool
1 q, S' } q; a9 x% [# [ - Return True if x is a NaN (not a number), and False otherwise.3 O+ d' |* t; w5 R5 J3 b
- >>> math.isnan(23)
# }) {; p' c8 p% h+ b/ H* H0 I - False
4 _) w' i" \2 b - >>> math.isnan(0.01)
3 y/ l: f* A) N/ p/ [7 K+ S - False
复制代码 ( K8 v9 P) v) p% `
math.ldexp(x,i) 返回x*(2**i)的值
: _. v$ L' I. Q. N& F y- #返回x*(2**i)的值
$ O( I- a9 G) @1 W% V - ldexp(x, i)$ j) [3 g- R: x' _
- Return x * (2**i).; J+ U( X8 C: g) h
- >>> math.ldexp(5,5)- E/ U9 V1 D+ E. O! J
- 160.01 R; Y# n5 y8 K) h5 l1 h* u
- >>> math.ldexp(3,5)5 i6 N: K, x: y0 x5 Z
- 96.0
复制代码 . V3 ~5 C ]2 j- R* k% ^" E! `
math.log10(x) 返回x的以10为底的对数
; z: D1 u; ]" O! Y; @- #返回x的以10为底的对数
! v! E: y* I' ]/ { - log10(x)4 S, J: Z$ ?& k% f i
- Return the base 10 logarithm of x.$ R( G* J4 K$ C1 }& Q) d6 R/ Y
- >>> math.log10(10)
# x9 r* [% k5 M! i0 o- \. \ - 1.0 M+ E5 x# ~# _
- >>> math.log10(100)6 m4 E2 F2 S- C
- 2.0+ R3 L7 l+ `/ e# P) s, k
- #即10的1.3次方的结果为207 E) g F9 u9 ~
- >>> math.log10(20)
) T/ A& a2 x; w; J - 1.3010299956639813
复制代码 $ K B" r1 T# G8 E M5 |1 c
math.log1p(x) 返回x+1的自然对数(基数为e)的值
, a6 G$ {, X4 a- #返回x+1的自然对数(基数为e)的值' {8 t; l+ X7 [2 Y4 `2 T2 u
- log1p(x); O# L; E; _/ q% \
- Return the natural logarithm of 1+x (base e).$ s( n$ x, K# m4 j2 A" I; F
- The result is computed in a way which is accurate for x near zero.2 O. |- |1 t G& K: z+ H+ a1 n
- >>> math.log(10)
, q4 w: N* I) T& V; I: u - 2.302585092994046
+ Y" y, S7 M( w - >>> math.log1p(10)$ K2 u% s' ]3 f
- 2.3978952727983707$ |3 S! ], c( K( x! Y( O1 w( @- K$ g: t
- >>> math.log(11)
! [+ R! I/ k& h# v3 N$ C - 2.3978952727983707
复制代码 . C) @0 r8 p B, W1 x, C* `
math.log2(x) 返回x的基2对数
, g7 T( R2 o& h* b2 [- #返回x的基2对数
. g1 I6 J$ V' ^# q! c - log2(x)
- p; \( q( F- y# P* \ - Return the base 2 logarithm of x.
/ [# ~( q: r, j* w: X - >>> math.log2(32)/ m+ l0 t" ?$ z9 t/ _" }
- 5.0
% M/ L( | R* g* g- ^0 T& i - >>> math.log2(20)
, F3 W6 [# W' A( L2 U3 Y - 4.321928094887363
# S4 V. p3 @& |3 J% x - >>> math.log2(16)
3 g5 C: V7 x% \! v5 m+ Q - 4.0
复制代码 $ u% K _, I0 R1 v# k, C
math.modf(x) 返回由x的小数部分和整数部分组成的元组, _9 p9 N: r/ L4 K7 e; \
- #返回由x的小数部分和整数部分组成的元组3 t7 b: K3 R c$ W( ?
- modf(x)
. I; i& p' G, v7 o5 U0 n9 |* j f& l - Return the fractional and integer parts of x. Both results carry the sign0 x# V+ p W1 L/ b2 N! ^' H
- of x and are floats.# O$ g7 u- D6 D3 T5 r
- >>> math.modf(math.pi)
" A. m! ?3 e$ O* V; U. k - (0.14159265358979312, 3.0)
+ u9 H) r0 y( y - >>> math.modf(12.34)
/ u5 G* [- A* q! T - (0.33999999999999986, 12.0)
复制代码
7 D( v( l8 v7 o- }& Umath.sqrt(x) 求x的平方根0 L j, y3 F9 h' G+ y0 K6 Y2 h
- #求x的平方根
) Q' c6 g8 R$ u$ Y - sqrt(x)2 v, G0 p' ^1 ^1 x3 ~9 Y$ e
- Return the square root of x.5 ]7 v7 k' H2 m/ y( I0 X' u4 v
- >>> math.sqrt(100)7 l* ], e$ F3 w! _" K
- 10.0- V. q, t" E. O
- >>> math.sqrt(16)8 S" O F. u$ l
- 4.0
9 b7 r) m) }/ m6 e) l+ z - >>> math.sqrt(20)9 S0 n% v) {$ c" V4 x# t- [# ~& B
- 4.47213595499958
复制代码 & {6 c/ p3 K/ g8 m% n; A
math.trunc(x) 返回x的整数部分- #返回x的整数部分
) d- ], q% @( R$ k) g - trunc(x:Real) -> Integral R! R2 v' `) h4 m
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
: T4 M8 l6 }+ o% C7 J" m1 u9 c& ` - >>> math.trunc(6.789)9 W9 k5 P, c6 Z9 T. D
- 6, b( W7 Z+ H$ c/ u9 i
- >>> math.trunc(math.pi). Q4 D0 H! W* S$ B( f! ^) u
- 3
+ [! h* F2 }" t - >>> math.trunc(2.567). d: e8 M/ I) l; ?, G: I
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|