马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ r6 T1 V2 @" F5 X% Y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! _+ \' @3 `8 i- A( t4 a* }$ V
" M- O* f2 ?- B+ w; ` F方法1:
: c, M+ y1 w. o8 {* m& D% O- >>> import math
^- D- j0 a) H- C+ ] - >>> math.sqrt(9)* a8 `. b: A: z7 }+ A* C i# S6 {
- 3.0
复制代码 方法2:; |9 W" a" U; E+ O
- >>> from math import sqrt
% L; V9 H3 X" d& Y - >>> sqrt(9)3 V$ n0 M d" s3 J7 l
- 3.0
复制代码 " \% @2 b* F1 A7 T# E
8 ^! I* q: U9 h# f
math.e 表示一个常量7 N* Y0 }8 T# k! I8 r! F: S3 g
- #表示一个常量
3 G; p# x6 H0 F/ c# B& I - >>> math.e5 O, M. T/ p& M- s7 V
- 2.718281828459045
复制代码 ( a$ k% o" q/ y# A3 t
math.pi 数字常量,圆周率2 h8 M$ I h) r/ d
- #数字常量,圆周率
: {6 G" h1 w5 G2 M2 ? - >>> print(math.pi)
' h+ G* L6 f$ X9 u$ I - 3.141592653589793
复制代码
; u5 T: J; K( H# k# omath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 F* p- d& l) V% T0 P( L" L
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ q$ Z8 R: n; z3 I - ceil(x)% v6 c6 T6 g/ r! B; U
- Return the ceiling of x as an int.7 R- E3 |7 H6 F- L' Y
- This is the smallest integral value >= x.; A0 H4 U. ?5 r
/ ^6 {8 X* X1 T- n- >>> math.ceil(4.01)
1 g& e2 K2 [, ?' x& F1 X+ F - 58 u0 V# V' F8 k6 U5 |! a
- >>> math.ceil(4.99)
. Q; O7 O; {/ p( J - 5 y4 k; A2 c1 f* z$ \' x: Q# D
- >>> math.ceil(-3.99)
5 C8 Y0 v8 M$ o1 v8 W7 d - -31 Y: e6 J4 c. p* p c8 c
- >>> math.ceil(-3.01)- d2 C7 V3 ^$ a3 H5 c
- -3
复制代码 3 T$ M6 T: r* Z! H }/ |
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& f) [* d- Q1 ]: z- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身! W% z% n2 { Y% B1 I; ?
- floor(x)
: f/ x! m3 \( ^ - Return the floor of x as an int.$ }3 J! |7 s2 q# ~$ m9 J
- This is the largest integral value <= x.
; @0 h. o# Q, M+ x. g* b - >>> math.floor(4.1)* k% f8 f8 h1 J! B# ]
- 42 H, M$ B; R3 Z+ [
- >>> math.floor(4.999)" z( T& W* q* T$ ]: l; r
- 4, U+ u( m. G( e4 r. z9 X* j9 R1 V
- >>> math.floor(-4.999). u9 `. ~2 S3 Q. c; C+ V
- -5
( ?" u! Z7 A) F# F - >>> math.floor(-4.01)5 }" v: ^' `" @- @4 z
- -5
复制代码 $ F6 q# P- X" E' C
math.pow(x,y) 返回x的y次方,即x**y
& ]7 R! |' C$ A8 P6 L6 X- #返回x的y次方,即x**y2 A, S* K4 _2 x' L/ o" a P1 C$ P) q) {
- pow(x, y)* L& V' g5 X- z
- Return x**y (x to the power of y).
8 U+ V( r7 Y/ j9 y! H: c - >>> math.pow(3,4)
; P7 j8 N7 D* M* @9 m4 T8 y% l9 d - 81.0
2 S; o4 ~4 t7 v2 J+ I - >>>
. `( b: |0 l# Q% }$ K! A - >>> math.pow(2,7)$ E# T/ N5 {2 i* ?" ]
- 128.0
复制代码 ! r: |9 v9 v! C6 \
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% {4 }4 L+ Q& }! B: {8 I2 C9 D- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) W6 {1 C8 h; S' Y8 q
- log(x[, base])
4 Y+ T# [/ ^6 `6 v" U - Return the logarithm of x to the given base.6 |9 c" J( Z Q
- If the base not specified, returns the natural logarithm (base e) of x.
\; w8 T1 \) e+ l [ S - >>> math.log(10)! x$ K% H4 c, W$ A
- 2.302585092994046
9 W% X$ z- W) Y - >>> math.log(11)
7 W& b3 z! ]2 W0 S - 2.3978952727983707
j# i) I1 d2 Y* ` - >>> math.log(20)8 S4 S+ M) h, `0 B
- 2.995732273553991
复制代码
0 S& ]/ k7 C* y) f2 Omath.sin(x) 求x(x为弧度)的正弦值; ?( w! b* g+ L$ e6 s& u4 H
- #求x(x为弧度)的正弦值
% v" C' n/ k G) {( V9 v* C - sin(x). f8 A% e7 K! Y$ e$ f
- Return the sine of x (measured in radians).
4 z( |+ V0 T' A7 C2 A3 f% o* A - >>> math.sin(math.pi/4)% U& a W$ T8 U2 l' _
- 0.70710678118654750 o9 Y. X6 h6 g2 Y* n
- >>> math.sin(math.pi/2)5 g3 T2 {& K O0 h
- 1.0+ ?4 |3 V5 a# c3 H- ?) P% }1 \& k+ Q
- >>> math.sin(math.pi/3)2 C, Q1 _( E: E) m& N
- 0.8660254037844386
复制代码
5 g( n: Z/ ?2 A% a, rmath.cos(x) 求x的余弦,x必须是弧度7 I9 e* s( e: G& _
- #求x的余弦,x必须是弧度
' ]5 m$ y( A+ |/ q3 J/ x. u - cos(x)4 t* L7 `' h8 U; Q
- Return the cosine of x (measured in radians).
6 \6 ~& }+ {! ^4 A# o - #math.pi/4表示弧度,转换成角度为45度& S1 J" O) q2 ? E- v( g
- >>> math.cos(math.pi/4)
6 I I: f3 U: H* S$ D6 \ - 0.7071067811865476
7 o" Z" F# Z. [- r - math.pi/3表示弧度,转换成角度为60度. u/ w( i1 j% h/ x& \& B, j2 Q
- >>> math.cos(math.pi/3)8 f$ W, T& l$ @ X8 w% U# W6 j
- 0.5000000000000001$ m* A4 X3 L$ M/ Q G( i6 r. O" s3 ^' A, ]
- math.pi/6表示弧度,转换成角度为30度8 W; R) \- T$ F4 |' e
- >>> math.cos(math.pi/6)3 l: s; d# F) F) l+ h' }# ?
- 0.8660254037844387
复制代码 ( x. \8 w3 f( e9 V
math.tan(x) 返回x(x为弧度)的正切值% G# \2 B" ^4 O
- #返回x(x为弧度)的正切值- l0 ~) s% a; t2 d1 e
- tan(x)! W$ o6 W9 H: @
- Return the tangent of x (measured in radians).
! Z3 @ K0 Z) h" D - >>> math.tan(math.pi/4)
) N/ s2 Z2 y1 E4 `* V# w, Z" J - 0.9999999999999999; {4 i* y0 H) c' |" w7 ~9 ]+ `
- >>> math.tan(math.pi/6)
% x. U0 t' F8 @% t4 N- f - 0.5773502691896257. F. W$ N4 g# D, k E, J
- >>> math.tan(math.pi/3)
8 O: c4 p x7 [3 B& W" A - 1.7320508075688767
复制代码 " \* e$ b5 Q: m) t* l- B9 c
math.degrees(x) 把x从弧度转换成角度. h/ r$ [# T% }/ l3 `5 j2 t
- #把x从弧度转换成角度
2 J0 R( L8 _$ o) D1 w - degrees(x)2 R9 W: ]/ @" _) n4 f* a4 a: s
- Convert angle x from radians to degrees.
" ~+ J9 J/ w5 O# D. L- h9 T - ; {- v# B' \ [# B, z4 ^% _- c
- >>> math.degrees(math.pi/4)
: U8 n& y0 s7 F - 45.0
' F1 E5 U$ z2 g2 ]0 c - >>> math.degrees(math.pi)
4 {6 E, q6 {4 Q6 _ - 180.0
* c6 Q; h" t* w! Y8 h, i - >>> math.degrees(math.pi/6)8 P& ?8 I$ o! Z3 C) q$ Y( Y
- 29.9999999999999964 h. N5 \1 {% T5 e/ r( F
- >>> math.degrees(math.pi/3)
+ Y# ?% g; ?9 D$ _0 u. k2 L - 59.99999999999999
复制代码
) F3 e1 }- q( \8 _- tmath.radians(x) 把角度x转换成弧度# }- g) e0 r5 P( ]5 L1 G# ?
- #把角度x转换成弧度8 Q* V- W6 C& e/ F
- radians(x)' G6 F7 s- o0 _1 [. x v* m; a
- Convert angle x from degrees to radians.! b$ n( d% e5 D( u9 w+ T
- >>> math.radians(45)
5 |+ R6 ^& T" N( O1 ~7 W - 0.7853981633974483
$ ] N O4 m# T- j& J - >>> math.radians(60)7 L, e" s' o' W. }( G1 s8 N
- 1.0471975511965976
复制代码
6 O- }+ m0 o6 F4 ]$ b# xmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
3 b2 o E1 H; f6 T) h7 F4 B* v- #把y的正负号加到x前面,可以使用0
' C, F$ G6 i& l8 s0 c - copysign(x, y)! X) B. O& a3 P e/ ^3 N
- Return a float with the magnitude (absolute value) of x but the sign / @& _: }6 b: x) q
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) " [0 L% O$ Y: ]# k" n% I
- returns -1.0.
, h& C5 s4 |' O0 x' L s, ~- @ - ! g" E- B. }( c* _
- >>> math.copysign(2,3)
; _% U5 K4 r# `1 Q. S - 2.0
- r9 A9 P+ R1 K: _' k( d$ b9 G - >>> math.copysign(2,-3)
9 @3 F3 z/ a" }. F/ c! J - -2.07 X# h+ z+ V) j. a6 O
- >>> math.copysign(3,8)/ `# V$ I: ?* K! G1 d: G
- 3.0/ E% d1 V0 j. c6 R/ w( k4 w S
- >>> math.copysign(3,-8)) s4 J. x- |. l6 Z( D
- -3.0
复制代码
1 b4 p$ r, {2 u/ m% _% z: b. M. |math.exp(x) 返回math.e,也就是2.71828的x次方
9 Q, B/ m6 p- o' e1 p2 R. C& g5 e# D- #返回math.e,也就是2.71828的x次方$ M5 i) H8 F; `2 ^3 G/ s: S
- exp(x)
9 y! \6 C3 R; y4 x6 x" N - Return e raised to the power of x.3 G$ [/ l) Q+ w& C
- + ?1 i! i2 k! n/ D
- >>> math.exp(1)9 N1 ~% J) C) s. c! j4 N, Q
- 2.718281828459045* u- q) W: [% i% @& i- G
- >>> math.exp(2)4 x8 B. M+ F) Q
- 7.38905609893065
' q2 W& Z/ R# e. m' \3 o- b0 q - >>> math.exp(3)4 }8 B% A& V( C9 B0 v
- 20.085536923187668
复制代码 $ [8 U" d3 F5 O" H) n& A: v( d
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减14 a) [3 R2 J! i. @0 ]# S: w& v; g# A5 R
- #返回math.e的x(其值为2.71828)次方的值减1: [. Y9 T x% W- }1 `7 n
- expm1(x)
% w8 I, d0 U( q3 T, X2 S# g - Return exp(x)-1.
3 J1 U) k' o8 C - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
6 }3 \& E/ ]% o$ _& }, D# n& L - " T! O: O3 ?+ l# c' O3 v
- >>> math.expm1(1)
' x* ^9 ^) Y/ M3 h j- q; i2 {& X9 Q - 1.718281828459045 ~" F; y( v, m4 J. U% v: I
- >>> math.expm1(2)
2 U! s* Y4 `/ J" H* Y - 6.38905609893065
" w8 `3 [8 ]+ J8 A/ ] - >>> math.expm1(3)- f0 l# h" [% T# Y6 B6 w
- 19.085536923187668
复制代码
, o2 j& d' y' T5 U4 [/ P' H3 b" Umath.fabs(x) 返回x的绝对值
& ~7 _( ^( Y- h6 T6 k- #返回x的绝对值
- [: k! ~; V8 u - fabs(x)' A0 x9 p( x/ V7 [, ^
- Return the absolute value of the float x.# I- q- m4 L7 `( T w3 j
- 8 z% i9 E7 L2 k
- >>> math.fabs(-0.003)
0 E/ T# e$ o3 }# y - 0.003" |5 F8 g: B; J+ S+ b
- >>> math.fabs(-110)5 E4 }* q7 ?; t9 j; m% A9 w
- 110.0
5 _ y4 I/ W3 C7 j& F - >>> math.fabs(100). s! i* _% O: @0 c' n
- 100.0
复制代码
8 I7 U4 O" @' |+ Q6 ^+ i/ ymath.factorial(x) 取x的阶乘的值1 ?) D! i$ ^$ M5 t; g9 o5 S& q
- #取x的阶乘的值
5 j! x( f" N: ], F8 d - factorial(x) -> Integral
9 K5 O, z& @: C Y6 ~ - Find x!. Raise a ValueError if x is negative or non-integral.
. E8 H7 g2 `2 n7 H - >>> math.factorial(1)1 k) |. |" `3 r* N* z
- 1
! m: o5 ~ q( {* H$ i( w - >>> math.factorial(2)3 {' u- {1 A$ m! |) W/ j* n. ?
- 2
# ?9 r9 h: Z k3 ~ - >>> math.factorial(3)1 F% E: f8 x% \. o8 z) T
- 6
/ S5 @; \2 e9 b$ f5 a" b - >>> math.factorial(5)/ e$ [5 a T' |/ R' ?9 i
- 120' x G d) A! g. P
- >>> math.factorial(10)
+ p7 f$ x* [( R6 o$ o& e0 r - 3628800
复制代码
0 ?: L' U2 _, \& a1 ^: Vmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
; z) g) v4 @& A8 S. }- #得到x/y的余数,其值是一个浮点数& R0 E& r- {; ~# q7 C8 z
- fmod(x, y)
6 J- N6 c3 _) k( A1 k. B- D0 j e4 \1 X - Return fmod(x, y), according to platform C. x % y may differ.
$ I/ B2 ^ m) j- s; |* D - >>> math.fmod(20,3)$ S" u9 M. ?1 s3 d4 w8 ^) g3 ~
- 2.0, J5 l" _ z0 v5 l
- >>> math.fmod(20,7)
3 c$ r. {# |4 s+ w" m5 T; R - 6.0
复制代码
+ I& ~8 X. _+ kmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围6 Z! g6 m, O# ?- J* G' e8 w
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ r& D1 K6 ^8 M! C! S( O
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值! L V; ~$ O- v2 @
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
! X; M2 q6 @ j i: p( z - frexp(x)
/ c% F/ H( @4 N6 N8 B( p - Return the mantissa and exponent of x, as pair (m, e).6 p9 R1 t2 X) _) n- ^5 f
- m is a float and e is an int, such that x = m * 2.**e.& R+ V: x& ?" f9 \
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" ~( l$ L P1 F6 o - >>> math.frexp(10) @: F3 j; E7 K6 [4 }( [4 b
- (0.625, 4)
5 A! V, V Q5 n' Y) j- L9 B6 g- h* F - >>> math.frexp(75)
- M( N2 I" h% G& k( e2 _2 q+ f - (0.5859375, 7)' _6 x4 B7 r% U0 p1 c S) B( ^ a
- >>> math.frexp(-40)* o& B+ A' V- ^" q, p; @0 `
- (-0.625, 6)8 v ^6 n. [2 S
- >>> math.frexp(-100). k4 V/ b& m3 l+ c8 g) G2 ~
- (-0.78125, 7)
/ u6 K: d( W" Z! j - >>> math.frexp(100)
* G% j) v! O: \* l7 k - (0.78125, 7)
复制代码
' M; d2 h1 l3 C. K; L: xmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
# k1 k' h& Z$ \- #对迭代器里的每个元素进行求和操作
9 l+ x3 m, J+ W2 H$ w, W - fsum(iterable)
) L5 I. ] R& L7 ~! e% ?2 ~$ y4 | - Return an accurate floating point sum of values in the iterable.
, _1 M+ i2 c8 Y) L* Z+ d - Assumes IEEE-754 floating point arithmetic.( M" y1 h+ z: B0 i% U
- >>> math.fsum([1,2,3,4])
1 o$ z& p# P9 ~" d: h - 10.02 ]& [+ q% J u1 P/ l
- >>> math.fsum((1,2,3,4))3 S" a0 U! J5 r6 _7 M- M* F
- 10.0( G& G6 l' t! h* F& U$ S
- >>> math.fsum((-1,-2,-3,-4))
/ ^" _* Z' p5 }& X - -10.0
8 v: B( E" F. \' D. l: ] - >>> math.fsum([-1,-2,-3,-4]) \; `: q- Z4 a/ d; N
- -10.0
复制代码
$ I. e4 G. @" E. ~* f$ \math.gcd(x,y) 返回x和y的最大公约数* [6 {) b% P9 I; {+ Y$ W
- #返回x和y的最大公约数
% Z9 G1 m2 {" ^9 r, g: |$ U; B - gcd(x, y) -> int8 C$ K: p* ~0 b! Y! ]( ~
- greatest common divisor of x and y
) P: Y1 X7 {6 v; u! |1 I4 F5 b - >>> math.gcd(8,6)
6 X- S: u6 p8 f- w6 G) J - 2
9 b* Q0 u3 E& P; D0 }+ @. { A2 z - >>> math.gcd(40,20)+ X" |3 D8 B% ~/ O5 o
- 201 u2 s; e3 H2 V; j
- >>> math.gcd(8,12)
. N/ h6 s& ?* K - 4
复制代码
$ L6 h1 u5 q! cmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
" w/ y1 z+ H/ m, [# V# ~; z# ^( }1 W- #得到(x**2+y**2),平方的值( c9 R7 d" [1 l `
- hypot(x, y)) F2 Y2 b, v9 P
- Return the Euclidean distance, sqrt(x*x + y*y).' P8 F. U- e! P9 s3 s
- >>> math.hypot(3,4)0 m/ a& o u- [9 D- U$ p- V- x; F
- 5.0
2 f- P$ q0 v" E. g' h Y1 U - >>> math.hypot(6,8)
+ }8 R2 k: {+ m v; M - 10.0
复制代码 ' n) v0 C" M9 s: s) H
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False4 ]5 g9 @) J8 P6 B
- #如果x是不是无穷大的数字,则返回True,否则返回False
0 S- k8 A7 h0 P2 J) M* j - isfinite(x) -> bool
/ [: q" P2 R) d( {, I; } - Return True if x is neither an infinity nor a NaN, and False otherwise.- q0 X$ u; a& y' c( b% |
- >>> math.isfinite(100)
2 Q9 g' y& f I - True
- o3 A" G! b5 h8 S( |: N - >>> math.isfinite(0)
' j! ?$ c" x* J - True
3 R4 Y; b5 \ y4 z+ Y - >>> math.isfinite(0.1), D8 o8 x7 ^# E J/ T, W4 R# m/ K
- True/ b$ F/ D6 j" i5 J: f
- >>> math.isfinite("a")
8 m0 L/ |4 _# r+ c% j! |: d$ T - >>> math.isfinite(0.0001)
# N* u( f+ Y2 k$ X4 ?( ^ - True
复制代码 $ e) x6 o8 U+ y: }
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
3 ~3 z/ J! ~$ b* } b! ^& s- #如果x是正无穷大或负无穷大,则返回True,否则返回False
" U# G0 q& Y- o+ O8 F( \ - isinf(x) -> bool. D2 r6 h6 W* w/ W. c! U' E
- Return True if x is a positive or negative infinity, and False otherwise.
( Y: Y2 C8 ~* V x! `6 S7 [ - >>> math.isinf(234)
, H' |5 _: C! _) j" K) L6 Z - False9 X8 @3 K3 C" D! W- f/ w1 S3 t
- >>> math.isinf(0.1)2 G+ D) Z* a @ M1 a+ g
- False
复制代码 . J/ }; u# _% J7 e% M' L
math.isnan(x) 如果x不是数字True,否则返回False
4 G( d9 e* X# d# ?- m4 Z- #如果x不是数字True,否则返回False1 d" w7 G2 d7 l4 M8 h1 M1 l7 u
- isnan(x) -> bool* F# t- J( ~; A! ?0 K) B5 i5 v- s
- Return True if x is a NaN (not a number), and False otherwise.
$ r* i* B+ I, q - >>> math.isnan(23)8 k2 i& _2 H. V
- False
% k0 Q. G: [$ v( h - >>> math.isnan(0.01)
. v( Y2 i/ w" ]* E$ S# c - False
复制代码 / P0 i" B1 x H( n
math.ldexp(x,i) 返回x*(2**i)的值4 J* M# I$ k n
- #返回x*(2**i)的值! L% D" J$ Z0 W. h
- ldexp(x, i). @+ C1 j. a. J* L# M2 |
- Return x * (2**i).
( @3 e7 q1 F" A; B) E - >>> math.ldexp(5,5)2 r8 q2 X6 r" W4 g8 H; h- }5 I7 u
- 160.0% D1 d+ Q8 J; e# Z/ u7 c
- >>> math.ldexp(3,5)
$ V9 {+ h& [9 x* h - 96.0
复制代码
4 f- o3 w! h" }, b7 M1 Mmath.log10(x) 返回x的以10为底的对数
+ D8 |% ]1 _& a7 @6 a6 x- #返回x的以10为底的对数9 w6 p! S; P0 ^8 A
- log10(x)
* X4 D# X2 G: N& E' Z8 T - Return the base 10 logarithm of x.
8 p( Q3 b4 U' P) m& v7 i, Z6 a& | - >>> math.log10(10)/ f6 ^; n4 h9 p, `
- 1.0
$ Z2 K+ n- X& d) V - >>> math.log10(100)8 \3 Y& W) ^% a( v2 A S( l2 b
- 2.0
7 T9 |3 V+ j7 w, b - #即10的1.3次方的结果为20# P3 f5 g' o5 v, c
- >>> math.log10(20)9 V9 l8 |+ o `2 l/ G6 ?
- 1.3010299956639813
复制代码 % \9 ^$ K7 N4 H) L
math.log1p(x) 返回x+1的自然对数(基数为e)的值
" E2 D) r$ g: a2 H- x! d0 ]6 w3 c- #返回x+1的自然对数(基数为e)的值7 X* L [1 x1 I( U h6 N
- log1p(x)( X3 `+ u; _8 c# d' P0 u
- Return the natural logarithm of 1+x (base e).# x/ j$ ]& F0 j' ]! k; }" z
- The result is computed in a way which is accurate for x near zero.0 F# O6 u, V1 j" x. Z
- >>> math.log(10)
6 C$ o b: J% d( H - 2.302585092994046
* y6 t l- i, U4 c" B3 y! G - >>> math.log1p(10)8 Z. |! Z G# p' {/ c2 v5 j: B
- 2.39789527279837077 m1 {) p/ x, F
- >>> math.log(11)
# W6 J7 m4 E$ {5 @# G+ f& P - 2.3978952727983707
复制代码
1 D* ?' c( Q$ n; |* |" }4 Imath.log2(x) 返回x的基2对数
! R- ]% K! l- R$ Q- W- #返回x的基2对数
% i r$ U( c, K/ e% _6 ?6 _ - log2(x)) v# s! X2 m6 M9 r9 [
- Return the base 2 logarithm of x.
/ [) {# h+ S9 k2 E/ @ - >>> math.log2(32)
5 }4 V, @( I( b9 \: b5 J - 5.0/ Y& g) D# r9 ?7 x0 |; U# x+ s5 R) q
- >>> math.log2(20)& z9 m" f& J/ E/ _# U* R
- 4.321928094887363
1 Y* J& K0 E/ E' M9 |" E - >>> math.log2(16)
8 j5 N7 _9 t- x- P& W" q - 4.0
复制代码 " y6 H6 [, b7 P# z1 C+ l
math.modf(x) 返回由x的小数部分和整数部分组成的元组
5 M E" A7 s8 s& i9 ? Q3 l- #返回由x的小数部分和整数部分组成的元组- H7 j4 B# h8 D$ b4 Z: B- H
- modf(x)! f5 j- \$ [/ U" U7 s5 v" a1 m
- Return the fractional and integer parts of x. Both results carry the sign0 D% s8 y" @2 ?2 i$ N+ S
- of x and are floats.
, C- t3 a; N1 i% E1 ~4 w) ~' | - >>> math.modf(math.pi)4 j, ~9 I& t9 k
- (0.14159265358979312, 3.0)
. Z& |, u& [: I! g9 @6 a - >>> math.modf(12.34)
; p) B( u0 U1 P, E+ Y# } - (0.33999999999999986, 12.0)
复制代码
3 j5 d; W! {1 H" ?math.sqrt(x) 求x的平方根3 {( I( o8 y: a1 W. i) ~
- #求x的平方根8 V! c5 [- v& p C" B I4 g
- sqrt(x)
# q* G$ l4 H1 V- H3 X$ x - Return the square root of x.
' w6 P7 V% _& `9 x* H. S7 \ e - >>> math.sqrt(100)% q7 N( w; p1 H6 ^
- 10.0" y" Z/ U! D0 N, @
- >>> math.sqrt(16)* q! z/ ]* @" y: m3 N. H
- 4.09 b: ^% M* H: K# N
- >>> math.sqrt(20)' P7 l6 c& c; s8 z! D# E) J* r
- 4.47213595499958
复制代码 ' J/ u9 W. K; ^" Y, S
math.trunc(x) 返回x的整数部分- #返回x的整数部分) V1 E+ }0 s7 d' {* H' T
- trunc(x:Real) -> Integral
. m' c( B2 c4 j8 z- `& H3 Z - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
% x7 D1 a. t E% U% X5 W2 r; R - >>> math.trunc(6.789)3 f6 h" J4 a5 W# P: m
- 69 \! f" a$ ~5 B6 S" \4 N E: N1 M
- >>> math.trunc(math.pi)
. X5 ^; e- k( U4 q5 _2 x - 3% }1 i) L* [7 }% l/ W5 T' A, Y: ^
- >>> math.trunc(2.567)3 g" N' ?% k" n H! p% W0 q" F
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|