马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 z: x- S/ e' f* @4 C' L8 _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& |' l3 q8 ~0 M8 r
% r) i. [ \) p6 k6 j方法1:
; n. I% B! n" m3 S- >>> import math
! x( R* z8 D8 ]; ^% w6 U. u - >>> math.sqrt(9)
3 q; a2 N0 I3 p5 k - 3.0
复制代码 方法2:: Q$ I9 ^* _6 W7 D1 K1 W5 s. l5 h
- >>> from math import sqrt1 D) ~9 d" v, ~& l$ b
- >>> sqrt(9)* A. v+ Q7 H5 P! y+ d$ X
- 3.0
复制代码 0 `# ?7 S( ]' l7 A+ u
; ]9 h# u: z. E" B w, J O8 y. }5 a5 |
math.e 表示一个常量( z( r; c2 ~- w! j
- #表示一个常量) i$ {) @ W% P
- >>> math.e
! m5 q* ~7 z, d$ ^/ a: V - 2.718281828459045
复制代码
, m7 n: y* o/ p0 jmath.pi 数字常量,圆周率
- A) T2 ?" q/ ]0 ]1 d8 U# B; Z- #数字常量,圆周率
4 C( e2 p0 ]7 K1 I- I2 K- Z) e; W - >>> print(math.pi)
+ z! T5 j: d) B5 S: q6 \, _ Q - 3.141592653589793
复制代码 7 K8 `9 k+ H! U; `% O
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
) S- F9 Q; O: u$ J R- #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ U$ g/ K' n5 p) w+ ~* x# `
- ceil(x)
4 [$ S( f5 ^5 p6 q" k/ a - Return the ceiling of x as an int.8 |5 z2 v1 H: O, q( f6 Z5 o
- This is the smallest integral value >= x.+ R8 V/ ]% U3 t7 y& {% S h
* N# Q, K' _. D- >>> math.ceil(4.01)
E& |) _6 R$ V9 F - 55 Q6 S; M6 F2 b$ X" g
- >>> math.ceil(4.99)
. r+ Z$ l6 q2 Q2 h - 5
2 M/ g( N. H! ?, Z+ L - >>> math.ceil(-3.99)
& _9 E$ p0 z5 Y" f7 b2 i6 C/ _ - -3
3 M w+ z8 u2 q5 P - >>> math.ceil(-3.01)7 r( I- `# A% t( [
- -3
复制代码 9 p! o3 k: z: ^# U+ e
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身, ], R9 y& `1 ]- @* J
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身: {) n# \7 l9 k5 X
- floor(x)
3 P, q$ B7 Z# P% t; D - Return the floor of x as an int.
- N8 Y/ w5 M* L, [+ N9 o1 b - This is the largest integral value <= x.
4 I1 K" g, ^. k- A - >>> math.floor(4.1)
) q L$ X7 U" M3 P: J1 D - 4+ F) o1 \: u+ C+ z3 |
- >>> math.floor(4.999)
( e" K L m' Z/ ?6 V - 4! L5 _7 }: X: H# N9 m# S+ ~
- >>> math.floor(-4.999)( ^6 x+ l. v% W5 ~# c! w2 d% j
- -5! _4 x$ [9 a& d" s2 m% G
- >>> math.floor(-4.01)
% W$ ~/ v' ]0 t0 w1 X2 ` - -5
复制代码 & S( p! G# M" V% x! O
math.pow(x,y) 返回x的y次方,即x**y6 Y0 f5 Q4 |; D; `$ i
- #返回x的y次方,即x**y, T: a4 w, I/ J/ Y! s3 [
- pow(x, y)& u! i& R' N5 c2 v# C* |+ y
- Return x**y (x to the power of y).5 k; U$ o. `/ W
- >>> math.pow(3,4)
( q/ [7 M J2 e. ]4 c: X. J- v - 81.0$ J$ N7 b( \( c0 q( g5 Y: z0 x
- >>> 3 n3 Q& O5 W7 X, L
- >>> math.pow(2,7)& F& D- P5 `0 q( @. C2 o
- 128.0
复制代码 : z4 r+ m! P ? z$ H q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 s: L" a3 s/ b$ P, v: [: N4 l
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 I% C/ p3 D5 { - log(x[, base])! T, Z1 F& A9 J( ?, k7 l3 L7 V
- Return the logarithm of x to the given base.
* ^. H! [2 m% f - If the base not specified, returns the natural logarithm (base e) of x.
8 Z6 K3 M6 _% b+ \' `" o. V4 Z - >>> math.log(10)9 {( y1 u9 n, O! P) O H4 r
- 2.302585092994046
* k2 J' R1 W9 K - >>> math.log(11)# C: V9 T/ k0 A# _
- 2.3978952727983707
/ ]7 i2 q ^# a6 B) M4 u7 C& D - >>> math.log(20)- ~& o$ e0 o) \) N, Z0 Q
- 2.995732273553991
复制代码 1 t" ]+ [( H4 F# O
math.sin(x) 求x(x为弧度)的正弦值
( j- O# [( N4 h6 S- #求x(x为弧度)的正弦值% S1 \. y/ N) U/ E
- sin(x)- s2 Z% ^8 j& r
- Return the sine of x (measured in radians).
( o$ {9 i) n4 c5 [) [ - >>> math.sin(math.pi/4)
& w6 w U1 c; p/ p - 0.70710678118654753 ^. _! V) S, @7 q& U
- >>> math.sin(math.pi/2)5 @, L% L7 b) J; L. j5 _2 z6 K
- 1.0
A( e: F0 U5 I1 W) {! s - >>> math.sin(math.pi/3)
' g. I% K! x/ C: O: N7 d - 0.8660254037844386
复制代码
4 u. F0 m0 i& G4 x. ?0 imath.cos(x) 求x的余弦,x必须是弧度4 Y0 g- m" t# J) f {. `/ _
- #求x的余弦,x必须是弧度
. P; L" [& s0 s4 k- ^ - cos(x)" O- l7 }+ n' C0 f0 X4 K
- Return the cosine of x (measured in radians).; H1 T2 v5 R ^! z) d
- #math.pi/4表示弧度,转换成角度为45度
; m+ a/ R& l8 g. \: f7 }8 l - >>> math.cos(math.pi/4)
3 o0 K7 [2 l) q& F6 L) a& U- k - 0.7071067811865476
! W7 ` ~8 M4 K - math.pi/3表示弧度,转换成角度为60度
# ~1 ` _# ?2 E. b0 O - >>> math.cos(math.pi/3), P8 c% L( h8 ~$ P+ B+ E" k
- 0.5000000000000001. \2 d; p% B/ q, l: U L! t4 {* {
- math.pi/6表示弧度,转换成角度为30度
( o( @( j P2 w9 Y7 Y D/ F - >>> math.cos(math.pi/6)1 @5 p2 ]: t9 I. _: ]4 U
- 0.8660254037844387
复制代码
6 Y6 z9 J) e, \8 f* V0 Pmath.tan(x) 返回x(x为弧度)的正切值
- U9 J! o) U7 W% W5 H- #返回x(x为弧度)的正切值+ ]6 I) n$ E$ v
- tan(x)4 X$ V2 n, }- Q2 o# ?8 U& U
- Return the tangent of x (measured in radians)./ Y9 U6 z l1 W2 A$ G" t3 g+ x
- >>> math.tan(math.pi/4)8 C& x5 j2 h: ?5 Z) K. a& E
- 0.9999999999999999; U |0 v- y2 Y, f4 \- E
- >>> math.tan(math.pi/6)8 ]1 ^. N$ R0 ~; ^0 h
- 0.5773502691896257* h% k! h; R, F; n& R
- >>> math.tan(math.pi/3)$ N) ~/ C; K% ?, K9 U" \& f
- 1.7320508075688767
复制代码
8 ]% M" i1 @# {# K) ?, fmath.degrees(x) 把x从弧度转换成角度& Y2 Y! g4 T! O( J1 k
- #把x从弧度转换成角度/ \3 B" N" X7 t% Y5 [9 F3 v
- degrees(x)
2 ~! X v4 e7 N! a, W/ I - Convert angle x from radians to degrees.
+ s( {) x1 k# {9 n
& A5 C5 M2 a) `( t& `- >>> math.degrees(math.pi/4)
. v% w/ q$ p* \7 f6 @& R+ _ - 45.0* w2 Z) D' p4 M8 u
- >>> math.degrees(math.pi)0 S( u9 M- ~1 h r) C1 ]
- 180.0
) T7 \ I+ l; i( p/ v - >>> math.degrees(math.pi/6)
h( H! \5 U) {% e) `1 R( g4 r$ x( _ - 29.999999999999996. a$ ^ z9 t# j3 c# |4 G( C
- >>> math.degrees(math.pi/3)/ D* p* X! o$ `" l, L! P% D9 H- u
- 59.99999999999999
复制代码
9 f/ S3 u7 [6 n4 e' ~* fmath.radians(x) 把角度x转换成弧度* k3 @" |% `# x
- #把角度x转换成弧度
" u+ \/ s# [8 i5 K9 k& z - radians(x)" j$ O2 P# y6 o: @9 p# f# y
- Convert angle x from degrees to radians.+ K/ i0 `) O2 x2 k( C$ t1 X
- >>> math.radians(45)7 M9 s% z; U; \
- 0.7853981633974483+ W! b$ m# v9 l
- >>> math.radians(60)1 {) u" F' [! Z8 \7 x7 c
- 1.0471975511965976
复制代码
7 \$ |( S* G' I, L+ I4 Ymath.copysign(x,y) 把y的正负号加到x前面,可以使用0: E& X1 I" @- G$ S% F8 D; \! n9 J2 h
- #把y的正负号加到x前面,可以使用08 c9 Y, o2 A3 |+ f3 M2 J, I: M
- copysign(x, y)# `7 x* L) v% x) z |1 f( o# J
- Return a float with the magnitude (absolute value) of x but the sign
. S- T/ }, b; V* p6 j e/ ` - of y. On platforms that support signed zeros, copysign(1.0, -0.0) + y7 s- D3 [/ F' y& X. O
- returns -1.0.
& a# w' A0 n1 c; r - $ v1 |- J0 A1 ?1 Z7 v5 W
- >>> math.copysign(2,3)/ N1 Y3 j, q0 u% `$ x
- 2.0, `) p% P7 O6 x9 A, ~
- >>> math.copysign(2,-3)
6 H3 ]1 V F8 v0 X - -2.0
2 P) P: i2 s2 G- d8 i( J# q; I - >>> math.copysign(3,8)
. _1 f1 v' E9 L* I) I3 F1 W" }2 ? - 3.0+ k& e( H! F# @8 ?) y
- >>> math.copysign(3,-8)' C' w7 D, ~. j0 w7 a
- -3.0
复制代码 0 h+ q* ?2 x# g( H. E1 V
math.exp(x) 返回math.e,也就是2.71828的x次方- h, h& b0 f# S9 s9 D
- #返回math.e,也就是2.71828的x次方
, m% N' }9 { l& t/ [/ a. x - exp(x) j, c+ n* ~7 Z( A
- Return e raised to the power of x.
, J4 n% I& y4 ^" k
3 G( L& m2 v. x: ^$ ]' N- >>> math.exp(1)
0 ?6 \9 h4 g* f, O) s - 2.718281828459045
) w( Y3 D2 z6 p# R5 ] - >>> math.exp(2)5 M' ~: { q1 j$ r' Z: U4 _
- 7.38905609893065
; i* S5 d+ l$ G. R - >>> math.exp(3)8 n" u) d6 `/ [; v4 h% m# _
- 20.085536923187668
复制代码
3 U. \! B" X/ imath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
0 r# H+ Y. v; J- #返回math.e的x(其值为2.71828)次方的值减16 x) G1 k% ~. D, ]" c+ ^) O
- expm1(x)4 d8 c" ?7 T4 X& j7 v
- Return exp(x)-1.5 k. H0 ^9 w5 f+ H0 _6 D% Y" q9 U
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
9 z7 A2 c3 j, h; F* w - 1 I ?# S2 i" W+ j% m
- >>> math.expm1(1)
: {; H T% @8 X: o" `3 Z; _+ u" h - 1.718281828459045
1 N( {1 h8 B u: s1 U0 H3 T5 Q - >>> math.expm1(2)0 \1 ]$ y) }- ]! j
- 6.389056098930654 r- J* Z0 c$ N4 [4 Q
- >>> math.expm1(3)
+ r) `2 q8 O! I+ j: u# c5 _( X - 19.085536923187668
复制代码
/ M% i* Z1 G9 Q6 |8 Ymath.fabs(x) 返回x的绝对值
H. w# E* C9 B- #返回x的绝对值
5 x% }: x" P! a* G, j - fabs(x)
2 E' k, t% S4 Y7 K6 x - Return the absolute value of the float x.5 i0 Z. C" v, J- Q' ~1 v: f
- & g2 y* K8 |7 }& @) f0 g; \' r2 C9 i
- >>> math.fabs(-0.003). L- g, Z1 n/ O! r
- 0.0039 v% u# @/ A) C; O, M
- >>> math.fabs(-110)
& Q' _; R% `# A) c% O - 110.0
2 a) g7 d( h0 m, F - >>> math.fabs(100)
: q; e6 e9 t% `8 G - 100.0
复制代码 ! i9 z: A/ T5 @' t
math.factorial(x) 取x的阶乘的值
; }5 c+ _+ P( G3 T+ g& C- #取x的阶乘的值8 N |/ N9 |1 V @! ^* ?
- factorial(x) -> Integral$ B& b x! W7 ] z7 m% f
- Find x!. Raise a ValueError if x is negative or non-integral.
3 m: A m4 f( R+ t - >>> math.factorial(1)" ^/ S9 {1 L0 D2 ]& I( X7 Q. G
- 1
! u+ E! h' o$ |8 v - >>> math.factorial(2)
1 P* u! ~0 q2 O' i e4 v - 2
9 ^% z9 `+ m& X2 o7 j7 c0 W - >>> math.factorial(3)
6 |. Y# i5 f; W! D1 ] - 6
$ i5 a$ h+ d6 F+ ~ V% b5 r6 m - >>> math.factorial(5)1 e& d6 J1 r$ w& C, J
- 1200 Z& t/ _% G6 V5 L0 g
- >>> math.factorial(10)
9 d- K4 c; w% a) Y5 f* L8 w$ a - 3628800
复制代码
. x* }$ v9 ~7 B, l8 k# E3 Emath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
/ O0 v- ~/ J ^' \9 |- v1 E- #得到x/y的余数,其值是一个浮点数
; n0 P) H( z6 H# J0 g - fmod(x, y)
' x6 `0 d U T8 p0 I0 g: a - Return fmod(x, y), according to platform C. x % y may differ.
* d% {( S3 q) C" W7 j% } - >>> math.fmod(20,3)
( d% s2 B0 i! G7 r5 y - 2.05 r3 G) l0 F( c$ o$ q3 f
- >>> math.fmod(20,7)
& y1 c- ^4 X; P H* D - 6.0
复制代码
p3 r/ N/ A% d9 nmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 F% r1 l1 u o9 K5 [) j
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. [! `3 [# _7 I4 Q9 u4 s - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ @; J1 `7 M& }6 C, f
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' ]& p: h. `' X% `1 E2 k! A
- frexp(x)6 S0 ]) o% C3 ]0 H3 v
- Return the mantissa and exponent of x, as pair (m, e).
8 l9 \2 V! E- N - m is a float and e is an int, such that x = m * 2.**e.1 C8 P- v" N: |- k/ j7 ~
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.2 Y( v! l. y; [
- >>> math.frexp(10)
+ {! E# L. r) W; V7 r: n; e# _ k - (0.625, 4)* }/ R: J) I2 X. F$ s5 l
- >>> math.frexp(75)2 h3 Z. R; \9 }4 N" O. A' O8 I
- (0.5859375, 7)
- y) t' b1 X( D s- @8 I& t - >>> math.frexp(-40)
' \$ ^0 X" V4 x, K, S - (-0.625, 6)5 B' W+ k# A0 f8 t1 o* l
- >>> math.frexp(-100)
- d/ F: C- w7 ~: W0 c - (-0.78125, 7)
8 n- U! c; A O- @/ s g - >>> math.frexp(100)6 ~0 O. m4 b; }2 s
- (0.78125, 7)
复制代码 , o. v- \& ]5 g# ~
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
. i3 Y7 u% {# q. `- #对迭代器里的每个元素进行求和操作, C4 K/ P) u6 }* D5 [2 l+ r: ]; D6 }
- fsum(iterable)! |6 Z7 T5 c, S
- Return an accurate floating point sum of values in the iterable.) Y f3 Y; [- s
- Assumes IEEE-754 floating point arithmetic.; h; ~% g2 A4 q: e& \ H
- >>> math.fsum([1,2,3,4])
& K; S( d; {* W, V" b) b; d - 10.0( S# a6 F1 e$ C% h5 h2 I! N
- >>> math.fsum((1,2,3,4))
+ i' p# C+ e* Z$ N' w. |# W7 ?! @+ N - 10.0$ v& B2 |8 u! s% W- q
- >>> math.fsum((-1,-2,-3,-4))" `5 s: w' v7 O: `
- -10.0& X; l7 D- y* N4 f
- >>> math.fsum([-1,-2,-3,-4])
7 U1 _5 R2 ^5 a - -10.0
复制代码
( c- A+ t- [" k" X, ~4 a, omath.gcd(x,y) 返回x和y的最大公约数0 E/ ~5 q2 X0 @$ z
- #返回x和y的最大公约数; ?* P% @$ d I( c: p+ _* e
- gcd(x, y) -> int' L" n: J& P8 J; o6 K% F" }
- greatest common divisor of x and y7 }* F3 M$ H, c; B% D
- >>> math.gcd(8,6)8 b& u1 L$ ~# ], d1 [6 |. r
- 2
2 _( Z9 f8 q8 I; G1 f8 ~ - >>> math.gcd(40,20)3 N3 W; b( p. U
- 20# c! y# c& |- d9 v7 G% ^
- >>> math.gcd(8,12)& c$ \$ W& `! a* h) p
- 4
复制代码
4 {: T5 I; z& o8 A Q" amath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 b/ P \5 N& u* Y2 Q
- #得到(x**2+y**2),平方的值+ z4 Q, S* p7 J- F
- hypot(x, y)% d; `9 [* ]% w2 |4 @! H
- Return the Euclidean distance, sqrt(x*x + y*y).
5 p) S8 B1 ]/ Y8 m - >>> math.hypot(3,4)
3 J; }. H4 D/ m9 v - 5.0
* n2 X1 Z6 E) d8 s$ P2 E - >>> math.hypot(6,8)
. \& W5 Z% T! N, i7 }7 f, y/ a - 10.0
复制代码 , x$ ?( h: J2 N: v$ ~1 J
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False5 U7 r( e2 S( F5 ]/ _3 x. _
- #如果x是不是无穷大的数字,则返回True,否则返回False
! ? ~6 c! U# W - isfinite(x) -> bool
7 a4 v. \+ y1 w3 n - Return True if x is neither an infinity nor a NaN, and False otherwise.
" K9 @9 u' @" D4 t5 K3 h - >>> math.isfinite(100)' D4 d0 x# @! A( A' i
- True- {7 u% n" m* U' v [
- >>> math.isfinite(0)! p7 j/ ]% [; E+ K% c: Z8 B
- True
; x, p5 Q/ {% U- C/ F2 I6 \" J2 i* m - >>> math.isfinite(0.1)
+ K2 ^1 x/ j- ^- K* t% w - True
: ^$ ?% J/ ^4 O4 q - >>> math.isfinite("a")7 S( ^, \4 J- J; I( M: ^8 V
- >>> math.isfinite(0.0001)5 S5 l8 a' q. m3 j; {
- True
复制代码
* |7 i- j8 E. K3 Zmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 }3 ^: W$ `; x# k; g8 W
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
4 R' L* N8 |2 b9 k" p9 i - isinf(x) -> bool4 k( B" A l5 P1 O7 O
- Return True if x is a positive or negative infinity, and False otherwise.
6 p B# i; C$ c! K - >>> math.isinf(234)
; Y" H& a, d8 F9 {! v3 o1 } - False
7 T3 l# V' C. P. T6 y! ` - >>> math.isinf(0.1)# |+ T; H2 f8 W
- False
复制代码 6 a$ O0 r3 e8 a5 b
math.isnan(x) 如果x不是数字True,否则返回False) I, s. H5 v2 \- e& n+ O
- #如果x不是数字True,否则返回False3 i7 X. L3 C9 q6 p7 L' F; f
- isnan(x) -> bool
+ W' w( F8 }1 R - Return True if x is a NaN (not a number), and False otherwise.
1 Q0 T/ {" r/ @% u- P - >>> math.isnan(23)
0 s4 X& ]! v/ l - False( L( y% V% r( H1 g) U
- >>> math.isnan(0.01)+ c. I" |9 p. S
- False
复制代码
+ p( A' ?+ _) S) z$ m; K7 Ymath.ldexp(x,i) 返回x*(2**i)的值4 u' _% h: {- O% {! u+ c
- #返回x*(2**i)的值# y$ v3 x3 Z% j! a
- ldexp(x, i)
* E- l# ?6 E h* I# R: L& F - Return x * (2**i).+ e& w$ a3 e9 P$ b* Y3 `
- >>> math.ldexp(5,5)
1 }: m4 i, U( h; x1 c9 J, p - 160.0; e6 F% L: j% U2 D
- >>> math.ldexp(3,5)& H- O3 _' s8 s# _
- 96.0
复制代码
' u9 k" O2 f! lmath.log10(x) 返回x的以10为底的对数5 {' Z4 u) Y- n' n* O! J; z
- #返回x的以10为底的对数
4 c$ l& W. f% g- ^6 S - log10(x)7 ~$ [5 f$ \+ i; C. n* m" f
- Return the base 10 logarithm of x.' c8 N K7 }5 L
- >>> math.log10(10)% W d1 x: v) a
- 1.0/ T/ O1 Q' M" k. p
- >>> math.log10(100)
; S( c d" u+ [9 ~; H) a! M - 2.0
$ [) W# v4 E9 `2 G% i - #即10的1.3次方的结果为20
- M6 e# M+ V9 H# D5 Q- Z# H- T3 Y - >>> math.log10(20)* y* K& P: |; y# k* H) n
- 1.3010299956639813
复制代码 6 a: ^ P) h; }8 `3 W* _7 s2 q5 u
math.log1p(x) 返回x+1的自然对数(基数为e)的值% p. Y3 S6 x/ Y6 E- z
- #返回x+1的自然对数(基数为e)的值
" ]/ {6 _" a( s9 L6 M* ~ - log1p(x)
) H# P0 O' q0 g9 t2 ?* g - Return the natural logarithm of 1+x (base e).4 T* T& Q2 W. {9 R7 l- P) F+ B/ I
- The result is computed in a way which is accurate for x near zero.
( Z( y. o& L5 F/ u, p% i: t+ X - >>> math.log(10)
" Q; u8 b: K7 F( y4 `, b - 2.302585092994046' q, H4 ^6 h& R8 D
- >>> math.log1p(10)% [. ~8 n% r- t* y
- 2.3978952727983707: b/ _" B4 e' n; T e
- >>> math.log(11)' r- q% w: t/ j* M
- 2.3978952727983707
复制代码
) N+ ^& _8 h7 J9 T7 O; ^- t0 R. `math.log2(x) 返回x的基2对数7 D- [2 m* K5 c' G7 w- j& d- C2 o
- #返回x的基2对数
7 ]) U8 S9 x) a; U& k - log2(x)8 |9 S' A! B" u" T3 R1 C
- Return the base 2 logarithm of x.+ u( z. C% W% V4 G
- >>> math.log2(32)' s8 p' F& q0 Q9 U
- 5.0
# t2 M4 e4 {% Y3 P - >>> math.log2(20)9 V+ t0 }# p9 h( ?7 J/ z) e3 m
- 4.321928094887363
- N3 z2 s G& j) S: ]' I3 L# m - >>> math.log2(16)/ c& k, J8 U G; V3 b: P
- 4.0
复制代码 * I3 K" @! `. h
math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ I* O, J; K* a! c; H' p! {- #返回由x的小数部分和整数部分组成的元组
' _. k* p* }: [2 j/ ?: `5 ]+ x - modf(x)
1 i5 b9 s) ?0 t: T! { - Return the fractional and integer parts of x. Both results carry the sign
* b! e# j5 t# n - of x and are floats.
9 K: O7 l @: {6 j( b' T - >>> math.modf(math.pi)
" {' u$ D+ Y. i. [. l# | - (0.14159265358979312, 3.0)
$ K# g% g3 P/ E. p9 A - >>> math.modf(12.34)
7 B3 s }6 ?. k6 ?" K# W- U9 q# L - (0.33999999999999986, 12.0)
复制代码
G1 G' H( o- D. \math.sqrt(x) 求x的平方根
# _% _ j' c3 K; g' R8 X. U L- #求x的平方根
: T0 B4 B3 T* o+ V - sqrt(x)0 ?- Y: D t9 N1 k, \0 E4 l, C
- Return the square root of x.
% C* u E1 u2 s# Q' i - >>> math.sqrt(100)
& K6 [5 Y' ]3 Y& N8 {, _* o1 d - 10.0
% \: I! ^. u2 r. }+ g - >>> math.sqrt(16)
, @" x$ a: _$ s - 4.0
% w2 Q) C* t3 {; Y2 ~+ N - >>> math.sqrt(20) q' `( g$ g% t: W+ k
- 4.47213595499958
复制代码
, r( \! \3 ^: s/ q/ O6 h: Lmath.trunc(x) 返回x的整数部分- #返回x的整数部分! Q I) i; y- i7 S
- trunc(x:Real) -> Integral
7 Y B C2 f' ~( a7 a6 n - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
/ S7 R: R Q9 @, X# X8 w - >>> math.trunc(6.789)
1 S- }5 F9 h+ F, u. z - 6
0 W$ U9 R" d& _& s# h0 ] - >>> math.trunc(math.pi)" ]& `0 k+ R% D* z* O- F( a
- 37 g; p' u0 A7 I3 Z! P/ P% K- w- ? q
- >>> math.trunc(2.567)8 H. u0 Z) N8 a" b0 X
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|