马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! V5 R! N9 y) _$ B8 C0 s0 r2 }3 |- X: c
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。* e6 j5 H* S+ M% S$ A: {
5 z* t v' M) j5 c6 k
方法1:
0 R4 N3 H4 `) Y" G; u$ U- >>> import math7 W4 V! ?' h% s6 ^3 f
- >>> math.sqrt(9)
4 M+ ^( W; u, {, p* z$ B - 3.0
复制代码 方法2:* U2 @! N( j8 }" z! }5 D6 e/ j
- >>> from math import sqrt
% _9 \$ M/ j- y" q* o, l - >>> sqrt(9)
7 p9 b U9 c3 N - 3.0
复制代码 & W7 P* D1 O. o" _( k
2 Z' N5 ^& W0 umath.e 表示一个常量3 N; K) Z! `! {' h! a( G( h5 M
- #表示一个常量
' h- p1 J$ w8 u# A$ ~; \ - >>> math.e" K4 |$ L) h, o. O q4 v6 _
- 2.718281828459045
复制代码 " T! D1 w# ~1 U r
math.pi 数字常量,圆周率
- i9 h: P# y1 v) N5 \& @- #数字常量,圆周率4 E3 x; D4 v" ^. [. X2 ^' J
- >>> print(math.pi)
0 p6 b! j8 f+ k' `6 \9 F - 3.141592653589793
复制代码
6 O! ~9 e O" j7 P x2 ?( gmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 h, Q) `( @: u# Z2 K: |& p- #取大于等于x的最小的整数值,如果x是一个整数,则返回x2 L1 O" q) [9 x# D, `0 G+ p
- ceil(x)$ C/ P: E- P) Q: V
- Return the ceiling of x as an int.
9 M" u- c, ~+ A( S - This is the smallest integral value >= x.' k2 ]) h8 V/ Q. O( T8 F
- ) d) [! Q' Y; ^/ J# a
- >>> math.ceil(4.01)
" v( L8 W% U* T; | - 5( O/ N5 D- H0 T( Z9 R6 n
- >>> math.ceil(4.99)' n, ?8 T) H7 v" I% g+ D8 A; X
- 5
# K q( V7 W/ p) b - >>> math.ceil(-3.99)
7 ~8 v! ?$ ^$ x8 I# @ - -3
/ i3 j/ Q7 e9 e - >>> math.ceil(-3.01)
" Y5 |+ s$ e+ O4 o! _! f' r' S - -3
复制代码
+ _. f& o4 }/ z; w( ], m) ]math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 B5 e) Q# b6 W$ l; y1 Y5 W& o- W3 b+ A* b- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 s; J1 v) ]! N - floor(x)% W7 G3 }6 o' |* t5 g
- Return the floor of x as an int.
3 [- Y& f m' i% e$ T! b - This is the largest integral value <= x.
7 a1 ^9 E" P7 }( I - >>> math.floor(4.1)
5 i# _3 d# {' l- Q2 t8 _ - 4- H# r, o* D5 E8 h. I4 o0 ~- k
- >>> math.floor(4.999)" H* D1 S/ u+ h* a2 u4 }: V1 b
- 4
O' D# U% I* e- S8 r8 u b+ x1 D - >>> math.floor(-4.999)
9 F# U+ v1 ^) C# G$ _7 J# c) I4 e - -5$ x5 f5 f$ P6 n) _
- >>> math.floor(-4.01)
$ X5 @( \; ~) f# O9 p% d) a - -5
复制代码
& f! Y0 v+ P2 X Q2 |, Y$ s' Ymath.pow(x,y) 返回x的y次方,即x**y
; z% `( g) l( j" v1 j- #返回x的y次方,即x**y
& P: E) [( }; ~1 Z - pow(x, y)6 u! r& z6 o2 q4 w% R$ j$ ?: q) y" L
- Return x**y (x to the power of y).
$ E6 e4 I4 `/ m( D - >>> math.pow(3,4)/ A1 J8 P& Y, c4 t
- 81.02 z0 a$ a. ~. v$ b6 Q! R, Y5 L1 K
- >>> 3 q$ F% k; n( E: i( C3 x
- >>> math.pow(2,7): n* ]' b7 U4 b' a" z7 w- H
- 128.0
复制代码
6 d( ~. |4 h5 U' }, s8 J% j1 pmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, {% T: X s @, x7 H- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
* @: ^0 A* L g( z& F5 ^" m" `: c - log(x[, base])( a& D4 K- }! h4 N' i
- Return the logarithm of x to the given base.
/ ~# g5 c2 W z* ]6 g) @$ W - If the base not specified, returns the natural logarithm (base e) of x.
0 L7 _* t# k2 j+ K# S; s - >>> math.log(10)
9 I. Z$ F- b1 x - 2.3025850929940465 U% C8 s; x. ], x( j6 C5 {
- >>> math.log(11)$ H; `7 @2 b' `# B7 _
- 2.3978952727983707
0 z9 A' d- K+ i4 I- }' X& D - >>> math.log(20)0 p- A& k b! A2 [* |
- 2.995732273553991
复制代码
0 D( v, k6 d( Y+ c/ E; Q: Kmath.sin(x) 求x(x为弧度)的正弦值
9 M8 Z" t- v# L" N1 n8 |1 N- #求x(x为弧度)的正弦值
* R2 K& l8 f# N' f1 G$ e" H; h - sin(x)
2 o1 O- @. Q/ B( ]# M: o - Return the sine of x (measured in radians).( f" W# ]; c! f5 B+ `
- >>> math.sin(math.pi/4)
9 |8 b7 N4 W$ ]3 d - 0.70710678118654759 _- U6 x* Z* F$ t, p
- >>> math.sin(math.pi/2)
( T! A' J5 d* i9 W& U - 1.0
: g; d5 Q' P, Z9 }! G: d0 c - >>> math.sin(math.pi/3)$ l/ K0 o/ W1 y# z. {7 h. Y
- 0.8660254037844386
复制代码 " }* u1 s; l* D% @, J, B+ I6 @
math.cos(x) 求x的余弦,x必须是弧度
- ~ P* P9 F& \( o/ P9 b- #求x的余弦,x必须是弧度- ^8 j$ }! j( X4 j6 \
- cos(x)0 I6 C- ]' Q7 I8 _5 h4 f; n
- Return the cosine of x (measured in radians).
( w& x! M) I. @) A8 \ - #math.pi/4表示弧度,转换成角度为45度
, K/ j- j& K3 {( | - >>> math.cos(math.pi/4)
8 h1 V6 a# ?& } E; ~& _ - 0.7071067811865476
1 e( ?. N) s/ @. ] - math.pi/3表示弧度,转换成角度为60度+ V; B+ z w% T) U G1 \( l9 J4 T
- >>> math.cos(math.pi/3)
. M( t$ U8 o, o$ b1 Z( }. J - 0.5000000000000001
5 L- m: E/ q! Y$ l# ~6 R - math.pi/6表示弧度,转换成角度为30度. I, p- J) ]% t' p% U4 {; n9 Z
- >>> math.cos(math.pi/6)& n q% q8 a8 `1 [- e3 \
- 0.8660254037844387
复制代码 # R( j) h9 E6 H* {
math.tan(x) 返回x(x为弧度)的正切值8 h( K2 _- J6 N* G: \" M5 `
- #返回x(x为弧度)的正切值
! V8 G9 j" Q$ f: K - tan(x)
, M g0 E" s- ]0 T - Return the tangent of x (measured in radians).
8 D$ X- N6 s3 ] ^; I - >>> math.tan(math.pi/4)4 l- r1 Z( T* |" n
- 0.9999999999999999! I$ _) C P" {3 t3 U0 \
- >>> math.tan(math.pi/6)
5 s+ j5 k& f- ?. V% e" L& _& C - 0.57735026918962571 Q" _! y. O9 I+ D+ v
- >>> math.tan(math.pi/3)
3 F9 k9 h) m! J$ m - 1.7320508075688767
复制代码
( t3 a2 S3 Q2 |math.degrees(x) 把x从弧度转换成角度
' o& [0 h* t) _& n, s- #把x从弧度转换成角度' _. c! T& @ U1 w5 u) j. P- z9 s
- degrees(x)
. K# ? Q4 W/ j) P0 F' u - Convert angle x from radians to degrees.6 X0 F8 q9 q) }/ Z/ p, L
- ! d' y% P9 D* \: J: M u* }$ A
- >>> math.degrees(math.pi/4). X& c5 I7 `5 m2 l L7 Z9 Y
- 45.0
1 i' q% m0 S2 p$ c* v/ S - >>> math.degrees(math.pi)3 N- t; J: O" H. x, L" R* ?4 e
- 180.0: y( t f! k3 G2 X
- >>> math.degrees(math.pi/6)
+ X) Z7 p. i9 { b% e - 29.9999999999999968 m8 R( t4 N1 l
- >>> math.degrees(math.pi/3)
' l) }0 d2 {" n) c; C: x - 59.99999999999999
复制代码
7 u( y7 ^) ^" o/ ~! E& R0 Umath.radians(x) 把角度x转换成弧度
# x! V( Q5 A& S8 N1 y4 S- #把角度x转换成弧度
& v; t' Y p/ q1 W! z - radians(x)8 z: d$ F( w$ A9 j$ w' s
- Convert angle x from degrees to radians.
- }% I3 m, o$ G& N; d6 u0 | - >>> math.radians(45)
[; {/ O( t- f2 | - 0.7853981633974483
; y/ n/ N. R! x. R - >>> math.radians(60). ~1 z( Z+ {8 o
- 1.0471975511965976
复制代码
& B* V; x& C6 p' f* E% f: k( l1 [math.copysign(x,y) 把y的正负号加到x前面,可以使用04 c) ]2 {% \* x
- #把y的正负号加到x前面,可以使用0
! \/ y1 p" k; o5 u- T3 p - copysign(x, y)9 c" N4 C' k! o4 m
- Return a float with the magnitude (absolute value) of x but the sign 2 }- N7 U% V3 q, V# m
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) * x: X2 [' K. P8 t6 G) y- d* ^
- returns -1.0.
3 r/ ]1 e$ y. c ] - : O7 J% n; k V% r1 ~. a+ w
- >>> math.copysign(2,3)
& j# ~ K8 j9 o0 X- [5 ?% U - 2.0
# @( x& `4 Q1 C. P - >>> math.copysign(2,-3)
# X; L; ]# Z% J7 H1 P7 l. D - -2.0
% ]7 W4 h; D9 J/ I - >>> math.copysign(3,8)4 J* ?8 Y% Z' ~
- 3.0
S; i) `" D5 M/ H# O3 {2 F7 s9 \( E - >>> math.copysign(3,-8)
+ }/ S# {4 D" z0 ~5 { - -3.0
复制代码
* _% j! U9 Z# H! L% g# [1 Omath.exp(x) 返回math.e,也就是2.71828的x次方( k7 p% y8 {+ ^' K* Q/ v1 J
- #返回math.e,也就是2.71828的x次方
" `% W& u) L; }. ~. l - exp(x)
; ~$ k. l7 [8 X# D2 \( y7 P - Return e raised to the power of x.; S! U: q( }2 f7 L: h; ?5 X% E
- - ~- j4 K' M! z5 Z6 a
- >>> math.exp(1)8 V& V/ k# t" }) H! a
- 2.718281828459045
0 m& f# r; M( N( n. w - >>> math.exp(2)
; R* j9 K+ i- u9 V - 7.38905609893065
& ]+ @3 f2 T4 K5 k$ C - >>> math.exp(3)
& K" m2 Z8 |8 x3 a- U4 G - 20.085536923187668
复制代码 * M1 Q; b! e6 a4 m* b; u
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# f- [5 R/ L% x- #返回math.e的x(其值为2.71828)次方的值减19 ~3 d. h& m8 ^/ e& f
- expm1(x), ^+ r T! t9 ~0 V. N+ O# y
- Return exp(x)-1.
" S- I' K% w0 T1 M9 z - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
) ]+ t% n' ]7 |+ `6 J; M- X5 P - $ D/ X6 j. K, S" m( B7 V
- >>> math.expm1(1)
; \; U) C& h- f0 R* a2 x - 1.718281828459045
1 o! u+ F4 \$ v# I/ z, n4 @ - >>> math.expm1(2): K' y" {3 V, n9 m, \, c. O' m( `
- 6.389056098930650 l E5 K" W R: O+ W/ B
- >>> math.expm1(3)5 c; V/ k4 M( I- Q4 w# ?
- 19.085536923187668
复制代码
! l/ l! s- P7 nmath.fabs(x) 返回x的绝对值6 k: s$ l" W8 O1 u+ ~
- #返回x的绝对值3 V6 v5 U, M5 ~6 t4 [7 L/ M2 p
- fabs(x)
+ n1 h/ y S: v- {$ ?! L' U - Return the absolute value of the float x.' \! S& _% D6 e, h* ~- p
7 C4 S1 H1 Q$ m) h' h9 s/ S+ c- >>> math.fabs(-0.003)
- d9 C& |1 A; Y2 V; u - 0.0034 ~, I4 {- m/ c$ T
- >>> math.fabs(-110)
9 p; H, q8 U; P - 110.04 P; q {( m' V) d; [ }
- >>> math.fabs(100)/ L" H' L8 Z& b s
- 100.0
复制代码
4 i: G9 |8 q- }& Mmath.factorial(x) 取x的阶乘的值
% q6 q2 l/ n. x% `1 q* f6 _4 L- #取x的阶乘的值
/ I$ c; p3 w, P7 w' n# {" U% N) k) X - factorial(x) -> Integral0 u0 l$ t, C* j" x5 A" U% ^* q
- Find x!. Raise a ValueError if x is negative or non-integral.
+ g$ B5 J; R( B6 i" f$ j - >>> math.factorial(1)
+ f: Z/ ~. X0 s - 1- Q u3 i2 A2 M4 v5 V- t% z% s5 \
- >>> math.factorial(2)* c4 Y/ U9 Y# d0 C. T8 \/ G
- 2
! ^) o) m% c8 l4 H: _9 R$ p - >>> math.factorial(3)0 S" S7 f2 i E( G& W k
- 6( V- _' b# s8 F# \/ b5 \
- >>> math.factorial(5)
" }; A& I8 B. e# @' n) f - 120
' a5 [9 u+ z5 h t" [ - >>> math.factorial(10)
+ X9 D N! ? v! Z; Z - 3628800
复制代码 # Z! |9 q0 O5 _: | x
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数 j+ l; `0 n/ c: e4 j* @ ~+ [
- #得到x/y的余数,其值是一个浮点数2 ~# u# q4 Z( o/ Y4 ], }: I
- fmod(x, y)- A4 U" h) c/ P. W
- Return fmod(x, y), according to platform C. x % y may differ.
; D% Q9 |) d) f1 R+ ~# \& c0 M - >>> math.fmod(20,3)
( L& c, N" P6 v - 2.0! K$ {+ v0 M$ F) y L E$ d
- >>> math.fmod(20,7)
/ O$ k. i6 S/ N5 K2 i/ h0 P2 L( y - 6.0
复制代码
# Q2 [+ q; m1 a- N; H% Dmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围3 @. W' ~3 q* i4 J5 o. Q1 E8 g
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( u: i* F* M& H6 d6 ~
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" }( U: q% |- u7 ?' R1 f
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) q) E* w' r3 m3 A1 B6 Z
- frexp(x)/ L: B) {& C6 ^0 }+ N5 R
- Return the mantissa and exponent of x, as pair (m, e).
i( Q" V* t$ n0 Z4 B - m is a float and e is an int, such that x = m * 2.**e.
, Y$ E# I+ ~" }3 N$ R( Q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
+ H9 y) N* u, `3 D$ u - >>> math.frexp(10): ^* Z% \; V. k$ K, {
- (0.625, 4)
* {; ^- v% V0 l* ` - >>> math.frexp(75)
+ N! n" h5 p4 s: l - (0.5859375, 7); K" H Q' C3 E% `
- >>> math.frexp(-40)
1 ]& m& Q" r& a - (-0.625, 6)5 `: _3 l: C; o) G. B& }% e
- >>> math.frexp(-100)
. o( Q- t1 a# N9 G' R% C - (-0.78125, 7)( i) A0 B' g9 }/ X( u
- >>> math.frexp(100)' s" A; ?$ V, e% n
- (0.78125, 7)
复制代码
/ y1 ]# ]8 i' e ?2 tmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
r5 k( Y+ o' C$ H1 i& l- #对迭代器里的每个元素进行求和操作3 J/ s# t1 A3 u" [( N1 u5 D% @
- fsum(iterable): q# w3 N8 Q; d7 t) {/ G! F i
- Return an accurate floating point sum of values in the iterable./ ^. e) Z! R' @( @: `9 Q0 c- x% ]3 g
- Assumes IEEE-754 floating point arithmetic.
7 C. J. g- K; H+ B# y - >>> math.fsum([1,2,3,4])( J9 p4 M8 r. k) p6 l9 K: j3 f
- 10.0
. o9 l3 N1 R- B K" y- ? - >>> math.fsum((1,2,3,4))
, B+ z3 x3 Z/ T: z% P. Q O ^$ H - 10.0
+ O$ X; ^: x' | ` - >>> math.fsum((-1,-2,-3,-4)). \: u2 b6 |& p$ d6 E
- -10.0: ?, ]2 C! c4 z; t
- >>> math.fsum([-1,-2,-3,-4])
! b& o) D/ l0 K$ C- g. L4 `* A5 Z - -10.0
复制代码
" |6 x6 o: X1 Z# C# omath.gcd(x,y) 返回x和y的最大公约数: K! u6 l5 r% ?# U8 t
- #返回x和y的最大公约数$ N) u1 p" y& i' J2 b) f! _
- gcd(x, y) -> int
5 Z4 J3 \8 ?' G6 P/ N - greatest common divisor of x and y
) q% @0 y% G6 p - >>> math.gcd(8,6)9 f2 X+ W" {! ?7 }
- 2
: u4 b' D5 G% G- m; ~ - >>> math.gcd(40,20); R7 ]: z0 G6 Y( R
- 20
* M3 p( D/ T- ~3 O$ Z0 |" g, | - >>> math.gcd(8,12)
" S* d* i6 K9 s0 j+ H" l - 4
复制代码 2 d+ n; G! Q- G" f' ^2 j4 H( D
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
9 n9 W$ }5 a, y9 [, u- #得到(x**2+y**2),平方的值0 ~/ K2 \; H8 ?; E' A; A
- hypot(x, y)
7 y% S$ c, G6 U - Return the Euclidean distance, sqrt(x*x + y*y).; a- e$ G7 ^6 B/ ^2 A
- >>> math.hypot(3,4)
7 t$ D( N3 M8 K( C4 a. k - 5.04 C" k) y3 W* ~5 S; k3 o0 M
- >>> math.hypot(6,8). s D, {+ ?8 j0 W" J/ ~& V K
- 10.0
复制代码
+ w' |' j7 K0 h/ l9 ^/ Jmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False8 f9 t2 l) h0 ?) w& D: |$ z
- #如果x是不是无穷大的数字,则返回True,否则返回False
+ }$ |# U/ `: v- f. T7 I K - isfinite(x) -> bool
7 j7 l! D" P; Z+ `* _9 n$ g - Return True if x is neither an infinity nor a NaN, and False otherwise.
" o, r# x3 X' X3 K% Q1 y4 | - >>> math.isfinite(100)( i: _8 Q8 H9 N' q; m% e3 l# Y2 z
- True
! Z$ b/ P5 @: q% ]) q9 c - >>> math.isfinite(0)
a# S; J. u# M& V- i9 L+ ? - True
* q% X. ^6 C1 D9 m - >>> math.isfinite(0.1)
# q0 W" W/ [8 s; t& n) \; d - True' g, e1 C" a; t) }5 T) j v, A
- >>> math.isfinite("a")! H3 Q$ f7 H* @9 |- u
- >>> math.isfinite(0.0001)
6 b( Q1 R& H9 U2 J. w/ B6 G- ^ - True
复制代码
" W; }2 B2 F( q' w9 Pmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
1 |& }+ Q+ E* f7 D( k5 u7 r- #如果x是正无穷大或负无穷大,则返回True,否则返回False
2 W4 f4 I( F% u' s a - isinf(x) -> bool
7 i5 f7 c7 s2 C5 {/ Q - Return True if x is a positive or negative infinity, and False otherwise.
( l9 S3 f2 h- g* Y - >>> math.isinf(234)5 g6 c4 _7 a3 Q4 @ \- p3 _
- False \' g9 b7 R* l% C* l: s& q
- >>> math.isinf(0.1)* W" ^ v4 W2 N, u* Q
- False
复制代码
. w' a3 ^6 D q; I. U5 t' mmath.isnan(x) 如果x不是数字True,否则返回False
$ M- F9 h3 F6 l" }) g- #如果x不是数字True,否则返回False
8 R) W$ {9 _7 `9 c6 {4 [( n# v( Y - isnan(x) -> bool
1 |" C% Q3 b o% `' W* g - Return True if x is a NaN (not a number), and False otherwise.
, ?! c, `4 n+ u" j- T, i/ J8 s3 C - >>> math.isnan(23)
% Y, l6 M$ e' {2 V4 r7 ^) b) s - False7 a9 c7 m8 d& o) w
- >>> math.isnan(0.01)$ n8 t, c4 _$ t* j# r2 R
- False
复制代码 * @: O* W$ k# @' q7 U
math.ldexp(x,i) 返回x*(2**i)的值
- W0 j- X1 C* j- G: U- #返回x*(2**i)的值
: I6 J0 q4 v( S8 M0 C+ u8 @3 j - ldexp(x, i)$ l# Q; [# w* R: v* A) I+ m
- Return x * (2**i).; k* u' @* E* {* V; `2 F
- >>> math.ldexp(5,5)/ A6 O. t0 h- O1 s% m
- 160.0
3 B# M% l0 _' V ]# t# W; L - >>> math.ldexp(3,5)
( Q9 M p+ C4 J( C. h* ~7 [. [8 k - 96.0
复制代码
% H2 R' a( I- Q" [* Y, r) a) emath.log10(x) 返回x的以10为底的对数
" D* b3 m! P- W/ [. A- #返回x的以10为底的对数
8 H# T5 ~% `5 U# y5 E# U - log10(x)7 |; C& Q: g9 V# X5 R# b/ W
- Return the base 10 logarithm of x.3 o9 ~/ j+ p1 T, W e* P2 s
- >>> math.log10(10)0 Z ?7 U# g J3 W5 v: t, H
- 1.0
; M6 S6 P6 f9 H - >>> math.log10(100)
D& Q7 r& G, ^, _- [4 H - 2.0
, l! H+ F4 a {$ t1 H; p - #即10的1.3次方的结果为207 A( `' {5 ]! O, d6 J: |2 z7 H1 a
- >>> math.log10(20)
: V% a2 | Q" }: u9 J - 1.3010299956639813
复制代码
7 H0 K+ W, C5 e: K5 o6 ~8 x% O. }math.log1p(x) 返回x+1的自然对数(基数为e)的值
' Q8 O; k! s4 E: F) X- #返回x+1的自然对数(基数为e)的值
4 d) ^. _" {$ l* a - log1p(x)$ E2 j! D: e5 x; B+ y! A9 H3 {
- Return the natural logarithm of 1+x (base e). c3 H. @/ i5 d8 }$ ^# H1 O
- The result is computed in a way which is accurate for x near zero.9 R. k! i2 N8 O+ k, ~2 a2 \
- >>> math.log(10)) ]- l5 W- f$ ?. u9 X
- 2.302585092994046: \- A1 _0 s. E- {; W8 n2 A
- >>> math.log1p(10)+ V* x0 s- W1 l2 U& S' J3 X( d
- 2.3978952727983707$ ^$ G9 R4 W2 o- M) V" C+ S7 ^; ~
- >>> math.log(11)
% w6 [3 Y. [: w; h - 2.3978952727983707
复制代码
+ K B( k) f' @, V+ I6 O5 Wmath.log2(x) 返回x的基2对数
2 P5 m; ^: D8 O! D4 t- b2 q- #返回x的基2对数2 H& y. d; L" c: u" i( U
- log2(x)3 X0 s& u2 I/ F% V& o' S# z9 d' }
- Return the base 2 logarithm of x.: I* D4 r/ k5 ?6 S+ I7 Q
- >>> math.log2(32)
; [! m9 \- b, }7 p# \! ?: t - 5.0
' E& W }4 S$ `& A/ m0 ^- F - >>> math.log2(20)
7 G7 K% z n- F5 c6 Z, | - 4.321928094887363/ `9 H5 b! U# N1 c1 G: {: a) Y
- >>> math.log2(16)! I8 V/ G, N& V
- 4.0
复制代码
" @: q+ L- J) r( R g9 E3 H$ dmath.modf(x) 返回由x的小数部分和整数部分组成的元组
% l z1 V9 J* z1 q. f8 L; a& O- #返回由x的小数部分和整数部分组成的元组
8 s0 h& Z: q7 {8 t# |1 ^ - modf(x)2 ^6 h* W+ L1 y
- Return the fractional and integer parts of x. Both results carry the sign; m! L, Y) j, s7 Z+ x0 ?
- of x and are floats.0 I! A+ {: q# Z$ P3 C. _8 Q3 T
- >>> math.modf(math.pi)
$ [0 _5 L" Z3 y: \8 Z - (0.14159265358979312, 3.0)
! e; [0 Z1 A9 y [ - >>> math.modf(12.34)
' `4 `, Y/ }( `* M- u - (0.33999999999999986, 12.0)
复制代码
& b" K% A# R. _math.sqrt(x) 求x的平方根
s% X I' {6 S- @$ A' Q% i- #求x的平方根
+ _' W+ `- ?9 k; t - sqrt(x)
: z- d8 j+ @( n4 |" R! X - Return the square root of x.
7 x H; j# h [, u5 ]$ x - >>> math.sqrt(100)
* X. W* v$ T6 L - 10.0, V5 e, J4 m4 J
- >>> math.sqrt(16)8 `3 M: n" R6 z6 d$ E
- 4.0
2 M( r! ~8 z% J* N" h - >>> math.sqrt(20)
& |8 w& p# L) b7 K; k6 x" l - 4.47213595499958
复制代码 & j! N: u4 ?0 M# `% ?. o
math.trunc(x) 返回x的整数部分- #返回x的整数部分 }* ~- Z! q) p0 m8 M
- trunc(x:Real) -> Integral
9 L2 U, I% I& d. ~- r1 j/ s6 e - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# E) H8 O g e4 z. Y- E+ D
- >>> math.trunc(6.789)& c2 R- O: U) ]. `3 E
- 6
6 V) A2 e0 [& `( g' C. ]; Y+ v - >>> math.trunc(math.pi)
' ^2 p B8 s7 c6 r( g3 B1 g - 3# R5 P3 N j! z6 G( J' m. E* A) L
- >>> math.trunc(2.567)
4 |5 i, l7 ~; q0 e - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|