马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- B; v4 [( g3 a: }, h. t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
n6 O0 I3 X R( `. T; L- {5 W# a7 O4 x: v c, k
方法1:+ f4 T# \( Q X
- >>> import math* i8 Z/ _0 D! n9 h" \
- >>> math.sqrt(9)1 \, b* r( {- H: s( u( O% g
- 3.0
复制代码 方法2:) e9 _0 Q+ x' t6 x
- >>> from math import sqrt# _& R; m" O y) n" R
- >>> sqrt(9)
( l3 j8 \1 F1 ]/ A0 ? - 3.0
复制代码 " D: L+ a! J- r" E
# w* {' u! U% W" Imath.e 表示一个常量
3 c& u6 F2 L) L) D5 \- #表示一个常量$ n# _: O" k+ h& `
- >>> math.e6 m5 J2 d5 E1 x/ W: D8 u
- 2.718281828459045
复制代码
/ Y5 E+ J, l3 }& u% {/ nmath.pi 数字常量,圆周率
9 h; o% F; Y, ~0 h& @2 t9 z2 [; P- #数字常量,圆周率' _# Z6 W9 k; ~$ H* } X
- >>> print(math.pi)
4 V4 r w6 L+ k# { - 3.141592653589793
复制代码
' V' [' T- b5 p' I) ?4 T$ Amath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x: \7 W" o( N) `0 c* J
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
. [) n/ i3 H) a7 j& W* _# u - ceil(x)% M0 [$ y) ~$ j. ?8 s/ K
- Return the ceiling of x as an int.
$ F6 f) S i/ q* y0 M - This is the smallest integral value >= x.% R" P* M9 c$ v6 l% e# U1 H: z
# [+ W/ s" Q1 Y, Q( q/ r# R: m- >>> math.ceil(4.01)( {( p% |: w8 o, G1 p( C! A
- 5" w/ v7 u# v7 P1 \2 i, ^
- >>> math.ceil(4.99)4 _, Y6 u/ ~) d* P4 N
- 5
/ P, B, v# j& H- }. ]. t - >>> math.ceil(-3.99)
, O7 q2 D1 x! ] - -3! y" I* |5 S6 F" N8 C
- >>> math.ceil(-3.01)
, ?' ~" V) O$ P1 P, A9 P0 m2 Q - -3
复制代码
0 B0 j0 t& ?1 S! L) R5 Z) D3 ]5 ?: {0 Gmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 C! Y& q, W+ c8 B6 {
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' ^9 d9 p0 v+ s- K - floor(x)
T* { b' g; }) q- M - Return the floor of x as an int.
7 b0 K$ p( y: t- j# n+ a - This is the largest integral value <= x.
|1 R7 ?' k; y2 c8 b/ n7 }# ^ - >>> math.floor(4.1). N4 ~5 z" e2 g5 I9 n
- 4: W1 G: B2 Z3 s8 l4 h
- >>> math.floor(4.999)
' d |; W t+ e9 i' o4 \ - 4
$ ]* \$ Z2 h. J# u/ n1 Q1 v5 f - >>> math.floor(-4.999)1 Q! R* u4 |; A. J8 k7 H3 `
- -5; c$ r7 a: A7 G8 ]
- >>> math.floor(-4.01)
, p/ G: T, Y( u9 o9 v& O! j - -5
复制代码
4 ^0 @- M }+ \6 a1 @; D, zmath.pow(x,y) 返回x的y次方,即x**y7 z. U. t. x/ P2 G. F8 N
- #返回x的y次方,即x**y
, p4 ^1 \$ @4 n8 m( ]& T; G - pow(x, y)5 w+ a; C9 v4 F$ n
- Return x**y (x to the power of y).
1 e( D. Y6 _+ W5 F8 l - >>> math.pow(3,4)
6 [- ^. P: W" a5 |. Y - 81.0
# g/ L& O- e% M" i& S - >>>
. V" \& X6 E& |4 j( G6 ?4 Y - >>> math.pow(2,7)
& c; W& m- r( K - 128.0
复制代码 2 {2 Z4 g" S$ F5 f8 m$ O0 V7 E" o
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 ?* k& l5 |. b" j7 u( Y9 Z
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ {1 ] s" \1 N) |2 x" O - log(x[, base])
$ C' m5 ?% U2 R6 ?. Q - Return the logarithm of x to the given base.
6 V; p7 Z5 J6 w- @ - If the base not specified, returns the natural logarithm (base e) of x.( b* g. ?$ W K f7 U/ E
- >>> math.log(10)
5 n) Y' R# r0 {2 h - 2.302585092994046
) \0 ]' A+ j: L8 ]" \ - >>> math.log(11) Y' D E6 \8 N: F
- 2.3978952727983707
. G3 M- S+ r O1 b6 \0 R' F2 P - >>> math.log(20)
& B( s& b5 X. I; S; ^; \& i* e1 k - 2.995732273553991
复制代码
0 a) H0 a+ c& \7 X6 N+ rmath.sin(x) 求x(x为弧度)的正弦值
# r. d) E4 h8 O- S1 |; S- #求x(x为弧度)的正弦值
+ p4 D( w% e# B9 s - sin(x)' `( k( {: }" V
- Return the sine of x (measured in radians).
( h$ w. p/ D# M - >>> math.sin(math.pi/4)
$ e2 {0 C; M- f/ l' [7 N/ C- x& u2 Q - 0.7071067811865475: Z1 m6 k1 S/ ]1 r) G% ?+ \5 M, c
- >>> math.sin(math.pi/2)
1 N2 T- v, }/ c0 q - 1.0
$ `& i6 \ }9 v7 B( ` - >>> math.sin(math.pi/3)
+ b- Z1 V5 ~! n+ _1 E - 0.8660254037844386
复制代码
" e) m7 A! m3 P+ W) o) \ Mmath.cos(x) 求x的余弦,x必须是弧度4 k$ n3 o9 ~$ V8 j' ]
- #求x的余弦,x必须是弧度
3 D, l) Q: p* L' V5 y - cos(x). L2 V) ?( i, X4 m
- Return the cosine of x (measured in radians)./ o: n+ t- y/ x8 P3 K
- #math.pi/4表示弧度,转换成角度为45度( Y8 N, [( j/ }5 v" a5 r
- >>> math.cos(math.pi/4)7 ^: @4 F6 i7 _* x3 W7 ?
- 0.7071067811865476! [. `) H( f/ [2 u- y
- math.pi/3表示弧度,转换成角度为60度
: R; J" j$ u/ H- v, X1 h" ], p - >>> math.cos(math.pi/3)
& [/ V( `- X. E5 @ - 0.5000000000000001
, [6 i& o% i: V+ [* r- w1 O7 L - math.pi/6表示弧度,转换成角度为30度
$ \7 E/ l W3 c1 b - >>> math.cos(math.pi/6)# Q1 }" H, m" h4 [# i/ N
- 0.8660254037844387
复制代码
; @- W6 M7 q. J t9 Zmath.tan(x) 返回x(x为弧度)的正切值
+ U' t2 Q9 B3 D* q c/ X* T- #返回x(x为弧度)的正切值
+ r3 Z% z7 W/ I4 Y: z2 P) N - tan(x)
" f8 l- v! ~+ D( C$ H3 x0 w3 ` - Return the tangent of x (measured in radians).. _6 O+ c* }+ g; d
- >>> math.tan(math.pi/4)
+ ], M( c+ A/ E2 ?( a - 0.9999999999999999) x; o: q3 D- |; h+ Z; \6 T
- >>> math.tan(math.pi/6)" k+ @& V/ [: u' z' N- s! y
- 0.5773502691896257 y# X, h/ [; r1 _6 p/ T
- >>> math.tan(math.pi/3)
+ v0 m7 _) j9 H) D& G; V - 1.7320508075688767
复制代码 8 }$ G f7 t2 L
math.degrees(x) 把x从弧度转换成角度
9 X& ^1 K8 R! O, |& s4 d Q- #把x从弧度转换成角度/ k9 z, I. Y. ^7 K/ Q: w" h9 z
- degrees(x)
1 Q8 M4 c8 K3 E5 U6 ~3 a' Y) l$ { - Convert angle x from radians to degrees.
5 S" X# m G% ]- B) Z - 4 T9 Y3 y4 i+ Q8 l5 {2 ]
- >>> math.degrees(math.pi/4)! R* Y+ l" e, d+ S
- 45.0* O8 h, Y( x$ D( s
- >>> math.degrees(math.pi)- g" u7 `* h& P) k- ~, o
- 180.0
0 J* z" p) x8 V/ p8 h% ]3 `. u! n& ^ - >>> math.degrees(math.pi/6), z) a6 F$ J) A
- 29.999999999999996
- @* K1 C. t% @* x1 H, i9 A$ W( x - >>> math.degrees(math.pi/3)# P2 e5 Y" u0 k7 Q1 v
- 59.99999999999999
复制代码 * g2 R( l1 Y1 Z/ ?- }
math.radians(x) 把角度x转换成弧度) [) z) m9 G7 Y- }
- #把角度x转换成弧度* z% I2 i4 C& d9 G. s
- radians(x)
2 S! e' z$ S! x6 L' D/ c' r - Convert angle x from degrees to radians.
# d* W1 P) M" T* a! [ - >>> math.radians(45)& Q: S1 R" @3 s/ [( |! z2 Z* @
- 0.7853981633974483
4 I) U+ v3 }, ]+ `' K# X - >>> math.radians(60)
8 @8 l k8 l9 B+ S$ p5 O - 1.0471975511965976
复制代码
# h% I* y# Z, D: O" `9 [0 W8 Amath.copysign(x,y) 把y的正负号加到x前面,可以使用0. C8 ]. H4 r* t5 p6 _- M0 x2 y$ a
- #把y的正负号加到x前面,可以使用0 V7 F! D1 ?3 \- q
- copysign(x, y)
& K n, f2 K# G/ e! c - Return a float with the magnitude (absolute value) of x but the sign 1 Q# d$ s+ ?0 h5 I
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
; _* |* r0 ?% z i - returns -1.0.2 t2 M5 B" I* O, p- Y' v
- ; p+ B- p6 j U3 _2 i
- >>> math.copysign(2,3)
3 R% ?9 t8 C t$ ~ - 2.0* W8 ]3 J. b! [( ]. n
- >>> math.copysign(2,-3)
! I3 R+ C2 l5 B- a' S/ [* P5 ] - -2.0
( K; R" m$ Z8 v7 h D$ l, f) v# B( G - >>> math.copysign(3,8)) k" n. y- X S) V; \; M' B
- 3.0
2 S7 Q4 f7 M* ~0 j - >>> math.copysign(3,-8), }, [' |% R$ z
- -3.0
复制代码 8 K% [4 x0 C9 ^
math.exp(x) 返回math.e,也就是2.71828的x次方7 C3 D& P# i2 @3 r& z3 Y5 e6 r5 H ?
- #返回math.e,也就是2.71828的x次方
9 `/ z8 G2 }& ^ - exp(x)
9 O2 C) Y( G2 Q; m* ? - Return e raised to the power of x.
3 p0 ?, ? E+ J5 W# U9 p. p$ {: l - |, ~* X- R! X( J+ ^/ x1 g7 D* k+ s
- >>> math.exp(1)
T# C$ u/ J, c' h8 `( m# |) T - 2.718281828459045
; }3 i E. H P6 a - >>> math.exp(2)! p; Z8 x- y5 F0 n t
- 7.38905609893065 h' m+ P9 g) ~. u
- >>> math.exp(3)/ m$ r7 v! t1 L& b6 L4 k$ e. M
- 20.085536923187668
复制代码 5 A V1 K+ }# w8 N+ b2 }% |) C# y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
1 i6 o% e: D0 O9 F) l) A( h- #返回math.e的x(其值为2.71828)次方的值减1
% r) c0 J+ \/ V: { - expm1(x)
: f, P2 U* J* i - Return exp(x)-1.
4 R2 e' o0 X/ | G0 U/ d- J - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
0 ?# j1 d/ \( Z: q/ N
( d8 t9 t+ F% I- >>> math.expm1(1)
9 Q4 s: D, K' z/ q - 1.718281828459045
$ \( H5 X- X8 [$ [( J4 o' p. g' ~ - >>> math.expm1(2)
, q) C, P" Z- P7 B - 6.38905609893065
' P" Y1 T8 y0 f: V - >>> math.expm1(3)
2 O, S% v( ]1 f- a- R7 @; u1 b( q - 19.085536923187668
复制代码 " Q' _5 D% z9 P& s _) ~) l, R0 v
math.fabs(x) 返回x的绝对值' X2 }$ z( X; k9 {; `
- #返回x的绝对值
, f4 \: }3 j- q9 E% M+ o - fabs(x)
5 x, ~! c' o7 O3 x% k# z - Return the absolute value of the float x.+ D4 Y# r8 `& t& w! |. g9 [6 m
- # j# y0 v% j5 I, G& K& h2 E
- >>> math.fabs(-0.003)
3 P/ Y6 q1 [, |+ G2 J* m4 c, U, Z - 0.003
L0 a- T Q) X' z- m) n* c7 _) i - >>> math.fabs(-110), X- A, l* D: {& X5 t
- 110.0: C x3 C; i6 e$ T0 X' H+ P: o" ~
- >>> math.fabs(100)
2 Y0 r0 C. k/ W1 s2 m; g - 100.0
复制代码 4 c+ O4 _: s, E2 X, D
math.factorial(x) 取x的阶乘的值2 ]8 m4 [$ v t
- #取x的阶乘的值( N0 B6 U y" m: A. S% S
- factorial(x) -> Integral6 L% h$ i/ }9 O/ H
- Find x!. Raise a ValueError if x is negative or non-integral.% w8 V. x8 H- S1 w |1 h: _
- >>> math.factorial(1)8 o$ J4 y) e/ g, h: J* V& X
- 1
c% X3 f% [: ~+ k - >>> math.factorial(2)
x7 a$ I7 f2 u/ e+ y5 z4 X" L - 2* U" [8 c$ D2 C8 T) T
- >>> math.factorial(3)
2 C! X0 ^8 a* X& S) @ - 6
; Y+ k) W( y% J& D, C - >>> math.factorial(5)
7 l3 [0 {4 T9 z6 i6 t$ s - 120
8 V5 V9 N( n2 ~$ @) z2 O1 B' b - >>> math.factorial(10)) C1 ^2 w6 y8 A7 ~. b: w- h
- 3628800
复制代码 ! i- B/ n% [) P, ?8 K
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 f$ g" L# s7 q9 V5 b1 l4 C$ |3 I
- #得到x/y的余数,其值是一个浮点数
0 Q9 V6 L2 B6 }- E/ s: Q2 O - fmod(x, y)
0 {# g" K G' l7 l" _ - Return fmod(x, y), according to platform C. x % y may differ.) V, r6 w0 W |& X
- >>> math.fmod(20,3)
# n8 C% q) C; b; B' \6 Z - 2.0/ u/ Q* ~# g9 p0 A) L
- >>> math.fmod(20,7). p& T# l, E: z8 d% i
- 6.0
复制代码 / \) h- ]& B4 O7 b9 E
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
0 \% Q W0 n* e( y4 G' P- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
$ f( E2 C8 e3 p( Q( D! C1 x6 X4 ] - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" ~) r' H# j+ F. N5 I) d
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
! i6 f5 F K& s! r r6 \1 w - frexp(x)
5 j# Y) x- W! \9 W' n - Return the mantissa and exponent of x, as pair (m, e).
4 r" b$ e5 {: [) h - m is a float and e is an int, such that x = m * 2.**e.
5 B" a) f B9 `9 R9 O0 _ O5 S' Q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.' ~; |8 W- N' g* E, R) u, M% `
- >>> math.frexp(10)
1 I* u9 T* l2 C0 ] - (0.625, 4)
9 f- }8 o0 R' t- K - >>> math.frexp(75)5 I; ~$ [) I, k
- (0.5859375, 7)
) s+ \4 v0 D. w' b; B! A9 f! w0 K - >>> math.frexp(-40); p' ?1 o4 h. Y% |+ i; X) U
- (-0.625, 6); `( B" }4 E* v! h
- >>> math.frexp(-100)9 q: Y- E3 z1 S9 O# G0 ]. U; b/ n
- (-0.78125, 7)
/ }# |" m. ]: L" z' d - >>> math.frexp(100)' m+ h3 L- }5 Q+ o* v
- (0.78125, 7)
复制代码
! P& P Z+ e% ~" Umath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列): X# Z5 H, h$ K
- #对迭代器里的每个元素进行求和操作
: W2 t0 S. D2 Z- S' D1 O7 [ - fsum(iterable)
+ A# a; {, P" k9 _% w. r3 T. X* d - Return an accurate floating point sum of values in the iterable.
& ~6 Z: m6 B$ \# }( H# i4 o! { - Assumes IEEE-754 floating point arithmetic.& G; |6 B6 p7 S: ^* F
- >>> math.fsum([1,2,3,4])
& D5 E W3 f/ ~' B! z) { - 10.0
] J7 c/ B0 N# W4 [$ x+ W5 f9 e - >>> math.fsum((1,2,3,4))
5 {6 d& i4 A" u - 10.0
9 H5 [/ {2 m3 o4 u& L - >>> math.fsum((-1,-2,-3,-4))
4 L4 J" I3 K* S% c; S. ^, P+ Y8 G - -10.0
6 M7 |2 ]1 T) o- o8 b - >>> math.fsum([-1,-2,-3,-4])
5 H* ]2 X L5 E3 M - -10.0
复制代码 9 Q, L3 F; X5 j3 @' b# g* h
math.gcd(x,y) 返回x和y的最大公约数
, P/ v: ?2 m& r& }+ w4 U% ]- #返回x和y的最大公约数
! C7 ^& p+ I6 U: Y - gcd(x, y) -> int, ?' `% Z) Y" D' K
- greatest common divisor of x and y9 r5 \1 l4 `( G1 N$ { }! J
- >>> math.gcd(8,6)" U. m3 ]. m% N' d# n4 A8 u: p, r( _
- 26 ?2 o2 r& b' H2 q8 C7 F8 j# C7 {
- >>> math.gcd(40,20) W. u |5 M4 ~* k
- 20- H1 }1 j" K$ \ K, D2 [
- >>> math.gcd(8,12)
" x7 u+ N( j# c( Z5 b: U3 F4 x - 4
复制代码
7 _5 ^8 R& @; \* Smath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
6 d; w" w8 |& e4 c' x, _- #得到(x**2+y**2),平方的值0 a- j( }7 w- P& B' x
- hypot(x, y)
$ C* v8 }. I+ U' C9 x: }& V7 G& `1 X - Return the Euclidean distance, sqrt(x*x + y*y).% B; Z: Z5 v$ U2 {- t4 ^) N
- >>> math.hypot(3,4)" f( d, r/ `# B: P
- 5.0
1 _# x) t8 [& q4 W6 Y* a# z. h! j - >>> math.hypot(6,8)
, g* y1 h a* w9 ^9 E7 v - 10.0
复制代码 k& P( k, ~7 A/ z) L
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False2 C) E' b) u8 l* f8 I- j4 N
- #如果x是不是无穷大的数字,则返回True,否则返回False9 F0 |& A/ J4 O
- isfinite(x) -> bool/ m5 U5 P) u, Z3 ?/ R: k
- Return True if x is neither an infinity nor a NaN, and False otherwise.' b* l. _& c# T( p9 J* X
- >>> math.isfinite(100)
* f! {9 u- L& \+ r Y5 {7 n - True
K3 o, E- P; ?- u4 V! _0 ]. ^# a0 h - >>> math.isfinite(0)
, u4 I) p9 e: g8 e# i K- i' J - True. N* ^; O/ n. q
- >>> math.isfinite(0.1)
9 J Q' W# S/ b/ R" Q9 r2 O - True
! p6 h$ y- Q4 Q - >>> math.isfinite("a")
' {% b6 x9 Q$ K - >>> math.isfinite(0.0001)
9 t0 u i5 Y% f1 y8 m - True
复制代码 8 l+ C+ ^5 e* G8 ]6 K
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False+ L4 m; \* D9 p3 `
- #如果x是正无穷大或负无穷大,则返回True,否则返回False4 s1 z" l. T; D* j
- isinf(x) -> bool
5 W& Y1 |* |6 ]" L# Y1 l3 a - Return True if x is a positive or negative infinity, and False otherwise.
, K6 v3 k# Y! V! E" { - >>> math.isinf(234)2 a8 w# U5 G. j& B/ O1 p; b& x
- False! j# n3 t7 y8 f% P+ T
- >>> math.isinf(0.1)% Z8 i3 _, v0 F8 a# W
- False
复制代码 & ?% P3 Q& V% C$ g
math.isnan(x) 如果x不是数字True,否则返回False
7 k) E! u* Z3 ^6 M: G- #如果x不是数字True,否则返回False' P7 W% s5 r" S9 ~5 r* E p3 w7 F
- isnan(x) -> bool% `9 [. d. G% k/ n
- Return True if x is a NaN (not a number), and False otherwise.' u+ H" {1 ~% L" @
- >>> math.isnan(23)
, l _+ u; y U( j5 j2 u - False. [! N; `- o" ]- E# y/ j3 p, s2 } O
- >>> math.isnan(0.01)
$ T& e1 A; X4 _4 t# S4 F2 m - False
复制代码
7 e' P, K' m" r. Y8 i6 f% ^math.ldexp(x,i) 返回x*(2**i)的值
( n K. Q1 G& }5 S- #返回x*(2**i)的值
, l8 T$ X; L( _4 M' R - ldexp(x, i)7 V2 U; a) j- _( v: m" |9 b
- Return x * (2**i).8 \, z( o L; }! W7 E9 p- Y% o! S9 `
- >>> math.ldexp(5,5)
& o3 _& }7 s7 F7 Y5 { - 160.0+ J3 [& z+ J8 [: v
- >>> math.ldexp(3,5)) m* f2 O/ u* k5 P! t' w
- 96.0
复制代码
) ]: N+ I. E8 O4 p0 f; N7 ~math.log10(x) 返回x的以10为底的对数7 [8 k( R/ X2 G) t/ j# p
- #返回x的以10为底的对数: W; x& N9 E( s' M; R3 H4 i9 Y
- log10(x)0 T z8 ~8 N A/ b( D9 D' ?
- Return the base 10 logarithm of x.9 d9 b1 F; O/ C! [' R
- >>> math.log10(10)
6 }2 ]* e1 s4 m, \ - 1.0, y: B9 w; Q4 D8 `
- >>> math.log10(100)+ Z* f0 |' C2 R, a: d' C; ]" Y* R' Y; [
- 2.0
" S6 y8 x8 ?/ e; E - #即10的1.3次方的结果为20
7 Z9 j. S1 c- }" l - >>> math.log10(20)& m, t" U; U; g% O% g: O1 x( s
- 1.3010299956639813
复制代码
3 f) n# @/ h6 t$ r3 T Ymath.log1p(x) 返回x+1的自然对数(基数为e)的值
7 N* Q O( e0 c+ o- d/ ]- #返回x+1的自然对数(基数为e)的值) K" E' L5 f) O
- log1p(x)
, K3 D" q' W7 Q( [% u7 W - Return the natural logarithm of 1+x (base e).3 g1 p* t" O" V& b5 B5 [% u9 A
- The result is computed in a way which is accurate for x near zero.
8 h# @6 E$ f! g# ] - >>> math.log(10)9 |9 \, x- U/ E z
- 2.302585092994046
r" q, ^; G" G: B# {# x/ ?2 M0 X - >>> math.log1p(10)
o6 M! {" z) H( w `5 P - 2.3978952727983707
( V6 o( X! s5 T/ ^; |4 g - >>> math.log(11)& o& d& m# q& r! _! j1 C
- 2.3978952727983707
复制代码 0 ]+ n1 q E/ l! v" Q9 i
math.log2(x) 返回x的基2对数
0 o8 G( ~: d4 K5 P- #返回x的基2对数
5 E- q. ~! k. G0 V) d - log2(x)! {; k$ g2 N" V2 N
- Return the base 2 logarithm of x.
2 @% N# S0 H$ G4 `( w! r - >>> math.log2(32)4 [: r7 J/ } v/ H# a. K
- 5.0
8 K% v* w& }) ^) P' y6 f - >>> math.log2(20)
, t. q' \# o+ f- h - 4.321928094887363: q2 R$ { o/ h$ r4 f7 |; q' D
- >>> math.log2(16)& O7 S% E7 J( N0 ]6 [) ^* r& `
- 4.0
复制代码
3 q$ }4 D8 {) D/ vmath.modf(x) 返回由x的小数部分和整数部分组成的元组% W6 y: H, q" N6 S4 D* X
- #返回由x的小数部分和整数部分组成的元组
# D0 ?. K9 }2 B7 L1 V - modf(x)9 g/ M* k7 O: K V9 k
- Return the fractional and integer parts of x. Both results carry the sign
% S5 F8 n+ x7 V - of x and are floats.; s: I$ n d5 q- V3 q+ Q
- >>> math.modf(math.pi)
- r1 ^7 c6 `- c4 K" L( D. ~ - (0.14159265358979312, 3.0), ^2 c. _( T! C
- >>> math.modf(12.34)
* a. z" V: c+ f* x; R, L; x3 c' d - (0.33999999999999986, 12.0)
复制代码
- k* S0 V9 N3 ~0 k+ S3 hmath.sqrt(x) 求x的平方根
* T* J' i$ t" H- T- #求x的平方根
( C' ~- c b$ z - sqrt(x)
) U% ~$ s- {* k, q0 o - Return the square root of x.
, A% u+ F! g5 | - >>> math.sqrt(100)+ S4 y0 |- D a9 E- l* `% V! n
- 10.00 Q* ^, V D, p' S, Y( t x
- >>> math.sqrt(16)
) g A9 i. @: p - 4.0
: j+ Q! T' P [% g8 r* t+ u3 U - >>> math.sqrt(20)2 V. G+ n' H3 {' l; X! E
- 4.47213595499958
复制代码
6 t: ~6 [4 ]( a3 S/ }7 f* g; ^math.trunc(x) 返回x的整数部分- #返回x的整数部分
1 g5 J( x% ^( F* M3 Y4 y - trunc(x:Real) -> Integral$ A% a) J: G5 J
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
2 Z6 y/ i! K$ C' U - >>> math.trunc(6.789)
- l& c+ e% }4 H - 6
/ a0 y6 s$ Y/ s9 x5 N- w - >>> math.trunc(math.pi)* q8 L- L/ a, f, l
- 30 M6 |6 S& m; n9 k
- >>> math.trunc(2.567)0 ^" q$ ^8 q* l
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|