马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 T$ g2 c/ x9 g- Q9 j- l【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" d4 E) i5 s( ~# A; q) A+ s' d/ I. B$ w
方法1:+ M. \0 b* P2 S& G9 B2 y* `4 T. j# O
- >>> import math, K, @4 k1 q+ M; V' k
- >>> math.sqrt(9)0 k4 R2 S5 K2 f3 e7 [
- 3.0
复制代码 方法2:$ f, i; b1 E8 W; E
- >>> from math import sqrt, K& \' F& T& t% K
- >>> sqrt(9)
. N( N9 w9 K, {6 d3 o: g - 3.0
复制代码
3 r& |' o7 g4 w; m3 ]3 P$ W: u 2 Z% o1 |! g) ^! S1 b) S2 w0 X" s8 {
math.e 表示一个常量
( h1 W& T* {; p. p( j) D5 e- #表示一个常量
' C' z/ h7 S# t. S. a - >>> math.e# k! J E2 c" O+ ^
- 2.718281828459045
复制代码
. `9 O" K6 N1 m# B/ B; bmath.pi 数字常量,圆周率( s( X" o% a4 i
- #数字常量,圆周率 [1 p5 ^) l; w) ^ h$ E
- >>> print(math.pi)3 ?$ N! m0 [* g( x" `+ ]
- 3.141592653589793
复制代码
) P4 z) v9 n" L d3 Pmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 y0 l; a) e8 K. H4 V: X- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
( `$ S! J9 v9 y' \- O) a" {. B5 X - ceil(x). j8 K0 @! Y7 X) D
- Return the ceiling of x as an int.; A- u3 a8 J9 Q7 t+ P
- This is the smallest integral value >= x.
4 R) `6 ~$ X0 U( I
3 }' `; U' C2 t( d* K8 p/ q- >>> math.ceil(4.01)
" p4 j0 U X; [' o3 m5 Z% } - 58 S1 _8 R& h! T0 @4 Z3 H
- >>> math.ceil(4.99)
# D7 A! \5 ^3 n3 I - 56 j' z; _! q+ E" G( Q) h8 S3 D0 l
- >>> math.ceil(-3.99)$ K& z( `( @* Z
- -3
% f+ J2 z) L0 G - >>> math.ceil(-3.01)
: G1 N0 }( B w2 h' K3 \ - -3
复制代码
4 `) T- H4 Q$ c; \math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
B1 {4 t" Z/ E. R$ O! \- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: `- [# c! I! x' \4 B% @ - floor(x)
1 V% J9 ?* L! q. d: r - Return the floor of x as an int.
$ b8 k2 a$ H1 _: I - This is the largest integral value <= x.$ k! i7 k/ y( q4 G
- >>> math.floor(4.1)
- x0 a' ]' j7 G( D' `5 h - 4# @/ Z3 ]& h$ x0 e, _. h$ Z! \
- >>> math.floor(4.999)$ _4 D' R% C5 W. H; \; z' V
- 40 Y: z' d* \ Z, Y+ T
- >>> math.floor(-4.999)
- j8 `2 F/ K: s3 v - -5
9 ]+ j1 N9 z4 }; ` W( o - >>> math.floor(-4.01)5 w0 |" J0 D6 q* O3 C. b# }
- -5
复制代码 % P- d+ p t2 R% S7 |) E
math.pow(x,y) 返回x的y次方,即x**y
3 Y& J3 f2 z, B, j- #返回x的y次方,即x**y
. u( k& p) N1 C5 ?0 \* w - pow(x, y)
f3 |$ G' X$ `- ~6 j5 k - Return x**y (x to the power of y).) m$ _0 ~ |' c1 Q2 e
- >>> math.pow(3,4)4 E W: j4 x! x! D' ~2 w
- 81.0
M/ ]. T6 r7 k0 C4 { - >>>
( `% E7 _" \( c% y8 p5 F: t# U& R - >>> math.pow(2,7)2 O: L F9 z( J2 o# n) G: d
- 128.0
复制代码 0 u+ @. q( X1 H4 P2 q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 r+ b; ^, ?8 E- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# {1 @- u) u' r - log(x[, base])' t( Y8 |/ J7 J& e8 a' v
- Return the logarithm of x to the given base.
$ v4 _$ o6 x$ @4 V9 W/ G! U; t - If the base not specified, returns the natural logarithm (base e) of x.
% i0 B3 ^) P& Q3 J2 Y& D4 ?/ g+ G: A - >>> math.log(10)% P1 [' O7 K8 O- }
- 2.302585092994046) o1 a1 C* H" [5 W* ~
- >>> math.log(11)8 U3 i9 M9 D% d( j& c
- 2.39789527279837074 [8 R) }6 M# ` X7 P
- >>> math.log(20)2 A/ B5 I [( ~
- 2.995732273553991
复制代码 6 }6 y3 e* p* G) A
math.sin(x) 求x(x为弧度)的正弦值$ G4 p# r7 h- T9 j0 w( ]( E
- #求x(x为弧度)的正弦值
/ ~) v! K/ H" ~! {% o9 x" \& O* j - sin(x)% ]+ l1 B- f: {. @
- Return the sine of x (measured in radians).
/ X( U8 I: ]! M$ G* R - >>> math.sin(math.pi/4)- F- Z3 }# h; x* G
- 0.7071067811865475; B/ R; n9 Q! Q+ V; D( m' m1 C
- >>> math.sin(math.pi/2); {, p* ]# c& D$ H
- 1.0# }6 O' ^0 P$ V* v, W% @, y3 K# @
- >>> math.sin(math.pi/3): g z0 ?1 G3 H- h5 w- h
- 0.8660254037844386
复制代码
/ @' Q8 ^$ A' M* M; \math.cos(x) 求x的余弦,x必须是弧度. R) I. N) W0 G* ?$ k& D$ ? Y
- #求x的余弦,x必须是弧度
0 b5 m |* G) }6 ^, j4 S' `, u - cos(x); a1 w0 k3 f% p1 r2 w% G
- Return the cosine of x (measured in radians).: t7 t1 O9 D% W8 k1 n+ D2 B m
- #math.pi/4表示弧度,转换成角度为45度
& X1 Q4 m2 P3 h: p1 ? - >>> math.cos(math.pi/4)' |; D) R+ S- x* G
- 0.7071067811865476
& P% a0 E# @3 y' a5 ~ - math.pi/3表示弧度,转换成角度为60度$ Z/ D5 T" m; q+ q& h
- >>> math.cos(math.pi/3)
. U* L: u1 j3 J! q! d7 q - 0.5000000000000001; @0 }+ M0 _6 q
- math.pi/6表示弧度,转换成角度为30度( G/ z+ z; t6 [# J- J0 _1 L
- >>> math.cos(math.pi/6)
$ _3 Y0 ]) w6 J" m. E, r" g2 _ - 0.8660254037844387
复制代码
; }$ [% B j1 h/ t9 A0 s6 Imath.tan(x) 返回x(x为弧度)的正切值
# Z* ?# l2 T/ P) k& P% y- #返回x(x为弧度)的正切值3 V! _, N/ h6 `, l7 ^( n
- tan(x)
. s9 r* S" s0 m5 R x - Return the tangent of x (measured in radians).
* ~/ F3 ^ r; j( M, ^/ c9 O$ ?( q - >>> math.tan(math.pi/4)7 \3 A, V; p' q
- 0.9999999999999999
* z+ I& w3 V l: J+ @ - >>> math.tan(math.pi/6)
/ `6 R! h7 U, m7 a& {. W - 0.57735026918962575 O2 Z p. E3 \8 J. n. B4 }
- >>> math.tan(math.pi/3)% n( Z2 L$ C! A. G( G |( E* u& S
- 1.7320508075688767
复制代码
5 b ]) a$ {' Y( \3 lmath.degrees(x) 把x从弧度转换成角度
+ Z0 T) R) N k. n! X- #把x从弧度转换成角度3 M# H& M: d6 K/ f% b- `
- degrees(x)
2 c% s6 o+ I/ ]. g - Convert angle x from radians to degrees.
' F2 Z, ^! R+ i! w9 y+ ^' L- y6 U* c - h) J/ E% N+ {) B
- >>> math.degrees(math.pi/4)
* h# n9 Y$ x9 j' {5 v% Q8 t - 45.0
/ N/ O, Q# E4 O" \/ ?7 i) v - >>> math.degrees(math.pi) p0 f3 o) j9 p+ R
- 180.03 \- |/ h% j5 x2 m
- >>> math.degrees(math.pi/6)
! l9 S# I M; ^, x) ? t - 29.999999999999996
( _5 o+ B: s5 V O3 e* M2 a6 S" b - >>> math.degrees(math.pi/3) d! l- Y8 D8 B$ ~: [2 ?3 u- G
- 59.99999999999999
复制代码
0 W% p: I$ ~% g" r, F: dmath.radians(x) 把角度x转换成弧度
0 U/ Y* T# ?. u0 ~7 N# p- #把角度x转换成弧度
3 n5 l/ T [$ f$ S+ i/ K! _2 } - radians(x)
4 R, x1 a4 @+ V" b( |" h - Convert angle x from degrees to radians.# }5 p. W* t+ f. ^- v! |
- >>> math.radians(45)
/ e: X/ B% q9 H( s# j1 I* a - 0.78539816339744830 R6 j0 {0 t; ^/ @; j
- >>> math.radians(60)
. B2 c2 T% N8 _5 i& r" P$ ] - 1.0471975511965976
复制代码 3 r& m+ e7 E' X" B8 I9 b
math.copysign(x,y) 把y的正负号加到x前面,可以使用0 `- T0 g3 Z% L6 r
- #把y的正负号加到x前面,可以使用0
* y( ~' i2 ? S- ?7 X - copysign(x, y)0 d: n8 ~1 x4 p3 t
- Return a float with the magnitude (absolute value) of x but the sign
: F% G8 P$ X" m - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 v. Q- X- f1 `+ R9 b% L
- returns -1.0." s5 r2 ?* S! Q0 ] a
- 2 R- D4 ?2 Q6 D5 {9 d8 x1 m
- >>> math.copysign(2,3)
4 h: w% Z9 R/ m @; m4 T9 l - 2.08 q8 ^4 G5 @& K% U: ]+ A/ U8 L
- >>> math.copysign(2,-3)* }3 t0 U1 K# T) p% z" P! N
- -2.0
4 D" c: [$ S. f2 k - >>> math.copysign(3,8)
5 }# N7 v, l# r3 b5 d( b - 3.0
( |2 Z( s5 `% c. b - >>> math.copysign(3,-8). {# | T+ ~# c$ e7 l
- -3.0
复制代码 # c) [2 y" @4 ~. j! H, D& D0 ^
math.exp(x) 返回math.e,也就是2.71828的x次方) D+ X& Q2 Q' `; r$ y
- #返回math.e,也就是2.71828的x次方& p' h* o4 c; _7 N/ G6 _) p5 c
- exp(x)
1 ~7 V6 P9 o8 W! A - Return e raised to the power of x. a' U' p' t7 B9 w# z6 ~7 O
- y2 @( E6 z( R7 i1 w. o. e& Z. ~
- >>> math.exp(1)$ h7 }0 V% @' t) E3 ]
- 2.718281828459045
' |! h- E/ Q, C: H+ f - >>> math.exp(2), R3 k0 j! y) t! U" P
- 7.38905609893065
6 ]7 k& `# b0 A9 _5 j- G) U - >>> math.exp(3)
1 A: }5 o* z9 e" \ - 20.085536923187668
复制代码 ' @+ R p, [* E4 a. S! i
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减18 |2 M; c1 C* e/ b$ g
- #返回math.e的x(其值为2.71828)次方的值减1
+ t" n* j. Y2 }5 L2 D5 {/ K - expm1(x)
1 g$ j3 P% v' U/ S6 n2 m5 D. T - Return exp(x)-1.
. P* V2 y2 `, X7 v% Z7 i: t. `/ ]+ a - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
4 X0 N: b. M- q# l
5 D( ]$ ]. V7 f* z$ D- >>> math.expm1(1)# m6 P$ @! D2 a3 D4 k: j
- 1.718281828459045
1 X7 C! z: K+ _# n" y* h - >>> math.expm1(2)
7 K& e$ S6 G: H& L" Z' W' z7 x - 6.38905609893065, r) }1 L8 q. O8 a1 B+ Q
- >>> math.expm1(3)* ]/ v# |; t: z' s
- 19.085536923187668
复制代码
5 D5 L) ^3 Q/ C1 R# Q9 L2 lmath.fabs(x) 返回x的绝对值2 v" e: s8 W$ V" ]5 I# a: B
- #返回x的绝对值
r- t: G& i; ~9 J: A - fabs(x)
) w1 Y0 x4 N. T8 P - Return the absolute value of the float x.
+ `6 e" S- q' Y2 w* R
1 q8 g& K- `5 k- O- >>> math.fabs(-0.003)4 _' y) U/ m; F7 A. b
- 0.0035 E) M1 K* [9 e9 Q R- B
- >>> math.fabs(-110) Q: l8 r$ e5 ~
- 110.0
, Z" R# {- c4 V! { - >>> math.fabs(100)
; d Z" g% B2 ]; A; _, y/ _2 ^ - 100.0
复制代码 * t4 l9 h2 }9 u# ?1 D% s9 Y0 `
math.factorial(x) 取x的阶乘的值
. a9 s- H$ I) ~- M- #取x的阶乘的值
2 D0 s" l. x$ f3 O! Y - factorial(x) -> Integral
5 p2 c: K' P v. d# P# N - Find x!. Raise a ValueError if x is negative or non-integral.4 P( z7 X/ F2 V% i7 X0 I6 S9 `
- >>> math.factorial(1)# z% V0 X; V- m
- 1
2 j( r% v2 z {5 S+ l3 L7 h - >>> math.factorial(2)
" n7 Z9 X, r% Y& I2 { - 2: e/ r1 A, G8 f( w& Y) C
- >>> math.factorial(3); v& q5 ^% ?& ?! N$ w
- 6- S4 d, O8 N, `4 J& R4 D4 g% k g
- >>> math.factorial(5)5 y2 c! Q! }* l* Q% F
- 120: m) \ d5 p. k* m X3 f9 z9 }4 K
- >>> math.factorial(10)1 `( s" ^" ~) |9 c6 r
- 3628800
复制代码 " O2 w3 c7 ]+ C( U
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
" m) k" ]- f: J# I' v) Q- #得到x/y的余数,其值是一个浮点数
( j- T. w8 T# O - fmod(x, y)
5 {, `% f2 @( x& J - Return fmod(x, y), according to platform C. x % y may differ.
7 S1 ]. e4 u' A3 \7 s - >>> math.fmod(20,3)& L, y3 F0 B1 b( a: a J/ _, N0 y; ~
- 2.0
7 F6 s. N+ k* J' E# S/ s8 S% B1 w5 \ - >>> math.fmod(20,7)
* Y# ?& a% H! r- i" n: e( m1 B( C0 e - 6.0
复制代码
* `! I% j. I, L* O5 E" I' Smath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
* }3 [* ?0 d) {) j& R- M% c+ d- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
, V! S+ T2 X/ j - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
* s! c7 {: |3 j3 A( B0 ? k; l - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 y3 y% q0 u$ l' d; U - frexp(x)
( {, B2 h8 p; X6 U m- o5 \$ A - Return the mantissa and exponent of x, as pair (m, e).# b& P2 x& l% w5 D( I
- m is a float and e is an int, such that x = m * 2.**e.
4 {6 u4 G% E) U7 h+ n - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
8 \" e* Y: `$ {- o - >>> math.frexp(10)
; r; w+ ?9 C3 U* G/ h, V( ^0 H' x - (0.625, 4)/ j* R2 E/ u2 {9 j" _' T
- >>> math.frexp(75)# a; b2 W: o& k# @5 I" k
- (0.5859375, 7)
8 `& f4 I6 M! k7 h x' z - >>> math.frexp(-40). o- O/ g' w: k8 d) F
- (-0.625, 6)
% H4 E7 y& Y; p7 ~ - >>> math.frexp(-100)0 |7 @( Q0 L% ^. e+ \' ^# \5 T* C
- (-0.78125, 7)+ Q( c/ {3 U2 V) r1 b! t& D$ U% i
- >>> math.frexp(100)5 F2 G! B& v( I( |- q) O
- (0.78125, 7)
复制代码
7 \8 C7 Y* N/ C* Jmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
6 L8 M7 x$ N y- d& M1 z- #对迭代器里的每个元素进行求和操作
4 H- b! c, h: L* I, _8 F* m; U - fsum(iterable), p; M; a3 v- [" k& E+ ]7 F
- Return an accurate floating point sum of values in the iterable.6 e! G( U( z0 g
- Assumes IEEE-754 floating point arithmetic.3 B( T3 R: `( ?6 e& ^
- >>> math.fsum([1,2,3,4]); l+ ^" X6 o. `( C
- 10.0
4 h) F" I& l2 w! T, V/ @, G - >>> math.fsum((1,2,3,4))1 y; A. {5 J, r' i! V1 m
- 10.0
' V4 \! p' m# @% ?1 ^ - >>> math.fsum((-1,-2,-3,-4))
9 x5 d8 a# p# \1 @ - -10.04 f% l& N2 P' U6 w- w
- >>> math.fsum([-1,-2,-3,-4])
" {; l) [) o7 l5 f: { - -10.0
复制代码 . K' ^! a6 z o5 _: B5 f4 V8 K
math.gcd(x,y) 返回x和y的最大公约数
2 ?5 H8 U- ~5 A$ L! G1 d0 T4 J- #返回x和y的最大公约数" V- s* D" G( Q% n
- gcd(x, y) -> int" k. N+ s& a0 i" I" ], U' i% O
- greatest common divisor of x and y
: F$ N9 `( @& U! z7 N2 M% K1 u - >>> math.gcd(8,6)+ D& w) Q( `$ t3 `# _
- 2( I' i5 q; B8 k& t( O6 E) J+ U
- >>> math.gcd(40,20)% Q9 e9 O# A$ L2 D o
- 20; N- O' H' T# v6 r6 \! g
- >>> math.gcd(8,12)
% \4 |0 C& \3 A - 4
复制代码 2 Y) U" a4 A5 p' ~; m+ a
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False: ?; H0 z# B3 W' H7 X
- #得到(x**2+y**2),平方的值
0 N" P! d7 }+ ?) Z6 V+ x - hypot(x, y)
1 f( j9 l7 R/ L0 Y# n" K7 {2 O6 t - Return the Euclidean distance, sqrt(x*x + y*y).6 l" Y7 ?- |; m, I
- >>> math.hypot(3,4)
* E) H! e5 ^1 I* w" ^ - 5.0
- |5 R) Y3 q } - >>> math.hypot(6,8)
5 M9 ^* V) ?7 A% |+ P - 10.0
复制代码 8 X) @# v( f: ~% t7 k
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False m# h z1 Z8 i. d) Q% @( ^6 T
- #如果x是不是无穷大的数字,则返回True,否则返回False
! x; `, E1 Y" c- _+ V K - isfinite(x) -> bool' A3 i" ^! B u- C8 ?
- Return True if x is neither an infinity nor a NaN, and False otherwise." b3 g- |- i: U8 E$ I
- >>> math.isfinite(100)
O/ @: D1 c% r* r: t4 o - True
) w1 C4 |5 A/ S) o) v6 W- ` - >>> math.isfinite(0)6 ~* H& v+ |, x' s$ Z* R) t
- True6 e7 C" H3 i/ }9 i2 D- B6 b
- >>> math.isfinite(0.1)
3 V1 q# s6 @" S" X" M# c0 v - True
1 W" T' Z+ U9 P0 S, }( ?( l - >>> math.isfinite("a")3 T5 h `9 e$ l; f9 r4 k0 [0 ^1 p
- >>> math.isfinite(0.0001)
# \9 T$ T/ r; N2 _ - True
复制代码 & v3 g C+ Z3 z* {, i9 @; o9 Q6 Z! z
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
# a1 L/ o& H6 U' \. K4 \- #如果x是正无穷大或负无穷大,则返回True,否则返回False
* V9 c& j, [6 ?, U; j, ?: A - isinf(x) -> bool W6 S" Q5 ]7 [
- Return True if x is a positive or negative infinity, and False otherwise.' d$ T: X% h; E& ?4 A# W
- >>> math.isinf(234)
2 B. e6 j5 z) S. H v* \9 ~" n$ A7 U - False* K" j# q$ Q+ }& _, ^" X
- >>> math.isinf(0.1)
& m+ _& f2 }/ F- _ - False
复制代码 & Q$ R! j/ e% `
math.isnan(x) 如果x不是数字True,否则返回False2 q2 K# r7 ~8 Z: [$ K
- #如果x不是数字True,否则返回False
- Q5 X) E7 V0 }1 s - isnan(x) -> bool* m0 p! q6 s( u% s% G( u- k2 P
- Return True if x is a NaN (not a number), and False otherwise.
5 Q4 s' |" \! y& R: h K& z - >>> math.isnan(23)1 o5 y& o: w; F6 ?$ W
- False" v @& D+ _9 ]' e% Y4 S! e1 U
- >>> math.isnan(0.01) E0 Z v) `& ~% Q$ E E
- False
复制代码
+ | f( e1 _- W8 d/ ymath.ldexp(x,i) 返回x*(2**i)的值, Y8 _0 U0 G- Y4 M
- #返回x*(2**i)的值) T- u. j3 P1 V: s/ q" U- N
- ldexp(x, i)
' J6 A2 u- {* A# B - Return x * (2**i).& m$ d' X- p; W+ u4 ?
- >>> math.ldexp(5,5)$ q* I+ U V! c# I) c
- 160.0
' C" W" w2 T5 f+ v8 K6 j - >>> math.ldexp(3,5)5 ~7 d: M% u% O; s/ U
- 96.0
复制代码 8 Z3 Y1 y- q8 M: s& x0 x: |9 Y
math.log10(x) 返回x的以10为底的对数; ~" ?, b7 d% n- y" n
- #返回x的以10为底的对数
: d+ ?5 y4 m9 H' G% H% ?4 j" ] - log10(x) i: [. w T/ d% C# Z
- Return the base 10 logarithm of x.* _1 X M/ v/ N9 g5 G
- >>> math.log10(10)
% Y4 b% W* X* w/ U% ? - 1.0
3 @( x3 T; d; ^7 I# ?% \ - >>> math.log10(100)
1 F, W# ^: B! a% D! d - 2.07 N) F! E) ^1 V+ W/ L; \
- #即10的1.3次方的结果为20
4 g: X) M2 }: M8 Q/ Y b- | - >>> math.log10(20)) {3 o5 ?. m8 H) I' i+ ?+ J- s' q
- 1.3010299956639813
复制代码 ; A# Z. V% V$ R$ K8 d
math.log1p(x) 返回x+1的自然对数(基数为e)的值
) w6 v0 e S, [5 n* K1 s' j/ R3 P- #返回x+1的自然对数(基数为e)的值5 ^4 M$ \) ?4 H6 \/ i0 W
- log1p(x)
3 r, s0 {1 {+ m& h - Return the natural logarithm of 1+x (base e).+ f3 a- E% @7 |& `9 k" T
- The result is computed in a way which is accurate for x near zero.
6 N, l# q3 F% m6 Y% o) `4 C - >>> math.log(10)
+ c$ Q& _" P* D+ s4 m. R' d, m b - 2.3025850929940463 s0 H. f2 y: f
- >>> math.log1p(10)
1 I, J" I( H+ z6 f - 2.3978952727983707
! N( Y5 @% b# f# t( | - >>> math.log(11)# q) l" s# P8 I# e
- 2.3978952727983707
复制代码 6 Z; e. X, }3 l3 v% Y1 r
math.log2(x) 返回x的基2对数 u4 ^- N/ Z) m U
- #返回x的基2对数& e' w, @3 ?8 f4 _ K- N+ L
- log2(x)' K: b) }7 M4 A. ]( v
- Return the base 2 logarithm of x.
2 ?7 h: {; _6 e" O' N - >>> math.log2(32)0 U6 V1 W& K1 q* {+ H0 z3 E
- 5.0) y# K4 Y( ?* N" X! h6 M
- >>> math.log2(20)9 m1 h3 r" N2 A! T$ k0 B
- 4.321928094887363
1 G& o+ n! B% I* ]2 z* B: g/ G - >>> math.log2(16)' ~& k7 J8 F+ _5 ]) N M
- 4.0
复制代码
5 {9 f$ _2 g: Z& `! t# t+ t; \+ j6 _. Gmath.modf(x) 返回由x的小数部分和整数部分组成的元组
, g; s ^; A1 H- #返回由x的小数部分和整数部分组成的元组
0 \6 X4 ?$ L' s. _3 E - modf(x)" k8 M' n! M: z" t# l
- Return the fractional and integer parts of x. Both results carry the sign- M6 m6 A% |3 x: m! B+ t/ y0 B
- of x and are floats.9 k y2 j' r% Y% d& _
- >>> math.modf(math.pi)
i% x1 i8 v+ q8 I7 W; w - (0.14159265358979312, 3.0)4 ~$ b2 ?% ^& e+ F
- >>> math.modf(12.34)% a3 [: {( z/ u: }+ w" W9 E
- (0.33999999999999986, 12.0)
复制代码 6 \/ ` N7 b; A5 L B* N V
math.sqrt(x) 求x的平方根
- i# J% u0 I# @- #求x的平方根! {9 j4 |: K4 W, _( Z! ?7 @( m \
- sqrt(x)- H1 x% _* S H5 D- c) o
- Return the square root of x.
+ Q# e/ Q- D/ J! k - >>> math.sqrt(100)7 K" s7 r2 L; d+ Y/ S3 y% C/ z
- 10.0
8 m- _9 S6 P- p% s. p. @ - >>> math.sqrt(16)
5 \0 r4 S- N' P' K) x& `1 K6 I6 u6 L - 4.0
' A& G$ {- G: Z. d - >>> math.sqrt(20)
! m" M7 n, {% R' E- F6 O7 B. h% Y - 4.47213595499958
复制代码
) y" f; O) Z& imath.trunc(x) 返回x的整数部分- #返回x的整数部分# d4 r0 O0 ?! X# i- U" W' ^: `
- trunc(x:Real) -> Integral7 F) w, V' C& i. b9 j! k# r
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.+ i+ Z% v0 A4 l8 e- v
- >>> math.trunc(6.789)8 R2 X8 a9 h" B1 H2 s
- 6( y- f6 F' @. u" S3 W
- >>> math.trunc(math.pi)" R! }% d3 {, f& f) b S( M0 @
- 3
, [- a& Q0 w; X) x/ f3 V% e. n% b, y - >>> math.trunc(2.567)- n' o, F/ E! f" q, w
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|