马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 U1 m; F' f7 }$ S7 x' a3 G
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- y* Y! s! _/ {/ S6 w2 l' h
; S) @' e8 i/ T# G' Q# u- ]4 _; i; k
方法1:$ q% n$ S- b2 P7 R* x
- >>> import math, `( p* i7 y# {% j6 J7 Z7 u/ t8 I
- >>> math.sqrt(9)
2 A, z1 P3 w8 e w( o) i& c - 3.0
复制代码 方法2:
& B" I) X2 X7 f) X- >>> from math import sqrt
% G$ L! Z" b0 E# L - >>> sqrt(9)
' ~ o) }0 D" B( a - 3.0
复制代码 ( o2 ]: H' m! h) d E" b( w
* Z/ \% q2 W$ j
math.e 表示一个常量0 }+ f3 T" b& T! X
- #表示一个常量+ E6 P( n- v) J" @. p. X' g
- >>> math.e
2 d$ m( m' k9 w+ A' n! ? - 2.718281828459045
复制代码
9 U1 G2 P" E( X: _0 M. Umath.pi 数字常量,圆周率1 R0 G% I. h. U6 `4 L
- #数字常量,圆周率
% v# `% a2 Q5 @7 s7 k7 D) ]. i - >>> print(math.pi)
7 ~# Q9 k" z! J1 o6 K - 3.141592653589793
复制代码
6 V6 T5 H+ L& qmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 O8 t# O7 H1 x. F9 T+ V2 Q- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, ]7 r# m' p, e, Q' k7 }! {/ B
- ceil(x)
& P/ w% C1 I' A - Return the ceiling of x as an int. v9 y- }( e1 q4 w
- This is the smallest integral value >= x.
/ j, p6 E) ]7 d0 \$ B
, ]* E) Q" F' C, m: @6 Q- >>> math.ceil(4.01)
8 z' h) W; Y$ q - 5
( S3 e) K P4 q0 t1 E0 K2 o2 j - >>> math.ceil(4.99)
) Y% r' K0 {1 z, C - 5/ B- x+ z. L! l# J
- >>> math.ceil(-3.99)
0 ? C% q) g% ^# W - -3
3 z8 N) Y$ t& j' F, q* t1 S - >>> math.ceil(-3.01)- c) n; o% o9 R" X3 S) K
- -3
复制代码 ( g6 X7 L; E# _& ?5 X$ ~
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ M( z! \! [. ?+ ^
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 f3 P$ c9 i/ Q# P. ~: N
- floor(x)' c, X- C: O( u, f. G- ?/ z) K
- Return the floor of x as an int. |/ A% Q' ~" P; e9 R- F' V* G
- This is the largest integral value <= x.* y0 [+ B, n [4 H# i2 X C5 s
- >>> math.floor(4.1)
" S5 y& @1 I9 w4 n5 W- j - 4
8 N" }( ]9 V( _+ V$ Z* X, Q - >>> math.floor(4.999) P" G" `' a5 _% M- c# w% `( b( l
- 49 X- o( a b5 e3 |( j. Z
- >>> math.floor(-4.999)
8 p4 Y6 k- g) s% ]. ~ - -5
; V, ~/ r3 r8 ?" | - >>> math.floor(-4.01)$ ^; o! J, x: @+ c$ u! Z: u
- -5
复制代码
% ~& h- Q2 v; R5 K. Umath.pow(x,y) 返回x的y次方,即x**y) `- S( [- f) j: r
- #返回x的y次方,即x**y& }' F! k; A6 ?
- pow(x, y)1 t: K8 ]5 Y8 ~1 {- }
- Return x**y (x to the power of y).
r Z3 l' v: [7 u5 G' Y! k - >>> math.pow(3,4)& a/ A+ u% |7 b' x T, x" k" Z
- 81.0
: j7 f, t+ ]$ h: n! o - >>> ( }6 `% j. S; ] z# K p2 d- e7 K
- >>> math.pow(2,7)
3 p q4 M! f* @" |7 f - 128.0
复制代码
- O5 W" f0 D5 p4 I4 Rmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* T/ l) n9 t [
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
; s1 y3 d, i& ]! Q2 K5 i5 x- x8 _ - log(x[, base])/ ~3 G. A% ]4 r8 l
- Return the logarithm of x to the given base.
; T( b+ q6 a B* C - If the base not specified, returns the natural logarithm (base e) of x.+ u2 z; h1 h1 z4 `) {
- >>> math.log(10)- `) @) g' [$ y8 Y
- 2.302585092994046
5 r( \: Z5 L( I1 M - >>> math.log(11)! Q9 \7 `3 W) S, r! L, m
- 2.3978952727983707
: x, q p# Z( J- s8 S! a - >>> math.log(20)
! @1 e% c& U! o* H' a. j$ n - 2.995732273553991
复制代码 ) d/ o g" P9 P$ v. i( N
math.sin(x) 求x(x为弧度)的正弦值. H; ^' N, Z: i) \9 [' o
- #求x(x为弧度)的正弦值
7 d6 l0 Q6 Z( C) i L - sin(x). ?4 n: W! z7 Z, r, x2 Y
- Return the sine of x (measured in radians).
6 e G* A0 u4 L/ z) g% I" S - >>> math.sin(math.pi/4)% x3 f+ }) Y& S$ Y4 ]
- 0.7071067811865475
" Q5 V/ x* _9 ~ - >>> math.sin(math.pi/2)( @3 [) J& P: e" t
- 1.02 C0 L) z1 e# m; R2 L0 ~: x
- >>> math.sin(math.pi/3)* d8 V6 Q& h! P) _, H
- 0.8660254037844386
复制代码
: T* G7 R# K( ^, Hmath.cos(x) 求x的余弦,x必须是弧度
/ U o+ I/ A7 c [, r2 J- #求x的余弦,x必须是弧度
2 e- y1 h5 V( P; g - cos(x)
" U3 ?' F+ z9 b% n - Return the cosine of x (measured in radians).
) \6 P$ w% A1 e8 {2 ~0 { - #math.pi/4表示弧度,转换成角度为45度
/ {' i$ G5 ~" t& w5 Z- _' u - >>> math.cos(math.pi/4)
Z6 V) \& L# H - 0.7071067811865476
$ k; \$ t" o7 R3 ~7 O - math.pi/3表示弧度,转换成角度为60度
/ w& }( e2 a' D+ V* s. q' } - >>> math.cos(math.pi/3)0 }0 b5 U7 c* t& o/ s5 E: y
- 0.5000000000000001. _/ b8 U, H( X" d$ |
- math.pi/6表示弧度,转换成角度为30度) a. G, D0 r% c1 F# X( a; r5 I
- >>> math.cos(math.pi/6)
, u M: V$ B1 F0 Q C9 E - 0.8660254037844387
复制代码
) n" Q' \' |& I7 y. q. q9 _4 x0 t$ Nmath.tan(x) 返回x(x为弧度)的正切值, e1 r2 q2 f- m( K# y& P' }
- #返回x(x为弧度)的正切值
' D& o& g2 R' G. y. g( ]6 S# C8 y - tan(x)$ N5 C+ d, G1 _0 {/ O( Z8 v3 P7 k& w% Z, o
- Return the tangent of x (measured in radians).
2 G! P( V* y0 J# U9 o3 r5 z - >>> math.tan(math.pi/4)
% n4 X+ \* n. U& J' } - 0.9999999999999999
% x& q) O* m" z I1 ? - >>> math.tan(math.pi/6)& n2 V G- b+ Q" p( k
- 0.5773502691896257
' }* \: l v+ \ - >>> math.tan(math.pi/3)
% q" d1 n5 m+ u: a - 1.7320508075688767
复制代码
( h7 h: b+ H ^0 Gmath.degrees(x) 把x从弧度转换成角度 h O4 ]: C8 u- m9 `
- #把x从弧度转换成角度
6 M2 y8 G3 P/ C - degrees(x)+ Y J% ` \/ X7 I8 q7 I
- Convert angle x from radians to degrees.
5 z1 w/ G1 M/ Y) Y
/ b1 l @) [: {; G& O- >>> math.degrees(math.pi/4)
% j$ G7 m7 ^# N - 45.0, \; J, y2 Z/ e" p; ]: p o
- >>> math.degrees(math.pi)
) l) Q: a) v9 ?; h4 w, H - 180.0
( n+ [/ I2 p9 N) C8 p: Z - >>> math.degrees(math.pi/6)$ A; I) C$ _- M/ v' A$ I
- 29.999999999999996) _2 H* F, w+ T2 O
- >>> math.degrees(math.pi/3)
5 N# u: e% f: P! T+ ~$ `. G4 @0 X! E - 59.99999999999999
复制代码 7 M) t' }+ e m3 ]
math.radians(x) 把角度x转换成弧度! z4 O. V: L1 _/ ?3 Z- ?( Y1 i
- #把角度x转换成弧度' a9 C8 C' Q: f1 W2 H1 Y
- radians(x)9 N- q. N0 [& B# o- b1 r
- Convert angle x from degrees to radians.( E2 E1 @7 F2 t8 v6 J. ]4 U1 u
- >>> math.radians(45)
+ V/ M) W5 o! v+ I8 [+ F/ R# } - 0.7853981633974483- ?8 Q. G2 Z9 W4 V: s: [5 k2 |
- >>> math.radians(60)9 X, _! _$ H. J( c% r
- 1.0471975511965976
复制代码
9 G7 Z7 ?0 s( Y+ R9 b% gmath.copysign(x,y) 把y的正负号加到x前面,可以使用00 w2 l9 ?$ ^' @! k
- #把y的正负号加到x前面,可以使用0
2 ^) e/ U4 U+ X' n - copysign(x, y)0 T- Y5 | f$ o) N8 S" d( P* U
- Return a float with the magnitude (absolute value) of x but the sign , u4 T. \: @. U X$ V
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 W, [+ t1 Y8 v& I7 Y
- returns -1.0.' G; b) C% k8 x
- " g E4 _; b0 y7 m4 T
- >>> math.copysign(2,3)
: X# {! U+ s2 k1 x - 2.0
% t: z& @( C) b" G3 A$ e# o5 Z$ j - >>> math.copysign(2,-3)
" u7 e/ o# c# u, g y/ u - -2.0
% z3 r5 G7 [6 g+ v( z - >>> math.copysign(3,8)/ c7 T0 i% f/ F* f; Y
- 3.01 E# P% G9 Y D' {* k0 w" ]
- >>> math.copysign(3,-8)
! p. a+ T1 T% n. K- y* d - -3.0
复制代码
5 {. J4 i0 m: V3 S1 c) H% Pmath.exp(x) 返回math.e,也就是2.71828的x次方" u( \( j7 h1 o2 k+ K+ {5 H, t0 ~
- #返回math.e,也就是2.71828的x次方
# i; x2 B$ o4 k) m2 @ - exp(x)
i; S$ R0 _" c* G0 h7 D - Return e raised to the power of x.' E; _7 q0 U+ k1 a+ K- S; Y
2 y% i9 y0 s6 m% b( M- >>> math.exp(1); t. ^8 f6 u" v; y) a
- 2.7182818284590452 \/ G# }' G! H7 c" o7 s; q
- >>> math.exp(2)9 C( a' ?0 v, k5 c
- 7.389056098930657 c* S+ n. Y6 P' [5 G- N k
- >>> math.exp(3)* ^2 I8 V% z) V, n3 ^5 r
- 20.085536923187668
复制代码 . X" P, O2 [( U5 \. a4 j
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
% L+ I/ I, n# c8 U. e, y- #返回math.e的x(其值为2.71828)次方的值减1
" r# E) _! S1 @) X h - expm1(x)) Y3 L, ]/ A5 v2 w) H3 G% ^ b
- Return exp(x)-1.* o" f! {$ ^3 Y+ y3 T- Q* E
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ S+ q7 ?$ D2 o0 _4 O
- S8 r0 C/ t, f3 Y9 f' S: u- >>> math.expm1(1)* F3 Q X+ t' p7 i5 g
- 1.718281828459045) e- _3 e |5 M# r2 O- ~ X: |. A; F
- >>> math.expm1(2)4 {6 K6 m/ X: X% L
- 6.38905609893065
( b4 S% Q6 U& B2 t - >>> math.expm1(3)3 U |' q+ | h0 A6 S8 c( A
- 19.085536923187668
复制代码 t* V7 n& ]% Q8 j+ w; S
math.fabs(x) 返回x的绝对值/ q1 T9 b6 e# `2 k
- #返回x的绝对值
! D: R. ]+ Y$ c9 d9 t - fabs(x)" k. L1 u5 m W% n+ T! w8 _& ]3 S
- Return the absolute value of the float x.7 o* h7 T' O- ^3 N" o4 u0 q
- % P6 D+ s6 c2 W) w4 _: D" L
- >>> math.fabs(-0.003)3 t) \ A* O1 [* f0 ~: T
- 0.003
$ U+ V' `# [* o' ~$ n4 K; d - >>> math.fabs(-110)2 S) Y) b& F8 B( B
- 110.0 M) o% K( x8 g: P5 C$ x
- >>> math.fabs(100)
! K; ^4 W- O! `% } - 100.0
复制代码 ! c, {# [: M! j" Z! t% A/ T0 e, r/ `
math.factorial(x) 取x的阶乘的值/ f' P, R9 Z% X/ ?6 f) L
- #取x的阶乘的值
* @- G8 ~$ Y$ _) H$ f" ?' f - factorial(x) -> Integral* L& c3 ^- q4 e& C/ N' `# ^
- Find x!. Raise a ValueError if x is negative or non-integral.+ @; Q* r2 W$ Q: w- |
- >>> math.factorial(1)% [- ]0 e; r) ^( o, I k0 H- K
- 1. K/ n! B& @' @6 R* V
- >>> math.factorial(2)
8 I; ]; S ~- U( j8 d( r - 2
! F2 p J/ v; i- k( i - >>> math.factorial(3)( M& k9 a5 Z% [ \
- 68 p1 }$ X( L6 @4 z# w
- >>> math.factorial(5)
3 x8 T$ _. ~# p# k( B5 x3 z - 120
. a/ ^/ Y5 l1 @8 [8 j. c - >>> math.factorial(10)7 Y! n+ G3 Y/ @! P/ L% k
- 3628800
复制代码 / o# r/ C5 l" f* D. _! B
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
, K3 u* s! E: o& ~$ c4 o$ @# G- #得到x/y的余数,其值是一个浮点数5 K) z5 L" f$ s# ?( h( O
- fmod(x, y)
7 n- Z4 q9 n6 g+ K, T - Return fmod(x, y), according to platform C. x % y may differ.
; Y" t G8 @: } - >>> math.fmod(20,3)
; g# w7 E8 f. E - 2.0
8 {* {0 p# v+ W) j( \; o - >>> math.fmod(20,7)
$ y& ~& D( ~3 b N9 c - 6.0
复制代码 & j3 z/ a& _6 O% s3 Z ]% @4 V& @
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 `: ? |% p1 Y3 V5 p# y
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. f) U; d5 ~7 @# p# G* H - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' y# i' M' u* S& H
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 M( [0 T' o+ V3 n! }8 j
- frexp(x)
% W/ f4 o' y- p4 H, t - Return the mantissa and exponent of x, as pair (m, e).
( y. J1 p& A1 _. P9 {2 T - m is a float and e is an int, such that x = m * 2.**e.
% U2 O8 ]% ^4 O - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
4 S [0 @4 Q% B h$ }( u - >>> math.frexp(10)
* _) g( S# a5 I - (0.625, 4)3 c6 S4 j* }6 Y- S! |( q+ p6 X/ Z
- >>> math.frexp(75)# v) b6 s; F, n. P$ o
- (0.5859375, 7)
( P8 I' B' c! J) u8 t - >>> math.frexp(-40)
/ S/ b* R# o5 U3 z - (-0.625, 6)
- d2 k0 Z0 o6 R7 T {4 K& `6 S - >>> math.frexp(-100)
& U# R8 ?$ J: I3 L: D0 p - (-0.78125, 7)
. Z; L# J3 R3 H2 S - >>> math.frexp(100)6 |4 \' ?% E, [4 Y
- (0.78125, 7)
复制代码
& q( q5 c5 e- x- F: Z5 p$ nmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
6 Q) b; h, B. j* `# C$ O! U# [. w* ]- #对迭代器里的每个元素进行求和操作
/ k! A) a7 b0 [+ ? - fsum(iterable)
! D9 ~7 U( J- }. ]4 ?' Q* y7 j - Return an accurate floating point sum of values in the iterable.* {& Q" E/ b% p* ]$ }
- Assumes IEEE-754 floating point arithmetic.5 H5 F7 Z$ B, G* f r
- >>> math.fsum([1,2,3,4]). g: L+ Q/ p/ J: ]- K
- 10.0% T8 `7 X- f5 g' B. k
- >>> math.fsum((1,2,3,4))9 B+ p: C& q/ b" h! b
- 10.07 U+ R# L$ Z% z4 M7 J: e+ h
- >>> math.fsum((-1,-2,-3,-4))
7 ?' q# @- s( a' U( W8 a. ~* G0 g - -10.0
$ [+ {" {4 N7 t. d - >>> math.fsum([-1,-2,-3,-4])# D: Q% N5 T) m$ ], k! Y
- -10.0
复制代码
4 P' W) b$ z5 \ W gmath.gcd(x,y) 返回x和y的最大公约数6 }" ^9 n7 e! k5 t
- #返回x和y的最大公约数" @8 j- V$ K6 b1 ~/ t: Z& @% m
- gcd(x, y) -> int0 ^9 [& T5 i$ m% @: k& t, E/ @
- greatest common divisor of x and y& |6 k. l$ ], g/ s5 [/ Z$ P, Y( v
- >>> math.gcd(8,6)% ]' q: g# c0 ^; C/ f
- 25 ~ |2 d- x" P( i: v0 p
- >>> math.gcd(40,20), u# ~* e# m. C6 @1 B4 e
- 20
) z* b5 e% K1 l8 u, k X5 ~2 T - >>> math.gcd(8,12)6 i+ p& s& a1 n4 @, f4 @
- 4
复制代码 ( W& R1 S/ z9 I5 W
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 a+ D ~6 \ |* z( x
- #得到(x**2+y**2),平方的值
/ y B9 w' ]/ U' @/ T, Z - hypot(x, y)1 m/ q$ a0 Q! z. `# o# |
- Return the Euclidean distance, sqrt(x*x + y*y).1 A: _) g# \7 c( k
- >>> math.hypot(3,4)- v0 ~8 g P1 W2 E# ~3 g( p
- 5.0
! u2 S0 s* w. p9 _- _- X3 M - >>> math.hypot(6,8)/ b' s) i. I8 b# E& X3 I, p6 _
- 10.0
复制代码
7 C8 R7 K: u9 v8 M/ a }% L1 umath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False) _9 u9 f& J' k( N' H2 ] ]
- #如果x是不是无穷大的数字,则返回True,否则返回False+ d8 b& @, M; M. C2 @1 D- B
- isfinite(x) -> bool
9 M; {; n" ?6 ^ - Return True if x is neither an infinity nor a NaN, and False otherwise.
* {5 r& T/ u0 s6 e1 X0 f - >>> math.isfinite(100)" f5 A- `' d) D+ k
- True
6 M3 ?2 a! N+ H+ W1 x n# R - >>> math.isfinite(0)
; R5 v E4 N. B! F3 |5 A - True( G5 n5 r* L& _/ U: F; _
- >>> math.isfinite(0.1)+ v' r: p+ j3 y0 E0 k8 ]+ G% s
- True0 Q$ a# K' r0 w X) s# v g/ e
- >>> math.isfinite("a")1 i7 V+ S* F, V) \; @4 s9 B
- >>> math.isfinite(0.0001)
8 X$ J5 b- {5 B0 e/ A/ V7 z. n1 M6 F) } - True
复制代码 ) z: d" Q) O- U1 ]. P6 E& m1 ?0 J
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False L, Y% F K3 W, U# e: y4 p
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
) s. U1 |" B9 }' v. B& _ - isinf(x) -> bool' H) l4 @2 _( f" D0 z
- Return True if x is a positive or negative infinity, and False otherwise.8 }; \- b9 R0 m8 B1 m( [* x
- >>> math.isinf(234)
$ F9 p O; y5 Q% k" T - False3 R# ?4 b0 J, |4 W7 s Z
- >>> math.isinf(0.1)0 ^6 P' o: F, h3 m `8 B2 b% x
- False
复制代码
. x7 A8 U9 K& Ymath.isnan(x) 如果x不是数字True,否则返回False2 A$ z" L) j# z
- #如果x不是数字True,否则返回False
5 a- I3 w% l, M - isnan(x) -> bool6 z- H$ U7 [! P8 `" ?
- Return True if x is a NaN (not a number), and False otherwise.& ?1 Q1 m1 m: z* m5 w3 V* n1 \
- >>> math.isnan(23)1 t5 H6 e. k" A: h7 V3 E. ^! i
- False
+ p( C' r8 f# {& U6 e k - >>> math.isnan(0.01)
3 d) r9 ^; E% {3 `/ x - False
复制代码
9 c3 U+ Y2 c( A5 emath.ldexp(x,i) 返回x*(2**i)的值
/ n! E) |8 r9 h l4 Q' b- #返回x*(2**i)的值0 O7 o% j% ~2 D: {
- ldexp(x, i)' I- @! Q3 {3 w* W
- Return x * (2**i).2 e6 D9 ]; O$ }- G% ^$ x
- >>> math.ldexp(5,5)
- [0 k7 X: ^- p - 160.03 ]6 I3 W; G' J5 c
- >>> math.ldexp(3,5)& N# l# r, @/ `/ m2 i& W
- 96.0
复制代码
, O5 q( C) N; Pmath.log10(x) 返回x的以10为底的对数, Q$ X3 Z/ G1 a' c0 J
- #返回x的以10为底的对数' M0 p$ {4 O. e7 e3 }5 f
- log10(x) l" d* v: d' |4 Q
- Return the base 10 logarithm of x.
" C# Z/ J; |5 V; w5 i - >>> math.log10(10)- b4 X: Q9 i; X5 @* i5 g1 Q3 j
- 1.0
3 M% t' N0 H! E/ O8 N - >>> math.log10(100)
- g$ I# |; C4 s" ~" F% F: q: E# o - 2.03 V; b! {5 n7 u% ~* }
- #即10的1.3次方的结果为20
/ ^+ ?4 x$ e* ^0 ^ - >>> math.log10(20)
0 T: z+ s9 D7 b, w. \ - 1.3010299956639813
复制代码 2 _, N6 x1 p' f) y9 H: m7 L
math.log1p(x) 返回x+1的自然对数(基数为e)的值
$ a1 ~/ s+ }! q7 @9 p2 u- #返回x+1的自然对数(基数为e)的值' g1 Y7 b9 ]8 k, X/ l/ z
- log1p(x)
' {( T3 O# O% B; p" R! K5 M9 t - Return the natural logarithm of 1+x (base e).' F) n: \9 g9 a+ t3 p# r; S* @
- The result is computed in a way which is accurate for x near zero.
' G9 o, q9 Y% ~& N5 f: r+ R - >>> math.log(10)
3 m9 {. I: w1 N w9 G6 W - 2.302585092994046- \& c4 q# b3 v
- >>> math.log1p(10); ?( {0 l# c/ }5 k( {* g; t
- 2.3978952727983707$ j& r x( Q( X7 |9 h! v; @
- >>> math.log(11)
% k: t3 Z* p) F1 U2 z+ W, i' h* H - 2.3978952727983707
复制代码 . i8 k& |: }$ j
math.log2(x) 返回x的基2对数
+ B3 G1 @0 G( F( {9 u3 B- #返回x的基2对数
/ ?" ]4 v* s, V+ R0 g- w( j, i - log2(x)8 r1 C( [2 _& q0 M% f
- Return the base 2 logarithm of x.4 L: v7 y. _# T! y& |
- >>> math.log2(32)
. b9 b! R5 ? F4 C( G - 5.0/ z& I& u. B, T; \9 v# f
- >>> math.log2(20)
5 g9 l+ M" ^9 A1 w& s( J - 4.321928094887363
% g! W9 M9 F* v$ c - >>> math.log2(16)9 N3 _8 i" \) X, V: U
- 4.0
复制代码 5 k3 T- r3 D% e6 d; Q4 {; [
math.modf(x) 返回由x的小数部分和整数部分组成的元组) Y) P$ ^9 \5 I% N1 ~& N
- #返回由x的小数部分和整数部分组成的元组
+ V* J+ r9 H) I% s6 v - modf(x)
7 q, x$ {; N' e. E. x, ` - Return the fractional and integer parts of x. Both results carry the sign1 X/ J# T* w' y
- of x and are floats.
5 D8 z; J" q s, E6 n - >>> math.modf(math.pi)5 q- [. H3 X7 |, W3 W- A
- (0.14159265358979312, 3.0)
6 |- \1 }. D6 J a6 `5 r - >>> math.modf(12.34)
- u* S$ ^% ~9 | - (0.33999999999999986, 12.0)
复制代码
! X" j- T! n6 F! ~math.sqrt(x) 求x的平方根+ I$ j/ M% V" i9 y6 t/ F
- #求x的平方根0 G* g2 r4 P2 @( a* l& i9 h
- sqrt(x)$ h" D% Y4 r, a% r! D& o9 o- f
- Return the square root of x.1 y$ s% |! _' f2 U
- >>> math.sqrt(100)
( }. V; u9 \9 N6 ]# f - 10.0
6 c% ? v. Z% B" X. d( _ Y, h - >>> math.sqrt(16)
/ i5 l3 `8 u( { g" @ - 4.03 f% ~2 _5 d8 T7 Y
- >>> math.sqrt(20)0 q, q7 Y6 R) P. s( d5 Z
- 4.47213595499958
复制代码
! s- A7 }7 ^6 m" xmath.trunc(x) 返回x的整数部分- #返回x的整数部分
6 G' b5 ^, H9 H - trunc(x:Real) -> Integral. ^% c; y! ?5 V K3 ?
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ V; W, `$ n& g0 l0 I. X/ |
- >>> math.trunc(6.789)7 i8 y; Z$ ~! B- H6 `5 ^
- 65 A' C# Z# v* F+ D+ |2 |
- >>> math.trunc(math.pi). @8 Y/ ?7 [* A
- 3
* o6 W) @/ p- K/ o1 p - >>> math.trunc(2.567)3 p* T9 |+ U6 z8 y! J& {
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|