|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
& ]2 Q' s6 |8 Q. m9 J. a- E! W) o9 |【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 w; F' ]# O( Y$ |* v
( o6 V9 O# D% Y8 m) W方法1:
8 b/ l" ?8 U+ }% h5 |$ i- >>> import math
/ u: N& \6 I! A' @+ S1 Z0 `' q) } - >>> math.sqrt(9)
! [/ z, D# R5 z; D3 l" R - 3.0
复制代码 方法2:
# w; ~! h8 b' c- >>> from math import sqrt3 e# d8 W$ D3 p1 w
- >>> sqrt(9)
* d8 S; u2 X7 x4 s T - 3.0
复制代码 + ^& Q9 b; f# E- K. D& t! F
& o/ A+ i: N( \: ]; n
math.e 表示一个常量
& I, h8 V2 O$ k# d" J- #表示一个常量: d1 E8 A! x \: C T
- >>> math.e
" A) p3 W% K9 }. o: m% j - 2.718281828459045
复制代码 - G6 ~$ N/ p' G" w$ F
math.pi 数字常量,圆周率2 g0 V0 J9 c6 N& T5 s$ R
- #数字常量,圆周率& ]9 W A% ]; s
- >>> print(math.pi)
3 A3 i* O: _$ b- I* Y4 }; G - 3.141592653589793
复制代码
& l g' N8 S; c; v) z5 F4 A. U# Rmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
, i8 |/ |' z+ U# S0 E& ~( L2 T- j- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
: l/ N- \% P; j1 b& [8 I k3 l c - ceil(x)
; m" \. s( ~! p - Return the ceiling of x as an int.
" K2 r5 t+ s R. y2 h8 Q - This is the smallest integral value >= x.5 l4 }7 l9 c1 S. r* S8 O! ~
: { |3 W: u6 T* @/ R- >>> math.ceil(4.01)- n5 x# j6 p1 k- E* L0 O' u R
- 5
: T3 e ?; g4 t8 E4 ^- ` c - >>> math.ceil(4.99)6 q5 R& U1 `1 }' H: Y
- 5" ~) Z! K! z9 A
- >>> math.ceil(-3.99)
% w7 I' F4 }7 n. v' K6 C% d5 ] - -38 z( d: h1 |5 {# z+ @- u% ]
- >>> math.ceil(-3.01)
6 W, ~, B: I1 R$ G - -3
复制代码
( D" c" e$ u9 s5 Y4 xmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' o j0 n+ N6 Z3 ~- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ I7 ]4 O. Q+ W! G4 T9 P, o; b
- floor(x)
) {/ C2 _ v. k# _$ y% U - Return the floor of x as an int.; @$ D! d3 J. B* ?+ h" ?8 a( l
- This is the largest integral value <= x.
! Z: _/ [! U- X* ~) ^ - >>> math.floor(4.1)
* h, i& l" V6 f3 ?$ s- N9 I - 4
" x$ ]; a- Q( c& o! i9 D - >>> math.floor(4.999)
$ b- ] [( n, _8 a - 4
% V4 M3 _3 m9 s6 {% C' m - >>> math.floor(-4.999)% g; K* O3 S" {% ~
- -52 j1 P, a2 I- v1 Z$ Y- `9 u
- >>> math.floor(-4.01)/ P. C9 \/ [( U
- -5
复制代码 0 y1 p, r1 A% b" u. u
math.pow(x,y) 返回x的y次方,即x**y; J" s2 W9 V6 b6 p- B
- #返回x的y次方,即x**y
2 u8 y6 M E1 m- ^% I2 [ - pow(x, y)7 M- k, k* ^1 P& Q6 r8 w) i
- Return x**y (x to the power of y).- @+ A) \+ H; g: e
- >>> math.pow(3,4)
* q2 F5 [. ^1 R( v$ @8 Y6 X - 81.0 E9 [0 r& P7 L2 }, d$ a
- >>> - J" b) ~, c. V5 E
- >>> math.pow(2,7)
6 @$ a- z# h7 r3 {: P - 128.0
复制代码
" u W: b$ B) U% V/ n. omath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 l/ G2 A% L. H3 {" n- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 h+ T; m2 r9 B0 ~( R) d - log(x[, base])
0 o7 t# L8 ^; s9 ~+ h& x - Return the logarithm of x to the given base.
$ w4 X$ ?) p3 Q, P( q! Y( N/ t - If the base not specified, returns the natural logarithm (base e) of x.& o0 u# H. Y; v1 x# _2 ]% J7 q
- >>> math.log(10)
3 z& i h& A8 Q2 w' I - 2.302585092994046
" `8 i2 A$ `2 V2 f - >>> math.log(11)$ U3 Y* Q/ K1 C. A
- 2.39789527279837078 \: @6 C3 B5 l0 A/ o6 H
- >>> math.log(20)7 x, ^- l0 [9 e, x
- 2.995732273553991
复制代码
( H F6 ?7 t# Z& g4 Vmath.sin(x) 求x(x为弧度)的正弦值
% E4 H1 j$ Z9 b6 b: u3 [9 [- #求x(x为弧度)的正弦值
9 y3 e s* H+ ^ - sin(x)
2 G5 I7 ]6 I1 F - Return the sine of x (measured in radians).! r: \# ]. A0 b. f- ^
- >>> math.sin(math.pi/4)( u4 e7 a! S2 I7 ?; s
- 0.7071067811865475
1 L& ?3 ?8 [( P' f) @ - >>> math.sin(math.pi/2)
6 Z, x1 K$ u, J0 z& T3 W - 1.07 j6 v. t9 ]& B" R1 R! T) H
- >>> math.sin(math.pi/3)
4 t; k( Z! s- ? - 0.8660254037844386
复制代码
% l- a+ N2 b" C" y% Pmath.cos(x) 求x的余弦,x必须是弧度
! q# ]4 {# r; X: X6 f- z/ r- #求x的余弦,x必须是弧度" f1 ~9 j( |& s8 a6 X+ B
- cos(x)' A" L) R c, O1 A2 n
- Return the cosine of x (measured in radians).: s1 _0 {+ D5 H: T0 p' A8 c
- #math.pi/4表示弧度,转换成角度为45度
6 ^* X# k3 D" z( R4 u- {$ L - >>> math.cos(math.pi/4)
9 i# Z+ h, D1 _5 h9 p8 L! J - 0.7071067811865476
" s6 B0 J' ^1 Z/ h* p" g - math.pi/3表示弧度,转换成角度为60度
; d" O; ]9 A8 t8 G6 f - >>> math.cos(math.pi/3)) p4 _: h. z! N
- 0.5000000000000001. Y9 P0 {& i2 C+ G; ?, e
- math.pi/6表示弧度,转换成角度为30度
6 {: i; K6 g' m - >>> math.cos(math.pi/6)
9 w0 ^1 i! [, \, J/ x! e' p3 ~ - 0.8660254037844387
复制代码 z" }3 n, ^6 j
math.tan(x) 返回x(x为弧度)的正切值. B9 h& Q& Q6 Q/ S
- #返回x(x为弧度)的正切值; h* D5 z. A# W; P* R
- tan(x)
6 b2 q+ u: [. v) U* ^. i% z - Return the tangent of x (measured in radians).) y( d: _+ ?( C- z8 N. y' T: f
- >>> math.tan(math.pi/4)
- R- z F: h- \' E" b3 M - 0.99999999999999991 N) m( A+ d, ]6 I* p
- >>> math.tan(math.pi/6)- A; u5 O/ z! R+ v
- 0.5773502691896257
- k6 H0 Q6 x& ]( f# ^. y' v - >>> math.tan(math.pi/3)
( B) W$ s5 ~6 t6 l# p, o% H& F - 1.7320508075688767
复制代码
5 }) J& J ]9 ]2 t1 a7 Umath.degrees(x) 把x从弧度转换成角度$ c3 `: u( t3 W9 o6 z; v- ~
- #把x从弧度转换成角度! r: q: k: @* I; q
- degrees(x)
C; j- I$ U1 s4 h$ u* c - Convert angle x from radians to degrees.
1 l) }+ Y) c: }7 v5 E& U - & l+ z2 `0 [4 a2 ]" B2 ~
- >>> math.degrees(math.pi/4)' W* y& V9 ]* S/ J
- 45.08 @, v4 b0 R6 ?
- >>> math.degrees(math.pi)+ c/ z6 r5 g4 G( i
- 180.0
* Q9 z n5 ^0 h& Y) G+ I# B+ n, M - >>> math.degrees(math.pi/6)
3 u6 Q; v5 k$ X/ C% J9 L1 R1 } - 29.999999999999996
, S4 R- T" ^! j, c8 q7 y& o+ Z0 \* H - >>> math.degrees(math.pi/3)$ \7 R7 ]( n# k. F0 S
- 59.99999999999999
复制代码 & Q8 |5 G5 Q+ u" P4 \+ h3 H
math.radians(x) 把角度x转换成弧度
& {5 w1 t+ H5 Q$ o1 p' |- #把角度x转换成弧度
" Z$ z+ u, L$ e' ] - radians(x)2 h5 O4 ?3 v/ j
- Convert angle x from degrees to radians.$ S+ Q: ^2 p7 \% r: {2 y4 W& n/ U
- >>> math.radians(45)
6 B. c1 J! s6 P6 T% N% L - 0.78539816339744831 [( |, k( i+ x& s& W
- >>> math.radians(60)
2 s$ o k& Z2 K# v( I) b* s - 1.0471975511965976
复制代码 : U J, Z0 ?9 D, u
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
5 H7 E8 L3 J1 F0 e, I" q2 \- #把y的正负号加到x前面,可以使用0% ~, A- V9 W+ h
- copysign(x, y)
! k- ]3 d& x% b" a4 z - Return a float with the magnitude (absolute value) of x but the sign
3 t5 `5 m) ~( [1 D" l4 X8 ]6 E - of y. On platforms that support signed zeros, copysign(1.0, -0.0) / h. s. x9 T! @
- returns -1.0.0 I- p4 E- F. C0 A6 {% s, B
- ^2 q9 W! L$ Q6 J* j' F+ d
- >>> math.copysign(2,3)# w* T* {- C3 T* e* r- P) u
- 2.0 ^5 h5 j. V" P. _3 o+ e2 [: V0 o% j+ }. B
- >>> math.copysign(2,-3)2 T, c. u/ ~# g$ N6 `- `. m% z
- -2.0
' r# V4 T7 N6 I - >>> math.copysign(3,8)
- Q& } h' z' v; ~# a) t, Y$ A% a - 3.0
) @( H% `1 I& T2 j7 g - >>> math.copysign(3,-8)# l; a8 c, e8 p% {
- -3.0
复制代码 - h9 A c# @% G( i, n
math.exp(x) 返回math.e,也就是2.71828的x次方
, _# T* M9 R) G7 d2 H/ ~9 W2 T- #返回math.e,也就是2.71828的x次方% Y& ]7 q# x. g9 M1 q% V- K9 u/ f
- exp(x)# X2 x* O7 z: ?2 J* J
- Return e raised to the power of x." a# x2 F- u, g- z
- 8 {. P6 {' j1 {
- >>> math.exp(1)5 {% n _, ] z' B$ J1 Z5 N
- 2.718281828459045
( _( W$ |: |; u - >>> math.exp(2)
! O& h4 A v/ `5 D9 g# f - 7.38905609893065
$ H: q3 Z6 W+ k$ ] - >>> math.exp(3)! ]$ p$ L9 [/ S* ]
- 20.085536923187668
复制代码
( S, J& S2 D3 i( z, I/ b" }math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1* U8 B1 G) @/ N, e% Q, ^4 ]$ h
- #返回math.e的x(其值为2.71828)次方的值减1
. l# r$ h2 D- l# g2 y - expm1(x)
# V6 b; j7 r1 ~! v - Return exp(x)-1.! D4 G8 Y( ^& l
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
( G$ r0 l+ a$ x7 ?) y; { - * p1 z( F7 A# u0 n/ O- v' e
- >>> math.expm1(1)
) Z3 l/ D) Z6 }5 ~. O$ Z* j - 1.718281828459045
8 j5 p- U6 G" q2 y7 h" h5 S, ` - >>> math.expm1(2)# P/ S& J% T, }- j
- 6.38905609893065
# `- c6 ?1 F; s$ m. }& [( M - >>> math.expm1(3)
8 }: x0 ?& Y( B8 H" d8 u! K, b - 19.085536923187668
复制代码
0 m: O+ p2 h/ ymath.fabs(x) 返回x的绝对值' s! V3 o( k) Z% R* j$ P
- #返回x的绝对值" Q; `. O( L$ Q7 ]7 r" s
- fabs(x): G9 d, G) N- E! A6 w: N
- Return the absolute value of the float x.
$ i2 W8 Q& c v - - G7 j# m4 }% l, L/ M1 H
- >>> math.fabs(-0.003)4 A, r: m) z) @! u+ D# q$ X# z( H* Z5 A* ?
- 0.0036 I) _1 {, F: G* `8 b
- >>> math.fabs(-110)2 H1 K4 O$ E" Q3 r1 Y$ B
- 110.0
! Q* E& y9 V) L9 f- K* D" \2 Q - >>> math.fabs(100)
" A1 a) |8 Z4 i8 g6 W" F - 100.0
复制代码 6 T7 d$ y _* G4 b8 L( b) r
math.factorial(x) 取x的阶乘的值$ A3 X# X" C$ W4 r
- #取x的阶乘的值
% T5 _9 S2 {. q - factorial(x) -> Integral& T- X) e$ h( f3 ^6 _% b
- Find x!. Raise a ValueError if x is negative or non-integral.9 y& n- X' k8 C
- >>> math.factorial(1)
8 k8 ]6 j1 V. e) D) ~5 k - 18 b0 b' ]! T' }: p+ m. A3 ^
- >>> math.factorial(2)
9 S2 Q! R9 E- h1 g& [ - 2& Z$ A% j" W9 u( F! c
- >>> math.factorial(3)
, ]: u5 D9 ~2 r/ U8 R$ ?1 e - 6
" s7 ]$ w1 F4 ^& \$ U - >>> math.factorial(5)! M8 b5 R& H+ m2 x5 R, ^4 R
- 1203 I" H* L* T8 ?. p: A; u# s% C
- >>> math.factorial(10)% x/ W3 Z% T4 ?1 E/ m3 b n
- 3628800
复制代码
0 V# p e' {7 `6 A! P' j" Hmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
: m# e( E8 e2 D) o0 _# d7 }- #得到x/y的余数,其值是一个浮点数: I$ Q8 p' K+ A3 x. g
- fmod(x, y)" X! }" z% f6 y% F
- Return fmod(x, y), according to platform C. x % y may differ.
, z* `: N3 o [+ f: l4 R, |) S5 q - >>> math.fmod(20,3)
. I! n& L: B* p w4 F - 2.0; P. N: f/ H" u8 l& G7 l
- >>> math.fmod(20,7)
9 [3 Q4 G2 M! u/ w! u - 6.0
复制代码 4 q/ z7 {1 V) B8 N/ ?# {
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 m, c/ T3 s5 J4 a. n) B- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, M* O' o9 f, s
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
7 o7 I& S6 ?# H t# f( p [/ K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和15 \; z. _4 R" N$ D9 y* `- I* ?
- frexp(x)
# p E& u4 t0 Y+ l - Return the mantissa and exponent of x, as pair (m, e).; D, H# D) a8 E$ V' o7 n
- m is a float and e is an int, such that x = m * 2.**e.
1 L! b+ ], U1 k2 ~* Z8 @ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.: e) s+ C. I# ]7 ^( a& S. P, v1 o8 {
- >>> math.frexp(10)
( {8 X5 j/ }3 e% ?0 {5 j" R/ c - (0.625, 4); _; n# _- ]7 K/ |& D5 g; A
- >>> math.frexp(75)$ \) s" k: s, D# b/ W/ R
- (0.5859375, 7)3 j3 i# Y% z3 H' F3 t! F
- >>> math.frexp(-40)+ Z2 _; W# |- u$ I N7 i
- (-0.625, 6)
, S1 Y2 s5 y/ ^ - >>> math.frexp(-100)
" d+ E& |. e: P! c; ^ - (-0.78125, 7)
+ i6 i# s- x. s7 p7 E' } - >>> math.frexp(100)
+ ^# t' ^. b* ^$ v* G - (0.78125, 7)
复制代码 , J# p9 M( L4 R5 B+ H) w' x( L
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列), O# E% c8 k1 w/ o$ k+ |6 c
- #对迭代器里的每个元素进行求和操作
! ]) }& l0 @1 m3 D8 t% W - fsum(iterable)9 G+ s" a( f+ D
- Return an accurate floating point sum of values in the iterable.9 D3 I! |' R* x, E
- Assumes IEEE-754 floating point arithmetic.
+ R5 p+ v0 q3 u9 l' {6 H( N - >>> math.fsum([1,2,3,4])
, A5 ~& S3 u; K s% U - 10.0/ O& S' ~7 _( V0 K5 I
- >>> math.fsum((1,2,3,4))! M/ _7 O/ f; x1 Z
- 10.0( X8 {2 {! ~7 {; ~ b0 l; t/ L+ p# U2 _9 F
- >>> math.fsum((-1,-2,-3,-4))
9 e0 t; ?& u; I" i$ F2 A) \" J5 u" z - -10.0
/ _. P7 c- r4 y" }, n; O - >>> math.fsum([-1,-2,-3,-4]). |, P* i. z6 b2 `
- -10.0
复制代码 + U2 ?# G L9 b O- S( l2 Y
math.gcd(x,y) 返回x和y的最大公约数
. c5 q' b- i( ^- #返回x和y的最大公约数2 U& e$ s) T& _/ A; ?5 h9 T
- gcd(x, y) -> int% W ?4 E% G0 }3 X4 H
- greatest common divisor of x and y
3 m8 B: }% L" f# o5 J5 p - >>> math.gcd(8,6)% D+ ?* J) h, `9 H7 W% l
- 2& N8 S( F6 a9 Z9 p* b' R
- >>> math.gcd(40,20)$ A: J: B3 a3 z: t2 d
- 20
1 [% S+ g8 F. u b; C" m3 q: H - >>> math.gcd(8,12)
* I, L9 |6 E$ I% } - 4
复制代码
" E+ K7 K5 W2 {# g7 Tmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False: E. X& N6 }9 E4 G* j& t' ?
- #得到(x**2+y**2),平方的值$ b5 e9 y6 Q. n! }6 O# d
- hypot(x, y)$ {% Y! w# ~3 E* C3 x2 \4 ?
- Return the Euclidean distance, sqrt(x*x + y*y).
$ P/ ?% I# w% v f7 q9 Y - >>> math.hypot(3,4)0 K7 q8 F9 m9 r7 b6 z
- 5.0
) ]$ ?4 v$ O |; e6 E; j7 C - >>> math.hypot(6,8)
$ Z; m1 n. E2 [* L; R - 10.0
复制代码
2 G& O) v [2 y+ F e( T. H: nmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False+ H6 P& d2 W# G# s
- #如果x是不是无穷大的数字,则返回True,否则返回False; C2 G7 w, Z$ Q; p+ G7 N- K
- isfinite(x) -> bool: p6 h E) _& u
- Return True if x is neither an infinity nor a NaN, and False otherwise.
/ v y# z6 G! ?0 H$ ~ - >>> math.isfinite(100)2 T& K' S" D# t8 R, S" p
- True; x6 g; s X7 _
- >>> math.isfinite(0)
/ `, J: U7 L; C6 \; d - True
4 i4 N A, o* P: ?- q0 Q1 Y - >>> math.isfinite(0.1)
5 O' M8 v0 S( X6 }( o - True* u' n2 g# y$ d9 v' ]4 M
- >>> math.isfinite("a")/ d: M& S) Y8 E& u
- >>> math.isfinite(0.0001), D; x; I9 w1 P8 v+ j% h
- True
复制代码
: h" K+ y% F) o. d, ^math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 T' u$ k2 w; X* b: ~; L- #如果x是正无穷大或负无穷大,则返回True,否则返回False
8 O8 v9 P* K3 _% Q' E - isinf(x) -> bool4 S/ l8 _! s4 ^0 Z& J
- Return True if x is a positive or negative infinity, and False otherwise.
# B* }5 _8 F. K% {2 Q - >>> math.isinf(234)
4 s( w/ O6 z% c3 i( h - False# a* Z# U; |) h o1 x
- >>> math.isinf(0.1)
* T8 h, K. A$ z4 J! m% ?$ Y: R - False
复制代码 ; i2 g6 p7 v2 ]$ n3 G5 f( A }8 b
math.isnan(x) 如果x不是数字True,否则返回False, h8 a+ a, T3 b. P2 O+ O
- #如果x不是数字True,否则返回False: _( h6 l: K& k D% r
- isnan(x) -> bool0 L+ ~2 C: P. w9 Y3 ]! e
- Return True if x is a NaN (not a number), and False otherwise.* E4 r+ Y- D- L' U5 l
- >>> math.isnan(23) I- y, M+ ]: @3 \; z% f! C
- False8 A; S5 U" R# K8 R( M$ k
- >>> math.isnan(0.01); O6 d: ?, f* t; {6 Q Z! B
- False
复制代码 7 Y: J3 `2 I, [( o, b0 i
math.ldexp(x,i) 返回x*(2**i)的值
. a; j2 v+ L/ l- {- #返回x*(2**i)的值
- p- v8 o% y, ] - ldexp(x, i)% C: p2 A l( ?
- Return x * (2**i).. Y4 l; S6 M! @& s) Q( X5 {. K
- >>> math.ldexp(5,5)' Q3 T1 z+ V: X+ b# \: @, f6 a
- 160.03 n+ ~7 n, X X( P! @3 q
- >>> math.ldexp(3,5)
2 O& z/ e* D. M) @ - 96.0
复制代码
9 P8 V1 Z5 B! X- ?% T# |- smath.log10(x) 返回x的以10为底的对数 q: m/ V" }" I+ z- ]
- #返回x的以10为底的对数" C: z) ~( }) u0 R2 ]3 Y
- log10(x)- s, m: H- W3 m4 ?5 c" Q9 o
- Return the base 10 logarithm of x.% G( |) u" m, ^8 X6 X2 W
- >>> math.log10(10)7 K% l7 W0 p2 w) e7 h- ]$ D& i
- 1.04 x! y5 v; m5 g% G: n2 u! ^
- >>> math.log10(100)
5 J( ~9 A4 P( |2 r - 2.0
- U$ U C# z' J3 }/ A1 M9 w0 F: Q - #即10的1.3次方的结果为200 k2 M1 Z9 E4 T, u) j) Y
- >>> math.log10(20)
9 E9 m) I, h; F; w8 e [ - 1.3010299956639813
复制代码 # B6 [! [- W. ~( ~6 c
math.log1p(x) 返回x+1的自然对数(基数为e)的值
, L4 E0 H( q2 }8 h" ?" v0 h- #返回x+1的自然对数(基数为e)的值8 k! \5 _" `# r7 ?9 u% K
- log1p(x)+ v' ?1 G5 g$ T
- Return the natural logarithm of 1+x (base e).$ @1 |& A$ B# d) n# g# g* t
- The result is computed in a way which is accurate for x near zero.! N- H% v) R9 f: V
- >>> math.log(10)
8 }: x/ u0 }" ^) W: s6 q - 2.302585092994046
2 O3 r! T6 [1 s6 o6 e - >>> math.log1p(10); n! ^9 b5 }4 @
- 2.3978952727983707
% Y7 n9 v3 k$ L8 A3 `3 o" a; q - >>> math.log(11)
5 T |* V9 P) W( p! Q0 | z( J c% T - 2.3978952727983707
复制代码
. u: ]& e3 s P6 z8 t. L% \( Dmath.log2(x) 返回x的基2对数
& a# Z! t& @- M" n0 Z- #返回x的基2对数" f$ m# H8 A5 R/ A
- log2(x)
0 h; @9 Y' M" f# v& ~, c( v - Return the base 2 logarithm of x.. W) k0 } z% n' o M$ w u( \3 E
- >>> math.log2(32). d: w% X% z! V1 _; @" ]7 n5 N
- 5.0
9 @) a) t7 L1 |* ^% p - >>> math.log2(20)
! F, \+ t, m" m* p, R - 4.321928094887363
: ^. p, K) x5 f5 |; T0 j9 [) N3 x - >>> math.log2(16)
1 j' {6 l* L, q/ N. m - 4.0
复制代码
5 E) w! F, K' Bmath.modf(x) 返回由x的小数部分和整数部分组成的元组4 H7 s7 f, {7 h1 m" a( m. q: |
- #返回由x的小数部分和整数部分组成的元组
- J1 p: v" _# h! ` - modf(x). H* q% ?, P( \ w/ B
- Return the fractional and integer parts of x. Both results carry the sign8 D: s/ L' w* _6 U, K3 I+ r& G! W- n
- of x and are floats.
. t8 e$ }0 n2 d7 u5 \ w3 w0 ] - >>> math.modf(math.pi)2 t. C* C' k! H7 r
- (0.14159265358979312, 3.0)
6 G& q# h) a! {2 u/ A1 F - >>> math.modf(12.34)
u4 B7 `6 i+ @ - (0.33999999999999986, 12.0)
复制代码
9 Z" |# Z* s7 F4 h& `$ jmath.sqrt(x) 求x的平方根
1 B8 Y) r6 I2 j3 ~' V- #求x的平方根
, h2 G4 ]3 W% M - sqrt(x)0 s( [& Y. L- E! z4 T
- Return the square root of x.$ z# t7 |: J5 b. O- K; i7 K; t
- >>> math.sqrt(100)
4 a1 Z) N. p# r2 Y - 10.0
6 c8 h; \& h0 E9 \ - >>> math.sqrt(16)# x' l5 K; m/ B$ X
- 4.0* t( v7 u" Y- G% z
- >>> math.sqrt(20)4 ^, v& f9 M' f3 m
- 4.47213595499958
复制代码
3 {" ^3 n; G& ^) Gmath.trunc(x) 返回x的整数部分- #返回x的整数部分
- r! d) W# l' E; w2 c% r - trunc(x:Real) -> Integral
1 Y [) w: u% L: A - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* P2 S4 i7 E0 P' Z2 R; h; h
- >>> math.trunc(6.789)
J2 Z1 s6 }2 G# e7 u - 6
( Q2 k+ |0 y t - >>> math.trunc(math.pi)
. F$ |1 l; b6 _ T - 3, A7 `5 r; L w
- >>> math.trunc(2.567)$ l! z3 B: w2 U+ |" |% t& w
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|