马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 a7 k8 w y5 D- K" B4 }
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# l1 n3 \# i. j6 n
1 ?% }8 p+ X+ K% @0 Z% A方法1:0 u6 c L, p) L
- >>> import math
5 V) k r( h5 a* i% f1 Z0 A3 G4 h - >>> math.sqrt(9)# Y) M3 P5 S' J% s% p/ I
- 3.0
复制代码 方法2:& y% O7 C0 ?. H2 w
- >>> from math import sqrt
/ i. @# D/ z8 c( E9 }8 n - >>> sqrt(9)
. _$ H$ R8 S7 `: V& m' H - 3.0
复制代码
" k2 F4 j8 z1 V& a! R( Z
8 T& Z, f: v# C& Imath.e 表示一个常量; A0 _2 Y. t4 x7 p; }5 c
- #表示一个常量
# [! A( S. N' `0 E ^3 ~" X5 D) d - >>> math.e/ ]: {) V& S* B
- 2.718281828459045
复制代码
# G% e6 C5 w0 |' i( C5 D2 umath.pi 数字常量,圆周率
) c) Z( n' G j1 ~: P5 q8 S- #数字常量,圆周率
1 t6 S9 Z$ j: }' f8 g - >>> print(math.pi)
" A. |6 }. h0 K, X, Z J( R - 3.141592653589793
复制代码 3 a# K: O4 S9 a" c0 S- I5 G
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
. ~& _! K) A! Z& R `- #取大于等于x的最小的整数值,如果x是一个整数,则返回x7 U3 T% a7 N, Q
- ceil(x)
* Q. U1 `0 B7 ` - Return the ceiling of x as an int.) H) p9 l8 A, {5 q
- This is the smallest integral value >= x.8 ~4 @8 H2 e/ E, b( ^# P v+ @
- 3 x6 }4 g: a+ N# j
- >>> math.ceil(4.01)
" E% L" O, G2 A$ y1 o8 n( {, { - 5' J+ H# I0 J5 z
- >>> math.ceil(4.99)' o& H: Y2 A+ e- l. ^" L. S- W
- 53 U0 \- ^# O. J* F0 f1 V
- >>> math.ceil(-3.99)
6 |2 {6 d2 E' L6 O- X: F - -3( y* ^! I" K$ i( R3 f# R9 P' R7 s3 W
- >>> math.ceil(-3.01)
+ d, O: e6 K: c- E1 Z5 @! r - -3
复制代码 $ J- y6 `) q1 ]9 k7 a' |2 z7 K
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身& w; C+ X* }+ x# n
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 L4 ?( ]* X6 a5 I9 y) Q$ f2 r- W9 f
- floor(x)
8 x/ E: y% y ?& B - Return the floor of x as an int.
0 L$ w2 }/ F% N1 o' x) U4 P7 q$ K - This is the largest integral value <= x.8 N* _ T9 R' I" z9 X# V
- >>> math.floor(4.1)
$ W/ O7 A+ g, a3 ?" J$ v O) m - 4
: G9 l( g7 v% s, e& C - >>> math.floor(4.999)8 p1 x# @8 l* @4 y* D
- 45 {/ k0 E# I; q* v
- >>> math.floor(-4.999)
" C# [, i: n- U! L8 H! Z. F# U - -5, @# b/ o( b2 {: R* H6 V- e. |
- >>> math.floor(-4.01)$ x% q4 }2 t" Z$ H2 n0 ?
- -5
复制代码 ! R( o( _0 [4 g" U0 @8 x" m' I+ s
math.pow(x,y) 返回x的y次方,即x**y2 z- L5 P! W, T8 W; n, q
- #返回x的y次方,即x**y
8 I$ A! K: d6 \6 ~7 z; v7 G - pow(x, y)+ f+ v$ Q% s6 O8 d: _* G( N- `5 R
- Return x**y (x to the power of y).3 |4 \/ v2 o" t$ c" f. Z, n) N
- >>> math.pow(3,4)
# D4 |. E( |1 K! S - 81.0
* E. @# h0 _# U- R& K+ h5 F9 e - >>> ( C' {& B0 Q T
- >>> math.pow(2,7)7 y( X! [; D: i; b( m: I
- 128.0
复制代码
% {/ I, r3 Y4 q* g7 g8 mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 `' F& V- J5 \/ r: D% Y; ]- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! S' z' N+ Y4 n) o - log(x[, base])/ Y, g# o4 U7 k6 o; G
- Return the logarithm of x to the given base.
2 I! \$ k. U) W) q5 e) `8 y/ e - If the base not specified, returns the natural logarithm (base e) of x.1 B1 E) S' [4 T3 G+ T% T8 n( a3 {
- >>> math.log(10)1 W. o3 ` `! P0 P- c
- 2.3025850929940467 \8 F+ W: X A( B9 u: s' ?9 W
- >>> math.log(11)
- ~! L3 [3 }8 n* q - 2.3978952727983707
2 {% _( L$ o$ K# ?# M - >>> math.log(20)
! E; n8 H! {, g7 F3 {! ^( W, e - 2.995732273553991
复制代码 / g3 j5 J+ z x2 R0 F" s
math.sin(x) 求x(x为弧度)的正弦值) K4 z; \# t1 N3 ?0 q$ Q8 Y) Z
- #求x(x为弧度)的正弦值) [+ U! v% X) Y# `' s K! M, U# U
- sin(x)& ~2 W% ~; K7 H l: f3 B, ~8 ?
- Return the sine of x (measured in radians).
- ?0 g$ O, r8 e3 ^" j( P: X - >>> math.sin(math.pi/4)7 X& F% z" I, a& D: F# l% K5 o
- 0.7071067811865475
8 s( P- Y: {/ V$ M - >>> math.sin(math.pi/2)7 F! j& k" k0 H9 \" U3 h6 z
- 1.09 o" b; }( Q; z( O0 X
- >>> math.sin(math.pi/3)$ |9 c! b' ?) ~8 M
- 0.8660254037844386
复制代码
" L9 T% Q: _7 _! d5 @6 J1 o* amath.cos(x) 求x的余弦,x必须是弧度
& _, x6 M6 q2 |( h- #求x的余弦,x必须是弧度
4 X7 D' K0 j9 Q - cos(x)
F# J& d& N+ V4 l9 \ - Return the cosine of x (measured in radians).2 a% D" A" ^2 z4 i) ]1 W
- #math.pi/4表示弧度,转换成角度为45度
( }0 q/ }! X0 | - >>> math.cos(math.pi/4)
) |, E: {1 ?1 ?5 f7 p( `: |2 W2 Q - 0.7071067811865476
) c4 Q' B4 U! N: Y' I7 [" y% v - math.pi/3表示弧度,转换成角度为60度: T$ I, v8 L1 E o% X
- >>> math.cos(math.pi/3)$ s/ v+ I( S3 ^! E
- 0.50000000000000016 H. E! {8 F. p$ ?4 i& l0 @
- math.pi/6表示弧度,转换成角度为30度
, L9 V1 b }. t' ^9 v: @$ x - >>> math.cos(math.pi/6)
7 x! `1 {& B# ]1 Z1 g - 0.8660254037844387
复制代码
) |) E2 z$ h+ U3 u7 vmath.tan(x) 返回x(x为弧度)的正切值
. L; C1 j j5 h1 Q- T+ T) \6 h- #返回x(x为弧度)的正切值
) ?* Y% l3 h- |( G3 y7 ^, I4 V! \ - tan(x)$ n! B% f( n% X- v( k2 |5 P& {
- Return the tangent of x (measured in radians).
$ i$ X1 M& O' N2 W0 F: |2 `7 V) _ - >>> math.tan(math.pi/4)
9 s' k( g4 c8 B9 U) N0 m* e6 P$ O - 0.9999999999999999
5 P) z7 c$ o' T - >>> math.tan(math.pi/6)1 X2 ^8 H" j7 _6 K% T# l/ e6 m& b
- 0.5773502691896257/ z4 h T K* k1 q$ N. s
- >>> math.tan(math.pi/3)
; i8 l2 p/ b2 d7 ]1 K# M - 1.7320508075688767
复制代码
! `- q2 S# b' b1 wmath.degrees(x) 把x从弧度转换成角度
/ P! Y+ k: L! a+ N6 h( t9 |- #把x从弧度转换成角度* R& a! ]0 ^: K. P0 M, N- }
- degrees(x)
: n! y- C0 d0 ~4 S7 v: |; i' I - Convert angle x from radians to degrees.
+ I' u$ a' S7 q( Q5 `. U- Q+ |
' { }! N% J! t0 j, H$ D$ R- >>> math.degrees(math.pi/4)
$ ?* n" o" u3 w$ A( Z - 45.0- T; z8 q. s1 e0 X4 C5 a
- >>> math.degrees(math.pi)! z/ x& `) ~$ l8 Z0 F+ _
- 180.0
- |. l2 a4 y" W* R - >>> math.degrees(math.pi/6)8 ?/ r5 \: v6 O3 c; s( \& `7 `1 g
- 29.999999999999996
% ?( ?4 I# z$ B) B% ~ M2 k" n3 u8 @ - >>> math.degrees(math.pi/3)
8 o9 D$ q$ v5 A$ [" G - 59.99999999999999
复制代码 * m4 y3 D; g+ Z& y- r- d( o
math.radians(x) 把角度x转换成弧度
! d: n; B( {/ l8 \9 O- Z% W6 ^- #把角度x转换成弧度
* H) g, c0 m: @ - radians(x)
# }- F: T! u9 D7 B R' R1 x( t - Convert angle x from degrees to radians.* R9 \1 Z- G4 V0 M2 t6 y
- >>> math.radians(45)! r. N% X7 e6 n9 @8 N
- 0.7853981633974483
- u _* v+ P; l9 g+ h1 b6 _ - >>> math.radians(60): d3 |- A3 L2 B& e4 V4 ?
- 1.0471975511965976
复制代码 6 R7 B. f. b' W; e
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
! l- K. s- _" `. U* x2 B; E- u- #把y的正负号加到x前面,可以使用0
+ Y" Z+ \/ \7 [ - copysign(x, y)
! Q$ D8 Q9 r0 {- ?( ]* U3 ^: y+ F6 `/ }* S - Return a float with the magnitude (absolute value) of x but the sign ( R$ p/ Y" v/ U# }/ H% `& W8 g
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
) T2 g' Q8 I* f# c: ]2 p/ a - returns -1.0.
# P4 x6 }+ J8 T7 k* U - / c2 g2 C9 i+ V. T2 V
- >>> math.copysign(2,3)! Q/ n8 h4 p9 [/ l
- 2.09 L* u$ I1 i+ U, V' H
- >>> math.copysign(2,-3)
2 @+ u8 I# j/ |; K/ V2 ]# G - -2.0
. b, b( l: ^& U c. D7 K - >>> math.copysign(3,8)2 d+ l: l# X, [, E2 I) g* P
- 3.0' ^* i! c8 E9 @
- >>> math.copysign(3,-8)4 I3 T1 e9 E2 }1 y4 \
- -3.0
复制代码
+ S0 P- J2 ?0 T7 K7 vmath.exp(x) 返回math.e,也就是2.71828的x次方0 v/ ?: H9 n' s/ h% j- z1 B
- #返回math.e,也就是2.71828的x次方
% W8 b+ Z* b- X# W! _ - exp(x)
}' V$ w, C1 d T7 W - Return e raised to the power of x.
+ Z. L0 V2 L3 b/ n: N - - W. \6 l# o; y! y! {& q- m; O; l
- >>> math.exp(1)
2 B' l) J6 y: R# F ^' w# m - 2.718281828459045
# s% F0 Q1 D% c( h3 c" G - >>> math.exp(2)
) V9 w& z0 i& G! [$ c - 7.38905609893065
* e7 ^; D& u7 | - >>> math.exp(3)3 {8 |% i3 u) P+ X8 j/ U
- 20.085536923187668
复制代码
8 P7 E' h% y. x* P* t0 x7 Gmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# z% T" c, P9 `3 `( I) f- #返回math.e的x(其值为2.71828)次方的值减1! u/ b X7 u- _' W% {& e J
- expm1(x)
. q) a+ M& `. g- C2 k - Return exp(x)-1. ^9 T. Q: Q L! f- ]
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
# _) W) t4 a+ h2 `
! J4 H4 d4 ]7 y- C& ?* i- Y- >>> math.expm1(1)
( r4 b% w- s4 y) L+ i+ b$ p - 1.7182818284590459 b2 s- D6 a2 O# T
- >>> math.expm1(2)3 n" C+ Z, N+ r; }& ^2 {, m
- 6.389056098930650 `- k- x! h3 r, u
- >>> math.expm1(3)5 l4 t. H1 a* k- u; t B' Z8 n& J
- 19.085536923187668
复制代码 : p9 k6 D( X; x3 s
math.fabs(x) 返回x的绝对值" M" r$ }0 P" U' @
- #返回x的绝对值& a+ E7 t. N9 q7 [
- fabs(x)% v& Z9 O% p4 D2 z. d0 ?& X
- Return the absolute value of the float x.2 V' d# Y ^0 u* U( q
- , \3 h* E6 Y: p0 [8 j( _
- >>> math.fabs(-0.003)
6 h3 @# M4 Q! S7 f5 }- \! ^: V - 0.0034 f+ j$ X Q/ z: ~! c: n# v8 s
- >>> math.fabs(-110)
. n% i2 e1 a& M+ c2 I. i - 110.0
/ t1 P! Q1 _0 V( g, R; z - >>> math.fabs(100)
: U2 {, ?% u9 u9 W: J! D4 @ - 100.0
复制代码
( I5 Q9 w3 d! `! _math.factorial(x) 取x的阶乘的值/ L* L$ e, K- E' E/ W7 z
- #取x的阶乘的值 S4 U, Q# u$ W0 B: y0 _
- factorial(x) -> Integral. a& p5 n* h7 f! e
- Find x!. Raise a ValueError if x is negative or non-integral.! Z( n; H) ?: A& G( K
- >>> math.factorial(1)
, y7 o4 C& i, c2 q! g - 10 L( ^4 L& I' n. w+ q1 h) T
- >>> math.factorial(2). \/ B [" ?2 S, z
- 28 w; u" I' c& l+ b! x0 E
- >>> math.factorial(3)
( G) k3 T* I- W - 69 [ L8 z9 R/ ]* I
- >>> math.factorial(5)
3 d' T! P; _( y/ B1 w: b- ` - 120
7 t2 Y3 n- c/ \, g - >>> math.factorial(10)" K6 o: C: ^ ] w% q% M+ }
- 3628800
复制代码
2 H( M W- h: J- j8 X1 Omath.fmod(x,y) 得到x/y的余数,其值是一个浮点数5 P( l/ R) W9 c6 A$ o8 @4 L
- #得到x/y的余数,其值是一个浮点数
3 P) K8 G3 N3 I! O* [ - fmod(x, y)' D: ]' }# a% s5 e" N
- Return fmod(x, y), according to platform C. x % y may differ.
P: ?4 _8 E: I) {& A% U - >>> math.fmod(20,3)1 C8 M) D* _) L3 o4 n0 J
- 2.0
8 t) p( r1 V. L- y4 \4 l) [& x- K - >>> math.fmod(20,7)5 F9 |5 P' w' X. d9 Q
- 6.0
复制代码
$ r' G! v k$ x) ~6 pmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围5 _5 C' }. D. \6 y
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,: D% A7 b0 g1 c/ ^ P: Y
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' p0 N' O+ {# @! S% ?* ?
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ P C5 e7 W) b4 d! j/ F! [/ o
- frexp(x)+ W, K1 v+ b- _) p6 ^
- Return the mantissa and exponent of x, as pair (m, e).
; p" f& N) Q0 t- g7 O) k2 I) v - m is a float and e is an int, such that x = m * 2.**e." p" f* R( S# j' z }
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 b: r7 C5 _6 F ] - >>> math.frexp(10)
5 s# M* n3 R3 A - (0.625, 4)
$ s3 T, O. Y+ O5 } - >>> math.frexp(75)" g8 u4 A( h% x; `
- (0.5859375, 7)
. U+ i7 }! a8 ^8 s& D4 W - >>> math.frexp(-40)0 @5 w0 q; x- o
- (-0.625, 6)
& ]2 o+ F6 Z8 b; _$ c - >>> math.frexp(-100)
6 d2 M' f$ H* Z' ?4 v/ \" o# ^ - (-0.78125, 7); F; S$ z) J2 a: `8 K# V& ]- D A
- >>> math.frexp(100)! M7 I- g2 G. |4 M: X
- (0.78125, 7)
复制代码 p/ z3 V l$ z9 e4 w& b7 I
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)1 R ~4 L" \- \/ Y0 g, R
- #对迭代器里的每个元素进行求和操作: y% l) H7 n+ s- R- E! R
- fsum(iterable)
- m/ ^ y& U( E1 O - Return an accurate floating point sum of values in the iterable.% q+ V& R6 s- e; Z" _) e
- Assumes IEEE-754 floating point arithmetic.
8 y7 v& \/ z Z$ y8 v7 p; r - >>> math.fsum([1,2,3,4])
$ v% l: k. ^* P" l8 i: C2 q8 t - 10.0 U0 u( V7 ~. i2 o2 T' x
- >>> math.fsum((1,2,3,4))( ]9 {( M! S! C1 q5 x
- 10.0
; A% J+ t' p7 z - >>> math.fsum((-1,-2,-3,-4))
! k) g/ Q- W# F5 \, E7 h* G - -10.0
/ q) @" T/ T! d: H - >>> math.fsum([-1,-2,-3,-4])
/ E& G, f4 J7 @$ ^ - -10.0
复制代码
; Z2 B- X9 O2 |/ kmath.gcd(x,y) 返回x和y的最大公约数
) e( h; O- D+ l( O# |3 b- #返回x和y的最大公约数: l( Y# S$ u4 D6 I" [2 G
- gcd(x, y) -> int
+ e/ z1 D% u' d7 T5 j# G) X - greatest common divisor of x and y
9 E' g) B( i, l, J - >>> math.gcd(8,6)
0 g5 u* G1 R/ Q ] - 23 @% C3 {" Y2 ^, U9 [6 K( Y
- >>> math.gcd(40,20), j, Z9 g5 Q; ?$ D+ D" O
- 204 ?! ]' g f7 I2 i
- >>> math.gcd(8,12)
; G0 P! F( Y0 p - 4
复制代码
% ]. {9 O# J0 [- V; Wmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
8 _: w+ i' |" j4 z- #得到(x**2+y**2),平方的值. s- E* t; p5 @+ Q; \0 w2 z, F
- hypot(x, y)
, v: j5 `& U! p3 d. C - Return the Euclidean distance, sqrt(x*x + y*y).
# y# v, ?- A+ U- x6 @; @ |5 Q - >>> math.hypot(3,4): H4 Y6 Y& a' l% K$ |: T Y
- 5.0
2 o4 g }( G& f* h - >>> math.hypot(6,8)7 i) m' Y! c3 a& h# O; q3 b3 _9 N/ Z
- 10.0
复制代码
0 n1 \" s8 f; Amath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False7 V: u, u$ s5 [# `5 ~! B! [2 v
- #如果x是不是无穷大的数字,则返回True,否则返回False. ?, [0 q& ~4 a
- isfinite(x) -> bool; J, q0 o) @& r7 o
- Return True if x is neither an infinity nor a NaN, and False otherwise.
9 R/ |5 v* c) ^4 ^4 p7 ^2 j, G6 u - >>> math.isfinite(100)
1 s2 t7 `+ k. |0 h - True# p' c# P$ n' G; x h. a( m
- >>> math.isfinite(0); F: z* x& N7 F2 I. O
- True7 v2 O& k4 a& O5 f
- >>> math.isfinite(0.1)
0 x1 e6 n2 a. K3 x - True% R. L' e$ x6 Q6 @6 A& f8 W) a; ]
- >>> math.isfinite("a"); m0 S, W1 O$ m/ m _. l, K$ [. g
- >>> math.isfinite(0.0001)7 ?: f" g) U2 ]2 `8 B3 [+ h( S& D
- True
复制代码 + d& Z5 r1 K/ g' H
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
. d% c, f V* A$ f- #如果x是正无穷大或负无穷大,则返回True,否则返回False
. H8 @. ^! B5 @; o | o2 l - isinf(x) -> bool6 @$ Q- o, I9 H+ j3 j* O$ F
- Return True if x is a positive or negative infinity, and False otherwise.
( m0 i8 {! P2 @ - >>> math.isinf(234)) F' \$ I4 `3 R, z
- False4 j% |, k0 `+ a# L0 q B( z
- >>> math.isinf(0.1)
4 c7 ^/ {2 v1 y, j, o - False
复制代码
, A( m' @& w( I& dmath.isnan(x) 如果x不是数字True,否则返回False. @1 t# i) E" Y! `$ c9 N& Z3 s
- #如果x不是数字True,否则返回False& i* S2 W [ \* A
- isnan(x) -> bool+ b- a; q# ]$ C2 Y$ I
- Return True if x is a NaN (not a number), and False otherwise.
" R2 _* U$ r; o3 d" s5 P5 m - >>> math.isnan(23)
: [" A B: R; `" @6 i - False4 A" D* q% y" ?6 a) @
- >>> math.isnan(0.01)
* y0 u* M" q! \1 e - False
复制代码 5 F& d2 n1 Y f* ?3 G; N) J8 {0 Q2 G- E
math.ldexp(x,i) 返回x*(2**i)的值
3 N2 z$ u8 n! D ~& I6 U( u& @4 |- #返回x*(2**i)的值2 t# y g8 W& d' y* O. M+ A
- ldexp(x, i)
: e0 l+ p8 ]4 j - Return x * (2**i).# @- x# Y# \7 G- j8 q! x" M
- >>> math.ldexp(5,5): }, Q, b" s) M8 Y& u9 `
- 160.0
# y8 Q0 H0 R. h1 r, U% w- z - >>> math.ldexp(3,5)
$ o. b( F6 ?4 w7 L: N9 f, p% J' [0 a - 96.0
复制代码
$ b/ B2 x8 C" b) y2 s' ?" ?5 Cmath.log10(x) 返回x的以10为底的对数+ y" t% Q5 Q, Y o7 i: {- L0 a
- #返回x的以10为底的对数
9 l, i8 h/ ? S3 O8 I1 p. [% C - log10(x)
/ F7 K8 q/ }1 I. G/ |+ `- S) c - Return the base 10 logarithm of x.# s) a4 ]% M7 p! h" J B
- >>> math.log10(10)9 M& m. L: e. @
- 1.0
, ]! c/ Q3 Y* ~7 V - >>> math.log10(100)
3 p6 J; n4 r4 `, N9 X, Z - 2.0) r) d7 D0 V) D, E* P( v' b* K
- #即10的1.3次方的结果为20
1 o0 o$ Y# Q- \+ @. U - >>> math.log10(20)6 |7 E4 X2 N$ b4 T
- 1.3010299956639813
复制代码 / U( O8 l! O3 q( p: l+ R
math.log1p(x) 返回x+1的自然对数(基数为e)的值- G9 M. p- h! o5 z
- #返回x+1的自然对数(基数为e)的值
3 H4 D4 i& s# {& m& G - log1p(x)
- d. B9 }3 K1 I5 S0 C7 c. x$ R - Return the natural logarithm of 1+x (base e).- p/ H7 @9 ]' ?* D; w/ M5 Z6 ]
- The result is computed in a way which is accurate for x near zero.
) ~- T# C$ l0 Z' ~+ @ - >>> math.log(10)$ J' l8 Y+ A1 H3 w
- 2.302585092994046% O% L, \% V) Q' d: e3 u% A7 \
- >>> math.log1p(10)
( `$ O* v& R* E' W: g/ d- [ - 2.3978952727983707
/ e9 Z( Y9 Q5 @* N - >>> math.log(11)2 O/ q: q1 P9 Y/ y% N; Y( C
- 2.3978952727983707
复制代码
+ O0 F+ q4 `3 hmath.log2(x) 返回x的基2对数- j# m" P+ }: a% o: M
- #返回x的基2对数/ u! V! s( o# l, Y* v
- log2(x)
, s6 K! h5 J: d: u" I! F - Return the base 2 logarithm of x.+ }7 d' F# A9 ~* i3 }
- >>> math.log2(32)
" V9 r% o: {' c1 X - 5.0
0 D4 s) R w* H9 m - >>> math.log2(20)
" c2 j$ c$ Z* P9 G' R9 B, Z - 4.321928094887363
- Y/ b7 M. Y: f - >>> math.log2(16), E9 t% N$ i% N( A6 j* s% f9 a! [
- 4.0
复制代码
1 W9 ^# b8 X& R' Mmath.modf(x) 返回由x的小数部分和整数部分组成的元组
" V8 K. G. ?6 h0 X4 d% c" B2 L& r- #返回由x的小数部分和整数部分组成的元组
- l c7 ]! r0 k/ S - modf(x)
, V5 w& [. V4 }2 B - Return the fractional and integer parts of x. Both results carry the sign' v% W4 ^& j* a; J r2 V9 P1 d& H
- of x and are floats.
. I; Y {' l( L/ H - >>> math.modf(math.pi)! R7 f- K5 G- u* z7 {
- (0.14159265358979312, 3.0)0 e/ F k% j8 D
- >>> math.modf(12.34)
1 W8 }1 Z0 {7 F1 L& Q( _ - (0.33999999999999986, 12.0)
复制代码 * f$ o3 D0 A# [: C
math.sqrt(x) 求x的平方根- @: T9 h4 g/ w$ |; W0 J2 n/ ^
- #求x的平方根
( f0 z/ r+ b( L8 ^9 \: } - sqrt(x)$ ~2 X) \! \. q$ d2 N
- Return the square root of x.
Y# @& p D% B* j( ^ - >>> math.sqrt(100)
! b, v; \# _7 O8 W; c - 10.0( w& y1 p V1 U3 Q# Y
- >>> math.sqrt(16)
! p% s! P) }; R3 } - 4.0) ~& C) c$ H( h7 q
- >>> math.sqrt(20)
' o# C$ M8 p# ?9 i9 K9 g - 4.47213595499958
复制代码
6 f! P* _3 \- d7 ]) l. s8 |math.trunc(x) 返回x的整数部分- #返回x的整数部分/ O% ?% n: Q; O# p
- trunc(x:Real) -> Integral
5 ` G$ X' I M3 Z) x8 n6 N3 [2 } - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
5 R* w' c# u! t# `# S - >>> math.trunc(6.789)3 v. }% T1 K. H. K. o& |! i
- 6
! Z4 v& P6 q4 y# S" r - >>> math.trunc(math.pi)
6 g. y' \, y' q* M6 M" r4 ` - 3
9 G6 l7 ?3 I r |6 P - >>> math.trunc(2.567)- ?( r( @- t/ l
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|