马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* @9 E2 s$ T$ V0 @4 P【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。+ r& v6 m4 Y0 `5 A e
5 ]/ g$ z; U6 p8 w, F8 M* m, A方法1:1 ?% U! ~; ^* p5 T/ h7 C
- >>> import math- T2 `+ m, Q( |7 H: |) z
- >>> math.sqrt(9)
' |# V- M [, M: e7 ~ - 3.0
复制代码 方法2:
7 S0 ^# [9 E! i3 M+ G Y# U- >>> from math import sqrt# l7 h* b6 ~0 o6 l
- >>> sqrt(9)
9 v3 X K" i0 J( F5 U - 3.0
复制代码 ) o0 c4 E; V- j! X* a0 s u
# W6 R0 X1 u; i! c6 S; m. L2 d. E T
math.e 表示一个常量4 Y' _0 G3 Z9 _) b9 D) F8 J) Z6 i
- #表示一个常量
6 H( _1 J- G8 ]3 k1 N: x' V - >>> math.e
- d: P/ t, ~2 ]' ^& T" Z - 2.718281828459045
复制代码 7 q# U. C& h$ N7 K. w
math.pi 数字常量,圆周率; \) d( ?, F7 f- f a
- #数字常量,圆周率' I' [: p i( H$ I0 f( Z* C- \0 k
- >>> print(math.pi)7 g5 o3 O5 a/ w; v, ]9 Q6 Q
- 3.141592653589793
复制代码 ?, [3 f* O& O; V: V/ y
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x. b2 ?$ E! [+ l B
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x0 j* n r y. {5 D4 N4 t
- ceil(x)
* ?& k1 f2 \' p! ?+ M' \1 `! K - Return the ceiling of x as an int.- S- ]0 u# ~: r9 c" ~2 f3 G* g
- This is the smallest integral value >= x.
; ? [5 p. m' V1 v - 5 m; I+ K, ]+ F- W
- >>> math.ceil(4.01)
' r$ P7 N! V1 s- A - 5
$ b' A8 M0 }; m. |( h - >>> math.ceil(4.99)
( _- |6 H* O+ a - 56 G/ d. ]/ ?: I& {$ Q; b
- >>> math.ceil(-3.99)
" A5 K6 d P/ a- N( h - -39 x5 r# w5 K' v# {8 }$ g! U: Z/ a
- >>> math.ceil(-3.01)& e3 z3 I4 h( p) N# [' e5 F
- -3
复制代码
: X, n: V/ P. b: J: H" Pmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ H# ~" _( t- r- w! }& b- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
D& Z! T1 x6 |9 t+ M. Q% s4 P# b1 u - floor(x)5 q4 g7 D# G/ {) d7 m
- Return the floor of x as an int., d3 `9 e5 ~5 P3 y3 y
- This is the largest integral value <= x.
, t2 `2 m9 r' o; D8 N - >>> math.floor(4.1)
8 B( p5 G: `% O" ^$ G" A5 _3 X7 J - 4# U. r5 h5 |9 X+ s9 H8 u3 n
- >>> math.floor(4.999)
( y* ?8 {; x4 h' M0 Z - 4
) v9 |9 H* c: n+ f0 S4 C+ N0 C - >>> math.floor(-4.999)
) G% ]) x, R; ^/ P4 `" i9 e - -53 v7 n* u& m3 i& ]% }9 K( |
- >>> math.floor(-4.01)' a# t0 e7 y8 A4 L! ]5 B
- -5
复制代码 1 D5 B4 n1 x& U" O1 Y
math.pow(x,y) 返回x的y次方,即x**y
! q- |0 @ e9 `/ N$ e6 M- #返回x的y次方,即x**y
) G5 f* U2 P- p! Y! M - pow(x, y)
3 I, P; d( j" X, V8 w; g+ R0 T - Return x**y (x to the power of y).; ~+ B! c6 M* ~* r' W& n' p
- >>> math.pow(3,4)+ w5 q9 K. [: l/ G9 k0 J
- 81.0
( N$ C) f/ d, F - >>> % h! I" N4 t2 ?$ Q3 j5 N6 t
- >>> math.pow(2,7)
9 }' |# F. N, k% W6 e: c - 128.0
复制代码
. X) f% M) y& q0 z5 s9 q$ Q, W) b5 n; Imath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 G; @# ]) P, p) b0 Z- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ c% p$ T. `$ r" p' K - log(x[, base])5 B) N- _1 {& ]' f" \
- Return the logarithm of x to the given base.$ O+ s3 H- g6 e7 k& I% s8 a Z
- If the base not specified, returns the natural logarithm (base e) of x.6 _+ i* P" U% @' t5 ]: a
- >>> math.log(10)
0 L( C7 b7 ?& G5 ]! H8 R6 u5 ? - 2.302585092994046% s/ s" d% g$ l
- >>> math.log(11)
$ `% G9 a# V' N& ]! F - 2.3978952727983707
4 U2 N o) E- K3 t* t$ A# J' a - >>> math.log(20) J0 h" o/ W/ u$ n3 Q
- 2.995732273553991
复制代码 8 B* y6 D7 J: |. y: D' |8 i' l
math.sin(x) 求x(x为弧度)的正弦值$ E9 q% h, `0 C$ M8 n' ^
- #求x(x为弧度)的正弦值
$ y: x$ J% w6 D - sin(x)
" \ k: V9 @8 h - Return the sine of x (measured in radians)./ Z1 M* e0 D# ?% N
- >>> math.sin(math.pi/4)( N3 z4 q2 w; K8 d
- 0.70710678118654759 h) k6 D1 [5 e! F/ ?8 A! M, y' m
- >>> math.sin(math.pi/2)
0 N+ q' r# ^; c7 a2 A- p - 1.0
; [( O+ A2 |7 {7 p% a2 n( V. R @ - >>> math.sin(math.pi/3)
1 m5 f+ C& d; x: W( V - 0.8660254037844386
复制代码 ; O# X9 i5 P& ]: \9 l4 i4 S* H
math.cos(x) 求x的余弦,x必须是弧度
& b2 I& }, M1 s; D: v8 N7 ` g- #求x的余弦,x必须是弧度5 {& B9 I8 ^* ^; W$ [: y1 f
- cos(x)) g% m- S: O) `! ?6 Y; `0 b2 C
- Return the cosine of x (measured in radians).# E' d: ~& O6 v' N! T" h7 t9 P5 M
- #math.pi/4表示弧度,转换成角度为45度, v1 s2 n H! S+ [
- >>> math.cos(math.pi/4)
& C+ Q& i, {6 Q7 T - 0.7071067811865476
! r; Y$ K/ v. d1 ^ - math.pi/3表示弧度,转换成角度为60度, g9 B: d$ \ @
- >>> math.cos(math.pi/3)
4 ?) U7 U6 u( _1 i/ P! J# ~/ F) i - 0.5000000000000001
! S9 }3 n" G$ T+ U' ?( F - math.pi/6表示弧度,转换成角度为30度& M5 l: J0 N; ~# C
- >>> math.cos(math.pi/6)% O7 Q/ K: I7 [0 z0 B- f4 Q
- 0.8660254037844387
复制代码
9 e9 m! U N$ I) {. @$ l; N! R$ G* mmath.tan(x) 返回x(x为弧度)的正切值
7 V# u; P0 u0 ^5 C, e3 k- #返回x(x为弧度)的正切值
. f* R. C. |. L3 m1 W - tan(x)
" U1 k: E* h- t B* F- k - Return the tangent of x (measured in radians).$ T# ` ?* I6 {
- >>> math.tan(math.pi/4)
+ e! Y) y1 B1 U' N* I9 l9 F2 o- q* V - 0.99999999999999990 y* \( C% V8 F% Q: u
- >>> math.tan(math.pi/6) s9 e: d6 q* c) U5 @% O# M( y
- 0.57735026918962574 D% I' N& K4 T/ E8 S
- >>> math.tan(math.pi/3)2 @7 |) n" m( W* y0 N t
- 1.7320508075688767
复制代码 ! n3 \$ [$ g' ~( w8 I
math.degrees(x) 把x从弧度转换成角度: `+ s. k* J1 v3 r, d
- #把x从弧度转换成角度
% J8 c$ O, L ? ?9 C `6 {! y& X - degrees(x): X* i$ c5 X/ U8 D
- Convert angle x from radians to degrees.
4 ~ r, T5 [4 D# P: ]) x( y' n* Q
$ d% J- x# R- C# Z j1 t- >>> math.degrees(math.pi/4)
% I$ v5 Z( S& _, m" L2 T4 B* `7 T - 45.0
4 A3 X' h X4 w! w) C! G - >>> math.degrees(math.pi)" d* X! B" H! @8 D! e
- 180.0+ y5 k X& g8 |) K
- >>> math.degrees(math.pi/6)
% H1 J( z/ c8 ]9 i/ h - 29.999999999999996
7 C& f4 K8 x3 M( o" k8 C - >>> math.degrees(math.pi/3)
, m S- W) j2 ]4 p - 59.99999999999999
复制代码 1 m' R% g& a5 X0 d0 ~ _$ y9 @" v
math.radians(x) 把角度x转换成弧度
& [) \; ^" Q+ X$ c# X- #把角度x转换成弧度$ C5 S1 O) H( o. i) F
- radians(x)7 |& `- I1 y# D: E' b! y- |
- Convert angle x from degrees to radians.2 a6 K E7 R6 ~" l, H% T; q
- >>> math.radians(45)8 J9 G4 S' f% g; k) F
- 0.7853981633974483
- \& k a; B/ G0 o, k. l. p- |4 k - >>> math.radians(60)8 w4 W% U, r1 u1 P
- 1.0471975511965976
复制代码
+ r* u+ [6 K( wmath.copysign(x,y) 把y的正负号加到x前面,可以使用0 S6 F6 u; ]% ]* v m# S$ q
- #把y的正负号加到x前面,可以使用0
6 q% E3 `% L l6 {' P, N - copysign(x, y)
# [3 B# @. `5 v& J" s - Return a float with the magnitude (absolute value) of x but the sign
! E @. W1 f# v8 ^3 v - of y. On platforms that support signed zeros, copysign(1.0, -0.0) " L- {' T4 @& j* C2 Y$ |) [$ [
- returns -1.0. b* W; b; a; R3 q2 P, k
' n5 v5 o6 `( _; T( b- >>> math.copysign(2,3)
1 e# f) }' s0 A - 2.0$ r X( w( ?) V7 Z
- >>> math.copysign(2,-3)
. N" A x2 ~( Y( t - -2.0
1 m" i2 w; j; H. ?5 }7 ~6 c - >>> math.copysign(3,8)0 O4 A9 j8 y/ n& K, B8 D/ V
- 3.0
4 x+ {8 F7 C, f7 ^9 X - >>> math.copysign(3,-8)4 W! i+ q) X; S. T
- -3.0
复制代码
) c( l- M% R2 w1 r pmath.exp(x) 返回math.e,也就是2.71828的x次方
( E; ~8 q/ i8 \/ X S. L- #返回math.e,也就是2.71828的x次方
$ j! i8 Z2 B$ N) S( X; ?& D - exp(x)! _2 _1 O, k- `
- Return e raised to the power of x.
4 [, i& y6 Z* P/ Z- X# M+ {# ]& \ - 5 \+ ` ~7 z: s$ ]# i t
- >>> math.exp(1)& {6 y) ]# Q' `' W" R& e, A( U B
- 2.718281828459045
3 M4 r! s0 ^+ i& u - >>> math.exp(2)* @1 m: c' x8 o* e/ w
- 7.38905609893065
4 K# H% e1 s2 I/ L T! n - >>> math.exp(3)1 B% O/ {* u; Y! ^/ m5 j& o4 y/ W
- 20.085536923187668
复制代码
. h' V& c6 T+ z" d& _math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
! b- B7 C" s3 a( E. X$ a; D- #返回math.e的x(其值为2.71828)次方的值减15 J- H! w1 T; |; _8 V" E- `
- expm1(x)
# J3 T& _1 C8 i% M; J; q6 h5 G4 Q - Return exp(x)-1.* h7 e! ^5 \# q5 s b
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.1 l! ~' n5 R% z/ A" j; e$ N' h
- ) o$ q$ a0 |9 q# { P
- >>> math.expm1(1)* T- Y( i$ `6 {& ^4 H
- 1.718281828459045
. V3 r F5 ^+ i0 D/ G3 [ - >>> math.expm1(2)1 l4 g& ]& a0 u% {8 r3 e& G, a4 i' H
- 6.389056098930655 w$ {6 Y1 w: v& |
- >>> math.expm1(3)+ g; ^5 m- j- G* B4 W, z8 X
- 19.085536923187668
复制代码
6 P0 _) _, C2 c5 Kmath.fabs(x) 返回x的绝对值/ n& Y- C& D# s/ a# M c! u, T
- #返回x的绝对值 M. T+ N! v, P6 y
- fabs(x)
0 ~0 E- Z/ D* e - Return the absolute value of the float x.3 o9 H9 B1 x, ^( X9 M! Q4 A
) J2 D0 [8 C8 |9 B, K- >>> math.fabs(-0.003); `, P/ M: k1 U) o. X
- 0.003
2 R/ A% S0 r% e7 ~+ |! d - >>> math.fabs(-110)
$ x. r$ ?' e) x4 W5 ~, @( S( J - 110.0. ?% K4 Q7 V0 O6 `) R4 L
- >>> math.fabs(100)
$ e' T1 T" f o5 k. j9 j* w( Y6 G - 100.0
复制代码
2 Z5 q! K+ g& Z2 s( pmath.factorial(x) 取x的阶乘的值
: w' U0 |. Z) U2 q- #取x的阶乘的值
c9 L/ J' Y w; l2 o9 B/ y( b- g9 m2 b - factorial(x) -> Integral$ u) H* z9 f L1 b3 C
- Find x!. Raise a ValueError if x is negative or non-integral.
5 K# y7 h& K% F% r( L' G - >>> math.factorial(1)
: T4 K1 _/ ]+ v# n( G - 1$ [1 ]8 |! I7 P1 u
- >>> math.factorial(2)4 b! G( e: E$ v9 I# e
- 2
" m! q- V$ \) K+ o - >>> math.factorial(3)
9 D/ ^: F9 X( Z - 6( r# o& c5 I& E- A
- >>> math.factorial(5)) v; x7 y: ]0 U+ K* W! A: G. m% i* J% k
- 120
& ?& b( X' h) F& @1 b* h+ Q: J R - >>> math.factorial(10)
8 ]) S5 p6 t0 S- S - 3628800
复制代码
}' l% G, T; }math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
1 ^) r) ]. f6 N X, d6 k- #得到x/y的余数,其值是一个浮点数
# }8 k- b; J( w; Y1 t2 p m4 P - fmod(x, y)
7 |- E1 T. k' i - Return fmod(x, y), according to platform C. x % y may differ.
7 Q) v \ y0 \, g: G - >>> math.fmod(20,3) A/ U# w$ H! ^9 @1 k$ [" m
- 2.0. @; n/ n2 N4 ?$ c' s2 s
- >>> math.fmod(20,7)
7 d: ~1 d* a, u4 D+ @5 q6 b - 6.0
复制代码 ( W7 C5 z7 ?. Q
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
1 e' t( u7 ^0 {* I+ d+ K- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,, w0 M2 K5 i- |" ~* B0 ~
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
$ o2 T) k! |" G - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- ^5 K% e! [9 J. R7 o u& \
- frexp(x)
% A+ `) Z0 `2 F: G6 c' g - Return the mantissa and exponent of x, as pair (m, e).2 F. s- m$ [% M$ ~/ b2 M
- m is a float and e is an int, such that x = m * 2.**e.6 x1 V% E5 q5 |+ v. `; q
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
6 _- z" H3 s( v0 Q7 I5 h% b - >>> math.frexp(10)
4 A" L) F# K# w8 Y - (0.625, 4)
- E& L0 ?! k' k: C: f) Z5 C6 s - >>> math.frexp(75)
- [- [) J' r: }% Z - (0.5859375, 7)9 } T/ ^7 {: u. \' `" W
- >>> math.frexp(-40)
. ]( M+ R* p, o2 | - (-0.625, 6)1 w5 |1 X! _: q/ B
- >>> math.frexp(-100)
1 ]7 ^2 U/ _9 X2 R1 Q - (-0.78125, 7)
, T8 ]& ]/ Z$ j7 a' w+ z4 J - >>> math.frexp(100)+ N9 e( L: i( L3 |; \# P8 r
- (0.78125, 7)
复制代码 # _, Y( @$ }8 E" z8 Q1 Q; I
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
d: L: c3 {( I7 @1 ^6 E: m3 z- #对迭代器里的每个元素进行求和操作
" E/ l+ f" x2 i) Y$ w& l - fsum(iterable)& p' m% d2 t7 n9 {+ r- f) j
- Return an accurate floating point sum of values in the iterable.5 M: c, N8 l0 ^
- Assumes IEEE-754 floating point arithmetic.
7 O( h/ P2 q% @4 r - >>> math.fsum([1,2,3,4])
( S0 c0 F! ]+ y, e* j - 10.0
# S. s0 {- r. J) T - >>> math.fsum((1,2,3,4))+ h2 d7 M) C w/ s1 P, d! o
- 10.08 T* E/ a- n; s* Q
- >>> math.fsum((-1,-2,-3,-4))' a) V# {- m, }. {
- -10.0
% S- f% h! W1 L' v" R- i - >>> math.fsum([-1,-2,-3,-4])1 o# f, b0 A' W; G9 L
- -10.0
复制代码 % s# t, a9 T# m9 ~
math.gcd(x,y) 返回x和y的最大公约数, [- v* n B; f9 w2 F. R
- #返回x和y的最大公约数) { M# t4 f, v) E+ t. Q+ m$ Z
- gcd(x, y) -> int
% `: D( |/ Z$ i f% E: I+ r - greatest common divisor of x and y
: b9 [7 j, z. I5 j! T - >>> math.gcd(8,6)
% s8 _7 n" U N/ }# s3 p - 28 e3 l. J* O3 |& W
- >>> math.gcd(40,20)
+ R' e: K5 c" z, ?2 c; A - 20
2 I7 H+ b0 y) I. J6 G# b8 D/ I, K - >>> math.gcd(8,12)) l @2 e9 M+ b: P: w1 v' A
- 4
复制代码 % a) q9 `& ?# d5 E6 l
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
2 ]0 r4 W* _, j9 d. _ Y- #得到(x**2+y**2),平方的值1 D7 c8 K- ^+ f% Q2 M9 r. T* m
- hypot(x, y)- F) i8 [3 x; Z5 Y( B* b3 T2 d
- Return the Euclidean distance, sqrt(x*x + y*y).
3 |; k T8 G/ x+ w$ e9 n4 p @. k1 M - >>> math.hypot(3,4)5 t7 a/ L$ E# Z$ Q
- 5.0( y* s( D* v k$ b8 r
- >>> math.hypot(6,8)
9 P4 ^$ v% g T4 o) W, f' s" ^ - 10.0
复制代码 # L: h! G; O! w: o- [8 \
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! j# k& x6 n/ G6 D- #如果x是不是无穷大的数字,则返回True,否则返回False) Z: H; U% i- G6 |6 k& O
- isfinite(x) -> bool* a" M7 @( z! V4 N7 _
- Return True if x is neither an infinity nor a NaN, and False otherwise.
) g. C7 w" v) d - >>> math.isfinite(100)
. l& B; r1 u- I2 G! d5 L8 Q, h - True8 B9 L4 H2 u+ M: E8 ?
- >>> math.isfinite(0)
" u, o1 \! L3 V; x - True$ N' P8 ?% w# K0 a L
- >>> math.isfinite(0.1)# t; C+ E0 P* }0 D4 s- p3 S2 Z9 x
- True
$ Z1 {: b4 H# O+ T8 n7 J - >>> math.isfinite("a")
% ^/ {& I E. F1 f - >>> math.isfinite(0.0001)
7 a4 R# z& {! b" g* D7 o6 X - True
复制代码 0 W, y. B3 z8 i4 x
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
$ I( R9 ]$ B5 B. E9 S2 [- p" g2 f- #如果x是正无穷大或负无穷大,则返回True,否则返回False
( R! ^! ?6 s, }/ ]2 u - isinf(x) -> bool( a V" u4 Q9 x6 n4 x! e
- Return True if x is a positive or negative infinity, and False otherwise.
% i, Y8 X) l: Q' ^ - >>> math.isinf(234); v' B3 f+ G6 V% k" l
- False e- F1 a$ y$ U$ v
- >>> math.isinf(0.1)
" Q U3 J" T& b: V) ? - False
复制代码 9 G6 F R! V6 ]) p; }' {
math.isnan(x) 如果x不是数字True,否则返回False
! N& q8 V, h' n- z! t& _- #如果x不是数字True,否则返回False u: E! m. p3 `4 T3 a/ A- F
- isnan(x) -> bool
2 w. e+ E. @% C - Return True if x is a NaN (not a number), and False otherwise.
9 Y3 M" r, v0 H" y# O. e; } - >>> math.isnan(23)* u* ]) ?3 M0 X3 n0 V6 w0 q2 i: [
- False
$ g- m( {8 ?0 Z/ W1 d3 s/ a - >>> math.isnan(0.01)3 q7 H+ m1 D' m
- False
复制代码
3 s4 [$ T7 f; J0 Tmath.ldexp(x,i) 返回x*(2**i)的值9 I: k" t9 w3 P) a. k8 ?8 |- W
- #返回x*(2**i)的值
$ R: {8 C( W; V( j' m8 G% d: j - ldexp(x, i)
/ I; m0 [7 t! p4 r - Return x * (2**i).
2 k- |/ l+ H; ~2 R" o+ h - >>> math.ldexp(5,5)
+ R) X$ E8 x) ]! C2 s - 160.03 G. W5 B; U" b N0 |
- >>> math.ldexp(3,5)
S: d. J6 s# [/ F: q - 96.0
复制代码
' u. [) y6 l* b8 |: pmath.log10(x) 返回x的以10为底的对数
, v/ o; | K- R5 f- \- #返回x的以10为底的对数
* [9 }( X) t, [ - log10(x)2 k: i1 c% r& j% \1 X
- Return the base 10 logarithm of x.
" f& n5 z9 g3 q2 H! i7 ^ - >>> math.log10(10)/ w7 b( X T$ b1 U
- 1.0
; f- d" O: M" r0 h2 \# s) `6 b - >>> math.log10(100)
4 \7 d) [* @6 w6 W5 [+ A: h6 O - 2.0
9 r2 }$ `+ w- }5 _' J; N' ? - #即10的1.3次方的结果为20
& d1 S+ o q" s8 G0 }& M& P - >>> math.log10(20)
7 a# R; p" T/ _/ q - 1.3010299956639813
复制代码
( I! x! u9 g7 p( f1 Y* r$ D# Lmath.log1p(x) 返回x+1的自然对数(基数为e)的值
+ |1 l0 x" u* G* X+ ?# J, ?- #返回x+1的自然对数(基数为e)的值) F/ [; u/ I9 v8 N) r& C8 V
- log1p(x)
) z& o1 z4 L% c9 U5 ^1 j" G" q - Return the natural logarithm of 1+x (base e).$ _9 \) T; u0 f' r
- The result is computed in a way which is accurate for x near zero." ^# j6 |. T) M- c
- >>> math.log(10)
# ]2 U4 N# Q0 i# P! J3 R1 _ - 2.302585092994046
6 g- [% T B- C9 G& e8 W! V$ S - >>> math.log1p(10)( N9 z) X) |+ N! r
- 2.3978952727983707) n+ q" i: A, w) R
- >>> math.log(11)- D l/ E" i w @
- 2.3978952727983707
复制代码 - J0 r2 I6 Z8 ]$ `" g
math.log2(x) 返回x的基2对数% M9 v0 k# r# @1 W# ?! H! |
- #返回x的基2对数% m1 a: r( s/ X/ ~0 ^6 k) ^
- log2(x)7 @! J2 N: R3 N
- Return the base 2 logarithm of x.% [& Q+ \+ T8 B, q; g% a2 y, I+ m H
- >>> math.log2(32)
' X7 L5 S; R) Z3 ?+ V - 5.0
5 F& e- F; h: J! F8 j - >>> math.log2(20)4 p7 ?8 f- H% J. Q6 F. N
- 4.321928094887363
: g1 _: P$ E; d) n; k0 [8 e7 m - >>> math.log2(16)3 \# w6 O9 L' P7 f3 }
- 4.0
复制代码
9 n1 M8 q6 |4 X4 p4 e5 e( Zmath.modf(x) 返回由x的小数部分和整数部分组成的元组
& W( ?0 s( h: u3 c; u- #返回由x的小数部分和整数部分组成的元组
2 b. i9 t, ]" Y8 D; d9 N! m - modf(x)
7 h! l9 s9 l, I, Q0 s& L% _ - Return the fractional and integer parts of x. Both results carry the sign+ A* B# ^8 ` A" i( P9 z R
- of x and are floats.
1 m3 t- ]) K5 T* r2 ]4 y& F - >>> math.modf(math.pi)" O, _7 |1 I% F; @" g% O5 C2 F* ?0 L
- (0.14159265358979312, 3.0)3 Q* Y' |7 s$ R# u
- >>> math.modf(12.34)' }& u/ a# L, A, E0 i0 H
- (0.33999999999999986, 12.0)
复制代码
& [& r3 j# V+ m* M# y8 umath.sqrt(x) 求x的平方根
& @' b; n: w3 X& ~- #求x的平方根
; N% O6 J8 M5 E$ e - sqrt(x)
5 @0 ]( t: q4 v6 P ~- C% y - Return the square root of x.
: m/ {- I1 Q$ U. n5 ]2 e; ~$ q - >>> math.sqrt(100); t% F( S% b+ k4 ~; ?
- 10.0" x! S: x; [4 p+ j5 R
- >>> math.sqrt(16)
: a& J) T( E8 H( p* T, X( ~ - 4.09 \0 m; t' A, F6 Q
- >>> math.sqrt(20)1 N, h9 Q) M6 s
- 4.47213595499958
复制代码 ~( q0 X9 b. u9 S6 [" |2 P, e
math.trunc(x) 返回x的整数部分- #返回x的整数部分
) z9 q3 ]. \* X" T6 { - trunc(x:Real) -> Integral. v; t! Y* l( [3 s( d+ Z. _: S9 G9 y
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
! h x2 r$ @# [6 H2 {' p% b+ r/ @ - >>> math.trunc(6.789)* ]4 l- j; u0 d3 W7 j
- 6' f) Y, S# S* \% D
- >>> math.trunc(math.pi)% C O6 T! m9 k9 N' j0 w% ]+ K! t
- 3
! O. K$ D4 `/ K d3 q1 ^ - >>> math.trunc(2.567)# j& K. p F) u( q. i& s
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|