|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 Q/ m( f4 D+ U4 A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
W" o( T( @6 k& T, B% \& {+ m2 |! P
方法1:
# C1 z0 Z( E0 X$ M' X. p- >>> import math; l( \# z) H1 k6 n M9 y' s
- >>> math.sqrt(9): b3 i9 [9 c: Y) J; {
- 3.0
复制代码 方法2:* A4 C, [: V& O8 @7 B/ F
- >>> from math import sqrt
, ?- z- G3 P8 F# R) Q. F - >>> sqrt(9)8 G/ n) I# N$ `. [9 P
- 3.0
复制代码
/ M0 d4 s+ |6 A. D! A @- ~. _9 p Q 6 ]' s; c5 l8 c+ I- b
math.e 表示一个常量
8 P# ^! g i. X. [- n6 A- #表示一个常量0 Y: |5 w3 }) c: s$ m5 L
- >>> math.e8 _9 a8 ~! S0 D# _. z
- 2.718281828459045
复制代码 , w- T/ T7 Z4 @5 u3 _. o5 n
math.pi 数字常量,圆周率9 U+ m- Y2 C5 l# g
- #数字常量,圆周率% G' q. c" L8 K9 | G
- >>> print(math.pi)( ?/ j' f1 l! V; p* Q! W8 h2 ]8 y! K
- 3.141592653589793
复制代码 ; G' ~, [* u l6 [% \. o
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 t6 U2 v% P" }" ^( e6 q$ k- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
" G+ R- H2 P# x0 @3 l& U. Z - ceil(x)
( i! f5 j9 T/ s$ O, F - Return the ceiling of x as an int.
4 V# A+ j5 a( ]# D/ I! c& E3 e4 T7 R3 F - This is the smallest integral value >= x.6 i# @0 t1 _9 B) B1 b1 r1 x: {
p S4 ~/ F% ^3 s* k& y- >>> math.ceil(4.01)3 b, a* Q* f2 e' ^) k
- 5
) {4 B: B* w$ s( b" i0 X$ d H - >>> math.ceil(4.99)+ B2 }, `5 m4 v2 o0 {
- 5
. [8 f3 c' j, [+ W3 t - >>> math.ceil(-3.99)9 h. x0 ^& \% R7 ]# q: H% K8 P% Z
- -3
' e$ K( S! P R( |2 I - >>> math.ceil(-3.01)7 g; s. p: T& H! X0 S
- -3
复制代码
* d* P3 Z& G( @4 amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ F; }" B, {9 r2 Z
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 I6 S! Y* Q7 x; {5 s3 S3 D - floor(x)
( L$ O" E2 U- o- _ - Return the floor of x as an int.
. E/ @2 H! J& e - This is the largest integral value <= x.
; j8 U' }! Z9 u6 d, ^4 R8 n - >>> math.floor(4.1)
9 s# I# i. i6 n( H( u3 H4 W" K$ n - 4
1 t$ a9 O/ x# o; w6 V$ f - >>> math.floor(4.999)% r1 U) f0 w0 N" W
- 4
( K6 Z. k* N. M0 P - >>> math.floor(-4.999)
y+ _+ `% U! u) a3 _6 V - -56 M6 o: G: R- {4 r, h
- >>> math.floor(-4.01)3 v/ T0 B( b: n% x6 w
- -5
复制代码 4 X- V9 ~) p6 h7 g8 U# z
math.pow(x,y) 返回x的y次方,即x**y
7 e. v+ R' j- R4 ?- O5 `: U- #返回x的y次方,即x**y/ [, S& W) a* d% R9 V3 n0 [
- pow(x, y)
$ y2 y# n9 x- n: _1 Z - Return x**y (x to the power of y).
" P% E8 X; m% c. a- e; C5 ] - >>> math.pow(3,4)
5 l0 d& g+ o6 J: ^' d- q- | - 81.08 n9 i Y, A! u+ t& }. M- g
- >>> % a' b6 Z0 o k D. H, j0 }
- >>> math.pow(2,7)
# k% N& H! e4 ~4 y - 128.0
复制代码
/ ~8 g6 v& k* D+ c, Z" jmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 C& P% R4 Z* [, k- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). n+ ]: o( ~9 m- b
- log(x[, base])
4 D" s+ L% G2 C0 {! T - Return the logarithm of x to the given base.: j) e8 a& ], F- `
- If the base not specified, returns the natural logarithm (base e) of x.
& L# ?( n5 M& U. G - >>> math.log(10)
5 N! z- s( I% t: A7 o - 2.302585092994046
! A [- h. X. a' E) x5 K - >>> math.log(11)6 G9 h& ]& h: u" e
- 2.3978952727983707
5 `) K! o$ L5 D, b+ X+ ` - >>> math.log(20)5 c4 `1 O# @: L1 _
- 2.995732273553991
复制代码
1 I( N0 \6 k/ Wmath.sin(x) 求x(x为弧度)的正弦值; n+ d3 G/ o# t2 [
- #求x(x为弧度)的正弦值
: _$ k: t( W6 w7 n - sin(x); F* b7 I+ `8 e2 Y. t
- Return the sine of x (measured in radians).
% o: t+ s' u0 N; u6 b; m1 f- L - >>> math.sin(math.pi/4)
9 u7 W% a- L) H7 P/ h - 0.7071067811865475' S* U& I" w, a; f6 H; Q
- >>> math.sin(math.pi/2)
6 i/ N/ _0 T d6 j3 M - 1.06 ^( b9 K8 k6 a4 _) z3 N! a
- >>> math.sin(math.pi/3). V2 m$ A3 s$ Y) X( u! U
- 0.8660254037844386
复制代码
4 ]! j6 @4 N2 P, Y4 mmath.cos(x) 求x的余弦,x必须是弧度8 o# u! K; O6 Q( I5 j0 O
- #求x的余弦,x必须是弧度
k7 @; `* K2 ^/ H, g - cos(x)0 I. f# B% G5 ?" i; K/ Y( X6 ^
- Return the cosine of x (measured in radians).
- M6 f0 {& y8 I3 ^ t3 C7 m - #math.pi/4表示弧度,转换成角度为45度* v: w8 e, s m: X
- >>> math.cos(math.pi/4)
7 L+ _' G1 f8 Y8 b2 z - 0.7071067811865476
9 `. w a! [/ \4 Y+ H - math.pi/3表示弧度,转换成角度为60度- q7 b; X& f; g: p" f# f
- >>> math.cos(math.pi/3)
1 R- u9 M$ U, u- |$ _' v( _0 } - 0.5000000000000001" ]( k7 Y" m1 m Z2 r+ X
- math.pi/6表示弧度,转换成角度为30度' s% y4 i0 L0 S) U
- >>> math.cos(math.pi/6)- P2 ~, N1 D7 { n7 C" \
- 0.8660254037844387
复制代码
7 O+ Q5 `+ g7 B+ vmath.tan(x) 返回x(x为弧度)的正切值$ Y1 q% X1 [+ f8 K. _4 f) ], \. @
- #返回x(x为弧度)的正切值6 y& }1 f* M# C2 N8 m- T
- tan(x)% r# Q7 k' ^7 _: |6 ?
- Return the tangent of x (measured in radians).
. ? x" H) m- v - >>> math.tan(math.pi/4)" V5 [6 N: E$ j% j" S5 \
- 0.9999999999999999
$ w+ b/ Z8 ~ n2 q: @ - >>> math.tan(math.pi/6)3 R9 U4 G- j4 c* p2 \( D
- 0.5773502691896257
; ]5 H1 F1 q- J; ? - >>> math.tan(math.pi/3); e8 ?6 t# L& y8 f8 S6 T. u* r- |
- 1.7320508075688767
复制代码
' H A4 `3 q- N& {% ? z, Lmath.degrees(x) 把x从弧度转换成角度" k+ W# C" v+ X( t
- #把x从弧度转换成角度
6 R" Q7 y, @ N* i - degrees(x)$ [( ]& J( S- T3 R1 a8 W. H
- Convert angle x from radians to degrees.
( r5 x& z- [! j- h" D7 {/ Q - : g; L' S8 C5 B% _) s2 ?
- >>> math.degrees(math.pi/4)) M0 V5 Q+ k1 C$ Y3 x
- 45.0
" k1 O# K7 f8 R: l) K. \ - >>> math.degrees(math.pi)
6 @/ E: Y0 z# e4 D3 O# P* S9 t# u - 180.0
. L6 q8 I$ Z+ w# A5 l - >>> math.degrees(math.pi/6)2 C. v8 A, p& [ k: m
- 29.999999999999996" i" O( H/ y( Y3 u- d- e
- >>> math.degrees(math.pi/3)5 c1 P8 t7 g# j S, R# v
- 59.99999999999999
复制代码
9 @9 s( o9 Z6 |3 e" R; J1 j' t0 nmath.radians(x) 把角度x转换成弧度
5 c$ z7 X- p$ @) |- #把角度x转换成弧度
2 i) J( q7 p1 v) Z. W - radians(x)
Y8 e' g. S) q - Convert angle x from degrees to radians.
- ?" `0 }/ q K/ |) v, P8 S$ F0 I - >>> math.radians(45)
' }5 X' P4 P G5 K - 0.7853981633974483
* M& Z' k/ @& v5 p h" ~8 k% K' n0 K4 c - >>> math.radians(60)
( J, ~% K4 n% s - 1.0471975511965976
复制代码
5 u2 ? U/ h1 m! _ g5 i% cmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
( |3 w9 c- X" ?% s- #把y的正负号加到x前面,可以使用0
! a/ ]9 q5 ?% L3 K - copysign(x, y)8 x3 l/ ^! V1 b: S2 @( Q
- Return a float with the magnitude (absolute value) of x but the sign $ _# q' K4 f d
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 5 |+ |# ?5 X+ l& j) `
- returns -1.0.8 F2 U F) M1 k: T/ D" W/ }3 x
7 l) a4 b9 X+ q/ N8 _2 a0 t- >>> math.copysign(2,3)
) Z: m$ k- l8 \9 Z0 H6 R - 2.0
) u# X* D/ X0 M* T - >>> math.copysign(2,-3)
4 X8 E I, I q2 q% g - -2.0, g: j! y& t+ K$ V% g. } f h# b
- >>> math.copysign(3,8), w1 V( {2 i- \ G( g
- 3.0
/ u( P7 R; d. {; \; F$ P( Q - >>> math.copysign(3,-8)
" F) n/ C/ w" _: x7 D - -3.0
复制代码 " G* w7 A9 \( _4 q0 O4 z6 J
math.exp(x) 返回math.e,也就是2.71828的x次方0 c( B' j4 G0 g: Y, h" D. E4 i' F
- #返回math.e,也就是2.71828的x次方: I! }3 h5 [/ i% ? f* }/ k" V
- exp(x)- U8 A! S+ O+ F8 y
- Return e raised to the power of x.
5 X+ F' E- Q5 b D" ` - 5 Y+ G- w& D% G! q6 M
- >>> math.exp(1)
& H0 c8 }& V+ `/ ~$ }8 _: \4 F - 2.7182818284590454 B; Z( ~$ J. r) r; ?
- >>> math.exp(2)/ e+ x4 U7 L* `1 h
- 7.38905609893065. F9 S5 r: U1 v2 S m' p8 _! _
- >>> math.exp(3)6 `" w) H& Z) U5 z9 L$ T3 P+ o m
- 20.085536923187668
复制代码 : ]5 Y; Y# H8 F! C% R
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
4 R9 q$ p: j& [' d: H: S& I- #返回math.e的x(其值为2.71828)次方的值减1* o% ?3 _( G9 [4 E, B
- expm1(x)
$ i5 n1 j: C! E( ^7 w4 f, ^. Y: `9 i - Return exp(x)-1.
, v2 v) b6 r' ?' ^$ _ - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ _0 W5 B/ q" ]. o
5 @- @' j& S; |+ L% z) n8 t- >>> math.expm1(1)
* {/ u1 |- v( j9 S; @+ U% _8 X, L - 1.718281828459045
1 e3 @1 V9 n* w& T - >>> math.expm1(2)# P: p* x j1 L& |+ |% g. ]+ e8 u/ |
- 6.38905609893065, S1 B9 A6 P6 L+ s
- >>> math.expm1(3)
}3 c7 _$ d* L3 i& D - 19.085536923187668
复制代码 / ?, o6 @" O$ G/ c
math.fabs(x) 返回x的绝对值
- ~9 _: k5 ?/ J5 x. F) ~8 h7 x- #返回x的绝对值& N! b; ~ Q; f5 I; _7 r$ k/ R: H! {
- fabs(x)
# D, o" n/ c) B' N- x' ] - Return the absolute value of the float x.# q ^" v+ i/ ?* w
$ H; n4 ]& t" f0 g- >>> math.fabs(-0.003). d2 a4 W. w9 \8 {6 d+ Z
- 0.003
9 p7 I3 w5 a$ [ - >>> math.fabs(-110)
* Y2 r7 G: X7 t - 110.0
# U: B: I# |& j5 F - >>> math.fabs(100)
% [& J5 Z4 p, k. ?$ Y - 100.0
复制代码
- r3 v4 y) Z# R. H' `math.factorial(x) 取x的阶乘的值, K) c0 D' c f6 q0 r. K6 D
- #取x的阶乘的值) X w3 s g2 f$ p! x' ^3 F
- factorial(x) -> Integral
4 P$ _3 `4 X3 c; M; w9 a - Find x!. Raise a ValueError if x is negative or non-integral.8 z) i7 N9 q/ y! b5 e4 ` |$ F
- >>> math.factorial(1)7 _- t9 w' X' M3 f- Q2 U# ]
- 13 T( F9 r* r6 h/ ^8 `3 s
- >>> math.factorial(2)1 h9 j% v9 K. Y4 `4 J
- 25 @+ o. S% u7 ~) `6 j% t; J
- >>> math.factorial(3)% z6 i2 o& \ s; [7 ~/ n8 j6 `
- 6
+ ]3 N& ?. ^2 y z' g O - >>> math.factorial(5)
" N: X: B. K5 O0 L5 ] - 120
7 w1 |9 d: @9 e' I K/ h - >>> math.factorial(10)
& y- ?4 W- q. x; B - 3628800
复制代码 2 p/ p# E1 _* Z4 [8 r
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
* s% Z" v6 h2 z: i; D( u0 [- #得到x/y的余数,其值是一个浮点数( D: m& ~3 A4 O9 y( C$ z# M
- fmod(x, y)4 Y$ d* d. N7 I$ |1 c' d$ K. O" \
- Return fmod(x, y), according to platform C. x % y may differ.
/ H+ l5 N& M7 n! [& S7 B3 _/ T" v - >>> math.fmod(20,3)2 M2 a* N# ~: r0 g
- 2.0
4 G4 C0 _% S: r/ @1 @4 x - >>> math.fmod(20,7)+ z8 L- C% Q( m! A2 o7 O
- 6.0
复制代码 2 l- N o" }3 a# I2 \
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
+ i- x; ]; s0 g2 A& c$ A- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
1 p# O8 f( _1 i* o& c: U - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ J& _5 N& l* d& `* i6 `. \3 S2 S/ |: m; H
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
) h( f @; b8 a+ a6 W" A6 m4 j - frexp(x)" o4 p8 p+ ?, x+ |, [5 |
- Return the mantissa and exponent of x, as pair (m, e).
" g. i9 m. g* c( f" _0 K5 h" q1 e - m is a float and e is an int, such that x = m * 2.**e.
; f. E2 I) a; S" c& Y; | - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 X' W& [' }5 k* b
- >>> math.frexp(10)! p3 d1 S$ C' E6 Q: s( S
- (0.625, 4)- t4 p- C" f& n/ P: V
- >>> math.frexp(75)0 P: g5 t+ |+ [; P
- (0.5859375, 7); S' H$ g' H4 a- v* \% m$ d- R
- >>> math.frexp(-40)1 `# m5 ^/ m% I6 S9 b* j
- (-0.625, 6)
7 l i( S! F; Y+ M - >>> math.frexp(-100)4 y. ^2 u) e% x* H H
- (-0.78125, 7)/ m5 r! L1 W- P$ G) S
- >>> math.frexp(100), D1 Y2 M7 P6 V: M+ z6 W h
- (0.78125, 7)
复制代码
1 ^! {* Z' M% ? z$ r% Gmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 E3 m6 ]9 r$ Z7 J& ^( I0 o- #对迭代器里的每个元素进行求和操作: b$ w* Y( S9 Q+ ?
- fsum(iterable)! k; }! k% D1 g: p1 n
- Return an accurate floating point sum of values in the iterable.
# z7 K+ v. y) o1 ~ - Assumes IEEE-754 floating point arithmetic.
4 E6 @/ t2 l7 N8 ~: X4 Q8 ^ - >>> math.fsum([1,2,3,4])2 @1 j" a# G5 k' h# K
- 10.0
% ?5 f8 k$ [' `$ h g- Z# v; ` - >>> math.fsum((1,2,3,4))- ~: l% O& T' P: `* f
- 10.0
3 z5 a7 E3 R6 ^6 D - >>> math.fsum((-1,-2,-3,-4))
) t3 l2 y. X; ] W2 L0 B8 }/ z - -10.0
) ~" T! b" C2 H) m9 ]; B4 A - >>> math.fsum([-1,-2,-3,-4])
$ J+ d0 g+ M" e9 L1 ?1 r; g4 s - -10.0
复制代码
1 |& n% F3 h: Q' d! }! Y; G3 {& ?math.gcd(x,y) 返回x和y的最大公约数, r% j( T; j7 b$ z, A
- #返回x和y的最大公约数
6 Y2 U( l' `. k- K - gcd(x, y) -> int1 S* C2 D1 p6 M- p/ T! _
- greatest common divisor of x and y3 t' y( Y0 M, f7 _7 G6 Y
- >>> math.gcd(8,6)
$ A( t3 M9 Q& z9 F; f5 H. ? - 2
* r* n3 f% J& N0 K - >>> math.gcd(40,20)
% s7 O, o* W' b - 20
9 B$ K' X% M6 C% K- C! [ r$ ~* I - >>> math.gcd(8,12)9 t: Q! V! ^% F3 T6 p( D6 K
- 4
复制代码 " b1 t9 K8 ~5 M' P" V+ \: L# q0 u- a
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False0 l, q J& M! v
- #得到(x**2+y**2),平方的值6 ^4 t, F$ a; }
- hypot(x, y)/ F8 o5 H- ]( m; L8 ?
- Return the Euclidean distance, sqrt(x*x + y*y).2 ]# K/ e" {3 f% u4 @0 j- n+ }5 R3 h
- >>> math.hypot(3,4)
. E5 G( s7 Q I( Q D* n# l4 `( r$ ] - 5.0
2 t- ]+ e2 s5 U& x- P - >>> math.hypot(6,8)% ?/ ?9 `& g5 n1 V! t! o4 q
- 10.0
复制代码
' b8 \: [1 Q* m5 I" i9 amath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
7 f; W9 \- l9 t1 Y- #如果x是不是无穷大的数字,则返回True,否则返回False: \, N6 l7 c6 q# k3 y6 I
- isfinite(x) -> bool
' a" U1 f$ k( c, l6 o& v& H - Return True if x is neither an infinity nor a NaN, and False otherwise.
; G& E8 a, e* G3 ^0 M( n - >>> math.isfinite(100)
6 n @( M% k7 ^+ p - True+ t1 Y. i2 g. ^
- >>> math.isfinite(0)
5 M5 A* [$ Q$ F3 [$ ^4 I - True) M: l3 \' A# E, s
- >>> math.isfinite(0.1)& q. D0 K0 n" X% n* D1 O0 h
- True8 I* J! o# i0 o/ D( d7 n
- >>> math.isfinite("a")$ c( Q+ r4 i3 c4 ?# a& |! L' {
- >>> math.isfinite(0.0001)
9 F+ Z3 q" V. t, `: A/ S# q& k - True
复制代码 % K- k" S0 q; t4 O4 W
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 V1 ]% A) x: ?) t8 }. }( {
- #如果x是正无穷大或负无穷大,则返回True,否则返回False2 e4 n- @1 V8 B+ C
- isinf(x) -> bool1 V2 B+ d) c) |4 _
- Return True if x is a positive or negative infinity, and False otherwise.* n1 Z1 @, V% @# L8 ~9 C
- >>> math.isinf(234)
: v. s4 d- e& O. d; g" f6 e b - False
6 L! Z0 C$ E' u- t - >>> math.isinf(0.1)
5 P9 k+ f" o' ` L - False
复制代码
% F3 v- P& L; i& J5 T/ I; o8 J$ p% s0 b! vmath.isnan(x) 如果x不是数字True,否则返回False
; X2 c% z, \6 N( ^- #如果x不是数字True,否则返回False
+ U7 X# e& R. |" f2 t' k- K. a - isnan(x) -> bool
. E- J6 u. T4 Q; ^2 E1 S, y - Return True if x is a NaN (not a number), and False otherwise.- j0 ]) \! U% P+ I" e
- >>> math.isnan(23)
! E6 f Y6 H$ T - False( N9 G" W# p! ~& T
- >>> math.isnan(0.01)
; W: O. K- X" q! G - False
复制代码
$ b/ u3 F) B/ r/ ^3 {2 h/ f/ Smath.ldexp(x,i) 返回x*(2**i)的值
# F) _+ p/ p$ w9 q5 R- #返回x*(2**i)的值
- [; R9 T6 Y/ z8 N - ldexp(x, i): m6 Y8 F/ h X+ X& S6 m+ j) Y z0 a
- Return x * (2**i).
/ {7 H* Y- j( d4 G+ c6 q& v. ^ - >>> math.ldexp(5,5)8 }4 s2 A; N0 ?( {# o
- 160.0. I6 Q; t& D% x
- >>> math.ldexp(3,5)3 p z7 z+ {( M4 n. ~
- 96.0
复制代码
, A% w+ `5 V# Q7 z, G' Q4 \% Kmath.log10(x) 返回x的以10为底的对数* D; R; @" G) B5 q& w
- #返回x的以10为底的对数
3 q0 Y0 G. V: z8 c5 }* f( U - log10(x)& }' ^! x- I5 F& W9 d
- Return the base 10 logarithm of x.
1 n; c1 f- B8 q, g @* a9 x - >>> math.log10(10)
: X' _1 Z# n* z6 z - 1.03 Z% n O- R6 g& S" l! `
- >>> math.log10(100)8 Y" _% |5 d$ n& G
- 2.0* r( |) g% Y; t' [6 _/ ^
- #即10的1.3次方的结果为20
0 |' l H$ l5 z1 ^' A% g. { - >>> math.log10(20)/ h! k, `! V0 e
- 1.3010299956639813
复制代码 ^7 x, Z( _8 W% R1 I& J5 Y
math.log1p(x) 返回x+1的自然对数(基数为e)的值
8 P6 Q: u6 Q% h2 F+ d; Z: _$ `- #返回x+1的自然对数(基数为e)的值
0 N4 u* c/ T) [ [, d# w - log1p(x)
2 W4 `! Y; P: Y, z! { - Return the natural logarithm of 1+x (base e).
2 V, x- ?% M* ?* b& T - The result is computed in a way which is accurate for x near zero.: k* J/ X9 Y# d
- >>> math.log(10)% @$ Y* t ?: w/ W9 O! f! Z
- 2.302585092994046
* h- F& L7 u# Z# d& g. R/ X/ l - >>> math.log1p(10)
* l8 B6 g/ ]' m0 B9 Z - 2.3978952727983707* n9 ~! V, s; p$ P& O
- >>> math.log(11)
( E/ N a# H2 r: D$ ~ - 2.3978952727983707
复制代码 7 D+ @) d8 ^% V2 V' i
math.log2(x) 返回x的基2对数: M- L& G& \+ e( ]9 X$ c0 t
- #返回x的基2对数
; k# l0 R1 B" s0 c& n - log2(x)
8 q* s/ I: l* l - Return the base 2 logarithm of x.
2 X3 @) `+ g# l. o9 n - >>> math.log2(32)
& S4 p. z8 w( b0 B% ?' m) H - 5.0
9 y, X X6 H3 C$ l6 P- ? - >>> math.log2(20)8 o+ \, n& M$ R0 q
- 4.321928094887363
% h/ K( R% v' h6 d1 s! f9 e/ L - >>> math.log2(16)
, X7 |! g+ [& c1 v1 b4 @ - 4.0
复制代码 " h' o9 y0 ?0 p9 t' ?, p
math.modf(x) 返回由x的小数部分和整数部分组成的元组
) ]/ C3 H% ~* Y8 X: D# u- #返回由x的小数部分和整数部分组成的元组
: J- h( G7 E1 h( \; [+ C! \ - modf(x)
0 [: Y$ ^+ Z! g, r+ h - Return the fractional and integer parts of x. Both results carry the sign0 b; W4 N; d- W+ V# F2 {
- of x and are floats.( u# _5 h7 T$ E
- >>> math.modf(math.pi)
T/ y. Z0 E+ b1 X3 j/ S7 P. F - (0.14159265358979312, 3.0)
% M( |8 l1 A9 w! N$ G% U - >>> math.modf(12.34)
5 D5 s* Z! ]; {. H. q9 a* O - (0.33999999999999986, 12.0)
复制代码
# K u {$ B4 w/ s4 t& Pmath.sqrt(x) 求x的平方根
D H. m7 u" |3 N- #求x的平方根7 B- v5 T8 X6 r$ \$ }' B
- sqrt(x)5 k* H1 r( Y) S) u3 o4 Q; k
- Return the square root of x.% @( n" I ]/ x3 m
- >>> math.sqrt(100)* w! k( C# R: L. e6 M9 T* r# J
- 10.0
5 k8 y3 N9 d( D( } - >>> math.sqrt(16)
8 _2 D7 T! K" ?- M& X - 4.0
( G. k# C5 c% s - >>> math.sqrt(20)3 c# N- q/ `' o* N b
- 4.47213595499958
复制代码 ) `: l, w, H9 V& E6 m5 t8 x
math.trunc(x) 返回x的整数部分- #返回x的整数部分
( `, S' i. s' U; P - trunc(x:Real) -> Integral
; O* A: Q0 E O& @2 q - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.; d( ?) I! o: M8 T
- >>> math.trunc(6.789)
; Z' ^- \, V2 _* l9 P# W' H - 6' c5 a# P' v; [0 `: n
- >>> math.trunc(math.pi), ] R+ W D8 O7 y# { Y
- 3
( p8 y a! d) j6 ` - >>> math.trunc(2.567)
) z# M' G: J/ l5 J4 w. h - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|