马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ O& v* K3 Q$ r) l【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 v% O/ r2 t* t+ s+ H1 d/ l) H) G+ l% O X! [* W
方法1:* V/ }, d/ K9 p$ H& q. C C8 }
- >>> import math. {& b0 f( r8 Y- D
- >>> math.sqrt(9)
% u% s( b' }1 c; v - 3.0
复制代码 方法2:! m1 {9 S, F* I+ o
- >>> from math import sqrt
) D9 N) D& m' L - >>> sqrt(9)* G0 b- \- {! e
- 3.0
复制代码 3 I7 N4 b1 H' \" P% ~& b5 Q' Z
) }" o' u4 G' ?" D1 Xmath.e 表示一个常量/ C7 H) ]2 W9 |0 m5 j4 \, U
- #表示一个常量+ v! L. k; ` c8 D5 i9 W
- >>> math.e
, l& K( ]5 [+ ^7 h" \6 F9 m- | - 2.718281828459045
复制代码 6 c" _, }4 T4 z! E) \' G
math.pi 数字常量,圆周率
& F1 W+ _+ {8 g0 E. r( W% h! V- #数字常量,圆周率
) c w K( [" J [# M! H( r) O4 R - >>> print(math.pi)% ]% r' u2 ]# ~. p
- 3.141592653589793
复制代码 3 G2 @# t9 W0 L1 w/ K2 T
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 s5 L. J( p* }6 A- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
) {) n3 b; l* k/ ^) H9 Y - ceil(x)
0 J+ M) I0 ?& o5 M; Z `. N: H- V9 M - Return the ceiling of x as an int.. X( y0 j' g& Z$ q' Y6 Z _
- This is the smallest integral value >= x.% I2 E% O5 M9 U1 V
- 5 W& @1 W r5 s3 r. Y n
- >>> math.ceil(4.01)
9 | ~( R- u5 y9 b8 z% I - 55 m/ g2 N* @4 O
- >>> math.ceil(4.99)( B, R; g1 \+ l+ f. S" F
- 5
1 W6 n; V8 Y$ C+ H |4 G, I - >>> math.ceil(-3.99)
* g! o/ A6 {* b1 M - -3
. ]8 P. N' {$ \0 {- @( @) u - >>> math.ceil(-3.01)' R% s+ J% ?* {' `& n) h3 K7 s
- -3
复制代码
B. q5 o+ N9 Umath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身% a, Q5 b: l. P2 r6 Y; w
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 o v7 w# u1 b' `' i N( ?# w - floor(x)
" r# l9 O. h6 V$ j6 L - Return the floor of x as an int.6 \# w# k! k" ]" W p5 Z
- This is the largest integral value <= x.
; m* y- g6 T4 v! |3 l& r - >>> math.floor(4.1)
4 v- Y* Y/ I4 x9 S# s - 4
. j# @4 T7 @/ O3 k! i5 U - >>> math.floor(4.999)( J8 L) S6 T: t7 `" R, ~
- 4( L8 j+ A; Y( A2 D
- >>> math.floor(-4.999)1 P. f* Y9 g I& X5 N" d {
- -57 o/ {: ~- s) p( Y8 K
- >>> math.floor(-4.01)
, T* E* O$ I( ~4 l5 T# x - -5
复制代码 / H E/ d; s/ L: j2 P" {
math.pow(x,y) 返回x的y次方,即x**y ^* O ?, l. E6 w) q1 j: V2 l
- #返回x的y次方,即x**y& V7 |" s J9 N+ t6 [
- pow(x, y)6 ^) y( E' Y: H0 F! C+ O2 e" U5 n
- Return x**y (x to the power of y).- a) K: [5 N+ m$ k
- >>> math.pow(3,4)5 r( Z0 E: g) L/ X8 a6 j
- 81.0
5 `+ _! A p3 Q7 v - >>>
: f- g: ^: r0 e! q5 h# X! \ - >>> math.pow(2,7)/ l: M O7 Z- i3 q# ~% c
- 128.0
复制代码
# K4 E* W/ r: [# ?$ L8 Rmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& G9 g0 N8 p2 d+ Z! [% ~& i- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) |7 k3 N5 v9 a" r4 k! r; b b
- log(x[, base])
2 n0 W; ^- t3 O3 L- @/ C* B) g( X - Return the logarithm of x to the given base.# O J! c3 l- K9 I
- If the base not specified, returns the natural logarithm (base e) of x.
! V2 j: @+ O3 }/ t H2 `& a - >>> math.log(10), [8 l, g6 q# }. F" d
- 2.3025850929940462 V" c1 ]+ }* U: a
- >>> math.log(11)# p& f( X4 T/ C5 j) V" o
- 2.3978952727983707$ f3 n0 N* z% b; Q
- >>> math.log(20)2 Q/ ?2 f5 e, U$ H/ ~
- 2.995732273553991
复制代码 1 ]6 p; v5 n+ s; |+ H2 ~5 R
math.sin(x) 求x(x为弧度)的正弦值
) z0 Y3 C0 m! ?- #求x(x为弧度)的正弦值- k' z. A; V. J- \% O) \, J( _3 u- B
- sin(x)
8 Y; k6 S$ b9 d, K$ x - Return the sine of x (measured in radians). X2 N* _& S9 ?! f
- >>> math.sin(math.pi/4)# c* f) {& h9 J# m
- 0.7071067811865475
9 e) C- ^# I. d. k$ o" g2 H - >>> math.sin(math.pi/2)
F$ ~! r' I7 ]' N: t - 1.0) \% u# w o0 D) p0 k" O: k8 b: A
- >>> math.sin(math.pi/3)! n* T- u% l- e; e5 }) Q
- 0.8660254037844386
复制代码
/ D( n1 x6 E0 [2 i. C9 {+ `math.cos(x) 求x的余弦,x必须是弧度9 C1 |$ Q- \9 S! s d& O
- #求x的余弦,x必须是弧度
. `. i+ y5 X) J - cos(x)4 o0 ^! } S1 \$ _' p# [
- Return the cosine of x (measured in radians).4 `, l8 h* l+ k# o( Y% ]" K- l4 a
- #math.pi/4表示弧度,转换成角度为45度
' \" _ ?0 n+ G( ~; B% v - >>> math.cos(math.pi/4)
( ]9 X C$ K, p3 z - 0.7071067811865476 S1 E X# M& Q8 v
- math.pi/3表示弧度,转换成角度为60度3 k; D4 ]- _/ F
- >>> math.cos(math.pi/3)
4 U* \4 j7 R; `0 }# d$ W - 0.5000000000000001
# i3 L. B6 J! }9 U$ J+ E. | - math.pi/6表示弧度,转换成角度为30度) D1 _) `" L6 u
- >>> math.cos(math.pi/6)
) |+ @) N# c; T! r K) ~ - 0.8660254037844387
复制代码
9 z: R) z6 K8 smath.tan(x) 返回x(x为弧度)的正切值
2 w+ L [9 S5 X' r- #返回x(x为弧度)的正切值
0 O2 Y( J% a6 D; m - tan(x)
# b$ l7 n; R7 L: ^3 i* m - Return the tangent of x (measured in radians).
" E1 S; P0 l& r# C" T, q - >>> math.tan(math.pi/4)" c" A M/ s$ g( K2 {4 C/ B
- 0.9999999999999999
2 M& X z, T5 H5 }. f/ t - >>> math.tan(math.pi/6)
( G+ x5 B" B) e+ k0 ?$ Z - 0.5773502691896257
7 i+ M' \$ U) C; A- O! i! Q) G - >>> math.tan(math.pi/3)1 M% C3 t% ]$ y2 j
- 1.7320508075688767
复制代码
5 q* @+ `) L+ t9 Y! p' q4 W' {, Fmath.degrees(x) 把x从弧度转换成角度
^4 g) t1 j' ]7 h2 l- #把x从弧度转换成角度
/ `2 J2 s2 o8 k- D u9 T* y - degrees(x)
; q& p0 f4 m- c4 P- U - Convert angle x from radians to degrees.
$ K% Y6 D6 j9 o6 R2 `
% W( I, g. h0 x2 H0 q1 t- >>> math.degrees(math.pi/4)6 _7 C0 {; ~$ s1 T+ l' s9 y5 N
- 45.07 @9 C6 C( B, C$ q
- >>> math.degrees(math.pi)
2 M( E( M* g8 K A r - 180.0
2 j; [% m& f |, L; o) y. l# F - >>> math.degrees(math.pi/6)$ w8 j/ g( l+ H( d+ h
- 29.9999999999999968 ]8 Q# Y+ j q! z
- >>> math.degrees(math.pi/3)2 a F! @8 J6 E0 R* U
- 59.99999999999999
复制代码 ( m+ p8 `3 a- n5 f
math.radians(x) 把角度x转换成弧度, c/ Z! @: }& v$ d! J* s/ ?) R
- #把角度x转换成弧度
. b, s, r# Z' D) S0 C9 l9 u+ s - radians(x)0 a! J; K. W; `( F8 O
- Convert angle x from degrees to radians.' ?* h6 E$ |* i: N) T" V( X
- >>> math.radians(45)
) A7 M' e3 R# T o" _6 L9 _1 b - 0.7853981633974483; p8 e& q! i" Z t$ G
- >>> math.radians(60)
/ @8 m( p' a- x; u8 f9 d. I - 1.0471975511965976
复制代码 3 d' m s7 J I8 d; G- W. i. o
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
4 H/ q! l: z, {- #把y的正负号加到x前面,可以使用0# d: [1 o1 Y4 [) c
- copysign(x, y)) M n) j, v7 E) B- E$ q
- Return a float with the magnitude (absolute value) of x but the sign ' B: J' o7 j$ Y3 P4 }& @
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) + {& L. \# E( D+ o8 g3 J/ j A
- returns -1.0.7 i4 e3 P/ t2 d+ O! l: H! ^2 L/ y
- 1 {+ J K( Q! M% Z
- >>> math.copysign(2,3)2 b5 E0 O: N5 ?5 a4 f& J
- 2.0
; ^) T- I- ~! A7 N. G - >>> math.copysign(2,-3)
7 m( H* J' g; q# ^7 {5 T - -2.0) I( m% T& `* o
- >>> math.copysign(3,8)6 R" u& R4 ~. E3 @* Z
- 3.0' C) u1 s% _6 b/ i
- >>> math.copysign(3,-8)
) M i; K1 ]0 x3 J. J0 g% O - -3.0
复制代码
7 B7 Z. K1 D# ?# }& e E$ o# Amath.exp(x) 返回math.e,也就是2.71828的x次方4 a: S( p3 H7 o# F; v8 h
- #返回math.e,也就是2.71828的x次方: s5 h% z2 f' J5 O
- exp(x)+ x$ x4 {. y; [0 a
- Return e raised to the power of x.# U* \* }8 J6 p# }8 d
- $ d6 {& k$ B, c7 p2 j
- >>> math.exp(1)
6 W2 A6 C+ z6 @6 w5 }3 ` - 2.718281828459045$ \0 m8 {9 b1 o1 w. z! D- D6 l
- >>> math.exp(2)2 @0 f/ q. k1 n+ S# g
- 7.38905609893065
6 v0 D& ]0 j( {) ]* l - >>> math.exp(3)
0 Z2 B% N! o- i5 Y, m - 20.085536923187668
复制代码 9 ~! T$ o2 ~9 M
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
3 b q" U7 B* t0 j' P8 X p- #返回math.e的x(其值为2.71828)次方的值减1
( Z7 u4 W5 F. Q, C3 c: b) |. u) O - expm1(x)
3 Z* G) a! p1 J' G" f: K - Return exp(x)-1.
2 c; v' Y0 N) m- l( \0 R) v: V - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
3 ]0 e& w0 C: s) y" T: ~$ O/ F! R% W
) M; i) u# N; E7 l4 L- >>> math.expm1(1)4 L2 X% n( F! ~( y+ h3 G
- 1.718281828459045
1 b) I0 ~8 n- w - >>> math.expm1(2)
9 v1 z2 v, l9 Q& _; I ~3 F - 6.389056098930653 t/ q) ^# J; u& E) P
- >>> math.expm1(3). Y6 K4 f% f k, @' t
- 19.085536923187668
复制代码 t+ r6 n$ C. O$ A% B8 R
math.fabs(x) 返回x的绝对值
1 D0 J! O$ ~5 C! ~8 Z- #返回x的绝对值
7 h- x2 a2 ^: _% v+ B( i, ~. Z - fabs(x)9 _1 ~1 `) P& @. S/ V
- Return the absolute value of the float x.
/ M1 O3 w! W8 F) h u, @$ Z U. B - ~" y% `0 I+ X' l% H
- >>> math.fabs(-0.003)
v+ R' P' P; U4 v/ D( V" ?" k. s) l2 V7 G - 0.003# ]8 T4 X/ [" p4 O
- >>> math.fabs(-110)3 e8 s8 f/ D- j5 S8 Z% L/ x6 f
- 110.00 r+ e X. S Y& r6 Z: Y
- >>> math.fabs(100) D8 c5 c, }, [' p; N
- 100.0
复制代码 # B; r. J* p$ s' D
math.factorial(x) 取x的阶乘的值
7 p; n* V1 G9 ^3 _# P3 E- #取x的阶乘的值- d$ Y5 y4 p5 ^ `% o' K
- factorial(x) -> Integral2 H" W6 a! N1 h
- Find x!. Raise a ValueError if x is negative or non-integral.
0 K/ a& k' X5 Z6 s+ a" l; _ - >>> math.factorial(1)
; F3 Z: U! q! e2 e: ^ {1 j - 1
$ S- D" y8 d( s" e( M) s - >>> math.factorial(2)
( j+ ^$ u( V9 i$ k3 l% O" z6 Y - 2
" @, x$ B1 F4 O- Z! o( A7 c, f9 h Q - >>> math.factorial(3)
6 `9 G/ C0 `) j1 j - 6; y7 F- o: a" }4 w, U" X8 O
- >>> math.factorial(5)6 a9 f. j' c' E2 V* y( _
- 1206 B2 N8 X/ i, q W- d
- >>> math.factorial(10)
/ w7 v2 `4 R- K! v - 3628800
复制代码
: {3 }' c A* N9 {$ Kmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
9 ~; S( z1 a* V5 \' ?" f* ^9 V6 r- #得到x/y的余数,其值是一个浮点数( _4 y! {( Z" y
- fmod(x, y)- I$ Z& w# H4 p- h/ ]
- Return fmod(x, y), according to platform C. x % y may differ.# u7 t/ K# y2 w; A* x
- >>> math.fmod(20,3)
, q, P2 r+ w# m - 2.0
I+ N( K, `, e5 ~. D4 ]' ` - >>> math.fmod(20,7)& ~# I8 \2 l# ]4 T; D
- 6.0
复制代码
3 z' _. `) A7 K/ K* ~math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围: s* y' b9 `: e! X. m1 J
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
0 G) r* w5 v. v. H8 M - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ e; x2 b ]# V* u7 s- h, j
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1* U) j6 A4 d1 h3 u1 |
- frexp(x)
- H5 t( Q( }4 F3 I' q. W! Z - Return the mantissa and exponent of x, as pair (m, e).
* ?( O$ R2 [ x% m2 _ - m is a float and e is an int, such that x = m * 2.**e.: V! k) o$ ?2 S' d5 n" \2 V$ d
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 z' g; }* v" O1 T - >>> math.frexp(10)* j3 F' V) w9 q0 d2 p
- (0.625, 4)) A1 B1 ]0 Q, a, y
- >>> math.frexp(75)8 f, ?+ l9 d- j1 P. \
- (0.5859375, 7)! B" N1 ] V# I3 p' Q+ r' c
- >>> math.frexp(-40)3 ]: i% m$ P6 m0 m& ?% n" Q
- (-0.625, 6)' n' P$ H k" s$ r
- >>> math.frexp(-100)6 ~8 J# B, v4 ~
- (-0.78125, 7)
+ h2 ]9 n# _$ Q - >>> math.frexp(100)
. V; }* f( T6 t% `3 G - (0.78125, 7)
复制代码 5 B( l q \. S% y/ t. g
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
9 T. t$ J' N! F' S- #对迭代器里的每个元素进行求和操作6 K$ b9 a1 S7 ~2 m( z% _
- fsum(iterable)
( N2 ~" j' A. X% r5 B; I1 T - Return an accurate floating point sum of values in the iterable.
& R) Q5 f' J* q* w) q$ ?1 x - Assumes IEEE-754 floating point arithmetic.$ ]9 @+ \; ]- @' o$ `
- >>> math.fsum([1,2,3,4])
( O" x! n" o8 I8 N; @8 S( M - 10.0, V7 D' b6 f1 m& M
- >>> math.fsum((1,2,3,4))
. y h- X- y- n - 10.0
' O+ I( X/ K |% Y+ _: ^& K - >>> math.fsum((-1,-2,-3,-4))! @. E/ @5 U$ Q; o$ a# O+ R) j
- -10.0
) n5 h/ z5 J' Q: Z9 |+ X+ T6 x - >>> math.fsum([-1,-2,-3,-4])5 q- A$ I W9 t: f# a/ c1 t
- -10.0
复制代码 ! t' V( ]; H4 W/ P1 p& {
math.gcd(x,y) 返回x和y的最大公约数
6 Y: P9 m8 o" s7 r- #返回x和y的最大公约数3 q2 F6 _$ y6 [- [6 E A
- gcd(x, y) -> int
. u/ |) O: l2 w! w5 v: U9 u - greatest common divisor of x and y1 m1 k' s: P) g+ I6 X( E
- >>> math.gcd(8,6)
6 p: b+ }. e6 h5 z - 2$ G: f0 ]$ U/ u& E5 t
- >>> math.gcd(40,20)
\- Y: t5 a2 {) U" E2 L7 C - 20
3 j- O2 P8 V; \1 J% I' M4 f - >>> math.gcd(8,12)
& x- c6 u7 c. V- z - 4
复制代码 ! _: v' ^% j+ b1 O0 B @
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
" ~! R0 g4 e6 F) v- #得到(x**2+y**2),平方的值
) o( T1 D& Z, L2 R7 c - hypot(x, y)
2 k7 z2 E$ N1 P) v6 u - Return the Euclidean distance, sqrt(x*x + y*y).) q- H1 {2 d5 s! S! D
- >>> math.hypot(3,4)% E6 ]$ s0 G8 L) K% m
- 5.0
* S9 ^7 R& Y/ Y. r7 I' M6 _ - >>> math.hypot(6,8)+ \0 h- I4 w9 j3 c! J8 B9 U
- 10.0
复制代码
; w+ I: G% e0 K' x. Vmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
$ n2 Y7 ~! P6 p# ~8 [5 u- #如果x是不是无穷大的数字,则返回True,否则返回False3 U% j9 Z. `/ _$ }
- isfinite(x) -> bool. k9 V: T& p) g: X
- Return True if x is neither an infinity nor a NaN, and False otherwise.( M. |7 ?9 T! w) r* x
- >>> math.isfinite(100)8 T, E% [; h1 W$ I" t& k
- True. C' ^: ]% p- j7 t$ }- n0 h$ n; r
- >>> math.isfinite(0)
7 b* K+ C0 ^: J, y% [2 I - True
2 Q: u: j3 S2 E% ?. Z - >>> math.isfinite(0.1)4 V7 z2 U P# V* g( x; V: c
- True
5 P J3 ?9 A4 \0 b - >>> math.isfinite("a")7 i. g! j; }5 G- H; I, l
- >>> math.isfinite(0.0001)
9 t! z Q6 \. @, J - True
复制代码
( ]) i1 k2 n+ k( T- A* i3 rmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 g) Z" c9 Q7 Y! T# L* ?$ D- #如果x是正无穷大或负无穷大,则返回True,否则返回False. g7 m9 ]( a( X7 U" P
- isinf(x) -> bool; ~- ^% D6 K; X; q1 ]
- Return True if x is a positive or negative infinity, and False otherwise.
2 X1 B$ Z- ?# O, \ - >>> math.isinf(234)
1 T# Q7 t! ?. v# w l - False
* V6 ^' @9 I2 c, V - >>> math.isinf(0.1)
, Q9 l9 z" k5 b1 A2 p# P+ G2 Z - False
复制代码 ; _* @) a. X. P8 @: o2 s1 I% R
math.isnan(x) 如果x不是数字True,否则返回False
1 k: C; V: q& O+ F0 E/ a( }: e5 j- #如果x不是数字True,否则返回False# o* x$ g8 A! m/ r; e1 M2 G3 s
- isnan(x) -> bool
; T7 f3 M8 ` R% p - Return True if x is a NaN (not a number), and False otherwise.# \' A' y6 c5 a& V1 |& g' x
- >>> math.isnan(23)
. }4 n& l( i# o! d9 x - False- X7 @) l& ?' Q
- >>> math.isnan(0.01)$ |; u3 P7 Q* Z
- False
复制代码
5 i' @: m+ u0 v- ?# q2 Rmath.ldexp(x,i) 返回x*(2**i)的值
1 `( Q N, P6 ?- #返回x*(2**i)的值) Y- Q5 W5 ~0 |, F o" }
- ldexp(x, i)4 Q k! @0 U# z4 Z, ]: i
- Return x * (2**i).
" C# e1 b8 o8 C L5 r' Q - >>> math.ldexp(5,5)
4 l4 c% J' I6 R0 m" s - 160.0
# W. m' P& `4 k _% r4 Z1 v7 F; t - >>> math.ldexp(3,5)
2 I5 i2 ? ]9 J! @# n - 96.0
复制代码
3 Q5 [0 C$ \( ^9 t5 U' h" g' ~+ |* mmath.log10(x) 返回x的以10为底的对数
" K' p$ U5 `: e1 s6 m- #返回x的以10为底的对数
$ }! k. N& f( z - log10(x)4 C2 v$ `9 q% X4 q/ V" e9 F
- Return the base 10 logarithm of x.
: c; F" j9 R! @ - >>> math.log10(10)1 P6 ^7 y- Y' Y Y! V. }
- 1.0
3 c" {9 J- V8 S) v+ Z( t' O: ] - >>> math.log10(100)
& S3 R6 e! p/ N/ F - 2.0
* T- x ?% B9 F% P. y8 O" `( W: Q$ C* ^" r - #即10的1.3次方的结果为20
4 y5 f. _# ~+ r8 ]; y - >>> math.log10(20)
: T3 \( F1 e( {. B - 1.3010299956639813
复制代码
2 s O" u2 m% E/ jmath.log1p(x) 返回x+1的自然对数(基数为e)的值: Y0 B# G) c! `
- #返回x+1的自然对数(基数为e)的值9 ]: @; c( i) A' h) T) [
- log1p(x)/ f3 s H/ T; q7 ]1 Z, x( q8 k. A R
- Return the natural logarithm of 1+x (base e)., V. q) ]9 n4 t2 U8 ?, s
- The result is computed in a way which is accurate for x near zero.
$ R0 G5 F: T# S: f& e/ h6 F - >>> math.log(10)
% l- g& H4 C5 y# [. g - 2.302585092994046
$ q/ t% E2 O" H Y) u+ b& M4 D5 B' ^9 I - >>> math.log1p(10)% B% p- ?9 w F- j& [$ j
- 2.3978952727983707
, I3 Z z" w5 \* _/ h# X, s - >>> math.log(11)* ?% F& h- {( P! b
- 2.3978952727983707
复制代码 - i5 b8 ~& k/ H( \: B# F. Q( T
math.log2(x) 返回x的基2对数1 }# g: e3 D! _/ A
- #返回x的基2对数7 H9 K: n8 W9 k9 ?2 U$ @
- log2(x)) [% Y1 {% J3 Z2 ]% s& \
- Return the base 2 logarithm of x.
& n. V$ P4 h' R - >>> math.log2(32)9 @( }, w# i3 A# T5 W5 K( ?$ m& g8 q
- 5.0 E; ?+ `$ [, W
- >>> math.log2(20), n- n- P2 }) n! y
- 4.321928094887363( r8 Z, B& ~2 B+ {0 ?4 |; g
- >>> math.log2(16)/ y% I) @) \9 f) ^& i" J
- 4.0
复制代码 ' w0 Q$ ?9 e, E9 V3 T, b+ [
math.modf(x) 返回由x的小数部分和整数部分组成的元组$ x0 l2 F: {, A* J
- #返回由x的小数部分和整数部分组成的元组
( K& O5 @* Z* S9 L1 Z% P8 m6 }) B0 v - modf(x)
9 _7 Y5 ?, z6 l' _( N5 } - Return the fractional and integer parts of x. Both results carry the sign
3 d) _! l, m" _: v! Q8 p8 f( t1 { - of x and are floats.8 @& w ~- v* U+ L7 N
- >>> math.modf(math.pi)
' F3 E6 }0 T' K8 K3 B. N - (0.14159265358979312, 3.0)
( ]% Q, Y: L+ \# C8 Q7 C - >>> math.modf(12.34)* V/ m; @! b* T, M! v/ D
- (0.33999999999999986, 12.0)
复制代码
( j4 U; i/ g: K- s0 V6 Z* R0 c. v( D9 ?math.sqrt(x) 求x的平方根
" G% R* z: w. ?; n+ M2 L: r- #求x的平方根
, b9 a* x/ S9 o+ N$ P5 @ - sqrt(x)
5 R/ }: o" H) L6 |* }. p - Return the square root of x.% X8 ?: x; k: d, D1 o( l& @% y1 I
- >>> math.sqrt(100)
. ?6 L J9 z5 p# j - 10.0. W5 a' @& v) [+ Q: K2 G" j4 T- q$ u
- >>> math.sqrt(16)/ }5 i6 c4 t8 g/ k
- 4.0# [$ o/ b: u4 u! Q# N5 E9 A% d
- >>> math.sqrt(20)
6 U7 f. `8 c. _# O3 F# R7 L7 G - 4.47213595499958
复制代码 # S k* W6 D) A3 s k
math.trunc(x) 返回x的整数部分- #返回x的整数部分
3 G. G. `. u7 e: K0 _/ J - trunc(x:Real) -> Integral
7 z% G: [2 w ? - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ s8 G/ r Y8 _* X" ~! ^5 v
- >>> math.trunc(6.789)
, z5 p% e: x8 A' t2 k) m - 6/ C. B: D$ ]- t" E
- >>> math.trunc(math.pi)6 S3 r( k/ U9 S9 [9 ^1 N
- 3
* ~8 a) K7 v- J7 l( O) z" } - >>> math.trunc(2.567)
5 ?9 g( P: z2 L8 Z/ V# Q" z& u- b - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|