马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
. g( @# e0 b0 S; T0 }% n, P; e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# Q* Y7 n5 T' j( U$ _0 ]: b
8 K/ a* T# }8 J. r3 f. f+ ^' |! B7 v6 y方法1:' |2 y5 k! k( n M2 a% k+ \0 R
- >>> import math
' z W \( k* ~% v0 i - >>> math.sqrt(9)1 J; i5 t6 r3 {) X& y, M
- 3.0
复制代码 方法2:; t1 z+ _* P8 q9 F |
- >>> from math import sqrt
1 o+ P) D" g" c( {4 Q$ p! l/ V! l - >>> sqrt(9): i; {# K2 h* G! F& U+ s% Q
- 3.0
复制代码
- ^# y* E3 s* x& g& p
. u2 k) i1 R# X( k' d4 lmath.e 表示一个常量3 B8 |, k. I) V
- #表示一个常量
2 d, A; A1 n8 M4 V7 g8 H4 S) W* \ - >>> math.e$ \5 z6 j, r. o; N$ h8 W, ^" [% n% h3 w
- 2.718281828459045
复制代码
% F% p0 m6 u; d( q8 y" Rmath.pi 数字常量,圆周率
! N9 D, y) K$ j( T2 f. f2 y- #数字常量,圆周率
9 I, S' Y4 q# r* p - >>> print(math.pi); K# j2 \5 p5 @1 U
- 3.141592653589793
复制代码
' z' x, p6 M4 w# k& \math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 T& \' n$ {. b' O! W7 e2 P- #取大于等于x的最小的整数值,如果x是一个整数,则返回x f4 j2 E0 a4 c' a3 I
- ceil(x)
& m6 r8 s: g2 m1 W0 C% f7 Y6 f$ b - Return the ceiling of x as an int.
3 c, p( X# w- Y% c7 K - This is the smallest integral value >= x.* X0 t' Z: \0 v1 {& `6 W1 w1 M
- & @! D7 I- n& M
- >>> math.ceil(4.01)
1 b! Q- g# ]; P - 5) n9 W. y, D% ?# m! L
- >>> math.ceil(4.99)8 [; y f0 Q/ ^( a3 H
- 5( }( z- J1 [8 i
- >>> math.ceil(-3.99)
! \+ l( b6 S( d Y$ w+ u/ V( b - -30 a! ?$ _6 x, h# H
- >>> math.ceil(-3.01)
k% m% V" X& w& M9 x - -3
复制代码
/ b0 `% w3 x' s! Y# Zmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
2 ]- c- c1 l; c0 j4 w# B( w2 a- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
W" s* u6 Y0 f- \ - floor(x)/ J" y5 N) F6 n3 F( n' Y" V: [8 n+ [
- Return the floor of x as an int.
e# l* q: ?1 e( D# {/ A2 Y! X - This is the largest integral value <= x.1 R) O- T$ G& e7 B7 L) R
- >>> math.floor(4.1)* \3 [: q$ N/ A3 a
- 4
% r$ n/ X7 A* B) M7 P+ ?* T - >>> math.floor(4.999). O$ c* m# S T
- 4. I, D' N1 f0 v6 W$ Y
- >>> math.floor(-4.999)- S1 F& Q; d! @, k) w
- -5- @: g# ~7 n5 H, X) H! \
- >>> math.floor(-4.01)) U+ `- I7 x9 E1 ~! V2 p
- -5
复制代码 5 ]; `% w# q" j
math.pow(x,y) 返回x的y次方,即x**y9 `1 O$ c) |, G2 j& X% s+ R: ?7 x/ a
- #返回x的y次方,即x**y* }5 ]+ |6 r$ A7 [9 Y$ K
- pow(x, y)
0 D7 c5 z/ W- Q' j7 J - Return x**y (x to the power of y).* N& D- } L& w
- >>> math.pow(3,4), T8 m* h2 ]3 ^- Z% S+ l: D
- 81.0
w# u' c5 a' _; x7 s - >>> @! r' z) E' @" a. p- {8 A
- >>> math.pow(2,7)3 [8 m9 o/ I2 _6 X9 H% \. e
- 128.0
复制代码 " V9 B. q9 N; p" `/ g+ ^+ {
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 U+ `% u7 E* f! C- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ F' }$ y& H* q$ Q( A7 P
- log(x[, base])
( B. V" \: b& h1 }4 G - Return the logarithm of x to the given base.
( L+ a+ a7 {, u% o, F( d - If the base not specified, returns the natural logarithm (base e) of x.
: E- \ B9 I7 J' z - >>> math.log(10)
" h; S" C3 d% q: \$ Z7 G7 ^- e - 2.3025850929940462 ?! J) l$ x5 j( t
- >>> math.log(11)
: ?% o( }5 x: [: p - 2.3978952727983707 W9 ]& i3 j% X7 u9 }7 A. F! u) K. x
- >>> math.log(20)
4 g) n e2 V2 f) `" y - 2.995732273553991
复制代码
4 d7 n+ U' u4 n9 z1 W* V$ L& wmath.sin(x) 求x(x为弧度)的正弦值
. i4 t4 n% M8 f, e% w, `8 K V7 ]- #求x(x为弧度)的正弦值
) e' f1 L' q5 t B. p# t - sin(x)- R& S/ s; z, {) j* j# L
- Return the sine of x (measured in radians).
: T2 P$ n$ A! h8 v( b; p9 C# c - >>> math.sin(math.pi/4)
o5 K3 Q2 i$ n( C3 r6 S - 0.7071067811865475: M' K( F7 M- Q; P6 n
- >>> math.sin(math.pi/2)
0 m7 z! |* \! r3 D5 b - 1.0) u; z. G% {0 C% H/ e7 O# A4 l" {
- >>> math.sin(math.pi/3)9 b$ R5 A2 }' n6 a
- 0.8660254037844386
复制代码 ; ]% o+ k7 r1 w0 V; L U0 G
math.cos(x) 求x的余弦,x必须是弧度
, }& y+ T/ x& e: L& ~- #求x的余弦,x必须是弧度
, f. H& H6 ~9 I% R6 G - cos(x)* c: H: i! Z. Y
- Return the cosine of x (measured in radians).
5 @& V. E. R2 p3 Y - #math.pi/4表示弧度,转换成角度为45度$ j( L) A- O8 r5 m; h
- >>> math.cos(math.pi/4)
/ K+ ^4 d9 y8 b6 U" d7 O# | - 0.70710678118654762 N% O% u9 l. n$ m6 g7 t- \8 B
- math.pi/3表示弧度,转换成角度为60度
1 t2 E0 G( S0 u9 \- L& D - >>> math.cos(math.pi/3)
4 O6 G2 v0 l" ]; p - 0.5000000000000001- U: c! o& c6 E; g2 v1 V
- math.pi/6表示弧度,转换成角度为30度9 h" u9 A8 L* V% I/ l: c* c# R
- >>> math.cos(math.pi/6)
7 S. M5 F8 i' s3 e8 c0 E- G - 0.8660254037844387
复制代码 0 U" z m- F, b) M6 d+ ]
math.tan(x) 返回x(x为弧度)的正切值) w N2 |/ i8 O; {# M
- #返回x(x为弧度)的正切值
: |3 t0 O" b2 ]2 ^# r: L - tan(x)7 x, J3 z' j+ T$ d5 Z
- Return the tangent of x (measured in radians).' }: x2 I4 w% [" g
- >>> math.tan(math.pi/4)4 b+ I/ W/ c' ~3 V
- 0.99999999999999998 x8 j! z: e# t
- >>> math.tan(math.pi/6)
% d2 o+ [$ O9 Q# E) b - 0.5773502691896257
D. y7 Q( C5 n& A3 z' D, U - >>> math.tan(math.pi/3)8 y9 S7 L3 P* H% @: u1 K9 v" P. H
- 1.7320508075688767
复制代码
7 s& H2 a1 z+ m- gmath.degrees(x) 把x从弧度转换成角度6 b9 j( I7 Q, @7 S) B+ b M/ k: A
- #把x从弧度转换成角度; i( d9 h2 o' Z6 e" p
- degrees(x)
2 S6 C1 [/ V( o - Convert angle x from radians to degrees.! C( w; z0 u; ~- N/ O' z
2 c" r" \3 u' B9 c- r- >>> math.degrees(math.pi/4)+ C# q0 S: u1 w# j* M4 i. ~0 O. E3 W
- 45.0
9 X- n# M0 ?# V b/ c# @ - >>> math.degrees(math.pi)& [- k6 }. `6 @. u
- 180.0' H; {% y3 |6 |# h
- >>> math.degrees(math.pi/6)
: f4 p4 p3 ^0 N) x4 t; u - 29.999999999999996
: |; H/ R4 S7 W - >>> math.degrees(math.pi/3)
" o7 b0 f7 o# \% Z3 [ - 59.99999999999999
复制代码
: g) D6 d# L4 t3 M3 Omath.radians(x) 把角度x转换成弧度
" Z* [2 w, _( C- #把角度x转换成弧度
' z0 V6 E' C/ B# J6 P - radians(x)
2 Y) T( B$ { Z! G: Q - Convert angle x from degrees to radians.! F$ [9 A, [; i! L( O# p1 N
- >>> math.radians(45)6 f) M4 e( g9 P- Q* M6 y
- 0.7853981633974483/ S/ n2 c/ X/ L! ?1 r8 Y
- >>> math.radians(60)
1 N( d: T. C% E, ?; W) g - 1.0471975511965976
复制代码
" J# a# v$ n6 jmath.copysign(x,y) 把y的正负号加到x前面,可以使用00 i- q0 q' t s( T$ C0 V
- #把y的正负号加到x前面,可以使用05 W8 F" ]" v- O |6 V/ e
- copysign(x, y)
2 w* J0 q! b0 ]3 a* d# ^% W - Return a float with the magnitude (absolute value) of x but the sign z6 I/ n! P9 y8 V: d! V' T3 B6 r
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( J. h& j1 j! H. d7 ]1 Y
- returns -1.0.
: g9 _6 c$ ~' Q& C. V
& ^$ b: t5 t& w# i- >>> math.copysign(2,3)7 B8 p; N$ J2 [6 Q7 G2 g
- 2.0' {1 o9 x( J0 ~2 w: E
- >>> math.copysign(2,-3)6 F! L% e6 g: _5 q+ A
- -2.0
0 ]# c- e3 ^; [- o( h - >>> math.copysign(3,8)
1 B( {: p( _8 L5 V" P6 H# b: R - 3.06 ~2 c G' b/ B, B, Z, `
- >>> math.copysign(3,-8)' }8 ?) W% V3 V3 d& A2 E! R
- -3.0
复制代码 8 }, w8 r4 m( w- N2 k: ^$ n
math.exp(x) 返回math.e,也就是2.71828的x次方8 `: _' T" R! h! P& v
- #返回math.e,也就是2.71828的x次方$ L8 k% x' ]: p: f$ X4 ~0 l" c
- exp(x)
0 W1 h: Q' f- R5 Y' ~, R/ ~$ a - Return e raised to the power of x.7 d# H. K$ k0 i. R$ M( q7 B0 J: H& G
- - {# ^! T! s, m# v2 [
- >>> math.exp(1)
. `9 P0 v: f2 `$ r - 2.718281828459045
6 k) D" T6 x; i6 ? - >>> math.exp(2)) k# c) n3 N% o% u3 L2 m2 J7 F/ ?3 y
- 7.389056098930655 m: w8 s: F$ C
- >>> math.exp(3)
$ n& b& F) Y8 s b1 F - 20.085536923187668
复制代码
) w( K W$ n- d5 h" r3 umath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1( n! u; f# M, k3 ]
- #返回math.e的x(其值为2.71828)次方的值减1/ B- ~. B" [- T9 l
- expm1(x)9 ?2 S$ m* _4 Y4 l) T6 F; f6 o* r
- Return exp(x)-1.
7 h2 l) }# _8 u* g3 ? - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
& m1 p/ ?: _' n, ~6 k; U- L! m/ C - . _ |( D( B! m4 }
- >>> math.expm1(1)
9 F4 S% H$ p8 }2 ]! A8 l: n - 1.718281828459045
7 X+ h1 G3 |0 r1 E* V1 A5 G - >>> math.expm1(2)" w4 M5 h( [ |5 e
- 6.389056098930654 C; `( D0 m ~
- >>> math.expm1(3)- \2 b; i6 e6 i, |1 r5 ^$ ]
- 19.085536923187668
复制代码 % X; [: k g3 \- D; e! T" ]& l
math.fabs(x) 返回x的绝对值( M# y8 k1 E \. p7 D
- #返回x的绝对值
; y& ^8 K( A+ _+ C - fabs(x)
- w0 t4 Z- \& d1 I# N/ t. U - Return the absolute value of the float x.0 a! K% ~$ ^3 k3 K2 ~; `
- # Z$ b) Z# O: P! P, g2 J
- >>> math.fabs(-0.003)$ p9 z' F% M9 y" F) D7 n1 @# \
- 0.0032 a* t, y+ m% r: E. H, h% v# z
- >>> math.fabs(-110): J0 O6 T: a7 Y" a8 Y
- 110.0# q5 Y( g2 I* Z3 s, H$ R
- >>> math.fabs(100)
4 T i* c+ i) s - 100.0
复制代码
: C. _$ Q3 H6 Qmath.factorial(x) 取x的阶乘的值/ `# ^# Q! Q) A. @( u; m
- #取x的阶乘的值
2 N+ Q+ \0 \3 N1 o6 L9 t0 d. a - factorial(x) -> Integral
' ]$ r- {" l- o+ L, r - Find x!. Raise a ValueError if x is negative or non-integral.
4 u8 Q/ N) J- o - >>> math.factorial(1)
3 X' ` A9 e# D+ j/ _0 b$ t7 D# _9 E7 N - 16 F9 o1 p' y5 _# [, x
- >>> math.factorial(2) `3 z6 ~8 h! g9 f4 R" H
- 2& F/ X" x) s% _8 y; l+ V5 r7 c
- >>> math.factorial(3)
' q% ~& a! |* Z - 61 u. X9 j4 J. c* c# y) R
- >>> math.factorial(5)7 V5 b. K& c, O) r3 j2 c9 n$ Z! {
- 120
/ r! Q- }0 i$ R0 L6 Y - >>> math.factorial(10)
! x, a# P0 } x8 o% u& }) ^ - 3628800
复制代码 8 T1 g" {0 R/ p c# B1 s
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数% J% P4 E% m# v4 j& i7 c8 B
- #得到x/y的余数,其值是一个浮点数5 z$ `. k' }: A( ?/ }
- fmod(x, y). a2 `. a+ [% r6 p: c% i0 h
- Return fmod(x, y), according to platform C. x % y may differ.
5 K6 ]$ ^2 [' D$ ^+ l - >>> math.fmod(20,3)
. f3 G* Q# u5 X - 2.0. g3 U9 b8 p! A7 l& n8 z
- >>> math.fmod(20,7)
. H. U1 ]# Q* u1 E4 z - 6.0
复制代码 3 ~3 u+ l0 a; D. v' W2 {
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; m$ E6 d+ j$ x# C3 l- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 i8 o+ `/ G1 l& i& _% i
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
) a+ c) e. a& @5 J3 f - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; O& \2 D% s) ]4 w
- frexp(x)
: t+ ~) d, J r9 N! A8 w0 j - Return the mantissa and exponent of x, as pair (m, e).
c6 O: I3 M* ?! A - m is a float and e is an int, such that x = m * 2.**e.
2 G) N; S) L$ ^) U9 E- S/ e - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
3 j3 T) E8 D: F" i, m" [$ X - >>> math.frexp(10)3 D/ l9 h. @6 J- M) S
- (0.625, 4)+ m7 B0 R/ e- N3 W9 E
- >>> math.frexp(75)
% u% M0 I/ `1 ]) e0 x3 X - (0.5859375, 7)
$ f1 }" D* J3 P7 h/ w5 } - >>> math.frexp(-40)- n% B: L7 r0 H6 _4 s1 n7 ?5 \' @
- (-0.625, 6)
# h' O0 C7 S! r. W - >>> math.frexp(-100)
8 Z0 g$ }, z# F0 H9 `3 V - (-0.78125, 7)
s7 g/ U- q& [ l- a0 k2 r. ` - >>> math.frexp(100)2 F) e# B5 I" g( t& L
- (0.78125, 7)
复制代码
% M A: K5 D% ^ c; r/ lmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 R ~) [' L1 c7 q/ D& x3 k' h- #对迭代器里的每个元素进行求和操作
$ ] L8 Z: s& J- w: b - fsum(iterable)
. J' Q3 o+ g4 q) g - Return an accurate floating point sum of values in the iterable.* b5 m: p4 E4 g2 [' s" N! l$ b( Y
- Assumes IEEE-754 floating point arithmetic.5 E, }8 u$ i9 w7 i- t6 S s
- >>> math.fsum([1,2,3,4])
: G$ Q5 C! o& v; o! f' e- ] - 10.0
$ {8 R& c* ]: }# a - >>> math.fsum((1,2,3,4))2 L7 U9 i3 h# y' W1 M3 X2 R# g
- 10.0: i" P* w$ p/ s) n$ |
- >>> math.fsum((-1,-2,-3,-4)). h" f$ n0 Z# W W
- -10.0- e. _3 {1 p B. J
- >>> math.fsum([-1,-2,-3,-4])5 S4 Q! f/ o: A, t+ u# V3 }
- -10.0
复制代码 : y+ t9 ~# s& F% J+ G! f7 z( Z
math.gcd(x,y) 返回x和y的最大公约数$ _! U, I3 }3 ^
- #返回x和y的最大公约数
% {/ v& j) Q3 C3 U% g6 h* h% T - gcd(x, y) -> int- e7 t! y1 f# y, I# ~- e5 c! k$ V
- greatest common divisor of x and y. h9 B$ L, k; @- ?
- >>> math.gcd(8,6)
5 z1 t3 W4 p* j! t - 2) X2 F3 ?' P- [3 `4 i* [" w% L* k
- >>> math.gcd(40,20)
4 ^1 @8 d+ M9 R - 20
$ e. a6 \5 Q" d - >>> math.gcd(8,12)
" S3 [' ]# q; ?" ]# R: I! r - 4
复制代码 " B7 n: K* q/ C7 V' l
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False. f4 i! w7 @ B9 q: T# H+ e# D8 s+ y
- #得到(x**2+y**2),平方的值
+ H/ P6 M* u/ ]( v! V - hypot(x, y): O! Y5 @7 {6 |, H: b0 g
- Return the Euclidean distance, sqrt(x*x + y*y).8 q/ |1 a% F, P* h
- >>> math.hypot(3,4)
" r7 M0 t2 w( h$ N - 5.0% v( G. W0 }. P8 Z% }
- >>> math.hypot(6,8)2 F7 s0 O& N: J
- 10.0
复制代码
) r+ c6 L) D8 ~3 }7 |8 t' P: }math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False$ o: e) j' A: n
- #如果x是不是无穷大的数字,则返回True,否则返回False
4 j) o& r& [' A) g- v1 s! M: r - isfinite(x) -> bool
1 X* m4 O2 Q: |9 K - Return True if x is neither an infinity nor a NaN, and False otherwise./ [! m$ m9 Z1 _9 F. B% ?0 r3 Z
- >>> math.isfinite(100)
8 f) b0 \( T' }( J u - True
8 [; P9 \9 Y: V8 q7 W) } - >>> math.isfinite(0)
4 N: p; b9 v2 f% d - True
v3 V) h( ~% C6 Y- b) m' P - >>> math.isfinite(0.1)3 c5 a8 T# v/ h" g( Y
- True; J3 q2 E. F# m- u7 u0 }; ?- T, P5 s
- >>> math.isfinite("a")- y- b9 v: J. h' |% t% k$ g/ n
- >>> math.isfinite(0.0001); j5 y7 X! K _7 m6 O
- True
复制代码
; C% j" R5 i- ^1 H% \/ A$ Nmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
: b9 [, ]# @; y3 D' D8 V) \$ z- #如果x是正无穷大或负无穷大,则返回True,否则返回False
% @1 }% W2 J# Z2 Y: @ - isinf(x) -> bool
7 [& R) A% q+ H x: g4 _8 [ c - Return True if x is a positive or negative infinity, and False otherwise.4 {& ?1 l! ]6 J. ~. H' v! d2 I
- >>> math.isinf(234): y2 Z& B( j: k ~& b8 c: {
- False3 V; u0 @& b {2 B- x9 V' _
- >>> math.isinf(0.1)
0 L& r+ E6 s" t) {4 v2 Z( J - False
复制代码
6 w4 T! p' { j h4 J- imath.isnan(x) 如果x不是数字True,否则返回False
* D+ S3 X- B: }/ [: W( }1 O0 B- #如果x不是数字True,否则返回False
" _1 E& R4 x0 @! f, s, M - isnan(x) -> bool) Q; L' e4 e- J4 F
- Return True if x is a NaN (not a number), and False otherwise.9 \3 X1 Q P4 k: \4 V& K
- >>> math.isnan(23)! a" o ]. }4 x
- False
+ v( Q. ~8 k. J& |3 T5 I - >>> math.isnan(0.01)3 T' ~) r' n! z
- False
复制代码
5 T: _0 W) [0 V" m( J: s6 |math.ldexp(x,i) 返回x*(2**i)的值" |1 ~6 ^9 r4 H) A
- #返回x*(2**i)的值
' ~9 C8 U' ^, L# X0 U/ t) U - ldexp(x, i)0 x% i: i& g# e7 t% {7 c4 Y2 _; W
- Return x * (2**i).' ?. l2 `8 S$ a( I. |4 v+ X
- >>> math.ldexp(5,5)$ Z1 p0 z% c: Y4 I2 o6 r
- 160.0
$ f. O/ w# b3 a2 x! v) q - >>> math.ldexp(3,5)
2 _. |* t+ P" n( U - 96.0
复制代码 * Y0 l7 N* f2 t9 U
math.log10(x) 返回x的以10为底的对数
$ K( M& E; h% X1 U+ U, w$ I6 A- #返回x的以10为底的对数
5 M# e& o5 M$ |8 Z - log10(x)
4 {/ ~* [. T& v+ g+ J' `: z+ R4 E# r - Return the base 10 logarithm of x.+ E$ }5 n: S* T
- >>> math.log10(10)) W' \; W3 K+ b0 @2 H& [3 ~$ V
- 1.0
, Z* Y* \. i5 x% ]) s5 a) j - >>> math.log10(100)& \$ }3 l5 z, J/ ^8 z7 {; a
- 2.0
) z6 h$ Z$ L& }2 j+ a - #即10的1.3次方的结果为20. U. j1 E' @- l& z3 ` g# O& W j
- >>> math.log10(20)
1 M8 k f U6 P$ t% u* V& d% R - 1.3010299956639813
复制代码
4 K) u' H% `# L1 v2 y) Gmath.log1p(x) 返回x+1的自然对数(基数为e)的值' c& ?# b$ X T" P! f& J8 m8 J
- #返回x+1的自然对数(基数为e)的值8 Y6 ?! g) Z. B! W- ?
- log1p(x)
6 x& [+ b+ b# ~9 ~$ L/ G' H - Return the natural logarithm of 1+x (base e).
$ V- s$ Z* y0 C% u - The result is computed in a way which is accurate for x near zero.
6 I! w" Q. M1 F {- E" g( Y - >>> math.log(10)
1 Y9 h2 D# p. H' e - 2.302585092994046; ~2 Z4 e; O( R
- >>> math.log1p(10)5 R, @' f2 F6 }+ c8 b0 {: M
- 2.3978952727983707
& h. l( r) f3 \# f - >>> math.log(11)6 }% k: P& s- [1 v& L
- 2.3978952727983707
复制代码
0 O- ]! ^. {1 R/ x, W- L4 _! s0 s( emath.log2(x) 返回x的基2对数% W1 b0 e' M7 D- r' y7 m2 ?
- #返回x的基2对数
f8 I# V5 j7 v4 Z ?% | - log2(x)
9 H) D8 K5 m3 n" u" q - Return the base 2 logarithm of x.$ S, D. r1 x9 c4 ~
- >>> math.log2(32)- V3 y! }0 J5 V* C V5 G4 u* Q
- 5.03 _( n6 y3 K$ Y6 I0 n" l
- >>> math.log2(20)3 d8 W4 m! M3 b
- 4.321928094887363( w7 b( C8 p/ f- I# x5 k( Q
- >>> math.log2(16)
, j# j& r! a7 i$ _' G - 4.0
复制代码
# y7 w* J6 P6 Tmath.modf(x) 返回由x的小数部分和整数部分组成的元组, W# g3 ^' U' h) d# T- }
- #返回由x的小数部分和整数部分组成的元组
4 p( |3 D3 f) |. A+ r - modf(x)! p2 q, |' i8 N4 M/ s0 C+ ]
- Return the fractional and integer parts of x. Both results carry the sign* W8 ]5 b; c- L
- of x and are floats.0 v2 L) d7 Y* G- N' W
- >>> math.modf(math.pi)
* @- H: e# n# L4 U! x - (0.14159265358979312, 3.0)
; s8 C4 s: `! F4 J/ L - >>> math.modf(12.34): I- s' ?& W! d, q
- (0.33999999999999986, 12.0)
复制代码 ; S3 Y% V8 ?) E2 s/ J
math.sqrt(x) 求x的平方根
9 ]0 G2 j. G" P% e1 x; [2 [- #求x的平方根5 t! s9 T8 A0 s
- sqrt(x)
) O7 H5 e" M: _1 } - Return the square root of x.) i w* G6 O& U& W& N: W$ M
- >>> math.sqrt(100)8 g d8 @; j3 S: t1 \
- 10.0( @2 J: e1 {/ G! ]
- >>> math.sqrt(16)
* ?5 h" f, f7 i - 4.0
+ {9 f. @6 }2 ~0 K2 g: f( N" @1 k - >>> math.sqrt(20)
7 M5 d9 }# y# H2 J: x b n+ h! C - 4.47213595499958
复制代码 ]' ~2 H9 s1 e& J9 f/ j
math.trunc(x) 返回x的整数部分- #返回x的整数部分
+ R# l. v" p* b( X& ~ - trunc(x:Real) -> Integral
* S: [2 ?. }/ M - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
* d, E# F9 m9 b5 M ^; e) \& N - >>> math.trunc(6.789)
, D9 M* a( e' ?8 D - 66 |3 W/ }# m. D- ^: p
- >>> math.trunc(math.pi)" p9 e3 E- t/ z. ^& I
- 3
, E, L6 r8 C3 d; L - >>> math.trunc(2.567)
' v0 R7 J7 w: o9 `2 ` - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|