马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# p( R4 }* H. k' N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。* E5 a# _6 H" }/ U6 u+ [/ n
2 L: w/ R/ t* K' ~+ X! E
方法1:) L7 e+ ?8 D; Q
- >>> import math
! u; j4 b, L* ^" F9 F - >>> math.sqrt(9)
% S2 u$ I9 y8 r - 3.0
复制代码 方法2: {6 J3 l {+ {8 e: F* y
- >>> from math import sqrt, L0 s4 t! x3 v c
- >>> sqrt(9)" [8 Z& L+ ?8 L. ]3 j8 L4 a- ^
- 3.0
复制代码
! |- C/ ~, K9 |5 c
, {0 f% ^: `8 y2 y% i7 ymath.e 表示一个常量" Q% r$ z. E" C* }
- #表示一个常量3 _% S9 ^4 `) r: [& n5 v
- >>> math.e/ Q' R7 n: t3 _/ c) Y7 }5 m" A
- 2.718281828459045
复制代码 7 C( h' p0 C* k) p. s' e' \' i J
math.pi 数字常量,圆周率
, `" k5 J5 v& R! }- #数字常量,圆周率2 I2 @' w# @; B, n5 }0 K
- >>> print(math.pi). W. A+ t. K4 L/ k8 R+ F. s7 p
- 3.141592653589793
复制代码
; b( J- y1 D0 C: x. |. ~math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! g6 |" I: h. b3 }6 }' h- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ I+ f6 i5 G4 C - ceil(x)
9 \+ c; `3 C Q; ~6 D- T - Return the ceiling of x as an int.
& y8 H- `% v+ S0 ~. t" F - This is the smallest integral value >= x.6 \ t/ \7 T3 H0 Q
- ; Y, u9 l. ?5 S1 D
- >>> math.ceil(4.01)/ a9 W% }! C: e4 u0 v: @* o" V
- 5
- q) u* f5 J$ F* u - >>> math.ceil(4.99)& g& M+ |; j: w) h$ a7 o+ j w
- 5
! a+ t* q# i$ P1 J B3 ^7 n - >>> math.ceil(-3.99)/ `% y0 }& d! K
- -30 ]7 W5 y. \2 I3 O" H" p1 t
- >>> math.ceil(-3.01)5 w# l( ]3 u8 _
- -3
复制代码 8 S3 r" \( c: S* @' m
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) `! g0 W% k" f- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: [$ h3 H' s. h1 R* h( F - floor(x)
/ @* s0 ?: Y8 D4 E1 }% g* {9 e - Return the floor of x as an int.+ r# b) q" I- k* e
- This is the largest integral value <= x.
$ |# H; z! B* K$ c1 M$ u$ v( w5 s - >>> math.floor(4.1), v8 F. }* T- j( u$ S, ]9 H9 |
- 4
, g# a; G2 `: p+ L- H - >>> math.floor(4.999)8 ^' e! w8 V _! `/ I& S6 `" |
- 47 V, d! x* O9 R5 b+ ~2 x
- >>> math.floor(-4.999)6 H# q r9 c0 T4 O; g
- -5
; \4 V. m, |, H& E$ T7 n! c9 ^ - >>> math.floor(-4.01)
" D* R1 O* ^& k. s% ]: A - -5
复制代码
0 C# x3 F( {& r1 G7 j9 O7 omath.pow(x,y) 返回x的y次方,即x**y
/ ~7 Q! O& Y9 R* E7 e- M$ K! S: u, [- #返回x的y次方,即x**y7 E. |# E/ C+ P! F; ?
- pow(x, y)
3 M+ a- A$ k7 p a' a) } - Return x**y (x to the power of y).
; v% n9 N% T) B1 Y% a - >>> math.pow(3,4)
) s4 j" N; K6 R3 t# B o7 B - 81.0
, e$ ]* u: ?/ o4 f8 H' H - >>> " F( [% i: {) L) G
- >>> math.pow(2,7), ]/ w8 n6 W, @ y
- 128.0
复制代码
$ R" ~' t9 P# G1 W: v# }math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: o4 w7 f& t, h& [: y! ?( F+ W- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 u' Y" F$ T; H, R3 ^ b1 c5 f
- log(x[, base])
% r& B1 L$ w* x* p$ Q2 | - Return the logarithm of x to the given base.
- c. { c4 `: Z* ], R8 H - If the base not specified, returns the natural logarithm (base e) of x.
& [9 V9 C1 ?' U) G) E7 W$ l% h - >>> math.log(10)
5 q9 C8 e6 e8 _& |1 s) l! X- |! e - 2.302585092994046
/ p4 u R9 P3 Q. l3 Y - >>> math.log(11)
0 @" j8 l) R m$ s" X8 `, a - 2.3978952727983707
7 F$ Z& u4 t! P& c! t4 ?% a; }! l - >>> math.log(20)$ H; ^- N+ |6 E) H) t! U
- 2.995732273553991
复制代码 # ]2 Z5 M: l5 s2 E6 w
math.sin(x) 求x(x为弧度)的正弦值
- @6 z S8 L" I d' V# d- #求x(x为弧度)的正弦值
$ r. y# U$ f% \( D3 B - sin(x)
5 ?3 V" f! c% K: O; \ - Return the sine of x (measured in radians).- [* Q6 a- p, |. `. U f
- >>> math.sin(math.pi/4)& \+ H' S4 ?; b. g @& \" y+ I3 @
- 0.70710678118654758 S0 Q6 L; g3 [/ J1 O& ?% S M! x
- >>> math.sin(math.pi/2)4 q0 J1 C' j* O8 j. F* `
- 1.07 V& S3 C/ Y6 I. O' k: U/ s$ ~
- >>> math.sin(math.pi/3)
8 S2 f: {7 N" t9 t$ P% y9 { - 0.8660254037844386
复制代码 5 R- Y% H/ w0 j. s8 a. \2 o& r# G
math.cos(x) 求x的余弦,x必须是弧度5 o5 y$ ~ f" ]( m& V; d' x5 G
- #求x的余弦,x必须是弧度# U; a5 d2 j& ^% [6 ]9 h2 w
- cos(x); U4 ? o7 K7 T2 ]
- Return the cosine of x (measured in radians).7 u0 b6 J: o% E7 D+ V8 o. H
- #math.pi/4表示弧度,转换成角度为45度" ?- G" b5 s' @! o1 d; ]
- >>> math.cos(math.pi/4)
8 R# V8 {, _9 s' g+ r* A8 J' u/ d. V - 0.7071067811865476
4 U2 {8 e5 v0 S; n; Z5 d! s - math.pi/3表示弧度,转换成角度为60度
, c2 H$ }8 K1 k P6 p - >>> math.cos(math.pi/3)# E1 R$ [! ^" p6 r7 S
- 0.5000000000000001
0 Q+ v y2 k( v3 y$ @. M# M5 g2 M - math.pi/6表示弧度,转换成角度为30度
, H( b$ |( |) m$ F9 h, q - >>> math.cos(math.pi/6)2 V4 J6 Z( {6 K$ V
- 0.8660254037844387
复制代码
" E& H3 H( }3 }math.tan(x) 返回x(x为弧度)的正切值
- `3 O# a$ g" P' y8 U# D- #返回x(x为弧度)的正切值
* j0 N7 o5 u, J4 {$ G* f - tan(x)1 ]0 p) O7 `( ^( B4 n8 c
- Return the tangent of x (measured in radians).
: l& U1 U, U% G2 \ - >>> math.tan(math.pi/4); o, V) i2 J; F) n- y3 p
- 0.9999999999999999/ Y- d. F- B; G9 M. }" _
- >>> math.tan(math.pi/6); x( U+ Q% n- D" _5 u
- 0.5773502691896257
0 P+ r) i; X+ J' s) F4 u& p8 m - >>> math.tan(math.pi/3)- X1 n' U9 l6 s5 L
- 1.7320508075688767
复制代码 : C' N8 g( R0 j* S
math.degrees(x) 把x从弧度转换成角度
$ I0 N% b! F- E5 c# {0 T+ ^- #把x从弧度转换成角度
. G+ k: Y- L4 f4 i+ A) \5 e6 E* W - degrees(x)& H' M' I T- X2 Y0 B5 v. R$ ^ Z
- Convert angle x from radians to degrees.: k4 u- h$ n6 ^5 _1 i! _
/ Z$ p( @ l0 H8 w; d- >>> math.degrees(math.pi/4)
, I: U5 r$ `8 }+ x% \6 v, b9 a! f - 45.0: M' r* ^* o9 Q! ?. o: h# l
- >>> math.degrees(math.pi)
5 k3 l" y9 ]/ Z! {9 t, E - 180.0
: [0 B2 ^- Z7 c' j5 M% W8 @ - >>> math.degrees(math.pi/6)
7 W; J" E9 [4 F; Z - 29.999999999999996$ D/ ?& W* Z) W; D
- >>> math.degrees(math.pi/3)
X# O9 `" m# T+ F" w6 A; v - 59.99999999999999
复制代码
# k5 Q/ u i+ x6 [0 ]4 `" amath.radians(x) 把角度x转换成弧度
: h0 Z- m& Y% M) r! K4 S' J- #把角度x转换成弧度
% e& {- u$ v# l, Y, N4 t4 v - radians(x)" x* L5 [6 ?# I7 f2 S& z9 j) Q
- Convert angle x from degrees to radians.
) H @0 l$ i$ ^0 {& V! k - >>> math.radians(45)8 Z1 q2 D: O! {' J5 p* l8 z9 \9 x
- 0.7853981633974483
, \2 c& \. S9 x. _; I9 b) v - >>> math.radians(60)
3 D) t s8 m% f# H: D. m - 1.0471975511965976
复制代码 6 _) H9 Q1 ~; W% C
math.copysign(x,y) 把y的正负号加到x前面,可以使用06 f/ b! m* C! q2 E( T6 r
- #把y的正负号加到x前面,可以使用02 A& H9 E1 I( x& ?, U2 m/ x
- copysign(x, y), i9 U4 K& C7 r! e6 A5 [
- Return a float with the magnitude (absolute value) of x but the sign
+ R f# r# P" l - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) z5 F6 ]8 V0 U O; p! Q
- returns -1.0.9 a1 W- [: f! O) ^
" |( z/ s) w+ \9 z4 q- >>> math.copysign(2,3)
' O" P0 r/ w; z, M7 X8 S - 2.02 ?/ \' s+ s7 T+ d
- >>> math.copysign(2,-3)4 x# i+ j2 v# I) g
- -2.0
9 p9 X c( C, q: O b! f - >>> math.copysign(3,8)0 W+ A! h& p0 `+ S& T
- 3.0
2 m- e4 g+ N( r# q - >>> math.copysign(3,-8)) V7 {+ X* y5 _, L+ f! ]2 q
- -3.0
复制代码
( X% I' G5 e2 P: B6 p4 Dmath.exp(x) 返回math.e,也就是2.71828的x次方
2 G: t& w) u3 {! H- #返回math.e,也就是2.71828的x次方
4 y: t! d8 c, \5 g% S - exp(x)
" U4 K$ {& r/ W; D% \6 T w/ ~ - Return e raised to the power of x.$ b: }2 w d: K L- Q. Y$ r% ?
- ! D: ^4 W. ^5 X: p; _9 f4 S
- >>> math.exp(1)1 U `2 T* n* I1 P2 i( I5 L
- 2.718281828459045
! V1 h/ e7 ~$ A, F2 ~6 _3 f# q& Z/ X - >>> math.exp(2)
8 E: V0 K" K2 d0 X& ~ - 7.38905609893065
! t! x! A3 s& A; z3 i! F( Y - >>> math.exp(3)
% } t1 _. c$ z+ W+ Z6 v - 20.085536923187668
复制代码
4 W' J5 e4 h# D4 p1 m# v. P) A# nmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# U) m( J+ }2 [: g- #返回math.e的x(其值为2.71828)次方的值减1
0 Q- g1 r* f& z) Q/ s: n - expm1(x)
& |" _& X% F K+ Q& i) @; x0 S1 w - Return exp(x)-1.4 r& }. c% _8 o5 _! [2 O
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.0 z$ a+ F8 e. r1 p+ T& ~
* _7 f. K% [# P) F7 h- >>> math.expm1(1)! K# |, O% H+ u8 K7 m
- 1.718281828459045
' d# ?& B! ~6 P' s4 `2 U, @4 F - >>> math.expm1(2)
1 _: j8 @( u0 V' s( x% n: E - 6.38905609893065& R4 n( u9 O( v) R0 k$ Q7 W
- >>> math.expm1(3)
* ^# C$ K, B8 y! { - 19.085536923187668
复制代码
& y, P) ]7 e3 P4 n: `5 K) Smath.fabs(x) 返回x的绝对值' S0 h) O: X) p6 Z& I$ s" h1 @
- #返回x的绝对值
/ |6 C* v! { Y' q+ I - fabs(x)
; a5 [* S9 s6 S: b' C - Return the absolute value of the float x.
. L# _/ m& m4 W) Y - * B, \( A! h7 Z
- >>> math.fabs(-0.003)
) z$ m. c: I1 v: q0 l - 0.003
0 L: E& t7 D3 R! L/ U e - >>> math.fabs(-110)( m1 P9 s( B- w( \% _ }
- 110.0$ P* @$ p h; O- z. m9 |2 U
- >>> math.fabs(100)9 W! N. d$ o5 r' M0 I. z+ c
- 100.0
复制代码 " o! _( U/ J2 ^& u1 n D# X
math.factorial(x) 取x的阶乘的值
! z* A: g) E9 F# Z: |8 B- #取x的阶乘的值
- x) {+ S) k3 l& H" { - factorial(x) -> Integral
# Q8 E& G0 X4 s1 q8 O7 V& w! g - Find x!. Raise a ValueError if x is negative or non-integral.
; w2 }! U; |3 @, w0 j1 p - >>> math.factorial(1)" |7 A: p! _/ Q9 s4 E
- 1
( H: R* z" l: z5 t: K+ v8 P - >>> math.factorial(2)
0 n9 y' R; W# l+ y% H - 2
$ T0 L3 L. }, @( a( b - >>> math.factorial(3)
$ b9 Y0 g1 n. n' W - 6
, e. H: S- [ |5 c W" l9 e - >>> math.factorial(5)
3 @! W& ?. J3 ^8 b2 X! z9 g9 U - 1204 q- r, S4 ]. B% i$ F; L: k
- >>> math.factorial(10)
* p- F- Z5 u# U. } - 3628800
复制代码 * ?6 O3 @+ a8 U1 K
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
5 u2 S$ n# i3 d- #得到x/y的余数,其值是一个浮点数( I" [( A9 j1 c
- fmod(x, y)0 K6 N4 F& W: H& t2 I
- Return fmod(x, y), according to platform C. x % y may differ.
! J0 V) `6 G/ x - >>> math.fmod(20,3)6 E: x* E E$ B
- 2.0
% n7 x4 X: @- z- \9 B- O8 _ s - >>> math.fmod(20,7), Q' F. h5 q# V; Z1 l4 g: M
- 6.0
复制代码 1 N; K% ~) o, r3 Z3 J; c2 Z; r
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围 k4 j3 {& H4 d5 R) \
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' Q; W3 m+ S* B
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
5 i% E" m6 Q) |$ g4 x - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, \! v, O# h( d0 p& v" o" `0 b$ J
- frexp(x)) I( O9 X- x/ S3 X) e0 U9 m
- Return the mantissa and exponent of x, as pair (m, e).
: R; o. c2 b" g9 F( T6 W4 a - m is a float and e is an int, such that x = m * 2.**e.
; |2 s% I! g, D' @ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
2 `( @6 {/ V: O# {; G: K: h7 W - >>> math.frexp(10)
* D% _: J+ a" i) n4 W+ Z - (0.625, 4)
* Q! v/ a7 p. g9 M. ^# R' e - >>> math.frexp(75)% W1 u) [' Y& O5 I) K0 w# h
- (0.5859375, 7)
; e5 |2 E7 O H3 B K, L! R - >>> math.frexp(-40)
5 ]. r0 G% o5 d2 s! b( n - (-0.625, 6)
9 g, h9 r9 a% J1 _4 { - >>> math.frexp(-100)
* L5 C2 d n5 }% \ - (-0.78125, 7)
3 ^% b" n8 a) ]8 z# ]; B - >>> math.frexp(100)# U( r1 ^0 n/ P' `0 y9 j
- (0.78125, 7)
复制代码 & p0 w s( u* Q. D3 {7 |
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
- |. ^7 R4 f# G6 G- #对迭代器里的每个元素进行求和操作
" @+ h4 O. Y$ M9 {) g9 t - fsum(iterable)
" a: m1 o/ Z3 |" q - Return an accurate floating point sum of values in the iterable.
0 i# }* B( I8 }( {, `0 s9 Q3 Y - Assumes IEEE-754 floating point arithmetic.4 p( D7 A4 Y; M! D! O' |) K n
- >>> math.fsum([1,2,3,4])
4 y; _ r7 s2 s) p; t, Z2 Z( k! ~8 ` - 10.0) H5 @$ j o$ H/ v h; S
- >>> math.fsum((1,2,3,4))& m; |, O$ H3 p0 I
- 10.04 ^0 s4 V/ V5 K
- >>> math.fsum((-1,-2,-3,-4))
0 \' j- b2 o2 y. D% m - -10.0 u; Y2 f4 o1 g4 _' u* F
- >>> math.fsum([-1,-2,-3,-4])
0 `- j5 F8 b- G6 K' M8 z8 p/ A - -10.0
复制代码 0 v7 b; w) D3 b3 j' k
math.gcd(x,y) 返回x和y的最大公约数
2 x0 r3 {$ t# n/ i, q- #返回x和y的最大公约数
3 b, C7 n/ `( h - gcd(x, y) -> int5 c6 ^) _- I) Q8 D
- greatest common divisor of x and y
0 `' C0 _9 s& n/ X' A3 o* R' Q$ K - >>> math.gcd(8,6)
0 C& a* y% F' |& f. x" R# O" m - 2
& z0 o/ ^1 F0 h* ] - >>> math.gcd(40,20)) |4 N7 d+ A& R: ~ C) |$ ?" b
- 207 ?) b4 E9 S$ S, w3 `# k
- >>> math.gcd(8,12)8 C3 U0 ?, D% w6 L( o6 ^0 M: L6 @8 ]$ v
- 4
复制代码
5 |; W( C5 c( J' gmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False6 c% x6 \9 j! Z ?5 y
- #得到(x**2+y**2),平方的值 g' ~# m* U3 b2 Q
- hypot(x, y)0 N- t# S9 y0 S1 T
- Return the Euclidean distance, sqrt(x*x + y*y).
; z; r! J# D$ W3 {0 N; b - >>> math.hypot(3,4)
6 j& I8 v4 n1 Y7 ?6 U - 5.0" c/ V! k) a( [; S
- >>> math.hypot(6,8)
( I, m: Y( b- Y. L: n4 `$ c' F - 10.0
复制代码
. t- }5 e2 g: G. n$ r+ J6 Ymath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False! G+ y9 a0 G8 `2 S b
- #如果x是不是无穷大的数字,则返回True,否则返回False) w: j& U6 H. u& Y
- isfinite(x) -> bool( C$ u/ X; h6 h& V
- Return True if x is neither an infinity nor a NaN, and False otherwise.
1 {; [0 s4 N' y K4 D - >>> math.isfinite(100)% Q/ k+ `. @( `
- True
5 U1 I! L; L& E# N - >>> math.isfinite(0)
+ Z0 s6 N5 h) Q0 ^5 N! p/ n$ @' { - True
1 E# C& y+ i3 G! L2 r2 k - >>> math.isfinite(0.1)
% P) B- E- z# t) s6 v* L - True. y0 N$ [4 Y4 m: i5 n
- >>> math.isfinite("a")! S8 _, `8 P7 e$ `
- >>> math.isfinite(0.0001)3 b* ?4 P2 z* h1 y
- True
复制代码 * ~5 O3 h% _ e; i
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False3 O. A: N4 |+ q/ k" X2 s
- #如果x是正无穷大或负无穷大,则返回True,否则返回False. g0 b$ | s; {5 Q$ h
- isinf(x) -> bool* K0 l" u* ?2 l A) j
- Return True if x is a positive or negative infinity, and False otherwise.
/ |; T9 w& l" K# N0 D3 z, x+ ` - >>> math.isinf(234)
! S. ` G: i! c2 f; o* W+ X2 u - False; | T/ F+ R$ o4 E0 `
- >>> math.isinf(0.1)
$ Q/ W2 b( q4 X2 @: h - False
复制代码 5 e r$ J- w( N: e
math.isnan(x) 如果x不是数字True,否则返回False1 h; h5 m ~/ z/ n( W7 O
- #如果x不是数字True,否则返回False* H# n' r) F& M; d
- isnan(x) -> bool
% d! u V2 r. p' g( T) |! [ - Return True if x is a NaN (not a number), and False otherwise.( q; S4 v r# ^2 t* `+ m0 @/ {
- >>> math.isnan(23)) S. e8 E: i1 c0 J8 B' C) f" A
- False! \4 |" ]) h _( Z
- >>> math.isnan(0.01)
) K$ ~' z9 Y# n! [3 E! L' I - False
复制代码
& M/ y4 U1 x0 \* D. Rmath.ldexp(x,i) 返回x*(2**i)的值$ q, V9 D4 m9 ]) D
- #返回x*(2**i)的值
- E; W0 e; ?+ m3 ^( L4 A1 H$ | - ldexp(x, i)1 u- s0 \ n6 o( k
- Return x * (2**i).
3 Y4 O6 h. [# O$ M: W! Q4 A- ~$ N - >>> math.ldexp(5,5)4 }6 C( O' Z; N2 g) E; b9 M, h7 f+ ^
- 160.02 u" i% u9 h8 V
- >>> math.ldexp(3,5); v5 u2 Z4 m( H% S/ W: W* P
- 96.0
复制代码 ' U* Z9 W' L) {/ {
math.log10(x) 返回x的以10为底的对数
& d; j1 ] r0 m/ ~: u! x8 D+ M- #返回x的以10为底的对数
# m+ ~" e5 A, E - log10(x)9 _5 B% Y5 }' d9 Q }% S
- Return the base 10 logarithm of x.* n7 j! u' O4 Z: O$ r
- >>> math.log10(10)# V2 `- A) r, o# f) [
- 1.0. ^% i/ b' v) g
- >>> math.log10(100)
, G7 d5 s6 Q( r - 2.0* S& W! O7 t9 S/ Z8 a$ B i
- #即10的1.3次方的结果为20$ z" |8 u# i( [$ @: @( p4 X
- >>> math.log10(20)
1 g1 F9 r5 {& r, G' d' _* X - 1.3010299956639813
复制代码 1 G; {) v. D5 S3 D: M1 y5 ` V
math.log1p(x) 返回x+1的自然对数(基数为e)的值
# n) C& {( {- f; ?# e( v- #返回x+1的自然对数(基数为e)的值$ V! s0 a) `) s. M& z
- log1p(x)
0 f5 z) G1 S% v1 z6 c5 _2 Q6 Z - Return the natural logarithm of 1+x (base e).
" s7 [" i/ z4 ?9 J) F! @1 i - The result is computed in a way which is accurate for x near zero.
+ P: f/ @5 z& E0 ^4 y- X2 u8 r - >>> math.log(10)5 i$ b2 s9 M% ^8 h: P9 L. l) h, {
- 2.302585092994046$ k5 z$ E: L5 B3 d6 p
- >>> math.log1p(10)% Q0 S) v8 j2 v1 s) n( s/ p; A
- 2.3978952727983707
3 I& O7 m7 P& S/ x/ a4 S - >>> math.log(11)
# u6 Z' ~/ q& r: s6 O$ F - 2.3978952727983707
复制代码 ; R6 U. m7 S+ _% ^+ r
math.log2(x) 返回x的基2对数! d3 X9 j3 | G6 |+ S
- #返回x的基2对数
. X/ l7 o2 ?! ]* \) d9 f6 V - log2(x)
7 |6 a2 q& c2 D0 | - Return the base 2 logarithm of x.
6 v" z: z" q/ {4 N9 e! u8 ~1 a2 v - >>> math.log2(32)) Y9 n' v0 |* k9 j; z' s8 V$ M
- 5.0! I) B, o: f: B9 |
- >>> math.log2(20)
6 ^! {% R+ @+ l; _" }9 z$ y - 4.321928094887363- M" W% h' p1 p3 H5 @: [
- >>> math.log2(16) ^8 O. N+ A# f! L6 D( l) Z
- 4.0
复制代码 % T- x2 G; T& M$ @8 O. p0 B& }
math.modf(x) 返回由x的小数部分和整数部分组成的元组2 o1 _/ H+ R/ p/ p3 s1 p+ c
- #返回由x的小数部分和整数部分组成的元组4 A/ c& Q: c' s$ I
- modf(x)
5 I9 h% ] U8 X) ^( L8 T3 C - Return the fractional and integer parts of x. Both results carry the sign- Z9 V/ L- L, Q8 Y6 b! |9 O% ?
- of x and are floats.
3 B J Y) g. g' ~0 m - >>> math.modf(math.pi)3 _9 P+ Z/ [! J& `6 Y( O3 D& o$ M1 V3 s
- (0.14159265358979312, 3.0). _! H, |' L8 J4 d. X
- >>> math.modf(12.34)
# H4 E4 Y4 B$ _9 g - (0.33999999999999986, 12.0)
复制代码 9 q2 m" }: s- j# k4 S. E
math.sqrt(x) 求x的平方根4 P8 V5 M T- R: g5 a$ `
- #求x的平方根
; Z1 \+ j( ^ m+ \1 P2 M - sqrt(x)0 J% l! p" Z ` |
- Return the square root of x., n1 t# }$ B! n6 n m5 B5 Y
- >>> math.sqrt(100)0 |4 j. r* A' s+ y- }
- 10.0: \ q5 F( G! G& F7 x0 D% r- N
- >>> math.sqrt(16)1 L5 n( S4 h! M/ N% h4 D/ u
- 4.06 v2 s7 x7 i$ l T& l
- >>> math.sqrt(20)
: A o0 b7 z: R$ u - 4.47213595499958
复制代码 ; V+ \9 a# U) V- W8 c7 b% F
math.trunc(x) 返回x的整数部分- #返回x的整数部分: y* W2 I: K" c
- trunc(x:Real) -> Integral* q) t" s; ^- Y
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
, N6 i% a* h' m. \& z4 V: C! d' c - >>> math.trunc(6.789)0 X9 L6 o4 Q% v5 y7 i9 }
- 6
* t/ {: ^+ |4 I/ j2 n - >>> math.trunc(math.pi)
& [( ^9 D4 h* f: p: w7 ^; k# w+ l - 3) T' U! i. a0 [2 ` F* j
- >>> math.trunc(2.567)
" s/ [6 X0 t6 v: j* n* E) ^ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|