马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ ~4 u. B, |( [: P
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ M" d( b9 c+ i1 w+ f* ?5 k3 G1 w" x' n5 n0 F
方法1:
8 @+ ~0 k9 \3 r: w. F1 [5 e- >>> import math# x6 v; L' S# e' y% G
- >>> math.sqrt(9)
2 m' A" @3 n9 }( z/ W7 |: K4 D - 3.0
复制代码 方法2:
4 D' O& H& t/ ?7 K' D ?- >>> from math import sqrt
. l- ?7 ~2 J+ H2 } - >>> sqrt(9)' Y) n+ d- q% q" U
- 3.0
复制代码
; h9 X6 l, t+ t B' M# ]6 a
4 }$ t4 O4 {* l" ?+ B! [2 p' ymath.e 表示一个常量
; S; `1 [5 W n- #表示一个常量
6 ]/ M+ B7 G/ W3 a) n - >>> math.e
8 ~0 T# Z: [. V/ d' i2 j' E7 V8 m - 2.718281828459045
复制代码
) {: S V7 `' q. `9 nmath.pi 数字常量,圆周率5 e0 y4 N: o- j& t' ]
- #数字常量,圆周率
9 x2 m4 W" \, s8 N- @ - >>> print(math.pi)2 e* G7 a* p0 F U! M% _4 ]
- 3.141592653589793
复制代码
, d5 P- _. W6 s/ Vmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
( W% p' J( w q" }- #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 {, l9 U6 k2 V
- ceil(x)8 F6 S( n+ d7 x
- Return the ceiling of x as an int.# \9 V: g2 `$ o! }/ f/ G' x
- This is the smallest integral value >= x.6 B, @; @3 Y7 y# T' I, b
+ r/ }3 u1 T; p' P- >>> math.ceil(4.01)
+ I8 ` a3 z/ Z - 5
) C) r- n& t3 c - >>> math.ceil(4.99)$ a c D$ ], w
- 59 C7 Q; x2 A# n( V+ u& U7 T
- >>> math.ceil(-3.99)
) w2 L( x: L0 c' V - -3. F3 {" e% E X1 [; g% b
- >>> math.ceil(-3.01)/ r g3 S. F1 j& G$ ?- }: ]% h
- -3
复制代码 1 x; L6 \- K# Z- M
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 M# t. N' x5 y- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# A1 _4 `1 k: D
- floor(x)( p9 s: v; Z0 c, x8 `3 s
- Return the floor of x as an int.8 r9 c1 P3 d2 K* H, h
- This is the largest integral value <= x.6 n7 j1 `5 d" a
- >>> math.floor(4.1)( w$ p* h: z% f. w& ?8 L
- 4. u9 `9 D( N0 o
- >>> math.floor(4.999)8 u6 B6 q. ]$ D+ ]! {$ h7 L
- 4
- E* c- J8 K9 n$ y - >>> math.floor(-4.999)3 T" f) K7 q. _# J% j1 `; u
- -5
! V# X* S4 a5 a: ^+ t9 l3 y! } - >>> math.floor(-4.01)
: a; ^ {+ {3 f- h+ L! s7 D0 {! E7 s - -5
复制代码 # n$ X! H0 k1 Y& M/ N; I5 g& ^0 o
math.pow(x,y) 返回x的y次方,即x**y! Q: O, X% }, L- k! Q, X; }
- #返回x的y次方,即x**y
" \( m6 I# ^! r! ] - pow(x, y)! L5 K7 [) g. |/ J- k0 ?
- Return x**y (x to the power of y).+ d& P: e. S- [
- >>> math.pow(3,4): o9 R! o# X* j
- 81.0/ p, _( U, a5 V/ D# S; z9 G) Z
- >>> 1 J) Y; [# }, R
- >>> math.pow(2,7)
7 v/ ^% X5 k5 G! G - 128.0
复制代码
: X: |/ p# R' M: g5 M6 nmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 O! T' n6 C/ _) S6 ~9 b- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% o* H% b9 A$ s/ r3 x
- log(x[, base])
+ B V4 A; x: y# b& _* { - Return the logarithm of x to the given base.
1 j( _/ e3 X4 n5 ^+ l6 i - If the base not specified, returns the natural logarithm (base e) of x.
- I) {# U5 u! y. J/ _ - >>> math.log(10)6 ~3 ~9 h; F1 ?4 Q; p
- 2.302585092994046, ]- |1 V# c2 [
- >>> math.log(11)
1 _9 D6 N$ m; I" q% B - 2.3978952727983707
$ X' u) [( l2 j ?. p: K - >>> math.log(20)( Q9 e. V- J9 U) h$ u0 v
- 2.995732273553991
复制代码
2 S) _+ j# @6 E- T' t) Umath.sin(x) 求x(x为弧度)的正弦值0 E, b A* |6 m; F: ?
- #求x(x为弧度)的正弦值
; K% I: B% W I3 E7 N: f - sin(x)
4 b& F& Q) K0 B - Return the sine of x (measured in radians).2 x/ d, q H( a' W* |9 B/ O8 `7 f
- >>> math.sin(math.pi/4)
' B5 }, V# z" R2 Q- X. h5 | - 0.7071067811865475% }% w. ^0 e L2 c [2 v
- >>> math.sin(math.pi/2)& N0 f6 S! b5 `% D
- 1.04 ^4 J# }" C( x7 f9 l1 ?# G
- >>> math.sin(math.pi/3)+ D) I/ E# [, s# o
- 0.8660254037844386
复制代码
! e2 Z1 g0 v% @; @$ v5 umath.cos(x) 求x的余弦,x必须是弧度0 B( G+ ~0 B, F7 M) l: r# q
- #求x的余弦,x必须是弧度& G0 W9 s7 t2 L, `% H. K
- cos(x)" p' d$ b# I% X- k. N8 k. `
- Return the cosine of x (measured in radians).* I: Z) n0 L+ K& z
- #math.pi/4表示弧度,转换成角度为45度' o" q( C0 d- w# N. Y- I. U
- >>> math.cos(math.pi/4)/ C6 H" E- i, P) t2 G# X
- 0.7071067811865476
0 ]+ ~/ H, y! { - math.pi/3表示弧度,转换成角度为60度
' h& f. }* v; P5 @2 ?0 K' F8 H - >>> math.cos(math.pi/3): `: o4 s7 B ?" e) u
- 0.50000000000000010 [( d' z& v) c
- math.pi/6表示弧度,转换成角度为30度
. a) E6 S% t2 ?1 K6 C8 x - >>> math.cos(math.pi/6)
7 b2 [3 L6 {2 c$ z' `3 u3 C - 0.8660254037844387
复制代码
" y5 [/ P5 d- T+ t" C9 ^$ hmath.tan(x) 返回x(x为弧度)的正切值 _( a F3 S# a
- #返回x(x为弧度)的正切值! a7 R- F% a4 ?
- tan(x)
7 w/ \ f& @5 l; N' p" B. F( Z0 V - Return the tangent of x (measured in radians).
, d9 z+ d0 p/ A& E% u$ ?, p- C - >>> math.tan(math.pi/4)
- X. G0 p% y. T - 0.99999999999999999 \+ {; R$ U& S/ b7 T5 R
- >>> math.tan(math.pi/6)
5 B7 E+ x. {) k) B2 n1 y" { - 0.5773502691896257
; @7 w2 }. E4 B1 z( q" \4 l - >>> math.tan(math.pi/3)& i9 w* J' l) V F, g
- 1.7320508075688767
复制代码
1 @3 b$ X2 I% D+ s* \math.degrees(x) 把x从弧度转换成角度
0 g( V7 N3 ^+ S3 [3 C& A: n6 R# |- #把x从弧度转换成角度) \5 A" Q1 G1 g
- degrees(x)9 j+ Y! ~7 m) @6 `. D( G
- Convert angle x from radians to degrees.
3 v+ i$ O" w2 Z& l7 l+ s - : D7 d# ~. n: Q# ^4 s0 c
- >>> math.degrees(math.pi/4)
4 J s( N; Q7 h6 J" G! N W7 d - 45.0! v! P: p; U$ F ~* J
- >>> math.degrees(math.pi)4 Y. e% ^! J8 d; ?0 x3 n4 v
- 180.0
- b6 w$ N2 B3 X4 e - >>> math.degrees(math.pi/6)
+ z- z9 q t* p7 u - 29.9999999999999962 ~: L2 n+ a9 j* q' F; l1 I( l
- >>> math.degrees(math.pi/3)+ U% N# k) P+ s2 a0 h% F8 N
- 59.99999999999999
复制代码 " t# M4 M2 z" n ^
math.radians(x) 把角度x转换成弧度
: ]( q3 u6 s- u3 `- O6 g5 l! k- #把角度x转换成弧度
: V$ h0 W( j% L) W, N1 I - radians(x)5 ^) W+ G7 {0 z7 [0 M/ j" ?
- Convert angle x from degrees to radians.
, O; p& r$ W+ b! U3 @0 E - >>> math.radians(45)! t) a! }, [2 d: E
- 0.7853981633974483
. |5 G! O$ N- d* u - >>> math.radians(60)
) y8 t7 F, X/ L5 t7 p - 1.0471975511965976
复制代码 0 F( j/ h6 }; y" c3 G* c( H" N
math.copysign(x,y) 把y的正负号加到x前面,可以使用0$ q5 K" i2 a* d; T* `
- #把y的正负号加到x前面,可以使用06 C6 n5 }( X( i, p
- copysign(x, y); `. b5 d/ g# O3 P3 u& S+ L# o$ N
- Return a float with the magnitude (absolute value) of x but the sign + R! y* _" o: ^$ |" @
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
( z2 j1 m& u' E! t Y# h: G - returns -1.0.
( G7 F) F$ M& d1 y0 B# i
( |; c- D- L! d7 d+ g1 f- >>> math.copysign(2,3)8 h& c1 N$ j( H+ [/ V4 N6 g
- 2.0
. o# I" ?9 s3 M. g" q9 m0 G - >>> math.copysign(2,-3)
$ z: @+ O. R# g5 m - -2.0
7 E7 U1 V- B4 x! r% R$ q2 B - >>> math.copysign(3,8)+ }1 }- {- \, H1 R/ S% Q/ k
- 3.0+ \/ \! e6 p/ ]' o7 {) }
- >>> math.copysign(3,-8)4 b: o8 d; R$ s, }, z
- -3.0
复制代码
! e2 v" t6 @! r# [# x( \3 Vmath.exp(x) 返回math.e,也就是2.71828的x次方/ }" ~( _& ~$ U5 `5 O$ @
- #返回math.e,也就是2.71828的x次方
6 V5 d6 o5 j! b6 n9 K) r. H! t - exp(x)6 W; N8 C/ J* i3 B% p( C( }0 V
- Return e raised to the power of x./ a& u7 f& D& q' S. k( N' z
& Y8 J! q( p& X- >>> math.exp(1)
" F) }" ?2 T3 R+ n" {& h/ t E7 F+ ] - 2.718281828459045
. \7 X1 A- o) [/ O6 `$ o$ E - >>> math.exp(2): P; z- l0 e, ]9 Z
- 7.38905609893065
8 C6 \0 Z: f( U! G - >>> math.exp(3)
6 j1 {2 ]4 F, W4 R) F - 20.085536923187668
复制代码
1 ~* [; i" {/ u- v: O! b0 V; amath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减18 j( m+ ~. p! Z3 q, C0 i
- #返回math.e的x(其值为2.71828)次方的值减1& V! k% p# b1 I0 S
- expm1(x). O S' ]9 V0 |* S2 p" z
- Return exp(x)-1.
* ~2 h1 }, f' _. k4 R" Y1 ~0 K - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.' {. @0 B$ l9 L# N) l
- ' F' x! r9 |# y6 B2 ^
- >>> math.expm1(1)
; p& x: m8 ? R `4 T' ?* V - 1.718281828459045' F5 U$ W8 ^ K" j
- >>> math.expm1(2)7 E: x/ G4 v7 E3 ?- M9 J9 s: }
- 6.38905609893065$ N) e" j" I* I$ {6 I
- >>> math.expm1(3) J/ f8 l. M. o/ K0 @$ m8 H
- 19.085536923187668
复制代码
9 n9 a' m2 _9 zmath.fabs(x) 返回x的绝对值' N" g! }! {! g. {9 a! W
- #返回x的绝对值
- e! V5 I! j; k; c4 j% Y* y - fabs(x)6 f% Q) E0 I) b8 n3 b2 q9 t
- Return the absolute value of the float x.2 i2 Z `. S7 Q6 `/ l2 z. e
- 4 y6 Y) T3 {3 R
- >>> math.fabs(-0.003)! ^$ ]7 F, c" w, ^1 i6 Y- o) i
- 0.003
1 U' g( Y8 a, m7 {# `4 | - >>> math.fabs(-110)
# g3 G n) k# M7 [! z - 110.0
! T2 G( Q: `& F+ w6 w# D4 o( N - >>> math.fabs(100)+ w% i9 I- ~: N! ^% Y1 ~: D
- 100.0
复制代码
& Z9 i' ?6 C" q0 O3 I; ~math.factorial(x) 取x的阶乘的值0 x1 i* [; B6 g) X
- #取x的阶乘的值7 D: E& [9 k: w6 k
- factorial(x) -> Integral
7 G# t0 o" E) w$ ^6 e y - Find x!. Raise a ValueError if x is negative or non-integral.) j6 q* [6 O+ O/ B
- >>> math.factorial(1)" U# A5 ?0 \* p) z9 L `* q4 E
- 1- s4 g5 ]' C1 ? e& @
- >>> math.factorial(2)) ?6 p# u/ v* h' I
- 23 B Y% n) f8 b
- >>> math.factorial(3)/ T5 S6 q5 a0 w. a, A# [% j5 N& W
- 6
4 {# u6 Y8 [9 X) M2 {8 ` - >>> math.factorial(5)
% c% Q- h8 o4 |6 T* B8 Q# s( Z - 120: [) p2 y# k9 G7 A1 b- c# q
- >>> math.factorial(10)# C7 f! q$ U! \ z$ j' {+ T
- 3628800
复制代码
( }; M. h' h+ @math.fmod(x,y) 得到x/y的余数,其值是一个浮点数' ?1 w) m( Z6 \ v1 d
- #得到x/y的余数,其值是一个浮点数
( Y' Y: m, }8 ~ Y( V9 `% a6 L: y - fmod(x, y)
2 x+ Z8 P2 ` O' v0 w& g - Return fmod(x, y), according to platform C. x % y may differ.
3 y3 W0 ^# U# Q0 L9 [& ? - >>> math.fmod(20,3)" t& T" y8 z; H! V3 S6 W; G
- 2.0
: j$ L2 g* ~" @% {5 `0 S9 @9 P& t - >>> math.fmod(20,7)7 a( {6 x' ?$ ^- G
- 6.0
复制代码
/ j2 N8 ?) S4 ]8 l# pmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& w+ I1 \$ R- \+ X4 z2 R4 @9 ]( v
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
' k9 H( O: }9 {& y9 _% G - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 b2 F, v }4 E$ d. M4 I
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1: m$ ]% u! X( _; ]/ m
- frexp(x)
% j/ e& F$ y: [, g. w( c. @7 ] - Return the mantissa and exponent of x, as pair (m, e).
5 Q, D1 A6 C3 C; b; {+ N - m is a float and e is an int, such that x = m * 2.**e.& `- T2 Q& w# O; e; G, H. i& I9 y
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
M2 h+ C9 J' G5 t& j7 q/ u2 M - >>> math.frexp(10)9 R+ ] s4 C4 |' D$ V- V
- (0.625, 4)4 {! V2 B3 ]( }, W B) y) X
- >>> math.frexp(75)3 z% O+ }* J! t$ ^% j
- (0.5859375, 7)
/ T4 w$ p/ Z% ?* B - >>> math.frexp(-40)0 c2 W) | n5 Q" p2 N* U5 W% x9 q
- (-0.625, 6)
" x, e% J9 N8 E+ y V: s; I% K) l - >>> math.frexp(-100)
5 F$ I# z B, V - (-0.78125, 7)4 s3 v9 C% K; O* v5 Y( U( h
- >>> math.frexp(100)
8 v; e" I; z2 r H5 J- C# [ - (0.78125, 7)
复制代码 ) N+ Q; u$ A- r; g; e
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
$ r$ p2 U& f+ @- #对迭代器里的每个元素进行求和操作
9 E4 {; h' O8 W5 r, H4 h/ q - fsum(iterable)
' q! S: r+ L0 P D+ W' J5 \2 F - Return an accurate floating point sum of values in the iterable.
+ u( I1 W6 g# {4 K6 I: `( a+ I* T - Assumes IEEE-754 floating point arithmetic." B) w6 a+ E4 ?8 b' @
- >>> math.fsum([1,2,3,4])
' {; F+ a3 W$ H* A+ Y* Y# \ - 10.0; ~* U7 s: u) `! H! l* U$ x( U
- >>> math.fsum((1,2,3,4))8 J7 J; S$ z$ x% D
- 10.0# ~" l3 E" ~+ t& f( ]& C' ^
- >>> math.fsum((-1,-2,-3,-4))
% Q& C% p2 h& n! v - -10.0
1 y' x/ L* _0 l. R' q - >>> math.fsum([-1,-2,-3,-4])( ~! ~" g8 o7 z; {6 {- G
- -10.0
复制代码 7 I9 p/ i6 R% c- y4 }/ e
math.gcd(x,y) 返回x和y的最大公约数, O! K% u# t. t7 J: y: r/ Y
- #返回x和y的最大公约数
" G1 n. @/ w3 U - gcd(x, y) -> int
9 G; z8 I5 v0 e }( q0 X4 w3 _* y - greatest common divisor of x and y E* x* Z$ r% }9 a, m
- >>> math.gcd(8,6)
" \. U, Y/ p' m. V; U - 2
% p$ i9 P+ V% ^; _$ T# s5 V - >>> math.gcd(40,20)
4 [8 W+ L6 O: X; I2 h d - 20
0 Y9 E7 B+ E/ ]' P8 Q - >>> math.gcd(8,12)
. A% W6 h: W8 @1 f. F) F. k - 4
复制代码 / H3 G: s" f/ |* J* e1 _% p
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False+ n) L( h. A% C& E
- #得到(x**2+y**2),平方的值
( K7 Y/ O1 s+ ]$ W - hypot(x, y)# @$ I, r3 o# s3 Z1 t" m
- Return the Euclidean distance, sqrt(x*x + y*y).# f2 Q4 T' S; D
- >>> math.hypot(3,4) `4 \ C& e2 S# h4 I. c" C2 l
- 5.03 G( j$ y: F- m
- >>> math.hypot(6,8)6 j' d: w. Y' h" U% Z5 Z. g
- 10.0
复制代码
! G' H: t% r: z# ^math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False! F7 x* b2 e2 Z8 g+ U
- #如果x是不是无穷大的数字,则返回True,否则返回False
/ _% f0 F( h4 ]2 ? - isfinite(x) -> bool
# ^+ S# Y' [0 d$ N% |4 e/ O - Return True if x is neither an infinity nor a NaN, and False otherwise.$ Z" n9 g2 ~; b, r; K! v+ `
- >>> math.isfinite(100)
& v: o) }2 |- I2 J* ?" ^( Q - True* G& K# b- T6 g+ t' z0 d! `5 Y8 F
- >>> math.isfinite(0)
+ m- {; Q; Q6 I - True
( P9 B4 @% Z, O+ z$ u5 v4 g - >>> math.isfinite(0.1)) @. x& J6 O- j$ p U& i
- True0 e$ D% u) O0 \
- >>> math.isfinite("a")* u1 i7 f" N( O) {: h& Y- N
- >>> math.isfinite(0.0001)$ \$ Y M! y9 m, M$ U# u2 [6 _
- True
复制代码
# C1 U* A# _" j5 v2 e. l4 \math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
- T3 n( W7 h8 L/ |7 D8 C- #如果x是正无穷大或负无穷大,则返回True,否则返回False3 y* Q* z* ^# {, p( j# L, y
- isinf(x) -> bool
/ D+ N. l% `7 Z" i: Y+ \ t, V - Return True if x is a positive or negative infinity, and False otherwise./ o( n5 p3 S4 T$ Q- }
- >>> math.isinf(234)& o6 A b( l, q- P9 }2 g; y1 a; `
- False
& q, C/ b9 H. s5 | - >>> math.isinf(0.1)0 u4 j1 k* ?; S' @
- False
复制代码
y) A. l- T gmath.isnan(x) 如果x不是数字True,否则返回False* o! A& o+ Q/ ]9 | P& X
- #如果x不是数字True,否则返回False
R" {6 v7 z( o# k - isnan(x) -> bool) k! K" i5 C* r( g9 r
- Return True if x is a NaN (not a number), and False otherwise./ N, D/ V0 z8 {0 t/ c6 b
- >>> math.isnan(23) e C% m7 I* R( r+ E1 @
- False E+ U1 P& R9 O4 X1 Y
- >>> math.isnan(0.01)( y9 M+ b% b: {- s$ U/ |$ a# L
- False
复制代码
u1 |8 [$ h$ I e- B- N9 Y8 Mmath.ldexp(x,i) 返回x*(2**i)的值% k3 a& J# E( D4 P8 }7 \" w
- #返回x*(2**i)的值6 F8 G. ]& [' Q1 N
- ldexp(x, i)
) K# H# W: ^6 \& U3 G9 m! m - Return x * (2**i).
. i+ s3 [* @- y" g( P3 h - >>> math.ldexp(5,5)& z# U" R6 m/ d6 r
- 160.0
3 G# N/ d K( j& b! r - >>> math.ldexp(3,5)
1 W* A! q1 v/ o# o0 {" w8 c) Y - 96.0
复制代码
+ X3 c9 z+ U6 g" F1 `math.log10(x) 返回x的以10为底的对数
* h/ s$ e6 L: i) _: w# n0 K- #返回x的以10为底的对数
" y7 c) f; ^7 {/ n# e& E- t - log10(x)2 M2 B% o' C: o7 h# j9 e, }
- Return the base 10 logarithm of x.3 H- Q6 p2 \9 J L4 V6 H% W6 z
- >>> math.log10(10)
u1 ?4 ]! y/ a! y/ P - 1.0
$ A+ ~; P$ Z5 G4 x - >>> math.log10(100)
. `1 m/ O4 n6 S% T& d - 2.0
$ \5 B. O* X0 W3 |0 B4 M$ T' H* _" b - #即10的1.3次方的结果为20
( |4 r6 m$ v/ D+ X - >>> math.log10(20)/ ] {3 s3 B% h( r' Y' C
- 1.3010299956639813
复制代码
) Q4 u: |" S: o6 p, { omath.log1p(x) 返回x+1的自然对数(基数为e)的值3 T1 `+ k. c. Q0 y( Z1 b1 w
- #返回x+1的自然对数(基数为e)的值( R! H7 x! U/ s0 k
- log1p(x)
* ]! z' M7 p. B3 s9 n$ J - Return the natural logarithm of 1+x (base e).1 c( k3 {! E4 J0 ?7 f; a
- The result is computed in a way which is accurate for x near zero. Y1 m' ?" H N4 h- i- t: U+ a# B
- >>> math.log(10)4 o: b5 [$ N* r
- 2.3025850929940467 d, W3 q8 v* J0 ?
- >>> math.log1p(10)
8 |5 C: H; `3 M3 d7 c! P - 2.3978952727983707
; B& S8 e# h3 b) p) ? - >>> math.log(11)3 P& K; d4 M- E- i: I+ J+ B
- 2.3978952727983707
复制代码 6 `3 Q/ F4 H7 B* A
math.log2(x) 返回x的基2对数
2 I; z9 Z9 A: X' @. N; m2 S+ q: Y- #返回x的基2对数% }' @0 o- {* j `2 }/ y v
- log2(x)3 l2 T" ~# x6 j' z/ A
- Return the base 2 logarithm of x.
' @; J) D1 @6 y- {; ] - >>> math.log2(32)
& Z/ S+ H/ A; ~ n, {* C - 5.03 h6 A! }0 W) X2 l
- >>> math.log2(20)+ G3 X/ ]7 S+ [; |/ K9 f3 g; U+ C% F& V
- 4.321928094887363
; y& E% H. l; n9 s# } - >>> math.log2(16)
& t( w. ]0 n' k - 4.0
复制代码
) Y/ I; Y- n* O% f( Qmath.modf(x) 返回由x的小数部分和整数部分组成的元组9 x# D9 @, U1 E$ o
- #返回由x的小数部分和整数部分组成的元组' {& T8 C' E1 b" U+ c. Y, a
- modf(x)
7 Z- X- d# C+ i3 ]7 X# B - Return the fractional and integer parts of x. Both results carry the sign
8 ]6 _9 Q: L% L) B - of x and are floats./ P5 P5 g2 ~" {3 C( c8 }8 [
- >>> math.modf(math.pi)
X5 ` H# y- l- s0 k% b - (0.14159265358979312, 3.0): R# l) ^+ n }8 h
- >>> math.modf(12.34)
- E1 D, q% R7 y- e: h. G: u - (0.33999999999999986, 12.0)
复制代码
* P/ r6 H& C8 m @math.sqrt(x) 求x的平方根
% K( @; G/ Q9 ]4 C7 f" O- #求x的平方根! }0 T) i' r! O! B( ?! C+ w2 G' n
- sqrt(x)* Y1 Y8 b. h. h) y+ ^
- Return the square root of x.
2 m& e& M3 d& u: L1 H/ F1 n - >>> math.sqrt(100): |7 k% K8 u$ m, r* I
- 10.0( h! l4 d- n; B i0 V+ V+ l! t. _, q2 ?
- >>> math.sqrt(16)
6 @# M3 P1 i& p0 i* c - 4.0+ @. H! v' ?; Q$ ]6 [
- >>> math.sqrt(20)1 ~: }0 c1 B; v, ~
- 4.47213595499958
复制代码
5 l4 v1 `$ b+ F% e; Fmath.trunc(x) 返回x的整数部分- #返回x的整数部分
+ y5 h; W4 n, k; F3 |& n/ ^! Z - trunc(x:Real) -> Integral0 ]: z2 |% Z6 `* g( F
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
, P0 Z) I% Z& ?% `6 s$ r( t/ V - >>> math.trunc(6.789)% _# \0 B3 \4 p4 m% v7 u
- 6
, x8 j# a( ]4 b+ G+ b - >>> math.trunc(math.pi)
; s, U5 b+ L! w+ e7 w; L; \3 y8 g9 g2 q - 34 o. F% q& u, I, [
- >>> math.trunc(2.567)
' i/ a R& A/ C* g - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|