马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 t6 s+ H6 B) R8 w O s【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。5 n+ c; j5 p- d. X! m7 `. i j
8 {9 a( x) ]* L
方法1:
. m& T2 @+ M1 ~- >>> import math
- c, g4 \5 U8 B4 t - >>> math.sqrt(9)
0 Q6 F2 w3 z0 k - 3.0
复制代码 方法2:% F& r6 l1 M" u* h" U4 b: R4 r
- >>> from math import sqrt
$ p+ a3 X! y# L2 ` l* X/ u; f - >>> sqrt(9)- `9 f4 w# d( w; |; ~+ {
- 3.0
复制代码
, N% g1 Z4 h% }4 s. `3 d
0 H, p) z$ I6 L! s; S7 ~math.e 表示一个常量
6 v4 n4 V3 b1 Z9 P1 B4 E& R- #表示一个常量. G, ]" Q& L. ~. R2 h+ w+ s
- >>> math.e
1 b. o. q3 l$ Z0 h. S - 2.718281828459045
复制代码 5 [6 i7 b; K8 n6 R8 d
math.pi 数字常量,圆周率
9 j Y8 J: w/ g9 a8 S- #数字常量,圆周率5 Z! R6 p6 I5 k& u3 u a" W2 z" \
- >>> print(math.pi)2 t4 H% ?, U3 N' S6 S& O; F
- 3.141592653589793
复制代码 4 `9 i, G6 ]" }1 J4 O, e; \0 M$ U
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, i7 L7 b5 {" o6 P. Q1 K
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 X$ ^( s( f7 ?. F1 }; D) `
- ceil(x)
+ w8 @+ _. a! ]; T0 r4 ^0 { - Return the ceiling of x as an int.4 @8 P. P q$ _6 g
- This is the smallest integral value >= x.' {4 g2 B- F1 z4 |* _3 N
+ B2 U9 w! T) G, e- v1 p; p# ~- >>> math.ceil(4.01)
# y5 _. L+ j# W: \# y - 50 v5 o' e1 o& O. t. w$ D
- >>> math.ceil(4.99)
" C+ S8 `# P7 W2 h - 5
- X, c+ q+ o5 ?' `1 K! _- k4 B5 B - >>> math.ceil(-3.99)
p0 y% c" b- Z% h5 ~; a - -3; [" W$ g* x0 @- R0 y4 q: V
- >>> math.ceil(-3.01)' b; p0 \1 k1 w% S
- -3
复制代码 , i0 A4 J* }0 ] f& I' X
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身. Y+ A) o b D% @* R1 X3 H
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& X. t" o; X; q$ e - floor(x)
5 X# q6 y3 {5 J. |6 T0 d% f& c - Return the floor of x as an int.
5 J& g6 q9 {$ M3 O% n/ E' P+ I - This is the largest integral value <= x.
( y, a0 _/ {4 j1 ^2 k - >>> math.floor(4.1)$ F! J. G# s& K
- 4
* W4 a+ D2 L( ]* b! K - >>> math.floor(4.999)
0 _; R4 Y; s" a3 H+ K, k - 4
" K; z- U1 g# }+ g - >>> math.floor(-4.999)4 J _8 m* O& I# \: A
- -5
V0 L$ X8 x3 w/ L4 ` q: i' e - >>> math.floor(-4.01)) z4 O. u9 A6 \! Z- Y6 _
- -5
复制代码 - I2 w+ U0 P- @0 A% u
math.pow(x,y) 返回x的y次方,即x**y
# \. x8 o9 t: @+ q. ~6 C8 }- #返回x的y次方,即x**y
. j% Q. o2 M6 ~' D( @/ O+ E& k - pow(x, y)
% V) V: j+ I( o0 [& H" z# L - Return x**y (x to the power of y).
% h; H4 {6 x9 }( i$ W - >>> math.pow(3,4); V. b8 W5 L; J* o$ e# V
- 81.07 }5 k. E& k4 z6 D
- >>>
7 `. T$ b& G- q. V! E: j - >>> math.pow(2,7), Q* ]2 b0 V0 F
- 128.0
复制代码 & G5 ?" u# s, C: f( G
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) l* W1 Y; H# u. d; ?) K0 K/ e
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& N0 Z8 t- x7 D9 b' X - log(x[, base])
+ {7 t! I* r, Y3 _* O( E1 Z - Return the logarithm of x to the given base.4 a+ A! d( V. ^4 g) Q# s
- If the base not specified, returns the natural logarithm (base e) of x.. N3 B+ y; B2 n$ r4 G, X1 Z- v; ~( Z! V
- >>> math.log(10)
# x& Y$ Y$ Y; I - 2.302585092994046' L: C* m4 t9 n& {$ ~
- >>> math.log(11), f; J0 x l9 Y- v6 w
- 2.3978952727983707: E( [. t# L3 ?& k
- >>> math.log(20)
6 w. m1 Q+ e6 @6 b- i0 c( a0 x - 2.995732273553991
复制代码
- P- s1 `: r$ E2 }; M+ q) ^' Cmath.sin(x) 求x(x为弧度)的正弦值3 X$ A4 r" W" U$ T" s! l' o. K
- #求x(x为弧度)的正弦值
: X) @# ]) D4 Z# l- E5 X" P/ c/ H - sin(x)
+ K$ d$ ]9 L$ m5 i - Return the sine of x (measured in radians).& q8 R: J. t; K6 X& }
- >>> math.sin(math.pi/4)
& h2 c: _( @) n. \ - 0.7071067811865475
, j+ f D9 a e9 d# k - >>> math.sin(math.pi/2)
+ p1 p: N' N8 Q+ D/ z - 1.0 ]6 U4 b) ?# D2 Y( }: u# C8 K
- >>> math.sin(math.pi/3)! y. l# K; P, s d3 I2 R+ ?4 O5 [
- 0.8660254037844386
复制代码 4 d! A m- ?: P3 P: E
math.cos(x) 求x的余弦,x必须是弧度* |) T3 q/ p) @0 w/ F
- #求x的余弦,x必须是弧度
, n7 [' D3 O, M1 O6 m3 L$ C( I6 v - cos(x)& R& c7 }# y( v
- Return the cosine of x (measured in radians).
$ H7 o: b6 ^% N# K' V! t @$ P$ f - #math.pi/4表示弧度,转换成角度为45度
4 _ ]' a! V5 s, s" ]1 a" f - >>> math.cos(math.pi/4)
$ m$ l7 q4 N0 c( q9 ^ - 0.7071067811865476
8 a% E6 j& r9 s; p - math.pi/3表示弧度,转换成角度为60度. `$ b6 p6 a; s" l/ I) I; J) r
- >>> math.cos(math.pi/3)1 J& Y; C Z- y8 t# q1 G
- 0.5000000000000001
$ M7 s+ x1 O! | - math.pi/6表示弧度,转换成角度为30度
8 z8 U' A, C! C - >>> math.cos(math.pi/6)
0 G9 E+ K/ ?' [ - 0.8660254037844387
复制代码
4 } Z' @& j* N2 K" ]0 W: V0 hmath.tan(x) 返回x(x为弧度)的正切值
1 `1 C+ \7 D$ Z) d# y5 E- #返回x(x为弧度)的正切值
- j9 B+ C/ m8 e - tan(x)
9 P7 R( ~' u% j0 B7 d! u* M - Return the tangent of x (measured in radians)., `2 R& f% ]6 t* T
- >>> math.tan(math.pi/4)
; l" [3 r/ ^9 A' _" ~: ]# }; O - 0.9999999999999999
& Z3 X3 _( }" L9 g; w% ] - >>> math.tan(math.pi/6)) ?6 N1 n+ p1 @
- 0.5773502691896257
7 n b" ]- T4 H5 f+ `" S. d* O9 W; b - >>> math.tan(math.pi/3)
* \. m8 w8 k) f* f, a - 1.7320508075688767
复制代码 3 i5 P5 |* Q" i9 ^6 t
math.degrees(x) 把x从弧度转换成角度" l% m5 r1 p3 S7 D0 T1 h6 B" d
- #把x从弧度转换成角度. X4 i' f0 ~( a& C( H7 z
- degrees(x)
1 O9 X% A: j2 Z! y( s9 \* Y - Convert angle x from radians to degrees., |4 M- n" ]# ^# H2 b2 }
- ; y* {" S: R+ z& Q- f( t) w. {$ \
- >>> math.degrees(math.pi/4)/ {+ s2 q v2 n8 G4 `
- 45.03 K1 k' m5 _8 H) N( [
- >>> math.degrees(math.pi)2 _" O8 c( I. K E* [/ \. h; q1 m6 Q
- 180.0' i \9 X/ e2 y+ s3 K3 W4 ?
- >>> math.degrees(math.pi/6)
8 @ d, r( a$ L* o5 g6 j- J4 ` - 29.999999999999996
2 w) t2 l0 _5 M, b4 w - >>> math.degrees(math.pi/3)# g9 D `( n- d
- 59.99999999999999
复制代码
' n* G- B" A. {" s+ I6 J1 emath.radians(x) 把角度x转换成弧度
$ h, I; _7 {3 i9 L$ G/ U- #把角度x转换成弧度% f: B- g, X- r! w9 o
- radians(x)
5 l/ D) o. u5 f; S9 R/ X - Convert angle x from degrees to radians.
5 ]) {3 d0 P. a- w/ X( z - >>> math.radians(45)
; M5 t/ V$ o4 v) E" B, ?; Z - 0.78539816339744838 g! w, q8 Z, Z6 R! w8 o
- >>> math.radians(60)
4 M5 e$ O- N2 l Z/ A - 1.0471975511965976
复制代码 3 W: Y8 Y' i9 D3 L
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
0 G3 E5 ]7 b2 b3 z \2 H7 I3 m- #把y的正负号加到x前面,可以使用0+ K: H2 f! b! c4 O" `& _9 G% a
- copysign(x, y)
0 R# X" N6 c" L- k" p; ]" | - Return a float with the magnitude (absolute value) of x but the sign 3 w, E4 v/ l1 G" x& i2 N
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
1 q* a1 h1 C" j& w, G2 ?0 C$ D - returns -1.0.
! r' f m: }! q- S7 o9 W" L - & d9 q4 y7 E5 p/ M/ n
- >>> math.copysign(2,3): x) G6 \1 [0 G# \
- 2.0+ G3 A1 R$ x3 L; G( @
- >>> math.copysign(2,-3), C; k; I- q1 _: w; m
- -2.0/ F! j( R4 d5 y- N# x5 J
- >>> math.copysign(3,8)
0 E8 ?$ c! Z" b2 m5 V! W% _ - 3.0
3 W/ P3 y5 D: X9 d - >>> math.copysign(3,-8)/ f* u5 L6 U! h1 m' F# \6 b
- -3.0
复制代码 / O& `" E2 @$ f
math.exp(x) 返回math.e,也就是2.71828的x次方$ x" |6 J$ L6 K! [; `; V
- #返回math.e,也就是2.71828的x次方; g0 @" a5 {; ?+ N- P$ A! q2 B
- exp(x)
5 v& y+ i! L/ w7 i0 y2 _ - Return e raised to the power of x.
( L# p. w3 s# a- ^1 I
7 c. |% U: @- j9 [4 z- S- >>> math.exp(1)7 s& X5 ?! o$ o$ j( }6 s( B' w
- 2.718281828459045/ T, { ~ k: ?" i% B
- >>> math.exp(2)
+ w! P% A" K: T! p9 v% e1 | - 7.38905609893065
2 }1 a" b3 M2 @2 Z4 L/ Z, k8 Q5 p" W - >>> math.exp(3)
: s# r+ {. g y - 20.085536923187668
复制代码
/ F" F9 K! e3 a7 {& Emath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1; f. ~) ^9 O/ m* R! Z
- #返回math.e的x(其值为2.71828)次方的值减1) O# [2 V( B7 S. i: y2 x
- expm1(x)& v1 v( w1 V7 S0 A. T7 c3 C3 H
- Return exp(x)-1.8 U2 r' e- M$ @# ]% ^. e1 g" S
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. p. K- e# W5 K* U9 {
8 S2 K5 {7 {5 c- >>> math.expm1(1)
; E: t) ?2 R4 t0 ] - 1.718281828459045
) [$ P9 f* g' `9 g" x. W. t - >>> math.expm1(2)
! {& ^8 L. j5 y8 `4 x) E* L: ~ - 6.38905609893065
9 V' m; I' Z* C - >>> math.expm1(3)
' ~, c' t7 m2 L - 19.085536923187668
复制代码 + p( d7 X8 q. a" q
math.fabs(x) 返回x的绝对值
( @$ f# R0 s5 ?" x" z; ]2 D5 }- #返回x的绝对值6 Z+ x6 R# W, y
- fabs(x)7 M: g ~0 G! z' f
- Return the absolute value of the float x.. l q9 m+ V+ G4 `
. u0 k1 f! W. z3 n- >>> math.fabs(-0.003)
- O6 c1 w' I2 i& k$ n( S1 ` - 0.003) y- q6 u8 {7 y1 U. c
- >>> math.fabs(-110)
2 B8 v0 [! l, H( D$ \2 f& ^. y' Q$ K0 K - 110.0% b/ Q5 W! l9 r/ `# h
- >>> math.fabs(100)
4 r& m6 C6 J* n - 100.0
复制代码 3 J \4 a, _% `/ Z/ |% ~. V6 L
math.factorial(x) 取x的阶乘的值
; Q; T8 \: j/ `; B- #取x的阶乘的值
' a% ~: W5 G2 y% `) J! p - factorial(x) -> Integral) N3 Y2 e! i3 {2 l% b
- Find x!. Raise a ValueError if x is negative or non-integral.: q4 _8 t& X1 C0 e0 u
- >>> math.factorial(1)
6 J p% F9 @5 ]6 q z! e; c - 1
+ U. s+ n, A3 n8 G: P - >>> math.factorial(2)
6 }/ {9 i7 H4 `5 n) K- | - 2
; U/ q' Q7 u+ H" e$ J7 ~ - >>> math.factorial(3)
) x9 t- v& k% F' Z* ` - 6+ b w+ a- ]8 \1 b( T( w( D8 r& Z
- >>> math.factorial(5)
X* M2 V& v- B% ]. ~ - 120
/ W$ g) e9 {$ }# u2 | - >>> math.factorial(10)
+ u; f1 b8 c* Z+ Z% o2 h$ v - 3628800
复制代码
# Y: S1 {# y, W" M! |( Umath.fmod(x,y) 得到x/y的余数,其值是一个浮点数% b: ?' [4 N# v8 }
- #得到x/y的余数,其值是一个浮点数
, [3 F* B/ D0 P0 o: R: d) n - fmod(x, y)
# c5 o4 D( f* Q6 ^% c - Return fmod(x, y), according to platform C. x % y may differ.5 M F0 e* W: z# J, F& U, [5 D- {
- >>> math.fmod(20,3) K$ b: o( F1 a. U
- 2.0( h) ]( f8 E% I0 c8 Z
- >>> math.fmod(20,7)+ m, F5 _ ~. G+ E0 e, i
- 6.0
复制代码 " Z4 g+ W" E6 u! e
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
0 m$ x; e( n2 x# F* z8 E- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, U) `7 e- Y- @( {3 T" v
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值* @: ?! W$ u- a p: d! E1 v! t
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和15 W( {( p3 {2 r& D$ V& F8 Y
- frexp(x)
q6 Q7 x9 i8 s( E& j/ o0 [% W" K: A' [ - Return the mantissa and exponent of x, as pair (m, e).
2 j! j! n2 Q; Y - m is a float and e is an int, such that x = m * 2.**e.# v3 |. ~+ I* y4 g- U) v
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.7 ~$ x3 N, g: [
- >>> math.frexp(10)
0 r% u# U% Y; n0 B - (0.625, 4)
3 K l$ X9 i) Y1 o% J2 u8 z9 D - >>> math.frexp(75)
( c+ f3 T) B/ A5 U; ^1 J - (0.5859375, 7)6 R: C# Q! ^+ F1 j
- >>> math.frexp(-40)1 C: Z. H* Z) e( d( a( @5 L5 c1 w: f
- (-0.625, 6)
' y7 n/ |% L, f/ t8 O7 U; N. u - >>> math.frexp(-100)0 b; p; r! w- T, D8 C
- (-0.78125, 7)! v7 t& p' r' l) V
- >>> math.frexp(100)+ c' H4 T5 k2 f& ^
- (0.78125, 7)
复制代码 7 j8 h1 }. O" }% A: H: _" T' K' D
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 }/ q8 S* k$ m* n' X( C+ z+ G3 n- #对迭代器里的每个元素进行求和操作% _+ ?# {( \! A7 {; a
- fsum(iterable)5 Q$ ]" ?9 z9 k
- Return an accurate floating point sum of values in the iterable./ f! X% l" |% z8 Z( M4 ~* B% _
- Assumes IEEE-754 floating point arithmetic.3 G W/ {: o8 K
- >>> math.fsum([1,2,3,4])3 O, q0 i% ^' ~! e( l
- 10.03 X) _" d8 E3 Y# e) p) E
- >>> math.fsum((1,2,3,4))2 n7 y0 T% P# G
- 10.0
1 E) ~6 d# w5 W1 ` - >>> math.fsum((-1,-2,-3,-4))
5 N, |) z5 G" K# B4 s - -10.0# O D0 S+ u3 j. X
- >>> math.fsum([-1,-2,-3,-4])
+ e3 b& B( [3 ~1 K8 c( h% ?( F - -10.0
复制代码
+ R6 [/ B7 g( |8 l. G* Lmath.gcd(x,y) 返回x和y的最大公约数
% V: {! h ]8 g! I- #返回x和y的最大公约数8 C, ~( v* o0 A5 C
- gcd(x, y) -> int, _$ j; ~* q3 m. p. ~" f3 |
- greatest common divisor of x and y5 g, ^: i' b/ L) }# o7 E
- >>> math.gcd(8,6)
3 r1 n3 g, X9 [( v" Z0 ?2 y - 2
6 L2 i: s2 J1 h' K8 [ - >>> math.gcd(40,20)9 Y, o- w x* v& {. S) _- E9 x+ L
- 208 _3 i3 e! y9 ~% m% P0 C, B* x, j
- >>> math.gcd(8,12)
1 S3 W7 o+ ?* V - 4
复制代码
6 H9 \* a. G4 I1 T& Tmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
6 X d2 q) Z# ]9 A& x4 l" U- #得到(x**2+y**2),平方的值' b* [ X4 s& s ^/ g8 s. Q; D9 T' n
- hypot(x, y)) `6 l$ t- @. r
- Return the Euclidean distance, sqrt(x*x + y*y).( W# P, p& U9 W, l# s) f+ j7 E
- >>> math.hypot(3,4); j5 ]1 {" q2 {, |
- 5.0
9 _# e$ g( R/ G5 Z6 Y m" J4 V - >>> math.hypot(6,8)
6 f4 J: w; k" e- Q N4 F8 _( W - 10.0
复制代码 4 A0 @. ]+ r( D& R/ Q" Q( U q8 n4 C
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
) E7 B) y& g& C- #如果x是不是无穷大的数字,则返回True,否则返回False
* w9 |' u9 k @ \: }6 K. T - isfinite(x) -> bool# c1 d6 @" M- {, N" f2 _
- Return True if x is neither an infinity nor a NaN, and False otherwise.
& M# \2 T; `$ U ` - >>> math.isfinite(100)4 S v2 D0 c5 d" m7 ]
- True4 i6 T, A" s+ p1 {( W
- >>> math.isfinite(0)
B4 j0 R+ i( x0 j! m9 I - True! `# f' X1 k, |! g
- >>> math.isfinite(0.1)5 T7 A/ `; z7 a. U" [
- True
4 L+ a+ @6 N7 N - >>> math.isfinite("a")
2 O/ C3 H/ X7 a6 f: t - >>> math.isfinite(0.0001)
0 Y: A' C8 w- \: v" X - True
复制代码 ' e- J8 _) K( u0 z/ g
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! W6 m1 W6 U6 l( u+ i: M3 a( j5 c- #如果x是正无穷大或负无穷大,则返回True,否则返回False; M9 g, N' ~1 O: i1 H& O
- isinf(x) -> bool
& ]1 P6 p9 J! Q5 }. Y! T - Return True if x is a positive or negative infinity, and False otherwise.+ L" o& V3 Q+ F+ s. t
- >>> math.isinf(234)
# Y7 j0 |: p0 {0 Z1 q8 t - False
k8 h+ J4 G8 K% ^" K5 @5 N - >>> math.isinf(0.1)" p1 c& f/ [* v$ t
- False
复制代码
8 T" Q3 C3 L: ?& Xmath.isnan(x) 如果x不是数字True,否则返回False
3 c2 Y! s; }7 V0 D. s- #如果x不是数字True,否则返回False
) l% Z3 ^8 p! B. [. D# M - isnan(x) -> bool
V3 Q# P( d- \ v - Return True if x is a NaN (not a number), and False otherwise.
2 t9 W" I5 d# R0 I0 `& Y) Z - >>> math.isnan(23)" N9 U, A3 c0 [" l. r6 w! D
- False
o4 a8 o. {7 }1 A y6 A9 G$ H. e- { - >>> math.isnan(0.01)
5 y! }5 ~8 C% H - False
复制代码
7 `' B; _/ R) I0 V5 b0 N5 K' \math.ldexp(x,i) 返回x*(2**i)的值3 L1 N6 K9 I# z/ G7 h$ ]
- #返回x*(2**i)的值
8 @+ Q# k, v8 D3 J4 q4 a, k - ldexp(x, i)5 o! T; T3 b- N* `" p
- Return x * (2**i).
. J) q9 F7 q/ C+ A3 B$ @1 t6 n - >>> math.ldexp(5,5)* g/ p8 v0 w8 l- E
- 160.0
; W9 J/ `+ W6 y - >>> math.ldexp(3,5)+ }. W' }+ h9 |5 \7 }5 `
- 96.0
复制代码 + C7 H# Z/ \) b$ k, v
math.log10(x) 返回x的以10为底的对数4 h" H5 r6 d. ~6 V/ N
- #返回x的以10为底的对数' V" ]" N- {' e+ m4 }# I* b$ A
- log10(x)
1 }( D7 {6 C$ ]! r) X$ w6 n - Return the base 10 logarithm of x." j* m* R T! C2 q, _
- >>> math.log10(10)% A. z; I- |( V, i3 a
- 1.03 F' `# l4 K2 c7 T+ s
- >>> math.log10(100)
, }( Y1 N1 g0 n* c& y/ P4 _ - 2.0
) W/ r$ s% ^4 a9 @4 D - #即10的1.3次方的结果为20
4 h& W- q4 X/ J4 N - >>> math.log10(20)
, p2 k: |6 D) r0 B$ I1 v" S; G2 j - 1.3010299956639813
复制代码
M6 J5 T3 v3 l+ ^math.log1p(x) 返回x+1的自然对数(基数为e)的值- p- t( @; ?+ q/ {
- #返回x+1的自然对数(基数为e)的值
7 j7 R# S3 L0 B! I - log1p(x)
1 q- s) c# B0 f5 X - Return the natural logarithm of 1+x (base e).6 j+ P+ W6 H' y# O- |! k
- The result is computed in a way which is accurate for x near zero.4 f/ S# w( q. ~
- >>> math.log(10)" L" W4 |$ \9 F( d' G L
- 2.302585092994046
& g' B. H% \9 `9 k+ y0 ]3 z - >>> math.log1p(10)
; `+ B2 n4 t$ V+ W0 u+ H - 2.3978952727983707
& o1 l4 x$ n. B9 U - >>> math.log(11)
3 t8 u6 G1 A* J/ K - 2.3978952727983707
复制代码 8 o8 `4 R: `; o: f$ J( |* V
math.log2(x) 返回x的基2对数
' H# d* e# C. R- #返回x的基2对数
+ ^: f; H4 X6 [1 }& v# G" o - log2(x)
5 G. g9 b$ L; i - Return the base 2 logarithm of x.8 Y, V$ X, K8 F' M# Q
- >>> math.log2(32)& [: O# g) G, s5 ]; P
- 5.0 y7 y; |/ B* o0 q4 \) b) d
- >>> math.log2(20)" u; w8 D7 k7 \ }5 i; n) K
- 4.321928094887363% }/ c# C& y2 w" I
- >>> math.log2(16)
+ z7 t1 t" o7 N7 W8 y$ x/ t3 y - 4.0
复制代码 . e3 d3 }8 U& v! a
math.modf(x) 返回由x的小数部分和整数部分组成的元组
% X, Z! Y% e ^9 w- #返回由x的小数部分和整数部分组成的元组
5 x; b& \; a" Q: }- n - modf(x)! I4 n! u; V- E' l
- Return the fractional and integer parts of x. Both results carry the sign& j- j) u5 ?; f, n/ |- H- n
- of x and are floats.
; F" ^: |; }/ y/ A - >>> math.modf(math.pi): l( C1 U% j( u% r5 G. g7 S
- (0.14159265358979312, 3.0)& b, L5 \# ]. \
- >>> math.modf(12.34)
+ s, R5 G" V/ k9 M - (0.33999999999999986, 12.0)
复制代码
2 w H( F2 s8 A9 Qmath.sqrt(x) 求x的平方根
, C2 x& o: I6 C, q6 U& Z' G5 {- #求x的平方根7 V5 y# Z1 i* W: h8 c: ^
- sqrt(x)8 K3 u6 d1 }8 O* y* T/ Y' S Y
- Return the square root of x.5 j# I5 A8 m8 f
- >>> math.sqrt(100); t0 B1 }: u! K
- 10.0
A7 L% y1 K2 A j, Y - >>> math.sqrt(16) C9 } q4 ]1 Y0 ?8 ~
- 4.0. Q6 J6 e" X& U; P1 \4 }, K
- >>> math.sqrt(20)+ r. T, P9 g# e% G1 ^! d+ X8 ^
- 4.47213595499958
复制代码 j& L$ f( z. n7 a0 O- j! w, U/ n
math.trunc(x) 返回x的整数部分- #返回x的整数部分( m3 h- x# L1 y
- trunc(x:Real) -> Integral0 e1 k+ Y; y7 U% \" m- [* u+ }
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method. y- I% g) ]& O+ i/ p B
- >>> math.trunc(6.789)
, w6 H4 V5 x4 f: O5 I) a# h/ Z1 q) V - 6
9 B( k& r# O8 ^6 a! c5 r7 z - >>> math.trunc(math.pi)
2 N9 h* ?4 Z6 N' w; `: u - 33 Z i: _2 M2 r! v, c. ^
- >>> math.trunc(2.567)
}5 Q- ?5 ]" Y9 k- _3 ^: l( g1 U' I - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|