|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# c+ L2 ]4 _# N1 V0 I, w) d& V( _& E. ]【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。 z* R; R2 N8 \9 n+ {# ~
* S+ N! x! ^) q1 p5 H1 D$ H0 C
方法1:1 w, j% M7 w: w9 p
- >>> import math
! p- t$ d5 ^. _! ]/ w( p4 ` - >>> math.sqrt(9)
( \" m" v a. S/ Z - 3.0
复制代码 方法2:
' q' v/ g) [* S% p4 ?3 a- >>> from math import sqrt% W/ H8 H* Y( |1 l) ]
- >>> sqrt(9)0 v3 \( f z* S( ?/ ^" U2 n
- 3.0
复制代码
; r2 ]( X, _. T2 H6 v # [2 F! J6 X+ q5 J; E
math.e 表示一个常量
: l) S; _6 m K; D0 `- #表示一个常量5 A- _8 F! _" |/ ]
- >>> math.e
2 Z; x% q$ U( l; ]' P; C - 2.718281828459045
复制代码
2 Z( ?( J) k0 xmath.pi 数字常量,圆周率
# S- L: A/ ` g3 z& \- #数字常量,圆周率0 r' [/ _# J/ W8 _
- >>> print(math.pi)
. O6 a5 X7 V" |2 D# ^% v - 3.141592653589793
复制代码 $ f8 }6 b& ~2 I: k) r& V+ ?/ T* ?
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 F, J( N. k: r& G; u- #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 x' d& [" H! o! i
- ceil(x)
0 p" l) M6 e6 y' H* M$ ^; @ - Return the ceiling of x as an int.) H& a3 T- |8 \2 M
- This is the smallest integral value >= x.
2 |6 K, ]/ I% |# N& _* A- M
& d0 b/ o& k2 ]) j' \. {# u# b- >>> math.ceil(4.01)
4 z+ v5 u: N# B8 h( R1 d - 5
( S% b' U0 ~' L" E - >>> math.ceil(4.99)3 d R7 v8 ]. i
- 5
% p+ P% R/ u4 p$ \9 T+ t - >>> math.ceil(-3.99)
1 R$ C! _; ^' g' a2 {) A0 z - -3- w$ n& t- O4 `/ e2 b9 T
- >>> math.ceil(-3.01)2 v4 W, K, i a+ U4 @0 ]* {& F
- -3
复制代码
: G3 S$ P6 l2 G- y+ Y. F# ^0 H' k+ A# `math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# }6 f" D" Z/ \1 p) q) }! E8 I- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! l$ Z! j6 w0 V - floor(x); l, r/ ^- W v$ k1 b& B4 y
- Return the floor of x as an int.
; n! O1 d! E; ~% a6 s' O - This is the largest integral value <= x.
5 Y2 S3 q6 J2 F) d+ d8 _& N - >>> math.floor(4.1)
3 \, ~4 G& @. R& P& F - 4
0 v: @" ?9 X1 L3 C/ W2 i! S' t - >>> math.floor(4.999)0 _! i* z! `8 K$ e" Z
- 4
+ r& R8 E0 b: ?" j7 F4 c - >>> math.floor(-4.999)
3 k* ]1 \- N# H1 b" p - -5( j0 Z4 F% t/ W
- >>> math.floor(-4.01); g0 G9 m2 @, H& b1 ?
- -5
复制代码 . \ J& R8 J+ T/ h, p K6 g- A* H
math.pow(x,y) 返回x的y次方,即x**y
3 H& \' B Y' T+ v5 k9 r- #返回x的y次方,即x**y. n$ f8 G( H! _! L
- pow(x, y)$ {- p2 Y# q! e( X5 |% i
- Return x**y (x to the power of y).8 U1 ]0 P! {& J4 l/ k
- >>> math.pow(3,4)
3 H# A g1 J$ W! V - 81.0, K9 @: n, r7 g, f- t
- >>>
0 Q( b' q, |8 @ - >>> math.pow(2,7)
) p) l6 ^& h( @* E& v5 ^ - 128.0
复制代码 : P0 E$ l& a/ H4 N% ~
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 ]& _/ q% K7 }1 U& W: L- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ J( v2 G/ q, @' v( {
- log(x[, base])( u ]; P4 k% M% |
- Return the logarithm of x to the given base.
( u+ x+ \& \( d- l; v2 d9 M - If the base not specified, returns the natural logarithm (base e) of x.
' i4 f: f0 Y! Q; l" m# f# g - >>> math.log(10)
& X3 v6 J3 l, h- J* p/ d1 B - 2.302585092994046
) _$ Q5 c( f4 d+ A* ]6 O - >>> math.log(11)
2 A* c: t& v5 w/ M7 U5 {7 u - 2.3978952727983707
" T& z( ]( x0 \ - >>> math.log(20)
3 K/ I! z7 [6 a, D8 @ - 2.995732273553991
复制代码
! w' L) M7 k! L+ y$ ` x8 k+ \math.sin(x) 求x(x为弧度)的正弦值* [9 u1 d& y) f' U% l3 r$ B
- #求x(x为弧度)的正弦值" ^! G- D7 `; g
- sin(x)
) V/ e$ ` K1 t4 w! F* J3 i3 g - Return the sine of x (measured in radians).
$ M4 y$ A0 O! ~" g/ y; H5 p - >>> math.sin(math.pi/4)' r# u/ z. S* Y) U a+ K
- 0.7071067811865475
/ B4 `2 J+ E. U$ t8 k/ ? - >>> math.sin(math.pi/2); X) V3 m/ R5 w' a( K" R
- 1.0 y& E! a! J- q" l
- >>> math.sin(math.pi/3)
4 M- Z+ d& a5 n9 @3 } s - 0.8660254037844386
复制代码
- G+ S& o# t# a3 U0 ^$ gmath.cos(x) 求x的余弦,x必须是弧度
) x7 v+ i6 S* T9 B! ?- #求x的余弦,x必须是弧度
$ }2 @! l6 F9 B2 t* v8 B - cos(x), T# m: g, P, L5 r. j$ j
- Return the cosine of x (measured in radians).. s, Z+ p3 `# ]9 V2 G
- #math.pi/4表示弧度,转换成角度为45度5 o0 Q# a4 J/ g
- >>> math.cos(math.pi/4)( M. X4 T7 B8 _0 j- v/ L3 i# ?
- 0.7071067811865476
2 x! a8 B- o* V& H/ w - math.pi/3表示弧度,转换成角度为60度, H) w" u0 U4 u9 D7 X( W
- >>> math.cos(math.pi/3)
4 M" C7 Q. G# E - 0.5000000000000001! v7 h$ Y- n- _/ e4 O
- math.pi/6表示弧度,转换成角度为30度
! h1 p6 {) q5 Y/ G7 z - >>> math.cos(math.pi/6)
1 t% h: Z* m" `: K$ B - 0.8660254037844387
复制代码 ! [ o! k( h" b/ E/ ^% O& O5 L8 Z
math.tan(x) 返回x(x为弧度)的正切值
8 G$ B4 r! J: S- #返回x(x为弧度)的正切值1 \; r5 c( f* }9 @+ J, t& F. G
- tan(x)
1 j2 }( W2 [/ \# t1 | - Return the tangent of x (measured in radians)./ I8 t3 E" j, B' \" |/ l' K+ D
- >>> math.tan(math.pi/4)) [/ d$ ?, E' |+ d' q* X
- 0.99999999999999995 E: b0 S* H) h3 M$ Z5 W% D
- >>> math.tan(math.pi/6)$ x( M f! }4 c* p+ j' O6 h
- 0.5773502691896257
) Z' \) u; i+ d - >>> math.tan(math.pi/3)) y& P2 [$ ^& O# q0 b
- 1.7320508075688767
复制代码 1 l: @* e# S0 r) b# J
math.degrees(x) 把x从弧度转换成角度6 i# k$ t) f" i( F7 W. _7 T' l
- #把x从弧度转换成角度
A" R A* r$ {: M# {" J - degrees(x)! }0 x+ D; Q8 M% j! A6 T8 ]
- Convert angle x from radians to degrees.7 Z6 p _5 l$ Y9 t
- , ]# k; R+ G' ?' A! j9 f. M( J
- >>> math.degrees(math.pi/4)
3 O) E6 L- R D' D+ M1 G0 i+ ~. q* t- k - 45.0% d+ Q- y U- c0 ~
- >>> math.degrees(math.pi)) c! h( K6 I+ M
- 180.0
6 q, j- R1 E$ p. \0 {2 X. P P* s - >>> math.degrees(math.pi/6)
; U4 d& H5 {5 H. p. T - 29.999999999999996
; k* L; @, A& u/ k - >>> math.degrees(math.pi/3)1 T+ ?* q1 n( R6 ^3 y) |4 o
- 59.99999999999999
复制代码
; P. M C$ g& I' |/ Umath.radians(x) 把角度x转换成弧度
( E) V& e+ F c4 B- #把角度x转换成弧度# w' G* S; H2 @
- radians(x), e7 [2 K' R$ H" C) F# ~# ~
- Convert angle x from degrees to radians.
. r9 ~3 g' P! @& B+ ~) t$ C - >>> math.radians(45)
|, ]# v7 c5 V! _0 D. T - 0.7853981633974483
7 z% R4 p+ x# S* m7 A T5 F% D - >>> math.radians(60)
+ Y( @" k) k0 @# w - 1.0471975511965976
复制代码
: H- F' s% e# b! gmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
! F _' _( I8 T' f- Z/ ?- #把y的正负号加到x前面,可以使用0! F) f @+ G6 v0 ~
- copysign(x, y)
* s* I3 g( }7 D3 p, _) W - Return a float with the magnitude (absolute value) of x but the sign " E2 ~& l t _2 n5 I
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
! g3 H, W+ x' `) U9 ?& ]$ z - returns -1.0.
6 Z: b% o$ a/ t( p - 7 V5 _& s+ o7 q) q& z
- >>> math.copysign(2,3)
# b5 f/ i2 a) x$ D5 p/ c - 2.0
$ V. U! n9 Z5 r, T8 A+ |" X7 q1 ?9 D3 M - >>> math.copysign(2,-3). f# U: c0 G% F, P
- -2.0
$ `1 |. H2 F7 Z8 J - >>> math.copysign(3,8)$ l8 J: d( d. ~/ k4 I0 `
- 3.0& X. b, R: Y& ]3 l5 q
- >>> math.copysign(3,-8)6 P- K8 U: L7 ~) z. \/ z
- -3.0
复制代码 " J) Q9 I! x/ Q6 c6 o( x
math.exp(x) 返回math.e,也就是2.71828的x次方+ {8 p, s9 d2 P8 h( q9 w) t' u
- #返回math.e,也就是2.71828的x次方
' U0 P3 D' w7 @. l' v; S$ V9 W - exp(x)/ D+ n* @+ j9 F( P& g- E% y* u
- Return e raised to the power of x.
6 x" q1 D1 y: M$ D( l" `- C
: E ]5 L9 H5 _- >>> math.exp(1)8 u$ {* \ ~6 l0 f" \* D
- 2.718281828459045# n9 @' i- |* n( B: r
- >>> math.exp(2)
/ s1 U# ^* l7 e3 z. i - 7.38905609893065, ?# }* |( m9 @3 x- @
- >>> math.exp(3)
4 y4 E! z! K4 }. P$ i - 20.085536923187668
复制代码 * d' D, c% j+ c) Y9 p
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1& p- Z0 h# k+ X2 @1 s, t9 l% d" g
- #返回math.e的x(其值为2.71828)次方的值减1
$ z6 K& ]( A5 S* `! {! ` - expm1(x)* h ]+ X2 p6 U7 a2 m* N' e
- Return exp(x)-1.
. K: L) M% C: H1 s6 w3 h/ S- y& i; z: t - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 Q) y4 g Z* X4 U/ j9 Q. U
, o9 T( C/ k+ \. _4 y( }6 D/ ^3 Z- >>> math.expm1(1)
$ }& ~. Y% i7 G8 W$ l: Y7 @/ y - 1.718281828459045
@) a( O5 I+ t* p - >>> math.expm1(2)! j) s% Z& v6 c6 b, }6 {
- 6.38905609893065
9 }( C+ R* W P% ?0 a L - >>> math.expm1(3)" T" H9 X: d7 k4 Z: h
- 19.085536923187668
复制代码
( X$ s. J* `3 J. T7 \9 V5 d$ Zmath.fabs(x) 返回x的绝对值2 ]& w: }# E9 t( L" u* e
- #返回x的绝对值
; u5 z2 a( R1 X; y/ }& L( a - fabs(x)7 x: n; t6 |8 W0 Z
- Return the absolute value of the float x.5 e6 g3 d7 P* Y$ w
+ K- _/ A. J/ l$ p t- >>> math.fabs(-0.003)* w9 h% D1 G: L6 O1 p1 q* |
- 0.003
$ e2 N! p" c! Q _2 s - >>> math.fabs(-110)
* x8 S3 b' s3 W/ z8 z2 P - 110.0% h8 u. d2 C: S% c
- >>> math.fabs(100); t6 S! v0 Q) K
- 100.0
复制代码
9 j" \8 W& k. |( X; nmath.factorial(x) 取x的阶乘的值
' b' Y: O" R" I- #取x的阶乘的值
4 k9 S7 C, M! v5 {' J - factorial(x) -> Integral
: n' S0 P2 u; B2 V9 i - Find x!. Raise a ValueError if x is negative or non-integral.) _) r- h( }& E4 ]1 F0 c* }1 ]
- >>> math.factorial(1)
$ h: E2 q) p- w( h/ X% s& h - 14 \. m k9 [- G
- >>> math.factorial(2)- f8 U) {! c, h
- 2
/ Q8 W" p) e* N8 d - >>> math.factorial(3)
; f0 ?7 F4 @+ I9 u1 X7 q. p& @ - 6, J5 q2 ^/ q" |+ n% r
- >>> math.factorial(5)
4 c/ `: a3 w/ o6 T/ _; {1 B! ^' [' P - 120
" t! ?7 [/ a9 A7 J n# W/ |; D* Y+ A - >>> math.factorial(10)5 F( A' X/ H' o* [6 r9 y
- 3628800
复制代码 9 ?% `& \ a( [( c7 h2 R5 D
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数" t+ v0 w# ~0 `5 d1 a1 E
- #得到x/y的余数,其值是一个浮点数" m/ n# @# [- r2 a; Q& f
- fmod(x, y)
6 L; j, L# K- @4 ~: g* E) n - Return fmod(x, y), according to platform C. x % y may differ.
2 {+ m+ k+ q- Y( w% O2 ` - >>> math.fmod(20,3)
' ~8 K" k8 C! C8 W M - 2.0
; n& A9 D6 K$ b% m6 t f- a - >>> math.fmod(20,7)- J4 f) ?1 q+ k/ G+ ^4 s/ I
- 6.0
复制代码 2 ?3 x- U, |7 f6 J
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
' Z0 ?! V$ K. C/ M+ e% ~6 E, h- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
7 Q+ X! w) u" ?6 z/ h - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 h5 f4 }+ Q B# `
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) C$ a, ?; B0 @; k7 a# H
- frexp(x)
- Q0 _: I9 J$ Y7 n; O: ~ - Return the mantissa and exponent of x, as pair (m, e).6 h2 [2 A2 \. o) D( B
- m is a float and e is an int, such that x = m * 2.**e.8 j) V) d2 m: W H9 y3 H
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0./ p6 k# d* m/ B$ U
- >>> math.frexp(10)2 ?3 M" J/ v( O8 V9 [
- (0.625, 4)
" B+ c, ]! H- T( f; Z - >>> math.frexp(75)
- ?# d+ z4 A( z* H( @7 Z) _2 n# c! v7 \ - (0.5859375, 7). F& j* G1 H: |/ \7 C( L
- >>> math.frexp(-40) k" {1 m# q0 E. g, o
- (-0.625, 6)
# k9 H* g& ]1 l2 R _6 `& r5 j; m5 r8 X - >>> math.frexp(-100)
* Q0 W6 Y0 `; l8 n \ - (-0.78125, 7)+ }- ?. @2 }' z0 q0 R$ b: P4 I! X
- >>> math.frexp(100). z& l+ u- `" j- c. P
- (0.78125, 7)
复制代码
; m2 P6 q: t( g% Q, A$ rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列): U# J' \0 W# y1 m
- #对迭代器里的每个元素进行求和操作
. g! Z3 H' d3 }1 [- D - fsum(iterable). H B8 T1 P" d* S
- Return an accurate floating point sum of values in the iterable. x& ?/ s& g- s. Q9 k; h0 s! T2 E2 N5 z
- Assumes IEEE-754 floating point arithmetic.
9 q$ y+ c: C& n5 X - >>> math.fsum([1,2,3,4])
' J* s. \) }5 E! ~+ U5 H p - 10.0+ f- o* p7 o6 R; O0 ^* T w9 x- Z
- >>> math.fsum((1,2,3,4))- G8 U) L" |, o5 a; [
- 10.0+ C! {; B$ b! V1 m8 h/ E% T& l. X
- >>> math.fsum((-1,-2,-3,-4)). x2 v* g% l( I/ v% S" r
- -10.0. Z3 Q1 e! N5 C% r4 b* _
- >>> math.fsum([-1,-2,-3,-4])
! g3 q; j( r" t" Z1 J+ i - -10.0
复制代码 ! h7 ^7 G% N$ m/ x5 ~' l! @" `
math.gcd(x,y) 返回x和y的最大公约数# }4 B+ h3 Y) }& P5 ^* K
- #返回x和y的最大公约数" t9 U% m. G- J8 P; z9 d7 L
- gcd(x, y) -> int
+ y" `% g8 L6 k; y' c - greatest common divisor of x and y
! [4 \2 a7 \3 i7 [) I6 r# Q - >>> math.gcd(8,6)
) m& y6 }, L% N4 k - 22 W2 o8 b4 l. W0 v# b: ^- h
- >>> math.gcd(40,20)1 [ z& T. d/ N+ n( g1 P; M6 s: C6 ~$ P
- 20# l7 J6 H4 `- ~5 I8 L6 I
- >>> math.gcd(8,12) ]/ o4 i/ ^! p# `' E
- 4
复制代码 + _0 G" ?5 A8 k. ]
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False- V9 C& f5 B% g* \6 G, i
- #得到(x**2+y**2),平方的值
! \* C J) h- Q7 O! p - hypot(x, y)
, u% q# m* {: u$ j ?9 c - Return the Euclidean distance, sqrt(x*x + y*y).; y0 p, c! E& }0 {# n$ H
- >>> math.hypot(3,4)
5 i1 N& i2 N3 G+ q" W- m: N2 I - 5.06 V) N. F# k/ n2 W
- >>> math.hypot(6,8)$ r! t5 J' y* Q. n" Z' p
- 10.0
复制代码 , R6 M0 I) W0 y# U. X
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
~/ k4 u6 }# ?8 I1 o. j5 Y( P9 I- #如果x是不是无穷大的数字,则返回True,否则返回False
( e2 F! G6 @. r# R, ^! N - isfinite(x) -> bool
8 H$ S0 T$ i- ]/ o$ F' |; ]2 t - Return True if x is neither an infinity nor a NaN, and False otherwise.' B& l. g2 M5 o* v0 S4 u* f
- >>> math.isfinite(100)
4 j7 R+ ?7 V! k6 g1 T3 l+ o - True
3 T" V9 e/ L( x$ V$ f/ m7 ]3 ~ - >>> math.isfinite(0)
2 {$ k: O! l1 h- M - True; j5 E/ D' S( s
- >>> math.isfinite(0.1)
# G0 B8 @' f) a: R$ p6 f( C, i4 }7 j7 G - True; e/ T+ [( V5 i2 @$ A7 t
- >>> math.isfinite("a")
( I4 M6 i6 _0 u4 x - >>> math.isfinite(0.0001)
7 c: M( q8 L6 Q2 _9 L - True
复制代码
# ]5 e& N1 @/ D5 |math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False, V2 l$ E; M' E7 Q9 L T. r, p
- #如果x是正无穷大或负无穷大,则返回True,否则返回False7 o0 x: ^. }2 a* {' d6 q8 I% a
- isinf(x) -> bool3 Q) i1 ~' N3 O6 E" F
- Return True if x is a positive or negative infinity, and False otherwise.
. s2 H8 {( U" _+ b3 R8 h& ] I - >>> math.isinf(234)3 o# `! N" i ?3 g. G' c8 x8 C
- False3 R" C- G1 V# B+ X
- >>> math.isinf(0.1)
# z! f; M& j! U4 r7 k8 Y, z/ l2 [ - False
复制代码 - {2 p3 K$ v" D# k
math.isnan(x) 如果x不是数字True,否则返回False2 ]/ A% Y8 r! ?+ Q; p/ x) t
- #如果x不是数字True,否则返回False
/ O; c3 m3 u& |, |7 u2 A/ x3 H - isnan(x) -> bool
" ~$ @8 [( X" {! W5 o+ C8 B - Return True if x is a NaN (not a number), and False otherwise.
2 J% ?; B2 |+ G - >>> math.isnan(23)* \0 P& f" O1 r
- False
1 K: h5 m9 O, ]" N0 a# w8 g - >>> math.isnan(0.01)
9 f( ]5 O+ t( B [" m, L% C - False
复制代码
# s/ X( C; D" Imath.ldexp(x,i) 返回x*(2**i)的值
3 q% }; L, V7 f6 y- #返回x*(2**i)的值& b) b6 v4 X0 X3 e- ^6 H- N
- ldexp(x, i)1 v9 r1 \1 ] L5 ~9 Z- x7 o5 ?
- Return x * (2**i).3 A! x6 e/ ]& H5 \" h& t
- >>> math.ldexp(5,5)7 o% c4 m8 p2 _- M2 }4 e5 I
- 160.0- Q2 B3 x: b- f0 b9 S |% ~/ f
- >>> math.ldexp(3,5)5 |% ~$ _: g0 a0 t
- 96.0
复制代码
6 k* t2 O& g6 G! X$ Tmath.log10(x) 返回x的以10为底的对数! }) _9 Q6 c ?8 }' g+ @
- #返回x的以10为底的对数0 y" B. _$ c9 a! _% N g
- log10(x)
. T O* H5 e! y" Q! i6 L - Return the base 10 logarithm of x.
% r' U6 a9 I/ K. J1 a! S, [1 i0 f - >>> math.log10(10)
/ k& O$ R4 u. V" p8 K - 1.0
5 ]$ G X& S; d- e - >>> math.log10(100)
6 ?! D( h% B3 j) j/ C0 }, w4 L6 Y - 2.0
- _8 ?8 ], a* |, H) ^1 A2 i7 `+ Y - #即10的1.3次方的结果为208 y6 q+ w8 y7 R/ L$ }- i& J
- >>> math.log10(20)
7 ~$ o# A0 ]) } - 1.3010299956639813
复制代码 / N+ Q& N: D, L3 i) d
math.log1p(x) 返回x+1的自然对数(基数为e)的值1 @) _& q5 J, g( N
- #返回x+1的自然对数(基数为e)的值
; q$ F% G8 r4 p% U' p P - log1p(x)
* m7 [ I8 V) k, z/ h - Return the natural logarithm of 1+x (base e).3 r5 {& _, L" J. q6 W1 |
- The result is computed in a way which is accurate for x near zero.
7 q2 j) \* {1 E - >>> math.log(10); H/ ?% g1 z Z8 v6 k3 _$ {
- 2.302585092994046
1 g: l( ~3 g5 r5 A& p" B" k - >>> math.log1p(10)
8 ]& n* x, @6 N4 T7 b# g% M - 2.3978952727983707/ X' r- E! Z4 U1 S# r4 z' F
- >>> math.log(11)/ d- }" s* g' f2 e4 a& \8 N4 N
- 2.3978952727983707
复制代码
) y4 u! A$ c5 D1 W$ k+ f9 cmath.log2(x) 返回x的基2对数
( M; D: |2 o( z: n- #返回x的基2对数
- [0 b4 d' i+ K) C7 o - log2(x)# R* h5 V; K, ]
- Return the base 2 logarithm of x.
7 @9 r5 H `$ \, h - >>> math.log2(32)
( m4 _* A1 C% N- h, h; u' y - 5.06 P% {0 D- n$ I+ Y) a* p2 n
- >>> math.log2(20)
; o+ @% p' m$ j4 H* M( Y - 4.3219280948873631 W4 y) ?# ?! T) _
- >>> math.log2(16)
. o+ y3 A9 l" V2 J O - 4.0
复制代码 " _5 L4 v3 K' X# g* j
math.modf(x) 返回由x的小数部分和整数部分组成的元组
, D; B# x1 U1 z I- {# [! y- #返回由x的小数部分和整数部分组成的元组
$ [" t! L! K% w; D# c `8 O/ o - modf(x)1 c2 c& }! A# m9 \
- Return the fractional and integer parts of x. Both results carry the sign- a/ B! w5 R q3 A! P
- of x and are floats.
/ q/ p; H, }# V7 m7 X: J - >>> math.modf(math.pi)4 Z' ?4 h2 g* Z
- (0.14159265358979312, 3.0)/ g. q9 d; A5 W0 N2 p
- >>> math.modf(12.34)
" v: z0 _' Q) f/ J2 Z - (0.33999999999999986, 12.0)
复制代码 : [7 p# B- K5 k5 v% [% ?
math.sqrt(x) 求x的平方根( P6 O0 p& Z. e3 G- {/ ^( w
- #求x的平方根
, E0 V9 m5 n3 A0 m& ^, h - sqrt(x)+ |0 h( G& F% \0 e
- Return the square root of x.
/ W' {+ k+ W# E. h. O) M9 n2 \9 B - >>> math.sqrt(100)
2 X( l Q- K# B7 }0 {7 K - 10.0
& D9 {3 T6 ~/ ^; E7 A; | - >>> math.sqrt(16) P& y ?! V2 K# }8 G$ @! Z" x
- 4.0
5 N7 V) u0 Q, s9 l% o5 N - >>> math.sqrt(20)+ g( h. l' v0 K) @
- 4.47213595499958
复制代码 % b2 `7 [; z# x- d8 t
math.trunc(x) 返回x的整数部分- #返回x的整数部分4 R8 {' X b: v, b
- trunc(x:Real) -> Integral! z' K( O- @/ J8 u
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.. Y& j; P# J _5 p; \) s
- >>> math.trunc(6.789)( r1 ]8 b0 p$ T+ U3 {2 h: J
- 6 N5 s" }7 w+ a. g
- >>> math.trunc(math.pi)) U( H! P; @, v( {
- 3
' c# B" O. \9 Y- S! l: d - >>> math.trunc(2.567)
# k; D+ [3 C, ?/ O4 C4 _9 q0 Y2 x - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|