马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 [. J. R+ N9 C
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ p1 `5 F" x% R/ D) e- G9 v, R4 ~+ d9 P& B, L+ X9 [, S @
方法1:/ g% J+ k0 C/ R
- >>> import math5 U$ G/ Y1 e$ k. `
- >>> math.sqrt(9)
+ A. n# m8 m5 D - 3.0
复制代码 方法2:
6 }0 d2 e. b! q- >>> from math import sqrt
% G. S) K ?* T9 q - >>> sqrt(9)1 H5 w% ^. q6 v1 E
- 3.0
复制代码
7 `! L4 p, }8 j0 q1 o 0 J5 L' {1 P L2 R r! r. R9 C' H
math.e 表示一个常量
& S- l+ B7 X) T" X1 k# ^. G- #表示一个常量+ p+ M; U! x, J% v' |. u
- >>> math.e
|! H4 p k2 ]5 _: W$ U - 2.718281828459045
复制代码
/ ?4 @# O6 F. a# O9 R0 tmath.pi 数字常量,圆周率
* E9 J* Q5 g: w8 _# {: ^- #数字常量,圆周率
, s6 n0 }, I4 v - >>> print(math.pi)
' c% O/ Q; }% l9 C - 3.141592653589793
复制代码
& i! Y- U7 ?# Z/ I0 T3 b+ Jmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x0 j: _# A- x i- H
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
, o/ d+ o! o4 P% m; N, a3 T8 O. D+ k- t - ceil(x)
. H5 A5 k* |: M5 R: h" Z - Return the ceiling of x as an int.: y/ [# y( {1 c) s, j5 f4 M5 p
- This is the smallest integral value >= x.
8 o+ [ i* S/ i3 J - 0 u2 G i. k0 f( T) Y3 Y
- >>> math.ceil(4.01): E) X3 H8 V" ~7 Y4 ^; A. v
- 5" K9 q- }/ B7 s0 e% S, H
- >>> math.ceil(4.99)8 b; N0 D T1 n* ~4 |: t6 @; R
- 5
: k7 k0 a) l6 ?; \$ b - >>> math.ceil(-3.99)+ a y: [& a2 K8 w
- -3
4 S+ U+ K8 ~# Z4 m4 P9 z& F - >>> math.ceil(-3.01). t- R% b' }2 N* K9 a
- -3
复制代码
' x6 h1 H1 m9 n7 p' A* Gmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( ^% L; j: U6 T ]- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' h! w% j$ d8 z6 S. s3 r - floor(x)
9 W- \6 B: X V - Return the floor of x as an int.9 R1 z7 c6 S" }
- This is the largest integral value <= x.
9 c7 _! K9 F; J' D( N0 b5 }* H9 o - >>> math.floor(4.1)
4 ^9 h A9 _* d9 L9 l' T - 4& V3 L1 Z6 y/ i
- >>> math.floor(4.999)
& m. T' w" H* \, K3 w& B7 t - 4/ N! @; ~* `# j" S6 h* D( F
- >>> math.floor(-4.999)
$ M: y9 f( r# w) J5 X4 X1 V - -59 y* A0 E, P/ [" j ?* ]0 f
- >>> math.floor(-4.01)5 `; [0 P1 B" @' z8 A% q+ k
- -5
复制代码 . O* [6 D% k5 @" N7 `2 b4 z
math.pow(x,y) 返回x的y次方,即x**y I" O6 Z( Z2 l
- #返回x的y次方,即x**y% t# V6 @& c/ ^& v0 { K3 [
- pow(x, y)
% @2 }9 ]: q" V- K$ ^# p - Return x**y (x to the power of y).. s& v' ?0 q. m: o( `; k
- >>> math.pow(3,4)
+ |" h- [9 {* d# N8 u* x - 81.0' m9 `" k# o5 l8 i: @8 k
- >>> 0 P) c; A% d% J
- >>> math.pow(2,7)
5 \. D- i8 }. c - 128.0
复制代码
# H7 T9 s9 i; }. b2 [, E# U6 ~1 umath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' V' v# i1 X" c( e0 B- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! Y# p' `5 ?( u5 e) M3 H# r
- log(x[, base]) h( W, D' v8 \" C1 t! F0 L
- Return the logarithm of x to the given base.
9 R% H d3 r7 n3 t - If the base not specified, returns the natural logarithm (base e) of x.
' M% U' y( e' ~7 \) [( w - >>> math.log(10)5 {8 t- d( O( x" j' K: k, X" N7 F
- 2.3025850929940462 Y! X) w0 G& p, O
- >>> math.log(11)
8 \1 I+ Z8 T* R: O, r8 h - 2.3978952727983707% P( t' U( g. [% h- j
- >>> math.log(20)2 W: o& y* b! B5 }% C; X7 @
- 2.995732273553991
复制代码
. @( g" Z6 I5 X* q2 Wmath.sin(x) 求x(x为弧度)的正弦值" B. K8 f# d- m! K a; W; ?8 [
- #求x(x为弧度)的正弦值5 j7 m4 a" r2 P/ ]4 K) `
- sin(x)
8 X9 g+ C, ]+ V - Return the sine of x (measured in radians).
* N; W2 x6 { ]6 U- y8 \" C - >>> math.sin(math.pi/4)
, @7 L% Q4 v6 }1 A: {) d - 0.70710678118654754 ?9 @6 X v) B9 c; L" `! E) f5 }
- >>> math.sin(math.pi/2)
) }( B( ?7 B, l; `8 F - 1.0
& K& i' ^- O: `$ t6 j - >>> math.sin(math.pi/3)
3 `" U) z7 w/ C: Y0 M# u% K/ T - 0.8660254037844386
复制代码
7 q u M6 b0 h5 |8 n% e: Wmath.cos(x) 求x的余弦,x必须是弧度
# u) u5 R1 \2 P/ S( u) V+ I- #求x的余弦,x必须是弧度
8 J% [5 _1 H% o. c$ |1 D5 T5 Z) O - cos(x)
4 P' k; H, V; T- d2 l - Return the cosine of x (measured in radians).
% u# e- K% I$ A/ \ - #math.pi/4表示弧度,转换成角度为45度% E* g D# f4 O7 f! Z& Y
- >>> math.cos(math.pi/4)7 e9 _3 ?% A, o9 L9 F3 Z1 A
- 0.7071067811865476% \ [ ?; E6 T+ z) O- [" t$ _
- math.pi/3表示弧度,转换成角度为60度 ^% K% c7 `& `. C" B
- >>> math.cos(math.pi/3)
) L! I6 [" y+ o - 0.5000000000000001+ o, m0 o( G$ k' a8 A9 `
- math.pi/6表示弧度,转换成角度为30度8 T$ F# `! S! v9 ~$ _
- >>> math.cos(math.pi/6)
4 B) l* b% Q3 F8 X4 |- {, I5 y - 0.8660254037844387
复制代码 ! a9 n+ g7 L' F9 E
math.tan(x) 返回x(x为弧度)的正切值, a x! O- M" j' O
- #返回x(x为弧度)的正切值8 Y9 _4 R0 Y, j; t8 K! ~
- tan(x)& y: N+ t0 o a$ f
- Return the tangent of x (measured in radians).6 R. Z: Y! M% }/ ?5 ?
- >>> math.tan(math.pi/4)4 Z9 @0 j* z3 O$ r" J. ^" T9 f
- 0.9999999999999999 `: ?: Z5 i! b
- >>> math.tan(math.pi/6)
: Y z3 Y% h; t( {- r. F# A" o - 0.5773502691896257( Q4 h4 j3 y2 \; o$ M
- >>> math.tan(math.pi/3)
9 b) @% j- w1 A2 n ~ - 1.7320508075688767
复制代码 9 O1 m( \9 j& [9 a0 P( a, A" \
math.degrees(x) 把x从弧度转换成角度
+ r: o' l, ]8 W5 e p9 ~! `) t- #把x从弧度转换成角度0 V+ G. `: i) p& [9 J
- degrees(x)
- k; a) z0 j3 A1 r - Convert angle x from radians to degrees.
4 n B) J1 ~( Z& k! k
3 D# @! `. H# ?; ^- >>> math.degrees(math.pi/4) v8 e5 F# A: r! c, H; y( s
- 45.0
# h0 o: Y5 M/ d9 V' l. u5 m - >>> math.degrees(math.pi): m g T- V& ~% {% l, h( [
- 180.0
0 m( X! ], {% E% H9 V' G+ O4 {! | - >>> math.degrees(math.pi/6)" G& g* f2 R+ r. [# n, O* j2 M5 c
- 29.999999999999996- w3 w. a; P" }
- >>> math.degrees(math.pi/3)# V; z5 ^. A3 e4 N3 e9 t
- 59.99999999999999
复制代码
" ^' B1 B4 [1 S7 K. L# G0 _math.radians(x) 把角度x转换成弧度
( O& o6 ~5 o. b* }' Y- #把角度x转换成弧度$ H" Y/ ]) p/ n! N/ j* H2 I6 j: N
- radians(x)
. I. @8 q6 e- S& c; s" v5 K - Convert angle x from degrees to radians.
/ c6 i' N% a% ?0 X - >>> math.radians(45)
' d& Q9 u7 [- \) |) N - 0.7853981633974483 C. L& b$ b3 v% Q k
- >>> math.radians(60)- `% M2 a; X4 ^+ u( ^
- 1.0471975511965976
复制代码 ; N+ F& K9 \, O; `
math.copysign(x,y) 把y的正负号加到x前面,可以使用0# p( b: ~" o1 P9 ~2 [
- #把y的正负号加到x前面,可以使用0
. _4 U- w4 O6 `2 P+ n* |# I - copysign(x, y)& `+ l( T7 l3 i
- Return a float with the magnitude (absolute value) of x but the sign
1 S( p* [# n6 X) B r% c - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 R$ q1 _* C( J i- f8 X
- returns -1.0.0 c9 h) a6 u1 e' z+ o% p
* J! [: {7 |3 ]8 f* @% C0 a& d- >>> math.copysign(2,3)9 ]" |5 O0 ]1 A4 |- V1 h1 r B! w
- 2.0' r+ m+ M9 j" b7 X
- >>> math.copysign(2,-3)
2 z2 }1 m& H, ~3 d- s- n9 B - -2.06 k& T0 V$ @# |, j8 H3 }
- >>> math.copysign(3,8)
7 S1 p$ O: h5 Z8 k, z - 3.0 K- i9 E2 B0 z: I. X7 G
- >>> math.copysign(3,-8)
4 m2 [3 i7 l6 ~ - -3.0
复制代码
8 A/ P+ y: F" J% p8 n* Nmath.exp(x) 返回math.e,也就是2.71828的x次方
; Q" W* b3 y7 i/ }- #返回math.e,也就是2.71828的x次方% J0 @4 w7 s/ H4 \1 c
- exp(x)
8 A$ d/ _7 g; J6 n+ E+ r9 U' V - Return e raised to the power of x.& \2 z* H/ S5 N- A) G
- " J$ J- O4 a4 O
- >>> math.exp(1)
( ]7 y" d+ r3 B0 \3 X) v - 2.718281828459045
: j$ f, {# Y- ?) Q! E, X - >>> math.exp(2)& C+ k3 V8 z; R+ ]: V
- 7.389056098930654 n+ X& e# g, H( t
- >>> math.exp(3)) s7 k: u6 ?2 `7 P& N
- 20.085536923187668
复制代码
# _3 G* X9 ] i% h5 G( qmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( }; E; P. Z% s" J1 F
- #返回math.e的x(其值为2.71828)次方的值减18 A# x1 l6 S' P) g( | T/ o
- expm1(x), A) o- |$ c0 [4 a2 _+ b
- Return exp(x)-1.
3 p5 D5 v4 c! S$ |3 H: z+ D/ S" o0 _+ ^ - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.0 ^. }7 u$ H& X' S* `9 `6 A
- % X& i# ^6 g) h
- >>> math.expm1(1)
* J4 ^4 L, G1 e) @; Y - 1.718281828459045# ?8 O4 \9 }% r K% O4 ^
- >>> math.expm1(2)
4 M1 I0 u6 B, t( L0 K - 6.38905609893065
* w' c* I! c4 W) \( R - >>> math.expm1(3)/ g* p# M' X5 R: k' e% M0 Y
- 19.085536923187668
复制代码 ; e; q6 ^4 n3 `' e
math.fabs(x) 返回x的绝对值
l! Q; ?0 g8 _4 K; X; U; z- #返回x的绝对值
- q' s! d4 R2 M B2 t$ g' a - fabs(x)8 U2 E4 Y) _! T
- Return the absolute value of the float x.
# B" o0 q1 Z0 E6 q# K - ! c, J0 `' ^8 f5 A9 L* o+ m$ u& N
- >>> math.fabs(-0.003)
6 U- `* V$ l7 q+ [ - 0.003( x: U: \- F' l w. f% ?
- >>> math.fabs(-110)
8 u) g1 f# U8 B1 p( ]5 o - 110.0
1 M# y) V) t" b! F! q2 y - >>> math.fabs(100)' q0 B9 }7 g- v8 O6 k
- 100.0
复制代码 - v! ^7 E# ?1 {3 J, I! X8 B
math.factorial(x) 取x的阶乘的值9 S( _4 D3 B7 e# s) H% v s
- #取x的阶乘的值1 l/ X. F5 T4 t$ T7 |( t9 ?, F
- factorial(x) -> Integral: `2 L1 g) @- R$ w" w
- Find x!. Raise a ValueError if x is negative or non-integral.: i- S6 ?" ~ c, M1 K& R3 ~8 c
- >>> math.factorial(1)
- N# c D$ y# [/ o - 11 C2 ?4 E7 u1 @% e, W
- >>> math.factorial(2)0 ?6 X# Q/ t y; R
- 2
7 i1 m }# a+ m7 Z8 l8 c% ? - >>> math.factorial(3)
- C' d, r ?( [6 k - 6+ p8 ]) _: W' G# H% ^
- >>> math.factorial(5). c N2 }/ l9 X, C8 h, N( _
- 120! |& ^/ p" B5 R" z* W: T, Z
- >>> math.factorial(10)3 W y. x ^; L
- 3628800
复制代码
* s5 _8 D; Q4 {* H( ymath.fmod(x,y) 得到x/y的余数,其值是一个浮点数$ v2 J" P. `2 I
- #得到x/y的余数,其值是一个浮点数$ S0 t8 ?" u3 Z$ d1 n
- fmod(x, y)
3 Q0 A# O4 O) c7 g& |. h& [" Z0 r f - Return fmod(x, y), according to platform C. x % y may differ., ^+ m, d) [/ R) T+ V' p- T- m
- >>> math.fmod(20,3) Y, r* f) ]5 I+ `/ X3 w6 P
- 2.0
0 r s! B4 Z4 O9 Z: D( b# M2 w; V - >>> math.fmod(20,7)
1 H% Y$ L Y/ o) N, P- m, i - 6.0
复制代码
2 D8 R7 z( m& N+ j: Tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 Y b3 \3 z( `8 C8 w, j- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
5 U! B W0 L- S& X+ C z% [ - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 c( ^: t$ @1 z" }8 W
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 W6 |; H( `. g7 `
- frexp(x)
r. a# } `: d: J - Return the mantissa and exponent of x, as pair (m, e).
6 G& u+ K/ P; J C0 j9 l8 N - m is a float and e is an int, such that x = m * 2.**e.
' m0 i$ b/ w8 N2 u8 f0 t8 l7 N - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.! d/ _( D' [7 Z7 w8 X. l
- >>> math.frexp(10)
7 T0 i3 w" b! j, t9 D - (0.625, 4)2 K5 S" x/ _4 \2 c$ T) p6 \
- >>> math.frexp(75)4 Z+ _ N. i' s' c7 C$ O& i
- (0.5859375, 7)' J5 Q$ q: z- u D" f' w3 V
- >>> math.frexp(-40)0 n* ?+ \$ V2 Z3 Q7 t7 w
- (-0.625, 6)2 j# y1 c% I, w" d5 r5 W
- >>> math.frexp(-100)* S$ D. L# l; y5 Z
- (-0.78125, 7)6 C l/ y) A( H4 @4 u2 ]
- >>> math.frexp(100)
! j1 `4 m& D* U1 }$ Q - (0.78125, 7)
复制代码
5 ]% U* h( o |# Omath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
7 R8 v; M. f2 I- #对迭代器里的每个元素进行求和操作$ {+ ~) x3 `6 r
- fsum(iterable)6 g/ y3 ^- Z* L1 {4 o7 ^1 m; l" w
- Return an accurate floating point sum of values in the iterable.
3 a' n- h" J$ G. ?- F0 {3 ]! L6 J - Assumes IEEE-754 floating point arithmetic.) f& a4 ~; P5 i
- >>> math.fsum([1,2,3,4])
1 M$ L9 H1 v: |. H4 r - 10.0
. ~9 ]& J7 e/ Y& H: s6 C - >>> math.fsum((1,2,3,4))
; g; C# g% O3 j3 m7 ^& @" f8 ~ - 10.0" X+ F+ P% F3 X6 ]0 }
- >>> math.fsum((-1,-2,-3,-4))3 w/ G8 B& m x7 g# T3 M
- -10.0& Q7 \7 t* s5 J: M
- >>> math.fsum([-1,-2,-3,-4]): [5 Q; d5 D* E l
- -10.0
复制代码 # U1 [6 `( a* J; I7 f7 B, ^6 D
math.gcd(x,y) 返回x和y的最大公约数+ a( ?2 A+ ?' r# I8 @! h+ g4 u
- #返回x和y的最大公约数5 ~9 {! R: f& V. a. X( b/ z
- gcd(x, y) -> int5 U) A& |( [' ]8 b& n
- greatest common divisor of x and y) B% f0 }* Y4 a: ] Z5 ?
- >>> math.gcd(8,6)8 o0 {4 W5 G0 Y6 O% s1 i
- 2
+ C) m5 @/ j) `2 B/ w6 A8 A - >>> math.gcd(40,20): n7 M1 t5 R! S
- 20
' k* [* e" r; d$ I - >>> math.gcd(8,12)
* J- y( C; }7 d0 T0 P - 4
复制代码 , R( m+ b; o- v X* D: T% K3 ]+ {
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
/ ~0 _& h e, ?" a ]. K4 m, x- #得到(x**2+y**2),平方的值: U5 V* s; ]3 v2 L
- hypot(x, y)4 @, N2 b' r. o0 c9 {. [9 r
- Return the Euclidean distance, sqrt(x*x + y*y).
$ u2 J0 C: E' @9 N - >>> math.hypot(3,4)
2 S! I" W6 ]% v( s# Z/ V @. P - 5.0- A( e& q9 _" ?; F" f2 @
- >>> math.hypot(6,8)- W6 \# V( m9 b h# u
- 10.0
复制代码
; W4 t' n( b- |+ S0 Z/ h: O; j/ Zmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False8 M7 b- p- n% i4 e1 P6 e
- #如果x是不是无穷大的数字,则返回True,否则返回False
9 m7 i) i' `' y p; A6 b0 J - isfinite(x) -> bool7 \: \- v: O) p1 e \) D4 [3 i
- Return True if x is neither an infinity nor a NaN, and False otherwise.' \$ a& |- r5 X) g" }7 A& Q
- >>> math.isfinite(100)2 q: G# J$ i8 t( H( N) h5 J
- True
* X8 o% d9 p+ _0 U - >>> math.isfinite(0)
9 W+ A t- O( ^2 ?% N l; V" P - True/ p S' g6 E- i8 R5 o' o. t
- >>> math.isfinite(0.1)
+ Q5 T" n) G% o - True2 G+ P9 P8 [+ l$ G) B J
- >>> math.isfinite("a")+ e8 I- d7 _+ ]$ R4 N( f, @% k5 @6 P
- >>> math.isfinite(0.0001) M8 D. M' c5 A- P3 P! U5 }+ t' @: A
- True
复制代码 ) T% H2 y1 W, g& \
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False+ n; \/ X6 C' B$ Y# s6 o
- #如果x是正无穷大或负无穷大,则返回True,否则返回False" b w P) e0 @' D0 m' m: D
- isinf(x) -> bool: b) ^8 v- u+ L. e
- Return True if x is a positive or negative infinity, and False otherwise.5 j3 `4 X" ^: p# V# `. q
- >>> math.isinf(234)
7 C% O9 p" s3 }% x5 G$ ^# B - False
4 N- Y# b* _. A6 U, C& K0 ? - >>> math.isinf(0.1)
/ d! l; t3 P9 A. {2 Y( z5 y0 m' E - False
复制代码
. w/ D' w. ]# e! w! U2 k$ rmath.isnan(x) 如果x不是数字True,否则返回False
9 f9 y* J6 w, N/ B, ^+ ^0 k- #如果x不是数字True,否则返回False
- v7 ~1 B( X! l, A4 D6 v" D - isnan(x) -> bool$ |5 m" j3 r, H- ?5 y
- Return True if x is a NaN (not a number), and False otherwise.' C7 l; G# h4 k. V0 m: s9 r! ~
- >>> math.isnan(23)
! J% e# s6 P; y3 Y9 K' {/ S - False7 A: g+ z6 ^7 m# m% b" c: u
- >>> math.isnan(0.01)$ t5 K$ a6 N) n0 @
- False
复制代码 7 ?9 a& A6 E) z3 B+ W! T
math.ldexp(x,i) 返回x*(2**i)的值
" _: D% u* r4 A" v, b4 x7 s7 s- W2 }- #返回x*(2**i)的值1 ]0 {$ P8 M5 O( n( W# Y
- ldexp(x, i)$ w. j3 w: e* _- ?: x
- Return x * (2**i).
. [) `" u7 L7 U0 C3 g5 } - >>> math.ldexp(5,5)7 w6 T% }. Z3 I6 B/ f' b
- 160.0* K3 f' R/ a. ^* d" b/ F5 H
- >>> math.ldexp(3,5)
$ i( Z4 U- r3 h! a. l - 96.0
复制代码 5 b) p' j. d c9 v& n& H
math.log10(x) 返回x的以10为底的对数 C3 K+ I' S9 b3 v$ n" Q! f8 l
- #返回x的以10为底的对数- h# _$ C6 c( x+ K9 J2 M- Y
- log10(x)
' k# b1 D% T+ [6 I8 r - Return the base 10 logarithm of x.
- r+ l3 H! m2 c7 x- o5 ?6 N/ a - >>> math.log10(10)3 t: i/ P e. {4 _
- 1.0
6 a$ I" P* o. _3 [0 f9 {8 n" f - >>> math.log10(100)
/ r# S, g' e( A) o7 A" c% n& _ - 2.00 ?4 N( u9 i9 J8 G0 c; t% G- Z$ ^
- #即10的1.3次方的结果为20
! Q/ X/ [- |; f# ~, [4 T# d - >>> math.log10(20)
# T/ O8 E/ O) E - 1.3010299956639813
复制代码
; R" m. k2 v* b, w* imath.log1p(x) 返回x+1的自然对数(基数为e)的值4 M7 O2 z ?1 \! X/ J, B) B
- #返回x+1的自然对数(基数为e)的值' R& y& I8 ^8 K" `% I" w3 c
- log1p(x)
3 x+ L" n, R8 v - Return the natural logarithm of 1+x (base e).
, x. {/ J: g, U( v# Y2 B - The result is computed in a way which is accurate for x near zero., a; K/ K7 D5 d& }5 o
- >>> math.log(10)- Y+ a6 O" K& F: f% t" n7 A& T
- 2.3025850929940462 f- b2 S& x+ M* b& f$ ]' ]
- >>> math.log1p(10)
# A3 P3 e# y# ?9 P& |' N - 2.3978952727983707
& C, c G b2 n. h: U - >>> math.log(11)
7 J$ i1 k# R) F; V% V7 A; C$ ~ - 2.3978952727983707
复制代码
2 O! F1 S4 X5 h( o7 o8 `, |, vmath.log2(x) 返回x的基2对数% F0 x0 u. X* T( @8 {6 d
- #返回x的基2对数1 C) ]1 R2 s, j- \5 Q$ B3 Z
- log2(x)+ P! Q& Q* l$ r0 f- }3 @# F
- Return the base 2 logarithm of x.
0 [$ N, x" a& L* t - >>> math.log2(32)( o a2 M* j6 ~
- 5.0
3 O3 X8 @2 ^- `( A& [; o - >>> math.log2(20)
0 U# v4 p$ V! |6 C - 4.321928094887363; R6 x E: B9 A! w0 f# h+ @
- >>> math.log2(16)
1 j: \2 H3 e( F - 4.0
复制代码
% b* {& @9 j% n% Nmath.modf(x) 返回由x的小数部分和整数部分组成的元组" w3 P0 p7 H2 E! D; q
- #返回由x的小数部分和整数部分组成的元组
8 Y& W( D. i6 o ^8 A% c - modf(x)
: X! I) t& f* N. C, ]0 L - Return the fractional and integer parts of x. Both results carry the sign
6 q* t& ^* G6 V+ L7 M2 V - of x and are floats.
+ U: {, |! \2 C5 I5 \ - >>> math.modf(math.pi)* m1 q* B: K" J% ~7 r+ C5 }
- (0.14159265358979312, 3.0)
: T5 V! w C( z- k7 P1 D; y - >>> math.modf(12.34), @/ y0 Q; _( l x" O
- (0.33999999999999986, 12.0)
复制代码
2 n( h' m9 R" Xmath.sqrt(x) 求x的平方根6 R: P( v0 q2 `* C7 ]: w) @
- #求x的平方根! p# u4 T$ k+ \# _ Q1 K) m
- sqrt(x)- m+ f$ Q$ I6 I0 T
- Return the square root of x.& E( t( F8 v2 ]; j; q) c
- >>> math.sqrt(100)" O5 J, N! J' `
- 10.0
( u) @, ^+ c1 v' O7 H5 q - >>> math.sqrt(16)
3 O9 D. O- C, a# d/ J/ m - 4.0- Z9 B% [/ A/ j- N- N; [9 L
- >>> math.sqrt(20)
% Z; D: q7 n8 _% Q - 4.47213595499958
复制代码 3 B2 C) z" x9 s$ y, i
math.trunc(x) 返回x的整数部分- #返回x的整数部分' l( ]) Q: M' L4 S' {
- trunc(x:Real) -> Integral
1 I0 @- W, t6 g& G: |5 o, a) | - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.- \6 B+ B5 U. K8 T k
- >>> math.trunc(6.789)
% G5 d5 F7 }: F - 60 c& n, y* w, f5 W; ]% [7 e+ \- B
- >>> math.trunc(math.pi)& e% W! t" Z3 H: g) g; ?
- 3
; z, d- c' g A" u L/ U4 e) h - >>> math.trunc(2.567)
' F) {* u: ?2 I2 _7 N$ u4 t - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|