马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
% b( ~3 N& l# U$ F( W2 P" J4 F
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 u/ p4 t& n& \$ c, H4 h) P; T. o) G
方法1:0 n; H, y$ o6 }
- >>> import math! X9 C4 Y+ J( V+ w
- >>> math.sqrt(9)
. Q7 Q8 E/ u7 o# O1 U4 R - 3.0
复制代码 方法2:* Z7 v1 ~, }5 j1 X0 w1 M7 Z
- >>> from math import sqrt
8 @8 y, w, P& T - >>> sqrt(9)- f" W9 P* ]! e* A0 a: S
- 3.0
复制代码
. s( w, M1 Y1 r5 ^ & Z i9 ?0 D2 a7 r/ r; n5 Q, {! m
math.e 表示一个常量3 Y5 ~* F9 w, p# Q8 _7 O0 c
- #表示一个常量$ d5 v0 C q9 }0 r# y ?% @- ]% C0 d
- >>> math.e7 _9 t( c6 S9 f9 v
- 2.718281828459045
复制代码
9 f1 s* l$ K& |6 E0 Smath.pi 数字常量,圆周率: P( ?4 f3 t3 s4 s9 K# K7 d
- #数字常量,圆周率
6 I: ~. k n. H& X- ^: P' l! A - >>> print(math.pi)
6 |/ @1 k' f. O - 3.141592653589793
复制代码
' b/ C) X+ V- ?# o2 Amath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x5 t/ v, j1 Y% ^/ e: f
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
, O7 w/ B% N. W, u: u- p - ceil(x)
. c( e2 P$ |" S8 h4 m - Return the ceiling of x as an int.
+ t9 Z1 N( A/ }! p- P' a - This is the smallest integral value >= x." G* U' p, e4 H" S1 L
/ k: [8 ~; _) s( S/ j- >>> math.ceil(4.01)
9 i0 K! ^$ E3 l - 5
) j5 b' l6 ]5 q r- x2 [ - >>> math.ceil(4.99): h: ]( I3 E% t& o9 _! v
- 5
) o* F( O* |+ k, A- I, p! { - >>> math.ceil(-3.99)# `/ W+ s: q+ M. J, D+ Q
- -3, V8 M( e, R/ c- D1 g4 `* D1 Z2 d
- >>> math.ceil(-3.01)( o7 O. U* d' q; O$ s0 B
- -3
复制代码
+ n X& d7 T% o' h9 a# N7 z9 o" ~math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
e& G) K' Q$ k7 z% L4 q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身: a( f; @% [# |, q$ r5 i
- floor(x)
4 @, x! {: D. `9 Q! V - Return the floor of x as an int., t: I6 x& x7 Y1 C; J8 }- D5 s
- This is the largest integral value <= x.
$ a [3 h- O' ~& [2 o+ T - >>> math.floor(4.1)
/ b6 `6 G4 O+ l6 i - 4& k; _1 K# v" L. G' Y& m
- >>> math.floor(4.999)
1 t$ ?! r* x6 b$ e- l - 45 q% p$ ~# Q. F: T5 o
- >>> math.floor(-4.999)* e6 N' b. b0 Z. c, @0 S
- -58 D* a% r [: J% `. A( t
- >>> math.floor(-4.01)! N8 E: _% k" k5 I# ^1 g# A& p& C
- -5
复制代码
$ [9 ]3 ^* _! v, ^6 }math.pow(x,y) 返回x的y次方,即x**y, E3 t; N# b6 K+ h6 v
- #返回x的y次方,即x**y3 p |/ Q0 W$ \& S8 A4 _
- pow(x, y)
8 [# x) t" ?/ A% V* q, o! Y - Return x**y (x to the power of y).
) N/ L u8 d, `6 t8 d9 F2 l - >>> math.pow(3,4)
9 n: X2 [1 S. s# F: z" z, w - 81.0
9 M: b2 f% n0 v; d f% l7 U - >>>
6 ?9 ?% D6 Y2 S/ s5 I" m/ U. } - >>> math.pow(2,7)
- Y6 D: T' Q9 a, U+ ~. B - 128.0
复制代码
5 r7 G' {! R3 k0 o3 f9 ? I# E0 vmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
; o1 V; C+ U, r; |/ X8 ]$ a- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)4 z. d$ z9 l) w" o
- log(x[, base])
& s* a- g- O4 Z4 e - Return the logarithm of x to the given base.
1 S6 J# E6 j$ S - If the base not specified, returns the natural logarithm (base e) of x." ]* c: T. I- k# g
- >>> math.log(10); |. y; F, N! X' X
- 2.302585092994046+ T5 q' @* U# @# b- w8 y
- >>> math.log(11)
# O8 L7 R1 _4 o; W7 x: a - 2.3978952727983707; O6 h8 n& c8 @7 `- h
- >>> math.log(20)% y- P+ X+ {- d8 m, E
- 2.995732273553991
复制代码
3 R/ u; ]* B. Y& Tmath.sin(x) 求x(x为弧度)的正弦值/ ^2 n% v* Q- N6 d1 t/ v
- #求x(x为弧度)的正弦值8 @" R2 |/ y' \. q
- sin(x)
: @( r. ^9 P p/ Z' w - Return the sine of x (measured in radians).6 R' ^% q! _1 T: B
- >>> math.sin(math.pi/4)
+ @) g8 V: a: ~! k! u; P, j - 0.7071067811865475# p; G- g0 K8 y, T* l
- >>> math.sin(math.pi/2)" M+ A) r* g# q, Y
- 1.0
) c; I% F5 f1 F1 @ - >>> math.sin(math.pi/3)9 M$ I- `6 z2 w* d/ _& E! M T
- 0.8660254037844386
复制代码
! A9 |# g: X2 Emath.cos(x) 求x的余弦,x必须是弧度2 T; Q( U* n3 d4 o1 |1 g
- #求x的余弦,x必须是弧度
# z# Q$ ~; y7 ^5 R R7 I - cos(x)
4 M3 r# h# p! c9 V9 x9 s) L" t - Return the cosine of x (measured in radians).
4 q2 P; v! h- N- P: e# } - #math.pi/4表示弧度,转换成角度为45度
- V3 J) B$ v4 D: f1 ^ - >>> math.cos(math.pi/4)
4 V4 H2 W( S$ o% M0 p6 M8 d$ E" ^ - 0.7071067811865476
6 b$ g$ v6 N. n0 C* {" h# S - math.pi/3表示弧度,转换成角度为60度& X1 x% E. ~7 w
- >>> math.cos(math.pi/3)
. X, S2 L0 x4 \ - 0.5000000000000001# ]+ x' G7 H( R) S
- math.pi/6表示弧度,转换成角度为30度9 L w' ]8 k% [! w) v* I. B2 P
- >>> math.cos(math.pi/6)
S0 v. N& ~' J0 ]6 l# R, t5 ] - 0.8660254037844387
复制代码 . e% G) ^6 h3 j: y
math.tan(x) 返回x(x为弧度)的正切值
. R; { K+ L/ F- #返回x(x为弧度)的正切值 B- V: V# h2 H0 E, [% M
- tan(x)
! j/ l" a. m; V3 F/ e# Y - Return the tangent of x (measured in radians).
' r) j- P7 X, _, F+ d- f& r - >>> math.tan(math.pi/4)2 q6 r, G* S- b$ w: T) L
- 0.9999999999999999, J# k: a: g, a3 ^
- >>> math.tan(math.pi/6)
& M( n) B1 \# K$ |4 Z1 P3 i' k - 0.5773502691896257
, H$ A( Q3 v& a# L3 e+ k$ f9 g - >>> math.tan(math.pi/3)
3 ~# U! H' g& M3 j4 ? - 1.7320508075688767
复制代码 1 f, p$ z, z4 o
math.degrees(x) 把x从弧度转换成角度
4 a+ v2 A3 E2 K( j0 \- #把x从弧度转换成角度3 X3 {- L# X5 e
- degrees(x)
Y- y$ t2 B, w. f! H P - Convert angle x from radians to degrees.
5 H. j% j: C5 ^5 ^' ^9 J; B - " M; J& ~9 n7 W( R
- >>> math.degrees(math.pi/4)
$ F" O9 z! v2 j' W" M - 45.0
* A1 r6 F+ e' ~8 Q7 s) w) t' i - >>> math.degrees(math.pi)# c) P/ v- D4 m- Q/ ]' P
- 180.09 n9 G7 ~0 r- K0 f: j# ~
- >>> math.degrees(math.pi/6)7 w& {( S- u7 a- g; ^8 {
- 29.999999999999996 H8 q' |$ p1 s/ G
- >>> math.degrees(math.pi/3)5 X9 i: H( S1 j* h- Q7 F8 o
- 59.99999999999999
复制代码 + K' v9 c5 P+ h
math.radians(x) 把角度x转换成弧度0 K/ m" b" Z# i- ?
- #把角度x转换成弧度
" b0 x& x" U ~ - radians(x)9 B% p n5 f/ s& I/ v$ N, h. W
- Convert angle x from degrees to radians." c) b" d# n4 r' v1 L$ d
- >>> math.radians(45)
" d- h' |; I1 d- H9 `1 U6 e2 H& E - 0.7853981633974483' P. ?9 W* }! W. k5 B" u
- >>> math.radians(60)8 m+ x1 {; x& c' ^: n( I7 z
- 1.0471975511965976
复制代码
+ u# N2 a+ ?, j& ^! [! Qmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
6 d# R+ R9 u9 X- #把y的正负号加到x前面,可以使用0
$ f- k# h: m* x; a' Q - copysign(x, y)3 P( X8 U1 @+ p: f+ Y) L
- Return a float with the magnitude (absolute value) of x but the sign % g. H8 c4 u. ^& |% ?0 l3 e
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 p- ^3 Q$ M$ r. I
- returns -1.0.
3 G# S1 Z8 w& }) V/ N
# ]+ f3 | O. M$ U+ o/ U& F# i) m- >>> math.copysign(2,3)
6 K k( A' k* w/ s - 2.0# b- a; y& `# n% c
- >>> math.copysign(2,-3). q7 M' M4 \& I1 V, b' \. c5 F) [: m1 J, u
- -2.0
" S/ Q4 q9 A6 Q9 Z% L% U - >>> math.copysign(3,8)
7 S1 l& X# `9 T - 3.04 V/ V( A7 ~+ v3 e" A) l+ _. i/ }
- >>> math.copysign(3,-8): D x- I# ]+ J1 D) `
- -3.0
复制代码
, F! Y* C+ ]- \% c# l7 r0 V, Kmath.exp(x) 返回math.e,也就是2.71828的x次方
; I$ {' }, ]2 o$ y- #返回math.e,也就是2.71828的x次方
4 A, {" ?4 s9 n6 M, }; W$ j: m" L - exp(x): t! W4 Z6 t7 M$ S5 A$ D4 ^
- Return e raised to the power of x.* |+ A0 T: z' D* q2 r% V
6 ?' L! q3 l! m7 ~3 ]7 t4 }- >>> math.exp(1)/ }% I0 E6 k% r* d$ q: f9 s/ h
- 2.718281828459045( r5 W0 c, m! w6 w( p) c) Q
- >>> math.exp(2)5 [! W. k5 m& U
- 7.389056098930650 o3 G4 |8 V% ~& P
- >>> math.exp(3)5 L1 \8 n* {( \+ m- v9 U7 g. \8 S+ a- C
- 20.085536923187668
复制代码
1 \& d7 W* a) Q/ Wmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) U$ b7 u2 [2 O" ?( l( {
- #返回math.e的x(其值为2.71828)次方的值减1
" }( }3 M! a/ R- t( X - expm1(x)
# s/ U4 I8 i5 a4 u6 Z% x5 I - Return exp(x)-1.! H$ m$ J0 p" k$ a: Z9 Y
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x., `! y5 h( j6 S" q7 ^( ^; G
- 8 l7 a" e9 _$ x
- >>> math.expm1(1)
3 T) i0 e7 t1 U - 1.7182818284590451 o; U# G1 S" `3 l
- >>> math.expm1(2)
9 C- K( c2 M- }0 g, H$ x - 6.38905609893065+ T* z8 V9 e& J, ?5 |
- >>> math.expm1(3)2 O9 d. G. U) K# L+ b& j
- 19.085536923187668
复制代码 6 g5 Q6 V" \8 V) N
math.fabs(x) 返回x的绝对值
9 M' w' E5 A$ f" n' ^ K) g- #返回x的绝对值
# h- h- E- U4 t. A - fabs(x)
; \! d" Y9 |! R" z1 W6 Y - Return the absolute value of the float x.
) |4 t) v& C) t2 a) T+ C/ n/ `. x1 n
^( B. x/ A" B- >>> math.fabs(-0.003)* v. f) O6 a% K7 y
- 0.003( ?2 b* }1 O- t
- >>> math.fabs(-110)
0 z4 k& D5 Y+ n; c+ ~: h w - 110.04 `1 q' r. o+ f' r6 f
- >>> math.fabs(100) w, K# `/ L J
- 100.0
复制代码
6 P$ j* O2 U0 u1 n. ]) j. [( jmath.factorial(x) 取x的阶乘的值
( A5 e# I" c3 d, ?; E- #取x的阶乘的值
, V" _7 }$ D7 [% n5 b$ l4 V/ R5 v9 ? - factorial(x) -> Integral
6 u. |7 u' k& y# |) M. O# ] - Find x!. Raise a ValueError if x is negative or non-integral.$ ~. m" [" m( q/ w3 L4 Q
- >>> math.factorial(1)% L2 _' |! V$ K5 |
- 1
1 x2 Y! \! }2 B9 z. D, f0 _ - >>> math.factorial(2)
8 ]% w, o, D. B; S% ?1 f - 2, c" g; u; ]2 u0 Z: m" t1 a2 k# E
- >>> math.factorial(3)
T) o3 T2 I% } |" L" G - 6' \% r6 {- h# E& P" k; v
- >>> math.factorial(5)
6 k1 Q. I- `8 u0 ^6 q- ?. q8 V - 120/ X8 n" @3 K9 E8 Z, x6 @
- >>> math.factorial(10): j0 P* L1 s) T
- 3628800
复制代码
( l6 _2 h5 w& F+ ?, l- cmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
* H: v+ D; G( l# ~- c- #得到x/y的余数,其值是一个浮点数
9 \3 e) d' r h; C - fmod(x, y)
- F. }# v( H. i' R; f - Return fmod(x, y), according to platform C. x % y may differ.
; I2 m. ?% a; g0 i - >>> math.fmod(20,3)* Z+ `, C9 P3 Z7 d/ F; I( W
- 2.0
% A) B6 b5 Q! Y8 Q. B - >>> math.fmod(20,7)
2 X" G' x6 s" [0 _ - 6.0
复制代码
) w/ ~$ m1 H( q9 u1 zmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) U% N, `: [5 f* m* j
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( n# d2 \# V- w- ~& j
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 d4 E8 \3 {$ u3 H: m: i
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
0 N! }% d. a5 i& r2 T1 K- d - frexp(x)7 N0 G4 B% B. `' Q4 B: o% \# v
- Return the mantissa and exponent of x, as pair (m, e).
1 W! ^# q0 R$ V& r: ~" ~' \1 C - m is a float and e is an int, such that x = m * 2.**e.
+ s9 p5 l; F/ ~) M6 u - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 S) Q3 a6 {8 E3 _: F - >>> math.frexp(10)
6 {, Z, I: i; g( s* p - (0.625, 4)1 X' E, k: m( D/ o
- >>> math.frexp(75)
) D" G$ T9 X+ v- b7 `& k3 w - (0.5859375, 7)
' C- A! M4 _- b) C) V' c - >>> math.frexp(-40)0 S p0 d1 U0 l$ A, q. X( l" t
- (-0.625, 6)
) H: `: z/ ~! t: K% X6 T: F - >>> math.frexp(-100)# L5 B* T. [: _& W
- (-0.78125, 7)$ Y, k& @3 c0 @0 ^6 x4 Q# ^# W: s
- >>> math.frexp(100)" e" s& e% @4 v* F( H+ t
- (0.78125, 7)
复制代码
& t( y# f/ T0 b1 g* Bmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
, }" H3 ?: a0 F$ J( h- #对迭代器里的每个元素进行求和操作
& w# M' `, F$ h S/ [9 R% @# n2 ? - fsum(iterable)+ B9 ^5 R" S, ?. ^
- Return an accurate floating point sum of values in the iterable.
# Z/ A) O1 a3 ^- S - Assumes IEEE-754 floating point arithmetic.
/ k' \0 k3 N8 I, a6 ] - >>> math.fsum([1,2,3,4])
" I* T G: Y+ j0 ] - 10.0
. I8 h6 m4 Y- j) x4 W - >>> math.fsum((1,2,3,4))) O0 d0 Q8 i8 C& A0 G$ c& P+ |
- 10.08 ~# _; L: \+ m) ]* }6 X A
- >>> math.fsum((-1,-2,-3,-4))& i6 r) T" J. R6 B2 o
- -10.0
4 D% ^, a$ {- l* _. `9 @9 O - >>> math.fsum([-1,-2,-3,-4]): Y, `5 J! _5 b' [
- -10.0
复制代码 6 p( c3 S# v# Y: n! T
math.gcd(x,y) 返回x和y的最大公约数
* T% q8 k) |3 ]. N- #返回x和y的最大公约数
* n# J/ L6 k P) d - gcd(x, y) -> int2 e) k) I- ^5 U; _. B8 y7 S9 N, }
- greatest common divisor of x and y
3 T$ x( _8 O" z) `4 k7 h( B# ]3 W) { - >>> math.gcd(8,6)$ W, m& Z7 s! i
- 2
7 c+ q# y6 b& D - >>> math.gcd(40,20)! }$ _1 y% }* Y3 b
- 20# B% ?8 ~5 a3 s- Z5 y& P% \
- >>> math.gcd(8,12)! X# o9 |2 H9 m' n
- 4
复制代码 7 P2 _4 |: L4 N, v4 H- X+ p
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False, f6 m. W! d# h! X$ m
- #得到(x**2+y**2),平方的值 [5 t8 M0 M$ ?2 C0 P0 ], M7 M
- hypot(x, y)
7 k+ f7 { W4 [: Z/ Z1 e+ e - Return the Euclidean distance, sqrt(x*x + y*y)./ P! y8 f8 S q& {+ j+ ?: b
- >>> math.hypot(3,4)0 P6 H! q6 k0 f( H1 y+ {) \
- 5.0
# o3 R$ s/ L; q/ |) R - >>> math.hypot(6,8)5 j' p6 K& h4 H# w' j
- 10.0
复制代码 1 {( J" j) G. V# w' o% j8 p1 _
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
1 h, q- W$ a/ Q! [$ a- #如果x是不是无穷大的数字,则返回True,否则返回False
2 |( L8 L1 Q6 \" v \. Y - isfinite(x) -> bool1 W& ]# P1 t5 V/ a V
- Return True if x is neither an infinity nor a NaN, and False otherwise.+ e# o" e3 ?% _/ L( t
- >>> math.isfinite(100)
+ }* C% b2 x) X* J: L+ j9 ]. c; q - True
& F5 C4 \- C4 ? - >>> math.isfinite(0)
: _5 k6 Q ]% o - True
' C0 i/ z% e$ n& ?0 l - >>> math.isfinite(0.1)
R {' b/ h7 \ - True
# C5 s3 [8 f- R9 ^' w1 t2 [ - >>> math.isfinite("a"), P4 x- s' d" i2 `% g4 D) W8 Z
- >>> math.isfinite(0.0001)
7 K; q' M7 r% r* z! c& j, v) ` - True
复制代码 3 r/ z9 @+ S# l: G0 o0 O% |% h5 u, y
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
* ^2 z3 H, j. q' ?- #如果x是正无穷大或负无穷大,则返回True,否则返回False
5 W3 m4 [. @1 E0 `: w" \" r; f! E - isinf(x) -> bool! b9 D" g' e: E+ N
- Return True if x is a positive or negative infinity, and False otherwise.
! X$ D9 [* ?( n* ~ x/ V - >>> math.isinf(234)1 u, O- f4 g& Z6 e6 j) Y
- False
7 s8 L6 [ \4 ~! { - >>> math.isinf(0.1)2 y3 S* ^! i$ u
- False
复制代码 # Z5 r/ P0 D* N& o/ {0 i- `( O# n7 i% B$ ^
math.isnan(x) 如果x不是数字True,否则返回False# [1 [7 F- |7 O! Y
- #如果x不是数字True,否则返回False
; ]9 G3 B( }& H: @ - isnan(x) -> bool
( r. @8 W# M( A) H( L$ Q+ F - Return True if x is a NaN (not a number), and False otherwise.! \# @4 `2 d: @" z' D5 U
- >>> math.isnan(23)7 ?0 w( U3 o; q/ d6 [# m
- False: X# Y& v/ }+ i2 i8 m' K5 D& A/ U0 d
- >>> math.isnan(0.01)1 g6 C6 v5 W. W0 G& z' N
- False
复制代码 , h7 ~3 X V- [% `! K
math.ldexp(x,i) 返回x*(2**i)的值
) `' H! u ]& _- #返回x*(2**i)的值2 M6 l5 W, K# \$ {5 j
- ldexp(x, i)
6 C" Y. |2 h( W2 V5 h, M - Return x * (2**i).$ z, [; R' W. y. W
- >>> math.ldexp(5,5)
) c2 E1 n0 h0 B" ~8 O' J - 160.0
/ i# x+ X' r( r& ~2 _8 z - >>> math.ldexp(3,5)
' A k/ ?' n3 i0 _( I! N5 I - 96.0
复制代码
* r. z2 g w( N, f, V1 Smath.log10(x) 返回x的以10为底的对数* F9 A2 v2 o7 ]$ f1 i
- #返回x的以10为底的对数
7 T& ^3 g' W2 Y& \9 A w2 i1 a - log10(x)& i J! A) j. w, R/ b! s& a$ y
- Return the base 10 logarithm of x.
' u. u/ Z, J1 T! [0 d; R1 D - >>> math.log10(10)
" D8 d2 o& ?$ |9 q0 L: V! j3 Z - 1.0 l8 v, U; l: G' O9 Z. R2 g. ~
- >>> math.log10(100)* x, m" H+ ~ |$ g! v! |" u
- 2.05 ]4 D9 U' ~1 h& B# V9 G8 U
- #即10的1.3次方的结果为203 f: q0 U7 j" J5 }+ j! u
- >>> math.log10(20)
9 i d# F n0 K0 a - 1.3010299956639813
复制代码
& T- d& W$ J" l' ]math.log1p(x) 返回x+1的自然对数(基数为e)的值" i( @+ n# T# o+ g+ B
- #返回x+1的自然对数(基数为e)的值& e, b/ m/ O) y" F( P
- log1p(x)
1 t- a! N9 k% z4 X' } - Return the natural logarithm of 1+x (base e).
. u+ o9 @2 U: m+ f# M - The result is computed in a way which is accurate for x near zero.
, G- n3 ~7 u# f - >>> math.log(10)2 u% U0 R' X1 C9 \3 [
- 2.302585092994046. @- P8 u% _. G$ u p$ o0 l
- >>> math.log1p(10)# K2 e( P) T) ~' z0 }
- 2.39789527279837073 [ n/ [8 x1 f
- >>> math.log(11)
0 I/ [( z) P" q) B4 } - 2.3978952727983707
复制代码 7 Y/ E# i* G2 t8 p' q- v* L* q& ^
math.log2(x) 返回x的基2对数
+ `7 p# A0 b" `* T1 ^4 b) Z, j- #返回x的基2对数 c0 W+ ]# @7 s8 v8 Q3 ~
- log2(x)
4 V% l& h9 H7 @# k1 K v, z: ~' z - Return the base 2 logarithm of x.
# Y* K' ?3 {4 ?1 p% U- e - >>> math.log2(32)
& a, ~+ q" d/ r& X - 5.0
" M* T7 g2 f0 f& L - >>> math.log2(20)0 R4 H: r- F( S% \3 r5 z) S
- 4.321928094887363
' H/ y* U: V7 y9 A - >>> math.log2(16)6 \4 W$ D1 `: @) i, J" v7 n* Y
- 4.0
复制代码 8 {; X6 D+ R: P4 Q5 K
math.modf(x) 返回由x的小数部分和整数部分组成的元组
* y7 p; q6 L, n2 [# F- #返回由x的小数部分和整数部分组成的元组
( H" z, u0 H3 M - modf(x)% \& J, |( }; G4 k& O8 D
- Return the fractional and integer parts of x. Both results carry the sign8 ?, U+ ` `& S% u/ C
- of x and are floats.+ @$ v+ B. _& Z. [+ }: d, F
- >>> math.modf(math.pi). n! b& `( _, Z8 Z& p% @
- (0.14159265358979312, 3.0); e" p# l/ X, \; G T. d5 g) Z) v# D
- >>> math.modf(12.34): a8 O4 B6 |7 ~ I' F' \
- (0.33999999999999986, 12.0)
复制代码 & p3 _8 X( K$ K- [6 f2 x, `
math.sqrt(x) 求x的平方根1 b' v3 V6 w6 A- L
- #求x的平方根
. E0 w- e6 y a: l6 {; r8 [ - sqrt(x)
. }1 B0 J3 y s8 ^% a; O/ j* e - Return the square root of x.+ p0 D( V$ @! t M
- >>> math.sqrt(100): Q0 B6 n5 u* } |% K
- 10.03 D# T% h Z9 p F( O) z
- >>> math.sqrt(16)6 W% A+ B2 T* A* ]+ Q
- 4.0: k* B( Z$ x* B2 R( U
- >>> math.sqrt(20)! U, B! m- ^1 B5 J
- 4.47213595499958
复制代码 / P+ L5 H8 i$ K3 c6 j& l- e4 e1 L2 n9 D
math.trunc(x) 返回x的整数部分- #返回x的整数部分" G- K! g% v9 g$ P/ L
- trunc(x:Real) -> Integral
' v5 D' j/ B' V& w - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 n3 s: d8 U' J, x% C2 |
- >>> math.trunc(6.789)5 d/ c$ t# z! Y5 b9 G) U
- 6
e/ x; R C: W, J8 s - >>> math.trunc(math.pi)
+ b L* |- Y% O6 K U+ U - 3
# R, U( `3 F. x, B - >>> math.trunc(2.567)
6 m) l5 u1 e& X" t4 g( [ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|