马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ {# X3 {2 L6 g& T# G9 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
3 B! F2 {" g" F. g, l2 d4 ]$ Y" g* Y, E! |) W7 @5 k5 q7 Y
方法1:
% Q. r4 U3 W) @; E: V- >>> import math A6 L6 s% Z! ]1 r3 Q
- >>> math.sqrt(9)
( w/ @! R6 G* h - 3.0
复制代码 方法2:3 ]! q8 L* [ q1 H
- >>> from math import sqrt
* T4 [% Y/ ^" _( ?/ M - >>> sqrt(9)
" c3 L/ V! v. l6 Z - 3.0
复制代码
2 o) k& C, j3 ]4 }9 s8 b# D , S _+ G! |" ?4 G, ?7 n3 c( q
math.e 表示一个常量: k6 }, R; `6 I: v4 H
- #表示一个常量4 g. J* F2 u4 \1 Z) \
- >>> math.e1 C( e, I& x( K7 D: S0 p
- 2.718281828459045
复制代码
. ?% t( n: J8 a k+ W3 imath.pi 数字常量,圆周率
& s! z0 _4 _2 V$ @- #数字常量,圆周率
2 D! O% T- ]3 W, m2 b) K - >>> print(math.pi)
5 Z' J0 f- n' _3 [8 b5 I - 3.141592653589793
复制代码 & ?- b3 u- D7 _! ]; u( _0 |
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
L% [- {! N" }1 ^* d: e: }- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
7 {0 M _. D; a3 V! K; \ - ceil(x)
* N- `- Y8 b8 U; j+ |8 Y- i - Return the ceiling of x as an int.( W. ^6 I, E1 \9 I2 P3 S
- This is the smallest integral value >= x.( U8 H2 ^& h' s% v
- ( l$ `/ p* C' G! c
- >>> math.ceil(4.01), G9 ?1 J# t+ E
- 5
7 `6 D4 o3 N1 _7 e* t( M5 L+ p; a4 X - >>> math.ceil(4.99)/ |/ Z0 I+ Z0 x7 R; v3 W, c3 w
- 5; S5 J; z5 @ S# X/ X2 o+ E6 `" e) }
- >>> math.ceil(-3.99)
& w! s. U$ h2 u# [/ ` b - -3
! y `% n9 T! V( j j - >>> math.ceil(-3.01)4 V; c. b1 Q' h6 I+ K, G
- -3
复制代码 6 B1 f9 Q. E3 F0 b* L' I# T
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身; i" F; Y0 M0 w( D8 n0 {" J& s$ r
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: a1 L. ^- ~' t) M - floor(x)- D( g5 O7 @6 N, a
- Return the floor of x as an int.
3 C( w5 A& x& H2 [" V$ A* @, z+ N - This is the largest integral value <= x.
M+ ~. S+ |/ I2 Z% Y% P6 s - >>> math.floor(4.1)1 H: X; ?4 h: B8 `2 `' x
- 41 l& {# s& n& s* |
- >>> math.floor(4.999)3 f2 }# N+ H6 \4 m& Y
- 4, y2 F3 k8 x) f
- >>> math.floor(-4.999)) v" D7 R* U i1 t' s
- -5
' l' t2 ^4 E( r S. h* Z+ P) \ - >>> math.floor(-4.01)
) f: U! j# S2 E9 z' X - -5
复制代码
- T! A/ ?: L+ { \4 l$ gmath.pow(x,y) 返回x的y次方,即x**y* ^1 n c6 y- \: _+ Z# p; c
- #返回x的y次方,即x**y# r1 j4 F9 y5 s" b2 l
- pow(x, y)
2 D( W# I/ a6 I& E - Return x**y (x to the power of y).! N; l+ `; B$ F
- >>> math.pow(3,4)6 A1 `; D, m8 l0 }1 S" F
- 81.0( T& D; b5 v' ^% i/ b, |: F& \
- >>> ; L6 F6 k7 _" m6 a: v. W
- >>> math.pow(2,7)/ b. X" L4 G9 j& {* h# H
- 128.0
复制代码 5 _( A4 L2 t9 F4 [
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& p0 Q# [8 D2 N( M- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
- e% \( \5 P& d- R v - log(x[, base])
, Z9 p6 [+ |3 { - Return the logarithm of x to the given base.. d, D% e' E* }# E& _/ O
- If the base not specified, returns the natural logarithm (base e) of x.
4 J, N; i9 U5 t. J5 X - >>> math.log(10)
6 i g1 ~- p2 G% X" P, i6 C# G - 2.302585092994046
: V0 W2 C, b) O$ S - >>> math.log(11)
! Y3 p, y9 O$ s - 2.39789527279837075 E9 E* d/ M, A' s/ k2 v( g* _
- >>> math.log(20)
. q, g. y5 {) E - 2.995732273553991
复制代码 9 Y, ]: W4 Z/ f$ ~ i4 A0 ], d
math.sin(x) 求x(x为弧度)的正弦值
2 v/ p: W1 z+ Z4 g. F+ t# [# C \6 t) L- #求x(x为弧度)的正弦值' R0 k; P' W5 f: t4 t c/ j7 l/ z
- sin(x)2 R7 z( L( u/ D; ]6 C4 t
- Return the sine of x (measured in radians).6 l* k3 \) v7 E' g1 u) f2 @
- >>> math.sin(math.pi/4)
% ?; m" J8 E8 ~ - 0.7071067811865475; C9 h3 F: ]: g: u/ F9 l+ D
- >>> math.sin(math.pi/2)
* c- f7 `% y% n2 t2 D0 U& S7 O - 1.0- r& |- C* N! b: r8 C0 N2 P" x
- >>> math.sin(math.pi/3)1 N5 K% J$ `0 R7 s( }+ {
- 0.8660254037844386
复制代码 6 h# w# w- y, ]) C6 ?5 ~
math.cos(x) 求x的余弦,x必须是弧度( x5 S% {" P) o; C$ ^ s5 c
- #求x的余弦,x必须是弧度
! |1 g9 \: w1 `) c4 }% G - cos(x)
: x$ \, G! R: v7 y7 t8 ~8 ? - Return the cosine of x (measured in radians).
1 j2 i: w- K( l) e. M5 P) ? - #math.pi/4表示弧度,转换成角度为45度
6 I% z3 S! [$ t - >>> math.cos(math.pi/4)
2 w7 ?, ~! V/ W( F5 Y& K - 0.70710678118654769 |/ v( v! q+ l1 o: @" m0 b2 k
- math.pi/3表示弧度,转换成角度为60度- f2 R/ N5 T5 p% T$ X& }$ z
- >>> math.cos(math.pi/3)' l0 r& k1 l6 J
- 0.5000000000000001
8 i0 L) E5 t% ?/ \: G6 ` { - math.pi/6表示弧度,转换成角度为30度
- M/ J& |7 Z- `% ]/ j - >>> math.cos(math.pi/6)
# j1 a E6 U. O - 0.8660254037844387
复制代码
# Y1 O f; K# Vmath.tan(x) 返回x(x为弧度)的正切值
d2 K% u- j! v3 u- #返回x(x为弧度)的正切值
9 V! C, \% j' T% f! M - tan(x)! g& W! z" w0 T
- Return the tangent of x (measured in radians).) T* W! E" v0 r6 r" ]8 w
- >>> math.tan(math.pi/4)) P; [3 D5 H5 y4 f( I7 |
- 0.9999999999999999
3 J9 @( q; V/ o - >>> math.tan(math.pi/6)% F& s6 |" A) `
- 0.5773502691896257
) Z; V( H/ C' }7 Y) _: J - >>> math.tan(math.pi/3); U7 q& W" v; F7 L, ] L- p
- 1.7320508075688767
复制代码 ! K; q( z& v4 p" c: W' O0 q0 C: h7 t
math.degrees(x) 把x从弧度转换成角度) s- Q9 S5 g5 m$ M+ g4 i
- #把x从弧度转换成角度, j; X! F2 s8 o
- degrees(x); Q) e1 i. U# A. Z
- Convert angle x from radians to degrees.
* o1 ?! o D( Q. l6 K! M- K - ) Q3 E0 o3 g0 f- H" Q2 M H
- >>> math.degrees(math.pi/4)
+ B4 y0 d5 Y* J$ D+ w0 k! c9 N% o' h - 45.0
) }! E% \: y' D) [ - >>> math.degrees(math.pi)
. n# y5 L) P7 r, \ - 180.04 ^1 U5 g& }# p5 d
- >>> math.degrees(math.pi/6)4 E2 ?5 `1 k, T! g/ P% U- `
- 29.999999999999996
3 N% b) b* o6 k8 P& u7 ? - >>> math.degrees(math.pi/3)0 s8 {, t$ p" w2 q$ Y3 b3 J* b- w
- 59.99999999999999
复制代码
* o0 f' Z" |9 j1 Q, K1 \' T+ Kmath.radians(x) 把角度x转换成弧度/ Q$ r8 D$ f8 v! O8 i: z
- #把角度x转换成弧度
4 Z" s- S& K4 N. R4 G* N/ w& g - radians(x)
* q' g5 x. ]( S - Convert angle x from degrees to radians.# |5 ]2 ]' P# ?& T
- >>> math.radians(45)
" y$ B8 Y8 Q3 O2 F- L - 0.7853981633974483( u! F/ i6 ~4 z0 B% S# E7 E
- >>> math.radians(60)
6 P, P" t; @; I! i/ u3 U - 1.0471975511965976
复制代码 . _5 j. \, i8 J3 b
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
8 m/ P, j9 f1 U) u) D- \- #把y的正负号加到x前面,可以使用0
8 e! V! k( P% p3 R( | - copysign(x, y)1 T9 f! }" u0 Y+ q% t. [
- Return a float with the magnitude (absolute value) of x but the sign Q/ e6 p8 X& g# V( K& {- I/ ^! p
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 5 G3 X; a$ e4 r9 @% ?. H
- returns -1.0.
! f5 k; n9 Q, r' D5 q3 ~ - * T4 R4 m$ K( n% M" K7 ~0 P, o% \
- >>> math.copysign(2,3)
+ w8 m, e2 ^' W+ s( k$ M' q, ^ - 2.0
% _% Q1 l) I2 C" ` - >>> math.copysign(2,-3)
1 l0 n; Q8 |) P0 l( u% R# E- F - -2.0$ N) s2 g) Y8 X6 X
- >>> math.copysign(3,8)8 n2 H* p! G+ L5 h
- 3.0 t" O: b3 X2 v; W, v+ X
- >>> math.copysign(3,-8)+ @" d c7 g B/ U
- -3.0
复制代码
; S S3 {7 }+ smath.exp(x) 返回math.e,也就是2.71828的x次方% u3 d0 @, X( _) s& }
- #返回math.e,也就是2.71828的x次方
- }0 R R3 W* C: h# |- e* j2 G3 ? - exp(x)
/ X( g( {# z3 z& g/ I) B- @" G: A/ w - Return e raised to the power of x.
4 C' U$ n* A" E4 a# R
. z/ h+ S9 V9 |' k- >>> math.exp(1)0 ~, s* Q7 A: Z6 T! N- J7 P
- 2.718281828459045
8 z3 _$ f2 e( Z M8 |) a/ l - >>> math.exp(2)
! j8 N1 I7 J4 W) d - 7.38905609893065& H! a, {0 G+ @/ _! P; h/ D9 W. X7 _9 ^
- >>> math.exp(3): y! j" X" C( A! _
- 20.085536923187668
复制代码
- U" Y" G& P8 zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
7 h3 ~- k# }- g B! E- #返回math.e的x(其值为2.71828)次方的值减1
3 n" Z( p/ v! X% Y! \- l3 f - expm1(x)
4 m+ U: M X8 H8 w - Return exp(x)-1.
: V& Z6 j0 B3 }5 h - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
+ x. l# l. \4 I3 n! r) Z
3 x; w0 z2 H" ?$ y5 H% x# j- >>> math.expm1(1)7 S) q/ }# S& D+ P. k! f$ T, p
- 1.718281828459045# G q8 f8 p8 A! ~3 J0 N
- >>> math.expm1(2)( g' a5 x, v/ H0 }. B/ Y: C/ |
- 6.38905609893065
9 ]/ M; Y$ B4 b- L5 r - >>> math.expm1(3)0 a+ Z1 _6 J1 h9 d1 h
- 19.085536923187668
复制代码
' g% K% u4 K9 Q5 U$ F- z6 Q/ a5 C. N+ ]math.fabs(x) 返回x的绝对值
' B+ a9 |9 L% w4 ]+ {3 o6 C( f' P- #返回x的绝对值
/ L5 |. |1 E9 T( K2 M4 S. } - fabs(x)& ~9 j4 `# ? d3 s5 V# }, \
- Return the absolute value of the float x.
# Y w/ z# ~/ J# o, B n: r V2 O - ( G' L3 x7 G# j6 N3 ^3 d
- >>> math.fabs(-0.003), }" b5 o% V% L1 ^& P0 ^" y
- 0.003
) F1 [3 A2 v' _& e: p - >>> math.fabs(-110)
# c& d [: B' K" d/ t+ j7 W0 ^ - 110.0
+ O! J t; ~$ w: h4 u8 p - >>> math.fabs(100)
/ f( ?5 h1 x1 `0 Q6 y3 |: Y - 100.0
复制代码
' y+ _0 f# ]! Y' b. |# g' cmath.factorial(x) 取x的阶乘的值
+ h' c' C$ V* Q8 ?' U0 P$ f- #取x的阶乘的值
( u+ e9 d3 ]. [* W3 u7 Y - factorial(x) -> Integral% U, i! U: e+ X Y+ ]
- Find x!. Raise a ValueError if x is negative or non-integral.% T( r8 q& m. K/ t6 t/ f
- >>> math.factorial(1)
' X0 c; O6 v, g. t - 1
/ y5 B& X- }, O' S6 u0 B - >>> math.factorial(2)
# b, `! l. w4 q6 Q - 2
$ D6 H* o$ r- {% c$ \9 o7 X - >>> math.factorial(3)1 r' [- c" J5 w% i" J* s+ ~ V
- 6
2 C8 E; O- y$ O+ Y" ? A# x& c/ ] - >>> math.factorial(5)
: X- r) l, }% H/ A' k - 120 w( Q3 x: H$ {
- >>> math.factorial(10)$ ~4 g( e; H- S/ U c9 F& n
- 3628800
复制代码 $ [: c5 _# {, r+ [ O
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数 I. T# ?8 m/ ~! r
- #得到x/y的余数,其值是一个浮点数
7 D1 d: S2 P, J" p! o+ } - fmod(x, y)
4 P8 d s7 p1 q8 s. s2 j; J6 Y - Return fmod(x, y), according to platform C. x % y may differ. r. n \- W! H
- >>> math.fmod(20,3)( n! N* V; S" E- X1 A
- 2.0
8 a. ?# t$ t$ \0 h& w - >>> math.fmod(20,7): |% k# H% U; b5 p4 t: u
- 6.0
复制代码 & @/ d: ~. L* G: Z$ A' I* ?
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ A$ l1 c& w, T; n, d6 Q) t$ M% J3 _- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 W! f/ Z1 ~# {/ W: C$ F/ Q4 T& S - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值& _2 z' q5 B& {8 N) M9 k* D
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1 a2 R( W# I0 h; M( c
- frexp(x)1 }( \0 |5 u; `% T2 ]& F
- Return the mantissa and exponent of x, as pair (m, e)." Q! \8 x( W1 P# g+ E% G
- m is a float and e is an int, such that x = m * 2.**e.! ~$ z3 h3 Q8 ~5 o; F7 w
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.8 K# L; B9 k) l# T. c" P
- >>> math.frexp(10)+ Q! z5 z* t2 E. z
- (0.625, 4)
0 z' o9 c9 ^! O7 f9 N% C - >>> math.frexp(75)
/ Z2 f/ x _$ q/ d( W' i5 Y - (0.5859375, 7)
+ ~) u: z- f% u- D- ]- k( K6 T - >>> math.frexp(-40)
P* H( L4 [" A& |8 G/ \, s- Y4 ]- z - (-0.625, 6)
0 p; g! [) ?4 k8 ~$ U" ?: M - >>> math.frexp(-100)
4 G$ Q1 h; W g% Z b0 ~% y - (-0.78125, 7)
- \. i3 f$ N; M& a8 M' _" o( K - >>> math.frexp(100)
* S6 y, S7 W# }3 P/ A - (0.78125, 7)
复制代码
) ~/ ^5 n5 q9 H0 `2 t( s& u$ Q0 h* ?math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)- V4 ?7 g- O# T0 R b3 H n& N# e
- #对迭代器里的每个元素进行求和操作' A0 x1 F( ~# H& ?: W
- fsum(iterable)8 ?: A: a- b# q( ~& r! {, o
- Return an accurate floating point sum of values in the iterable.
- B$ n' R! Y' u! y9 S" r - Assumes IEEE-754 floating point arithmetic.; k; x5 q" W X
- >>> math.fsum([1,2,3,4])9 ?& k. d. c; n2 h$ \# A' c
- 10.0, k+ w6 E/ a- f5 C% Q1 W# y% H/ Q! C7 h, d
- >>> math.fsum((1,2,3,4))) q+ G3 i+ k: v4 @7 f! D
- 10.0* O1 m- v6 p6 }' C+ L
- >>> math.fsum((-1,-2,-3,-4))% p+ _; n* W: [1 D: D9 d3 n) e; o1 G
- -10.0
6 ?4 J2 z( a6 \4 t+ u: }9 C - >>> math.fsum([-1,-2,-3,-4]), d0 _7 m# s, y, J
- -10.0
复制代码
; f9 ]/ Z7 U4 j) H0 M# Zmath.gcd(x,y) 返回x和y的最大公约数
# A' S( m& i; t3 `6 T5 X( d- #返回x和y的最大公约数
, R: K8 W& J( A* ?. m% W6 g# `8 V - gcd(x, y) -> int( \& O5 U2 m, u6 x' [2 a4 N
- greatest common divisor of x and y
8 P; m1 P9 L% P) | - >>> math.gcd(8,6)
- W3 U2 _0 m/ A& _/ U, ~ - 2
% J @/ p; E. R7 E6 _( d8 P8 g0 q - >>> math.gcd(40,20)
" X0 w0 R$ e! l4 G - 20
* l; {! w1 _6 ]5 T3 P6 t# E - >>> math.gcd(8,12)
* M0 T7 L8 m0 t5 `- y+ G. h3 `* | - 4
复制代码 1 Z6 t0 ~5 y$ w0 k
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False& }- \" H6 ?! @/ j) u% M4 S' Z% A
- #得到(x**2+y**2),平方的值, F) s; Y& A* m5 Z5 j
- hypot(x, y)$ g4 q" \3 @/ b3 ]. S, w
- Return the Euclidean distance, sqrt(x*x + y*y).# _! ?1 K. k4 M
- >>> math.hypot(3,4)+ F, E( N e1 o C7 x
- 5.0
+ l# p1 t. K" m: h7 z! N9 g- p - >>> math.hypot(6,8)
|$ k/ i+ a7 \# M$ ]1 n - 10.0
复制代码
3 ^3 F5 y; [: m3 Amath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False& O, ?' i" d& Y0 B$ ~7 N
- #如果x是不是无穷大的数字,则返回True,否则返回False2 V: W, ~! M6 j
- isfinite(x) -> bool& e% a0 L/ \& |: A
- Return True if x is neither an infinity nor a NaN, and False otherwise./ E! A) l0 `+ V! N1 H: m
- >>> math.isfinite(100)3 w4 y; |) g( T1 c* @9 Q
- True( k! n" g" |. f( p0 s* O* I0 p
- >>> math.isfinite(0)
# I9 _1 w' Q1 x; K9 {0 P4 m - True$ e& u1 @7 s- \6 k8 \$ x, n% L
- >>> math.isfinite(0.1)
$ u! c, j1 n3 W8 m! }4 a - True
3 [$ @+ p7 C. T+ c: t2 W - >>> math.isfinite("a")
# o; u- a8 i1 U) n* O* ~0 m - >>> math.isfinite(0.0001)9 l% A# r$ M X9 I* h) A
- True
复制代码 * d5 |6 E7 b8 Y1 n
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
$ X, `6 E& J. s( `1 d) b- #如果x是正无穷大或负无穷大,则返回True,否则返回False
/ c& i2 H0 I# i4 }: \ - isinf(x) -> bool5 \! v# s- j' I
- Return True if x is a positive or negative infinity, and False otherwise.9 F( {8 V. X2 m; v9 t7 S
- >>> math.isinf(234)1 p* R8 H/ Q1 f: q& [, J/ a
- False
S" U% i, Z# s; N" D# J+ |' { - >>> math.isinf(0.1)
9 h- T$ Y4 f. r- {( s4 Q - False
复制代码
# t1 G2 T! b5 [, L; m* ^5 smath.isnan(x) 如果x不是数字True,否则返回False3 ?4 O* A& r7 e! p, o5 R* [, m( U6 E
- #如果x不是数字True,否则返回False, Y" v" Q0 M! o M5 q+ G' h1 m( p
- isnan(x) -> bool
" `, X3 r9 d: j* P - Return True if x is a NaN (not a number), and False otherwise.6 A- o& _$ `, E) @
- >>> math.isnan(23)
( n4 |9 M+ W3 @ - False
; S: f8 Y+ j/ T+ ] - >>> math.isnan(0.01)9 k G# J# H6 k% @$ W5 y( V9 F- T0 F
- False
复制代码
6 `, K+ E# b3 D8 H) @- }- g- jmath.ldexp(x,i) 返回x*(2**i)的值/ s6 w! W7 \4 o+ W
- #返回x*(2**i)的值0 M' @. }4 g; B: _3 k5 z9 U% O
- ldexp(x, i)
/ ~/ `' [* J1 E9 U/ W - Return x * (2**i).' [6 @8 K u+ |/ O4 E7 P% h
- >>> math.ldexp(5,5)
& o# `1 n* Z5 \8 G - 160.0
2 A3 a+ S: C# ?1 f - >>> math.ldexp(3,5)& \3 o2 V8 E: c8 j& [( M w
- 96.0
复制代码
+ i" D3 D o; \7 B. D$ p( Z2 E/ `math.log10(x) 返回x的以10为底的对数
: h% B1 l( u& }4 H6 j2 O/ N- #返回x的以10为底的对数: N2 P) v( ^! L& O# x( B
- log10(x)" i3 B& i* X3 e c4 S
- Return the base 10 logarithm of x.# f" R$ I. O2 U& X% Y) d
- >>> math.log10(10)* i% ]: @! M8 E/ v5 W& B- P
- 1.0
: M1 j% s( Y1 { - >>> math.log10(100)
' m8 o2 Y U/ c - 2.0
5 Y2 X1 e- }. e! E - #即10的1.3次方的结果为20
4 i( i/ Q z) m( a1 j5 i, U - >>> math.log10(20)
7 c$ Q7 A$ `3 D3 Z- j$ y* O: B - 1.3010299956639813
复制代码 # i; z5 O# s% K5 M
math.log1p(x) 返回x+1的自然对数(基数为e)的值
7 L: L# a. l. j, y" J: L& H |- #返回x+1的自然对数(基数为e)的值3 Q& {$ R, [+ J0 v" f1 I' _
- log1p(x)
% @# N% X6 l: G! t - Return the natural logarithm of 1+x (base e).- e* B4 E: [: |# ?( ]/ u9 B
- The result is computed in a way which is accurate for x near zero.
* t* Y0 F5 I8 V+ @7 h$ h* u - >>> math.log(10)
1 e3 M$ V5 [2 ^! I; H @ - 2.302585092994046
* @1 h0 g6 F. ~4 V - >>> math.log1p(10)
. v( M. L6 p$ w$ _$ y5 U. b5 h9 P - 2.3978952727983707
6 i& }8 D% P' @' v$ q2 v# o - >>> math.log(11)
& D; G- ]1 y, @' X v; [ - 2.3978952727983707
复制代码
: x0 \1 Q+ O+ ]0 R8 x6 | Fmath.log2(x) 返回x的基2对数 x% X/ M9 [$ ^* V W
- #返回x的基2对数
2 b. P+ r) @9 s+ X6 v. Q+ z - log2(x)
' k) d8 T' y2 W* o - Return the base 2 logarithm of x.
2 K2 j; J2 O* ~ - >>> math.log2(32). v* Z( o! a; u+ y
- 5.0& }# S9 l% ]- A
- >>> math.log2(20)
' z! j- a+ H4 H8 p$ Q: D - 4.321928094887363
+ Z3 \. |9 p- `# k6 o: b. U - >>> math.log2(16)
( r4 e7 q: F: ~* h& A2 m - 4.0
复制代码 4 P1 j" u( z" S, i
math.modf(x) 返回由x的小数部分和整数部分组成的元组2 E( ^" m4 g9 y5 U2 _. z
- #返回由x的小数部分和整数部分组成的元组; K, M7 e: x! G$ w2 U
- modf(x)! E8 X6 m2 b5 z( g, K3 B
- Return the fractional and integer parts of x. Both results carry the sign
' `- Q! i! g" V' v* u - of x and are floats.2 X, b0 i# {8 m
- >>> math.modf(math.pi)( m7 j& t1 p) x2 Y
- (0.14159265358979312, 3.0)
) v4 k L C9 q# q& b" f1 _% Q - >>> math.modf(12.34)4 ^- x4 Q6 u$ r4 `
- (0.33999999999999986, 12.0)
复制代码
2 ~+ B$ O" n: E8 i% gmath.sqrt(x) 求x的平方根
5 \4 C# o" `7 q. t0 Y$ t6 [- #求x的平方根* i! d5 l V+ y- V( Y: D
- sqrt(x)3 K; H" {+ ~8 @$ C
- Return the square root of x.
2 a/ l7 k& s6 r; T - >>> math.sqrt(100)5 w' M1 @) c$ Q- ]. R, D( }
- 10.02 e$ f2 `" P) u
- >>> math.sqrt(16)5 ^" |# u4 c) X$ P+ ]# s' z
- 4.0 H( o; H7 Z# O4 w T
- >>> math.sqrt(20)# \9 L( [3 x* Q" U" f8 n- P
- 4.47213595499958
复制代码 7 c+ j: U9 W$ G) a6 \; `+ W
math.trunc(x) 返回x的整数部分- #返回x的整数部分
0 j5 u4 r$ ^8 @: L. c# c - trunc(x:Real) -> Integral/ n3 {' X. D, P9 @: |
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 k% _/ ^4 b5 a. s2 r( Z$ O( X
- >>> math.trunc(6.789)
8 e5 c( z& }# E# n% ^/ `& s - 6
) j! M2 o9 Z+ Q. G - >>> math.trunc(math.pi)8 K+ \% [% h' z/ e
- 3
4 n& E# c5 N, [& o+ i& N - >>> math.trunc(2.567)
- G2 M* F0 M2 i4 e7 g - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|