马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
% B) h5 W! K- E# o4 t! d4 a
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ y2 i2 z/ h) A: j& x, `- e; w, Q5 a7 I* { h1 y0 l4 _
方法1:
/ ^5 u }$ S# W9 B8 L0 _4 I- >>> import math$ i9 S; p+ M/ @) I& c# q1 x
- >>> math.sqrt(9)
~ A; @" ?6 O - 3.0
复制代码 方法2:
% d2 K9 S8 i$ p8 x* k- >>> from math import sqrt) @# X$ `! Q3 \/ u6 \3 n
- >>> sqrt(9)
) [1 P2 t% _7 y+ R, m - 3.0
复制代码
1 H, p7 v9 i3 l3 C7 h/ m * W" A6 X: M1 i# h6 d0 F% k
math.e 表示一个常量
( z9 n9 f- l% V& K- #表示一个常量2 M( P( Q4 t8 b1 F. M: G8 Q0 f
- >>> math.e3 D3 Y5 b6 V7 A' v- h, ~
- 2.718281828459045
复制代码 0 e* C/ J$ O4 B3 M
math.pi 数字常量,圆周率; A. U8 Q7 q+ F' j2 Y
- #数字常量,圆周率
! y3 y: J. ^, i2 Y% b* O+ D9 I - >>> print(math.pi)! U2 D* i7 C; H6 ~5 d% v
- 3.141592653589793
复制代码 , N1 v5 C- ]5 ?! U' u: M5 p
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ U+ I# ^2 _) u7 y, T- #取大于等于x的最小的整数值,如果x是一个整数,则返回x9 O- F7 b$ Q/ u) `( [ O4 m% ]6 V
- ceil(x)
* Z( {, E: \ S6 L - Return the ceiling of x as an int.0 V* u7 c8 s5 m/ P
- This is the smallest integral value >= x., J& T; S8 d1 Q6 x
- 6 U, E8 ~2 w/ V: [
- >>> math.ceil(4.01)
( i( d7 } r6 O. U' m/ H - 5
# H: D7 e- P/ l% U - >>> math.ceil(4.99)
7 t6 V0 \: l- o. }8 k5 q9 l$ y! t8 R - 5" q. S/ S) V/ P" f- T
- >>> math.ceil(-3.99)+ ^# c2 `% C4 _1 r8 c2 `! G
- -3" Y4 w* y6 K9 e+ |& f
- >>> math.ceil(-3.01)& G9 g" T8 C- h: }" O
- -3
复制代码 0 {0 d; R7 I0 \: Y4 y6 h% W/ ?
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 P& m8 ?" h; {# I6 Z6 Y9 e' U- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 K% E1 k! z$ A
- floor(x)
; Z( L& S" M: y5 B( y - Return the floor of x as an int.
; ] T) k4 @: s1 Y5 q6 a1 k - This is the largest integral value <= x.) ^3 a4 b% T% U: e7 j
- >>> math.floor(4.1)4 n( D p, V% N) v" a7 \$ e, a
- 45 L7 f4 N7 y4 s# }4 K4 }; c8 }' u
- >>> math.floor(4.999)1 v6 d! r; l8 [ J8 k
- 4
6 }4 q4 F* S6 A - >>> math.floor(-4.999)
; |2 B/ h) Y* i4 e) o5 L - -51 o j9 m2 p$ K- d! ~3 w
- >>> math.floor(-4.01)9 u" {' `; \& r) D; x! j/ K
- -5
复制代码 # {& x7 W0 J! z4 t, _" W1 U
math.pow(x,y) 返回x的y次方,即x**y
$ x" n$ W ~" L" h) {2 I- #返回x的y次方,即x**y+ \8 L- V1 U/ ~& l8 Y1 t, }
- pow(x, y)
7 F) d( p2 d& b0 s- B2 E$ B - Return x**y (x to the power of y)., }( p/ _" L; H( k) B* R' D8 s# ~
- >>> math.pow(3,4)5 ^8 R+ b4 P& u
- 81.02 C, d1 N/ u. Q
- >>> / K" Z& P1 v) e5 ~: R( ?) ?1 a
- >>> math.pow(2,7)" M7 C) [% g8 i/ r
- 128.0
复制代码
+ r ^0 ^) F& E; c3 nmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 S+ {& W* a$ M% k( @ n8 A- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 f0 e: Z- [( W- N2 U - log(x[, base])
$ N! o; a$ X- G" O# d) c; e1 b; h1 ] - Return the logarithm of x to the given base.
6 A3 t' R7 K! C5 v# H% } - If the base not specified, returns the natural logarithm (base e) of x.( [, G: r* H5 v$ C& `! @% X# | Y
- >>> math.log(10)4 X$ o, K/ }) q# h4 `
- 2.302585092994046
% l1 S2 }# `! \ w: T. z( E - >>> math.log(11)
5 _4 z5 ?( f7 X - 2.39789527279837074 y! w- Q; ^! H) V4 v
- >>> math.log(20)3 h+ m X/ P( D* y4 ^2 V
- 2.995732273553991
复制代码
1 p+ T4 p) ^" A, ~9 d4 Wmath.sin(x) 求x(x为弧度)的正弦值
/ e1 F) m4 s) }- #求x(x为弧度)的正弦值
" V* N% t5 U; {. ` - sin(x)
" S- p1 w+ M0 S' C: r - Return the sine of x (measured in radians).& [ M/ g$ r/ X& O, u
- >>> math.sin(math.pi/4)7 J+ s( O3 u& h6 [: x1 }, a
- 0.70710678118654754 F9 `% W; L x
- >>> math.sin(math.pi/2)
6 p* i1 y7 J/ h8 @( A9 F" ?1 j5 l - 1.0% t4 Z* {# X$ [" a _. C! B$ V
- >>> math.sin(math.pi/3)/ f7 |% z* ]; m; w1 C
- 0.8660254037844386
复制代码 7 P" o; U% q- K, N
math.cos(x) 求x的余弦,x必须是弧度' c, f ]4 [' z5 A& l
- #求x的余弦,x必须是弧度 N# y: F8 I, _' K9 W- ?. z& q [' C
- cos(x) i) p) A# \+ B5 b5 D- v
- Return the cosine of x (measured in radians).
' z9 s% V- V3 k R; k' t% C. j - #math.pi/4表示弧度,转换成角度为45度
: }) F6 n* Z: t$ N6 e - >>> math.cos(math.pi/4)- d) U) \" f: X3 d, p3 K
- 0.70710678118654762 c- I6 r2 l j C
- math.pi/3表示弧度,转换成角度为60度
7 q; w) Q2 |+ A0 m( F% Q/ W' ?0 S; {5 f - >>> math.cos(math.pi/3)3 ]' ?% p/ m! g8 P5 f9 Y
- 0.5000000000000001
; R6 F4 `/ J7 [# b - math.pi/6表示弧度,转换成角度为30度4 e) a; Y5 j4 s0 d+ s
- >>> math.cos(math.pi/6)
2 q% [; y- _+ I4 Y* _ - 0.8660254037844387
复制代码
* C$ a8 y& u, B6 `! S6 h! Kmath.tan(x) 返回x(x为弧度)的正切值2 Y$ e6 k" k5 z3 z
- #返回x(x为弧度)的正切值8 C% X0 v1 c2 D- t0 h+ ?
- tan(x)* o! @0 ^. G- H1 J
- Return the tangent of x (measured in radians).' T' g2 ?) e( I/ b4 p
- >>> math.tan(math.pi/4)
) P1 g7 Q; w; S7 t - 0.9999999999999999' _: [1 ]7 q6 I
- >>> math.tan(math.pi/6)% Z4 X, b, U; c/ Y' |& f
- 0.5773502691896257
+ \- E9 S9 e6 [) Y2 M, N2 U5 b - >>> math.tan(math.pi/3)
8 v/ D$ G, p9 I; s. o6 G m, Q - 1.7320508075688767
复制代码
/ M$ ~6 y$ d/ }math.degrees(x) 把x从弧度转换成角度& Q4 h/ q( R: J* o; ?2 t9 F
- #把x从弧度转换成角度
y5 E, Y& n/ v - degrees(x)
& E. s3 J* g9 I2 |; a# ^ - Convert angle x from radians to degrees.( o: u5 |# @) p' p5 C& A7 k
- + S' |7 `2 K$ h! a8 V8 s
- >>> math.degrees(math.pi/4)' Y$ B5 x/ l5 h: ]8 }/ {
- 45.0
. O+ {/ Q1 c2 o* m' J8 S - >>> math.degrees(math.pi), T k# Z% j3 m& n, y# f
- 180.04 E |7 v4 D' A4 M3 f
- >>> math.degrees(math.pi/6)6 B' V& S d, L; f \8 A
- 29.9999999999999963 u; u( D; h7 p1 }, _5 O+ P* K
- >>> math.degrees(math.pi/3)! J1 U7 ]. n4 B: Z
- 59.99999999999999
复制代码
$ C4 Y& u3 @6 i8 [! ]math.radians(x) 把角度x转换成弧度4 _- L6 [6 ]5 G. i# H
- #把角度x转换成弧度) x6 m' k6 c0 J3 k; B: H- Z& P, K( y; v
- radians(x)8 O }& G! `/ I$ b
- Convert angle x from degrees to radians.
9 v; f4 B/ l! @) F7 Z$ Q* P - >>> math.radians(45)" K6 M' y1 K; Z. f$ \
- 0.7853981633974483
% r6 h5 ?. F$ Z4 L+ V/ z - >>> math.radians(60)
7 E: d3 e6 A0 c* u# o- {! x - 1.0471975511965976
复制代码
" l' ?/ J1 p; I, N' V7 a& W; [math.copysign(x,y) 把y的正负号加到x前面,可以使用0
: U7 Q" V1 G* Q- #把y的正负号加到x前面,可以使用0
% q3 K/ [6 h, Y: E5 Q% e* P x - copysign(x, y)
- U+ v6 b5 f4 ~6 { - Return a float with the magnitude (absolute value) of x but the sign
' x, g( h4 [* Z6 u - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
! u5 @6 G: X" i: P- y - returns -1.0.* h# h2 ^) S& z M- L3 ?
- {: ]1 C6 S) R8 V
- >>> math.copysign(2,3)6 V. o O/ J% a# E$ ~7 Q2 u! v* a
- 2.0! ~ C4 k& S" h0 s0 a
- >>> math.copysign(2,-3)
+ t+ ^% P- E" [6 p7 y - -2.0! ]1 @% f: {8 l2 g0 p: c/ Q
- >>> math.copysign(3,8); l& H4 I M7 F8 R
- 3.0! o- x1 t9 G' L* r8 M: K
- >>> math.copysign(3,-8); _9 d. R9 R: d% K6 v5 B9 ?
- -3.0
复制代码
4 t6 a' W" \$ J+ ]# _/ p1 B0 emath.exp(x) 返回math.e,也就是2.71828的x次方3 d" @; t+ u# T& v* R3 B9 }% B
- #返回math.e,也就是2.71828的x次方
' v0 F9 }- H% X& T: Q! U( @! k - exp(x)
2 y8 D1 h; \8 i- H& k. j - Return e raised to the power of x.) p! T/ w: b- H% R9 D! k
- [2 h4 u% l( b( z& A% l- >>> math.exp(1)
O. l4 s4 \" f. q' ^4 \# H - 2.7182818284590459 @$ D* w) {4 N# d$ {
- >>> math.exp(2)
) `9 l1 F# x( O2 C - 7.389056098930657 Y A* A: ?/ d- v) G
- >>> math.exp(3)# v, j f1 ~( O N7 T1 k0 ^
- 20.085536923187668
复制代码 6 @! B) I+ L, b0 e
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( d7 M+ @& ]7 J, a4 h" Q
- #返回math.e的x(其值为2.71828)次方的值减1
' p& u0 C9 [/ C# z! i* z& {2 v& | - expm1(x)
: j$ r* j7 |7 k6 w) J5 Z - Return exp(x)-1.
2 z/ M% Q% m' s - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. j/ l# H- {- F# d
( F; ^ n1 @* {8 {! v. L- >>> math.expm1(1)
. Q! r) J2 {+ h r2 w) N - 1.718281828459045
+ D7 v6 _; v; b - >>> math.expm1(2)! k7 V% `4 v& V
- 6.389056098930657 ?, G- J+ d6 {6 o
- >>> math.expm1(3)/ e: ?3 W/ @0 b
- 19.085536923187668
复制代码
7 R ^( a) P+ v7 V/ Z: B( Umath.fabs(x) 返回x的绝对值& q9 [0 p1 {. n0 K6 r
- #返回x的绝对值
) S) Z+ a, r4 O, r3 c0 d - fabs(x): P* ]2 c) U2 F- j( F) J) z
- Return the absolute value of the float x.* f( u; X6 {- `
- % M9 K7 ]% M0 n0 C% u, q+ P2 B9 W
- >>> math.fabs(-0.003)
: W! x5 j' a: E a/ Z: C& R - 0.003
, O* Y/ g# j# E' V6 d - >>> math.fabs(-110)
1 ^4 e$ j# p5 g1 S3 x1 I; ?+ Y - 110.03 c& G* Y8 S+ H- V/ Z+ o! y
- >>> math.fabs(100)) ?( h, j' e: i7 \& O1 Y
- 100.0
复制代码
, J, [( U, L# c1 P2 h. v7 }math.factorial(x) 取x的阶乘的值 q- }$ \, j1 B7 r* @ u$ ~$ _
- #取x的阶乘的值
8 X# ?2 g6 A# ]- u2 c - factorial(x) -> Integral
. W( l' R; e4 [* u - Find x!. Raise a ValueError if x is negative or non-integral.
, {" M D7 B' Z/ y0 |) s - >>> math.factorial(1)" t4 c M8 [: P1 T
- 1
: W' r3 i3 P. K7 T4 I - >>> math.factorial(2)% B3 p. b' ?1 P3 a4 g
- 2% n4 x9 ~% A( E8 [: ?$ t! R
- >>> math.factorial(3)' D! L9 E( }3 i5 r/ P2 @
- 6$ f8 m4 |( A; ]) D! R5 P/ N
- >>> math.factorial(5)
! r' x. H( [, a8 G9 u# U& X! M - 120, H$ a0 l0 P5 {* Q, X6 M3 o8 B
- >>> math.factorial(10)
$ G4 ?3 _/ I- N0 O2 N: T - 3628800
复制代码
& e' R% d! m/ ]2 O3 X3 T% Amath.fmod(x,y) 得到x/y的余数,其值是一个浮点数! I1 N; s4 }3 m7 r% G
- #得到x/y的余数,其值是一个浮点数) E& y% R8 S3 ^) w, \
- fmod(x, y). m9 P' }+ U$ B
- Return fmod(x, y), according to platform C. x % y may differ.1 |6 U0 W" \. f) z# r! k- g" i5 l" N
- >>> math.fmod(20,3)- M& l# e! C( ?5 S) \* C
- 2.0' X7 q/ _7 g! }
- >>> math.fmod(20,7)3 p; ~9 R( ?4 ]9 K9 ]2 ] C
- 6.0
复制代码
$ Y4 i0 {' Y; c1 Z# Gmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
# C9 f8 Z/ Z* K, [: k- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,2 M$ I, E4 C5 u4 g! C9 E
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
! |3 e6 |, {3 p+ F% m - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
| ~( r6 M/ j6 G, ^: v - frexp(x)& G3 h/ R0 L3 q, f- Z' L
- Return the mantissa and exponent of x, as pair (m, e).& u1 i. g! b* [
- m is a float and e is an int, such that x = m * 2.**e.5 E3 }9 a: y$ V7 X
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" C0 G' u% p( C9 u* p1 V - >>> math.frexp(10)7 [: u1 `* V* n' ~0 k: X4 a+ O
- (0.625, 4)
, l. F; y& R7 u& y2 N - >>> math.frexp(75)+ [) `9 b' v" g
- (0.5859375, 7)
6 D ?; c' ~& y& v" s" C w - >>> math.frexp(-40)
" m; o+ ]- w; G5 l5 s - (-0.625, 6): W$ @% X* O$ @
- >>> math.frexp(-100)
2 S7 W- x/ J* A - (-0.78125, 7)
4 s% B1 }) I. u& G! ]! E1 q3 }' } - >>> math.frexp(100)7 ~; f7 l9 [/ U' R E: g( x! ^
- (0.78125, 7)
复制代码 ' Z- G+ f3 D, ^8 s' K" L; g
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
' R/ f7 M; y/ V- #对迭代器里的每个元素进行求和操作* ~/ i! V1 A" U3 T8 [, W
- fsum(iterable)
1 s9 h9 x3 p7 H( n' U: q - Return an accurate floating point sum of values in the iterable.
8 n o) O# S; x" e" I! g0 \: Y - Assumes IEEE-754 floating point arithmetic.$ ~1 t" f& G% _( l) O- W& M4 L
- >>> math.fsum([1,2,3,4])- [ f+ C, L* i+ c: l. J
- 10.0; n+ S! s1 w. X6 `
- >>> math.fsum((1,2,3,4))
+ K( [8 ?2 I; U& t. u8 _+ G1 }' G$ N3 r - 10.0
3 @' _8 R5 m U4 {" Y! R: k - >>> math.fsum((-1,-2,-3,-4)). f- I( O! ]& k( l! d
- -10.0( d4 \: E. B& R, A9 H
- >>> math.fsum([-1,-2,-3,-4])
& m5 ]: y$ A( i" ? - -10.0
复制代码 8 u, G0 c8 ^, N7 H5 \
math.gcd(x,y) 返回x和y的最大公约数
# r2 @) q0 o4 H0 r- #返回x和y的最大公约数
2 z) Y6 F& K% z8 W - gcd(x, y) -> int
2 Q: ?6 b! S2 u - greatest common divisor of x and y7 M8 J% o8 n& Z, V
- >>> math.gcd(8,6)
: h# v' z1 d6 z7 K+ |* a - 2
! E/ L K0 h& T: s9 w" j - >>> math.gcd(40,20)
' O, m; F; \- | - 20
) D y8 O" x# V7 d - >>> math.gcd(8,12)
2 F/ C2 F" n( _ - 4
复制代码 * ~4 N% n3 y- [) \
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
1 Y6 ~ Z/ M' N6 E; u" |) r n- #得到(x**2+y**2),平方的值; y" G8 j! j$ }3 g3 n2 e
- hypot(x, y)
. ?" @# @- G4 q$ d8 e - Return the Euclidean distance, sqrt(x*x + y*y).4 _, _# i) `) P C/ {
- >>> math.hypot(3,4)# ~5 k( ? p% t1 Y$ M5 J- h* L
- 5.0
& c) Q) j6 B+ ? s* M# ~3 X' a- n - >>> math.hypot(6,8)
) r' b* j) ?! }' }. k; O, K - 10.0
复制代码 / ^6 d* T! E0 U5 g3 e3 M! w9 O
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False/ K5 j$ P$ E! V( X/ e
- #如果x是不是无穷大的数字,则返回True,否则返回False
' C9 g) Y% ]; d# v" L - isfinite(x) -> bool
* t2 b- k. O. U- ?" U - Return True if x is neither an infinity nor a NaN, and False otherwise.' _8 X8 D- I8 j; r- X3 |& Q
- >>> math.isfinite(100)( u9 @ n6 B2 X, |! V, \$ F. E p. [( m
- True" ?: O( e+ z- y
- >>> math.isfinite(0)* l4 ]& w2 Z4 `. I2 Q5 }) z* d" a) t9 @6 \: l
- True9 W5 j2 q( M, b1 a! _1 V
- >>> math.isfinite(0.1)
3 m& O9 ?( [" @) J: o) v - True/ i& L( v6 }. ~0 v# Y( W2 h5 o
- >>> math.isfinite("a")
4 m1 x0 M3 m& P8 M0 K: X9 Y - >>> math.isfinite(0.0001)4 m- Z/ b: V: ?% k( H
- True
复制代码
1 V& {0 ]( @, kmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
( c1 i5 N" N: `2 ~2 ^7 P- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ ]# h, e& Y8 c" P" { - isinf(x) -> bool, {+ F* n+ b7 z) \$ N
- Return True if x is a positive or negative infinity, and False otherwise.
9 ?0 l6 o6 T; o' g+ W* @ - >>> math.isinf(234)
: J3 \( c7 D2 w3 T f - False
9 t- U8 x8 D h - >>> math.isinf(0.1)0 {7 H6 \" M A1 Z: N
- False
复制代码 0 w( g# L; W D/ I7 L
math.isnan(x) 如果x不是数字True,否则返回False
# Z" S8 i: E' r' T h2 S+ G6 T+ L& Q- #如果x不是数字True,否则返回False- i1 ~; Z& I! R9 i- [# I
- isnan(x) -> bool. H$ g g$ `- ?) L
- Return True if x is a NaN (not a number), and False otherwise.
3 W @5 t) @& N- s' |* A - >>> math.isnan(23)* P: ^$ f+ ^" o( P1 {3 i
- False
3 L1 Q3 W" a' ~9 E6 B - >>> math.isnan(0.01)
8 x' D9 b# T$ W - False
复制代码
, ]" c: N. S4 ]: ?( L6 imath.ldexp(x,i) 返回x*(2**i)的值2 H7 @8 E1 s7 c* H, F5 p1 J3 f7 B
- #返回x*(2**i)的值- Q( \0 C, l) u) u0 ^# ]# _/ L
- ldexp(x, i)
: ~ S1 l1 n$ n& T+ Q4 e% j+ d - Return x * (2**i).
3 g% z6 c% P* z - >>> math.ldexp(5,5)0 J# \0 x8 R3 d3 }8 H" I
- 160.0
9 F7 i2 I& c8 u6 w - >>> math.ldexp(3,5)
0 ?# e8 Z' I' D2 I. t4 v7 d - 96.0
复制代码 6 L3 J! g. N+ ^( L7 u
math.log10(x) 返回x的以10为底的对数& _( Y* p# |6 e' m8 w6 |3 j J
- #返回x的以10为底的对数
/ J( k$ L$ q& R- I/ R, _8 n8 w - log10(x)
. A8 t4 ?. s$ w - Return the base 10 logarithm of x.+ p$ i0 Z# T7 T J; F
- >>> math.log10(10)
& S7 J! E! @% ?, m2 B! p - 1.0) l9 V5 v7 ?, O/ ~1 J2 {: I
- >>> math.log10(100)
9 z0 z3 V/ j. G9 L% d( @ - 2.0) } w3 o) B$ E6 n# t3 I5 o
- #即10的1.3次方的结果为20
( t9 }& D: Q' g* s - >>> math.log10(20)0 D* W+ |5 x6 W3 T6 K) O q
- 1.3010299956639813
复制代码
% Z4 i/ P! I$ d+ t2 q8 [, I: Smath.log1p(x) 返回x+1的自然对数(基数为e)的值
8 \: J1 h( x( p8 A, Z! E3 y- #返回x+1的自然对数(基数为e)的值- D- N9 Y$ F( m" P% N
- log1p(x)- |6 m8 Q6 ^0 j. k* b6 a
- Return the natural logarithm of 1+x (base e).
0 Y8 s' }* j: O; {3 |; p, E - The result is computed in a way which is accurate for x near zero.% a2 P# K' n% Q
- >>> math.log(10)/ R# P# h% D$ l% o' R. m' }
- 2.302585092994046& Y+ D2 c2 v0 e
- >>> math.log1p(10), c3 k& e: b. D( H8 w* Z1 t# _* Z8 L
- 2.3978952727983707& n9 n: s* u9 W" w7 [- R
- >>> math.log(11)$ {0 u. T+ A/ o, P7 j
- 2.3978952727983707
复制代码
! U( x ?, Y! T0 c- J. Gmath.log2(x) 返回x的基2对数
0 y5 l' N% Z$ K( k' ?- #返回x的基2对数
$ a y1 I3 h( J - log2(x)- Z: e# n( ]8 i
- Return the base 2 logarithm of x.( e0 q$ b7 n' ?4 b. C( E
- >>> math.log2(32)
3 S5 b$ K" z% T5 I2 \% ?+ v - 5.0
" \, ?0 M! E5 A2 e+ d - >>> math.log2(20)
0 T& t4 v$ A) B% G# u$ d! c0 a3 m. _ - 4.321928094887363' I/ P8 Y1 }; H
- >>> math.log2(16)
5 K# g8 k8 o: O% i - 4.0
复制代码
! T6 x. t/ k$ A+ M' kmath.modf(x) 返回由x的小数部分和整数部分组成的元组2 I( `' ^ w* P+ G/ A+ j" K
- #返回由x的小数部分和整数部分组成的元组
* ?0 a d1 X1 _ - modf(x)
5 n* W9 R4 U$ l- ~: P, L: v - Return the fractional and integer parts of x. Both results carry the sign/ r4 _0 o0 L' A) q9 d8 L
- of x and are floats.
. B8 P' g) }3 F8 k1 U - >>> math.modf(math.pi)+ l# N( [: R$ t5 k
- (0.14159265358979312, 3.0)
V! e' G9 t+ S2 D8 K; G - >>> math.modf(12.34): X# M+ {- R; p4 m
- (0.33999999999999986, 12.0)
复制代码 - ^" K$ ?1 E: }; ~! l% m% n
math.sqrt(x) 求x的平方根% I& z5 g4 J3 Q4 r; R/ ^
- #求x的平方根+ v9 @. W' q4 b( k& n. u' p' W) {
- sqrt(x)
3 b6 o7 d: z4 e9 X! c - Return the square root of x.
5 a% ~$ [6 T w# E% M0 u0 _ - >>> math.sqrt(100)
5 }! ]1 |7 O ^% s, t/ q" G/ C( V - 10.09 K5 ]- T% e) n2 G4 Q9 p" C6 k
- >>> math.sqrt(16)
9 c5 X' [9 y0 F# p6 h0 q9 k* ^ - 4.0
# R6 T) K$ S- f4 H2 X0 v - >>> math.sqrt(20)
9 b1 G" e5 D) c# c+ W$ v( o9 d - 4.47213595499958
复制代码 . o* A* O1 _3 e/ v
math.trunc(x) 返回x的整数部分- #返回x的整数部分! o# M: T6 ?, [7 ~1 ]0 V
- trunc(x:Real) -> Integral
% v8 r/ D: P3 ?2 a+ ^$ R) z; L9 m - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
) J) I. L6 y, q, @% W3 Q - >>> math.trunc(6.789)
. ?/ _8 |1 e" `' I - 68 t z8 H! ~0 u9 C
- >>> math.trunc(math.pi), ^: k( d* m0 C2 y1 f; o& S6 U9 H
- 33 p1 ^) Q6 A. E; ?- o
- >>> math.trunc(2.567) i, \( N* G7 @- j
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|