马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 D) O3 |; h- \- }9 @2 l9 ~; _
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% s% Z0 C$ t! p {, H
- ~ |, R# E; f. o: P2 s方法1:
~& w6 }5 n# [9 K- >>> import math
; s: i" `/ g: c - >>> math.sqrt(9)
2 Y( ~0 ~9 J8 e. Q$ y( n& W - 3.0
复制代码 方法2:/ C5 b# R$ a+ m+ W* v5 d
- >>> from math import sqrt
# A3 \- c7 s) @! U2 }5 R - >>> sqrt(9)- V( t& |9 C& `7 u
- 3.0
复制代码
: h3 r0 @. R: H r( K ! D( \8 P2 s9 m# b6 |; j8 p
math.e 表示一个常量5 m5 u* m. @, E( s! w- Q
- #表示一个常量
+ V! o$ ^2 @; B, E2 A, J) \1 M - >>> math.e
) f8 l3 i2 w; i) t" E) Z - 2.718281828459045
复制代码
4 F& L0 D; p- tmath.pi 数字常量,圆周率7 r$ ~- V, D/ l1 u* W0 |" {
- #数字常量,圆周率& |, z4 x2 f1 E7 `; ~& X
- >>> print(math.pi)2 b" O+ P: ]8 q8 z6 |9 Y
- 3.141592653589793
复制代码
( t) K; R. P Z. C) z6 [( Zmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x5 @0 D9 X* v9 u, N* R% H
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 U4 R( c3 Q* M( i' V5 r# m
- ceil(x)
! R( j& l1 B5 A# r! G; E - Return the ceiling of x as an int.9 a$ K# e1 Y% h( q8 r; A
- This is the smallest integral value >= x.4 `" B' ^. f& d w4 R- n
! I8 ?5 j0 ~' T! _- >>> math.ceil(4.01)
* p) j7 i$ e2 T: q [' x! G' a - 5) c1 x1 |/ |. I" l6 a
- >>> math.ceil(4.99) B. t4 m& K4 `1 e
- 5
/ I" @5 @' L+ C6 \1 y - >>> math.ceil(-3.99)
* W* _3 ?: a/ K+ b - -3
7 ^+ w5 ?" J) t" g2 l$ _8 N( M! N - >>> math.ceil(-3.01)
% L( m4 H7 g* S4 F8 M5 g# C* _ - -3
复制代码
+ Z e/ F; P, z/ Dmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 q. _6 o2 `2 {4 L! c: y0 E
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, m H Q4 ]3 U) n - floor(x). X2 O. Q) h& x7 n/ ~' l! F
- Return the floor of x as an int.
. [; k5 ^1 c2 [/ H - This is the largest integral value <= x.. I. B9 R. Y3 L4 `3 F: y+ S7 B0 e/ r$ \
- >>> math.floor(4.1)
! a* e/ H( O" O0 U* F! D - 42 Q: w& t7 T2 V
- >>> math.floor(4.999)
. R2 l! B& `1 t2 n- }( G$ Y9 y0 Z - 4" Z% k/ k1 l2 C. v4 \2 Q, Q2 n; q
- >>> math.floor(-4.999)- v, ?9 x( Q6 N0 {4 A
- -5- G% h0 |7 R+ ]- N; z
- >>> math.floor(-4.01)
# G y& l5 T S( {4 j, J - -5
复制代码
( y5 |$ G% N: [math.pow(x,y) 返回x的y次方,即x**y, Y) Q5 `& s6 H, }9 M
- #返回x的y次方,即x**y
0 X4 S4 x* D/ X9 q6 U* A - pow(x, y)/ N h+ A, r. f: {/ b
- Return x**y (x to the power of y).+ j2 ~$ M3 M# t4 O) y, B
- >>> math.pow(3,4)) U$ j' {& T; A* g- ~) M" E9 @2 _% s/ D
- 81.0
+ K' v1 f& d( p$ o - >>> : O! ?& T7 b2 G5 M
- >>> math.pow(2,7)
& {' y9 [" b# q! f - 128.0
复制代码 + `" `7 k5 C$ g7 O" \
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 |& T+ J( H$ o/ O' S: I- }- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 {- U! u3 n; o4 X8 z
- log(x[, base])
. z# R# d8 O. F: _- c6 V- _ - Return the logarithm of x to the given base.) b5 b9 n: g( @5 D/ f
- If the base not specified, returns the natural logarithm (base e) of x.
$ k5 l" v) D/ n0 ?8 V - >>> math.log(10)9 E' C4 r0 g4 d1 V, V
- 2.302585092994046
+ y2 {3 _! @# O5 g! o4 O; ]3 j - >>> math.log(11)
! l/ D& t6 C2 Y. F& H2 M - 2.3978952727983707" b& Y4 B- O& W' @( x% n
- >>> math.log(20)
, U2 d8 {2 H5 Y - 2.995732273553991
复制代码
5 i+ f# ~2 |# t% e8 S- Pmath.sin(x) 求x(x为弧度)的正弦值7 {9 D$ e& I% i" h) S
- #求x(x为弧度)的正弦值
2 p0 K6 y! I4 C, b1 b - sin(x)9 f$ E, @ |8 e6 }7 p
- Return the sine of x (measured in radians).
6 a3 c8 r/ r. Q# U+ a* T - >>> math.sin(math.pi/4)% u4 X+ _2 K4 l9 o
- 0.7071067811865475+ u% W# v; B1 |" C& R- r; f4 g
- >>> math.sin(math.pi/2)
2 H$ R6 C3 M3 W5 @) i7 S+ c- d- L - 1.0
" a% V; M7 S- d - >>> math.sin(math.pi/3)
( i. r6 r) `0 n5 f' M$ N" u4 ? - 0.8660254037844386
复制代码 & @1 ]! I& C; H; j
math.cos(x) 求x的余弦,x必须是弧度
* j0 h- _1 _& q" ]1 z: V% l- #求x的余弦,x必须是弧度
6 L& d' c: n" {: m" R - cos(x)
' r) r" j. [' g& c. U4 ?- B1 _ - Return the cosine of x (measured in radians)./ k* J. f/ ~# _# I
- #math.pi/4表示弧度,转换成角度为45度
, h% W8 r4 _% n9 b/ [" |5 s - >>> math.cos(math.pi/4). w3 u8 i- _: c# o, d
- 0.7071067811865476
! s% H& F8 A( v5 U6 H5 [ - math.pi/3表示弧度,转换成角度为60度
: k+ \8 g! l9 J1 T/ l* X2 J8 w1 h j% D - >>> math.cos(math.pi/3)5 h# N" l- v7 e) |: \
- 0.5000000000000001- s0 O8 N$ S: i1 e
- math.pi/6表示弧度,转换成角度为30度/ B" O) O6 O- o3 }. s. L
- >>> math.cos(math.pi/6)) ^& A. n b' Y+ B4 ~
- 0.8660254037844387
复制代码
$ Y) i( c+ D$ A9 j7 Wmath.tan(x) 返回x(x为弧度)的正切值
) i \% i( q6 H% Q7 ?- #返回x(x为弧度)的正切值: U8 V7 W6 L( S" e; C5 W/ ^$ X" G5 O: c
- tan(x)( B, H, N+ L- j3 I1 K
- Return the tangent of x (measured in radians)./ t1 ?7 }7 Q) m
- >>> math.tan(math.pi/4)0 m9 N5 Y: `: v/ ]. b
- 0.9999999999999999
. Y( W* }. ]# {( p - >>> math.tan(math.pi/6)
5 P) o2 ?; n2 ?. h5 l5 O7 i - 0.5773502691896257
; ^! @) o' v8 ]/ s. W; S - >>> math.tan(math.pi/3)
# q; y, l, j( B - 1.7320508075688767
复制代码
1 x% p7 \8 i2 s* K' Wmath.degrees(x) 把x从弧度转换成角度4 G: L" ]8 I# m$ v5 E9 }7 s# V
- #把x从弧度转换成角度
) r4 k0 `( ~8 @8 ]$ M5 d* |9 _ - degrees(x)
' Y$ u" O9 L- v( N/ z3 ^: b& I V - Convert angle x from radians to degrees.5 ]5 j1 a# w! N+ s$ m/ F
5 s( h1 @$ B9 X5 f% i) Q- >>> math.degrees(math.pi/4)
3 T) t" F4 [& ]6 u9 Z2 q8 ~ - 45.0
6 |. [2 W+ I. P! u3 U0 f: Q- O! I4 b6 Q - >>> math.degrees(math.pi)' M z* y# d! `
- 180.0
) G b8 h! h# t4 e. j5 T - >>> math.degrees(math.pi/6)
1 _4 {& F4 u3 v- b - 29.999999999999996$ D E$ m6 j+ i; F8 V' F
- >>> math.degrees(math.pi/3)/ f; S) r- q/ }+ a( v* \$ j2 E- v: l
- 59.99999999999999
复制代码 9 [6 i* v7 @3 s2 {" y
math.radians(x) 把角度x转换成弧度
' C2 P: K3 N9 a/ }9 _% v D- #把角度x转换成弧度" T, S: L2 M: z9 L2 G; k3 \2 c, L% P
- radians(x) j+ a; Y& [* h1 l0 n
- Convert angle x from degrees to radians.
& u# g, U' w0 G, A" f( A - >>> math.radians(45)
8 d8 H: T5 Q$ U t' D - 0.7853981633974483% u' _, x/ V0 a9 s, g/ N
- >>> math.radians(60)
5 ?9 h2 H1 h1 y% i: P2 ~ - 1.0471975511965976
复制代码 ; f' ~+ y+ A8 V; C4 Y; F
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
2 w# m, ?$ Y$ ^5 N: ^, p. O/ G- #把y的正负号加到x前面,可以使用0
, P; |3 Q4 N: h - copysign(x, y)
9 _" q$ G: `3 f: i( a - Return a float with the magnitude (absolute value) of x but the sign
3 o: X, N( H1 K% C" c - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
( m- o; B2 z4 e- n( \0 U& C; W - returns -1.0.4 s, q- w& t+ N* s/ O& [1 B) t; e
- 3 i% y: U: b( Y
- >>> math.copysign(2,3)0 n: Z+ F8 W9 H( N' u `! ?2 p2 j8 i
- 2.0
3 k+ k" H9 Q8 p, s - >>> math.copysign(2,-3)
' e) x% X: e" x8 M* R - -2.0
: C, M, \) F0 u( T$ r0 T1 B - >>> math.copysign(3,8)
% G: n" T/ L2 f0 a0 Z - 3.0# H. S( Q4 P# S, c8 s& _
- >>> math.copysign(3,-8)
; e& Y0 w2 B/ W; `4 s2 s2 F - -3.0
复制代码
7 r1 p- h7 a) Nmath.exp(x) 返回math.e,也就是2.71828的x次方
2 ~# u! ~* B% B7 I. ]: }- #返回math.e,也就是2.71828的x次方
6 m8 o. H* J: D - exp(x)
3 p9 [1 a6 j1 S; Z - Return e raised to the power of x.
0 z$ I& Q( J/ H - `7 C$ R! g4 X' W- a) z1 Y
- >>> math.exp(1)
( L0 Y7 q2 C7 q. Q+ s5 @ - 2.718281828459045) t o9 k9 O D4 @( O3 X- \
- >>> math.exp(2)
2 t. |9 x4 v& j& f - 7.38905609893065, e& Y# l" o* Z1 w0 F3 |
- >>> math.exp(3)7 B) x: F6 R: w5 e, D
- 20.085536923187668
复制代码 . q+ j# Z, |$ d/ |
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
I7 T: Z$ {3 l7 ~$ D* D; ~2 y! B- #返回math.e的x(其值为2.71828)次方的值减1+ @4 Q1 @" ~! A
- expm1(x)
1 K* Z- Z0 y! {3 H9 S) V - Return exp(x)-1., F8 `' k- n5 ]% q i) }0 O
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
. _0 a. V& S, v8 U9 O+ w! z+ H7 \ - 2 b- ~# w' d, |/ I( T3 ]
- >>> math.expm1(1)6 c2 ~& N+ I8 f P! }
- 1.718281828459045
7 X( i, d, P4 G" F/ K( |' o - >>> math.expm1(2)
- E# u! ]' b, ~) |: A7 M' Q - 6.38905609893065) E! `3 r# h" j0 p/ M% ?
- >>> math.expm1(3)
1 y0 j7 F7 R( n* X# \ - 19.085536923187668
复制代码 3 x3 q2 a" {5 a8 A$ u
math.fabs(x) 返回x的绝对值9 t* r0 P) D+ @9 Z" |3 |
- #返回x的绝对值* g& C, k! \+ A1 W9 E7 ~
- fabs(x)9 D9 l* [7 _5 {/ @
- Return the absolute value of the float x.& ^ K% `: u8 D1 ]
5 m( r7 T+ Q, @( @& A ?9 ?- >>> math.fabs(-0.003)" N5 x9 q9 a; ~$ y6 n. r# [
- 0.003
& f4 Q/ B2 o8 Z# ^& N4 t1 r0 m - >>> math.fabs(-110)
1 O& s: k. H) l - 110.01 T8 F! q) U4 e1 T8 t
- >>> math.fabs(100)
0 z9 j/ K' ^- k# F9 x V7 F - 100.0
复制代码 5 e/ ]7 R% n4 }9 \; i+ v5 x G* l
math.factorial(x) 取x的阶乘的值
- }# B: S! e) L' C7 d- #取x的阶乘的值' g Z! N1 L, Q5 {& z% ^( E8 m0 e
- factorial(x) -> Integral% r% \* E2 c% i. G+ T1 {
- Find x!. Raise a ValueError if x is negative or non-integral.2 d5 h3 e+ L. M" P
- >>> math.factorial(1)2 e5 I7 {" |8 d' b* L3 J
- 1
6 c* V3 ?/ @& n% U3 a - >>> math.factorial(2)
3 V: m* d) [! w% C - 2. u/ V, F8 G& [8 T. L; I& W2 l
- >>> math.factorial(3)* S: k( Z# e# Q3 G! F
- 6. x+ F ?/ U6 C5 H
- >>> math.factorial(5)8 e8 x) E- u9 I5 X) L
- 120
7 i; i5 P. B, b6 R4 G0 J - >>> math.factorial(10)9 E8 t0 }# w2 Z0 \" B1 ?
- 3628800
复制代码
% M; \3 u- ~6 h' M, Emath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
- F3 {' Y0 c+ m2 V5 r7 W8 s- #得到x/y的余数,其值是一个浮点数3 S7 C' ?4 k! _$ \2 E% T
- fmod(x, y)
0 d; Z% O- z8 g - Return fmod(x, y), according to platform C. x % y may differ.1 w0 S: ]. n5 j- Q. C( {
- >>> math.fmod(20,3)
2 ~8 m1 B' ^6 W - 2.0
5 Q: [+ [, s `- p; p - >>> math.fmod(20,7)7 n+ O8 {, k( u6 `, T5 k. q+ k
- 6.0
复制代码 5 u2 |9 u& Z% ?8 u7 S
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! R" `# E. ]0 y+ Y2 U+ |6 [
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
: |. J1 I. R" P' Q0 h - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
9 T0 i! D5 U2 g$ M - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
) }& t! V- ]# i. q! |! Q0 X: x - frexp(x)
3 c7 V1 Q3 X8 O' ~- u6 l - Return the mantissa and exponent of x, as pair (m, e).
$ S: ]" N/ x: A; m o4 p$ [$ n - m is a float and e is an int, such that x = m * 2.**e.
6 j2 `* w+ Z' ^5 m" g3 Z$ x - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.8 i1 n0 Q+ e% ?. S( `
- >>> math.frexp(10)
3 s" M3 w! {4 F2 C3 h - (0.625, 4)
: I0 w. [$ V+ Z! H! x5 }2 ]0 y& ^ - >>> math.frexp(75)
; Z& Y! {9 b; I W - (0.5859375, 7)+ {0 d |9 H" A& _6 [ v
- >>> math.frexp(-40)$ ]% C/ N) l* w; ~7 [5 M: t
- (-0.625, 6)
' h Z* p ^9 X2 @' u' e - >>> math.frexp(-100)$ W6 K H3 a- e; ~ s$ g `
- (-0.78125, 7)
6 X3 i8 N5 m" O( E9 r - >>> math.frexp(100). W- _7 G( s% _7 a( {* k6 r' S
- (0.78125, 7)
复制代码 . M, h3 d9 j2 V2 }! @! B8 }
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)+ P( p- P, s, E) m& n) F! Q/ l
- #对迭代器里的每个元素进行求和操作
% X& A/ F# L7 }: h9 ?/ A4 Z - fsum(iterable)& T+ n8 B: |) n9 P& l
- Return an accurate floating point sum of values in the iterable.: x6 H$ s/ u7 G( g
- Assumes IEEE-754 floating point arithmetic.
3 Q4 s4 ~4 }0 U' ~& Y4 [ c - >>> math.fsum([1,2,3,4])
/ f: d4 C. ~5 B$ i, I - 10.0
5 P/ W7 J9 q0 q9 y" m - >>> math.fsum((1,2,3,4))
( V8 X% w' b7 B' L - 10.0* Y% O6 _! o$ e" m, [+ h6 {
- >>> math.fsum((-1,-2,-3,-4))
6 T8 x. A3 Z4 ]8 {7 W8 r$ O4 H - -10.0% c# F" M* ~( R5 R$ n
- >>> math.fsum([-1,-2,-3,-4])
# E" Q8 ^5 i" y+ { - -10.0
复制代码
6 K7 r9 R/ l8 b- a) m% Z6 [. q0 Omath.gcd(x,y) 返回x和y的最大公约数
! t4 q3 W0 B0 J% U; R: s- #返回x和y的最大公约数7 @5 A9 j' U" v0 R0 T2 H, Z
- gcd(x, y) -> int
: s- h6 x. i# c9 S7 c$ s8 ^ - greatest common divisor of x and y
4 n! G; i- Z4 J - >>> math.gcd(8,6)
" r0 U3 U* g- P9 O - 2
' J: \5 @+ P! Y0 E; ~+ Q- v - >>> math.gcd(40,20)+ K1 G5 _% f3 P& c. J, Y. [8 Q
- 203 a' w) r3 B+ e) L& T. g9 d/ G
- >>> math.gcd(8,12)
' w, n5 C u; r+ O' U# q. ` - 4
复制代码
( n& s5 f; X! b- B1 A, j( Vmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False, a. o- y e9 l: O7 X4 ~
- #得到(x**2+y**2),平方的值
7 U! Y& `7 S ]' K1 K' N - hypot(x, y)
3 S' w) d6 U2 w- T. n% ~- M% P - Return the Euclidean distance, sqrt(x*x + y*y).) k/ T3 V, f7 o; m u. w$ C
- >>> math.hypot(3,4), z0 e! |3 ]- s9 @/ a
- 5.0' ]) [* D( _4 U% u# m
- >>> math.hypot(6,8)* {% R, f% C$ S) C" N8 P
- 10.0
复制代码
. o. L2 N$ n$ a! q a3 Mmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
# y* [* ?( q+ {4 [- #如果x是不是无穷大的数字,则返回True,否则返回False
: | ^2 c3 c( g3 E - isfinite(x) -> bool3 b$ n9 [( v. O2 p2 x% g8 F7 ~% F
- Return True if x is neither an infinity nor a NaN, and False otherwise.
* J" t+ K) A1 j - >>> math.isfinite(100)
3 `0 t% g, t, K# q" f - True
' d' D. `& `2 d1 L - >>> math.isfinite(0)
+ s7 b. I$ U% E6 K - True
! N% H# ] u" P( l! Z; U" b0 y - >>> math.isfinite(0.1)6 ?* e! y C: E/ ]3 q( S9 x5 L) Y
- True/ |/ V. ] D4 \# J$ l" F
- >>> math.isfinite("a")
* [, Z/ `1 E {2 U5 p - >>> math.isfinite(0.0001)" E+ U! F1 C4 D% k" O
- True
复制代码 # v% ^0 j- }) W, p
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False5 V/ [3 ^3 I1 k& U6 ^
- #如果x是正无穷大或负无穷大,则返回True,否则返回False0 [9 O1 T# K* V6 N y
- isinf(x) -> bool2 f4 A/ M6 I* _8 r
- Return True if x is a positive or negative infinity, and False otherwise.
( B' E! ?0 I$ M# r" O+ R/ |! a' p - >>> math.isinf(234)* B+ A8 K, S2 p2 G7 Y9 b# A0 Q2 |
- False
* T2 z6 }, b& R0 _+ o. u - >>> math.isinf(0.1); b- ?( w6 A# \; O5 ~ ~1 f' D
- False
复制代码
9 o* \" u' z, C2 m% Hmath.isnan(x) 如果x不是数字True,否则返回False2 l$ R0 a" S* r; ]5 V' Z. o
- #如果x不是数字True,否则返回False8 D9 l( B' |* _. F9 i8 I! k- T% |
- isnan(x) -> bool. A- \" A3 ^4 o) X1 w
- Return True if x is a NaN (not a number), and False otherwise.
. f* s! s8 g7 m: w2 k- G4 T6 N! ?9 H - >>> math.isnan(23)
+ l8 K$ U( o* B; O( I( g - False5 `, q4 `. u# w- R$ F$ W( ]) d
- >>> math.isnan(0.01)7 v' D1 Z P4 K
- False
复制代码 ) Y# M/ V( l3 Y* d3 O, f
math.ldexp(x,i) 返回x*(2**i)的值$ H7 U4 O9 n. A% y5 w
- #返回x*(2**i)的值
! u0 [ X- J8 x7 F1 }. ]8 Y: N - ldexp(x, i)' Q; b" j t; u L. v
- Return x * (2**i)./ u' T, o; e- F
- >>> math.ldexp(5,5)
) l8 t. D" N# J# i2 w& s7 K - 160.0" ^; U, C* g7 z* A4 b% y3 A2 O
- >>> math.ldexp(3,5): t4 T; u( {6 K# {9 T% i3 n
- 96.0
复制代码
" u5 q; z& s+ W2 Q9 Umath.log10(x) 返回x的以10为底的对数
x4 K1 z' W Y. r8 S) V; s- #返回x的以10为底的对数
9 c* Y+ q+ `! e% _" s4 O - log10(x)* z! E1 @! j) ]3 U" ^; `5 z5 {3 Y0 V
- Return the base 10 logarithm of x.% ^4 Q7 E2 _ Q, ], |9 n) Q
- >>> math.log10(10)
8 T! t; O& G3 Z+ D2 L# u - 1.06 k4 q0 Z! n! I9 ^* \+ b1 Z$ G
- >>> math.log10(100)
v% v/ | q; U9 q; s/ J - 2.0
! g+ k$ Q! b+ _# D7 C, [- Q1 Y+ ] - #即10的1.3次方的结果为20
, _3 f2 O+ |$ [( V: k - >>> math.log10(20)
& d. n% V/ n9 f0 L4 o - 1.3010299956639813
复制代码
' ~3 A4 o% ?; x' y0 M& pmath.log1p(x) 返回x+1的自然对数(基数为e)的值, c% W! k0 o* |9 t
- #返回x+1的自然对数(基数为e)的值$ d: {( `" @, i2 l6 N* \
- log1p(x)7 s& u- U! D! J! I! Z7 L/ n% ]
- Return the natural logarithm of 1+x (base e).# i4 z6 e2 k+ W. Q+ w: ] e1 O
- The result is computed in a way which is accurate for x near zero.
2 Q5 P: V2 f3 w o - >>> math.log(10)0 U8 V& H1 y# e( u
- 2.302585092994046- j S3 g$ @& n% a% c( r
- >>> math.log1p(10)% S: W" s, v+ M6 y0 F p
- 2.3978952727983707$ v3 R% g6 b6 ^1 ]" k" B
- >>> math.log(11)$ e, y( }! Q) }
- 2.3978952727983707
复制代码
) W( B# _) x9 omath.log2(x) 返回x的基2对数
$ @/ _' W# ^2 u( n- #返回x的基2对数
4 [$ f8 P& r. P7 \$ x( k - log2(x)) t' V# {2 F) t4 m4 M
- Return the base 2 logarithm of x.; j- u; I# V4 ]$ V4 d v5 {7 W! `! f/ u
- >>> math.log2(32)
% N0 ~# q& {" h, F8 R9 X- i - 5.0$ F/ w; _, x" U( W0 l$ Q d" o
- >>> math.log2(20)
w5 Z( }% N0 q9 m" w% N6 S - 4.321928094887363# i; R$ ]; Z/ s f. ^5 @9 ]4 K* ~
- >>> math.log2(16)2 c' D2 ~: C+ t- S7 \' ] Q) y
- 4.0
复制代码 ' [. \* D M! n- s: Q; t, k
math.modf(x) 返回由x的小数部分和整数部分组成的元组
& x/ K( g: [$ w( k- #返回由x的小数部分和整数部分组成的元组- p7 a. P: O1 v" G0 { Q
- modf(x)
0 X3 B( a0 {; F2 n' j: W - Return the fractional and integer parts of x. Both results carry the sign! m$ Z& A. w, q0 Q9 B5 j2 E
- of x and are floats.( T3 B2 ?+ R+ R" x) l0 v
- >>> math.modf(math.pi)/ b; D4 W- K2 B/ N R
- (0.14159265358979312, 3.0)! e- K6 F: U: f d" g2 H
- >>> math.modf(12.34)
5 J. ^( H! e; G# m - (0.33999999999999986, 12.0)
复制代码 + D9 t4 ~3 ?' S9 I4 O
math.sqrt(x) 求x的平方根
1 z! a" Y- |6 y" w6 ?5 ?" x- Y; v- #求x的平方根& f8 X7 p a) `9 w( a6 o; Y! P" C
- sqrt(x)" s9 _" u; z4 w9 Z0 D
- Return the square root of x.
' ^( R. q8 ?) b - >>> math.sqrt(100)
! j" A& n5 E6 I: n4 t3 y( { - 10.0
, r# [1 l3 |) ~4 ] r- Q - >>> math.sqrt(16); O/ j1 S3 s5 E* |
- 4.0" l3 u1 ?! ^# |' b
- >>> math.sqrt(20)
R8 _, z& Z* C! v - 4.47213595499958
复制代码
4 }0 c4 ^# O0 G6 D$ xmath.trunc(x) 返回x的整数部分- #返回x的整数部分
4 @; B6 _1 Y$ _: | - trunc(x:Real) -> Integral
- x6 i. D" ^: @. L6 |! W+ j - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# J3 O/ r1 o( N' ^ ^# t
- >>> math.trunc(6.789)
+ j0 Q4 v0 V. O" a - 6
* @) D, f' }0 \& l p2 A" e - >>> math.trunc(math.pi)
( x1 B1 J" T! ~5 U" y$ C - 3- y$ K9 P" D$ O. \, h
- >>> math.trunc(2.567)" Z* {& W# b1 c' e U7 z
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|