马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
a& k# Y% D# t! P【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。; y( l$ x3 i! c
) Q" ]/ p) m7 m6 I' M方法1:1 S2 D0 H) D5 ?! e
- >>> import math
) M& Z' `2 {$ k - >>> math.sqrt(9)
/ @( r6 M) }$ W - 3.0
复制代码 方法2:3 r5 }1 @8 B: E. h
- >>> from math import sqrt3 ]7 L( o/ {" t' S, p
- >>> sqrt(9)6 W. X- V R; u& ~1 o' P
- 3.0
复制代码
; c q. Y: f: L; c + s6 c K* _- e* l& ~9 w! {
math.e 表示一个常量
& L5 s0 `. s5 U" ]5 U' j- #表示一个常量' I7 g9 e1 B4 N9 A; p
- >>> math.e
R( J0 i' e7 ?* B2 E# ], h4 T - 2.718281828459045
复制代码 2 z. q7 E. E& _1 r' `- J+ ~
math.pi 数字常量,圆周率6 @- g3 C2 H) T$ A- H9 U2 _
- #数字常量,圆周率
6 Y/ M: e; _$ p7 }* ]* Q# Y - >>> print(math.pi)
0 C3 X" `- J7 a4 H' h - 3.141592653589793
复制代码 + P8 M' [4 I* b2 L7 ~
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x& }/ L; a% x& g/ n1 ^& ~7 q6 a
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ R% \* j0 P% p) Z. u
- ceil(x)9 e/ v* o" v5 F# V1 |( q/ B- ~
- Return the ceiling of x as an int.; ?, ^) J; F2 f6 D2 w
- This is the smallest integral value >= x.
6 j, I, @* \; A6 b% `4 j
9 ]' Q o* b8 t" a- >>> math.ceil(4.01)3 Q1 p) T5 l1 j
- 59 `3 F% f4 `: v+ X6 G! d
- >>> math.ceil(4.99)* r& h8 y( ^1 ~$ A7 O
- 5
& v; L' E$ t5 D/ I0 d* o! w - >>> math.ceil(-3.99)
3 D0 }2 \) ~% p3 \/ i" ?2 p# _ - -3
; y, c4 ]9 v. l7 h - >>> math.ceil(-3.01)4 d" A) h$ M7 f2 Y$ W
- -3
复制代码 $ y- h) Z+ I+ T$ Q
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 X4 [0 H9 Q# u; X- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 M `: b: Q. W0 U; _ o - floor(x)
' ~9 o; s" k I7 P& M$ L: K - Return the floor of x as an int.$ h; F9 ]2 ^4 n+ C$ r) x+ t
- This is the largest integral value <= x., z* u- S7 k. D! `. y. S1 w! l
- >>> math.floor(4.1); U8 X5 x( K$ r9 N* w) x
- 4
' _/ {# }. r( k' j L* |5 S - >>> math.floor(4.999)2 Y( Z0 s6 h' g2 F2 W
- 4
" \- ]9 R: M* Y& v* J( A - >>> math.floor(-4.999)1 G$ {) j1 ~) R. t' }5 q& p5 x
- -5
1 l$ p8 _/ h) Q" j - >>> math.floor(-4.01)! J9 H; `! r0 t' E( `8 T, Y
- -5
复制代码
( {7 t, N! Q+ [$ Umath.pow(x,y) 返回x的y次方,即x**y* T9 ~+ m$ R. M' i) Y8 v& L
- #返回x的y次方,即x**y+ E7 H; p0 z! W0 q/ a
- pow(x, y)4 v* N h; G6 t
- Return x**y (x to the power of y).0 K; H6 d$ ]7 M3 m/ N
- >>> math.pow(3,4)
; M& u9 J9 b! f - 81.0/ [0 J9 W6 u0 t; P9 x N
- >>>
2 t' l4 y8 T% t) x* D3 z; I - >>> math.pow(2,7)
. ]. R4 O+ V8 {: ^, s; ?' Z - 128.0
复制代码
$ ?: g+ T$ d' E3 t; |math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" F: T. P; h/ F/ c
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( `! {! D$ ?7 o, I - log(x[, base])) d6 o5 D+ I1 r$ G; ?+ l D
- Return the logarithm of x to the given base.8 h5 x6 l$ O+ O& C
- If the base not specified, returns the natural logarithm (base e) of x.: W4 R( y$ n( Z
- >>> math.log(10)0 K; a9 c9 L: B7 e+ G" h
- 2.3025850929940467 p7 ]) y9 \; l2 J
- >>> math.log(11)2 \' N5 _" X# z' U" i
- 2.3978952727983707/ ?) l5 `7 s6 F: X8 g+ t) H
- >>> math.log(20)
- g( m r5 u) ~( J - 2.995732273553991
复制代码
" E4 [% d& m& y# R5 S/ m3 k7 rmath.sin(x) 求x(x为弧度)的正弦值+ s( {' ~. z( g `* D
- #求x(x为弧度)的正弦值
% W- I6 w" W( |9 d) q - sin(x)4 c0 R; r) a. @# U' r/ t% a# P5 g! e
- Return the sine of x (measured in radians).' R4 e* ^! y; g7 ^9 V3 V4 v! Y- n
- >>> math.sin(math.pi/4); B" d: t6 Z1 H9 j% h. g8 K* ]) E
- 0.7071067811865475
+ ^" _' B' t" f - >>> math.sin(math.pi/2)
9 d4 l7 @- C& M; |# K: Z - 1.0
0 S4 T; w0 L* s; }0 y - >>> math.sin(math.pi/3)7 b# C+ Z; s `2 t: p
- 0.8660254037844386
复制代码 ' m; W$ K+ E2 j; J6 H
math.cos(x) 求x的余弦,x必须是弧度
+ Z6 d5 j% ^! n+ g; `- #求x的余弦,x必须是弧度
0 C, E% c2 x8 u - cos(x)6 E" z& E8 `$ ^& I+ r$ x
- Return the cosine of x (measured in radians).7 H9 V9 B2 \# X2 S9 b X4 }8 D5 ?! h
- #math.pi/4表示弧度,转换成角度为45度
s* o, A3 D' E - >>> math.cos(math.pi/4)
0 J' s4 y X* ] Y+ P - 0.7071067811865476
2 A8 ]* B0 d5 @) ? - math.pi/3表示弧度,转换成角度为60度
7 o0 a: X5 _+ ^, |1 d) [ - >>> math.cos(math.pi/3)
O3 \6 ^2 x! L3 E* t - 0.5000000000000001& D' x! e2 O" m
- math.pi/6表示弧度,转换成角度为30度5 P" I+ Y8 M: \7 v9 J/ I9 f
- >>> math.cos(math.pi/6)
" @, s" p* o; I3 l: T4 j: Y8 \ - 0.8660254037844387
复制代码
, r' q8 \' s+ s, R7 Jmath.tan(x) 返回x(x为弧度)的正切值
$ Q! f& y( y0 q: ]" t8 e2 F/ X- #返回x(x为弧度)的正切值- r! e9 q% \ c+ X
- tan(x)
) ^1 {# k* P! g6 v: d, |3 _1 ` - Return the tangent of x (measured in radians).
! u' t1 c# `, z - >>> math.tan(math.pi/4)
k6 P2 ?2 T6 h* b - 0.9999999999999999
2 D% R0 b. J* T) X - >>> math.tan(math.pi/6)
% m1 q' S& ^3 c. t3 x8 z; t - 0.5773502691896257' g8 I# a3 F' `, i. h- N; `
- >>> math.tan(math.pi/3), c# d& x$ U+ @- \2 i( V
- 1.7320508075688767
复制代码
# V1 S' u9 M' f7 t' cmath.degrees(x) 把x从弧度转换成角度# i( R1 o+ K3 M/ a
- #把x从弧度转换成角度
* P. c5 ^2 c1 u" S/ @! z - degrees(x)7 @3 P! z5 |% l$ ?
- Convert angle x from radians to degrees.
: }- R) f; h5 S - 8 D5 C$ z# c5 W ^) J
- >>> math.degrees(math.pi/4)' R; Y0 u% b4 g& c
- 45.0
+ V' L$ H) A% O( ~ - >>> math.degrees(math.pi)- q; D: d! G9 ^
- 180.0* M- w+ j- x/ \2 z0 \
- >>> math.degrees(math.pi/6)2 P/ W# z5 ?, f8 |
- 29.999999999999996! J. m$ o9 q9 u; J' s3 ~/ f" J `" b
- >>> math.degrees(math.pi/3)4 Z5 [9 ?' T. j1 V, O
- 59.99999999999999
复制代码
/ V! M- m* k: h9 e! Jmath.radians(x) 把角度x转换成弧度
3 b( v" Z: X8 q- ]% b. K- #把角度x转换成弧度7 ~9 G: s7 X% N# o- ], S
- radians(x); M$ \# L: t! }4 H
- Convert angle x from degrees to radians.
% S! F" c/ b4 ^: x; }4 I% I - >>> math.radians(45)
7 R. b7 G4 ~1 l. [( N - 0.7853981633974483) ?' W0 I; U( a4 t
- >>> math.radians(60)
; A) m( I# r/ X% p1 |( K7 T& Z - 1.0471975511965976
复制代码
) D" T# Z" g9 y( p$ @! o! Cmath.copysign(x,y) 把y的正负号加到x前面,可以使用0/ ]: b% C( }* M
- #把y的正负号加到x前面,可以使用0
# S0 I0 |+ A& q, Q2 ]5 c - copysign(x, y)
$ x% J" z8 C5 K+ R9 h - Return a float with the magnitude (absolute value) of x but the sign ; J( ~# v9 z7 e$ V
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
! u, F$ o# b y( y; O& H6 s# s. ]$ E - returns -1.0.
1 U4 N1 [" T( H - 4 Z; v/ R2 ^5 z- K
- >>> math.copysign(2,3)9 _. ~" f) Q& H
- 2.0
$ `, P- e" L9 }# ^$ M4 i) p! t: Z - >>> math.copysign(2,-3)
4 L* X0 w" y% A2 d" Y9 l2 u9 j( ? - -2.06 O+ X4 N" |; ^
- >>> math.copysign(3,8)
8 i0 w+ I) A! m+ m4 b7 U Q - 3.0: c9 i; K& i3 X1 @
- >>> math.copysign(3,-8)
5 N f1 [# A; M& F- H7 X - -3.0
复制代码 % h7 [5 \7 H! z: Y9 {
math.exp(x) 返回math.e,也就是2.71828的x次方
+ |) g' K4 y9 l1 J; T; i- #返回math.e,也就是2.71828的x次方
- g$ g. a, I' A4 G: L! o' \1 c2 d - exp(x)
% K \8 Z) J# }2 g5 E - Return e raised to the power of x.; K j% b* \8 O/ x0 g% E) i
- ) B7 y {: [; k
- >>> math.exp(1)
3 r2 {9 k6 E- ?- i - 2.718281828459045
8 Z: `) c% g% [# T0 P8 w - >>> math.exp(2)- e. }! m1 |- T6 |
- 7.389056098930656 d4 W0 ]1 k8 S9 w) L6 x4 t; H
- >>> math.exp(3)5 h3 y$ m; t0 B# P! A V3 U9 w
- 20.085536923187668
复制代码
1 M5 i3 v4 b6 q6 Y: o1 X' Gmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1: F+ @. `9 {* J6 a! a
- #返回math.e的x(其值为2.71828)次方的值减14 y5 t0 h! _ [0 e9 r
- expm1(x). O2 b3 ~) t% ?( {
- Return exp(x)-1.
$ e0 }* K/ F# ]8 g - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ a' E1 P R0 i. d) T+ r: y" c
, Q& B2 u1 v" s, ^: T% h n- >>> math.expm1(1)* w- L* m# N% ~$ ` A8 |. ]' j
- 1.718281828459045) D1 `( z, ?8 n' M. }
- >>> math.expm1(2)8 U7 q3 l. d+ ~! i2 o
- 6.38905609893065
6 C5 l! {* }/ |) J - >>> math.expm1(3)# _" x! m& E: }8 k/ f7 a
- 19.085536923187668
复制代码 . N# d+ |. o. ?0 x
math.fabs(x) 返回x的绝对值: d/ }1 j: P+ Z2 K8 T X& \
- #返回x的绝对值! x E' [1 J0 a4 m! u
- fabs(x)
- |. U) Y. s" ]1 z: A1 i! B - Return the absolute value of the float x.+ b5 [9 H8 f+ f) l. T* f) O: k
4 c, B) i% B( g( g: X8 j( t& s- >>> math.fabs(-0.003)/ a% _* b# r: L# }4 ?, O4 q
- 0.003, f0 N; `1 f2 {8 M8 V7 y! |
- >>> math.fabs(-110)
9 v( h% x1 ~- F - 110.0" w" R2 K1 p0 F/ Q4 W# L8 k U4 o
- >>> math.fabs(100)
9 y& G9 b, s) ] - 100.0
复制代码
+ R& x ~9 d5 s1 d; bmath.factorial(x) 取x的阶乘的值5 w8 @" v5 r& m( |" Q i- h
- #取x的阶乘的值2 t8 v A# |1 k6 b; M4 {
- factorial(x) -> Integral
! v& v" H4 Z2 u - Find x!. Raise a ValueError if x is negative or non-integral.
: m3 e1 _: [/ l3 ^# i# E1 B - >>> math.factorial(1)+ u0 ~. m+ p) i e4 M6 Q
- 1
: }* {; Q; F* O - >>> math.factorial(2)6 S" ?9 M5 _: O+ \8 N7 x
- 27 n: L- a0 G* B: G3 U
- >>> math.factorial(3)
) G* M2 G* w) b - 6
- J. y* F$ Y2 Z- f L; p - >>> math.factorial(5)
2 V$ W5 ?8 D/ c& x: t - 120
0 I4 G$ m, M) A$ |, u - >>> math.factorial(10)
: i0 w8 D$ K8 ?: E' N - 3628800
复制代码 , ~9 _9 G7 M) k' y) w: l
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
( K( U. b5 E ~1 A' {- #得到x/y的余数,其值是一个浮点数8 E' t$ l. `& w! x3 K M
- fmod(x, y)
7 _8 H+ z, E; P, N: \ - Return fmod(x, y), according to platform C. x % y may differ.
& g+ c" j; Z" z3 c$ H% {1 X - >>> math.fmod(20,3)
6 M. d B4 |6 h4 I3 x - 2.0; z: G; a( J$ d& H) ^
- >>> math.fmod(20,7)5 ~6 J- m: ]- S1 y- `3 `
- 6.0
复制代码
0 K5 i' r& u, _6 P! a$ Umath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, f* w1 r$ y4 @* D4 U
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 G% @0 R# r x$ F( z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
; h! Z, U f f# ~1 B1 R& i, E - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" [& p6 T9 }) M& H
- frexp(x)
& m2 a$ F* O' m - Return the mantissa and exponent of x, as pair (m, e).! \9 M) s$ E. V6 L
- m is a float and e is an int, such that x = m * 2.**e.6 w+ U) x: ~; M/ G! G
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.2 J* H% |/ P& h* p( B8 B; c
- >>> math.frexp(10)
) A' P y2 h8 {; l2 b: S; w - (0.625, 4)2 v/ _6 C4 z) x& ]
- >>> math.frexp(75)
+ S4 y, z/ p ]7 ^* Y5 o" u - (0.5859375, 7)# h) F- ]3 D, l- P* `" U
- >>> math.frexp(-40)
0 U% Y9 _ d1 N8 f" Y7 R - (-0.625, 6)
* \& J5 w% |1 L/ s! ?! |* r - >>> math.frexp(-100)
& ~& S" F8 i' C+ k+ u: \ - (-0.78125, 7)
7 {1 C1 t. S% _/ t- V - >>> math.frexp(100)) t ^% T: j+ s4 J; b* ^* L
- (0.78125, 7)
复制代码
: J9 m/ `0 D8 r3 w3 Z9 vmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列) z$ L4 K7 F F$ y5 F
- #对迭代器里的每个元素进行求和操作
% w+ a3 Y: @' p$ V7 \ D& M, @1 ~ - fsum(iterable)
- K L" ^" S+ Q' |9 V- u - Return an accurate floating point sum of values in the iterable.4 H. t$ j* @, o8 a
- Assumes IEEE-754 floating point arithmetic.) H# q% u# Y, t' J( I
- >>> math.fsum([1,2,3,4])
" I: J* q; I+ {, i/ s# M: s5 X - 10.0
) w2 g9 ?7 r3 h3 I' O - >>> math.fsum((1,2,3,4))
5 b3 b& f7 }' _( f - 10.0; Z" ^: t, f6 k5 |. I) k! Q+ {
- >>> math.fsum((-1,-2,-3,-4))
, `3 U( G" O0 W i2 U - -10.0
% z p0 z' J1 ^7 g1 B O2 C a - >>> math.fsum([-1,-2,-3,-4]) `! f6 O% b9 i5 G$ m, Q. [, N2 |
- -10.0
复制代码
" M% F" i7 [) T$ O: X1 I8 R2 K- }math.gcd(x,y) 返回x和y的最大公约数! V0 X) v4 C$ I0 q& ]
- #返回x和y的最大公约数
1 t, ~8 x9 B2 w- Q% A$ ^/ ~ - gcd(x, y) -> int6 ~; o# v( z' I: S4 \6 e
- greatest common divisor of x and y
7 {- y+ _8 f0 d" d9 q - >>> math.gcd(8,6)6 }% z7 \5 n/ i' P. }) a! \* B
- 2$ I; S& a3 Y9 z/ G: H
- >>> math.gcd(40,20)
B- p7 [: _0 `( z& R - 20
. j. x+ y' Z. }& E" G; | P- ` - >>> math.gcd(8,12)
7 Z J* n7 M* ]: k' S0 A$ j0 P - 4
复制代码
; H. D6 O( i' f* q- B' Wmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* N( b( j6 x$ T2 o- #得到(x**2+y**2),平方的值
2 P8 U9 \, E2 g" N - hypot(x, y)% I: E% s! N6 X4 }; c. k
- Return the Euclidean distance, sqrt(x*x + y*y).' p7 u6 Y/ p6 c5 E9 y
- >>> math.hypot(3,4)
% T' g0 y" }; s - 5.0* W6 G) M- d. r
- >>> math.hypot(6,8)1 @. {7 ^4 v" G# C" m
- 10.0
复制代码 7 b0 ~0 A M% Q! Z9 _7 r! A
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False. H. d2 e" D/ E0 l; N: N! |
- #如果x是不是无穷大的数字,则返回True,否则返回False
' f2 K2 V1 U7 k - isfinite(x) -> bool
/ E7 L: U6 O4 S- A - Return True if x is neither an infinity nor a NaN, and False otherwise.' C4 v7 o1 s2 i
- >>> math.isfinite(100)+ ~- s( ?; m+ q6 B5 Q
- True5 k/ ]- X$ d5 U
- >>> math.isfinite(0)! l) M" T" t' U! k6 [
- True; Y& K/ L1 b2 E9 u, D9 z1 ~
- >>> math.isfinite(0.1)
/ \5 C4 r0 l4 l5 X# S - True
* I" }2 I7 g3 Y+ H$ P0 _ - >>> math.isfinite("a")
( L- L9 o7 t r/ p - >>> math.isfinite(0.0001)
# C! t8 ?4 s3 m - True
复制代码 . o# z) S$ C* l- O7 I. b R
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
S" P3 D+ O' J( Z* L- #如果x是正无穷大或负无穷大,则返回True,否则返回False
; S: S3 Q5 E! i- V - isinf(x) -> bool
+ G4 J: b! x8 {1 m5 n/ k1 |. ` - Return True if x is a positive or negative infinity, and False otherwise.
) ^" W0 M0 `' r- D - >>> math.isinf(234)% Q$ Z* c3 ~9 h+ H: r
- False6 U5 U6 g3 {1 @# Z6 r
- >>> math.isinf(0.1)
7 d- l, a4 P- ` |5 N( R - False
复制代码
) ]5 N" ?' l- I- rmath.isnan(x) 如果x不是数字True,否则返回False/ m1 f) U) a6 s
- #如果x不是数字True,否则返回False
9 O$ m# Y+ t: g) z* F - isnan(x) -> bool- F2 G/ Q( I0 [4 B: o" u0 N
- Return True if x is a NaN (not a number), and False otherwise.
# H4 ^9 d" s9 [; t( ~ - >>> math.isnan(23)
6 n8 F) l1 i6 k: W3 P9 y - False
2 n o% a9 l; A" W$ P( B" E! B) f - >>> math.isnan(0.01)
( t5 R* T1 E7 l8 q( a/ j' q4 D - False
复制代码
0 C) g* u* y, C( ]: \math.ldexp(x,i) 返回x*(2**i)的值8 F% h, B: A- t
- #返回x*(2**i)的值
3 C# d6 B2 x/ Y$ m - ldexp(x, i)
: G! G+ Y i3 `" V. E - Return x * (2**i).6 }/ R1 x& w" H( Y
- >>> math.ldexp(5,5) I3 [# `9 ?: q: f7 }
- 160.0/ _: y" B$ M+ r) u' q; J/ e
- >>> math.ldexp(3,5)
" f6 h6 r6 y: w( E8 s - 96.0
复制代码 3 F5 L+ L" p- M# r, ^
math.log10(x) 返回x的以10为底的对数
4 {5 S! g& X% N3 A- #返回x的以10为底的对数) o* a+ n' [. T. `0 V
- log10(x)
1 F- D4 B; ?! e$ T9 c& ^ - Return the base 10 logarithm of x.
; S, X3 n# b+ [- C* ^& J! [ - >>> math.log10(10)
: T# H! z$ T" E7 B u4 L - 1.0
7 r0 p1 ]& Q* I1 y/ l# i8 s. W3 Y! c - >>> math.log10(100)( I* Y8 e. S o. d4 n- |* d$ D
- 2.0; P, B$ q9 ]9 P1 D: n5 Z! m# G
- #即10的1.3次方的结果为20 N) q- j- u2 `- v# r
- >>> math.log10(20)
: s& R" Y6 w! v/ _1 J% p - 1.3010299956639813
复制代码 ) {5 _) o4 Y. g" I/ r( E
math.log1p(x) 返回x+1的自然对数(基数为e)的值
1 @* H) a5 h9 d2 M" i/ W- #返回x+1的自然对数(基数为e)的值3 N9 @0 l0 w) `- ]) N5 d
- log1p(x)
: A7 Q! p0 M6 s - Return the natural logarithm of 1+x (base e).% Q5 ?* Q* ^; A* X
- The result is computed in a way which is accurate for x near zero.
- B1 ~. O! [% {+ f, N* x5 f3 `7 o6 E - >>> math.log(10)
# U5 @8 T* |# n$ |- D" }, Z - 2.302585092994046
( F( Y& ^' E. t: s7 k; v - >>> math.log1p(10)0 i8 }& R3 Q3 W: T+ r, x
- 2.39789527279837079 E; D; }9 p) p8 g
- >>> math.log(11)/ ~4 x, L0 w" @; r
- 2.3978952727983707
复制代码
2 T9 y" j1 S- o9 j6 |% b' ?! Imath.log2(x) 返回x的基2对数
. ^9 k( k* V4 E! t7 h8 D0 |. s1 U- #返回x的基2对数
9 l' ^* Y& \+ e4 o7 l - log2(x)
, R, l% ?# _3 {6 M3 i - Return the base 2 logarithm of x.
$ H* M5 v# M( }1 j; C7 \. c7 v# P - >>> math.log2(32)
. V: g& L5 z% d: v9 K$ i( O. i( R - 5.0
# u. ^* \4 k3 b t" O1 S - >>> math.log2(20)8 L5 N( V) w: @9 {- i3 M, @+ v. k1 j
- 4.321928094887363. S. n9 f- K- }! ]8 z7 ~( L
- >>> math.log2(16)/ Q6 ?, h% O9 l9 M, x
- 4.0
复制代码
0 i- g+ ?, \3 A' v* E( K8 \3 ]math.modf(x) 返回由x的小数部分和整数部分组成的元组
2 T7 D4 J7 Y$ W- #返回由x的小数部分和整数部分组成的元组8 _! n$ m* N4 _( ]& d
- modf(x)
- f) l4 T8 S0 S ]5 e5 T - Return the fractional and integer parts of x. Both results carry the sign! T. x$ u ]& \! R) o" K) x
- of x and are floats.
' h7 t4 l5 i: m( t6 ^5 @% ~ - >>> math.modf(math.pi)5 K& Y" L3 K8 ~% }7 ?& Q" B
- (0.14159265358979312, 3.0)2 V9 ~7 \5 }" [$ U5 E, K8 y
- >>> math.modf(12.34), z" s/ B$ i9 G! P/ I
- (0.33999999999999986, 12.0)
复制代码
+ F9 p' ^5 n+ P1 t# L* |math.sqrt(x) 求x的平方根
" W+ E! W5 C6 u- #求x的平方根
5 k; C0 T; Y+ {1 F3 t. C g" A - sqrt(x)7 l* O& e3 j3 x" E$ `
- Return the square root of x.
/ p0 |) y/ q( R* {, S - >>> math.sqrt(100)3 _# p. g! ~% E9 @8 ^
- 10.0
4 P7 d4 r' p* [: W- r - >>> math.sqrt(16)
& t) H: @# B/ i; ~0 U3 n - 4.0/ \% [7 @ C. y
- >>> math.sqrt(20)
6 I* h- ^4 s( P3 n3 B4 w7 | - 4.47213595499958
复制代码
7 [: J, x: X$ Q8 D. J, Imath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 G! c- h2 J, f" d( V$ ~1 C - trunc(x:Real) -> Integral
* x9 f5 r9 A j. Q9 R - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! B! i1 p/ e# B$ r4 Q
- >>> math.trunc(6.789)1 T0 Q& |0 j, _ ?' g
- 6
0 F( Q; v+ C- \0 O - >>> math.trunc(math.pi)
( L! u3 p9 I) G0 {$ |" {2 d - 3
( l" V/ O+ ^' D; i7 d# m+ m6 P - >>> math.trunc(2.567)0 k( Z3 f, r2 z* `, i7 t4 v3 w
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|