马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ E7 E7 @9 Y" P" C' O% s. G2 q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 J x% L4 W* p, o& j; R! u* p' f3 l: f* j# l' }/ q1 v
方法1:
$ b& o9 S' l/ m* `6 @! t3 Y- >>> import math$ j5 m8 |5 w F) L. k
- >>> math.sqrt(9)- a; M/ k& Q/ |. E9 J' ]. G
- 3.0
复制代码 方法2:# r9 c; P) d* n% z
- >>> from math import sqrt
! g; j9 L$ |$ {/ W - >>> sqrt(9)3 n; r8 P! a- x" s' g
- 3.0
复制代码 ) r8 Y4 D, `! Y, S M7 q4 R4 @9 y3 Z
) b k. W& e2 @$ h) h: X: Gmath.e 表示一个常量9 ]# L# ~2 g7 J
- #表示一个常量, J$ u" _1 z( W) _6 V& e; y" B
- >>> math.e6 u) O! \4 a: X# A- Q! N' O' ]. h
- 2.718281828459045
复制代码 # d3 X& b$ L& s+ j" r
math.pi 数字常量,圆周率$ B1 T4 y$ ~5 M6 S7 k7 a- h
- #数字常量,圆周率
, F- W% k) |& _% g. ?: p - >>> print(math.pi)
* x% |+ ^9 s9 P3 v' n - 3.141592653589793
复制代码 ( V R4 G r# k5 Y& s
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x% X9 C/ n$ Q4 A& Z" _
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, \3 B3 A' v# f) P
- ceil(x)2 i k& U9 v* `) [/ C
- Return the ceiling of x as an int.
3 [ l+ m5 \0 x# n" E - This is the smallest integral value >= x.4 U) u9 m/ D6 T- q% v8 `
5 [) K7 n! t; j5 m5 F- >>> math.ceil(4.01)( e" ~8 f& U( _
- 55 n" Y: _% J9 H6 @# {* R# h# m
- >>> math.ceil(4.99)
- h: _7 w& Q2 b& I% H - 5
! c: c0 J$ ?4 m1 Q6 ^6 v - >>> math.ceil(-3.99)2 x0 o2 Y! [2 c3 y' |4 K& o
- -3
2 G. E% c6 f- o# L- ]" K5 ?3 Z - >>> math.ceil(-3.01): f; J) L( {4 ?1 Z
- -3
复制代码 4 F' @( D- W. w
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 N/ o# B9 ^ o5 }' D, f6 u8 q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' X! l# O8 L9 k- b* a# s - floor(x)
8 h" {; ]0 z3 J' P - Return the floor of x as an int.9 n+ v& A2 e. M3 J3 u
- This is the largest integral value <= x.$ M( U5 J8 g) b
- >>> math.floor(4.1)
3 T7 w* f9 V* P - 4
5 `" d: M$ Z$ _$ S - >>> math.floor(4.999). O& j6 h+ ~. g' `0 s+ x) a
- 4
; Q7 {+ z" ?6 s! F0 w6 | - >>> math.floor(-4.999)8 U- |6 V, I8 |; M
- -5
% f! q" V0 b2 ~ - >>> math.floor(-4.01)
0 w, b0 y6 w7 w/ o8 {. \4 u; q - -5
复制代码 : G- x; Z) V) s" N; N a: V
math.pow(x,y) 返回x的y次方,即x**y+ K/ v. U( ?1 G0 R. V; K! Q
- #返回x的y次方,即x**y
8 |: t' H8 R5 U/ W* _ Q: j - pow(x, y)
6 y& k1 I |( |& z1 i - Return x**y (x to the power of y).2 F P) H4 o3 h! P6 r" }
- >>> math.pow(3,4)0 P, B2 g0 G1 ~' c0 W
- 81.0
+ r2 X; r, [. I$ U, \, Y - >>> & ^3 |. Q$ I5 K3 a- {! i6 n
- >>> math.pow(2,7) R' a! G' o5 c1 T8 v3 J9 E
- 128.0
复制代码 8 h4 E2 `1 I' y8 K
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# U. L; z8 G" }$ Q
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)1 F8 X9 |3 Y7 j' i
- log(x[, base])* T$ U$ i; ?. y
- Return the logarithm of x to the given base. a$ ?3 |6 V7 `( A
- If the base not specified, returns the natural logarithm (base e) of x.
N H: N2 ~; R - >>> math.log(10)% K N' W: x4 [4 g, o; k$ r! s7 ?
- 2.3025850929940463 g% y# s& i$ R: o3 w0 A" |, Y
- >>> math.log(11)
) ?8 t) C1 o: V6 ~: {- n - 2.3978952727983707
1 _$ g/ ?; c: k6 P% U+ y - >>> math.log(20)
] G% |* U$ d# r) M0 r - 2.995732273553991
复制代码
$ k/ [( E7 s1 z" L8 Smath.sin(x) 求x(x为弧度)的正弦值
! ~0 u [' K+ o6 g/ a, ~9 @- #求x(x为弧度)的正弦值% v0 p' ]" V! Q( U
- sin(x)) g& g3 A. V9 P6 R
- Return the sine of x (measured in radians).& G* f$ a; t6 Z' H! b2 U
- >>> math.sin(math.pi/4)
( y2 i) ^: z0 G3 f" g - 0.7071067811865475
+ u3 t9 L) `) P - >>> math.sin(math.pi/2)+ G8 B0 z0 `6 a9 W# q; P( S
- 1.0
) m J W3 W/ Q2 y* S' ]" r5 r4 F' I - >>> math.sin(math.pi/3)
" I! n" c- n4 e - 0.8660254037844386
复制代码
# f3 b: ?% b0 c, { Tmath.cos(x) 求x的余弦,x必须是弧度
" N; }. D" g% S6 Z7 _- #求x的余弦,x必须是弧度
6 u/ }4 F2 ? @5 P- f5 ]' I - cos(x) R' o' S W5 C
- Return the cosine of x (measured in radians).0 L/ K! K0 [1 v
- #math.pi/4表示弧度,转换成角度为45度6 s q6 c, s7 f
- >>> math.cos(math.pi/4)+ v# S! E& Y; n* ]2 E
- 0.7071067811865476
, H; U: @ x$ P# X9 S0 V* K - math.pi/3表示弧度,转换成角度为60度
) L- I. j- B+ b9 n' w- j - >>> math.cos(math.pi/3)5 c1 z6 S0 A: w
- 0.50000000000000018 S: o) v; i3 i
- math.pi/6表示弧度,转换成角度为30度4 K% Z2 p- \& R! ~. c0 A4 {- Q, ^
- >>> math.cos(math.pi/6)) @/ e8 u- B& m
- 0.8660254037844387
复制代码
0 J. Y' ^2 \" P+ b; E! @" emath.tan(x) 返回x(x为弧度)的正切值- e1 g6 [8 W2 Z) f
- #返回x(x为弧度)的正切值
$ U* _, B$ \" _9 x - tan(x); E$ }* ~) L' H3 {
- Return the tangent of x (measured in radians)., t4 N3 @+ O4 l6 Q P2 m0 s
- >>> math.tan(math.pi/4)
5 [' |, |2 g* \ - 0.9999999999999999 q( e* v- Z6 o$ U6 H
- >>> math.tan(math.pi/6)
+ ~2 D, @ ^9 B - 0.5773502691896257
* b4 I$ x+ c& m4 W- i. j8 q1 b4 J - >>> math.tan(math.pi/3)
^: ~0 ^# N! X+ ~" } - 1.7320508075688767
复制代码
0 t2 y5 ~ f; }7 nmath.degrees(x) 把x从弧度转换成角度
% t! X# B+ x0 m. _) [3 v$ D- #把x从弧度转换成角度
: D9 S# X+ Z* F/ z - degrees(x)
1 Y3 u& E6 o1 j2 P- z1 ~) V6 i - Convert angle x from radians to degrees.
( y" r# D1 E$ ]/ ]4 M" k7 N5 P - 3 n {2 N* [9 x2 y' s3 R' ^
- >>> math.degrees(math.pi/4)
6 ]; I% T, l6 w8 V - 45.0: E. b" V& \" V2 f$ |
- >>> math.degrees(math.pi)! V& h# N7 A8 `6 a& G
- 180.0
3 r$ f$ d' n0 @' ]; z' [7 k+ m - >>> math.degrees(math.pi/6)& g; x/ J- z/ |. U9 J/ A: N
- 29.999999999999996
, s# V/ \, u) f$ u# G - >>> math.degrees(math.pi/3)2 s) c8 v+ q* D4 k" s0 Y
- 59.99999999999999
复制代码
5 g' I6 q o) t8 ~* f' G! Pmath.radians(x) 把角度x转换成弧度
, B+ m' M& L g) ?! f- #把角度x转换成弧度
6 |& ]* j% }) `4 F" W# C, n) d8 r1 z: { - radians(x)# g& X3 n. E( B2 L* o0 m
- Convert angle x from degrees to radians.
. g( A) [" F* { \2 j/ D) z2 ` - >>> math.radians(45)
' ]# C9 ?$ l6 `8 U - 0.7853981633974483/ r6 c3 n* Z" w% s; d: Q1 \
- >>> math.radians(60)% ]% [( R; a. q" d O0 m- p' f! U6 d
- 1.0471975511965976
复制代码
/ j" q7 ?, h9 t3 q3 s7 ]/ qmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 a6 P/ s0 W$ O2 |- #把y的正负号加到x前面,可以使用0/ F. Y5 N4 p" b P2 K2 |: c
- copysign(x, y)- w4 t7 z, E8 ~
- Return a float with the magnitude (absolute value) of x but the sign
/ H% n) ^/ w" p/ @ - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
: i6 l+ O4 v+ E - returns -1.0.
+ [7 U( k$ F7 b4 g - - \% H s( z. w9 g
- >>> math.copysign(2,3)
' W+ x: ~* S% |; @% J1 _1 _ - 2.0
( U* l ~( k/ W | - >>> math.copysign(2,-3)
6 L R8 z+ o4 b! D - -2.06 r6 ~4 G5 l7 y% l6 {
- >>> math.copysign(3,8)
3 Z- ?" M- T& c3 S - 3.0
2 C0 D% |" B" r: { - >>> math.copysign(3,-8)
& f- \* G: ~9 w4 U- ?6 I g" \ - -3.0
复制代码 $ N0 o6 }9 z+ d3 L& J8 {
math.exp(x) 返回math.e,也就是2.71828的x次方
% \! {9 p& b' N; Z( c- #返回math.e,也就是2.71828的x次方
$ M! b! Z5 M% U9 v3 P+ ?+ R/ Y, U! A; ^ - exp(x)6 t2 e& I l0 N4 x/ Z' U! ~
- Return e raised to the power of x.. N0 n$ m0 f" T! l. a
+ C1 ^& T$ T! S* Y' V5 C9 T- >>> math.exp(1)7 x! F' J8 [, c1 V; a* F! b
- 2.718281828459045
* ~3 R* S! E \# }+ y, E - >>> math.exp(2)
+ S, |2 a3 M! T- T Y; s$ Y - 7.38905609893065
1 I- T0 e! s7 R {: }7 J - >>> math.exp(3)
% s. l. b) f2 k9 W6 x1 c - 20.085536923187668
复制代码
2 i. B( |* n& @8 e" x9 Y8 Emath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减17 j& h$ ^% p# G% P7 K
- #返回math.e的x(其值为2.71828)次方的值减17 P5 |' t: F* C% C6 m
- expm1(x)5 C% _9 f+ j9 q! U4 t' p4 e
- Return exp(x)-1.1 ?% L k! a. F9 f3 j4 w% h
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 x8 {- X% H. w" L M4 q - % L d+ E0 F9 `6 o
- >>> math.expm1(1)
( g c6 c' W% ` - 1.718281828459045
| p( B6 `* J) l" k/ z2 m0 L - >>> math.expm1(2)
+ w/ K4 F$ D' e" \) J/ ` - 6.389056098930650 O7 l* @* N: ?/ R5 V+ q1 T9 o) ^
- >>> math.expm1(3)
- X# M; j: Z. e - 19.085536923187668
复制代码 5 _8 y- |* X) P' Q
math.fabs(x) 返回x的绝对值
* h: j; A' G% d4 R- #返回x的绝对值9 J1 P1 W1 y( O6 p
- fabs(x)3 V8 C! I' _3 O
- Return the absolute value of the float x.
2 w; G* F$ P X# p/ ^ - . q5 y1 D! v/ t8 M5 @
- >>> math.fabs(-0.003)1 \; { ^5 Q9 h2 |% v- p0 u: G4 i: W3 J2 C
- 0.0030 X9 N* a9 y- I! f1 P3 g
- >>> math.fabs(-110)
3 @# Q& m/ g! n5 ?$ f - 110.0
4 S1 ^6 k; s" p; x4 b' _- t" M - >>> math.fabs(100)
a# o; H& {) q7 d0 E - 100.0
复制代码 $ P8 c( V! D7 F0 u; C4 Q$ Y
math.factorial(x) 取x的阶乘的值$ b( j* Z2 T2 S% {4 Z6 h- P
- #取x的阶乘的值! i1 s1 n% Y8 ]4 Y7 O" M3 h3 v
- factorial(x) -> Integral
' q1 a7 w5 e5 R3 z& S% a0 ^ - Find x!. Raise a ValueError if x is negative or non-integral./ f3 b! y& D. C/ C0 f$ e' h
- >>> math.factorial(1)) L- T) R$ W p0 y* F! a" V
- 1
, v( \% ^/ K' }9 e* j - >>> math.factorial(2)
" {2 o+ \2 e; E/ F8 u - 2( Q1 n" v( Y2 t" z
- >>> math.factorial(3)
1 d& p( U7 l3 k8 Z& l - 6
0 ~; |. Z1 V0 w0 d9 H - >>> math.factorial(5)
; v: }2 t- ?7 D5 x( H3 o+ @" b - 120: i! a: Z: H; [. {
- >>> math.factorial(10)
4 a3 `! t- s$ Q+ c0 w# R7 a - 3628800
复制代码
# s \1 M" q* b! F. Imath.fmod(x,y) 得到x/y的余数,其值是一个浮点数- U' J; I- ]0 Y
- #得到x/y的余数,其值是一个浮点数 D& q4 | z) \% s5 I% G
- fmod(x, y)
! z0 i. v% B; [3 d - Return fmod(x, y), according to platform C. x % y may differ.
% j( n' q2 H& | - >>> math.fmod(20,3)
0 O) v/ r2 |6 C* \5 C - 2.0/ q, [, U5 S0 y V7 a
- >>> math.fmod(20,7)3 V; q5 p1 g: x& }; f* \2 A
- 6.0
复制代码 / x! r$ x0 U! o9 E+ F. x" t# |2 x- g( }
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 c: O$ u, ^8 ?8 g7 y2 S5 O* N/ t
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. D( O. _8 L" p4 R6 M$ `
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
M, `) p x/ ] v4 W; O& y - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ G- H9 V0 m- t* e0 x - frexp(x)( n6 s# R+ H0 u, X. C: @+ `8 n
- Return the mantissa and exponent of x, as pair (m, e).
+ t8 ~* B1 G% o4 [& [2 j - m is a float and e is an int, such that x = m * 2.**e.
. s/ g+ X7 X- \* N! z( p9 s' N; d - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
8 }5 v4 M4 U7 Q- V2 ~3 Q - >>> math.frexp(10)
9 R Z6 L0 `! O- V3 L/ g - (0.625, 4)
B3 |, ~/ n1 J2 B: {0 u - >>> math.frexp(75)6 S* U" x& R6 @; n! L
- (0.5859375, 7) j5 o" @8 h% L2 C! P/ U
- >>> math.frexp(-40)3 s, i" E/ E! p0 z7 C& p
- (-0.625, 6)
+ k5 r. [' Y& p. E7 x' P F0 S+ q# C - >>> math.frexp(-100)
. f3 \: h' \0 i8 t5 p) \ - (-0.78125, 7)) V- ]' ^& d& Y6 @3 Y; G. O
- >>> math.frexp(100)( N% ^' O8 M5 Y; n+ g
- (0.78125, 7)
复制代码 / M J+ \" r0 U8 u. k; T) a
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)) @) E1 E2 ~2 \/ m
- #对迭代器里的每个元素进行求和操作
. d1 b. x, q- {6 {3 b/ L - fsum(iterable) F/ G r) _# w. @+ ?7 T/ R2 D4 e
- Return an accurate floating point sum of values in the iterable.
! F7 e& n! X! L) p0 X+ F( A - Assumes IEEE-754 floating point arithmetic." [' g4 K+ h9 p3 Q
- >>> math.fsum([1,2,3,4])+ V& \4 ~' m! v4 j) _' ?- E
- 10.0- ]* M. h0 b8 `$ n" L2 N6 L
- >>> math.fsum((1,2,3,4))
7 N6 X$ F, d4 _- ]3 L9 E - 10.05 H: Q% {& n! b( f" y9 \
- >>> math.fsum((-1,-2,-3,-4))
3 \% Z2 A7 W7 ^+ h2 q5 I( f4 F% q - -10.0
5 z0 C& I& f' Y8 l3 `$ C - >>> math.fsum([-1,-2,-3,-4])
+ Q: Y, n+ x$ N - -10.0
复制代码
& x: B3 `- y" R, h: qmath.gcd(x,y) 返回x和y的最大公约数" P% r+ p' M, T; L* ^4 I2 F# z5 u7 d
- #返回x和y的最大公约数) C( m! f/ D/ |
- gcd(x, y) -> int+ V5 ]+ ~5 _( t& U# L
- greatest common divisor of x and y
2 O% m9 h4 g6 x9 q& S. O. x - >>> math.gcd(8,6)" c( _) t* T9 ]; L- P
- 2- m5 `( y5 z p8 C( D. W
- >>> math.gcd(40,20)
3 V& @, ^6 x+ i7 L3 B% N - 20 z- M8 N$ _ l% F0 ^ @4 h$ S& F2 k
- >>> math.gcd(8,12)
- T, C# `2 L9 Y: j, n- P& N( E; U* `6 U - 4
复制代码
& I- `5 Y9 |9 v& N, j/ G; Wmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False% w9 I; l" c! b/ e
- #得到(x**2+y**2),平方的值
" o) ?( v: X2 v6 C" J- w - hypot(x, y)
7 U% O0 H" ?' K2 ? - Return the Euclidean distance, sqrt(x*x + y*y).
/ k7 S. ^, r5 B6 w) b - >>> math.hypot(3,4)# T+ q/ P- u6 }5 [* g. w1 i
- 5.0
0 Y9 v' \! `- v& N, \5 j; D8 o, B - >>> math.hypot(6,8)
, N9 K! H D$ r' ]4 S2 Z - 10.0
复制代码
/ ^3 C! E1 L" }math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 E# B F$ h9 O* @2 v* Q8 @, o/ G- #如果x是不是无穷大的数字,则返回True,否则返回False" @2 Z4 C; A! E: f( M6 `
- isfinite(x) -> bool
" q) [" l4 f( A - Return True if x is neither an infinity nor a NaN, and False otherwise.: O( p, ?) o0 K. H0 i5 p
- >>> math.isfinite(100)
. }( M) r9 c. o" \# _' @ - True
8 j: a) K) [" A - >>> math.isfinite(0)5 {' G, X: |/ c. X8 j( L5 g j
- True6 M# N5 C+ l$ [3 u# x
- >>> math.isfinite(0.1)
& V8 O' C) M, B9 j. k4 d- E - True
: x5 L* f& A) q8 E - >>> math.isfinite("a")6 t7 u4 v5 @, b, s! g8 L( s1 I( v
- >>> math.isfinite(0.0001)
1 `$ W5 B4 F) S9 J) B0 H - True
复制代码
+ N! ]7 Z Z" Vmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False$ a+ D" X& l7 o, n" P& M. I' ~0 M
- #如果x是正无穷大或负无穷大,则返回True,否则返回False4 e. ^, e* c# j) `
- isinf(x) -> bool
# i+ @( r% W! {' @0 s+ |6 C& s% N6 ?+ G - Return True if x is a positive or negative infinity, and False otherwise.
1 O8 y$ O- K5 Q( t$ m) j - >>> math.isinf(234)
5 v1 r$ V4 q1 o+ f) h: v# r - False
& S6 m: S& p- ~1 h- [" q+ i% d% R1 a/ l - >>> math.isinf(0.1)
v# y8 s2 x; I! M, _ - False
复制代码 & Q9 J, D2 c* {7 d( z
math.isnan(x) 如果x不是数字True,否则返回False
7 J, a' A* ?: o+ J. S) O# _- #如果x不是数字True,否则返回False
9 q" T2 ]0 q% O, y( E# E* r - isnan(x) -> bool* j0 Q/ J3 m3 O3 a
- Return True if x is a NaN (not a number), and False otherwise.2 \2 c9 G6 k4 h/ k; j- }
- >>> math.isnan(23)# D9 `1 z8 l$ c1 X @' `
- False0 z0 d! l+ c/ j& @; M& R9 a' y1 S! n
- >>> math.isnan(0.01)
6 ?- @) h1 c, ^3 F6 E - False
复制代码 $ W! W9 B& g& h9 [3 r4 s# F1 \/ v+ G/ Z( p
math.ldexp(x,i) 返回x*(2**i)的值# c9 m- a. {" J7 A3 R, o
- #返回x*(2**i)的值
+ R$ [2 U5 ^$ H5 S. _" D - ldexp(x, i)
% i D( s( b' d* A- q - Return x * (2**i).- X2 A9 a' ^/ p! o6 l1 ~
- >>> math.ldexp(5,5)
- D% y; E c0 o% S% q - 160.0
; F+ t: ^& v* m6 ^! [5 h - >>> math.ldexp(3,5)
3 h* w+ [. _9 R+ N( {: M6 H9 Y - 96.0
复制代码
* o7 Q+ c I3 w7 C5 d9 r2 Y) k+ gmath.log10(x) 返回x的以10为底的对数
" P, `9 N0 v- m1 B! ^4 [: [- #返回x的以10为底的对数, z1 A. W* Y% t P* [
- log10(x)
$ |% d; {. r9 R. L$ b ~; X- ^& e - Return the base 10 logarithm of x.
0 A: M2 b r- c# U' i, y ] - >>> math.log10(10)
7 |- D0 t7 n* G/ O* v' q - 1.0
0 @, ]. O# q* _5 s4 A0 _ - >>> math.log10(100)# N, f" u( @! ^" E" t
- 2.0
( K* y, ~* Q0 \% m6 c, a1 }8 B+ R - #即10的1.3次方的结果为200 [7 u! _0 b9 _( ^1 h+ q
- >>> math.log10(20)
+ i& i1 O$ {6 G - 1.3010299956639813
复制代码 ' c. b/ G& O ]5 d: P) T! G6 q
math.log1p(x) 返回x+1的自然对数(基数为e)的值5 \/ j [ A7 B
- #返回x+1的自然对数(基数为e)的值
3 a% K+ ]4 `6 _( ^3 a5 K - log1p(x)
2 _0 _+ c! }' G" Z# n3 T/ R - Return the natural logarithm of 1+x (base e).' \( x q( h+ c+ L; _+ }
- The result is computed in a way which is accurate for x near zero.
' \7 L5 D4 Q- R - >>> math.log(10), e9 H4 z2 B) s, l+ h2 u: }
- 2.302585092994046( P8 d. {5 i1 _; r" r
- >>> math.log1p(10)( v: p" b5 k. `. \& y. J' }% D0 y
- 2.3978952727983707
2 C9 M! b, ?& J, B - >>> math.log(11)8 I& P3 t$ R* J7 `# l
- 2.3978952727983707
复制代码 ' d5 v' U; B( R
math.log2(x) 返回x的基2对数6 f t7 ]+ t* o1 B
- #返回x的基2对数
]) n5 X- l- \: N! F - log2(x)) b G! u, |# E" B9 {" W
- Return the base 2 logarithm of x.; s# |* M Q" t1 p
- >>> math.log2(32)
+ E# n* \1 ~% ~/ I* c5 R" ^& Y - 5.0
+ E" K2 } O. z$ a. l - >>> math.log2(20)
4 y- y- M, ]3 m/ C; [2 }: `- y5 n - 4.321928094887363
; a X2 P7 j5 r - >>> math.log2(16)
/ ^0 J4 b. i2 S* B3 K - 4.0
复制代码 B' P* R6 g2 f# M
math.modf(x) 返回由x的小数部分和整数部分组成的元组( S p$ X1 h2 x! g2 x
- #返回由x的小数部分和整数部分组成的元组
- e; l& ^$ h0 Z# p# O9 `3 \ - modf(x)& v3 g$ f' s! G/ ]
- Return the fractional and integer parts of x. Both results carry the sign
& ]6 e# g# c2 C1 W/ N J4 W Y - of x and are floats.
$ q$ c/ S' L& s; E. t - >>> math.modf(math.pi)) W$ U, m% L6 v1 z% G6 H
- (0.14159265358979312, 3.0)
; ?% M! x3 R( B: j M2 o/ e6 Y - >>> math.modf(12.34). ]5 @5 s0 O# r% c2 O( X. i
- (0.33999999999999986, 12.0)
复制代码
3 |$ f3 ?9 S; g, ymath.sqrt(x) 求x的平方根; L+ L1 i- e6 e3 n W5 Z e1 ?6 A
- #求x的平方根. S; j, e# B9 B3 [
- sqrt(x)
6 J8 @) C8 ?+ f% P, f2 k. S' o - Return the square root of x.: e( o1 C! d" b: V
- >>> math.sqrt(100)
4 ^% s, a3 o2 _" r- B t - 10.0( H/ l4 m' b$ K% W9 I5 v" o
- >>> math.sqrt(16)
) `1 ]2 [: ^$ F1 ?2 h( e9 E - 4.0
3 i$ v# k A# @7 J - >>> math.sqrt(20)# \1 i" G" [2 U
- 4.47213595499958
复制代码 . z& B. r5 g- H& U' W% k8 |2 g' V
math.trunc(x) 返回x的整数部分- #返回x的整数部分
( v2 N, ^9 ?% e" T: J6 K - trunc(x:Real) -> Integral: C# i e: E. [$ B6 _' y& S
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 r* f; C7 O% V# K( j' W
- >>> math.trunc(6.789)
" V! ?% l* c5 H+ M - 68 v$ W9 K- s* m& o3 v
- >>> math.trunc(math.pi)
$ B/ ^- w) g+ A! U. w/ `* ] - 3/ g v5 x* B1 c, w" d7 i, Y* k
- >>> math.trunc(2.567)
" P& _9 l* x! T$ ]" u# n - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|