马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 p. J5 o0 G h: ?0 ?8 ]( ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
8 L* f2 W% ^) ?! A5 ~" H! E" x3 b# g% t8 J/ |; B2 g% Z3 ?
方法1:( h& }7 }, T4 Z
- >>> import math
9 F) l1 f( H7 ?- ] - >>> math.sqrt(9)
\. V8 d- I0 e$ P - 3.0
复制代码 方法2:
8 S9 o* G ^2 m7 Y- >>> from math import sqrt
/ M0 r0 d/ W+ C5 Y5 [1 G" N - >>> sqrt(9)0 G* V' z8 p( Q) |0 d# J9 o& ?# x
- 3.0
复制代码
( q2 d. z5 T q4 @6 Z0 F, \ 8 c0 \; D: z, {5 g8 K, ? `5 b' L
math.e 表示一个常量
J6 f# b+ \ f- #表示一个常量, i0 X- a& _- E+ X
- >>> math.e1 {4 i' T0 ^5 y; y
- 2.718281828459045
复制代码
3 V( J1 @' G8 j* V2 H+ _math.pi 数字常量,圆周率 K! R3 u! J1 n+ k4 ~
- #数字常量,圆周率
% c* {3 V$ x6 b - >>> print(math.pi)
- l# n' L& Z4 g3 A* D# K6 `. I - 3.141592653589793
复制代码 . e7 ~% q8 d2 g- @* B9 h& G; L
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x) d3 C; q: ~6 p3 ^/ ]9 L
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
% ?. x0 [/ Z' e" n: |, ?# h2 p2 P$ ` - ceil(x)
: O" e; T4 {4 Y/ z2 e3 k - Return the ceiling of x as an int.3 s5 ^2 ~# G3 ~5 I {$ K
- This is the smallest integral value >= x.
+ X; U9 U) z1 F( W, E
* D$ P/ m5 x ?$ z; Y+ ]4 B; n# `- >>> math.ceil(4.01)
! ]1 A' Y4 n; M4 ^ - 5: G Q& t6 T3 x
- >>> math.ceil(4.99)
! o5 @# v( W1 ^$ s/ |5 s' _8 S - 5* w1 }3 b# T) E
- >>> math.ceil(-3.99)' x _- I4 \$ x( R0 V- L3 }
- -39 v! ^7 G, Y' x6 V
- >>> math.ceil(-3.01)
2 |7 b/ @% y! p - -3
复制代码 * e' m; H" S& X* f7 n8 }
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) X0 {+ R9 z+ i# e4 Z6 Z4 ?- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ M+ T, b/ b$ c/ U8 S - floor(x)# F* y" y, J) L
- Return the floor of x as an int.8 Y, J2 @! O1 y% k- @* r$ B) u/ X
- This is the largest integral value <= x.
7 p9 M- q+ `6 j$ J5 M - >>> math.floor(4.1)
0 z7 s) I$ c& u* j1 s# }" a - 47 ], Q* g) \. ~) o9 t: C
- >>> math.floor(4.999)
z" [9 ]3 k) C) J7 \ - 4
1 f; I! \- q/ n6 u4 Y" h0 x1 d: k z - >>> math.floor(-4.999). }7 n2 r% l# C, v# `4 z
- -5
. j( v4 i3 d h0 ^( V% R. p) P - >>> math.floor(-4.01)
2 z. C7 A: u1 S5 i. L+ R+ m' m - -5
复制代码
) X" H- K, m6 R8 v: H8 L0 T4 ?- imath.pow(x,y) 返回x的y次方,即x**y
3 L! N$ F" J9 x8 _1 W7 u- #返回x的y次方,即x**y
7 y# Z5 x' v8 l; ~9 `+ r+ X5 v& k. ~ - pow(x, y)
. v; s' k" P3 ~; _7 y. W - Return x**y (x to the power of y).
f. }+ u, T- j7 U, q) ` - >>> math.pow(3,4)! f7 d$ i& ]* I4 q; J1 N% o
- 81.0! U- P9 P9 I: I g8 v
- >>>
6 x% |2 S3 z% V - >>> math.pow(2,7). w5 O5 K- E' v" J- C
- 128.0
复制代码 : a! v/ w: n" |' h
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
; o2 B3 R6 b7 }) A- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
) L. }0 s, f @ - log(x[, base])
3 Z6 n, T( _. ?# a: G - Return the logarithm of x to the given base.
$ t& Y9 Z( _- E - If the base not specified, returns the natural logarithm (base e) of x.
9 M; o. m9 x8 C7 D5 M; Y - >>> math.log(10)
9 T, ~2 Q7 N9 f/ W/ m - 2.302585092994046
& ^1 K! O7 x7 @: X- {' m0 [, V - >>> math.log(11)
/ H; u! c5 `' B) y2 L, v - 2.39789527279837077 [) b* z o4 v8 z7 Q
- >>> math.log(20)" F5 |$ U* [ L9 G s( @ \! _
- 2.995732273553991
复制代码 9 r2 W1 D) w, R+ I( r9 ~2 Q
math.sin(x) 求x(x为弧度)的正弦值, o3 {4 N% Z h/ _4 A/ c4 H
- #求x(x为弧度)的正弦值
% r2 a5 L; R* T$ M* F6 ? - sin(x)
' t3 x$ I; a4 D1 G1 l4 b4 b - Return the sine of x (measured in radians).
: I4 M }, ?$ R' I( M1 ~ - >>> math.sin(math.pi/4)6 _9 a% S6 n2 b8 @9 I6 Y
- 0.7071067811865475
+ O7 N, k% a, {, d6 Z - >>> math.sin(math.pi/2)" Y' i0 l5 i& F6 N0 l
- 1.09 }* Y& Z! m3 F
- >>> math.sin(math.pi/3)
# ]* n; t/ B1 e& u- ]. J" t$ C" p! D - 0.8660254037844386
复制代码
/ r' O1 C+ |7 n' emath.cos(x) 求x的余弦,x必须是弧度
2 N' c4 b' w( ], \6 Q0 ?2 |- #求x的余弦,x必须是弧度
1 I* c& r' p j D6 j - cos(x)+ G7 l- u1 q1 m. w% C
- Return the cosine of x (measured in radians).
# w8 y+ X8 S5 Y4 D8 a& B - #math.pi/4表示弧度,转换成角度为45度6 o+ L7 f* |, o+ V1 T
- >>> math.cos(math.pi/4): T) M3 c' f! T5 e
- 0.7071067811865476" _# |1 }" w, b6 T7 `# _- P, K/ n
- math.pi/3表示弧度,转换成角度为60度
4 e- d0 Z/ n5 G0 e/ } - >>> math.cos(math.pi/3) v/ L4 L+ r) A/ N% b/ E1 _/ E
- 0.50000000000000019 z, ?% _# L# W7 }
- math.pi/6表示弧度,转换成角度为30度: c! H7 r1 P- e6 N1 ?+ d
- >>> math.cos(math.pi/6)
. F9 {5 h k( ?6 H& P$ Z% L( a - 0.8660254037844387
复制代码
' G& @6 t( ~3 _0 [math.tan(x) 返回x(x为弧度)的正切值
! i" ]- D8 B; k% T6 g4 S- #返回x(x为弧度)的正切值5 e z" q4 L$ p6 Y
- tan(x)9 K0 O& Y) A9 N
- Return the tangent of x (measured in radians).* w' c8 @& p3 r, {' f1 K: |. R
- >>> math.tan(math.pi/4); i4 |+ z0 H: v2 o+ V3 ?3 }
- 0.99999999999999991 m& N; N0 ^+ E8 e
- >>> math.tan(math.pi/6)" {6 s2 u4 i4 |- |- P0 @1 A3 V
- 0.5773502691896257
7 M+ a2 m* a& S: u' J5 ] - >>> math.tan(math.pi/3)- a" C6 B# t) T a
- 1.7320508075688767
复制代码
! [3 M1 G$ Y! Amath.degrees(x) 把x从弧度转换成角度1 }4 S Q3 |9 q# [8 D7 `- J) W# d
- #把x从弧度转换成角度+ w2 c- i6 X" R* }6 Y
- degrees(x)
& h ^# A S& o; b - Convert angle x from radians to degrees.3 h% R8 S* ^: A* p
, T( D' X, V& M! G/ j$ d C( }- >>> math.degrees(math.pi/4)
" L- t/ o% f! g4 }, d% C - 45.09 @( \5 E: f3 s( S
- >>> math.degrees(math.pi)
5 T; |" `% J) C: i7 S* ]) a6 q* g/ N - 180.0; _ J. O/ @- c ?5 P
- >>> math.degrees(math.pi/6)3 @/ J) z- r: `/ |- A/ q K
- 29.9999999999999966 W. @, V* b- N! B
- >>> math.degrees(math.pi/3)
; M1 [; Z. ~3 \ - 59.99999999999999
复制代码
& d: q) j% G; A2 smath.radians(x) 把角度x转换成弧度- Q/ d8 v7 f3 H: N, p
- #把角度x转换成弧度( P) I, s7 a- D' _8 K' Y( q
- radians(x)7 y' o0 v$ ~1 b& H' T7 W8 C
- Convert angle x from degrees to radians.
2 A7 X8 V3 I4 x+ Z0 |, f - >>> math.radians(45)
( G) P+ n8 K) s' U( O, Z - 0.7853981633974483& B: a5 }8 y! ^1 X3 I9 s
- >>> math.radians(60)
$ A: v+ e! _; s$ w - 1.0471975511965976
复制代码 + V! O% t; ?- F" G
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
. K1 t7 g U1 X- #把y的正负号加到x前面,可以使用0
& l# ~6 d* y5 Q9 L( c' g2 Q3 M - copysign(x, y)
6 I( Y+ O9 u2 W1 X; q' @ - Return a float with the magnitude (absolute value) of x but the sign ' y# W9 A( I5 x$ U9 A
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) . g: _% Q. `+ V4 [6 l2 f
- returns -1.0.
- {! E; K9 Q% i2 g h. J
& X+ Z6 B4 K3 f, W# @6 v- >>> math.copysign(2,3)
+ G) V/ l3 x% k1 R) p6 b. K1 } - 2.05 { Y, l( m% q4 z* Y
- >>> math.copysign(2,-3)
) _1 C+ D: }7 j2 w' R - -2.0! J9 }: f& d6 d1 D) S
- >>> math.copysign(3,8)1 n1 `# M7 E: H2 e! k( }) O2 D: K/ A
- 3.0( n! g% A2 Q6 L! n# P4 `
- >>> math.copysign(3,-8)
6 K/ l( i8 u) ]( A, k - -3.0
复制代码 # R+ k; K# z8 K9 C: q1 h" N, v
math.exp(x) 返回math.e,也就是2.71828的x次方
6 J8 d$ l( I3 v' R7 ~0 U0 J- #返回math.e,也就是2.71828的x次方
5 \ V' i+ T* n7 s - exp(x)4 l2 S( ^- }: R$ w, S: J9 o3 @
- Return e raised to the power of x.! m& [- @( Z }$ ?
- ! r- x. U5 @8 b! }! Y f0 S
- >>> math.exp(1). t! \% T: P9 u2 C
- 2.7182818284590458 z/ F6 N! X5 z0 L
- >>> math.exp(2)
/ }8 C: [9 X- h5 c - 7.38905609893065
9 y8 k* x% U0 }2 u! D& k3 x - >>> math.exp(3)
2 y, N$ m* |# R" a - 20.085536923187668
复制代码 ' z' s' ]/ X) p2 X+ R' ?. x
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
/ s0 T) r- R& k/ b; A* q- #返回math.e的x(其值为2.71828)次方的值减1" L1 U1 L$ [3 U! Y; \0 W7 o+ F
- expm1(x)- \# |7 x. H0 K% h. [
- Return exp(x)-1.
, X) y4 k! H" T0 b# P - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
7 k \, Y" ]3 V# k# P& a' U4 D
7 K5 p4 e: t* N6 I) {- >>> math.expm1(1)
( l8 N$ x7 L* m6 q - 1.718281828459045
, I0 l6 f8 H, q - >>> math.expm1(2): y3 s/ ~0 L8 V G
- 6.38905609893065
% y0 _. i2 q+ N" j - >>> math.expm1(3)! f m7 Z2 r( q" _' @/ @$ Y7 E
- 19.085536923187668
复制代码
% _$ G: Q- U1 ]* o+ V! C2 P' f0 smath.fabs(x) 返回x的绝对值
& \! x9 K/ @. y& X6 }- #返回x的绝对值& h6 n1 `3 V5 ~4 h0 K4 t" D6 s
- fabs(x)
5 @" W9 z0 [3 | B8 _ - Return the absolute value of the float x.% W1 G" o: b6 O% h( a/ e
- & ]9 g. O( C, ?, g. `% k% Q
- >>> math.fabs(-0.003)
( ~7 ~& L. P4 N0 l: B5 F1 P - 0.003) t. q3 O3 y" G1 j
- >>> math.fabs(-110)
9 m0 I f3 \; L: x - 110.00 W0 H @; \2 Y N; r" e, E
- >>> math.fabs(100)0 y/ l$ e9 y" t
- 100.0
复制代码
6 Z; u! h: }, j) u8 h$ U3 smath.factorial(x) 取x的阶乘的值+ R" z+ X: A4 D! n
- #取x的阶乘的值
J U3 E( Y/ R d; s* v" R6 L' M - factorial(x) -> Integral
. Z9 N9 P; `( z2 B/ b - Find x!. Raise a ValueError if x is negative or non-integral. l# D1 |) ]/ n0 W
- >>> math.factorial(1)
/ x% o0 i2 g/ ~' I7 h" a - 13 u3 j0 w7 T3 l6 {' a
- >>> math.factorial(2), Y; ]* q6 G0 ]' m6 H0 b: c
- 2: B! G. C9 [8 Y4 B b( U: r: c$ A4 p
- >>> math.factorial(3)
, }" \9 j$ T/ I9 C, ^2 T - 6
# y) w% j6 l# {. M - >>> math.factorial(5)
z5 O2 c( Q( g& q% H: X5 N - 120; C! P5 Q' F) J, ^
- >>> math.factorial(10)+ s# X; `6 O* L# y" @% r Q
- 3628800
复制代码
; ^" h ^2 e) t, cmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数. M" x/ U& q, \, ^: b3 u
- #得到x/y的余数,其值是一个浮点数
# L1 d0 K. m& k5 d2 W' @5 ` - fmod(x, y)% W: o9 h6 k% o( I' e
- Return fmod(x, y), according to platform C. x % y may differ.
: \7 \/ _# X! i; s+ V4 o - >>> math.fmod(20,3)! W q* y0 |, c* N1 v2 w# N3 n
- 2.0
0 {" L0 `& S+ U" t: }( z - >>> math.fmod(20,7)9 R' y) g, u+ b' F1 K0 e
- 6.0
复制代码
' r% x& ^" @' [math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! f; }- D6 q( z% s- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,6 H% ^! ~2 m+ C
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; _# q3 Z2 F4 ]* {5 J% p
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' Y0 p- k# J- j" q$ T
- frexp(x); _6 s4 n: V& b; u' Z- D2 M3 V$ `
- Return the mantissa and exponent of x, as pair (m, e).
' d# _/ W; Q3 A8 i- h - m is a float and e is an int, such that x = m * 2.**e./ R9 a5 m& n$ a1 {* |7 l
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.# J1 Z! p+ m2 F& l( m4 \
- >>> math.frexp(10) M4 s" _/ ?( J
- (0.625, 4) T: N/ D2 k5 _! B# e4 r
- >>> math.frexp(75)8 P' k. R) P1 I$ ^0 w
- (0.5859375, 7) g: e8 D: \: G2 @
- >>> math.frexp(-40)3 y4 \2 m l' N) y7 o
- (-0.625, 6)1 K' }, N$ j& \7 D* W9 r+ ^9 H. p
- >>> math.frexp(-100) ] L) H; m# t
- (-0.78125, 7)" l- n& h, n! D- t8 c; I, A/ _# q
- >>> math.frexp(100)
8 s/ X4 K: @4 ]3 Q" T8 ^6 ? - (0.78125, 7)
复制代码 : L' N% { z4 }' `3 k* p
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
$ B0 L3 D7 \" w- m- #对迭代器里的每个元素进行求和操作
% X" B! S; b& h" c6 Z& P# v# P - fsum(iterable)( M3 F( R X) a% _: F- g
- Return an accurate floating point sum of values in the iterable.
2 V$ H3 w: j' z r: ]- F( d& F) r - Assumes IEEE-754 floating point arithmetic.9 {- Y& E+ R H, H1 ], d
- >>> math.fsum([1,2,3,4])8 G( r$ ~( d/ [, i
- 10.0
8 H3 c' ~2 g( o2 ` - >>> math.fsum((1,2,3,4))
/ S6 h) Z c- c8 }* w - 10.0% [$ b3 S; {& X5 f
- >>> math.fsum((-1,-2,-3,-4))
+ o' m/ ~4 g! _6 L - -10.0" a" n5 Y% f. I% S) s
- >>> math.fsum([-1,-2,-3,-4])! z. @8 i2 Q! P% p
- -10.0
复制代码
9 c5 c/ V& h6 v2 |( _: Hmath.gcd(x,y) 返回x和y的最大公约数& d A% o3 q+ Q+ \1 z
- #返回x和y的最大公约数8 O# n- I; {. s8 P8 f! g1 {# s
- gcd(x, y) -> int
4 o X; T6 T& t# l# Z) R; a) r, z - greatest common divisor of x and y" ?5 U% l* r: K8 V/ W/ o. V8 }
- >>> math.gcd(8,6)
& c; U; H& F( r2 S: C - 2% @9 ^( L1 c, U" x" r- N
- >>> math.gcd(40,20)
- ?* |! a% O) X8 E6 i - 205 |( e* `1 n5 Z
- >>> math.gcd(8,12) P9 l9 ]/ O" ~( C5 u" H
- 4
复制代码
~% B2 n5 N, kmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
% @! x: K+ }3 V0 z5 h- #得到(x**2+y**2),平方的值
2 u. S7 O1 O" L: L, d - hypot(x, y)
. N$ g+ v* w; C- { - Return the Euclidean distance, sqrt(x*x + y*y).. P, m7 }0 }( z6 p1 P/ D
- >>> math.hypot(3,4)
' W0 o" w. F6 K6 w i - 5.0
+ W) J ]- l2 T/ y - >>> math.hypot(6,8)
9 S8 u, w! C* m, E- ^' K/ i - 10.0
复制代码 0 U! L2 [, c4 f( D
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
' D0 f" X. A+ b( W# w- #如果x是不是无穷大的数字,则返回True,否则返回False
@# M/ Z7 J1 h, H# j - isfinite(x) -> bool1 X/ u' U9 e2 O
- Return True if x is neither an infinity nor a NaN, and False otherwise.2 s! p; ?9 l8 z1 L# z3 S0 N
- >>> math.isfinite(100)) ?, Y' h# _( N# J( _3 y* _
- True
' j1 G$ e7 o* r6 g' Z5 ^8 s; g/ Y, A - >>> math.isfinite(0)3 Y2 l! _; H0 V
- True
* G; {" F$ N# Q% a0 r( D2 m - >>> math.isfinite(0.1); V. j+ ^5 X0 O$ u
- True) a; e: X4 W0 ]3 g& `. x
- >>> math.isfinite("a")
' d1 q( p* ?- @9 x$ E: i) q4 Z - >>> math.isfinite(0.0001)7 ~; V) M' A% p( V) I2 o3 ~6 K" Q
- True
复制代码 $ |& b! H/ \. n3 l9 u
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False4 m; f2 ?( ?4 C4 W$ ^
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
' B m+ Y/ U/ X# u - isinf(x) -> bool
% o8 i w2 e* \- l - Return True if x is a positive or negative infinity, and False otherwise.
. l4 d' t, l* t. q; D& S8 l - >>> math.isinf(234)
6 u: @$ n% ]5 ]4 @( n! { - False q8 j V: F+ k0 _. R" Q5 R
- >>> math.isinf(0.1)3 F# z1 ~% N B! J M' n
- False
复制代码 - {- D; y" }6 ]( Y. a3 ^
math.isnan(x) 如果x不是数字True,否则返回False
R" ] p C" N% o- #如果x不是数字True,否则返回False
* Z' @# i) n: m$ { - isnan(x) -> bool
. S7 `& p/ [; H3 D( v5 M - Return True if x is a NaN (not a number), and False otherwise.
% }) b7 V8 X0 F( l4 q( ?; U, x - >>> math.isnan(23)) R; ]; U: l4 S' [# `% y
- False
+ t% Z5 j/ V. ?' s) q+ |" f - >>> math.isnan(0.01)
+ Y* ?; T* i* u0 T - False
复制代码
( V+ b, C9 R8 j* i# O) Zmath.ldexp(x,i) 返回x*(2**i)的值
" @# O. V6 a: l2 I% h! k- #返回x*(2**i)的值# J3 Z1 Y/ b: E$ g, x0 c
- ldexp(x, i)
1 p8 b1 ^" j, X. |. D' o' i - Return x * (2**i).- l# _2 m7 v f K
- >>> math.ldexp(5,5)' Y2 ~3 |" h6 M
- 160.03 p e, L! {# q
- >>> math.ldexp(3,5)
9 q( Y/ x! o5 [ - 96.0
复制代码 ' j7 K+ q1 u) h" f. ^, e( r* M* S& o
math.log10(x) 返回x的以10为底的对数& V d# ~- v4 d% n% h; S# [
- #返回x的以10为底的对数
1 k3 A: t: D" S, ~+ a& H/ J: B8 q3 [ - log10(x)3 a5 b. c7 t6 l
- Return the base 10 logarithm of x.5 g& _! t/ a Y- f; F$ Y( ?, X* H9 C
- >>> math.log10(10)
8 p, P: {1 M1 w8 g& j9 s2 m - 1.0
% h: B# b8 z: G( r - >>> math.log10(100)+ @6 q7 A. d/ J, V& {9 K
- 2.02 Q$ @8 w! G4 q" R; t: C3 X( ?
- #即10的1.3次方的结果为20
* C) ~3 m) V. H7 T* |" H5 v - >>> math.log10(20)
2 ^" F2 ^- e. z0 _- C - 1.3010299956639813
复制代码 & F( c: Q# S0 H' |& v' \7 m
math.log1p(x) 返回x+1的自然对数(基数为e)的值; W! V! i# A% e/ e
- #返回x+1的自然对数(基数为e)的值% d/ y% z" t, n" [+ Y0 I5 c# K* K. h
- log1p(x)
A- h, J$ D& c3 ]7 ?6 @, i - Return the natural logarithm of 1+x (base e).
9 C/ L% N3 X8 N A$ p$ R9 l - The result is computed in a way which is accurate for x near zero.2 W8 ^- c9 ~& n
- >>> math.log(10)2 N: F5 l; e0 m
- 2.302585092994046
2 L3 S s- a& P. b/ y+ X - >>> math.log1p(10)
5 t# x' }7 M( S( I* H$ y1 X - 2.3978952727983707
0 g5 m+ M4 E" ^. L' O# i - >>> math.log(11)/ [8 w7 t9 {) b; p2 x
- 2.3978952727983707
复制代码 : n6 S3 Z2 U( F3 a! C+ H G
math.log2(x) 返回x的基2对数
T% w2 h( D% f0 r) H6 k- #返回x的基2对数
9 k' S: _& L- x& i* P3 d% r. F - log2(x)
" X- x; T. t! |5 T# G - Return the base 2 logarithm of x." b+ U4 a+ X& |' x5 M) Z' R
- >>> math.log2(32)
0 T+ J8 V1 _$ b _" S. r( Z - 5.0
: A; m3 k! H6 N6 j5 ^7 O - >>> math.log2(20); m; a: b$ m2 P1 O
- 4.321928094887363! ~' w% c' R A K: A F9 K
- >>> math.log2(16)6 T t+ F; G0 B: C { o; t
- 4.0
复制代码 ) O% r- U: J7 e- U( Z, R b; S4 h
math.modf(x) 返回由x的小数部分和整数部分组成的元组# u, p2 _' X' u$ ~* `
- #返回由x的小数部分和整数部分组成的元组9 X4 A' b+ K& Q/ R. @/ | C# M
- modf(x)6 W6 K, K0 E& x
- Return the fractional and integer parts of x. Both results carry the sign
1 |2 B* v; W1 c' n - of x and are floats.
9 m2 t$ D/ L0 a! `; J( @0 V4 Z - >>> math.modf(math.pi)/ `" z4 {6 ]9 |3 ?! v
- (0.14159265358979312, 3.0)- ?6 h+ q+ f/ l
- >>> math.modf(12.34)
% y* f! H& E0 R, Z - (0.33999999999999986, 12.0)
复制代码
$ J( G3 j4 o5 Mmath.sqrt(x) 求x的平方根
4 o' O7 D+ U/ B9 T K5 A7 s1 @7 |- #求x的平方根
3 z' {, C3 V- U" J- R& @) j - sqrt(x)$ L. ?; a( W' t, N. [/ f
- Return the square root of x.
- b0 Q6 V, ?& U6 b' u* M* Q: Z& s - >>> math.sqrt(100)( J# E: }# M |! R/ ]
- 10.0, m* V0 ?; ~4 ?* q4 d& @! b. S
- >>> math.sqrt(16)
3 u w' Z! G7 L, Y8 } - 4.0
" R) B5 n0 N$ ~# @2 @2 R - >>> math.sqrt(20)$ l6 Y/ S/ ` U' n" F
- 4.47213595499958
复制代码
# k. g# d r( p1 k lmath.trunc(x) 返回x的整数部分- #返回x的整数部分
9 H4 l6 l m. y1 b - trunc(x:Real) -> Integral
: N. x. ~9 Q4 W6 _4 F: L* |7 W. x( o4 l - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.7 f7 y% l$ D5 Y5 j6 {9 u; t
- >>> math.trunc(6.789)4 A3 q0 J- ]1 Z% G( Z- L
- 68 u* |" B$ L6 U1 d% z
- >>> math.trunc(math.pi)
% r: C" p: z$ S! P( W" h! V - 3
8 ^( R% H; L' i+ S% b( f - >>> math.trunc(2.567)
t* w( e$ V* b7 ` - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|