马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ n7 Y, z0 \6 O* c9 u
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& K) p) l) p" u
% O% c( U* R& K
方法1:
- [4 B$ c% {5 T. U2 u, v* T- >>> import math
6 v0 T! X3 s0 D% N - >>> math.sqrt(9)
& }: L" a5 M b - 3.0
复制代码 方法2:
3 S% @# o0 Q8 T* t" o- >>> from math import sqrt
" F/ ]. t4 X0 z6 _( U2 I% p - >>> sqrt(9)
+ I4 x- {, A. T- q/ r - 3.0
复制代码
8 Q4 G0 N4 @/ {* e
2 ]. E! {& I$ Fmath.e 表示一个常量4 `) j4 A$ x( R; L
- #表示一个常量
1 u$ @" c4 P6 ^, P - >>> math.e7 f# z3 B; p4 _5 ^
- 2.718281828459045
复制代码 % R+ g+ _) ]* ^" s# Q: a1 {
math.pi 数字常量,圆周率2 F7 Z8 _; ]( }; u* ?
- #数字常量,圆周率
. O$ t, a2 M$ }- j) @ - >>> print(math.pi)0 Y! r* c3 \. _8 M
- 3.141592653589793
复制代码 3 D/ m3 {9 [/ C- A$ i$ h3 M
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, M3 N6 l7 x7 Y# V# x# X8 w, b: j8 Z
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
' v3 w, ?/ f* [. h - ceil(x)
) z) q8 R7 k' O* t1 C - Return the ceiling of x as an int.
. p3 H/ b5 U2 G% I( L. W - This is the smallest integral value >= x.
& r" E, b7 F0 A - 1 k: b9 U) d1 ^% `& o, n. E! m0 G
- >>> math.ceil(4.01)
7 v: v$ P( J0 x3 R( H' d. m - 5
; m0 I; f5 U+ e5 c1 G" ~' |; g - >>> math.ceil(4.99). f, C' l7 B1 X7 R& d- w
- 5$ [9 Z$ L# ~- ]: x' y
- >>> math.ceil(-3.99), @" ]' H! I/ ]: Z. A6 m, X
- -3
& G+ W' U9 N) d - >>> math.ceil(-3.01)
- r5 |$ k0 z: o, A - -3
复制代码
{5 x4 y# t8 x0 E0 pmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ V9 k3 u& `3 T
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
2 g$ e- h) H# Q) g, i9 v- o - floor(x)4 ^$ j+ I5 f, f
- Return the floor of x as an int.$ M, Q0 a- }: D
- This is the largest integral value <= x.
V. k# `4 Z5 _2 I2 k/ V - >>> math.floor(4.1)
4 g. p% S& v" ^ - 4
% H# J' a$ {! s4 z- \ - >>> math.floor(4.999)4 h% r) t7 J6 X1 ]. {, d* A
- 4
2 w8 x' k; y: w- m2 S% W( C - >>> math.floor(-4.999)
2 a" [9 l# C. B - -5- s+ A; t9 z6 ?. g* w
- >>> math.floor(-4.01)
5 y+ e- G% K; x - -5
复制代码
$ a- {$ _8 T' q; P, @( T; Rmath.pow(x,y) 返回x的y次方,即x**y
; ]0 B7 r+ i+ u- #返回x的y次方,即x**y* J' o n: c0 o- @0 e. @8 h
- pow(x, y)
! R: {- l2 h- N7 ^/ H! m; X - Return x**y (x to the power of y).7 {5 X4 q% v) D; s8 ?/ A
- >>> math.pow(3,4)
8 w* }: Q: Z- J2 d N) Z* q - 81.06 L$ h! M+ ~) v* n) N6 L
- >>> . L9 j+ T8 z' I" u" ]$ r: i1 f
- >>> math.pow(2,7)$ Q, c( ]6 K8 y. J
- 128.0
复制代码
" |' r) }) `# @5 T3 smath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ `) I+ D) |% C: E( ]# n' ~- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% K0 T% X/ p1 M4 V8 G4 B+ B
- log(x[, base])
# m1 Z7 a- p% O! ^# B - Return the logarithm of x to the given base.4 |6 g2 }( S j9 X# }! l
- If the base not specified, returns the natural logarithm (base e) of x.! q# w [9 u5 b+ @6 r2 L
- >>> math.log(10)
/ H% _$ D1 S3 y3 x1 K/ }3 v - 2.3025850929940460 q6 c9 ?" h7 p. o8 W* P) o! B8 o
- >>> math.log(11)
( o" |1 ~6 k {, f5 K3 Z2 W - 2.3978952727983707
. C2 q, ?* x1 m$ }% K - >>> math.log(20)
( ?) Y5 U6 A) H4 j+ o2 e+ d - 2.995732273553991
复制代码
% c- h9 v% i: `. X% G- _: Wmath.sin(x) 求x(x为弧度)的正弦值
7 k% U( G! W/ N) V- #求x(x为弧度)的正弦值3 h. W2 t& O( \8 d2 j
- sin(x)4 s! _# m8 a1 N
- Return the sine of x (measured in radians).; t/ j& a; q9 }- [; ` q$ n f' m# _( ?
- >>> math.sin(math.pi/4)0 c- i6 `3 E$ ?
- 0.7071067811865475
$ O8 r& o% `+ W: L - >>> math.sin(math.pi/2)
; [/ ?6 w8 ^2 f/ r/ ?0 h, J/ Z5 j - 1.00 o: t3 i* b" r# `. ]$ j, C
- >>> math.sin(math.pi/3)
" n0 @& h$ f. M1 M - 0.8660254037844386
复制代码
" a, P6 H& |$ `6 _: B4 fmath.cos(x) 求x的余弦,x必须是弧度& N! w6 k# S% X- A4 N# _
- #求x的余弦,x必须是弧度9 v1 ~% x' c( D5 U- z& ]$ q, W
- cos(x)
4 _! l, k+ F, f. G& b6 c* H- u - Return the cosine of x (measured in radians)." i0 v* k! P! H/ J- `. S2 U
- #math.pi/4表示弧度,转换成角度为45度
, _& {9 B& `# e/ p6 y. O - >>> math.cos(math.pi/4)
# \+ X& k$ I1 G5 z+ ^ - 0.7071067811865476
2 Y5 C& M6 X! R- G, _" L- F. F - math.pi/3表示弧度,转换成角度为60度2 u5 ?8 }! k& D! t0 ]& J
- >>> math.cos(math.pi/3)
$ G! y- E1 y; }4 Q/ h - 0.5000000000000001/ J9 b6 }% i) i8 n3 M$ |
- math.pi/6表示弧度,转换成角度为30度
5 Z) ?# c: W3 e - >>> math.cos(math.pi/6)
3 V6 e* z4 d6 F - 0.8660254037844387
复制代码
$ P. V+ p z# I {' Hmath.tan(x) 返回x(x为弧度)的正切值
8 x5 j9 P5 R! C* m5 b- #返回x(x为弧度)的正切值# e* o/ }. P# O) r6 f& B' F
- tan(x)
- c# X% ?! v$ M' o4 @ - Return the tangent of x (measured in radians).
1 H& B$ }# f0 \% B+ @- W; u" N - >>> math.tan(math.pi/4)
1 c& B {. S- j/ P' B2 T: C - 0.9999999999999999
3 V G1 Q- d9 W+ p5 }1 X - >>> math.tan(math.pi/6)
& Y5 M. [2 A( l6 m1 s2 L* \ - 0.5773502691896257
( _, @ g# C" s8 A6 b+ n. [4 d' J - >>> math.tan(math.pi/3)6 i* p/ M3 C" Y: h" d4 [; Z
- 1.7320508075688767
复制代码 6 o( Z# K% R( a# r$ z+ a
math.degrees(x) 把x从弧度转换成角度
8 Z* e. f, z; V' B( [, D! i- #把x从弧度转换成角度1 k( z- i; o" f* R. Y
- degrees(x)7 h: h5 x% \# `3 Q
- Convert angle x from radians to degrees.
. o' G5 D, ]7 T1 D! U# \% z, g
! k6 U, Y7 c: Z X2 {, R# r0 e# z- >>> math.degrees(math.pi/4)
5 t5 U! A4 p1 o6 r& ^ - 45.0
2 S2 _( i) h; _2 X( K; h - >>> math.degrees(math.pi)7 d5 s j% N' l! A" f6 }; M @
- 180.0. c% R) ?+ a# z: Z9 ?2 q
- >>> math.degrees(math.pi/6)
- X0 ~, k7 R% q9 B8 b4 H$ p7 w4 L( F - 29.999999999999996' o. [, g! l& }( a+ B
- >>> math.degrees(math.pi/3); T( @( ]3 e9 ]3 M, x( w- p
- 59.99999999999999
复制代码
: X5 h' ^& }0 i% l* X9 qmath.radians(x) 把角度x转换成弧度
7 e& c' @* E0 i$ R4 B2 n* }- #把角度x转换成弧度5 U# l% L3 B& t, w3 i% F
- radians(x)
8 H) I9 S6 r" E6 f- b/ u - Convert angle x from degrees to radians.8 B" U8 k. k6 |( A+ Y: c4 w
- >>> math.radians(45)
. j" a9 M( {5 v; A. \- t - 0.78539816339744837 Z* r6 F( E- o
- >>> math.radians(60): S# e' D. p! C$ E6 ] [
- 1.0471975511965976
复制代码
' i+ H! c! r9 W4 t2 g j& Wmath.copysign(x,y) 把y的正负号加到x前面,可以使用0# i% [1 g5 n- ]7 H
- #把y的正负号加到x前面,可以使用0
; ^7 |' k3 N% K- z2 }3 y* L8 i4 g' K - copysign(x, y)
! ~: {5 G& d, {9 t/ a' w: Q# N- b - Return a float with the magnitude (absolute value) of x but the sign
6 t4 }$ W2 L0 y) T& @ - of y. On platforms that support signed zeros, copysign(1.0, -0.0) % g' @/ v2 t7 F! J! m; A+ X
- returns -1.0.9 f3 e9 f! R0 R! `$ C: c; `
- 5 \: M' R7 d4 c
- >>> math.copysign(2,3)
# _$ w; k" X7 a' m' f - 2.0
1 V; A; b, }# P5 b% h5 P2 q - >>> math.copysign(2,-3)
7 V5 R1 E4 `( t, J - -2.0
5 [2 e( x7 z, j4 j - >>> math.copysign(3,8)
0 w, M( m1 q! q! m - 3.0; o& h9 L# R" O P, q
- >>> math.copysign(3,-8)0 K& d7 W; j7 s! p3 f
- -3.0
复制代码 6 I, n2 I, C$ Z
math.exp(x) 返回math.e,也就是2.71828的x次方
{$ _: F. f1 P- p0 M8 l0 B- #返回math.e,也就是2.71828的x次方
) f0 b0 d/ W" e. G7 g$ [0 P d - exp(x)& W4 q# e2 q2 z8 a) Y# d# u
- Return e raised to the power of x.
4 Z T% b% L" K( v s/ }3 Z
% f# y" E. F' e8 f, }2 I J3 k. z \- >>> math.exp(1)! Z( U) J& d4 d/ f$ H7 Q
- 2.718281828459045
- ~- \! ^" I/ t, i - >>> math.exp(2)1 K* @/ F5 {% q0 j
- 7.389056098930657 p$ @1 @% e) ^5 C2 r" e, G
- >>> math.exp(3)% m2 |4 B8 b% o$ C1 t
- 20.085536923187668
复制代码 4 O( m H c! g J( z" S9 }
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减16 [- C. W5 J1 j5 o( K$ q" M
- #返回math.e的x(其值为2.71828)次方的值减1% \$ }: a( b; K6 @8 y. m5 D/ J
- expm1(x)8 I L* O% t5 R: k* }; d" G
- Return exp(x)-1.! A0 J# r) F: p9 f: z
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.% y7 g7 P0 m' M1 g
4 W) x: Q8 p! s% B! w) e- >>> math.expm1(1)6 L( ~+ I" i1 s, w
- 1.718281828459045
8 D9 ^: [$ `5 M8 ~- N: n' G - >>> math.expm1(2)7 Z, k2 Z7 d0 v# _
- 6.389056098930653 m' x. }- Y' y9 s% \8 @
- >>> math.expm1(3)
: g! k7 k9 s" h3 ~1 ?% R; y - 19.085536923187668
复制代码 . W5 n# J- T- J. O5 K% R, Z* Q
math.fabs(x) 返回x的绝对值
c* W4 }+ _0 N) e- #返回x的绝对值; M& o: C6 d3 V
- fabs(x)
- {9 C* e2 h! s: U - Return the absolute value of the float x.
/ O) ]( D( W8 k - 1 d. w& [: ~, S8 o: ?
- >>> math.fabs(-0.003)3 O: I% Y/ V# i/ \
- 0.003% [0 [: f* G7 o, I6 i$ H( L& m
- >>> math.fabs(-110)
( _/ B$ l5 u* u- l& l5 F2 n - 110.0% ^) Q4 I% G- \% e, G) g5 z; Y
- >>> math.fabs(100)- I' q: P( f9 U( [; @3 i
- 100.0
复制代码 : h3 j G3 V4 K* p, m
math.factorial(x) 取x的阶乘的值$ Q; ?) n$ ~" E& g
- #取x的阶乘的值! M8 ~7 S+ `6 X
- factorial(x) -> Integral
. g8 a/ p$ D$ D! P - Find x!. Raise a ValueError if x is negative or non-integral.
2 E% Q. s, |0 i - >>> math.factorial(1)
1 C, v2 C/ `( \# _' S# X& ? - 1
- v- }- i/ j& w) @ - >>> math.factorial(2)8 H% s. h! M, y9 ]' }9 U
- 2
- t& m- a& }& u7 W( Z - >>> math.factorial(3)5 `" k/ C* ~9 ?( ` G% a0 O5 P
- 6
' e: s$ @8 x w. A5 [1 @ - >>> math.factorial(5)
6 J1 u( A$ L3 f. G- L) f* ? - 120
6 u0 h/ y. R5 {2 F9 O2 M0 e - >>> math.factorial(10)) g k- P4 L% F; K; x* |0 _
- 3628800
复制代码 2 w" c) S- `" r) A; N; E- K& \: K
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数+ q* c H/ h/ ?/ O2 ?, O
- #得到x/y的余数,其值是一个浮点数
3 j# n4 h5 T& R1 X: [# V - fmod(x, y)0 [ c! `9 P9 v
- Return fmod(x, y), according to platform C. x % y may differ.
( Y) H0 r+ K9 S; C - >>> math.fmod(20,3)
1 W5 d5 M6 ?; Y- U - 2.0
, V6 G- _( `+ M U - >>> math.fmod(20,7)# Q7 @! X* W' K7 ~
- 6.0
复制代码 & U: h0 a' p9 }( b
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 b* l" G! b: A/ b2 z
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. ^, P7 C1 L+ l% _' v3 k - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 K- k3 h8 l4 B. c% f4 U9 c
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
0 U1 u4 e% P8 P( G* K - frexp(x)
1 D! c) H2 N' ]+ R7 {5 B - Return the mantissa and exponent of x, as pair (m, e).+ k; t$ Q0 \7 p- e6 e% y
- m is a float and e is an int, such that x = m * 2.**e.
o3 O$ O+ V& K- L" O' ?* U9 p - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.$ D/ R- i$ K! M% R
- >>> math.frexp(10)
5 ?. ^& {4 B2 g B) O - (0.625, 4)& P6 Y+ F3 Q0 W2 ` |3 O
- >>> math.frexp(75)6 K6 M% R8 c$ f" u( [5 n7 d
- (0.5859375, 7)1 I% \* S W8 Z
- >>> math.frexp(-40)' @) V: t9 J3 q6 S
- (-0.625, 6)
; X5 m6 F- y' ]/ @; K - >>> math.frexp(-100). D$ ?# V2 U! M% `( R3 e
- (-0.78125, 7)
- y: j! C/ \! Y! Y) \2 n - >>> math.frexp(100)
0 U* s* h0 f) [* c1 s5 z - (0.78125, 7)
复制代码 6 F0 q# {0 @% H7 R2 s( q1 n
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
) n' h" D1 W: t/ K) R- n, {. a1 I- #对迭代器里的每个元素进行求和操作
$ O+ @+ K8 j& h) B% x - fsum(iterable)/ d( {0 F) R. b6 V$ s
- Return an accurate floating point sum of values in the iterable.- C" Z# T' Y; z. @. _
- Assumes IEEE-754 floating point arithmetic.% b+ u* M0 S/ Z1 K5 @& H6 O
- >>> math.fsum([1,2,3,4])5 C! U: K- {. W* i
- 10.0
( W- W( `. X2 E% y- l - >>> math.fsum((1,2,3,4))
% j! q3 e- _1 O( K - 10.0
: D0 J* W0 U4 f& q. o8 T& o - >>> math.fsum((-1,-2,-3,-4))) J1 w1 Z0 Z) k
- -10.08 L3 f" E* ~( b9 f; p+ X$ j
- >>> math.fsum([-1,-2,-3,-4])
& g5 J( M% h4 W/ K+ C- f9 a& ~* E& ] - -10.0
复制代码 / P; |$ z) K) x. k* I% y
math.gcd(x,y) 返回x和y的最大公约数
! c) f+ i5 y% Z; E3 n0 _& i- #返回x和y的最大公约数. H( O# @6 ?9 j$ d2 a8 A6 J
- gcd(x, y) -> int7 O% t/ D0 B; Q. K3 C0 I
- greatest common divisor of x and y/ F+ g X6 o& \ N: Z5 J
- >>> math.gcd(8,6)
& z6 n* | u9 ~% _* H' r, o - 2
$ k; t; b- M8 L5 d d - >>> math.gcd(40,20)
" Z( j2 g* ~. U# F; F% C - 20- D+ O3 e, F8 @) h
- >>> math.gcd(8,12)& N- n! G/ g) ~" s V4 n
- 4
复制代码 % e$ c0 U* `- a
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
1 t( {" d( K8 R8 |' W0 d$ ^- #得到(x**2+y**2),平方的值 e" ~- o6 Z* M) C
- hypot(x, y)
! I% c3 W) C7 T5 J+ }% J9 A! ] - Return the Euclidean distance, sqrt(x*x + y*y).5 s+ Z" d# t3 }2 w9 @
- >>> math.hypot(3,4)
3 B3 B1 ^* K& m- z! T, w - 5.0+ t/ @+ \5 w' d/ G! `* h8 G
- >>> math.hypot(6,8)
# B) c! M; W/ m4 g9 L" b - 10.0
复制代码 # Z: G, `+ q- j
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
& k0 y2 r" G. o, t7 v- #如果x是不是无穷大的数字,则返回True,否则返回False( r6 S+ `, o! E: \: T# ?
- isfinite(x) -> bool8 t( v1 ~5 a' l$ A4 u' h0 M
- Return True if x is neither an infinity nor a NaN, and False otherwise.+ \$ _: e0 A+ W
- >>> math.isfinite(100)
$ s1 d6 N& a- B& E& b& x% d - True! O# }9 F6 `7 J% m @
- >>> math.isfinite(0)8 x% u7 E) R( G" O% N- [
- True c+ k7 F* {( {; j: J* B, {& `
- >>> math.isfinite(0.1)- t7 a! s0 V9 \3 n2 K2 N3 b' l$ _% |& D
- True
& r2 B& X; Q/ `" q/ ? - >>> math.isfinite("a")
3 m/ S" y7 s0 U - >>> math.isfinite(0.0001)
, h% l) _- d" n, O7 p0 o l - True
复制代码
: a9 s8 g0 }/ r, _# n: Rmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
( m1 t5 R9 ?& Y, b& f- #如果x是正无穷大或负无穷大,则返回True,否则返回False
8 ^5 W8 b$ m% Z3 P% T$ V0 u - isinf(x) -> bool0 `5 X. T) F- m6 V0 ]) f# F
- Return True if x is a positive or negative infinity, and False otherwise.) [4 ~ _( l* ^# [& S3 _/ t
- >>> math.isinf(234)& k' j S+ F! D% ~( i! l
- False- @( `1 i# M9 t" S# s$ H* `/ A, Q
- >>> math.isinf(0.1), t3 G$ @; Q4 B1 x0 Q- y
- False
复制代码
% K/ ? l8 y( }! y& W: Umath.isnan(x) 如果x不是数字True,否则返回False! Y& E' e+ N, t0 R5 s }0 w
- #如果x不是数字True,否则返回False0 b3 r) c4 ~3 b+ Y6 o
- isnan(x) -> bool1 ^7 K& s& d( Z
- Return True if x is a NaN (not a number), and False otherwise.
5 T0 Z- F* C: V& a$ Q- c - >>> math.isnan(23)9 M3 z7 Z* P# x5 c5 i$ `
- False1 ~) Y- c! a# j J+ G: k
- >>> math.isnan(0.01)
: e. ~5 f# Q( C7 e7 n: v - False
复制代码
8 D' D0 a; O6 ~1 f; q6 @" w: s. Ymath.ldexp(x,i) 返回x*(2**i)的值
9 i5 l; x5 k8 q0 p# j# x- #返回x*(2**i)的值
: _, t' p# R. V - ldexp(x, i); T5 k4 s. ?6 i+ E/ H! j
- Return x * (2**i).0 l+ w4 x0 w2 J! t. d
- >>> math.ldexp(5,5). l( j6 R: y- q# A
- 160.0
! _5 W0 {! \) N7 ?# j+ }3 A - >>> math.ldexp(3,5)
4 l3 o" S, D" G/ }% v - 96.0
复制代码
, T& C- V/ Y: \- _math.log10(x) 返回x的以10为底的对数
' s9 H. R4 c1 g, E; y- #返回x的以10为底的对数
7 {$ E- w, ?" `+ h - log10(x)& h1 M4 S9 @5 p! ?* n* f1 Y
- Return the base 10 logarithm of x.- Z" ?0 t" m7 G2 R3 s
- >>> math.log10(10)1 ~# f# n# g8 ?6 S3 ?' j+ z
- 1.0
) ~& }) d& J# Q5 }5 w* x - >>> math.log10(100)5 W9 b! t2 Y. T- @
- 2.08 m& H9 S) {5 k e
- #即10的1.3次方的结果为20/ I2 k1 Y) x4 ~9 K( Y
- >>> math.log10(20)
8 F0 R$ I% V9 N# d4 @; O - 1.3010299956639813
复制代码
1 O1 O. }( S8 ?6 l: J; Q$ J: nmath.log1p(x) 返回x+1的自然对数(基数为e)的值
& R* P ^4 t# r5 e# O" M- #返回x+1的自然对数(基数为e)的值
' `% H) o& E* S, J, [$ B9 g" b - log1p(x)4 z; Y) Z3 f: _
- Return the natural logarithm of 1+x (base e).# c3 G9 l; x; W" k8 x8 H
- The result is computed in a way which is accurate for x near zero.
- q4 z$ o9 y: m0 ^ - >>> math.log(10)
! V* N+ b! j) C0 j$ w - 2.3025850929940468 r& F( {, i& Q! j/ ]
- >>> math.log1p(10)
& `9 [ C( l1 ~9 f - 2.3978952727983707
9 A7 Z; k: g0 g( t( { - >>> math.log(11)2 W! [! m a' v" z7 h6 F
- 2.3978952727983707
复制代码
6 e) w' M9 R; cmath.log2(x) 返回x的基2对数/ B5 R+ C, Q1 L* u+ T
- #返回x的基2对数% g" u4 w: R: y
- log2(x): j; q% f, x/ X; t% Q4 C
- Return the base 2 logarithm of x.% O- {8 K- x( \+ }# H5 X2 `
- >>> math.log2(32)
- _! d) [, j9 J4 L - 5.0
/ N# e0 b, W9 V- O1 w - >>> math.log2(20). W# P `. _0 M2 W
- 4.321928094887363
9 Q4 j0 B& Z6 c( A6 l - >>> math.log2(16)) m) f. A% k8 H) z
- 4.0
复制代码 - S! a# o0 M) O/ ~* {/ q
math.modf(x) 返回由x的小数部分和整数部分组成的元组! E9 T) K* J, q4 ^ S
- #返回由x的小数部分和整数部分组成的元组
0 s& f* W) d' _" c$ B - modf(x)7 X( N5 Q q6 P1 K# v1 m6 R# Y$ f
- Return the fractional and integer parts of x. Both results carry the sign; }! E( L; K. F7 p: q8 h
- of x and are floats.
, q& n! e) I0 ?( ~6 B3 G - >>> math.modf(math.pi)7 }/ C T, F& i2 T& Q# u5 L
- (0.14159265358979312, 3.0)9 F" v$ O; D% T1 s% M& ]7 n7 i- o7 l, u
- >>> math.modf(12.34)0 J) r: P; @7 l3 X
- (0.33999999999999986, 12.0)
复制代码
; l' _! m1 M9 a% Cmath.sqrt(x) 求x的平方根7 } K: ?. S( F2 J5 g
- #求x的平方根6 G' B! q5 b$ i# Z
- sqrt(x)
& ~1 U+ |% M/ L; n( N - Return the square root of x.% H7 o( h2 U9 W! K1 @; v2 M3 D) }
- >>> math.sqrt(100)- K. X: C# J+ D% N% U
- 10.0$ I0 s( ]- e( v% R) w* H
- >>> math.sqrt(16)% A# p7 S! v# F8 V
- 4.0
: }, {( ]4 ~0 Y) [+ i# N9 @ - >>> math.sqrt(20)( Y. h, `2 n3 a% e8 f8 D( K
- 4.47213595499958
复制代码 + \2 s0 _1 ?' |4 A/ z% [: N) R& M5 g
math.trunc(x) 返回x的整数部分- #返回x的整数部分& R6 O; Z% r: T
- trunc(x:Real) -> Integral) @1 ^4 t3 }% L4 T% ]5 s: m2 u8 @
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.& K& f# g; F/ x
- >>> math.trunc(6.789)
3 E6 C' N: \ N3 T" y% { - 6
: w9 R) s, j- f9 [' i8 L - >>> math.trunc(math.pi)- C4 T1 d& y( m( H; s/ y
- 3- \& K. Q6 x, u
- >>> math.trunc(2.567)
: K. M+ X/ A, J- G' H" _% M - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|