马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* _) R2 W: w1 w* q8 t' `+ g【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, G! s# h. L' Q& Z% _! f* [1 _! m$ P. r! G U0 Q
方法1:( s2 O8 v3 R) M. Y* ?
- >>> import math
/ O* g) _3 a7 L - >>> math.sqrt(9) ] o9 k1 ] E/ |7 F$ T0 `! F
- 3.0
复制代码 方法2:
) `8 m" `1 U( A& c% X0 J3 T- >>> from math import sqrt4 F0 j& ~6 d3 b- W3 Q% P
- >>> sqrt(9)" h, j) Z9 ?, T" z% n! Y
- 3.0
复制代码
/ d8 C8 P4 S+ s9 C
' N9 E( e; W% ?: _( vmath.e 表示一个常量9 }" S4 P, ^" Z# [, E" z
- #表示一个常量
4 v9 T: N. V5 D7 }; ^ - >>> math.e
$ o9 @% @! a1 Y# t0 i# W& K- E - 2.718281828459045
复制代码 # B# e B% ~: p$ x9 e
math.pi 数字常量,圆周率$ L' O4 k) s4 ~1 W, a% f
- #数字常量,圆周率6 g, ^$ L K* _ G; n
- >>> print(math.pi)
4 x( A* i8 I- w0 y7 E6 M# X5 U. s - 3.141592653589793
复制代码
1 v; H3 _ G) xmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 w. @6 D* B' d# \- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
7 J( Y+ ?) I$ c, U: f& B- C# l" o& x - ceil(x). k5 j& V& m( w6 w
- Return the ceiling of x as an int.0 ~; a; N# Q# ], B6 c8 o' A
- This is the smallest integral value >= x., g7 F( Z0 j8 q" h
- 4 v! d+ h! ]+ o& `" J
- >>> math.ceil(4.01)- [4 j; D9 W* b( k6 S
- 5
) f5 M( v& a6 j, o/ A( h* ~' K - >>> math.ceil(4.99)
4 m' g0 E" ?2 J; q5 N - 5
6 C/ ^% A# v0 a! J - >>> math.ceil(-3.99)- _, u# T8 V: Y7 ~; B: k- x, ^) ~
- -3% F6 {: b) o2 R4 R# c
- >>> math.ceil(-3.01)
- s% S* S! N3 h; k/ I1 r+ ? - -3
复制代码 P: p9 p( x5 ]: D# P" q
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身 p( Q6 ^: ~. K1 q5 [$ E
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身 S# a8 z' ]! N# ~% F* ]
- floor(x)$ F1 H! E8 g) G. j
- Return the floor of x as an int./ C. N- H; l7 H; U; i# f
- This is the largest integral value <= x.2 L' L5 _% @8 ^- n" p" S x
- >>> math.floor(4.1): E* D& ~) y5 D
- 4
4 \! z9 _, V. f' w - >>> math.floor(4.999)0 h g9 Q$ R" x, @' S
- 47 N. _: k7 K9 z" i0 H. r% [/ L
- >>> math.floor(-4.999)$ t( o( P) @; Q! D5 q2 R9 A% ^
- -5& ]+ {( s: y6 @
- >>> math.floor(-4.01)) J2 v9 [5 Q" ^
- -5
复制代码 X: z4 u. R/ v* s7 W+ H
math.pow(x,y) 返回x的y次方,即x**y
8 k+ ^4 t, A' w3 b0 x, C, A" V- #返回x的y次方,即x**y
0 a b) c. k- a; W/ _% X - pow(x, y)
; }5 H w6 p- L$ C" Y6 Y! l( P - Return x**y (x to the power of y).
) T0 _9 {( h& y- M+ T - >>> math.pow(3,4)
& W2 I, Q; ]- a' ~. d. ` - 81.04 o# e1 S& n1 W- {: ]6 i
- >>>
( w" a5 g8 y, s& r - >>> math.pow(2,7)
# o* _5 \* v/ E4 m* G/ a# B! X2 j - 128.0
复制代码
5 R% l3 [" _# `! t2 _/ Qmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* X( {7 I1 G$ G3 l: x- W
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). ^1 F0 o! T- W1 _7 ~( F
- log(x[, base])0 `' d0 p$ ?+ n' S" k6 s
- Return the logarithm of x to the given base.9 Y A5 z/ z# d) d
- If the base not specified, returns the natural logarithm (base e) of x.
0 k3 g G z& Y, `( Z* g2 G - >>> math.log(10)
# x- ?7 H8 E4 M" j9 z4 b - 2.302585092994046/ x* }5 i2 E' H9 k
- >>> math.log(11)
( x. c( O4 O! t. p! ]+ n - 2.3978952727983707, B6 K1 m+ [$ D; Z) i
- >>> math.log(20)
+ y X8 G4 S# a- l, b8 C( E4 ^, E - 2.995732273553991
复制代码
- I" S) ?0 F& d) g" t* @- p8 Imath.sin(x) 求x(x为弧度)的正弦值 t( d9 o1 X, M6 e; y! [& z- x
- #求x(x为弧度)的正弦值4 f& T# O G* f; U
- sin(x)& z# }/ W3 y* V% O2 G! N4 O
- Return the sine of x (measured in radians).2 {0 J8 B3 y* W8 K) ?: h5 H3 }) l* ^
- >>> math.sin(math.pi/4), p! u0 c% E0 I) D+ d
- 0.70710678118654753 p" d3 m: z! k- g; \7 {0 @
- >>> math.sin(math.pi/2)1 A6 L* i; C1 w: J, y1 J. i4 d
- 1.0; P7 f8 G4 \4 R& P) W
- >>> math.sin(math.pi/3)
# C3 Y* V9 Q1 x }5 ~ - 0.8660254037844386
复制代码 ; R% v, m3 m# H# C4 e" H; N* e
math.cos(x) 求x的余弦,x必须是弧度
: M( }, ?! i# e8 C$ I- #求x的余弦,x必须是弧度9 g/ M5 w0 E; h( m/ W
- cos(x)/ c% v" O: Z' T# w
- Return the cosine of x (measured in radians).+ i/ b5 H1 T& P+ T$ |
- #math.pi/4表示弧度,转换成角度为45度# ]+ t9 X% o: ~
- >>> math.cos(math.pi/4)
4 [. i7 D/ b: Q M K - 0.7071067811865476
6 J0 @7 J- f4 ^" f% D - math.pi/3表示弧度,转换成角度为60度
) y, h! _% o+ X- A$ k" p t0 a" n - >>> math.cos(math.pi/3)5 w1 p% {( S p% ~. W
- 0.5000000000000001+ C" Z6 ], g) N; B9 g, H$ x
- math.pi/6表示弧度,转换成角度为30度# F) j2 e! _, a; `& t
- >>> math.cos(math.pi/6)) z! `( T/ t( g8 N9 w9 S
- 0.8660254037844387
复制代码 & k( o. X2 L& M1 l9 ~
math.tan(x) 返回x(x为弧度)的正切值+ h; b9 f* T, v
- #返回x(x为弧度)的正切值
/ X- h" T; \! K6 q7 J - tan(x)
( a6 p1 Y3 |" q6 _8 c) S/ H - Return the tangent of x (measured in radians).& I7 R+ @/ y4 x/ y: x: v, l
- >>> math.tan(math.pi/4): v& r0 ?( w! Q# i' O" v7 ~
- 0.9999999999999999
: o% w; N5 k0 y0 D; V - >>> math.tan(math.pi/6)' b. L. U/ ?, A
- 0.57735026918962570 X2 q/ I$ N7 _) D8 E
- >>> math.tan(math.pi/3)
# a& B/ T; J3 \6 p - 1.7320508075688767
复制代码 / U/ [- c0 p3 \% ~
math.degrees(x) 把x从弧度转换成角度
" J3 S( H D: K8 |, k$ _6 ~- #把x从弧度转换成角度
8 T7 y8 K4 O# Y. q. Q0 _: w) Q3 ] - degrees(x)
6 t, {0 V$ s$ `! k! @* a - Convert angle x from radians to degrees.
9 v2 X5 ^' ^7 ?% Z3 { - # L$ }( m; `2 V+ p( y2 l3 R
- >>> math.degrees(math.pi/4)6 }% `9 }0 X$ f6 x/ J
- 45.02 H0 C7 W. q( z) @
- >>> math.degrees(math.pi)- m+ B5 ^$ J. c: _/ o3 Z- m) N7 y8 C9 o
- 180.0
* h) |& M& Y2 X' o0 \ - >>> math.degrees(math.pi/6)
4 c; t4 K! W7 o% e t3 @" B% n" }: r - 29.999999999999996! x$ Z; P( p5 W" \. O" u% u9 g' L: x9 ^
- >>> math.degrees(math.pi/3): K% z' c, ^( A0 F& j9 N% c
- 59.99999999999999
复制代码 , m8 |, e, l H1 {
math.radians(x) 把角度x转换成弧度
( O) j7 S Y9 V x! [1 E) _1 c- #把角度x转换成弧度
2 @: q1 z3 _* e6 D - radians(x)$ `( z& a, g- w) J( t" t; g
- Convert angle x from degrees to radians.
3 s' [: H; F7 H5 ]5 F E$ J - >>> math.radians(45): h [8 z9 T; z/ [9 W$ d% w
- 0.7853981633974483
. }+ I9 K/ F5 J6 ^7 W - >>> math.radians(60)/ E4 N# d$ o+ y
- 1.0471975511965976
复制代码 # A f8 _. {. d* I4 T8 T2 D& Q
math.copysign(x,y) 把y的正负号加到x前面,可以使用00 Q: a( u2 U( r" \+ `. C
- #把y的正负号加到x前面,可以使用0
3 r |8 P+ r8 @1 c! c% S - copysign(x, y)1 `. w7 S& ^% |# x
- Return a float with the magnitude (absolute value) of x but the sign
; ~9 V6 m: y' U0 v9 e - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, c0 F- p: q" T5 E- l: i% } - returns -1.0.
/ ~: v8 d8 ]3 o# w1 T: I. g' j& x - $ E0 r, F7 \9 i$ M* q" O% H/ W
- >>> math.copysign(2,3)
( @) @6 s( M1 h: i/ }, e - 2.0& O1 ?* c: N0 x! d- v
- >>> math.copysign(2,-3)8 B* I! M( o" ` ?8 g7 W# N
- -2.0
6 j5 ?: X* C% S' T4 f - >>> math.copysign(3,8)
+ c/ B, l: z! @6 P/ R, c - 3.0. V$ b9 q! p E0 [, c6 S2 C2 C
- >>> math.copysign(3,-8)
( R, P0 Y: u" q. p& |: t - -3.0
复制代码
9 t- _' ^8 p& V) Lmath.exp(x) 返回math.e,也就是2.71828的x次方
: Q- l4 J, P! U; o7 |# c' T* i- #返回math.e,也就是2.71828的x次方
; d$ ~: a4 G& h/ A; a' W/ o' x - exp(x)3 A j3 l" w" c5 {
- Return e raised to the power of x.
5 f8 U5 z: A) \* X/ X
8 N7 i. C S/ F' @# t- >>> math.exp(1)
. Z; A& M; f- V - 2.7182818284590459 D y' {3 w0 B7 w
- >>> math.exp(2)
: Y8 j6 N; _5 e _& u# D8 c - 7.38905609893065
) j) e: |; }, i% `% H; j3 \$ k - >>> math.exp(3)
7 ]- Y4 l# }$ c/ I. _ - 20.085536923187668
复制代码 4 D1 ~4 a6 G1 A G
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
7 N3 u# H: J( K5 G$ X3 v( [- #返回math.e的x(其值为2.71828)次方的值减1. s: A) _! V' E& z4 x
- expm1(x)
7 d0 W. O: A$ ]( N) F5 N) _: T! ?2 y - Return exp(x)-1.- M2 s1 J [% q7 s
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, r2 s8 Q0 @5 h7 g! r
% P0 X3 y6 k/ z! Z+ a- >>> math.expm1(1)
& e4 ^. `8 G; @7 ^% z' } z, Q - 1.718281828459045
/ i0 ?. F$ o) m - >>> math.expm1(2)/ m9 F* J0 K6 j" Q0 [ I& s
- 6.38905609893065
+ B" ^. K! C2 \" n! G y; R1 g - >>> math.expm1(3)8 m; o* ` @7 G
- 19.085536923187668
复制代码 $ g" p7 w) Y! J
math.fabs(x) 返回x的绝对值
" P6 _: }& ^5 j D; [* `- #返回x的绝对值
2 l5 z( {# Q; O5 y8 L8 D - fabs(x)
) y. F% D! Y ?$ m1 n - Return the absolute value of the float x./ I9 p0 J3 |9 @/ v
; B3 M" ~% k; U; T& v& n. m. r" T: `- >>> math.fabs(-0.003)
! N2 x9 h( N% U% o" D - 0.0034 Q/ ~! B# `" t
- >>> math.fabs(-110)- |7 Q) \5 h4 A8 V- ?5 d& O7 M
- 110.09 k) W+ c% p. Q+ @5 Z) M5 ?1 H4 C, \
- >>> math.fabs(100)8 s& [ _0 d ~6 {* o: o, l$ q
- 100.0
复制代码
3 N" Y- E4 @ G% _- h' pmath.factorial(x) 取x的阶乘的值
4 |* U/ ?1 C! M4 H! j8 O8 n$ B- #取x的阶乘的值
' [7 p1 A4 I- |) @2 R- C. N - factorial(x) -> Integral
* O8 ]# f' U. G% K9 ]# ?/ A - Find x!. Raise a ValueError if x is negative or non-integral.
6 b3 P$ Q$ C; q2 k/ { - >>> math.factorial(1)# C1 N9 I0 K8 n! v. A( i% K
- 1; \ J5 e2 U0 y ]
- >>> math.factorial(2)
" I( z z& Y: G" T! k - 2
* E# { G$ C" p& W7 l: E - >>> math.factorial(3)
/ ^, o+ K# Q9 F. e+ ]2 L+ j$ ` - 6
0 b7 b/ }1 u+ P& v% l - >>> math.factorial(5)
: ^" c* t. s4 `: `" [0 g; w7 | - 120 ~6 x/ g/ G ?0 C
- >>> math.factorial(10)
) {0 c- N: Q. F - 3628800
复制代码 $ R. v% b6 ~) J# Z
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
. {: H; l% T* P3 C- #得到x/y的余数,其值是一个浮点数7 [, L0 b1 @" t: |* T) O
- fmod(x, y)+ C+ D L! j7 c9 o4 ^6 T
- Return fmod(x, y), according to platform C. x % y may differ.* Z, s- }2 O! v/ t7 q- x: Y8 \5 i' e
- >>> math.fmod(20,3)
4 }. I! l6 {/ R2 Z+ y% |2 T' n - 2.07 |5 l! N/ o/ O/ C3 Q- l
- >>> math.fmod(20,7)
4 U5 g9 a2 @% `# n- x* r$ c/ I - 6.0
复制代码
- {: f% R& W4 o+ e' |* R, ymath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围# i9 ^' H6 v# _2 v) t. O4 N. d
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
, {9 N- d2 [$ w, u; b - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 [( X& p* @/ E* J
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
9 L6 h( u1 _ C9 B3 Q2 G* l. c - frexp(x) \: Y7 r- l% V, I
- Return the mantissa and exponent of x, as pair (m, e)./ i3 {5 y6 h! U4 @7 i3 u
- m is a float and e is an int, such that x = m * 2.**e.3 \8 K. w5 r1 b% ?2 m: P1 z) a
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
( ^& i% ? t8 M - >>> math.frexp(10)
! ~$ V( d* m* v) ]/ _7 M9 K - (0.625, 4)
5 c5 v2 |" T* k* e, _. @2 R! \ - >>> math.frexp(75)+ {0 d0 k% Q. Y" S/ y0 h
- (0.5859375, 7)
* \/ {9 j, J. M% a9 N- |+ Y' o& a) }# t - >>> math.frexp(-40)
/ S3 f) q2 d. } - (-0.625, 6)
* |7 K+ Y5 c$ w: \7 I9 V - >>> math.frexp(-100)
* w4 I8 D0 L& _8 c/ G - (-0.78125, 7)
8 u+ D- `1 {/ y( C, O1 Z+ a6 J - >>> math.frexp(100)8 d& |& X% ~0 `4 H( I* [# g
- (0.78125, 7)
复制代码 9 n" z. z7 \/ H8 p0 X# r
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)( A$ T7 R5 ]/ F7 Q+ `
- #对迭代器里的每个元素进行求和操作" v0 h" y) i& S5 y2 m
- fsum(iterable)
i i/ p$ s |1 }/ J - Return an accurate floating point sum of values in the iterable.
: [2 @, w+ H: X* v - Assumes IEEE-754 floating point arithmetic.0 [& i5 y4 }; _
- >>> math.fsum([1,2,3,4])* C V& ~3 F4 x- W" J( N
- 10.0
" b' r; s5 \: X5 G - >>> math.fsum((1,2,3,4))
/ m7 E3 t$ I, n& B - 10.00 ?6 Q6 C7 t' _' M4 g
- >>> math.fsum((-1,-2,-3,-4)); R0 B+ X' l# F) x z1 Q$ B6 Y
- -10.0 S( c" R6 V( s2 E
- >>> math.fsum([-1,-2,-3,-4])5 n4 s. U5 V8 t4 U& |7 K8 i
- -10.0
复制代码
5 h$ H9 F' T9 n w! L" [math.gcd(x,y) 返回x和y的最大公约数( v2 K* F2 t# i/ }7 f
- #返回x和y的最大公约数
5 e" S$ F3 o+ W* J( ?. Q( t1 b - gcd(x, y) -> int. I6 B8 T: M& F5 J. u. U
- greatest common divisor of x and y
4 a" U( N0 |% v, P0 ~" E - >>> math.gcd(8,6)
; V- `- A) a; h: R$ l - 2
f2 _* \* q3 j7 P0 O% ^: ~ - >>> math.gcd(40,20)! G2 s; H3 h- k- w) S/ S# v
- 20
( t4 Q( f; v! E6 B - >>> math.gcd(8,12)% y2 \8 R h J6 x9 n9 ^& g
- 4
复制代码 $ i: T0 n: m" V& i2 K5 @
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False% j& h6 n0 l' q0 g: ?
- #得到(x**2+y**2),平方的值# W g/ m3 Z9 u/ S% Q
- hypot(x, y)
1 r4 {2 `) x2 d- ` - Return the Euclidean distance, sqrt(x*x + y*y).1 S8 z; m( J( W) m
- >>> math.hypot(3,4) V( p# X0 M1 U% N7 e6 L1 N7 j
- 5.0' X9 N6 m( h$ |& F) d0 Q, L
- >>> math.hypot(6,8)' H( n8 u& Y2 c+ u
- 10.0
复制代码 0 I. }) s' P6 o* W7 ^' Y
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
4 m# J; Y9 P$ ?( h; C- #如果x是不是无穷大的数字,则返回True,否则返回False" C) w2 ]7 ^$ V2 p$ E- F- P
- isfinite(x) -> bool; o( ` n3 z/ |# D3 ~! y
- Return True if x is neither an infinity nor a NaN, and False otherwise.
4 {4 a0 ~1 X# K& Z5 } - >>> math.isfinite(100) n% ~" N5 _8 M" a7 J
- True
0 ]" w3 ?! o+ o$ Z! v- i - >>> math.isfinite(0)
! _, O$ S3 i7 v Z8 t4 j - True2 N0 x8 ~, T' S6 {. _
- >>> math.isfinite(0.1)
% h. m4 s8 d+ S6 ]' G8 N - True
( t( `5 }) H, ^8 { - >>> math.isfinite("a")
/ U3 B2 W8 w4 {, |1 V& K - >>> math.isfinite(0.0001)
1 p# @5 f* P3 n( n9 e& m* _ - True
复制代码 + ^+ L9 ~1 q0 E8 p/ y
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
: e) t* f% W, v6 i$ w: o- #如果x是正无穷大或负无穷大,则返回True,否则返回False
$ Q; B6 T9 [: z, ]# w" Y9 d) S% s e - isinf(x) -> bool
5 Q' w- a# m) | @) [ - Return True if x is a positive or negative infinity, and False otherwise.
9 m1 g6 G/ j, l8 a5 ~% g- o - >>> math.isinf(234)
6 B4 ]1 C0 v+ s8 [ - False) z8 D% B1 Z u" \1 d
- >>> math.isinf(0.1)
7 W& M+ ~- Q7 y7 Z - False
复制代码 & ~1 R% p: J, n% \7 d8 J% \
math.isnan(x) 如果x不是数字True,否则返回False, H0 {" W! n" Y- U
- #如果x不是数字True,否则返回False
+ ^% y) y. d8 @+ H - isnan(x) -> bool" N, I8 k# ^3 q& Q" F" H
- Return True if x is a NaN (not a number), and False otherwise.
( [# ?2 G" c8 ` - >>> math.isnan(23)/ u$ y, s5 b- D) d5 }* Z
- False
8 j9 U/ e* `% ^! F/ R' R - >>> math.isnan(0.01)4 [4 n8 V- ~/ _' S# b. r& e. I' n
- False
复制代码
. Q4 }! e6 r$ L5 Fmath.ldexp(x,i) 返回x*(2**i)的值) u l e% D Y; ]% e
- #返回x*(2**i)的值' U+ x) d; [4 ^" \
- ldexp(x, i)0 ^1 D+ B, V5 ^
- Return x * (2**i).
2 a2 m S# \, U s5 H: M - >>> math.ldexp(5,5)! B+ Q2 S$ |4 r4 x8 s
- 160.05 l8 ?: b# W6 H+ F. F; O7 [3 v: B
- >>> math.ldexp(3,5)5 o& d( Q) u$ l& d8 |# \; U8 Q7 C
- 96.0
复制代码 & \) d) w9 [) s2 q @$ u0 U9 B
math.log10(x) 返回x的以10为底的对数% l- E/ O, D" O
- #返回x的以10为底的对数) o) f9 I' `( ^
- log10(x)* w2 J" X6 `$ d! i
- Return the base 10 logarithm of x.5 i8 q# L! M7 x' ^ V6 y
- >>> math.log10(10)
: s8 e1 Y0 k8 C; I( r; h - 1.0 `& q! G$ F% t5 L7 B; y
- >>> math.log10(100)' ^7 I* P, J# h% Z
- 2.03 T7 t( ~# A( ~6 W1 x, j3 n
- #即10的1.3次方的结果为205 K8 F/ m. i2 \) C4 C
- >>> math.log10(20)
- x" b9 N: R5 k. j9 Q/ h - 1.3010299956639813
复制代码 r0 N! d' m1 X0 ~# I
math.log1p(x) 返回x+1的自然对数(基数为e)的值
4 o( v* S* p# y, d8 C/ v- #返回x+1的自然对数(基数为e)的值+ @8 U$ T; K% Y$ o
- log1p(x)
. x5 X) q' j0 ]; |5 e - Return the natural logarithm of 1+x (base e).
! o+ F3 J) i5 { - The result is computed in a way which is accurate for x near zero. ?! b) m0 p/ G( W7 n2 s
- >>> math.log(10)9 E/ u8 A. }2 D; Z. r
- 2.302585092994046& o! U# {# n4 Q" w; I( T
- >>> math.log1p(10)0 @3 D: B3 M) f3 m) s5 n9 u
- 2.3978952727983707
) `! U! y" \; t - >>> math.log(11)6 n+ v" v2 F2 @6 C" c( y
- 2.3978952727983707
复制代码
. @$ J2 R% f6 C( o' ?+ lmath.log2(x) 返回x的基2对数7 }6 [: x% G# I/ M7 w9 k! K* j; F6 u
- #返回x的基2对数& f; \0 D* ]" l) S" l
- log2(x)8 h( R% f6 C+ K L9 f/ A
- Return the base 2 logarithm of x.0 ^7 a) \ G5 S! c+ d
- >>> math.log2(32)
0 G6 F/ |' P; t3 F" r6 z9 @; o9 y - 5.0
k0 I. q% V% Z% W4 r! r o9 x9 n - >>> math.log2(20), i* ^+ n& z K2 F6 e% O" e
- 4.321928094887363
% x4 T( [& R. M5 {, R) \ O - >>> math.log2(16)
; q* A- m, G& r - 4.0
复制代码
: o/ Z9 X8 ]& r4 k6 t) R9 vmath.modf(x) 返回由x的小数部分和整数部分组成的元组
% M" K. U: N' O- #返回由x的小数部分和整数部分组成的元组; ~% Q' c) n7 e m! N9 t/ D* Z, Y3 H
- modf(x)
. B; ^% E& i- t( n - Return the fractional and integer parts of x. Both results carry the sign
8 s1 M) ^5 V! T9 R2 g1 }. \0 C* x0 a - of x and are floats.
/ s6 a) @) a$ U8 j - >>> math.modf(math.pi)$ O/ l; M, c2 m8 _$ U1 h
- (0.14159265358979312, 3.0)6 B: }! @ u9 b( e
- >>> math.modf(12.34)+ R% y9 p5 r: y
- (0.33999999999999986, 12.0)
复制代码 ; }% `4 F% W7 y& [. O; {
math.sqrt(x) 求x的平方根, j8 y3 B: F+ H7 n4 r$ j- ^# s6 t: I
- #求x的平方根1 Q: k) J) V9 g! G/ U
- sqrt(x)
, A/ V$ K4 p9 Z4 ^* Z - Return the square root of x.& {$ ] V' u* Y7 U' K3 |
- >>> math.sqrt(100)4 T9 K0 u/ u3 Y, ]9 q( R6 B. k$ n
- 10.0+ w1 i) a! I- ^5 a2 P8 q. E5 X
- >>> math.sqrt(16)5 j0 X: O7 u' m6 @
- 4.0
0 R# v' w& U: \ - >>> math.sqrt(20)
7 R m: ]* \! f1 i8 g - 4.47213595499958
复制代码
/ G8 o6 q4 D4 C4 l6 @math.trunc(x) 返回x的整数部分- #返回x的整数部分3 _9 w ~; {; Y7 R% d
- trunc(x:Real) -> Integral
q& j9 n# X/ \: O" `/ s- l A - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.5 Q/ N# b/ |9 A. C6 e' w+ a# p5 I
- >>> math.trunc(6.789)
& h/ v' P$ X5 G - 6
; v* a/ K l1 E0 H7 u - >>> math.trunc(math.pi)
$ K$ n2 c& \' X! E: C - 3
3 N7 S! ]4 H+ ` - >>> math.trunc(2.567)# s% t2 {6 q, ?* x+ `: L; G
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|