马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 _5 J3 g" n. S! m% w2 u5 y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
) f3 ~- i+ p3 y, d" k
3 W( W$ q D4 J% e方法1:
& b, d! ?2 s- i3 z3 A( Z- >>> import math0 ?$ j }" p) Z" z
- >>> math.sqrt(9)
3 z" X4 s8 b0 M" x8 [3 l - 3.0
复制代码 方法2:
* \6 X4 M% L, A% Y, ~- >>> from math import sqrt
0 N7 @# W t8 P) x# F7 V - >>> sqrt(9)4 p& A/ Z6 Q' ^8 g- N
- 3.0
复制代码
- x2 u" J' o' c- u& |0 k ! h/ z: T2 K% P7 |
math.e 表示一个常量
! w* G5 |( g s5 E3 e- #表示一个常量
/ X! j' p5 A6 n1 W5 W' i% T - >>> math.e5 X6 J- n1 q" A( \2 e$ t' {
- 2.718281828459045
复制代码
) @8 I. O) Q3 X3 l9 G" X' d, B- r, l/ jmath.pi 数字常量,圆周率# \! O" Y8 U- [) c+ H3 k
- #数字常量,圆周率
0 @7 r# Z! Q) N8 N$ M3 Q- M' `. i - >>> print(math.pi)! K6 f: D8 ~' q- \4 o
- 3.141592653589793
复制代码
0 ^5 Y0 Z t' Imath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x- ^& Z( E# p2 b0 C" k+ ~
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x! D" w4 U8 b C
- ceil(x)
8 P1 O. x8 G/ l3 o, z: c - Return the ceiling of x as an int.( K, I% i3 a& G
- This is the smallest integral value >= x.
0 y; I0 r5 E% G
+ ~- P0 Q1 t& u- >>> math.ceil(4.01)
. W% c5 C, D( J4 Q& a. ~- H - 5$ f. z' U% B% u" V
- >>> math.ceil(4.99)( `7 |# C2 d' _2 f7 A
- 5
! y, l2 t9 m/ y5 D5 h - >>> math.ceil(-3.99)
$ {) k, m' v1 a4 N/ P# p - -3
$ D& ~" S) k/ ] - >>> math.ceil(-3.01)
& o4 |1 `0 @* A - -3
复制代码 . M9 Z6 r+ l H- l, Y- d: w8 i; u
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 u% \ \7 _* a3 H: S9 a/ @' Q- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 R8 d" f; ~' ?( i+ N3 x - floor(x)& }" e( {% q6 p. v1 Z' L5 v
- Return the floor of x as an int.( L! h, v$ o; {' ~" s
- This is the largest integral value <= x.
- r* N+ i8 `2 G) t9 t$ S$ K - >>> math.floor(4.1)
7 e$ O% }5 u$ {# Y; ] - 47 `7 M @/ D! Q
- >>> math.floor(4.999)
/ `2 v/ u' j$ { - 4$ y- x7 \+ z: m8 v) i w
- >>> math.floor(-4.999)
4 u; M- U# E# }: ^9 W4 M4 j - -5# {1 @! y0 |0 B+ w8 }2 G
- >>> math.floor(-4.01)
) G9 L; F3 d; @0 V# K - -5
复制代码
0 F `2 z% l; q, emath.pow(x,y) 返回x的y次方,即x**y+ ?# W7 D/ B. ~
- #返回x的y次方,即x**y
/ g/ N5 S# q/ c! M% K) R2 H - pow(x, y)
; z. T$ b+ K. X9 U - Return x**y (x to the power of y).
$ G! m+ n/ D0 ?% T7 l) e; j - >>> math.pow(3,4)% m0 Q+ y" G( E' {) E' S' v7 O$ Q
- 81.0
% A$ _& D; Q$ R) I# | j - >>>
- T' ^- ~- i3 j5 j: @0 }. Q - >>> math.pow(2,7)
- p% I8 P9 X2 r$ G, ` - 128.0
复制代码
* l p4 J. s* r7 amath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- J* N! g6 f( @
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
) n* Y- E7 o6 m0 c* o - log(x[, base])( p4 R2 ?; `3 P
- Return the logarithm of x to the given base.4 h& Y% ?+ o7 c5 r* D# X! c4 l- d
- If the base not specified, returns the natural logarithm (base e) of x.
a9 p/ T. ^/ _* v$ |+ J - >>> math.log(10)4 U7 l) I" M1 F% _
- 2.302585092994046# g7 n/ D$ [0 ~; B, C) b; h# ^
- >>> math.log(11)1 p5 ]; J9 T# p* J' q. H# g" _
- 2.3978952727983707
* i/ {% b$ e z( M5 a i( K0 e) v - >>> math.log(20)
1 z' \7 C8 a* i - 2.995732273553991
复制代码 + v$ {; x7 A H( E
math.sin(x) 求x(x为弧度)的正弦值* A( k7 |8 [/ f( W8 i5 Z4 s
- #求x(x为弧度)的正弦值
( R9 m$ h& ^! t. k) C7 Q - sin(x)
; a% T. X4 L) n% J, G% J - Return the sine of x (measured in radians).3 o7 M* Z+ F6 Y- ^
- >>> math.sin(math.pi/4)
# o9 x! P( u- Z/ x- R- i; F - 0.7071067811865475
0 ]' _9 ]9 \: V- \5 A% k$ e - >>> math.sin(math.pi/2)
' f" A0 `" p% N' a K - 1.0$ K" Y& S- l: W) |+ [; A# M
- >>> math.sin(math.pi/3)
7 |( t, F+ m! I - 0.8660254037844386
复制代码
# Q% Z0 P. o9 Z4 Q, } ~$ Emath.cos(x) 求x的余弦,x必须是弧度- j: i! H3 S) d9 [7 V
- #求x的余弦,x必须是弧度, {, E( x3 H6 N) L
- cos(x)
7 w6 f' P' U; f0 q s* X9 s - Return the cosine of x (measured in radians).
1 U5 J1 ^. l9 w S. ~ - #math.pi/4表示弧度,转换成角度为45度
" g/ F6 A. a- {- I$ r6 h - >>> math.cos(math.pi/4) X8 U+ v5 b- U9 j) R# Z
- 0.7071067811865476: N; ]1 ^8 J* l" X8 ~' q; g0 t
- math.pi/3表示弧度,转换成角度为60度$ x* C6 c- k: H, u3 y) }, v) c
- >>> math.cos(math.pi/3), W2 P* u2 {; v6 m# }
- 0.5000000000000001$ e1 v( c* s, C2 L* {' F7 j0 z
- math.pi/6表示弧度,转换成角度为30度
5 N) e. \# N1 N - >>> math.cos(math.pi/6)
F3 X2 v& M$ f* V! N' b+ K - 0.8660254037844387
复制代码 / u, N& ]) U9 g9 `
math.tan(x) 返回x(x为弧度)的正切值 g. a, Y" ?1 i3 H# N
- #返回x(x为弧度)的正切值
( C7 U3 \' }6 W$ N! m8 f( M! }/ u - tan(x)" g6 A% T# A+ ^, G9 \+ ?
- Return the tangent of x (measured in radians).+ u. ~, G' x/ @& b
- >>> math.tan(math.pi/4)
9 S: v; }, S3 y; w. ?) U - 0.99999999999999991 Y( R' x* c2 m! h, _; p \
- >>> math.tan(math.pi/6)0 j5 b: N1 ^3 m# @1 H% b, X; V
- 0.5773502691896257
* s" Z y8 O! v8 `# T1 q - >>> math.tan(math.pi/3)
( ]6 @& {* @$ b - 1.7320508075688767
复制代码 2 @! z. A2 E, r0 ~2 ]' X3 U: E
math.degrees(x) 把x从弧度转换成角度6 A- w; \5 {3 o2 N1 q5 f
- #把x从弧度转换成角度6 n: V3 Z6 ] Z6 m p9 y2 D/ y
- degrees(x)) f3 _% \4 R/ o U: g
- Convert angle x from radians to degrees.
3 A4 O. Q/ D i9 u. J& E9 ~' n1 T
7 t. E0 ]6 I+ v9 ^8 J: k [" {; U- >>> math.degrees(math.pi/4)
* z9 i9 f8 e1 t8 |; E - 45.0
0 B) O) Y# \5 i. P5 C - >>> math.degrees(math.pi)
1 u/ q( b# v+ Y - 180.0. K# y- K% C" h Y
- >>> math.degrees(math.pi/6)
" z" |+ o( F2 O. d - 29.999999999999996
* O; Y! x6 y8 o9 M - >>> math.degrees(math.pi/3)5 ]7 @7 c" c$ j& a( F0 y/ y6 g
- 59.99999999999999
复制代码
1 a+ s5 M5 w' M2 Wmath.radians(x) 把角度x转换成弧度
* B) f7 i) @+ o6 B- #把角度x转换成弧度. @& W, Y5 n( \+ @% b0 d
- radians(x)& T% v& {; }4 u/ t$ p) P
- Convert angle x from degrees to radians.
$ Y. \* ?- w: l6 r. c; k- c - >>> math.radians(45)$ T/ P$ M( l- c! [. \/ E9 I
- 0.7853981633974483
7 g- D) h& i! N2 f6 Z4 C4 s - >>> math.radians(60)
o1 }% G% N d! Z/ N ~; V - 1.0471975511965976
复制代码 $ `! T( Q* D, B; i3 }' |' D" t
math.copysign(x,y) 把y的正负号加到x前面,可以使用00 ]) \; K5 O) }
- #把y的正负号加到x前面,可以使用01 Q& ^! }/ \0 E' \8 ^( J
- copysign(x, y)
3 n! _. v# U# K, m9 T3 v5 Y - Return a float with the magnitude (absolute value) of x but the sign
2 L9 h: I6 b" j, r2 ` - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 h) C3 @6 n; W5 l, A( l3 ?/ a
- returns -1.0.. i* d {3 |& p
- # i$ {) ?, u/ p4 v- k/ E3 T( |5 c3 \
- >>> math.copysign(2,3)
# ?) u- B' t* V3 P* b - 2.0
: q# [( y5 d5 {' | - >>> math.copysign(2,-3)
& Q; x* V0 Z" l5 y8 p! ` - -2.05 F+ r8 p) x; {5 l
- >>> math.copysign(3,8)
6 ]9 \1 W0 h% k3 m) B: B/ ~$ |: X - 3.0/ L# J4 B( Y+ P1 m& d& c
- >>> math.copysign(3,-8)0 P! z5 v" z' _/ L% X: ]/ _8 m: Z# k
- -3.0
复制代码 : f k; j2 c! W: `3 o8 S8 Z6 k, b
math.exp(x) 返回math.e,也就是2.71828的x次方- B6 c) m3 O. g% H+ V0 A
- #返回math.e,也就是2.71828的x次方7 C3 U5 C2 i/ N1 w9 }( `% e
- exp(x)0 i: N9 y' J( D* W4 g
- Return e raised to the power of x.6 ]! ?, d' j7 ~: h+ M
- 5 Y3 i; h1 {- a! K
- >>> math.exp(1)8 r7 A) d( n# X9 `
- 2.718281828459045
0 A. j9 B+ `9 W* K$ C s) e. H - >>> math.exp(2)
9 Z0 b* t, r( r% J( E! X - 7.38905609893065* a4 B' L( v1 V: @
- >>> math.exp(3); k* a* N3 ~5 V& e8 {( Q4 Z1 W5 A* }
- 20.085536923187668
复制代码
% S1 R7 T) g( Y- Kmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
( m* c4 P+ i3 k- S- #返回math.e的x(其值为2.71828)次方的值减1 s4 Q' i/ w1 r8 {7 v
- expm1(x)# [% K2 ^: t" T4 l
- Return exp(x)-1.
1 ]6 @5 L8 g, t - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 a9 r+ O1 `. |! ]) f+ G
9 s/ }: \# T# h3 z6 h- >>> math.expm1(1)
3 v6 w; Z' g) M1 G; g" E* H - 1.718281828459045- h$ b9 h* K( j# k) B @
- >>> math.expm1(2)
; G/ E: ^$ b ~2 Y' ? - 6.38905609893065
1 n3 `+ y7 o; x: t% I* A; v5 c - >>> math.expm1(3)2 z- }" A% b- H7 N0 {8 e" V$ ^
- 19.085536923187668
复制代码 ( }" S7 `% Y7 B0 j) Q
math.fabs(x) 返回x的绝对值
& i1 @7 r" R3 e7 w- #返回x的绝对值! Q* @5 n+ k. X- ?8 o7 n
- fabs(x)
6 g& W1 n! c7 Q% I; P- |* t - Return the absolute value of the float x.
3 S, O8 \3 ]) j* c6 f
, \/ K+ t, z& E+ K- >>> math.fabs(-0.003)& G" R R4 q; _0 k3 ]6 Y
- 0.003
/ ]+ N P* k1 v8 \3 t - >>> math.fabs(-110)
: }) m3 w- N4 v; [) v9 |8 o7 H - 110.0 o: Y' m. d( x
- >>> math.fabs(100)
, S1 @( T3 |$ ^" t( S& w1 _' Q - 100.0
复制代码 9 N& V) @0 h8 W/ y+ ~
math.factorial(x) 取x的阶乘的值
4 C) ?. R- n; J2 F5 m8 M. Y- #取x的阶乘的值
/ D j1 ]6 a$ \& e& g# ^! u+ } - factorial(x) -> Integral+ d! z; t: P9 J* o/ |8 H) i( W
- Find x!. Raise a ValueError if x is negative or non-integral.
, w+ A# g: M0 W, x. y - >>> math.factorial(1)
5 k. \. P- X( E0 p+ J, q - 1
9 [* }+ O8 |+ @ - >>> math.factorial(2)$ _+ ]: {: _: e0 B' @! e
- 2
* m3 Z0 b3 d/ i4 m! D - >>> math.factorial(3)4 z5 r9 ?6 A+ r) @
- 6
' ]4 X, \+ e5 b% O" E1 `* @ - >>> math.factorial(5)
" b: ^, G6 L( U' M# N( A D - 120
& `8 @' G5 ~: R3 G/ Z; F2 C6 e - >>> math.factorial(10)- G+ k( U/ w9 ?1 O5 l
- 3628800
复制代码 ) d8 q7 M4 E9 J6 [) i8 G
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
" ~' z- o b8 o8 U4 c) z- #得到x/y的余数,其值是一个浮点数, i( N: o) G7 S, @9 Z9 W9 w! j
- fmod(x, y)8 D" T: {" {6 V Z
- Return fmod(x, y), according to platform C. x % y may differ.
2 Y: ? x- f( j. \0 M - >>> math.fmod(20,3)
0 x$ }# i+ T: a6 C - 2.0( G8 ^# }( N! f9 l
- >>> math.fmod(20,7)
( |$ q9 q$ W" o; `% t1 j9 i4 y$ L0 t1 E - 6.0
复制代码
0 d8 F: N3 a/ H( Umath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 A: T4 \, ]7 v$ R5 W# V! w! N
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
* `8 h: N) {7 X: Z' Q - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
A! Y+ u8 _# v6 H1 m - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 d ?; h* O1 ^5 G$ M% R" a7 O0 n
- frexp(x)
1 z( ?) r8 X0 k; }2 V) g8 t - Return the mantissa and exponent of x, as pair (m, e).
, ]. c* N, l9 S) h) y" g; g - m is a float and e is an int, such that x = m * 2.**e.$ ]5 D) _- M9 z: v) g- C
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" E$ i% B6 z8 z - >>> math.frexp(10)% d \" ]8 L; m) o3 _% k4 R
- (0.625, 4)
2 r% c+ w; ^* k/ S" N$ X; f - >>> math.frexp(75)
4 V: a+ _+ {- w1 ` - (0.5859375, 7)0 ? _' p) h+ q( Y( S* ~* X! A9 ]
- >>> math.frexp(-40)
' M' ~; W) m0 x8 w! d3 t - (-0.625, 6); Y9 _' S f' M2 E
- >>> math.frexp(-100)$ e; L; o% T) a& u
- (-0.78125, 7)
; k# {2 B/ d; w0 { - >>> math.frexp(100)
; D4 F# _; h" d- @ - (0.78125, 7)
复制代码
3 e# n' b5 o) _8 \math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
; r/ }: r: F) _- }! l6 ]- #对迭代器里的每个元素进行求和操作: @2 ^; L7 r; t2 \/ R
- fsum(iterable)
+ ?) p6 N% ~2 ?( `2 m+ P - Return an accurate floating point sum of values in the iterable.
( P. k, u- l; J: ?2 |$ U( S6 r8 L - Assumes IEEE-754 floating point arithmetic." A2 |; Q+ c; @: r" N7 H: _. j
- >>> math.fsum([1,2,3,4])1 |. j+ b& G+ j! |5 `. Z
- 10.0
5 B: Z; p/ A: L, T - >>> math.fsum((1,2,3,4))6 X I/ w/ G- M! L$ B6 {9 w7 {) z; y) m) d
- 10.0
9 X& q0 c8 Y* P4 g: G3 N4 g+ d) _1 g - >>> math.fsum((-1,-2,-3,-4))" o, [+ \1 f/ W* S
- -10.03 f/ n8 B( n# e( |
- >>> math.fsum([-1,-2,-3,-4])
1 S' D- ^9 p; |6 W8 t: h - -10.0
复制代码
* `, x0 }0 E* s& smath.gcd(x,y) 返回x和y的最大公约数
9 B, O0 k; f# F" G- }( i- #返回x和y的最大公约数
! N$ B4 b' Z3 b1 _* \ - gcd(x, y) -> int# ~ u; O* U8 h; \& a. c" O
- greatest common divisor of x and y7 }, [, h$ b- O N) [8 R8 m; [
- >>> math.gcd(8,6)$ ~/ N. n! ?7 _4 A. u
- 2) I# b9 P- [- |9 |6 H
- >>> math.gcd(40,20)! q1 q! F3 ~0 |( n
- 20
6 M1 l6 G/ N7 k6 I& p. a- v - >>> math.gcd(8,12)
, N+ R: Y% x- Z# Y1 K3 B8 t3 u - 4
复制代码
2 p/ c) L: X, k; [& r* Kmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False. U3 B5 U# h$ R0 t( e. s/ y8 ^
- #得到(x**2+y**2),平方的值6 F7 ^. f4 k8 W7 C
- hypot(x, y)
\4 J" c w% u/ ^* m - Return the Euclidean distance, sqrt(x*x + y*y).
& V8 W9 m: T" a- t! |1 V - >>> math.hypot(3,4)
8 w$ L( r* Q6 d- p! ] - 5.06 J2 Z. U8 G0 C" A6 v; ]& _
- >>> math.hypot(6,8)
7 A+ ^. b/ E( J6 \# X+ } Q R3 Q - 10.0
复制代码 * W. D9 `- x/ Y" Y# f8 {7 o' S% R! h
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False: z& v& K1 Z' }
- #如果x是不是无穷大的数字,则返回True,否则返回False
. T" u p4 ^1 g/ e& E; Z - isfinite(x) -> bool6 l& f) Q n9 \- B; }
- Return True if x is neither an infinity nor a NaN, and False otherwise.% N# ?% T0 ]1 K/ Y' A# P
- >>> math.isfinite(100)
# m% D& @9 e3 h/ K3 C9 X - True
' a/ j: \2 a e" [3 W2 N0 R+ u - >>> math.isfinite(0)% b, _7 u# R5 ]+ I7 B
- True
* f/ t/ Q! @# S- e# d3 ^! Y6 N - >>> math.isfinite(0.1)5 R( d' y# [1 l# m3 F! K6 X5 o
- True
. }7 z0 q# O+ i1 x1 E - >>> math.isfinite("a")
, e g3 Y: r/ ?1 M) q& C3 ^ Z% y - >>> math.isfinite(0.0001)
3 j8 T2 W# \+ B - True
复制代码 2 S s0 w5 {. v) m( Z) w
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False0 S$ d. w3 @! y& a5 a
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
" [& y: Y$ z! F- C' l" o9 D1 n. r - isinf(x) -> bool% @ i; i/ p5 y4 W7 o9 s; s
- Return True if x is a positive or negative infinity, and False otherwise.
: K6 D& j$ i4 Q8 }- p/ p: r - >>> math.isinf(234)3 H5 {# x) h- @6 W7 U2 f% [8 a2 i
- False
) V2 r' h: l0 w! v: \4 N - >>> math.isinf(0.1)
" m! \% n7 v9 p2 B$ W2 @5 l - False
复制代码 ! L }' v1 h- s* Q) d9 e$ K
math.isnan(x) 如果x不是数字True,否则返回False
4 {4 g6 D* Y+ q5 U2 ~- #如果x不是数字True,否则返回False; P6 f. ]3 D) U O6 i
- isnan(x) -> bool5 f+ R' W, u2 F6 U+ v2 B
- Return True if x is a NaN (not a number), and False otherwise.
+ q# R( q1 ^6 o6 x- h - >>> math.isnan(23); i- b% [5 m* x8 A' t/ f
- False4 s! O; q% o% M2 P
- >>> math.isnan(0.01)
$ r( f& e% i" ]0 t ]9 R5 ] - False
复制代码
4 C# _4 u: u# `# V; y& ~math.ldexp(x,i) 返回x*(2**i)的值/ J, P, K: |8 d, }+ y! |
- #返回x*(2**i)的值
7 q, `& `9 ^ b& `' j% X1 r# S& D i - ldexp(x, i)
% c* h6 K1 H$ {- N3 f2 I8 B' ?0 U& x - Return x * (2**i).# E5 g5 `- X% V p6 B x' }
- >>> math.ldexp(5,5)1 M5 G6 D- U5 |+ { b0 Q7 N
- 160.0: k" f/ i8 ~0 o1 M/ o+ G
- >>> math.ldexp(3,5)
: {' f3 A6 e1 O$ ?# o7 W - 96.0
复制代码
: r/ M, R5 s0 ^* i& ^( g* Pmath.log10(x) 返回x的以10为底的对数
7 {2 P5 A9 T C- #返回x的以10为底的对数
: V7 b8 B( o' t- Q( ~; F* X3 q - log10(x)
- |1 N0 i6 c% H - Return the base 10 logarithm of x., c+ Y! T7 U _% P s1 q/ ]8 p
- >>> math.log10(10)8 L7 ]& r% B- k% a& {8 d& D0 _' B
- 1.0
8 p0 Y! Q- ?. F5 d+ {, ~ - >>> math.log10(100)+ H% ^+ X" {# ]/ T3 u6 j, u; L
- 2.0 c2 L* c* h0 h+ S* |: C
- #即10的1.3次方的结果为20
- F O7 w! X- V+ N, u - >>> math.log10(20)
8 g6 N0 ^9 ]/ w0 \7 k2 v- h - 1.3010299956639813
复制代码
) T3 h I- z8 U, k- G9 L) y Hmath.log1p(x) 返回x+1的自然对数(基数为e)的值9 g% i% X" }8 u2 c
- #返回x+1的自然对数(基数为e)的值1 y) ^4 n: R9 X7 [$ w( `
- log1p(x)/ r* W2 q. q* ?/ M; a
- Return the natural logarithm of 1+x (base e).9 b" L6 G; K% q1 n) i" E/ }
- The result is computed in a way which is accurate for x near zero.4 j/ F$ r9 S/ ^& f5 q; ?3 }" s) ?2 p+ z
- >>> math.log(10)
. H; g. K' J/ W/ Y - 2.302585092994046- z2 p- X" k8 s( F
- >>> math.log1p(10)* x1 F, H% D0 W( j& y6 n8 u
- 2.3978952727983707
" d B. X8 g1 z6 M9 Z+ E - >>> math.log(11)
3 U: u& m# D* a, q9 A - 2.3978952727983707
复制代码 2 m* C/ Y6 U j. Q. m
math.log2(x) 返回x的基2对数
3 A( j2 I n8 V7 I- W6 x- #返回x的基2对数
3 O4 Z0 \9 A ~! q4 C- p - log2(x)
. I" t- P8 C+ L* B - Return the base 2 logarithm of x.9 y: a: ^8 M0 o+ j% J) s
- >>> math.log2(32)
% i. t5 e! U# L$ ^ T( s - 5.03 r% d5 ], {3 q1 u( S% E
- >>> math.log2(20)% N$ y& [1 O! ~% B. q) w
- 4.321928094887363; t( j" F) F, Z; Z0 v
- >>> math.log2(16), \# O1 r M$ `3 f
- 4.0
复制代码 3 F. h/ f8 X/ T" D# H8 v. y9 I
math.modf(x) 返回由x的小数部分和整数部分组成的元组2 @: B1 y# B% F) }( ]% v5 `; K7 C! N
- #返回由x的小数部分和整数部分组成的元组
, V/ J/ f- _9 o/ t, v - modf(x)
& {* B$ m" \/ A: T$ Y - Return the fractional and integer parts of x. Both results carry the sign) {4 l" i6 p% Q: r
- of x and are floats.
1 T& P" I: E' @8 w* } - >>> math.modf(math.pi)
# ^# Z6 [# K4 O2 a! E - (0.14159265358979312, 3.0). r _+ j* n: ~! E& `# ~
- >>> math.modf(12.34)
" ?, z" t2 A0 m0 u. o) M+ S - (0.33999999999999986, 12.0)
复制代码 2 |. b/ j5 v" _
math.sqrt(x) 求x的平方根& c- F, {7 l* c) G% U+ J/ S
- #求x的平方根/ S8 W# k) X8 L3 P; F4 {
- sqrt(x)
' J% n! ?) H+ y, o7 f - Return the square root of x.
7 o' v w- k5 H2 s0 o, Z3 z# E5 [0 s - >>> math.sqrt(100)2 z7 n/ v6 A5 I& q V4 d
- 10.0
8 z8 d" k6 j& ]& F3 z9 w - >>> math.sqrt(16)
* m# p6 i1 Q. _2 A9 N - 4.0
: E Q* \7 C" O1 L - >>> math.sqrt(20)
8 Y6 ~. c3 G9 d2 T8 b - 4.47213595499958
复制代码 ! q( y5 U4 J8 S- O8 E: g
math.trunc(x) 返回x的整数部分- #返回x的整数部分4 p6 { `( K# K' h) f# P
- trunc(x:Real) -> Integral! E5 ~0 b) g! G9 C; u7 P# e, z
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
s3 ]$ Y3 t/ U+ h7 ?& D3 O; H - >>> math.trunc(6.789)+ Y8 L0 C) S+ ?
- 6
: [; q% X8 v; {2 n& l# `: \4 ? - >>> math.trunc(math.pi)" s% u8 C4 g. o3 }
- 3# h+ u4 |" l6 u2 R
- >>> math.trunc(2.567)1 g' z2 f2 V4 @6 y- B
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|