马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- @' H, \8 R+ ^. m【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; T q$ H, S8 m5 I: o$ i) ^( x; _* L$ b( u l2 `1 _7 x. g0 m
方法1:! T- \- T `) A6 C
- >>> import math
) U$ V, F `6 Z2 M - >>> math.sqrt(9). [# z7 a: _1 }3 K; j, e$ P$ ?* R
- 3.0
复制代码 方法2:5 T, o+ D) Z) o c
- >>> from math import sqrt
4 B8 h+ Z4 A+ C' N: y% b - >>> sqrt(9)
4 r1 M3 C7 ]2 X, n% ? q - 3.0
复制代码 0 s6 Q& t2 w8 X# M0 ]
9 e/ F5 E' G4 l4 b, _: V& p
math.e 表示一个常量
* u0 X4 w' v0 t1 H! k- #表示一个常量: ?1 F, F) X0 H
- >>> math.e
% r& G0 D! g" k) [9 e/ D - 2.718281828459045
复制代码 ) q. a+ m5 c- I1 r6 H; a& j! o8 V
math.pi 数字常量,圆周率
1 Y U2 i7 H* s- #数字常量,圆周率) Q/ d, {- P7 R3 N* X7 E
- >>> print(math.pi); H) A" Q- [) h- J0 M% v
- 3.141592653589793
复制代码 : \# |8 G x$ j: ?0 J) b
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 O1 E5 L5 n0 i# W9 x! c" Z8 v% j* J
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x! Y1 i& ^. l" B/ M3 d
- ceil(x)
8 e9 n% t2 D' S& } - Return the ceiling of x as an int.2 }' `. w( G6 S0 g
- This is the smallest integral value >= x.4 m1 |' M# I8 b; t6 I! E; n. x
- - L4 n1 f9 g4 O" [: r
- >>> math.ceil(4.01)
" H4 P( u- l% o- C4 F- [ - 5! H' ]- G4 Z8 T9 g
- >>> math.ceil(4.99)
; g$ A* F& R. r+ ]; H2 m4 o$ n - 5
5 X( y5 w. s) G) J" s* r - >>> math.ceil(-3.99)" D( e* O5 ]' z- H6 z
- -37 ?2 h" ^9 r" A/ Q
- >>> math.ceil(-3.01)
\5 F% \3 \5 K- i: ] - -3
复制代码 ) @3 h4 w$ X! f
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 a: b/ Q8 V% T/ P, @0 P
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ O2 V/ V6 `& m3 @
- floor(x)
1 n/ I1 w# O3 w - Return the floor of x as an int." t; G4 y6 Z8 F N, J, h
- This is the largest integral value <= x. J; Q6 X$ s7 c5 k
- >>> math.floor(4.1)9 Y* b6 u3 ]# u% _& V$ x. D" k
- 4
; d) V- s9 }7 K* W- G - >>> math.floor(4.999)
( n) a0 n2 j) t/ z - 4
& h0 g) S* h8 I) C5 P - >>> math.floor(-4.999)- ]" C7 Z& U* P ?4 u. |
- -5
( k- x6 l) @3 v; _0 c0 m- | - >>> math.floor(-4.01)
2 V$ l \# K" m2 h( L - -5
复制代码 6 Z1 r5 ~+ x9 F* h
math.pow(x,y) 返回x的y次方,即x**y; B8 a" j) |6 ?8 L
- #返回x的y次方,即x**y: n: q- p4 s1 o: F
- pow(x, y)0 F) X1 Y4 D2 v3 P v$ e+ Y
- Return x**y (x to the power of y).
: `7 h/ V: G/ F" O8 J# N) P - >>> math.pow(3,4)5 J- z, s) l9 j9 e
- 81.0
' ?. O& P4 _* k) k) L- s/ V - >>> - i2 A9 e3 ], D' e, e% v4 a; ^
- >>> math.pow(2,7). a. L E3 b# _0 e6 P
- 128.0
复制代码
1 b+ g8 a$ k2 c1 j9 Amath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" s4 z& m5 c3 g! Z" f
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 A5 c* p/ O& e - log(x[, base])
$ W7 L) ~ e. d# q. N - Return the logarithm of x to the given base.7 z/ ~: b7 L% O+ u" s! G
- If the base not specified, returns the natural logarithm (base e) of x.
* ^ w: G5 a# i2 X7 q1 [4 T4 F - >>> math.log(10)* ]' Z* I; ]& l2 y: h
- 2.302585092994046
& A1 } O; ]$ W9 Q - >>> math.log(11)
. r* ]/ \7 V1 `5 B- |+ b' I1 P+ r; R - 2.3978952727983707
m6 P: N; s* M2 B# H - >>> math.log(20)
, h' W# L! H1 B/ m& K6 z; M3 c* e0 W, B - 2.995732273553991
复制代码
2 @% ~: M s5 Q/ V* R6 a2 imath.sin(x) 求x(x为弧度)的正弦值7 ]+ S$ K/ l+ a4 R) |9 n
- #求x(x为弧度)的正弦值7 ]. v) c$ [! u2 R% @- R# p) R
- sin(x)/ w$ j, E4 E. O' e
- Return the sine of x (measured in radians).
9 F. A7 J7 }! l - >>> math.sin(math.pi/4)& b- L1 i* |) P7 v
- 0.7071067811865475, L4 b1 e8 Z. x4 U0 b: L
- >>> math.sin(math.pi/2)( T% l6 v) W) K* z$ x# c
- 1.0
4 j. b6 O0 u( u- \8 n - >>> math.sin(math.pi/3)
1 u2 l$ ~& F" ~! p6 K+ w6 U - 0.8660254037844386
复制代码
' z, a' l Q2 y+ Dmath.cos(x) 求x的余弦,x必须是弧度2 i" W8 D0 T/ J# K1 u2 x" I( q
- #求x的余弦,x必须是弧度
- j( g# W+ H7 _. z/ |6 Q4 W/ S - cos(x)
4 [2 P9 n) f5 Q/ A0 s8 O5 w Y - Return the cosine of x (measured in radians). B+ p2 a6 S$ U! P: O3 _. Y
- #math.pi/4表示弧度,转换成角度为45度
& u. L) t9 ?/ x/ n3 U/ Q9 V1 n - >>> math.cos(math.pi/4)
! o- y' J/ j4 W& O - 0.70710678118654764 w# m$ y5 W1 H7 F* q- _" s5 z R
- math.pi/3表示弧度,转换成角度为60度
# y; \% X+ c. t% s3 J - >>> math.cos(math.pi/3)5 D% N, Z0 W) k1 \1 G
- 0.5000000000000001' \6 _# o E- l) W, c2 }4 c
- math.pi/6表示弧度,转换成角度为30度9 Q# l4 y! U" @ C
- >>> math.cos(math.pi/6)
9 A7 B/ n* S C. `: ]. Y$ @& u0 q0 c' L - 0.8660254037844387
复制代码
% g6 g: w$ w! b% B& Q$ cmath.tan(x) 返回x(x为弧度)的正切值$ T6 i3 c4 @ j# {7 i/ Z
- #返回x(x为弧度)的正切值' |. ~. S* N; B) B# z
- tan(x)' e: T/ O0 {: `6 D( i8 p
- Return the tangent of x (measured in radians).3 L& h m/ }* S1 F! l1 M8 ?3 u
- >>> math.tan(math.pi/4)) o" x" C$ b2 s, @$ Y9 I O6 r* O
- 0.9999999999999999
: ` R/ l/ o( f" ~- E - >>> math.tan(math.pi/6)
# l4 N' a4 j# t- {. _! ]( k+ g - 0.5773502691896257! V8 d% r( a4 r# T
- >>> math.tan(math.pi/3)
4 K7 j( z' [/ D; l' l+ p; L+ d - 1.7320508075688767
复制代码
0 M$ @" X" [8 V' l' y- Hmath.degrees(x) 把x从弧度转换成角度
/ D9 e$ Z5 F+ K3 W- ]/ y" M; b! G- #把x从弧度转换成角度" D! E# t$ B) l4 ?5 w o; W
- degrees(x)* b& [& y: K8 f
- Convert angle x from radians to degrees.1 N9 S Q7 s* [' D
, H5 N5 k \( [) R# c3 R) d- >>> math.degrees(math.pi/4)7 ?" s& w: V! X+ [5 C
- 45.0
" ~0 ]9 K2 z8 z$ m7 S - >>> math.degrees(math.pi)
( F4 p/ j4 @, z+ Y% Q - 180.0
- W9 j. N6 U: U& w- g G. D - >>> math.degrees(math.pi/6)$ j5 b, q$ N6 \, ^' E) f( B. i% S, ]
- 29.999999999999996
, z& }9 E7 ]8 V7 n4 z - >>> math.degrees(math.pi/3)
! q5 Z) U% A# `- d - 59.99999999999999
复制代码
. R! M8 l& j* o# ?math.radians(x) 把角度x转换成弧度& l8 ]5 t% k+ N" C8 U! f) f
- #把角度x转换成弧度8 a/ X7 e5 K" W: r' e2 y
- radians(x)& `* d) m: O1 W
- Convert angle x from degrees to radians.$ C1 t$ O& I2 U _
- >>> math.radians(45)9 Y8 B: M5 _: z7 }. v4 ]/ k
- 0.7853981633974483
5 {6 Q; Y+ d; N6 f5 R% r7 H - >>> math.radians(60)2 n5 h$ [) N& F0 _ N* ~
- 1.0471975511965976
复制代码
5 ^1 u4 L; |& H& @1 a# X, C Mmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
+ [- k/ _' L% ~2 E+ Z0 G% `( {1 S- #把y的正负号加到x前面,可以使用08 K1 P( r6 m& _2 ]
- copysign(x, y)" F# I5 a0 G$ T
- Return a float with the magnitude (absolute value) of x but the sign
! `; j; t9 J) v' G - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
6 H, H. l: b5 D2 T7 J - returns -1.0.
5 e. g. r) d0 E t
7 S1 \& n4 R* E- >>> math.copysign(2,3)$ E2 c( _& @7 K+ M0 i J0 g
- 2.0
; V! [; `9 D" H+ R, q - >>> math.copysign(2,-3)
7 m7 |1 B8 S- a& g0 \& e4 D! n9 ] - -2.0
- E1 b+ G4 V: }7 g - >>> math.copysign(3,8)
: z) z P4 d6 F7 v# T+ s0 K - 3.0" L7 [! G) H* d6 U% @0 u
- >>> math.copysign(3,-8)# h% d5 k" ~$ P$ Q+ f! q
- -3.0
复制代码
2 l: C' {" {/ B. \- wmath.exp(x) 返回math.e,也就是2.71828的x次方
7 j/ r5 H8 e1 X: [" o0 ]/ I- #返回math.e,也就是2.71828的x次方- ]' n: A/ q- I% ?" r1 L
- exp(x); Q9 b* F0 d2 O) A
- Return e raised to the power of x.# g; p7 r3 n" s# q
( J0 Y6 r1 c5 I; @ {5 d9 ]- >>> math.exp(1)
8 O3 A3 H6 f+ T) n8 d0 O - 2.718281828459045
0 X8 t! Q$ I# Q8 U7 p - >>> math.exp(2): Y( i# `# r. A. ^4 ]9 Y( N
- 7.38905609893065
- N5 |! {' K7 h% g - >>> math.exp(3), z) S& ?0 I. G9 u
- 20.085536923187668
复制代码 + T: c; u5 [5 l: P8 Y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 ^2 u8 M* ]$ e
- #返回math.e的x(其值为2.71828)次方的值减17 M8 F6 u" g+ |/ b: K/ P" [* j$ W2 l* m; f
- expm1(x)
* n$ Z( {3 q$ K9 Y: _$ J( c0 T - Return exp(x)-1.
3 c5 s+ i$ p) ?( B0 F - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
) x2 [6 R9 y' q8 M, @6 ^$ |) g - 2 H% J2 p, K/ H* l) E9 k4 o
- >>> math.expm1(1)- o& l+ r* R2 {3 C! |
- 1.718281828459045
6 P0 x1 F# i& f. e3 t - >>> math.expm1(2)
0 h" F" g2 n% B0 d$ E) _. E! X# i - 6.38905609893065
5 U; Y6 g- Y+ e! G4 c, \, Z& @ - >>> math.expm1(3)& w/ k9 i5 \9 E0 N0 H. Y; s
- 19.085536923187668
复制代码 & G8 H: G0 u% t' @6 J
math.fabs(x) 返回x的绝对值
& i* p% C9 y% \" C% m9 i7 y; p. @2 v! e- #返回x的绝对值
( D9 S/ `' j( B - fabs(x)
* x1 Z S& d k: ` - Return the absolute value of the float x.
. o. P3 s, Y; Q - # _3 u- O# Y" t
- >>> math.fabs(-0.003)8 h! g+ `( g/ P" ^2 R
- 0.003
6 y9 _7 F/ K8 v1 t - >>> math.fabs(-110)" R; C/ P+ i; z$ i9 @! G: y1 k$ c
- 110.0; C# d/ Z1 k4 g5 b- H2 ?
- >>> math.fabs(100)
+ M8 L4 A# E$ o9 O$ ~- z: t" V' f$ g - 100.0
复制代码
7 W; f h8 e$ W: Lmath.factorial(x) 取x的阶乘的值# r9 H# w- t' e! ^, }
- #取x的阶乘的值. _# ?% s( q" ~. a4 T3 [
- factorial(x) -> Integral. h) L8 g, a5 K% A! \
- Find x!. Raise a ValueError if x is negative or non-integral." u" Z6 ?$ J+ v+ G
- >>> math.factorial(1)9 y9 q5 N1 w3 M9 e; q) n7 Z& j3 w7 F
- 1$ Q" P a" h2 O% e8 g
- >>> math.factorial(2); Q# Z+ A0 Q$ B! u5 |, b% u5 g( I. b! I
- 2( S1 O* K3 G! Q7 _, c; X
- >>> math.factorial(3)
% Z3 `) x: B# V8 i+ K3 `1 c% m - 6
. Z4 |! i7 X# N# N- r- ]/ l - >>> math.factorial(5)
" L+ D; X! Z9 S9 K/ B$ [ - 120
4 O* B% x* B: l8 k l - >>> math.factorial(10)3 C9 ^9 z' P( _2 d Y2 F, ^
- 3628800
复制代码
) f- n8 v5 b8 J b9 B1 o3 omath.fmod(x,y) 得到x/y的余数,其值是一个浮点数. i) E# z* B! w* w A- j
- #得到x/y的余数,其值是一个浮点数2 Z* v3 U/ S) F# y) l; j$ x. \" _ x) _
- fmod(x, y)0 R9 f: g) q( S! Z9 L
- Return fmod(x, y), according to platform C. x % y may differ.
1 c! m* v$ g/ D. z. Z - >>> math.fmod(20,3)
& F) v/ ]# g& J, } - 2.0
: Q5 T/ d& g& j/ M$ y; ~ - >>> math.fmod(20,7)
# Q" F% \& f2 i9 I$ R - 6.0
复制代码
# F. \+ t l( c z5 emath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
+ \, U. k d y3 _* W& H- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* E7 _( Z, L" L- a
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
, P# N8 v7 t+ a/ D8 K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- Q* a* R) O2 E' e) w- J; f* O/ y
- frexp(x)
1 I. M! s& {) _ - Return the mantissa and exponent of x, as pair (m, e).
# {' m% L: n7 p9 ? - m is a float and e is an int, such that x = m * 2.**e.
% M3 s2 S7 x) z p! t1 P - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
6 W& }! [4 t6 Y; b& J. u9 d - >>> math.frexp(10)' W8 u' ~1 x/ |$ j
- (0.625, 4)
, x* f s1 M F. n - >>> math.frexp(75)
$ s* Z% L4 [- n- H4 H3 ^# `3 U - (0.5859375, 7)8 u$ V( [' g' K" R4 f+ f- @; G
- >>> math.frexp(-40)5 W5 J9 l' j. }' `, v
- (-0.625, 6)6 e6 A. d8 t3 N7 b6 m2 z
- >>> math.frexp(-100)" Y, J+ S, B! ?1 c k
- (-0.78125, 7)# R6 ^. Q3 h4 E
- >>> math.frexp(100)
# S7 n; G6 _3 C$ b5 s! i - (0.78125, 7)
复制代码
! ^6 L8 k5 Q+ u9 Y' ]math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)9 }2 m' N) Z7 p$ C4 e/ l9 T( B
- #对迭代器里的每个元素进行求和操作
& a6 Z& O. X; t - fsum(iterable)4 E, U6 V1 Y( g* j. b% O! A$ b
- Return an accurate floating point sum of values in the iterable.% B. \1 R4 G. I; R$ e$ p2 [' [
- Assumes IEEE-754 floating point arithmetic.6 u) h' x3 j& B7 p0 k
- >>> math.fsum([1,2,3,4])" I* G+ ?: g6 i4 L% m1 r* ^
- 10.0( z0 ?3 D* s2 ~3 u9 x+ m
- >>> math.fsum((1,2,3,4)). P* q' E: i( C. k
- 10.09 k# C( x/ l3 A+ R0 c" b
- >>> math.fsum((-1,-2,-3,-4))
4 A1 E7 V4 E: i9 ]' a7 | - -10.0
( m. I9 S9 {- L, j - >>> math.fsum([-1,-2,-3,-4])7 I/ y# h" r: @, f( B( o
- -10.0
复制代码 * j% l" M2 K# N& U- z- C
math.gcd(x,y) 返回x和y的最大公约数
, {$ }% J) ]9 m# s7 _; r6 f( ]- #返回x和y的最大公约数
$ e o# E6 s% i* J! F! f - gcd(x, y) -> int1 O, b% H5 Y0 o$ E, g# O8 E
- greatest common divisor of x and y
9 H: [4 L! W q2 \1 E& F - >>> math.gcd(8,6)/ H0 D1 S+ H' ?# ^! g0 Z. M( p
- 21 |% B- u! s0 x& |) @1 m' s( ^
- >>> math.gcd(40,20)
5 J( M, m. N. P - 209 d) K& `/ Y: m2 _" E
- >>> math.gcd(8,12)/ S) Z# Q n8 d) g7 f- F! V
- 4
复制代码
9 J5 ^ c0 b; Z' ?8 h" m- A7 X- v2 b8 Bmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
6 e) `; K. C% y- #得到(x**2+y**2),平方的值
7 I# H7 w4 I1 d3 a" Q - hypot(x, y)* b8 R; d2 D$ k3 k X0 P6 ?7 J
- Return the Euclidean distance, sqrt(x*x + y*y).& c7 O+ s. j4 J; E
- >>> math.hypot(3,4)2 o, x' M6 b5 N/ L% a1 c
- 5.04 }. n/ Z9 j* H% g+ l; I
- >>> math.hypot(6,8)0 `* G4 M* k! b1 a& I
- 10.0
复制代码
$ }) l. a. G6 E. w( omath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
' {# W1 }% E8 h% X- #如果x是不是无穷大的数字,则返回True,否则返回False
/ a% Y/ L% Y# {6 q - isfinite(x) -> bool
/ e1 E5 |- d- o3 V6 P4 p& x, F - Return True if x is neither an infinity nor a NaN, and False otherwise.
; X8 Z4 m4 B" o5 I) | - >>> math.isfinite(100)! D# Q/ o/ C8 w$ S# K3 e% e
- True5 M( r* M$ J1 [8 s: i6 L
- >>> math.isfinite(0)
8 r# c! J- S G - True
# @* i' v- y8 @7 X2 K - >>> math.isfinite(0.1)
' D/ m2 P, c# v& D - True
4 b* P% E/ \0 O2 ^8 x - >>> math.isfinite("a")0 ~+ }6 R, Q/ K8 H
- >>> math.isfinite(0.0001)
$ q0 d! \5 @' I% C' p0 U% z - True
复制代码 8 f! G8 I J6 p4 j$ _
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
- s, E8 K" |4 c0 ^ d5 k: I, Q3 \- #如果x是正无穷大或负无穷大,则返回True,否则返回False8 O) E( k9 @) C7 h; R M6 F3 }9 Y$ J
- isinf(x) -> bool: ?& B* F# ?. V/ h0 S
- Return True if x is a positive or negative infinity, and False otherwise.; A- T* X5 e' K9 T
- >>> math.isinf(234)8 m5 a4 f1 I; {% O
- False
# `2 ^$ }8 m1 { - >>> math.isinf(0.1)
+ g9 t5 O7 s* k" N - False
复制代码 . |8 x2 Z, l4 V2 L" Y& D# Q
math.isnan(x) 如果x不是数字True,否则返回False# k }' ?. @% o5 a( ~/ n+ }( H
- #如果x不是数字True,否则返回False! }2 X) u/ J! N! r
- isnan(x) -> bool
3 G. u/ B$ `& i, n6 G1 }& a - Return True if x is a NaN (not a number), and False otherwise.
1 m) u* p9 n6 g6 N; M# o - >>> math.isnan(23)
/ n. n/ [3 U: ^% g! C" |& x1 y - False
: j" Y7 \& }8 B" g g - >>> math.isnan(0.01)
% T1 Y, |( h/ f9 L - False
复制代码
8 [$ `3 y J" c1 @4 Wmath.ldexp(x,i) 返回x*(2**i)的值
0 j V2 U/ y7 R9 G3 N- I- #返回x*(2**i)的值4 W& [* r; E; [7 u
- ldexp(x, i)
& |8 h$ q+ O& z - Return x * (2**i).
# u# G, l0 u4 \. u - >>> math.ldexp(5,5)9 C& |2 w% v- u' v* a( x
- 160.0- i2 f/ B4 Y0 [, V4 q
- >>> math.ldexp(3,5)! y. L) e8 h1 n$ `
- 96.0
复制代码 7 j) j" R& ~+ l8 a y' }& c& {
math.log10(x) 返回x的以10为底的对数
* x& G& f2 a. N- G- #返回x的以10为底的对数
0 s: T8 B/ P5 a5 c4 p% x1 p - log10(x)" w$ p% S B7 S9 x' u
- Return the base 10 logarithm of x.
4 B$ x6 J/ D4 ^: X' W1 k - >>> math.log10(10)6 T ^, w' b% i- i7 l. F
- 1.0
/ D: v4 h; Z5 }* b8 B+ B/ ` - >>> math.log10(100)
4 N, n w' p6 ?, ]3 \ - 2.0
& O6 k. l) s e5 N/ @ - #即10的1.3次方的结果为20
% {, P2 ?) q4 D$ w6 s - >>> math.log10(20)8 c6 B( U' y( c" q1 e
- 1.3010299956639813
复制代码
3 M: y. y7 q, Y+ b# Omath.log1p(x) 返回x+1的自然对数(基数为e)的值
# x H/ S6 W$ D- #返回x+1的自然对数(基数为e)的值
/ F* S; ^8 Z+ ~ N" J; ?% g/ e - log1p(x)
) ~9 p5 q9 S) u; X3 \7 F* J - Return the natural logarithm of 1+x (base e).1 u4 Q( \( @: F: y8 F3 c5 D
- The result is computed in a way which is accurate for x near zero.
" m O7 [8 O+ f - >>> math.log(10)" k4 b; O0 ?2 x4 y
- 2.302585092994046" P: }+ i$ g8 F$ T3 N- T. s. e" Z
- >>> math.log1p(10)
% Z# y6 b. Y3 ~ - 2.3978952727983707
2 \! N0 l! U+ h - >>> math.log(11)* f5 e/ x" y% ?2 g+ D' q
- 2.3978952727983707
复制代码 + [0 @4 p; A- I! I7 V, J
math.log2(x) 返回x的基2对数
5 v; |7 {& W8 p! z7 |2 S- #返回x的基2对数5 j" [; \7 d$ ~6 s6 n6 X% s
- log2(x)6 K, a0 `5 `) ]; D* g! B8 R4 C' I
- Return the base 2 logarithm of x.3 C5 w. k! D1 E; B) D% H6 ]
- >>> math.log2(32)
: F) g' X/ x7 t* x1 A; n8 Q4 m9 Z - 5.0
) ^& A( a/ ?/ O7 ` N - >>> math.log2(20)
$ V3 ~! W- W7 n. _! D# q2 J+ b/ K - 4.321928094887363/ \0 P3 j4 O- p3 b8 T6 R9 B+ ?( b
- >>> math.log2(16)
5 ? r2 G3 ], L( x% E# N- Q& U - 4.0
复制代码
" H. a& q$ X9 h3 Y7 \1 q" \# [math.modf(x) 返回由x的小数部分和整数部分组成的元组' R( t C9 S, c9 h9 u9 X2 A
- #返回由x的小数部分和整数部分组成的元组
1 O @8 i2 U5 K& H) E - modf(x). F, s8 k$ ]7 ~' f+ K9 X( c
- Return the fractional and integer parts of x. Both results carry the sign
/ y( z- g/ ~2 ]% |: _: `3 X( k - of x and are floats.) j6 D& P% y5 E
- >>> math.modf(math.pi)
$ M% `4 I) X7 i - (0.14159265358979312, 3.0)
* t1 J" A9 L9 \. T - >>> math.modf(12.34)4 P! K# B0 _0 v8 w1 o
- (0.33999999999999986, 12.0)
复制代码
7 ]0 v$ q/ F: g0 C% |& Bmath.sqrt(x) 求x的平方根
$ E2 e& B$ \( h* h4 G+ I- #求x的平方根
* Q0 ?) @7 X, M - sqrt(x)
7 ~4 i2 _( X" C" }* y! B; n4 y* d - Return the square root of x./ r9 x" N: ]3 q. e+ l1 l
- >>> math.sqrt(100)* I9 r( y- {1 `
- 10.0, Y3 y {- A6 {
- >>> math.sqrt(16)& V$ ]* @+ g3 t; |! J: Y
- 4.0
7 H( J; \ ~* U& ^% n- G4 B* A - >>> math.sqrt(20)% v3 k! v- I* q) D2 n- K3 \0 i
- 4.47213595499958
复制代码 * m6 v4 R) ~# N- \) g
math.trunc(x) 返回x的整数部分- #返回x的整数部分
. _) W* t4 G: _+ |8 @' }4 C - trunc(x:Real) -> Integral
- E6 E0 s0 l$ O* p; m, t3 x - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
! a: ]9 r0 p. b4 o9 X - >>> math.trunc(6.789)
- W' J; S6 u3 b* f3 u - 61 l' |" l* D5 C" Y& P1 a" N
- >>> math.trunc(math.pi)
0 w% e2 q! j0 V* w" f' C - 36 c/ p) R8 W% l1 N4 n) }
- >>> math.trunc(2.567)
L+ ^2 b4 g6 D& z" p, Y* } - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|