马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" A& N. W* J3 F* T- J N: t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- U& W( G; L3 k% |+ R
5 r6 |$ ` ~# X$ {- m6 K( Z方法1:
5 |0 v' {$ A1 j3 c s- >>> import math
0 V- k# Y# k8 \1 F - >>> math.sqrt(9)
- S" S5 L# N! F; s% i% k7 M - 3.0
复制代码 方法2:
$ H) X+ k+ \9 I4 a* {7 ~- >>> from math import sqrt0 p7 S' ~' q/ [
- >>> sqrt(9)
" q2 [5 i* E) Y+ w; x. X - 3.0
复制代码
# K' F" L. l$ A
8 P' e$ z+ v/ Z8 _6 |1 qmath.e 表示一个常量* b; ^9 h1 M+ y% j }3 X% Y8 r3 _
- #表示一个常量8 }" Y* f1 m$ b' Y3 a
- >>> math.e# N& Y# [9 ], z, Z
- 2.718281828459045
复制代码 9 r1 h ~5 B3 y8 r
math.pi 数字常量,圆周率
/ s, w, D1 S: h- #数字常量,圆周率
( `) P2 k; T. u. I - >>> print(math.pi)
7 {6 ], F$ Y! Q; ?; B+ S - 3.141592653589793
复制代码
2 [8 K" d) T* `, s# y0 ymath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 [: X9 m8 n% F1 b" s- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, B9 D9 W0 Q; o" N! y4 A0 n
- ceil(x)
- j3 B+ c: D9 V4 h2 \4 U - Return the ceiling of x as an int.: g) F) P" K2 Z' Y& `
- This is the smallest integral value >= x.
* K \2 m9 n: b0 h: F - + N! ]' e# ~9 u5 N* y- [
- >>> math.ceil(4.01)
: [& B. d% ^8 u: z - 5
. C, U8 h- M2 i - >>> math.ceil(4.99), @" L0 F( k7 K: _. b
- 5( M+ a: I7 v9 `1 h; r( D; C! i; U
- >>> math.ceil(-3.99)
( S" s" l7 D! F$ o$ Y! P - -3
4 f8 }5 {+ d7 C& Y - >>> math.ceil(-3.01)
0 x3 x+ H5 w, `& K0 O' z - -3
复制代码
a, D+ b4 _6 l. V3 R; i5 Nmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 z0 g% Y* L3 T4 E( v/ k
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 W) r; @: G, g: o8 g" ^7 s
- floor(x)6 p; f7 v$ k, T" w9 U
- Return the floor of x as an int.0 O7 r5 D0 |1 h. R2 u: M. y( k
- This is the largest integral value <= x.$ e5 ~: U; j9 R
- >>> math.floor(4.1)4 g. G7 X: x- U- k3 Y* u3 b8 c( ^
- 4' U9 s% ~2 ?4 f6 G3 V3 i: U
- >>> math.floor(4.999): V- n0 G# r/ G W1 n4 Q) g
- 4
. a/ g' @; M# u: e& C* _9 s+ P$ w e - >>> math.floor(-4.999)/ c* Y8 c$ Z! I& b8 G# x+ ?
- -54 |& I, X" h/ Z( y y+ l1 N; y
- >>> math.floor(-4.01). q* r+ w4 J# ~; J
- -5
复制代码
* s- n" s' E( [- }4 H3 p4 A# Y$ Hmath.pow(x,y) 返回x的y次方,即x**y
4 f! Y$ ^7 X2 H4 U0 `, T- #返回x的y次方,即x**y
9 n2 n; P5 p8 `7 P$ h# L - pow(x, y)
O! v7 F Z8 L- Y - Return x**y (x to the power of y).
% F8 F( p* s! m - >>> math.pow(3,4)
8 }. z8 D c" X; w8 a: }; Y - 81.0# _8 |6 N+ T- E; n$ i, j
- >>> 8 F1 S! K; ?2 c/ p
- >>> math.pow(2,7); n$ f& t: O+ a0 x
- 128.0
复制代码 : S& H6 ~8 v0 x
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) m0 h" c% M+ a% W# |, q
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& B- A* g0 M* s5 L/ R" @ - log(x[, base])6 s* o2 t& J# V2 i" i7 s8 o
- Return the logarithm of x to the given base.
* X' r0 K' b; |- N1 ^, \ - If the base not specified, returns the natural logarithm (base e) of x.
8 v3 J7 E1 F# L' ]0 A. z - >>> math.log(10), ]0 N8 j# A J- z
- 2.3025850929940465 g: R9 `2 h( g' X" p( I
- >>> math.log(11), y. Q+ W* J$ _
- 2.3978952727983707
' p/ n# u' |9 K$ d% F J$ { - >>> math.log(20)
1 B6 g$ A G" L. |; A$ y9 y* A - 2.995732273553991
复制代码 6 @+ I4 r* R0 n! B2 l X! e
math.sin(x) 求x(x为弧度)的正弦值
% k3 x% X w! W; X4 x# b4 E1 n5 |- #求x(x为弧度)的正弦值
% z* g0 Q) y! B& s7 k - sin(x)6 W/ {& [2 F- T! {1 u
- Return the sine of x (measured in radians).' x4 n# N8 c2 S/ H; l. }
- >>> math.sin(math.pi/4)& A8 D; i; c7 q* U% O9 P
- 0.7071067811865475% @" N6 U5 [/ G- X4 G
- >>> math.sin(math.pi/2)* b5 u+ O% V% |0 m2 k+ s# X9 J
- 1.0
4 K! R( u4 k2 V4 Q' J. [, Y - >>> math.sin(math.pi/3)' N( \0 R! O3 ~ c9 ~6 f! F$ n
- 0.8660254037844386
复制代码
7 L! r) X" S2 F9 C) x/ T6 Q5 wmath.cos(x) 求x的余弦,x必须是弧度
7 U7 E! Q$ \: u6 r$ \; U- #求x的余弦,x必须是弧度
: z. R2 H3 s. S) b: {% `. c - cos(x)
, u0 x$ V. ^: T- q4 {) G5 J+ T2 b - Return the cosine of x (measured in radians).
! t! u$ d2 w- l+ ? - #math.pi/4表示弧度,转换成角度为45度% R# R r7 W0 }3 y5 X
- >>> math.cos(math.pi/4)" I- B* p5 b% P
- 0.7071067811865476" Q! c3 Q5 H3 z, C
- math.pi/3表示弧度,转换成角度为60度% D4 q/ r P, e# i
- >>> math.cos(math.pi/3)
+ V* G, ^5 X1 O - 0.5000000000000001
# V8 z1 Z( ^) ?6 J6 k - math.pi/6表示弧度,转换成角度为30度
, o, M$ n/ J4 s$ G - >>> math.cos(math.pi/6)
$ E5 Q. k) W6 Y% z2 C3 M - 0.8660254037844387
复制代码 " f0 @) ^! C6 l& j. X
math.tan(x) 返回x(x为弧度)的正切值
! @' N: M1 V& I- #返回x(x为弧度)的正切值
( W& }, F$ v2 z4 V3 J" _ - tan(x)
7 e4 P" O4 y* G& O) \1 V - Return the tangent of x (measured in radians).
+ W) Y8 n9 h) [" p1 }& r, o3 q8 r) r - >>> math.tan(math.pi/4)
; x- }/ ]9 p3 q- N - 0.9999999999999999
( j% R. \0 Z" E( w* | - >>> math.tan(math.pi/6)
1 K, @; R* t6 Q& D2 ` - 0.5773502691896257
3 F' d; d. B# E - >>> math.tan(math.pi/3): o3 T6 f* P* ~- `8 F
- 1.7320508075688767
复制代码 7 [9 E7 T* ?' Y( b
math.degrees(x) 把x从弧度转换成角度8 a- y/ K: W+ \) F7 o Q
- #把x从弧度转换成角度
1 r% w9 K; p) ] T# r4 A. R. `+ o8 g0 @ - degrees(x)
0 ]/ ]9 Q. V5 d - Convert angle x from radians to degrees." B8 Z' _4 P6 W1 \/ x5 z
- . c8 \3 ]8 L9 I/ J5 i k
- >>> math.degrees(math.pi/4)
# a8 V$ }% u6 p& x5 b& l3 J) T" a4 x - 45.0
* @' w0 d! e- e! m - >>> math.degrees(math.pi)1 S( L4 v& o7 C5 r6 d4 o8 L( l% U
- 180.0
1 j! O3 T4 x# o0 p @* ~% g - >>> math.degrees(math.pi/6)
) Z/ |) c* n( k: `. Q+ i: l) y - 29.999999999999996
& u$ ^- b" t1 {' v0 E& u/ I - >>> math.degrees(math.pi/3)
9 R- P3 d: a9 w6 Q - 59.99999999999999
复制代码 ( a% D& w* [/ T- e
math.radians(x) 把角度x转换成弧度: ^$ @8 Q/ J$ i% h$ E; T$ o: t8 `
- #把角度x转换成弧度( E [) o; Z* ^# g6 v
- radians(x)- n$ h, [5 T* T6 U9 x7 b% n1 u
- Convert angle x from degrees to radians.
% g6 C w$ ]( R: f' P+ g* a5 M - >>> math.radians(45)
% h, R( B. l8 J% t" R/ U - 0.7853981633974483- n3 l# D* Z# m+ A3 ^
- >>> math.radians(60)
5 d* t! W( l7 {3 v, ` - 1.0471975511965976
复制代码
3 e3 h1 S0 M% W) F5 {# J4 \" _( ?( zmath.copysign(x,y) 把y的正负号加到x前面,可以使用0: q+ x# V8 W7 ]5 h5 H' d6 L
- #把y的正负号加到x前面,可以使用0- o; a8 L8 Z7 o: b! l& Q% {
- copysign(x, y)
: ?) ], ~8 U2 a$ Z+ H. c - Return a float with the magnitude (absolute value) of x but the sign
5 d" [ G* l4 o/ E1 o& u - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
+ H6 `/ z+ y0 I# a* g - returns -1.0.
) [$ Z0 N# `; _; B9 p+ P: a z
* A( U" |/ W- O; W- >>> math.copysign(2,3)
8 J9 Y, V: h: \% J6 T - 2.0$ ~2 _1 T/ U/ ] p" v- L0 O1 r
- >>> math.copysign(2,-3)8 S+ E0 }. _: |0 O/ W& z4 D( [
- -2.0
$ |0 B6 t* V$ N$ Y" X! M9 U, ?3 j, z - >>> math.copysign(3,8)& z7 w1 H% Z. j M
- 3.0
* m7 R- G9 w# p2 w; q - >>> math.copysign(3,-8)! Y1 k2 f* f2 `. ~3 W% W3 k* ~
- -3.0
复制代码 ( W) `" ]% f& V2 I5 S/ t; g( [" y
math.exp(x) 返回math.e,也就是2.71828的x次方
4 Y$ v* m/ G' L+ K; P$ d- #返回math.e,也就是2.71828的x次方4 {/ s8 g" w3 g( `1 H6 g" F' D, M
- exp(x)# j/ j, _; p O% Y6 x6 X; X
- Return e raised to the power of x.
. ]3 S4 w. ]. h& N: S - 8 l. `! L4 P, u
- >>> math.exp(1)4 X3 n1 v, A3 R; f+ J) v
- 2.7182818284590456 ~/ ^. X2 G! K0 M" s' w
- >>> math.exp(2)" S5 f! r2 @: G% Q! p& H
- 7.389056098930653 p% _$ m! X% P3 x9 L [
- >>> math.exp(3)
7 w3 m) Y# v3 S7 J' Y$ Y) q7 ^2 A - 20.085536923187668
复制代码 # }4 G% Q/ G) q/ g* n& i u
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减12 B: G ^; I% q7 x
- #返回math.e的x(其值为2.71828)次方的值减14 r# q6 Z# E; ]3 p6 `5 n
- expm1(x)
0 ^! N0 r3 L# W - Return exp(x)-1.
2 b. j+ K) r4 C' a( {# `8 D - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.5 O4 ?" O$ C- @6 I
# O; ]) }6 U& u/ `- >>> math.expm1(1)1 K5 L$ m R, B8 ^
- 1.7182818284590450 }& c+ B) q; u. }) L% W: V
- >>> math.expm1(2)' }+ \' f% M0 F* T$ r, K, ~/ Q: O
- 6.389056098930653 T$ j6 b, y! L" Y `' a
- >>> math.expm1(3)
$ g! C' Q' ^4 W- T - 19.085536923187668
复制代码
" n. t% J. p5 ~; y; q" w4 Hmath.fabs(x) 返回x的绝对值
* o; R! l5 c6 A! l) H! x- #返回x的绝对值( i) f! m) q9 d5 E3 D8 P" T
- fabs(x)
. G Z4 h% S: u6 p - Return the absolute value of the float x.8 A# e; V, \2 f% Q3 R
- 6 Z; S J+ C( J* F
- >>> math.fabs(-0.003)0 E w1 Z1 ~& A0 `3 Y
- 0.003
4 C: E2 t) ^: F2 \0 J- H - >>> math.fabs(-110)" u: ]8 j' a$ J- P# U- O
- 110.0: ]$ b! b% x! v2 ]
- >>> math.fabs(100)
, B7 e) q: L) w+ `! @ - 100.0
复制代码 U$ @3 |$ Z) R3 e- w; u/ N
math.factorial(x) 取x的阶乘的值/ K/ O3 b& c& a# K1 W. Y1 F' x* b
- #取x的阶乘的值* w/ W- @, H/ @( N/ |1 f! K
- factorial(x) -> Integral3 m# z- k" \4 C2 o' g+ D
- Find x!. Raise a ValueError if x is negative or non-integral.
7 v# F' l, ?1 H4 Y$ N' L& b6 v - >>> math.factorial(1)( s# K" `* u! ?% I
- 1
& J; B( F: @ b1 |% L$ ?/ u - >>> math.factorial(2)9 s& n4 s( X# U; y# m, A1 f
- 2
) w$ g3 `7 z% N4 g& A9 b8 V) r - >>> math.factorial(3)
& D" O( U' O4 D. C3 \0 T+ c7 J9 y - 6 [; t! M6 m% j. o% Z
- >>> math.factorial(5)& d% |# y1 D0 V' m
- 120
$ E. c! K/ e8 X8 ]8 Y/ l - >>> math.factorial(10)
- Y. F' U6 S3 Z0 U - 3628800
复制代码
" r8 y9 F. s" B# pmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数9 E3 s/ X4 x3 y
- #得到x/y的余数,其值是一个浮点数+ ^& c$ H, N Y2 W/ S! ~0 b4 @
- fmod(x, y)' E; j9 }% }! l
- Return fmod(x, y), according to platform C. x % y may differ., l) r( a+ k7 D6 j: T Q4 R+ X
- >>> math.fmod(20,3)
$ Q" j+ h4 g8 {- c - 2.0; e) H: S; S3 ] y% C5 A( a
- >>> math.fmod(20,7)' w. n7 Q: Z. ~5 g: @- W4 p
- 6.0
复制代码
7 G" D* Z1 v* k5 x" Tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% H0 w" B! d1 u, }9 ]$ C- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,0 N3 h3 Q7 l+ o. F- n
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值8 h5 ~4 a! D5 ~8 |. U, o( C
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
$ G+ M9 c' F2 `/ L9 K# G0 { - frexp(x)
% D$ C4 ]; j# M) h% k" f - Return the mantissa and exponent of x, as pair (m, e).
! |/ U* g3 W/ h, Q - m is a float and e is an int, such that x = m * 2.**e.7 ]* S2 t% @9 h# [& b
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
$ ?( g: H9 L! l9 K) L5 p" Y - >>> math.frexp(10)
1 C! D; I6 U/ k - (0.625, 4)
/ }5 _/ S8 {! N, s# Z: h: P* d# d - >>> math.frexp(75) `* f+ V2 j R5 l; R. a8 v- ?
- (0.5859375, 7) e7 n- I/ R0 M% L
- >>> math.frexp(-40)
$ W* ?5 I" H1 o* M - (-0.625, 6)
& m8 k2 S. `8 j0 k - >>> math.frexp(-100)$ C/ M& q% O7 ^. j
- (-0.78125, 7)
8 o7 N2 c6 O5 b% X t+ L( k* x% ]" a0 S - >>> math.frexp(100)
/ H0 S v4 H. ~0 @; Y. M - (0.78125, 7)
复制代码 0 V( r" t0 \4 s2 S0 T9 o
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
* b: Z* C5 V7 C" d; I7 ]0 n0 d- #对迭代器里的每个元素进行求和操作
. T5 s0 y; x( B9 O9 }1 L" H - fsum(iterable)2 ]7 I. }; {. y P
- Return an accurate floating point sum of values in the iterable.
& ]6 R; K. `! V7 Z - Assumes IEEE-754 floating point arithmetic.
, ^& d. ^$ h$ M$ y7 s( z7 e- O - >>> math.fsum([1,2,3,4])2 |3 [ x2 w6 t" N
- 10.0# m8 t; X$ ]* w# U/ a$ j2 i' J
- >>> math.fsum((1,2,3,4))
m1 H+ f z' ?3 I9 \ - 10.0/ s2 n1 e' l8 e& x& c/ j
- >>> math.fsum((-1,-2,-3,-4))
/ k, ^0 i, v( x/ c( ~* M& ? z/ q - -10.00 w0 a8 e1 q( P, f$ h z
- >>> math.fsum([-1,-2,-3,-4]); y8 v, ~( x' J
- -10.0
复制代码 1 x) e: z G- D$ \8 D' z
math.gcd(x,y) 返回x和y的最大公约数: e' A' w% X7 V6 n+ l: G
- #返回x和y的最大公约数8 v6 x. S! X# q1 p1 @" J, X9 \
- gcd(x, y) -> int! o/ N: ^5 R* h8 t" y; e0 h5 F
- greatest common divisor of x and y
' f& |9 x% f' I, l - >>> math.gcd(8,6)1 z/ X4 f, O% i% J
- 2
6 H+ b% J# a2 l" s8 Q' n# L - >>> math.gcd(40,20)% ]8 k9 Y& q, ]* A$ A+ o2 V/ n
- 20
! d" c; `& e: _/ {2 | - >>> math.gcd(8,12)
$ K9 E- _% m& T, q - 4
复制代码 + h/ E8 Y- X0 U8 h; C" @8 @
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False. M/ `, c' x) d1 I6 A& v
- #得到(x**2+y**2),平方的值
" t% H. g l4 |+ e$ x9 b - hypot(x, y). ~& V" \, ~- x9 H
- Return the Euclidean distance, sqrt(x*x + y*y).7 l8 E& Y! S4 U3 v6 v' O
- >>> math.hypot(3,4)
g5 B( W( K6 }- e% o2 n - 5.0/ P. c9 J- y) s- ]. ?$ v6 X& g
- >>> math.hypot(6,8)
' Z( \! D) K# W, { {1 q - 10.0
复制代码
6 T3 I# R2 p: Z: N0 P" ]6 b7 }5 K3 Smath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False/ }8 L( C- b7 e
- #如果x是不是无穷大的数字,则返回True,否则返回False$ K7 f5 n# k( s, w" b8 r
- isfinite(x) -> bool
% S2 d( R9 w9 M+ }. ~8 Y# c9 c - Return True if x is neither an infinity nor a NaN, and False otherwise.! l H3 k S+ H! |' v6 E* t4 P- E: |1 A
- >>> math.isfinite(100), B3 W7 d. Y3 W% Z; m C
- True6 K/ R. @5 `( \/ t) S$ h l! U
- >>> math.isfinite(0)/ P3 L3 G8 K- S8 G, H4 Q+ e
- True
u6 i% l' ~* p( G0 ? - >>> math.isfinite(0.1)
; l' H# ~) z& [8 K& L; S - True, U' w* p; c# K) Y4 T4 r5 |7 ]
- >>> math.isfinite("a")
* d/ d1 x6 Z/ z" n8 e! ^ - >>> math.isfinite(0.0001)) j" v! Q0 _! v5 w2 e
- True
复制代码
! J. P+ l! U( P) i% g: _math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 l" a: q& d5 H% W4 T% g
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
6 `) J& m4 N6 x6 A - isinf(x) -> bool
% d; Q2 e8 L J; F" k - Return True if x is a positive or negative infinity, and False otherwise.
. F5 R8 F( q4 e; }/ H+ R* f; N R - >>> math.isinf(234)8 i9 Z9 q$ ~5 `, n+ c
- False( w2 h5 {5 Q& z: y8 }5 h4 `
- >>> math.isinf(0.1)5 q+ l7 \& k) N
- False
复制代码 / Z' V- Y* N; L
math.isnan(x) 如果x不是数字True,否则返回False
5 Z, I7 I) d8 Y6 g. G- #如果x不是数字True,否则返回False) c; a2 x" b4 Y; g
- isnan(x) -> bool
$ @ [; c& ^3 ~8 R3 |! e" E) Y: _ - Return True if x is a NaN (not a number), and False otherwise.) l; k/ C$ j5 k5 k! w1 Y
- >>> math.isnan(23)
+ z# y* u! G1 f1 ^- Q `2 W. [* Q - False; ~% z: j- v' n( _4 M! R& i
- >>> math.isnan(0.01)
; e+ ]) s+ r+ I# r0 Q- x - False
复制代码 3 \: p( e' ?2 S
math.ldexp(x,i) 返回x*(2**i)的值" Y5 |* O, }! y: q3 n' [
- #返回x*(2**i)的值, z2 Q) a2 \7 I+ O1 j% p( U
- ldexp(x, i)
7 l1 N1 E& E: [4 Y! Y3 ?# [! D - Return x * (2**i).
* O3 }- ?, f& }" s/ t - >>> math.ldexp(5,5)6 ], Y& A' N. j9 L+ d& X4 b8 v# J
- 160.0
+ o* G$ M& i2 j3 p4 Z5 } - >>> math.ldexp(3,5)
. M# W/ L# d; K7 j: T- H5 e - 96.0
复制代码 3 l; S5 H3 n0 p- \, \& m
math.log10(x) 返回x的以10为底的对数
2 p5 T7 F$ ~1 F5 T" |- #返回x的以10为底的对数5 A A/ Y: v1 L1 K, F! _5 _
- log10(x)
: C$ P) g7 p, C9 T - Return the base 10 logarithm of x.
$ K" ~. K# {) ~( ]6 x6 } - >>> math.log10(10)$ e% k$ P; ]1 d. i* J z& u
- 1.0
; g( m3 D0 `# l" p) p) w - >>> math.log10(100): _5 E, R3 l- W9 J2 s }% x
- 2.0
1 D+ Z) C" @/ T, Y - #即10的1.3次方的结果为20 ^2 W2 } F6 O0 W3 c5 I/ N! ~
- >>> math.log10(20)* r1 ]0 B( W9 X- ~8 ? [
- 1.3010299956639813
复制代码 7 \/ \6 y3 y7 u* r" O
math.log1p(x) 返回x+1的自然对数(基数为e)的值0 u1 v$ p1 `) n0 w9 [& M" h
- #返回x+1的自然对数(基数为e)的值
+ h- F6 Q9 K% \) T7 N - log1p(x)" I- \+ e/ [/ L: ~0 K% `
- Return the natural logarithm of 1+x (base e).6 Q0 m' d5 @8 b5 X5 J+ r9 V
- The result is computed in a way which is accurate for x near zero.
9 U' Z( f1 q( F( ]1 r9 f& p! G - >>> math.log(10)
& Q) E6 Y- j8 R1 W, x& I - 2.302585092994046: H) K9 i& J' l
- >>> math.log1p(10)
/ F* X& K$ ]; d: q% ] - 2.3978952727983707" v% X% G& \1 k1 W6 G$ o
- >>> math.log(11)
+ e9 ]. l2 ^: {7 R' b - 2.3978952727983707
复制代码
3 z+ S: w8 S$ j* V2 qmath.log2(x) 返回x的基2对数4 y% w) ?% q# B5 B8 W( k
- #返回x的基2对数( C: u& F9 ^0 Z# s- o
- log2(x)
6 b& i' M: q) w - Return the base 2 logarithm of x.
7 }. Y9 w+ W: m# V; Q6 Z$ U, C - >>> math.log2(32)' z: Q0 z0 `. t6 e" I
- 5.0
. N* R( [( m1 c7 ?0 b - >>> math.log2(20)/ q2 M8 H$ C& l% k( H% A
- 4.321928094887363
?2 \' r% ~3 h, G9 h, ] - >>> math.log2(16). e1 I* m P+ L/ l6 b
- 4.0
复制代码
3 v& T, B( ]: C, lmath.modf(x) 返回由x的小数部分和整数部分组成的元组0 l; A$ y" J7 L+ Y8 q9 [, b( g
- #返回由x的小数部分和整数部分组成的元组
" M7 y# y- W% {6 |; ~ - modf(x)
5 h$ b# i; c& T; c- a1 W& e% P - Return the fractional and integer parts of x. Both results carry the sign+ D) @5 p; S4 M H9 _6 @
- of x and are floats.9 P/ f) C+ G! A4 [
- >>> math.modf(math.pi)
d$ S0 i7 x8 T - (0.14159265358979312, 3.0)6 A" B4 ?5 e/ V" R J
- >>> math.modf(12.34)
# {5 g; }5 O3 B3 o - (0.33999999999999986, 12.0)
复制代码
0 i5 S5 l6 G5 Z7 M5 z" C+ kmath.sqrt(x) 求x的平方根
3 Q. b7 f6 N: {/ H0 W3 q& l# M- #求x的平方根
4 t ]; a) U, a% Z2 R' {, m4 u - sqrt(x)+ L; ~0 p( `, _- ^5 ~: o& {
- Return the square root of x.
4 \: E. n/ O5 @2 q1 y - >>> math.sqrt(100)
* o2 ?9 g ?9 p9 K/ n$ M - 10.0* A& F' D9 T* T$ d9 W
- >>> math.sqrt(16)
- d3 [1 J+ R7 V7 U( i5 y$ h- S - 4.0
+ [* _0 _+ m! V; ~1 e$ R% ]) n+ y - >>> math.sqrt(20) R) V2 n! t) U( }' B& Q3 i" ]% j
- 4.47213595499958
复制代码 / @; ?, U& A: U( Q( P9 m
math.trunc(x) 返回x的整数部分- #返回x的整数部分2 p; }2 b: j; U" n& l: F/ T
- trunc(x:Real) -> Integral
7 F R+ \' h8 b- e- v; o4 p - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ b) [7 l3 \) m' b/ @" s$ {4 C
- >>> math.trunc(6.789)
9 R" `9 Z) T6 f: q - 66 H( m8 s5 b6 Z
- >>> math.trunc(math.pi)$ p) L+ Q& l( b) I- b* f
- 3; m8 |) }: G7 u K9 H
- >>> math.trunc(2.567)
6 M, o% y6 Z7 e5 `; D- v6 [# o - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|