马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) Y2 _8 |6 v5 r【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 D+ P& ], @! B
9 d1 V6 T0 g' T" J/ b$ j
方法1:4 l2 n6 S2 t$ l6 s% q
- >>> import math& h3 M1 a& P& @8 W# P
- >>> math.sqrt(9)
+ @$ u. @- L4 b6 x& _ - 3.0
复制代码 方法2:, c- n: a8 s) p4 ]4 X# g
- >>> from math import sqrt5 r) R! {& g9 P |
- >>> sqrt(9)0 V& s& c) }) G7 H$ a& I& `; w
- 3.0
复制代码 * X" S) {8 y+ K5 C3 a- ?
: w5 ^0 t0 V) k2 h" K9 ]
math.e 表示一个常量. f* J; D7 ^- P2 a& Z/ U. Z
- #表示一个常量
, {6 u1 U& r$ H* i/ B8 h; a0 O - >>> math.e2 V' X1 z6 F& ?7 \0 |: l
- 2.718281828459045
复制代码 5 v+ X$ O" H" b7 X9 P/ ~
math.pi 数字常量,圆周率* q1 P K2 S5 b# h6 h
- #数字常量,圆周率
, U9 [% w$ [- S: I - >>> print(math.pi)7 h- [" z8 o, T7 Z3 e
- 3.141592653589793
复制代码
# N2 P6 g8 A) x4 m0 Z) `+ [math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ w% l+ `: |: ^7 g2 J. `- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
! |3 b4 W! V2 \6 E Y U) O) W - ceil(x)9 v' t9 a& ^4 s1 m1 x! D; a
- Return the ceiling of x as an int.
! J; N1 J @/ b4 N - This is the smallest integral value >= x.
) E" H2 t' ^$ I5 u; ?
3 ]8 L0 h7 w, a+ o- >>> math.ceil(4.01)
p- X0 i8 K, d" Z4 n O7 w& @ - 5% B( y1 s' ^) O d: _7 s% j; r0 C
- >>> math.ceil(4.99)
- B- t4 ]" ^/ I: O# ?) m% ^ - 5. X! [* r9 O0 x$ \
- >>> math.ceil(-3.99)
# \' a2 i- H4 A2 N - -3- o$ R- K U9 u& q
- >>> math.ceil(-3.01)6 ?- k0 _1 R' E, S/ _) z
- -3
复制代码
% y: N5 k; q+ U. r2 Y' k* {math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ I+ |0 S: W- b6 R3 Y2 ], g
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 g1 v N `( q2 X7 h u) S8 Q
- floor(x)
5 m3 m$ U' ~% K4 b. N' V - Return the floor of x as an int.3 h# |% z& T/ d, y* m- \+ G+ ]
- This is the largest integral value <= x.% E+ U+ _3 ~4 W8 ~1 V
- >>> math.floor(4.1)
" W+ o: \( Q" M- i! @ - 46 f6 b; P% N- D& f2 R
- >>> math.floor(4.999)
* ]) p5 P6 u& i3 t - 4
/ X7 S. x' Q3 e/ x0 H: c3 w - >>> math.floor(-4.999)
4 Z+ K' s, s" T4 m+ d j - -5% v; N4 w. O" f6 `3 u* @9 v
- >>> math.floor(-4.01)# O! n. H# w! o* I
- -5
复制代码
5 \% s! K8 h2 z" mmath.pow(x,y) 返回x的y次方,即x**y
! C& Q3 M, r) i- #返回x的y次方,即x**y5 |* T( a* x: B2 z0 e* T
- pow(x, y). R6 L1 l& A9 |2 d8 t
- Return x**y (x to the power of y).
0 N$ o; e$ _* [& v; W9 f0 } - >>> math.pow(3,4)- J" c) w( i: K8 l9 m+ r- v
- 81.0- q1 W) |+ u G6 c" f+ }$ ]
- >>>
+ Q2 J& p! x0 M' b/ A* x% y - >>> math.pow(2,7)
: `' w U" N4 x6 t& G2 Q; ?- ] } - 128.0
复制代码 7 p- C* R5 P; n* p$ g
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' o- j% q# H5 l1 a3 v- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% H |$ P2 M- C5 a
- log(x[, base])
4 c9 ?' d5 E, n; m" p: x8 B8 y - Return the logarithm of x to the given base.
) M0 n! S v, V0 j1 ~8 m5 w - If the base not specified, returns the natural logarithm (base e) of x.
u$ N1 B; z! | - >>> math.log(10)% |5 c% N" ?# g) s
- 2.302585092994046) e5 U7 D3 f, `) L
- >>> math.log(11)7 n4 v9 U L* j# U; o k
- 2.3978952727983707
! V: ]# x% f1 `9 y - >>> math.log(20)
- k1 c" l0 G# s6 z8 V4 |- p - 2.995732273553991
复制代码
, X) J1 |5 u0 T( T5 p1 Vmath.sin(x) 求x(x为弧度)的正弦值
/ o# ^- b9 a) V# N- #求x(x为弧度)的正弦值3 ]$ ~5 l/ D |& U6 Y' @6 B
- sin(x)
: n6 ^4 T" N4 W; r& s T9 c# X - Return the sine of x (measured in radians).
, d( G0 A- W: t6 G- Z* ]" l - >>> math.sin(math.pi/4)
0 u5 R7 h1 P# ~/ p8 ^ - 0.7071067811865475
/ M; T2 K l" n3 c! ?4 D7 N K, ~5 w - >>> math.sin(math.pi/2)7 |& m7 y) m1 J+ n
- 1.0
$ t3 a& g' S) L7 q - >>> math.sin(math.pi/3)
: L: E% s+ Q8 i# u6 d - 0.8660254037844386
复制代码
7 Y7 O3 l: }8 {/ w! X0 @. j4 jmath.cos(x) 求x的余弦,x必须是弧度
, b! T- x: q/ b; e) {- w6 G- #求x的余弦,x必须是弧度
- v0 _3 j$ ~6 h/ o - cos(x)
! F! j- m0 f$ Q" }" v, T- } - Return the cosine of x (measured in radians).
: F q4 B2 ~% ~( U9 u - #math.pi/4表示弧度,转换成角度为45度
; ?( J3 u! K/ \% ]# B ^ - >>> math.cos(math.pi/4)
+ B( Y' e3 ^; n% w- f0 H+ ?& E/ v4 X - 0.70710678118654760 \0 `; Q) p. W" D
- math.pi/3表示弧度,转换成角度为60度
" @: q" @$ C7 O& R: S. r, D - >>> math.cos(math.pi/3)
; d3 Q5 z# g0 |/ H - 0.5000000000000001
; P l" ~) ]8 k: Z: a1 f8 c0 w; r - math.pi/6表示弧度,转换成角度为30度
2 N7 p5 ]0 F$ S8 z" W7 x- z - >>> math.cos(math.pi/6)
4 h3 [0 q9 R7 K0 Z/ A4 h( m4 Z+ T - 0.8660254037844387
复制代码 & S7 O7 V) Q3 g+ n7 B# [
math.tan(x) 返回x(x为弧度)的正切值* o& S( e& M; M) F' F. L% S
- #返回x(x为弧度)的正切值
2 t+ p0 R6 U) |: ^/ Y( _) X0 c - tan(x)8 F. _$ Q, P% Y
- Return the tangent of x (measured in radians).% |- x6 y) \! u+ g* z& @3 _
- >>> math.tan(math.pi/4)
8 L& S+ l( c. R- \3 f6 \ - 0.9999999999999999
& _9 m' y3 d& q# M6 U$ ^9 j - >>> math.tan(math.pi/6)% m3 [3 ^7 P% b0 @" K9 x1 m
- 0.57735026918962572 L: l+ ?% r0 V9 h3 [ M3 L0 I
- >>> math.tan(math.pi/3)
" |9 v$ \ D& B$ \1 M2 F+ b - 1.7320508075688767
复制代码
1 N* N5 G0 ~2 |math.degrees(x) 把x从弧度转换成角度
* X: P4 M N. t" i6 X, R- #把x从弧度转换成角度0 I. l2 U2 {) ?! _" K0 F
- degrees(x)
$ I% Q; ^* e1 y - Convert angle x from radians to degrees.
# u; N9 {# U! C - & E0 P; a" O/ |7 D8 q+ \- g1 N( a- U
- >>> math.degrees(math.pi/4)
3 U' g; [/ e0 T; ]& W - 45.0! F a) [: I3 o) n, I% e: x' h& J" S
- >>> math.degrees(math.pi)
" H, G7 b$ x, [ - 180.0% a( K3 H( P# @; }0 V
- >>> math.degrees(math.pi/6)" B- E0 C* J; o
- 29.999999999999996
Z7 G; Q- B) u/ }; Q - >>> math.degrees(math.pi/3)# V- m; x5 t1 V6 K. |: X7 f4 `
- 59.99999999999999
复制代码 $ C {1 {7 h3 _9 |" j0 g
math.radians(x) 把角度x转换成弧度
; U. ]- n- r: z2 `- #把角度x转换成弧度
! W/ w1 n1 `% n" ?. O - radians(x)
4 ~3 R4 Z/ P3 _ - Convert angle x from degrees to radians.
' [* x% ?% @4 ^. ]. y - >>> math.radians(45) \ B% L# J, l1 F
- 0.7853981633974483
/ n9 {4 B+ }) F* B- ~& j6 h - >>> math.radians(60)
4 Y( ~) ]) ?) e9 H - 1.0471975511965976
复制代码 7 O) b1 \! d& A' d9 o u T
math.copysign(x,y) 把y的正负号加到x前面,可以使用0& s) w! U' W7 O" ?9 N# C
- #把y的正负号加到x前面,可以使用09 \4 b# V; X& E$ s- K( H) c0 o
- copysign(x, y)4 } x" y/ X% m& ?1 m9 W
- Return a float with the magnitude (absolute value) of x but the sign & ` M4 X. H6 a+ b5 m+ _
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) : t( o+ R& m9 Z7 h+ ? E; M
- returns -1.0.+ q3 Z4 |: Y: w/ `0 T, O/ t6 U9 x
- 8 O' }7 @8 E9 J% P' ^ v
- >>> math.copysign(2,3)
* J8 Z; ~0 ~: d, Q$ a6 h0 X - 2.0
6 @& R! \8 k( ~% b( P( q - >>> math.copysign(2,-3)
! p7 O/ d# b8 p6 I7 s - -2.0
2 o* Q. ~3 W7 d' V8 e! g1 `5 z; K - >>> math.copysign(3,8)
6 o) A, j$ [0 m5 a$ N- Z! \ - 3.06 h, ?* P: q, X, o0 d
- >>> math.copysign(3,-8) h9 T& Q+ a# n& _1 y; ]4 z: @
- -3.0
复制代码
+ A$ O9 B. ^! F5 J' H, f/ Xmath.exp(x) 返回math.e,也就是2.71828的x次方0 @0 p$ k! w3 ]" s
- #返回math.e,也就是2.71828的x次方
% q$ y0 a3 L e, y: b# P - exp(x)
T# a. ?$ f1 W5 z* g# m5 m - Return e raised to the power of x.3 d8 O P Y. o4 S3 k5 V
- 7 Q& t0 L M2 r' C! `1 n5 q
- >>> math.exp(1)' x' \7 u. {1 ^4 G6 Y
- 2.718281828459045! m( a$ y% o7 {: S, V7 j
- >>> math.exp(2)8 p; i3 z9 j, H9 _/ X w: S
- 7.38905609893065
' S; {' l: s6 W9 m8 p6 g8 Q - >>> math.exp(3)) Y& k# P% P, g: g( f% K* u2 F
- 20.085536923187668
复制代码
( `" T1 e3 z% q% Hmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1& \+ I/ P4 Q# H$ d) [ x1 q* S
- #返回math.e的x(其值为2.71828)次方的值减1% O3 K7 P: Y. o: T' M" J% ?
- expm1(x)+ K! ]2 ~1 M7 L& _) u' j. Z
- Return exp(x)-1.: D! ] r* x5 c
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.* ]4 {8 K5 d- G2 \" d2 X
! s$ G+ o3 A/ n! A; A: N- >>> math.expm1(1)
o9 p) f! O. x, `- T0 d - 1.718281828459045
8 ?+ H! N0 Z6 t9 ^+ I: ^! ~/ a - >>> math.expm1(2)8 h+ h8 e/ }. W8 x
- 6.389056098930656 n1 Y$ j+ H& ~# |, Y. c# m% |
- >>> math.expm1(3)$ e U* A }' F) ?6 u# V
- 19.085536923187668
复制代码 ! t1 M: q- @ E o1 t E
math.fabs(x) 返回x的绝对值
/ ~ b& g# R; ^1 x- #返回x的绝对值
a1 E6 P z& g' h! P - fabs(x)
7 A. h, }, W+ y7 M7 g: y - Return the absolute value of the float x.
% v. `4 v# W' y+ K
6 y/ V- L6 _. ]* V- p) q3 |7 f- >>> math.fabs(-0.003); I$ _- i7 i; h7 p6 @ G6 b, o
- 0.003- a" ^2 l. C9 w7 P
- >>> math.fabs(-110)
5 E7 m- s* y* T$ R* X% F - 110.05 Y1 S/ f; A4 D7 q- r
- >>> math.fabs(100)* C& n+ f* x" z* W, h' Q
- 100.0
复制代码 , e) D; D: M' m0 \
math.factorial(x) 取x的阶乘的值9 U1 ~' V9 j% [0 `$ n4 d( A4 e% m
- #取x的阶乘的值- G) H7 F* U; ?$ W/ y6 h
- factorial(x) -> Integral
8 Y4 m6 o, s! v( i3 i4 i8 N1 `$ e - Find x!. Raise a ValueError if x is negative or non-integral./ r! {* L( b- ]! U- A/ r: X0 s
- >>> math.factorial(1)- p" ^5 ]9 t# v; v
- 12 Q' O, a- A3 N3 \. R4 |
- >>> math.factorial(2)6 E( a) |+ a" m0 J& d0 t0 C
- 20 ~5 f6 c+ I$ ?& z: `: ~
- >>> math.factorial(3)& U; D1 {5 h. t4 O2 Z$ m
- 6
5 F6 O* f% v% H: X# ~) \, }. f ?* B - >>> math.factorial(5)/ H. W" Y5 t k. X: a* q. Y& q9 M/ s
- 120" H' K! G# ]# E
- >>> math.factorial(10)
& E" S" L( l% x - 3628800
复制代码
% w+ W. z8 N+ s, A0 R4 Y0 Fmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数" K5 \5 Y1 }) t" e, Y7 P
- #得到x/y的余数,其值是一个浮点数# K/ V+ v/ U. X3 ~7 \) H1 }0 [
- fmod(x, y)+ E. |, e: |- X( @5 k h
- Return fmod(x, y), according to platform C. x % y may differ.. \1 N9 u2 |8 A6 H: A2 z# O
- >>> math.fmod(20,3)
$ Q7 `. a4 M' R6 J' n# L- A - 2.07 n9 D2 s, ]( ]5 b6 a4 i0 o0 i
- >>> math.fmod(20,7)* Y$ e& h7 h% W6 S4 h
- 6.0
复制代码 * l% {+ f; d- |0 Z
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围' `& u9 i X' ?$ C. P8 n* y
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. N/ X/ W; [! V) {& ~0 \ - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' m' _4 Y' r3 ?" B+ A$ }
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和18 T: Z. `! A/ x& \$ ~& R8 {
- frexp(x)
% m( l& T( U) s3 |' K5 u5 X - Return the mantissa and exponent of x, as pair (m, e).
3 a/ V8 A/ b' K1 u+ U - m is a float and e is an int, such that x = m * 2.**e.- N6 ~6 N& h; q' i
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.' n# S& a4 q j
- >>> math.frexp(10)0 m4 I1 v& ^7 r
- (0.625, 4)
0 @/ `- e) Q5 E9 F9 ]2 L - >>> math.frexp(75)
# i2 S! S2 c$ D( E$ K l- ^ - (0.5859375, 7)2 o* |6 _; w! S9 Y3 M4 M5 F. ]
- >>> math.frexp(-40)& s8 b2 D' W1 Z; T
- (-0.625, 6)
# o2 j# y+ Q9 W - >>> math.frexp(-100)9 Q4 A9 T3 I: z0 E
- (-0.78125, 7)$ X6 q2 n7 t$ N6 T( A
- >>> math.frexp(100)
& d3 C% Q ~# X/ T8 g# M: G9 c$ J - (0.78125, 7)
复制代码 , Q' `9 k2 K9 W4 o- U1 O
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 z& Y! U' }% u$ ?, c6 e7 P% J1 C. a- #对迭代器里的每个元素进行求和操作. I' ? {0 v$ d) Q& S
- fsum(iterable): n& h! L2 y3 G. F7 A" i3 U; n
- Return an accurate floating point sum of values in the iterable.
# X, W U, a9 O2 Z2 c, \+ e& E - Assumes IEEE-754 floating point arithmetic.
3 W0 Y; ^ V Z3 y - >>> math.fsum([1,2,3,4])+ S, S3 S* K( I* u
- 10.0
0 n. ] U f$ V, K$ }1 b$ H0 J - >>> math.fsum((1,2,3,4))
' R; L* y9 ?2 G1 M+ T5 [ - 10.0* l# H5 V" _, f* }5 w' ~% p8 s9 R# i6 Z
- >>> math.fsum((-1,-2,-3,-4))
( q/ V$ e% d; z - -10.0
$ b* f: s1 n5 ]. {' x* r# R: N, |" w - >>> math.fsum([-1,-2,-3,-4])
2 s0 X! @( } b) o- b" D% G - -10.0
复制代码 3 ^8 T- k' d9 `) s! A
math.gcd(x,y) 返回x和y的最大公约数
& |" T% d( ^" D4 M4 A) C- #返回x和y的最大公约数4 z U& t* U1 a7 x
- gcd(x, y) -> int5 d- N: i+ j5 o& ]; G
- greatest common divisor of x and y
/ h3 P2 l! Q- W$ J - >>> math.gcd(8,6)- F3 U: m4 t' r0 c7 j
- 2
) q+ q5 a. n9 F& v - >>> math.gcd(40,20)5 E6 f7 S( g) b( Z" q* T/ e
- 205 o7 G" ?# E2 m
- >>> math.gcd(8,12)
& K8 p; B' Q& |1 N& _1 u - 4
复制代码
7 B+ N5 Q9 ~, K1 Q( d. f8 Q$ \math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
9 N: R* J. E4 ]4 T P' V8 U- #得到(x**2+y**2),平方的值% j/ i Q, j" [1 x
- hypot(x, y)' F5 w T) }0 y1 ^8 w" M; i
- Return the Euclidean distance, sqrt(x*x + y*y).
! e% t) u& f; q, M( S# d1 } - >>> math.hypot(3,4)8 J: ?# |2 a$ _/ j" W/ |$ ^
- 5.0/ i* l/ `2 X3 E# o: O) K" `7 J
- >>> math.hypot(6,8)" O% F* A+ `0 S: k- Z" e
- 10.0
复制代码
) M2 C+ H Z7 d& c$ cmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False. w( c3 o/ n; B# w8 D
- #如果x是不是无穷大的数字,则返回True,否则返回False+ i1 z- T% ?/ }" G- H5 _7 g% P3 r
- isfinite(x) -> bool
' O Z( q9 [4 C* _5 } - Return True if x is neither an infinity nor a NaN, and False otherwise.
" a |! y* C1 c' l0 Q( R/ w - >>> math.isfinite(100)* Y+ V4 s6 H7 Q! L7 Z5 Y
- True
) e* ?5 F6 L& d8 {9 @2 q2 P - >>> math.isfinite(0)* e$ d! r5 D, O4 y6 f
- True
7 Z: j! f( N3 E5 h% k* b - >>> math.isfinite(0.1)" W. {1 u9 P# }1 ~/ i
- True
+ d% K! ]# h( V- f& U - >>> math.isfinite("a")2 [# g, V# w. C: B8 n
- >>> math.isfinite(0.0001)) Q" _6 x0 o5 g1 |8 M2 d0 O
- True
复制代码 " H f5 ^8 ^# C3 G% k" j. p' X" H
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False/ j- D! _$ Z5 v1 Q5 y
- #如果x是正无穷大或负无穷大,则返回True,否则返回False; K4 W. o% Q$ Y& m7 l
- isinf(x) -> bool
/ M0 H8 B8 j% l4 m5 t7 z - Return True if x is a positive or negative infinity, and False otherwise.# ?7 u& Y4 _9 J; f8 H* c
- >>> math.isinf(234)# q& t& {6 i* i4 j# C
- False
% ?7 ^- V- _- G/ a0 L Q$ m - >>> math.isinf(0.1), t* L O h5 V; y; r( Y& c
- False
复制代码 % d; q* B1 a% {$ e$ z$ r0 q0 M
math.isnan(x) 如果x不是数字True,否则返回False
3 a4 ~# L$ n0 l. O; T% o4 U- #如果x不是数字True,否则返回False1 w! G% H$ L8 h. w4 z Y# b, Q7 M" n
- isnan(x) -> bool# ]7 f# K; p2 h q- w1 `+ h" k
- Return True if x is a NaN (not a number), and False otherwise." k4 H- t3 g5 O: W7 s4 p" m" J
- >>> math.isnan(23)' S* @0 b: x) a& K8 {% w
- False2 z t7 z k: W
- >>> math.isnan(0.01)* q- v& F2 H; ]
- False
复制代码
- G0 _ v& E6 B2 @) \math.ldexp(x,i) 返回x*(2**i)的值
2 N, H+ Q) V- w9 y- #返回x*(2**i)的值
! G! t' ?. L/ W% u6 f - ldexp(x, i)) b+ Y1 i! y9 T( }- `
- Return x * (2**i).
( c& q7 L# f8 z0 B: J( O! K - >>> math.ldexp(5,5)
8 `- b, j. e5 g I9 ? - 160.05 U5 _4 N/ `8 L9 r* |
- >>> math.ldexp(3,5)
7 z v) x+ Y5 M f# V* \ - 96.0
复制代码 1 r% K. s. X" U% r4 ]7 E& [
math.log10(x) 返回x的以10为底的对数+ e; J" T% G$ ~7 c
- #返回x的以10为底的对数0 g0 `+ {! n% M# [/ {8 Z( n
- log10(x)
5 z' s Q( S' G( J& p# r9 B - Return the base 10 logarithm of x.
, ?; p9 i9 G8 Y - >>> math.log10(10); H/ f ?; f6 `3 x
- 1.0' l1 j4 I% t+ L8 A/ D3 H
- >>> math.log10(100)
/ c, h4 ^ K6 V$ [ - 2.0
+ E8 J! l) `. B9 |: i+ M - #即10的1.3次方的结果为20
5 B, h2 e, e# z! ^% J3 Z& U1 w - >>> math.log10(20)
& d* ~' L0 N7 A% y8 k" C0 @ - 1.3010299956639813
复制代码
$ z: r6 l, t/ \math.log1p(x) 返回x+1的自然对数(基数为e)的值0 s. ?" E& e% g; R. m; G' `
- #返回x+1的自然对数(基数为e)的值" Q2 D$ R$ y: V k& b
- log1p(x)
$ R8 E. K8 h& f% T4 x* y - Return the natural logarithm of 1+x (base e)., A) W* x2 x$ y2 V
- The result is computed in a way which is accurate for x near zero.: a, Y9 E- R9 Q, m3 G. h' m: y
- >>> math.log(10)
3 H4 B! K$ Y8 E7 o+ q3 n* v1 J$ s9 {# I - 2.302585092994046, c& R$ O q B+ @) K Q# ^7 {6 Q
- >>> math.log1p(10)
5 j0 z3 Y v4 [# m& N - 2.3978952727983707
) E$ b b" ?# Y X& e - >>> math.log(11)7 K3 q% A2 u( U- r' `* g
- 2.3978952727983707
复制代码
4 o0 D: g! |( r) k9 xmath.log2(x) 返回x的基2对数: ]1 d' Z/ j0 e) T1 ]3 w# v# g# ~
- #返回x的基2对数
- O; ^1 E' j1 M5 W1 E- i - log2(x)
9 @( A* f2 z1 C. V1 g - Return the base 2 logarithm of x.
. y5 Q0 s' p' F- {% w0 G - >>> math.log2(32)
/ S& [2 y4 @& ]8 s( s& v0 M" b - 5.0
! w" @; j6 ~5 ^ - >>> math.log2(20)4 n4 M/ X8 l. d0 h! j" \
- 4.321928094887363
. |! h4 u) L5 `! z1 c. \7 } - >>> math.log2(16)
K% N% c! ~: ^' n - 4.0
复制代码
9 B2 V+ e- H( q, |2 Umath.modf(x) 返回由x的小数部分和整数部分组成的元组
$ w: Q7 C- T9 w! N. @5 h: Q; f3 g) U( C5 q- #返回由x的小数部分和整数部分组成的元组
9 G4 a& K1 L6 V& R' e - modf(x)3 R! z! u: T6 h7 n0 d1 m
- Return the fractional and integer parts of x. Both results carry the sign7 d L: S; V2 a
- of x and are floats.
: A& b3 W" P# o2 }, S. U - >>> math.modf(math.pi)$ c' a* E% \5 b7 ^
- (0.14159265358979312, 3.0)! j# z% @+ j0 A4 O; t( C- ]3 h2 k
- >>> math.modf(12.34)* r+ G5 R# ]' s& A+ ~& V
- (0.33999999999999986, 12.0)
复制代码 4 J# F% ~. C1 K9 v \* u0 K
math.sqrt(x) 求x的平方根
8 t% P2 H3 g4 s R2 g+ Z- #求x的平方根/ X: k9 R3 F& J8 t1 p0 {& A
- sqrt(x)" ^2 `1 E. G# z& s) g
- Return the square root of x.
$ g6 u! `: M J, _. ^ - >>> math.sqrt(100). v2 [7 Z- p" D, o- a
- 10.0
* a$ h" ^ W$ t! D! Q M7 i - >>> math.sqrt(16)
( l2 L. x$ H6 \" P; l3 ^) n; P - 4.0
# K1 r; o1 ^! }; i - >>> math.sqrt(20); W4 K& D5 L7 u! t
- 4.47213595499958
复制代码
6 ~% L3 E0 |0 B8 W3 Smath.trunc(x) 返回x的整数部分- #返回x的整数部分4 t$ Q @& k( G6 D" X
- trunc(x:Real) -> Integral
) z+ C( C2 d4 z! r/ } - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.5 g3 ?: I) i: ^/ R, N4 f' D5 G
- >>> math.trunc(6.789)4 f7 ?) {8 g* `. R7 l( p/ t2 u
- 6. Y. A' Z. T0 u" s
- >>> math.trunc(math.pi)5 q& @8 P9 Y5 R/ h$ W+ s; D
- 3
7 ^& k, ?; p* i# L/ i - >>> math.trunc(2.567)0 }, ] w: C2 i& j5 q
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|