马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
' t) q$ c, ?) f. s$ y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 B, n1 m4 V( i# K0 k
" ~ m6 F/ o) P( E3 K方法1:
3 u) r7 }6 v* L. _- >>> import math
" G4 V# a1 k* c3 A* N: s; f3 T - >>> math.sqrt(9)8 I$ q5 a, B( a6 J0 V
- 3.0
复制代码 方法2:- x3 e* C8 l2 _$ C
- >>> from math import sqrt3 {7 A( P' Z. K- p/ n* B
- >>> sqrt(9)
2 y/ u3 Q$ g% W - 3.0
复制代码
- B3 F3 z4 `. V) S$ ?' U+ H- P1 X
. _# O$ ]- e4 [% p! imath.e 表示一个常量6 G# l/ H: B2 y9 `/ f/ K
- #表示一个常量
$ I+ j" T8 {9 f6 J0 k+ |# l - >>> math.e a3 u3 }0 x: o) g
- 2.718281828459045
复制代码
( e. N5 u' z; f P5 D5 o4 J" U Q2 vmath.pi 数字常量,圆周率7 T3 f' h; W4 C8 M8 J1 y
- #数字常量,圆周率
5 i0 P( M) z i/ g) O - >>> print(math.pi)
8 q7 B2 |" e% s) P9 l - 3.141592653589793
复制代码
7 Z" Q, t6 O8 a1 t! G! g9 Nmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x# k" N. q2 B2 a7 D9 b7 M# W
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 S3 u. H6 q* ]' Z. z" v
- ceil(x)
7 M7 f4 i+ z6 K7 Y - Return the ceiling of x as an int.
$ x0 u; Y- X: t) i" g6 b* Y - This is the smallest integral value >= x.; ]' N5 l/ d, \; u: Y" ^1 X2 l9 D# t
, K! }$ o+ b$ Q V1 z; l) _ R- >>> math.ceil(4.01)
% }6 D F: S3 Z" w' C& e - 5
- T+ ?/ a* Q0 H, I- L, O* ^- l - >>> math.ceil(4.99)
6 B# S8 O7 L- H: T' q* l - 5
) p5 j9 n- i' e! N3 l* H$ [# h - >>> math.ceil(-3.99)( x# f4 p4 G, B: B9 X6 k
- -34 M- I: z; I! D4 [+ _
- >>> math.ceil(-3.01)
8 g. U9 z/ R; C5 u$ E$ c0 N - -3
复制代码
4 o3 D" g3 I* b9 w" F% \math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 m5 ]5 V& a/ R, Z- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ u! C# G' S/ f. N+ ]
- floor(x)" X+ y. B: V5 \2 Z
- Return the floor of x as an int.
8 h: z" ?5 v& l! C$ Y - This is the largest integral value <= x.
9 L* a- a3 C7 k4 x - >>> math.floor(4.1)( w W* e( x, A7 F
- 4; Q M! j# r2 w- v
- >>> math.floor(4.999)
- D7 D# V# Y% K' z - 4, w V( ~% H- D& O5 i( h
- >>> math.floor(-4.999)
) j1 q' ]% S |! @; h6 [( Q1 G - -5
: b+ v) g3 {6 g: U7 H - >>> math.floor(-4.01)
- q$ S: O3 e: v) H! s0 ~+ z: I2 r - -5
复制代码 ( z j8 F! Z1 D# d2 r# w
math.pow(x,y) 返回x的y次方,即x**y
1 E' f6 N, e" x- z- #返回x的y次方,即x**y9 R1 S Y" z x, Q. |2 A; \) E
- pow(x, y)2 h' `; t8 ]6 o& E
- Return x**y (x to the power of y).
0 ~# g( O+ V" G& W) R3 ] - >>> math.pow(3,4)' Z- R' c% m! M$ n: Y
- 81.0
; z' z9 k9 \9 q; I/ F - >>>
9 Y9 ]7 v$ _" y - >>> math.pow(2,7)
- e" G- s. r% W+ I8 i1 d - 128.0
复制代码
/ @0 W- m- M3 E, U: x! n) Gmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' i/ k, ~' m C; U! D5 N |! g
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 p5 k1 O* H" ^9 E - log(x[, base])
$ ]! d1 T9 R! ~: h t' b# q - Return the logarithm of x to the given base.
( O. A, t/ G2 v- I1 H: {& @+ R& e - If the base not specified, returns the natural logarithm (base e) of x.
4 Z* x; H' n' J - >>> math.log(10)8 I8 `; @# G5 T+ I
- 2.3025850929940460 d4 W6 K' E, t
- >>> math.log(11)% S. G% ~% U) F* Q- j
- 2.3978952727983707% Z, B" @: t" X6 k, k! I: B
- >>> math.log(20)7 p8 p8 U' `* r* T- M* D* Y) b
- 2.995732273553991
复制代码 ; Q0 |; A; t) J3 I* R7 W+ l/ a! P2 ]. C
math.sin(x) 求x(x为弧度)的正弦值* i4 o9 V E* C% {6 ~1 V! j+ f
- #求x(x为弧度)的正弦值5 ]; @1 d x/ T- Z
- sin(x)) M1 Y( @& k0 K" a1 E1 d
- Return the sine of x (measured in radians).
: i, i I; `' D& k/ Y - >>> math.sin(math.pi/4)7 u& n8 H/ \. h( P9 Z% G
- 0.7071067811865475# H" s' u( D# l/ i2 `8 k
- >>> math.sin(math.pi/2)
2 t: A0 U5 \! O; a4 q - 1.08 p* i7 @# M$ d- m b. i3 q
- >>> math.sin(math.pi/3)3 i0 I0 X9 v' y1 E3 a ^& P
- 0.8660254037844386
复制代码 0 a9 U5 s4 ~5 j# u0 m/ z0 c) p
math.cos(x) 求x的余弦,x必须是弧度& |0 p7 p5 C3 H( i, L$ Y
- #求x的余弦,x必须是弧度
5 u9 Q" Q7 o1 z, y) Q, ~, @ - cos(x)# E' c3 M" n* v
- Return the cosine of x (measured in radians).
' r: D: H0 S% |* v. c& H - #math.pi/4表示弧度,转换成角度为45度* L# l9 C: I) y8 `- X
- >>> math.cos(math.pi/4); ?2 E8 g1 I( |% e/ }
- 0.7071067811865476
( g( i5 b# i2 f - math.pi/3表示弧度,转换成角度为60度+ A1 L [& m3 w9 X
- >>> math.cos(math.pi/3)
8 k! M- d2 P6 E8 t2 M2 r; C6 |+ } - 0.50000000000000019 F& s5 p, h3 N# R
- math.pi/6表示弧度,转换成角度为30度
+ Z/ E! d4 o, R2 k) ]' l" X' [2 J - >>> math.cos(math.pi/6)6 n. a; _8 Q7 `
- 0.8660254037844387
复制代码
/ E8 j1 {6 l$ kmath.tan(x) 返回x(x为弧度)的正切值
6 n0 k+ S; i) `& X1 {7 }- #返回x(x为弧度)的正切值
7 M3 k. I/ Y4 T: E1 Z - tan(x)
% \7 @, w ~& t - Return the tangent of x (measured in radians).3 Q J- U$ `. d- r+ q' j
- >>> math.tan(math.pi/4)
8 W' i! s$ @+ Q( s6 @ - 0.9999999999999999
6 w/ @# y' R# o2 D7 c6 a9 `; ` - >>> math.tan(math.pi/6)) p& ~, o3 F+ X- V3 P9 Y" r# D
- 0.5773502691896257
: [" @1 j8 y2 C# j# F& { - >>> math.tan(math.pi/3)
" R# ^; h" @/ n+ F( I8 U - 1.7320508075688767
复制代码 ; b. m. d* T1 L2 M6 m3 F4 Q+ W& C
math.degrees(x) 把x从弧度转换成角度$ I( i7 o A" `2 K) D6 O
- #把x从弧度转换成角度
1 ^9 [& q* Y2 A' L - degrees(x)9 t' c3 d) B7 y
- Convert angle x from radians to degrees.- M$ l1 R D. S; E- _& F0 _
0 o8 S* H- @7 W- >>> math.degrees(math.pi/4)- A9 m, [! G4 m
- 45.0
" I/ P4 j2 X( j9 h" K/ k) } - >>> math.degrees(math.pi)
' L- @0 ^) c3 m! S9 F Q - 180.0% Q$ U0 P" {1 L8 C* p1 |
- >>> math.degrees(math.pi/6)& g: h: ?, d; V5 L
- 29.999999999999996
6 o# f; O* S4 G& [ - >>> math.degrees(math.pi/3) w0 q8 C: P. G* o; D
- 59.99999999999999
复制代码
h+ E6 Z. H b( T! ?math.radians(x) 把角度x转换成弧度/ n% U2 N9 [1 X4 E) K; t
- #把角度x转换成弧度
+ C/ R& v3 A. K- E4 r' j+ Q0 A - radians(x). Q) Z. X5 v: w7 S8 w
- Convert angle x from degrees to radians.. N2 d- I& X0 e( J7 w% W
- >>> math.radians(45)/ s. ]! X8 `! o* l# _8 S6 ^
- 0.7853981633974483
- @2 i9 C2 G! @, [% M - >>> math.radians(60)
3 Y5 C% @4 X) F' Y% k! d; g - 1.0471975511965976
复制代码
) w1 s( |( [, O+ [1 T$ I, [' m/ e, pmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
: D: ]& K$ D7 I2 d3 x. R) e' y- #把y的正负号加到x前面,可以使用0
( E' _8 @& q9 y, N+ u - copysign(x, y). T- {( C% R) G# r+ K. T B8 F6 b
- Return a float with the magnitude (absolute value) of x but the sign + `& X1 n( ]/ ^, Z1 R7 k
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) , @2 o2 b, B3 i( h7 A" Z+ b
- returns -1.0.& F7 z6 U- F$ N$ u/ g p [7 J5 E
: G _5 x( i) X4 G9 {" v3 {1 P5 |- >>> math.copysign(2,3) w+ g6 M7 _7 x( M. B7 q; ?
- 2.03 c, T7 n' }. h( z: i2 ^
- >>> math.copysign(2,-3)* `9 {1 U- l% m- y! J9 q
- -2.0
) T+ g& x/ H* S6 R& w$ Q- H - >>> math.copysign(3,8)' k+ N2 b4 |3 h: a! Q& M$ B5 {
- 3.0
) I4 j+ A2 Z G( p: `7 a& c - >>> math.copysign(3,-8)
0 A& j4 R" I9 k3 T X. Z( m - -3.0
复制代码 3 r2 s7 I! N' E/ I
math.exp(x) 返回math.e,也就是2.71828的x次方0 X: {( d# V5 S. q
- #返回math.e,也就是2.71828的x次方
$ s3 [ V p% k/ m) D8 @* x" W - exp(x)
. L0 R% d0 A. u5 C - Return e raised to the power of x.; N& I" b( o* P. c Q
- ' q, K& y4 K9 h. h$ b
- >>> math.exp(1)7 i) f+ i% C0 l% h7 g3 Q
- 2.718281828459045
; t8 e# d, `. V: q0 `& G: I - >>> math.exp(2)8 ~; U# P: t% n
- 7.38905609893065
- ?. O; L% t9 d! N0 a+ P - >>> math.exp(3)
* n8 S* b) s+ E0 d - 20.085536923187668
复制代码 2 \1 K$ Z" x# x7 u7 I, h
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1- B' ` ]" `" e; g6 v# K7 U' l7 o
- #返回math.e的x(其值为2.71828)次方的值减15 W4 ?4 S( L; `+ \' U
- expm1(x)! _' Y5 l; M* q5 E: y
- Return exp(x)-1.
6 r: K! ?* ^. G( ` - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; @& H1 v* ?6 W+ g
; P+ F3 g) g, o( [) d2 l3 U+ P- >>> math.expm1(1) v9 z( w: [9 j2 q, Q; r3 E
- 1.718281828459045. E5 ^# `% ?3 s- G+ x" h! ^4 `
- >>> math.expm1(2)
% F3 s) X- m( a) g T - 6.38905609893065( F/ _; D5 \1 W& A$ r
- >>> math.expm1(3)
+ i! h- P2 G6 a# \; \- U - 19.085536923187668
复制代码 % M4 _" z$ ]# O" F$ W
math.fabs(x) 返回x的绝对值& b" o7 f6 r5 ]; D9 p; K5 l
- #返回x的绝对值" r& R" H3 o; M/ `" y* F
- fabs(x)
5 i0 A+ x; D: m5 P* ? - Return the absolute value of the float x.
9 o( @* |" s% r# @
' j: y7 ]# U: F# }1 [- E, ]- >>> math.fabs(-0.003)
; i! v3 Y/ s4 X a$ [ - 0.003
6 T" Z" d! m5 @$ a - >>> math.fabs(-110)0 u) L$ v' _4 E) S
- 110.0# V! ]/ P9 z" x) @6 x
- >>> math.fabs(100)! B. x5 l+ ^% n h0 x
- 100.0
复制代码
/ R' Z' Y* a8 _. q6 p, O; pmath.factorial(x) 取x的阶乘的值; `8 m) i) B% R6 U
- #取x的阶乘的值
& }6 ]( g7 p9 g1 B - factorial(x) -> Integral/ x& H; X$ d. h
- Find x!. Raise a ValueError if x is negative or non-integral.* \- J$ j4 O! M( W9 b5 b4 m
- >>> math.factorial(1)* _; m& F6 A$ p- Q4 n$ L) l5 K& d) D
- 1
( P1 F& |0 J; [3 O: \' Y - >>> math.factorial(2)
9 s' u( Z" r4 m - 21 W2 s+ g$ p6 G p% j* p+ g) K
- >>> math.factorial(3)
1 U T! w8 U: l5 e G+ V - 6" [9 L( y2 h! x. e% O
- >>> math.factorial(5)2 \3 E1 D. R( q. u
- 120' R6 r# m/ a7 n9 b" d& K8 E0 y$ E
- >>> math.factorial(10)/ ~. f( O0 q, x0 w$ q
- 3628800
复制代码 ( W+ b9 ]3 b) S$ m% m
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数2 |$ S% W+ M/ U1 _8 u: H: S' o
- #得到x/y的余数,其值是一个浮点数
7 @7 h* H% ]; |& o0 K - fmod(x, y) n* P. M* t, J% M6 s3 N
- Return fmod(x, y), according to platform C. x % y may differ.
1 \3 j, I- \' E' q, A. Z0 c4 _ - >>> math.fmod(20,3)# I) ]) ^, q7 B d" _2 L
- 2.0
% H; W* O' ]/ Z- U9 U - >>> math.fmod(20,7)
# A' h' J- ]% n5 a' R - 6.0
复制代码
# x. ^' O& ^* d7 s; tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 O" L9 ]' z+ t1 j/ a$ M" ^
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
4 b( F/ c0 r4 |& O - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值, K# Q% ?; C# Q# f; t/ J3 n! G% [0 ~
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
, e6 s8 u% r# n6 g - frexp(x)
- i8 W: Z9 f' s( p, p0 ~ - Return the mantissa and exponent of x, as pair (m, e)./ E' H! g9 q3 k1 k
- m is a float and e is an int, such that x = m * 2.**e.
/ j3 A$ n* R) v) z* \5 A - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- N7 R2 a$ P; a* y9 ]+ d
- >>> math.frexp(10)
2 y7 j% `8 P6 A5 c/ @+ a$ L - (0.625, 4)5 [5 f$ L. P: {* G
- >>> math.frexp(75)9 s' x! R8 l& `5 C8 ]+ u0 b T
- (0.5859375, 7): H g1 i. e7 E5 }$ k/ d
- >>> math.frexp(-40)% V2 b! r4 l8 N9 F
- (-0.625, 6)
* L: v! `, f; m" r+ S - >>> math.frexp(-100)* f. v5 q( W2 z
- (-0.78125, 7)
8 S( j. _6 h- ^ t - >>> math.frexp(100)
: m' a" p2 s" U: A& N9 k6 A4 c4 l4 Q - (0.78125, 7)
复制代码 % _& A) i; J* i& M" i( p
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& \8 D) g) I p; j- #对迭代器里的每个元素进行求和操作 q" k, n1 s% v- `& o
- fsum(iterable)' [) O Q9 V; }: s# F
- Return an accurate floating point sum of values in the iterable. f1 a* x9 ^) u( O) {
- Assumes IEEE-754 floating point arithmetic.5 a* b7 _2 N. L V
- >>> math.fsum([1,2,3,4])2 k- q; Y2 s! S C8 g8 m( {2 v
- 10.05 f' @9 M8 W5 X% I* C3 x2 }, J6 H
- >>> math.fsum((1,2,3,4))- U: S& E7 j/ n7 t
- 10.0. @& U3 k3 H0 o F$ l) m) o
- >>> math.fsum((-1,-2,-3,-4))0 a8 V+ u7 N4 C4 @1 Y& z
- -10.02 a* R* m4 s' d2 H
- >>> math.fsum([-1,-2,-3,-4])3 h& W+ z, \& E+ ~
- -10.0
复制代码
; U! Y$ o U+ ~) s( Cmath.gcd(x,y) 返回x和y的最大公约数
% ~" Q4 u3 I9 Y4 i& K1 E- #返回x和y的最大公约数
7 Y$ A) n) m! g% p; u - gcd(x, y) -> int: y/ |+ H0 V1 s8 B
- greatest common divisor of x and y9 S" W8 J( {* C+ K
- >>> math.gcd(8,6)
e0 `& X& ]0 l% s; h - 2
; \0 S1 l" f5 q' C - >>> math.gcd(40,20)
3 {0 a3 E* S) g' H. e; v - 20) c5 T( D" j; R
- >>> math.gcd(8,12)
! l3 l8 f9 T$ f2 b K3 | - 4
复制代码
2 `6 v2 m& t7 bmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 `/ L9 W1 L* `0 q" g! B
- #得到(x**2+y**2),平方的值
& ^# Z% \# w! F R+ ? - hypot(x, y)
) k$ g+ E/ y6 ?2 s: k! W - Return the Euclidean distance, sqrt(x*x + y*y).
! p& R1 H/ J6 ] - >>> math.hypot(3,4)1 m M) v! s- g: }
- 5.0& u$ R1 p7 L5 H6 B7 _2 q7 `( s9 f- E
- >>> math.hypot(6,8)
# X7 B3 g b, S - 10.0
复制代码
8 a) H8 c0 a) }; {) ]math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False1 B4 c, ?" H: H& G" G7 y
- #如果x是不是无穷大的数字,则返回True,否则返回False
( n2 v9 }2 P2 C - isfinite(x) -> bool* T; c: b+ ~, E6 x: Y* Y; g4 P
- Return True if x is neither an infinity nor a NaN, and False otherwise./ E2 s% f `) R- a
- >>> math.isfinite(100)
- |- }. p4 u. ~; ^ - True
5 N" n5 W. ~# @: E& X3 o* y ]: p6 C - >>> math.isfinite(0)
* m# m! `7 C# J* }- T1 V - True
# M! z9 u M! g- V7 m9 b! e - >>> math.isfinite(0.1)6 n* ^$ |3 d o5 z' l, l' @
- True
6 w3 `3 ]: @/ Y2 g - >>> math.isfinite("a")" `, `, x2 d, Q( G
- >>> math.isfinite(0.0001)
$ j. B. N z* Y, t5 | - True
复制代码 . J' `% @3 `6 g
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False% K/ s0 s4 e- R3 }- c
- #如果x是正无穷大或负无穷大,则返回True,否则返回False$ d7 m0 C# K& c
- isinf(x) -> bool
' i( R8 I9 N1 j - Return True if x is a positive or negative infinity, and False otherwise.$ y/ M+ b- a, A2 n
- >>> math.isinf(234)
' H3 O/ O) x# k0 N - False
2 O! V4 |: u1 L* e* C( e: a) q - >>> math.isinf(0.1)* {2 i, _) Z& B# D8 j# B
- False
复制代码
$ [9 u8 ^' y* j2 a% ymath.isnan(x) 如果x不是数字True,否则返回False
. g# C1 v3 w! D6 G& U, Q- #如果x不是数字True,否则返回False4 a( ]' w$ {5 K/ r4 e
- isnan(x) -> bool
5 B, N4 i. J/ m1 s$ r - Return True if x is a NaN (not a number), and False otherwise.! m2 G3 R5 o8 ^6 k# q% w8 B
- >>> math.isnan(23)
' G3 R/ Y p6 i7 s/ h8 Z, y7 o - False4 A( N1 A# `% b# y
- >>> math.isnan(0.01)
( q2 r$ R( Z: C* F - False
复制代码 8 n4 n z$ V& R5 }) e7 z
math.ldexp(x,i) 返回x*(2**i)的值
8 Q" |! |# z; H- #返回x*(2**i)的值% ?9 `, @7 T7 G T8 K _1 J& O0 ~) m
- ldexp(x, i)/ n" L& ~1 E5 t6 w" a. K
- Return x * (2**i).6 D4 F G P# ~+ q/ X% j6 n0 J, p
- >>> math.ldexp(5,5)
% n+ x* n$ ~- q) U - 160.00 R" p! J& t8 T
- >>> math.ldexp(3,5)
2 ~# X m( y b - 96.0
复制代码 ( X1 W; A) h) X; [+ w2 w, p0 E3 Q
math.log10(x) 返回x的以10为底的对数
) {1 Z; R5 h& ^ f6 l3 W8 {- #返回x的以10为底的对数# f, B8 L( U4 s. z( W) a% {& _
- log10(x): L3 ~" p- a# C) l G+ K* ?5 s
- Return the base 10 logarithm of x.! H' t3 o0 I* Y g1 F4 _5 O
- >>> math.log10(10)
; h) O! {2 A$ b; g3 b9 w - 1.0" u: M; e2 R; i# Q6 b% Q: |
- >>> math.log10(100)" s/ z+ U# D0 y) G9 E" d
- 2.0
3 h6 g& Z- J' t/ J/ c" b9 n - #即10的1.3次方的结果为204 r4 s% p, Z3 V1 q
- >>> math.log10(20)! |+ h; U) C$ B7 s) G9 E
- 1.3010299956639813
复制代码 % H5 T5 p; R# E# W) O
math.log1p(x) 返回x+1的自然对数(基数为e)的值2 ~" p, Z* C3 h0 V$ J
- #返回x+1的自然对数(基数为e)的值
7 x& b3 B4 ?# W - log1p(x)
- u# ^" l, I |( B, S - Return the natural logarithm of 1+x (base e).
$ F: q' y% s8 }# Z4 w3 d8 M - The result is computed in a way which is accurate for x near zero.
" Y; |9 u' U: L, W$ z" Q6 i. E( Z - >>> math.log(10)
' q& S& Q! `: h) P& i& \ - 2.302585092994046
7 Z, y% B4 l+ r# @" }! `$ H2 I - >>> math.log1p(10)! `8 X9 N$ z2 R' f( O
- 2.3978952727983707
( i& ?1 Z! X+ K) n7 l - >>> math.log(11)$ K) F5 k3 ~. M5 j" T4 s
- 2.3978952727983707
复制代码 7 M+ L9 Q* U: ~; ]6 k( a
math.log2(x) 返回x的基2对数
0 `/ I4 G; H1 a1 S6 ^8 U7 ^- #返回x的基2对数
* A+ z% W, o0 F* f0 j4 V - log2(x)
6 M# E) U2 r& h& h/ R2 y. f; x - Return the base 2 logarithm of x.
% Y" h! _6 n' l& S! _+ Y - >>> math.log2(32)
' }* \( Y5 Z2 }& J* O: P2 ` - 5.0
) ?/ b b" z5 S2 ^- m( E& ? - >>> math.log2(20)
]: q/ k- U6 d( N f) j4 H - 4.321928094887363- ]/ g2 }8 E2 E/ l) P
- >>> math.log2(16)0 r* V; y+ g3 n8 V$ Q
- 4.0
复制代码
+ n9 | Q) V- W2 k" l3 v8 b; _, Rmath.modf(x) 返回由x的小数部分和整数部分组成的元组
# x8 L- P# B( m5 C% s4 Q- #返回由x的小数部分和整数部分组成的元组
1 i' Q% @$ T2 O. a# Y - modf(x)' d4 ]0 v, O! }) f! s6 m+ Y
- Return the fractional and integer parts of x. Both results carry the sign
% W: m6 ~ I/ [7 M - of x and are floats.
& B$ ^9 @: b5 c8 E( s6 @/ I - >>> math.modf(math.pi)6 w3 {) w" f5 Z8 a
- (0.14159265358979312, 3.0)# [. Q* @4 N2 u! s: g
- >>> math.modf(12.34)" q3 ?6 `( N% G( L1 m
- (0.33999999999999986, 12.0)
复制代码
: q" w2 g& v% y7 l' ~math.sqrt(x) 求x的平方根 Q5 D& k+ N: f# Q& `7 e' K
- #求x的平方根
: `, X: f5 v- a2 }5 d( }. B$ @" { - sqrt(x)4 _6 y& n" B$ c2 n) C F8 L1 i
- Return the square root of x.
' i9 m# n" Y% m4 D - >>> math.sqrt(100)2 }8 A n4 k% r0 R
- 10.0- y7 M3 d4 l3 N7 U$ F$ ]
- >>> math.sqrt(16)
$ ^$ `5 p' N9 t3 O6 H- w - 4.0
: I& G' w9 L8 e( ^8 o6 b - >>> math.sqrt(20)
7 p; h1 ^: s( |% A - 4.47213595499958
复制代码 & A6 _& a R2 V1 F3 g
math.trunc(x) 返回x的整数部分- #返回x的整数部分
; g. J+ o3 z8 g - trunc(x:Real) -> Integral
# q! z7 r% r. v+ M6 o' j' L- E - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
; x' x$ b1 o9 F* v9 q/ A - >>> math.trunc(6.789)" h* `8 Q1 ^' U. F
- 6
4 S8 u( m) s. D" w7 ~- s( a j; J - >>> math.trunc(math.pi)
8 E# w, e& j$ y$ i - 3
7 L& E- t* ]6 O& i4 q - >>> math.trunc(2.567)
) u- F3 L# W1 d/ @/ J2 b% b: M - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|