马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
; W) j5 V: [7 ^) e
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! @/ g- `" G. r7 K2 P# A* [9 E2 p
' g+ I3 [7 {. C/ v' |8 d6 D方法1:
0 G2 r5 {9 v6 v- >>> import math
8 Q" ^3 L( l2 k/ o8 ~ - >>> math.sqrt(9)+ p# t/ \4 Z7 ^
- 3.0
复制代码 方法2:
1 R( o }# w$ A2 ^: K" X- c3 i! f- >>> from math import sqrt7 }3 k f- z( K; e# Y: v
- >>> sqrt(9)
; {, A* p# D! z/ x0 O - 3.0
复制代码 2 I- f6 M* j7 c3 P! j! q7 f' B6 ^
6 N3 q# c" J: ?3 @
math.e 表示一个常量' e1 b5 H) {# T3 b- N1 B
- #表示一个常量2 l$ F* u+ t0 \2 p4 V0 {) ~& p* \2 p
- >>> math.e) [3 ], M L# F3 S) U0 g+ P
- 2.718281828459045
复制代码
! v1 z# h7 `2 d6 f, ^, M( X' Dmath.pi 数字常量,圆周率 q. {; W$ N4 o& Y$ W, ?( F3 E
- #数字常量,圆周率
- u0 F& J/ G W4 I9 z3 _ - >>> print(math.pi)
4 L9 ^: w" }+ q: d4 V - 3.141592653589793
复制代码 6 L- @ n" z, x
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x, K9 h4 M! w p. N
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 G# d) t( k7 E5 F6 f" m7 o - ceil(x)
& H4 B k6 d7 z- L- B! v - Return the ceiling of x as an int.4 Z# T2 w \% U Q( q, w
- This is the smallest integral value >= x.+ l1 q4 P# t ^" s3 S$ v+ U; L9 v$ p3 n
6 t7 g" r! f0 w/ M4 `* e5 J: o- >>> math.ceil(4.01)
2 r+ _8 s$ {8 J4 W& ?! p - 5
( q' F. m6 z' S% _7 X* V' {1 h - >>> math.ceil(4.99); Y3 {7 I# K4 O+ D7 B) _3 F* Z5 o
- 59 k& g, l$ p% L; \9 d
- >>> math.ceil(-3.99)
, R3 C5 P* S: z ? - -3' J3 g% V8 x( p9 W
- >>> math.ceil(-3.01)1 \, a* \1 ?( w4 B6 H7 l( c
- -3
复制代码 6 C7 b: o5 d4 O, Z; c8 o
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身" k' d" w0 @ D5 z0 A
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ E$ w: c: Q, R2 Q: j/ W; Q* L - floor(x)5 I8 `' ]9 Y( ~& }
- Return the floor of x as an int.
" T! X0 X( U5 c w; W: {4 x. N - This is the largest integral value <= x.
) I4 X; k) I& z! w' Z6 I" G - >>> math.floor(4.1)
' |- {! ]3 N0 Z$ z7 b - 4
7 z" k R l& B- t - >>> math.floor(4.999)$ A7 e8 G( z: j' P7 U& G+ [! A
- 4
1 i; a2 h e7 _1 Q+ S7 _ - >>> math.floor(-4.999)6 A8 x0 F1 w$ F/ w6 o2 j2 \2 a$ c
- -56 |8 @; \/ M; f8 Y" d, ~
- >>> math.floor(-4.01)
3 |* U8 J+ z( p: q1 ^ - -5
复制代码 : [4 z1 ]' t! X2 t! o& |. y
math.pow(x,y) 返回x的y次方,即x**y
: g( T) k. x' U$ U* o- #返回x的y次方,即x**y
& E" U1 ~1 q7 W# a1 x - pow(x, y)
2 M% i- t. N: H+ M) | - Return x**y (x to the power of y).% X2 H5 Y/ c# {
- >>> math.pow(3,4)
+ Z; c, L( E* U' B. D' o7 M - 81.0
0 y0 K" l# x# v4 j$ v - >>>
( T1 R8 o. ~2 {; T! z - >>> math.pow(2,7)
' C' ]* y: z2 P8 K; j: V. R, P; @ - 128.0
复制代码 * |0 t. P; |7 l4 U( [$ g
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 x3 ], }* C- g7 y- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): W0 X$ Q' L# K* f r+ y6 B
- log(x[, base])
0 V- G4 K3 l) T: R' e - Return the logarithm of x to the given base.
) D# g5 Z) E0 k7 J( ^. a" i - If the base not specified, returns the natural logarithm (base e) of x.
A5 g2 b( S" q& w* [5 T6 D( v6 z - >>> math.log(10)
$ ?6 D' R: _# z7 w: e/ } - 2.302585092994046
) A: V) `& b( e, v. ~% I* J - >>> math.log(11)) G/ h& a" T5 |/ A! n7 c2 d
- 2.39789527279837071 Q7 H+ g; C) _
- >>> math.log(20)
4 G( ?9 S* ]. |$ B& c - 2.995732273553991
复制代码
# Y+ ?6 w% L# |" Emath.sin(x) 求x(x为弧度)的正弦值
1 [7 V' X! K( Z9 F7 K/ R- #求x(x为弧度)的正弦值
6 s; z+ W, K+ \; v - sin(x). v- S# ]$ E, l. m1 c* G
- Return the sine of x (measured in radians).
* Z) l) L8 O, s3 u) B - >>> math.sin(math.pi/4)
/ B6 R1 C8 H3 m! V: \ y- m$ z& s - 0.7071067811865475
[( O- q* o- N0 D( h1 K( g9 T; p8 x - >>> math.sin(math.pi/2)) ?6 T+ q6 k" R: Y: f8 e
- 1.02 Q5 `+ k) B) s' c( h' r1 b2 G% }& p
- >>> math.sin(math.pi/3)
- O( Q7 F. o# F - 0.8660254037844386
复制代码 & a) T' e( f) h; L7 Z. S. ]' X2 j5 ~
math.cos(x) 求x的余弦,x必须是弧度
1 h- M$ B' M) |2 P7 D- #求x的余弦,x必须是弧度
4 ], y7 S% s* Q1 h4 I2 X4 \ - cos(x)' }1 s9 T3 G" i* U0 }. b
- Return the cosine of x (measured in radians)." B; M) L* H9 G6 q
- #math.pi/4表示弧度,转换成角度为45度* F0 r0 a }# G- w, F1 ~8 X8 d
- >>> math.cos(math.pi/4)- k7 Z: U0 X% y
- 0.70710678118654769 u" v- @7 n" {- k0 L
- math.pi/3表示弧度,转换成角度为60度
2 @8 _ [8 q& @9 G7 }$ H! I - >>> math.cos(math.pi/3)
* a( n. o8 S* J; K# e) ~: k. ?: w% a - 0.5000000000000001+ F2 n6 S- M; Z o. w( _
- math.pi/6表示弧度,转换成角度为30度
. r1 u% R. e ?8 \2 n - >>> math.cos(math.pi/6)
* _1 ]- F# d. V' T5 n. Q( l - 0.8660254037844387
复制代码 : L" q6 q! S- j, j! H
math.tan(x) 返回x(x为弧度)的正切值
- E1 }' }5 F8 t* }' h, O7 F- #返回x(x为弧度)的正切值
; w8 ~& z2 V' f7 _; z: ?5 _ - tan(x)
# | R& F- ]- D8 ^: ^' t: T! J+ a - Return the tangent of x (measured in radians).
% b5 t/ O; Q1 r$ B9 G B* b - >>> math.tan(math.pi/4), f# M% b) n0 ~* z( l" N: M
- 0.9999999999999999+ S4 _5 w$ ?; x; y, @! R/ r
- >>> math.tan(math.pi/6)
% F7 L, p7 u) Q3 y - 0.57735026918962570 l) O A* J8 {0 n% K. |( t. m( X: C8 Y
- >>> math.tan(math.pi/3). ?& R9 C; a8 `2 x
- 1.7320508075688767
复制代码 + o, w3 D6 R+ V" z9 ]7 O) C
math.degrees(x) 把x从弧度转换成角度
8 P# f; _: |! K2 m' }- #把x从弧度转换成角度% }6 X4 x3 i* s6 a6 i8 t
- degrees(x)
6 S& R: p' a, O3 z9 l% g - Convert angle x from radians to degrees.
. {+ w3 \7 ?, y* h& k4 l7 O
+ ^' `, u# @% Y3 \/ }. ?+ N- >>> math.degrees(math.pi/4)1 [. J' x1 u& P$ l& D
- 45.0* J m; \( g2 }6 @- C
- >>> math.degrees(math.pi)
/ P: `2 K5 w4 s( g2 z' i7 D - 180.0
* Q, i' Z7 r f" P. g9 S& N - >>> math.degrees(math.pi/6)
0 {7 l5 ^4 ^) N( H - 29.9999999999999962 b0 m9 o6 L+ W& O. g
- >>> math.degrees(math.pi/3)
3 |2 @, U0 }; O1 A- i5 C x - 59.99999999999999
复制代码 1 T, o3 ~( j) S' S8 G4 l! m
math.radians(x) 把角度x转换成弧度! Z R/ v( W- Z* |' k+ l9 {
- #把角度x转换成弧度1 b% F, O& t6 A8 E. @- c7 o' M
- radians(x) s& b/ k" D1 D: x( t
- Convert angle x from degrees to radians.
1 Z, T: {6 N% r" B: J/ m - >>> math.radians(45)' M ]5 Y- x0 ]- y8 B" G. T# ~2 K6 [
- 0.7853981633974483
$ i5 F0 u( [9 [7 a' y1 ~3 u( T1 b$ w# D4 Z - >>> math.radians(60): V, j9 |" b+ @
- 1.0471975511965976
复制代码
2 I5 l; D( U+ i& p; Cmath.copysign(x,y) 把y的正负号加到x前面,可以使用07 \( n0 M0 q3 ?! l6 j
- #把y的正负号加到x前面,可以使用0
* v, ]* \; o' Z- B% W! Q - copysign(x, y)- u0 g9 g9 s6 v& a9 K
- Return a float with the magnitude (absolute value) of x but the sign
" \$ }2 k! K$ Z" g9 ~% a3 R* L3 N - of y. On platforms that support signed zeros, copysign(1.0, -0.0) : i8 O+ S2 D. y2 k# t
- returns -1.0.
/ l% q% q! n5 d5 Q/ w& S! B - 2 {! I8 _. w d
- >>> math.copysign(2,3)
6 a) k% ?- [$ s+ z8 E7 Z - 2.0
$ Y' W% t( R5 Y4 b( |: j - >>> math.copysign(2,-3)
: H5 M/ d$ x; s3 E- r3 i# b - -2.0
/ H r& t6 P% l2 S# A. k5 P# ` - >>> math.copysign(3,8)
& k( L- s0 ^' p' s0 Y v2 Q - 3.0# u) t0 K- r4 \
- >>> math.copysign(3,-8)3 M% a0 G+ X/ ]- ~: D: W' b
- -3.0
复制代码
C& q- t& c- U( F; Ymath.exp(x) 返回math.e,也就是2.71828的x次方
% ^: @1 H0 }. @- #返回math.e,也就是2.71828的x次方( i c, G8 J8 |; ~( k( Z" k4 G# s
- exp(x)
* u5 p- X# q4 G - Return e raised to the power of x.
' ^0 a4 u* J: n* m: X/ _* d8 \ - , A; _1 R7 q1 Z
- >>> math.exp(1)- W! @+ I# J; u2 H! _: N
- 2.718281828459045
1 s3 J9 @# i' a" ` - >>> math.exp(2)
3 h- |4 }5 P, }0 C' {! q1 X - 7.38905609893065# Y5 d& m. C6 r% |/ u1 j$ O
- >>> math.exp(3)$ ^3 r' K" J+ }4 k3 X6 f9 J
- 20.085536923187668
复制代码
& Y, r+ u! e; emath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
8 X% a. x2 B; a+ f7 \- #返回math.e的x(其值为2.71828)次方的值减1
, |& @) `- ]- b. s - expm1(x)
- |) t" @ }. A% P! S - Return exp(x)-1.; u# K$ s' X% i
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. l3 G& V) C! P3 a7 V8 o) Q' v
6 o; t% w2 ]% w' x% A$ ?- >>> math.expm1(1)
- d. h s" d. X# ~ - 1.718281828459045% ~+ Y+ |. P' D# w+ S
- >>> math.expm1(2)2 T# I2 Q. N! O* c9 k9 I
- 6.389056098930653 @' h* W2 M' U4 O. ]/ O3 ]0 S! Z
- >>> math.expm1(3)
; P2 j* @- z- } - 19.085536923187668
复制代码 3 E8 q: J0 O0 O! ^, X6 w3 @- a% v. b1 i
math.fabs(x) 返回x的绝对值
( i" F/ V+ x0 I5 ~; x0 }- #返回x的绝对值# d: } {4 J& w! U
- fabs(x)1 b; b( q4 P+ ~: B5 _, Y* ?8 F4 H
- Return the absolute value of the float x.
: v n' V$ I c' r. q/ y
. t. u0 R5 W- Y2 T9 L+ ?- >>> math.fabs(-0.003), y4 W2 V. r3 ^# J. I1 t' O& F
- 0.003
( [& I9 l1 N9 _9 m - >>> math.fabs(-110)6 e, R- c! B( ^- ]4 _3 k
- 110.0
* l# l5 [0 t6 M5 ~8 j5 Y8 | - >>> math.fabs(100)5 q7 x: y7 \; b( K5 s
- 100.0
复制代码
: l `% l) K! C& X8 w* Smath.factorial(x) 取x的阶乘的值+ X( W/ l3 C" _, c/ w3 v6 @
- #取x的阶乘的值! ~7 {! k) g/ E$ h5 E- o; ^
- factorial(x) -> Integral
7 f" S* L% N5 [+ K5 G - Find x!. Raise a ValueError if x is negative or non-integral.
* U- }8 P3 K' Y) d; u1 ]+ h - >>> math.factorial(1)4 a0 `& z7 L/ A; C
- 19 l1 w. B; E' \' m- Y
- >>> math.factorial(2)
8 h- h+ k/ _8 W" X9 ?* J - 2
9 ]/ n! M6 J4 L - >>> math.factorial(3)
- l. u+ W* ]2 V+ E( R8 u/ X - 6' R y$ _5 H k3 A
- >>> math.factorial(5)
0 L+ F- _9 ]; N% y9 R - 120
* H4 Q, M; @; g3 |/ |2 ^& ` - >>> math.factorial(10)$ w9 p( Z z- m$ C N9 l
- 3628800
复制代码 . ]2 N2 i' X' m5 c
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
; N6 x' y: d2 k; i- #得到x/y的余数,其值是一个浮点数
% B, i: B; V2 Z6 A, E. L. g, b+ | - fmod(x, y)3 I6 |" b, A8 w" ~- ^' D
- Return fmod(x, y), according to platform C. x % y may differ.
+ M5 I0 I+ h4 R' }1 @ I" [5 f - >>> math.fmod(20,3)
2 F8 k# p1 R7 y% `: ]! z# E - 2.0
( i+ H3 w# U5 y6 O) @' D - >>> math.fmod(20,7)3 R# {, s0 [) ?9 N4 t( f0 y! ]
- 6.0
复制代码 ( [5 e; d8 b1 A
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 G1 X+ q, y; u1 V2 K7 F+ A
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ j* t' E, O+ ^# y% L1 i# N* v. [( t0 p5 [
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值) \; `& {6 n$ ?/ f7 T8 }
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 q& e. ^1 L0 B" o+ G0 j0 x' `
- frexp(x)
6 ~" ^1 }* K9 R! | k) s+ | - Return the mantissa and exponent of x, as pair (m, e)., v, v. |1 a; \" f
- m is a float and e is an int, such that x = m * 2.**e.7 ]. X7 H N( J
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.1 J. L! p0 R+ Y3 w- i
- >>> math.frexp(10)% T6 f/ w5 j7 u/ h& C
- (0.625, 4) m& d- S# [% ^$ S
- >>> math.frexp(75)
0 n. h2 E% O' S7 f) H - (0.5859375, 7)
& B& A9 \3 R( \6 b4 _* h+ Z - >>> math.frexp(-40)
2 p' ], h# R/ _* A - (-0.625, 6): L% s! {* i1 v2 x
- >>> math.frexp(-100)
% Q. H5 A5 o1 g: ~" j - (-0.78125, 7)1 p6 ~' e7 ~5 k f7 e
- >>> math.frexp(100)
# l0 U* u$ B( u' U4 H - (0.78125, 7)
复制代码 w9 z8 @' k; L- c
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
3 T8 q A/ E |' A: X4 ~9 S- #对迭代器里的每个元素进行求和操作2 k: K& l6 m0 a! N2 L8 r D7 m
- fsum(iterable)
+ [* K7 h& o- Q8 \ o$ ? - Return an accurate floating point sum of values in the iterable.
" E9 v; s9 O" s; z+ U# f+ ` - Assumes IEEE-754 floating point arithmetic.
9 Q6 g5 A2 x& ~ - >>> math.fsum([1,2,3,4])7 R, c9 n# f8 k8 r7 A* c4 ^: t
- 10.0- e; v7 ]+ j- x% ^1 Q
- >>> math.fsum((1,2,3,4))
' j9 z1 s/ V7 d) Y - 10.0
$ |3 \9 X( {& e - >>> math.fsum((-1,-2,-3,-4))7 f: X; I. p6 p# v' r2 u5 |
- -10.0
" Z- r3 z$ J, W! L4 ^6 n1 N$ u - >>> math.fsum([-1,-2,-3,-4])$ b$ f8 _1 E# C) o! Y
- -10.0
复制代码
6 B, z. U: m& u+ I# e9 R6 `7 D6 Imath.gcd(x,y) 返回x和y的最大公约数
+ E3 A; D: F: K. X& A- G0 P1 ^- #返回x和y的最大公约数6 J/ b7 X3 z% z3 F# ]
- gcd(x, y) -> int6 D- [. X& ~7 g5 S& ~% m
- greatest common divisor of x and y) a3 }2 @+ R; U9 O% p/ r
- >>> math.gcd(8,6)
: ?+ v" J/ w* ^6 k p( c# X1 V8 R3 n& _ - 2
- q7 I, q! Z6 o7 v7 C - >>> math.gcd(40,20)5 T) t: M* \$ z& G2 \3 y1 a9 d) O
- 20
* C: _; v/ l. z T( ^ - >>> math.gcd(8,12)+ c4 ^ a% B5 V0 r$ H2 l+ \! g
- 4
复制代码
0 q3 B7 @) |+ X) y- F3 fmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False/ ^. @! x: x( t/ \1 R
- #得到(x**2+y**2),平方的值
$ r- _# P9 _) r! g - hypot(x, y)
5 l, A6 ~2 ?9 P3 ?' f - Return the Euclidean distance, sqrt(x*x + y*y)." R# P$ Y+ c5 j i# t" F" W( [7 B
- >>> math.hypot(3,4)
7 W" U6 X3 G6 _- C; S" e - 5.0
7 k0 F& Z; e8 I A$ E - >>> math.hypot(6,8)1 |8 G( T, A0 N# m3 J" b; f9 ?
- 10.0
复制代码 5 }9 L' x4 c9 S4 m0 Z
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
7 t: V2 J; ~3 n3 r- #如果x是不是无穷大的数字,则返回True,否则返回False- \" m \9 t$ o5 f: N/ U. N9 d! @6 G
- isfinite(x) -> bool; e( s) V6 f' j: Z
- Return True if x is neither an infinity nor a NaN, and False otherwise.
& J; P& O( H( Q) P! d - >>> math.isfinite(100)
" e/ q3 `8 N) k - True
, T9 |$ O3 @3 K$ e. g% R- i - >>> math.isfinite(0) v* @! V1 i# N
- True
8 _4 }6 k# \' i+ o" z - >>> math.isfinite(0.1)' v \( \2 w6 l) @0 s# d. o
- True
3 o/ o7 v# d$ Z; q+ H - >>> math.isfinite("a")
3 v& W3 ]/ L2 u2 ] - >>> math.isfinite(0.0001)
5 t# Z) ?5 M) U$ d - True
复制代码 + B) @: c% Q7 y. W2 H5 M2 @7 C
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! H) R8 S1 r) `4 P- a, q- #如果x是正无穷大或负无穷大,则返回True,否则返回False
5 u! O; T9 e$ K( N; D - isinf(x) -> bool
4 ^. t9 s9 k( h& W - Return True if x is a positive or negative infinity, and False otherwise.+ T$ Y4 A* ~6 J- M: a5 f
- >>> math.isinf(234)8 H0 T& f! |4 l ~- [8 K' d
- False
3 Y1 ]8 }5 ?) S' t; g! _# L% [) m - >>> math.isinf(0.1)
$ z6 V& i" ^' G; y5 i - False
复制代码 ( z* a1 C; b" @) Y0 R1 } o
math.isnan(x) 如果x不是数字True,否则返回False
% b% E/ U7 t( {3 |. |. c' s) L- #如果x不是数字True,否则返回False2 f n% I L+ C- u5 m* {
- isnan(x) -> bool- N: k$ X& [; V5 M+ v9 ~' a8 y
- Return True if x is a NaN (not a number), and False otherwise.
w1 @0 E. o5 n' y - >>> math.isnan(23)
4 j3 D" R& }2 Z; ^ - False% Z% r7 U7 V' K
- >>> math.isnan(0.01)
; T6 n' z# }+ D6 ? o5 L$ u - False
复制代码
6 U7 i/ [8 U4 W0 ~. lmath.ldexp(x,i) 返回x*(2**i)的值" A& X: n$ }4 w, Y2 P8 e
- #返回x*(2**i)的值
2 {9 N! `$ a& D% m - ldexp(x, i)
4 e6 Q/ q- L+ c) R Y+ e - Return x * (2**i).) q* [/ v4 G' C2 b3 E
- >>> math.ldexp(5,5)4 ] ~3 o: p* I' J
- 160.0/ j5 a. D% C# H- b
- >>> math.ldexp(3,5)4 x1 U! C( J, `
- 96.0
复制代码
1 ?8 M! R: ]: g% ^math.log10(x) 返回x的以10为底的对数
' U6 }% ~4 r$ A! O0 |- #返回x的以10为底的对数
* {4 _1 x( v, A+ X; \8 |# G8 Y - log10(x)
1 u9 y0 Z/ b% H, o# N, d2 ^$ i - Return the base 10 logarithm of x.) Z3 Z( L% w1 O; k& j1 f h
- >>> math.log10(10)
: u2 @) s y$ X - 1.0; a' |2 z2 A' D( K; g" \
- >>> math.log10(100)
) U }6 |0 q9 a) l/ y - 2.0
S& o) r5 R* z' f5 x7 b - #即10的1.3次方的结果为20* _# Z: E2 \( L) x( D
- >>> math.log10(20)
: |3 _7 L/ o+ G6 x* E - 1.3010299956639813
复制代码
) X' z/ {- b( E0 j! L2 Gmath.log1p(x) 返回x+1的自然对数(基数为e)的值" P+ c7 s* v3 `' |: f: v, G
- #返回x+1的自然对数(基数为e)的值) @) N% _6 I$ d1 I& n
- log1p(x)
; q- U/ ^! o% C3 p* ^$ x - Return the natural logarithm of 1+x (base e)., [$ c' d. c n( a- x4 G5 o- p
- The result is computed in a way which is accurate for x near zero.6 B) H6 J1 _3 V1 s
- >>> math.log(10)7 K$ X8 J- s) r+ A7 l7 ]: D' y. x
- 2.302585092994046
! f; Y4 w! b, `) h) l - >>> math.log1p(10)- I2 ~) D- U( I$ w5 m
- 2.39789527279837073 I, u# ^" k0 l2 N# c0 H
- >>> math.log(11)
# O" [. f2 J8 o" o- d1 y1 a8 ?1 O - 2.3978952727983707
复制代码
' d3 B0 d" @! E- Y- ]" y' q( dmath.log2(x) 返回x的基2对数
3 p8 d6 j4 O7 {2 Y$ L& \- #返回x的基2对数
8 W p L9 @ W1 |4 t7 h, X/ O) u - log2(x); z, K6 D. U3 L2 h6 {7 g: J
- Return the base 2 logarithm of x.2 t( i3 z+ P, y* {
- >>> math.log2(32)
( K+ e/ w, ^6 F9 @ - 5.0- F7 B& O7 E: n* e% m0 |
- >>> math.log2(20)6 _2 b8 d5 K8 t
- 4.3219280948873638 l/ k- \( x# h. t7 R4 f! H
- >>> math.log2(16)4 H" [+ g; D$ m' V" l8 `! D$ n: G
- 4.0
复制代码 # q9 H- ?! ^4 L7 A: X. S1 i
math.modf(x) 返回由x的小数部分和整数部分组成的元组
5 ^8 ^/ j7 B+ b/ l$ a- #返回由x的小数部分和整数部分组成的元组' \7 _: g! x6 k6 g% v4 r
- modf(x)& g8 \) m8 _9 }
- Return the fractional and integer parts of x. Both results carry the sign" I) {% ]3 x E" n
- of x and are floats./ a0 S# t5 J/ g% P8 ?2 d) f
- >>> math.modf(math.pi)6 E5 m7 k0 k+ X8 G; G0 @8 Q1 Q
- (0.14159265358979312, 3.0)
! q% v4 g7 H& t- y. u - >>> math.modf(12.34)& r) |! S" D. L4 _" \- K- ~9 A2 v! T
- (0.33999999999999986, 12.0)
复制代码
- ` Y% t$ V1 Imath.sqrt(x) 求x的平方根
% ~1 c: j$ V0 Y' O2 p3 b- #求x的平方根
4 @. P; f0 i X3 K9 Y - sqrt(x): Y6 B5 r c* M# s
- Return the square root of x.2 ]6 t6 e1 S: \+ M
- >>> math.sqrt(100)+ |- g0 k4 c1 t3 s
- 10.0 D, g1 n" W4 ?( g2 h5 w. U
- >>> math.sqrt(16)
0 i+ W m( G5 d4 a! b! } - 4.0
8 U; z2 x' F3 I" f - >>> math.sqrt(20)/ G" }! q7 l0 U$ N2 J
- 4.47213595499958
复制代码
& H8 K8 x7 e0 h# v* x6 m4 V3 M0 }+ k2 emath.trunc(x) 返回x的整数部分- #返回x的整数部分
* Y T( i8 S+ L L7 y# x, R - trunc(x:Real) -> Integral# T6 X5 G% \5 s+ F) E$ ?
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% q2 L& i7 j* K# y x. q6 Y, a- _
- >>> math.trunc(6.789)
' d. T# M! p) X9 S0 w5 Y% l* ^ - 6% l5 I: g7 |; f3 H0 W5 D$ G; H( W. U
- >>> math.trunc(math.pi)
9 T O( ^5 F! _9 ?6 i1 e - 3
6 k: T$ ~& z+ E, N. M! b - >>> math.trunc(2.567)% b6 K$ t$ ]; j4 @ D" q
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|