马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
' K5 w L7 P7 j9 l6 @6 g: M$ V( Q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。( \" }9 G w4 u
+ ]3 r" n+ |7 k. z9 G/ K- d
方法1:2 A6 t) Y$ V4 b) ?. u q
- >>> import math
7 g' G+ h B8 W A3 m! |" O" V - >>> math.sqrt(9)
# c$ D0 u7 q `& ] - 3.0
复制代码 方法2:
1 `$ W) G, g" H2 z" {2 X! [4 P- >>> from math import sqrt* v% q, |: n; L' [1 A; ?5 k
- >>> sqrt(9)8 }$ I' ~8 B4 v" f b0 R" x
- 3.0
复制代码
3 K" k; B5 X. H8 n n
$ d: p, g) Y" k# imath.e 表示一个常量
- E! R! U3 x, u3 H2 J- #表示一个常量
5 Q' T2 h) \5 {+ y( ^ - >>> math.e
% K& L8 f6 K) j - 2.718281828459045
复制代码
4 A# n3 h2 T' C. Emath.pi 数字常量,圆周率
/ M# P2 y' T% U* K7 ]7 f" p! ^1 H- #数字常量,圆周率% J; E l3 s |$ \: W
- >>> print(math.pi)0 g1 |; s, T2 F& R; V: z+ k+ U0 j
- 3.141592653589793
复制代码
* m) F5 E. ]) U2 O: J& Hmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 N, k* j& U! Q2 o. l; h) T& ^
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x. A h1 k6 ^# z F& q: B
- ceil(x)3 V; K& W2 d# C: J E. G: v9 ]
- Return the ceiling of x as an int./ Y7 Y6 r' o' H, x; {0 u, Y& N; y
- This is the smallest integral value >= x.
2 G; f5 L% E' m( J& z: |
; p2 y& s: }! I: Q- >>> math.ceil(4.01)) F% _+ `2 U0 A( ?8 B- m" j
- 55 y0 _2 N$ S- E0 |1 z4 u+ r
- >>> math.ceil(4.99)+ J4 {% @$ s8 g0 {8 x- T+ R+ ?
- 5/ e5 r8 p9 Q# T7 A5 c
- >>> math.ceil(-3.99)4 }, o. v f8 c4 k
- -3
# b* F% g6 x7 g# J: j - >>> math.ceil(-3.01): T4 D- G6 a4 [ S& k4 m8 P7 w- e
- -3
复制代码
3 [6 o7 v4 z' v$ \+ [" n+ `) y0 fmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身& z9 h& p- D) q* a
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" b# {9 G4 ?( Y6 d- H - floor(x)$ b$ Q% O2 _% {0 t! R
- Return the floor of x as an int.# R" f0 l2 B% Q% ^3 r/ U2 x. N
- This is the largest integral value <= x.; m( d; r2 X& r3 ~' C
- >>> math.floor(4.1)6 u0 h# s% a+ y8 k) ^
- 4
& L% Z/ S# N, {) M - >>> math.floor(4.999): i3 d3 Y9 ]1 H
- 46 r4 c, W2 h9 i4 E
- >>> math.floor(-4.999)
) }$ ?) w/ J0 e( Q8 Z" _0 S% c - -5" V5 `; d0 g1 V3 _( ~! R# k
- >>> math.floor(-4.01)
5 E( [* q/ K4 |; x - -5
复制代码 ' _" K" V% X6 ]7 k
math.pow(x,y) 返回x的y次方,即x**y6 a: ~7 [4 u1 D! |( q" j& d) S) K
- #返回x的y次方,即x**y
! }% G, |, d2 q4 p9 H& C - pow(x, y)
J5 u! G( v/ S0 e' Z; ~1 {% P \ - Return x**y (x to the power of y).4 x& J: U% T% Q
- >>> math.pow(3,4)- l$ \1 l# V, H/ S: n
- 81.0; S' W0 T z" J6 O. W/ y5 G
- >>>
) z- h# |6 Z7 |7 d) A; K! O S% o1 B - >>> math.pow(2,7)
' b) M0 ^0 _: I+ q& Q1 o: P6 _ - 128.0
复制代码 / D3 N9 T) n+ @8 l w( ^2 }* s
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( Q) U" T" g2 X8 d5 K$ u- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 X$ {7 W/ t" P1 x, O" C: m5 v
- log(x[, base])
+ ]( w; N" D, Q' A- z - Return the logarithm of x to the given base.2 q( C+ u9 c' M* W+ i, Z- \; l
- If the base not specified, returns the natural logarithm (base e) of x.
/ [* L, k! E5 D; y - >>> math.log(10)
M" y' ?) S6 _* `; G - 2.302585092994046' Z; F/ c9 J0 p7 h8 I$ }
- >>> math.log(11)
$ T; p. C1 [, x! p$ Z - 2.39789527279837075 m0 l, _9 a4 y8 y
- >>> math.log(20)- N9 G7 U5 @/ A* X( v3 j
- 2.995732273553991
复制代码
" `# \- O7 ~% ^1 q0 c" @: b8 K! Lmath.sin(x) 求x(x为弧度)的正弦值
& ~0 X1 A* y9 y- n+ v- #求x(x为弧度)的正弦值1 v3 g( H/ G- T8 o9 q
- sin(x)
! x: I2 A) m7 V V1 p5 z. o - Return the sine of x (measured in radians).- A: R4 Q5 N+ W
- >>> math.sin(math.pi/4)
1 v0 M0 \$ A7 C( m+ @, G - 0.7071067811865475
" F" } i7 L, v/ n9 C& r/ I - >>> math.sin(math.pi/2)( E: q8 ?8 m, r3 @( O+ |+ f/ I
- 1.09 S' U; \! J3 F2 [* t7 C
- >>> math.sin(math.pi/3)* H. U. A$ T0 a% S2 Y0 f
- 0.8660254037844386
复制代码 * W4 }8 @) n3 d) K, Y
math.cos(x) 求x的余弦,x必须是弧度- o& `+ w4 N; a. Y: @1 ^. L
- #求x的余弦,x必须是弧度
$ m; C* Y8 J7 H& p" A - cos(x)6 a4 U( s% ? r2 B0 C# |2 B( O9 _' `
- Return the cosine of x (measured in radians).: w' y. g' c; L8 Z; |
- #math.pi/4表示弧度,转换成角度为45度( I$ g+ ?4 W# F- A
- >>> math.cos(math.pi/4)% t$ I* Q6 x+ }( W$ u* J' }- O4 A
- 0.7071067811865476
$ W, n. T3 {- w! r- N - math.pi/3表示弧度,转换成角度为60度
, b8 r% j/ N/ o' I( ? - >>> math.cos(math.pi/3)
/ e* @) `6 B9 ]' e8 Z8 F; G - 0.5000000000000001
! o. I! @/ f. o: E& \5 P - math.pi/6表示弧度,转换成角度为30度
2 C. b/ t& b/ ]' ^9 v; v, ?4 ` - >>> math.cos(math.pi/6)8 F: ?* m& y8 P5 Z9 H. l- x! F
- 0.8660254037844387
复制代码 , y! N0 ^; |8 d
math.tan(x) 返回x(x为弧度)的正切值: [0 l3 b9 [" w$ `
- #返回x(x为弧度)的正切值
~* ~* ~+ }' o+ H5 M - tan(x)
% c( I9 c" O8 a. l/ V - Return the tangent of x (measured in radians).* j! o8 V; _' Q! f% d
- >>> math.tan(math.pi/4)
" G9 ?4 v3 n' m4 y% D, o# _ - 0.9999999999999999
* L2 Y, O7 _" |* E- h - >>> math.tan(math.pi/6)- p( n0 _. r2 U0 M$ z6 {
- 0.5773502691896257
% g, j* W* L* ]: @3 q - >>> math.tan(math.pi/3). \5 s( W7 k8 R
- 1.7320508075688767
复制代码 - T( d& U; R- x9 f4 |
math.degrees(x) 把x从弧度转换成角度/ P: A- D* g' L
- #把x从弧度转换成角度
4 h% B# i% C6 c% i3 \0 w - degrees(x)
! b8 W6 X3 r# h" O7 z0 V: C - Convert angle x from radians to degrees.
( o% p6 L3 ^2 C - 0 E, z% u, x$ e
- >>> math.degrees(math.pi/4)/ j6 X* I" H2 M9 V' y) T
- 45.09 P4 J7 Y; [& _, [+ \& `* R' Y
- >>> math.degrees(math.pi)
" D+ R- A+ {8 x6 }' R! X( w - 180.0
; s% I. h3 B& o - >>> math.degrees(math.pi/6)& c* K) c+ W1 Z0 W1 \ p
- 29.999999999999996
& X6 n1 W5 Y; V% t: S, G - >>> math.degrees(math.pi/3)
$ U$ l9 }) ^% f, b6 f% S, V$ |$ h - 59.99999999999999
复制代码 5 @! E! ~% X% R1 Y; l
math.radians(x) 把角度x转换成弧度
Z* C8 \5 Z- a4 S5 Y V9 }7 x) ?- #把角度x转换成弧度
0 X& h! S) e8 x - radians(x)
' ~( }+ |! T5 [; E - Convert angle x from degrees to radians.. D: ?0 C7 J+ `; x9 j
- >>> math.radians(45)
' o" J5 t9 i& A2 B& J/ ] - 0.78539816339744839 ?( M9 E: ]) E* T: P
- >>> math.radians(60)$ ]* H+ h7 s! W, U! T8 f. w
- 1.0471975511965976
复制代码
! B4 |! r& Z* J# Rmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
% R1 F7 D" ?: b" G5 V" F4 B- #把y的正负号加到x前面,可以使用0. \2 ?" x. {$ A; A
- copysign(x, y)
8 ?- B4 R) w+ R5 X Z/ S# h) n - Return a float with the magnitude (absolute value) of x but the sign # ~: g8 i/ A$ j
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 V% p0 |, Y5 c+ w% o - returns -1.0.
2 [ K$ n& s2 M1 F4 ]& g - $ o4 s/ N! g' K1 @
- >>> math.copysign(2,3)
7 a2 q& ?8 M2 Q0 V - 2.00 ?# F* Y$ h5 J+ n2 L+ n* N$ e
- >>> math.copysign(2,-3)
0 p' l6 k, E" `0 T* p/ ] - -2.0/ v: ^ o6 G& a3 |( T
- >>> math.copysign(3,8)! E: V# I8 V8 Z0 t9 i
- 3.0
* h/ {. o7 J, |$ V) f h - >>> math.copysign(3,-8), Z- P0 k2 f( H$ q
- -3.0
复制代码 $ x: S2 ?, \( p. `
math.exp(x) 返回math.e,也就是2.71828的x次方
3 x5 a* h: D( n( _7 R+ v- #返回math.e,也就是2.71828的x次方5 ]. V* m) a. F1 n0 g- w1 z
- exp(x)" I, R) g( w9 Q" U' a$ V; u5 z
- Return e raised to the power of x.+ ]" z8 _/ R6 I+ W
- & I5 ^3 \! D; z- a% z
- >>> math.exp(1)
; u: x! j9 J) n# a4 G( w! S - 2.718281828459045& i1 [4 h9 y0 M
- >>> math.exp(2)) ^9 }( ?5 v/ r+ H
- 7.38905609893065" a/ ]# O6 P. k9 j$ i
- >>> math.exp(3)
# Z% w; O. M# s& | - 20.085536923187668
复制代码 1 T% Q( u v% D
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
8 A1 ?8 W' L9 d6 U( _$ I- #返回math.e的x(其值为2.71828)次方的值减15 o. e( T$ w, p8 \) b2 a
- expm1(x)7 I3 W2 o' `7 ]% L) o
- Return exp(x)-1., v2 {0 r/ N! H4 f# u
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
4 B# }/ U3 T* K - ; C/ \5 S3 B4 ?) f3 V7 n9 K% v2 j _
- >>> math.expm1(1)
/ D! n. c L! Z% S4 `, j - 1.718281828459045. w8 S' K% G# G( o
- >>> math.expm1(2)
* P" E0 ` ~4 U! a% |7 i; s: F - 6.38905609893065
) a/ g% I! N. F4 ?# A) ]/ e - >>> math.expm1(3). {2 F1 ~: X( l9 f* q
- 19.085536923187668
复制代码
) ?# N) b/ |4 `math.fabs(x) 返回x的绝对值- ~% S1 n9 k$ h# n _4 w5 }
- #返回x的绝对值3 F9 n, k" ?! L% X* E( `
- fabs(x)* E' r4 S) }$ f- A( k9 Z4 i# ?
- Return the absolute value of the float x.
9 v( l5 K) Z8 O - : Z# W8 N( Y! H& c8 Z7 R& a
- >>> math.fabs(-0.003)
" W; R3 b/ ~' ~ - 0.003/ T( @1 r. T9 l W, a( k! i
- >>> math.fabs(-110)
0 b0 Z4 n% E9 X2 u9 t$ O2 @ - 110.0
) ^/ h7 r1 v1 G - >>> math.fabs(100)
# u6 y0 E; k0 \( j4 J5 Y - 100.0
复制代码 h9 J( z- j/ @1 ~( Q
math.factorial(x) 取x的阶乘的值
3 H. Z8 [* Z9 V5 S n- #取x的阶乘的值
6 ^9 }5 W3 l+ g. {% s - factorial(x) -> Integral
/ J( H8 b. k! t% N$ c5 \ - Find x!. Raise a ValueError if x is negative or non-integral.9 Z8 ^$ ]7 p' D% w ]
- >>> math.factorial(1)
& t. M! j- ^ \, e# T& y - 1
! A* b8 j# v r# ]6 L: q0 Q8 U - >>> math.factorial(2). u& f8 l5 |& G6 [4 T% B
- 2
6 `+ B' s4 Q2 Y3 @& i - >>> math.factorial(3)
3 J' V; `* O, e: d0 D9 I2 t - 6
, }) Z* o P4 ]4 o - >>> math.factorial(5)
; ]4 \, _% [7 u - 120
7 a/ m. N4 O# H7 C: O+ l% y9 i3 x - >>> math.factorial(10)+ t8 M: _4 h5 L! J$ U: r
- 3628800
复制代码
& Y% Y* \) Q a5 omath.fmod(x,y) 得到x/y的余数,其值是一个浮点数& K5 {7 V, [$ }
- #得到x/y的余数,其值是一个浮点数6 W& K% W/ V3 Z5 z, c! ~9 R7 z
- fmod(x, y)$ |; K$ B2 Z, F3 R
- Return fmod(x, y), according to platform C. x % y may differ.' M' v+ S% G) d3 S/ r+ v
- >>> math.fmod(20,3)' m* h) \& c- S0 X) i3 |6 Q; W
- 2.04 S2 [* J% G' n9 D g) ~/ \
- >>> math.fmod(20,7)
4 P. s4 [; ?6 B; K5 v% g. n0 A# @ - 6.0
复制代码
W. J0 f4 V4 K4 R2 w# Mmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围( j+ P+ W2 d$ K, Q) a
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. Q9 }) h8 [) d( Q @7 `2 r& J& m - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
n+ C& s4 b2 q4 u - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
( T; k* \0 {/ H; ~' E - frexp(x)
$ {. O8 U& n4 A+ M& _" z \$ C- C* Q - Return the mantissa and exponent of x, as pair (m, e).
4 P m% q) C9 \# e - m is a float and e is an int, such that x = m * 2.**e.
7 \% z3 c' H; _$ T" ?* `3 g - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 q4 S: M% Q8 h/ E6 L: @ b
- >>> math.frexp(10)
% d1 X5 i+ w' ]8 O3 `, P1 e( ]8 W - (0.625, 4)# M, H" o5 p2 ]
- >>> math.frexp(75)- Y, O+ i0 u: H+ D3 R( K2 z9 ~, b
- (0.5859375, 7)
$ L: }9 d( G2 F - >>> math.frexp(-40)
6 L3 C9 c4 X" O- I5 v! w8 K - (-0.625, 6)7 a7 ?9 D: @9 m( V/ \
- >>> math.frexp(-100)
, ~$ ?( v! x! k5 U4 S' ^$ d! \ - (-0.78125, 7)
( e+ R4 \5 k+ @! V6 r$ T3 ` - >>> math.frexp(100)
/ m5 P7 _; r3 K - (0.78125, 7)
复制代码
2 _5 W8 n) p7 z" Omath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
/ N8 k# W- ?6 a- #对迭代器里的每个元素进行求和操作- @0 r) ]8 @; O# f
- fsum(iterable)9 C% S! S* T9 ~. d/ i
- Return an accurate floating point sum of values in the iterable.7 O+ n* x" k" e7 l) S; v. P: {& c/ N, b
- Assumes IEEE-754 floating point arithmetic.. t6 i6 m/ G- _4 \5 ?6 P) a0 e; @( m
- >>> math.fsum([1,2,3,4])* \& Y, P! i, z: y: k1 M; i* ~
- 10.08 y ]" t5 O1 c# U# [) U
- >>> math.fsum((1,2,3,4))
" V: s3 M! ?: v - 10.0
a. x: }$ k: v# ? - >>> math.fsum((-1,-2,-3,-4))+ D$ L' r# r" z9 T4 F
- -10.0
5 b& v8 J; n2 g1 ?+ t% ]2 E8 z% @ - >>> math.fsum([-1,-2,-3,-4])5 d. W: ~* l2 o& H; G# s; u& X- x
- -10.0
复制代码 3 x3 x; H. ]* N+ n
math.gcd(x,y) 返回x和y的最大公约数: D' d9 G- `/ k/ \3 o) G+ D! n
- #返回x和y的最大公约数; x6 J. E2 j$ {$ D5 }. J
- gcd(x, y) -> int: {4 p8 l( T: |! d ~5 `% v
- greatest common divisor of x and y
/ d) B1 e9 m: P+ r/ i - >>> math.gcd(8,6)
% f- {% @1 D" Q* Q N7 P - 2
- t C. M: Q; B - >>> math.gcd(40,20)
( C- ^' X1 K. ~( b i - 204 G/ u9 f0 @% x0 q! ~
- >>> math.gcd(8,12)
8 c8 K5 x4 V9 W - 4
复制代码 & G& ~. G+ o( y1 a
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
: K5 P. E8 X6 _5 N- #得到(x**2+y**2),平方的值7 P6 ?& V- {' }& g9 P- R
- hypot(x, y)* K5 q1 G7 p( [. d9 P' F
- Return the Euclidean distance, sqrt(x*x + y*y).
, P5 _" C# l7 h& m - >>> math.hypot(3,4)0 u [$ Q7 z; q' O$ |5 E! S' x
- 5.0& a* T4 c" U$ f/ x( q1 }
- >>> math.hypot(6,8)( b& O# l9 f# B8 W9 z ~7 k
- 10.0
复制代码
- F; f8 H7 Z! }! K$ t0 j' [7 amath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False4 w" I- ^0 g) b, Y3 E7 d
- #如果x是不是无穷大的数字,则返回True,否则返回False9 v, a2 }, U+ z \/ u; W+ t5 \+ f
- isfinite(x) -> bool
7 F+ P1 [' C- i/ ^# ^4 c# w. n - Return True if x is neither an infinity nor a NaN, and False otherwise.% I) Q/ d# G" I8 M3 V7 i( s
- >>> math.isfinite(100)
$ W; l7 I( c, f" [ - True
: k9 ]5 X% T, D5 V( P# d0 ] - >>> math.isfinite(0)4 l |+ T2 ]* e7 x9 X4 r
- True" R6 k4 |( S$ A9 F' K
- >>> math.isfinite(0.1)
! _+ a" E& P4 i - True! s6 N7 C' j2 `& k3 o5 C2 k4 I
- >>> math.isfinite("a")% m7 e2 U+ S$ v) A( Q/ G9 ?
- >>> math.isfinite(0.0001)
+ k. U/ h B7 J - True
复制代码
& B% c" y, T, omath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 L' T$ d T4 O8 l
- #如果x是正无穷大或负无穷大,则返回True,否则返回False6 S) x4 y. u( G
- isinf(x) -> bool
- x- z6 w" R" k. c9 r* m - Return True if x is a positive or negative infinity, and False otherwise.
, I' o7 V( E) y2 J* E( e( {1 B" N" n - >>> math.isinf(234)0 _: F( I2 s5 s6 h: T
- False
* }6 r% }, k. }; n* ~" ]) O - >>> math.isinf(0.1)
8 y1 ~4 V+ Y( M) E - False
复制代码
. ~4 J+ D8 n6 |9 H( K9 K( C" Tmath.isnan(x) 如果x不是数字True,否则返回False
% m/ h& y2 }3 @6 J; b- #如果x不是数字True,否则返回False
& b$ ?' p0 ^2 \ V; S - isnan(x) -> bool. ^/ }* d/ P7 e2 L0 ^- L
- Return True if x is a NaN (not a number), and False otherwise.) h$ d4 Q' M' ~' b4 m
- >>> math.isnan(23)
: Q2 u6 Q0 n9 R$ i, U - False- r3 Q* f1 M$ V7 f3 e' B, s
- >>> math.isnan(0.01)* T9 X/ A c0 X/ M% \
- False
复制代码 5 W3 T6 e( P( W3 |1 `2 n/ b# P9 ]
math.ldexp(x,i) 返回x*(2**i)的值
& d4 K" [% R( y+ E* ]- #返回x*(2**i)的值
: w2 W6 o% E6 f - ldexp(x, i)
" I# w" L; E1 v - Return x * (2**i).8 Z7 c) [. J& J) |" O! k6 F
- >>> math.ldexp(5,5)
% L9 |5 n6 W7 ?6 j - 160.0- R ^* T' d8 m. t$ Y4 g8 B
- >>> math.ldexp(3,5)$ P( a6 i2 x" k% _! D6 x
- 96.0
复制代码
H9 C9 X: Q5 _math.log10(x) 返回x的以10为底的对数' h' V2 I$ `/ s5 ?' @+ K% Z0 g3 P- j
- #返回x的以10为底的对数! F6 x% K3 @; H% o4 m
- log10(x)
# g; c8 }' ]2 m! f3 ~ - Return the base 10 logarithm of x.* `6 s, H( P3 f( ?
- >>> math.log10(10)
# G5 C8 a8 L" V% x - 1.0
0 n0 P) O J3 n1 Z. l - >>> math.log10(100)
$ ?6 r* a# r; U$ Q* D/ Y- r - 2.08 _( T7 K' T+ T- W; C. k2 z$ r
- #即10的1.3次方的结果为20& d; _' C0 T' R9 G( ~
- >>> math.log10(20)
6 D. |1 s. h2 Z% V2 {' F' Z- @% \ - 1.3010299956639813
复制代码 ; ? [! C7 i( I5 O A* v
math.log1p(x) 返回x+1的自然对数(基数为e)的值2 T0 s# y& v2 J( l+ _$ p) M
- #返回x+1的自然对数(基数为e)的值9 l8 M- x% M" y
- log1p(x)
1 {2 e4 t& D* }% Q: k - Return the natural logarithm of 1+x (base e).0 I& ?" Z2 J3 K$ \9 T1 T8 M' @: W
- The result is computed in a way which is accurate for x near zero.
! E: K0 w* }( i* h - >>> math.log(10)
r. J6 I9 `1 q/ D3 W- M! q - 2.302585092994046: \! i7 g f X0 V+ l7 `+ _
- >>> math.log1p(10)
* u$ c' N5 g1 n# `! x+ l& i( }( ?. F - 2.3978952727983707
8 j+ W# D1 i0 Q$ |& W - >>> math.log(11)
s0 B0 q: O* ` - 2.3978952727983707
复制代码
! [) |3 ?* M, r# Fmath.log2(x) 返回x的基2对数
4 E, u) H M! x/ | c& V" A0 X& Q- #返回x的基2对数
: `0 ]( H; Q2 ~) |7 N3 k - log2(x)2 d! B- X. K: X! E! N( Y+ `
- Return the base 2 logarithm of x.
) F T6 w$ L; o8 z) ? `, z5 Y - >>> math.log2(32)* d) Q6 M; J; O: P
- 5.0
{ I: b0 }( q7 ^ [5 l - >>> math.log2(20); y3 e7 k1 @3 l# }4 _; S( s* o4 _
- 4.321928094887363
e3 j- B5 t$ ]6 r1 u7 c - >>> math.log2(16)
3 m& ]! U- Q0 x9 H! U& r) B - 4.0
复制代码
0 w. b ], J y% m+ amath.modf(x) 返回由x的小数部分和整数部分组成的元组
& b7 u* A# A1 i, V% I8 h- #返回由x的小数部分和整数部分组成的元组9 _* y+ L$ E6 N& s: ^# h8 z" \
- modf(x)
) f6 }& a" \; \; V- j7 D - Return the fractional and integer parts of x. Both results carry the sign8 g9 n! r! P* Q6 C
- of x and are floats.
) |' P5 L, D1 Q- R4 ~6 ~+ f, n - >>> math.modf(math.pi)
/ d. j( y' \3 m# w - (0.14159265358979312, 3.0)
1 j. J8 `+ ^5 r' L: C8 e - >>> math.modf(12.34)4 C: |( F2 S4 V
- (0.33999999999999986, 12.0)
复制代码 * A4 h/ y i9 @6 p+ H
math.sqrt(x) 求x的平方根 x" T! z6 ~: E. \" l
- #求x的平方根! b! I9 G6 m7 ?6 J! k
- sqrt(x)% X: p) w2 ]: r5 U% T& g, {. g
- Return the square root of x.6 o' c! p. t; s5 a
- >>> math.sqrt(100)
3 q/ i$ b! e! X @. P - 10.0$ G$ D( k" |+ `) s' v: l- S
- >>> math.sqrt(16)
' \' [6 R! }5 l - 4.0; A0 C7 d+ P7 u9 w# V" O
- >>> math.sqrt(20)) r9 T) R; v f) _! |+ D
- 4.47213595499958
复制代码 . V* C) T5 e: o7 r" s7 |
math.trunc(x) 返回x的整数部分- #返回x的整数部分- N4 A( |( F/ N- Q' |. P
- trunc(x:Real) -> Integral, E! ~0 q- L# Y2 v
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# ~0 e* } v8 R - >>> math.trunc(6.789)5 ]+ |$ s$ G2 G c: M$ B3 `- A$ J
- 6$ f% |& N4 z7 v1 Z% c7 d- \5 H
- >>> math.trunc(math.pi)
2 O' H! A; |, A" r6 K# `: F - 3
, X6 G9 R/ P9 x# l' Z5 X - >>> math.trunc(2.567)( L% k6 \9 `; t% r" w& @# y) Z# \
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|