马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! {! v& O' Y( m0 i' L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
6 G% }. c0 V+ n K. i1 `4 Z+ n
9 E2 V k9 v0 }6 T# D方法1:1 T t$ D- C2 L: [+ b5 N+ y9 J
- >>> import math
3 r$ R" N( i) x, M3 Q1 i - >>> math.sqrt(9)
. T- g- Z* G$ n' @ - 3.0
复制代码 方法2:$ T) t1 A; r( c- d; V
- >>> from math import sqrt" H4 e' O/ a, Z1 P) s0 g* a
- >>> sqrt(9)
: B" L2 s" g0 b3 s) F, \ - 3.0
复制代码 4 C; X& S- z4 K" Y: n2 I2 w
' x( N r1 p* a" B }* H( R4 e2 omath.e 表示一个常量
: {# p. n6 ^( ?6 D" u- #表示一个常量
7 ]2 \- B! E! B+ m/ e% l - >>> math.e1 E! P, A5 Z0 { s( c
- 2.718281828459045
复制代码 + S+ ?3 v% K9 |8 L$ c( W: X6 i
math.pi 数字常量,圆周率
) D/ l8 X/ N" U" {- a: G& M' A# W) X- #数字常量,圆周率+ j$ K" B9 v! @ ]( \2 {
- >>> print(math.pi)
- n+ c% c6 U. J9 Z3 I! E0 w! m) t - 3.141592653589793
复制代码 . j! g) }: {' d" H8 q
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
" u" i! d& _: [0 O5 x- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
- K; x* {$ W, X( S# D1 [ - ceil(x)6 J3 ?8 ~" r9 `& I% `9 H
- Return the ceiling of x as an int.
$ D) F! D/ E( ~4 Y/ D - This is the smallest integral value >= x.3 o( F8 M4 x+ z, ~ Q
, S) W" y5 }/ o! B- >>> math.ceil(4.01)6 e9 Y: x9 X/ |& [" C$ B$ O9 j
- 5
/ _7 n! ?' ?- u% g) t - >>> math.ceil(4.99)
/ m- Y; A/ @; x- Q2 R6 k! ~ - 5) d7 G; q8 m4 e8 H
- >>> math.ceil(-3.99)/ B- Z" H2 Y4 m8 C3 l
- -3* k6 I& R; X5 c) y
- >>> math.ceil(-3.01)3 q A) H* ~' B: N/ F$ u
- -3
复制代码 7 b; p% C- @: l3 a0 H: w
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ s/ m, l z( h( f9 x- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 S1 i' K* O+ o+ K6 k3 j6 H - floor(x)( G" w0 s' l0 |6 t$ t2 r
- Return the floor of x as an int.
0 `- O+ @* M( D, t* G - This is the largest integral value <= x.
6 q: b4 \3 L* |8 a" D: n, z0 c - >>> math.floor(4.1), e, V: `: d1 s, z V, ?' ]
- 4+ v$ o9 P S* o. X5 r& i9 _ p
- >>> math.floor(4.999)- S0 y g% Q$ `- |6 i
- 4
4 f/ h2 v8 E8 U0 P - >>> math.floor(-4.999)% d1 T2 |. Y0 R, R) D, `: P& E
- -5
2 F7 w% E0 A5 O3 ~ - >>> math.floor(-4.01)" [% k ~0 D' v4 o" J# L: Q/ N
- -5
复制代码 ! a2 L+ l2 q! [
math.pow(x,y) 返回x的y次方,即x**y
2 w6 D$ E7 L/ s7 z3 t6 Z: y- #返回x的y次方,即x**y
0 I' \% Y8 L* e9 l& y) ` - pow(x, y)2 L6 |: N' f: I4 u
- Return x**y (x to the power of y)./ G4 V0 \% `* F; s p' ?! h
- >>> math.pow(3,4)
$ h5 D6 M+ i) e4 _$ E - 81.0+ E0 F- I; t, r1 B+ W3 k+ i
- >>> 7 N/ |; ]2 l* i/ }5 r
- >>> math.pow(2,7)9 ] j' t, t9 b
- 128.0
复制代码
) P; `7 w* |" Y1 W7 R) W0 imath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 \2 O% q8 z' g2 _* ^- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 v, l& a2 m; Q+ N! e0 B$ `
- log(x[, base])) u" _! Q* {' h" ]+ {
- Return the logarithm of x to the given base.# o! O% X) P a- m
- If the base not specified, returns the natural logarithm (base e) of x.
' g. l0 t6 d" ]/ p8 R6 _ - >>> math.log(10); m2 u, y5 i0 q8 Z; o7 @
- 2.302585092994046
4 ]. K% b4 E1 t( P9 ~' w3 o# d - >>> math.log(11)
4 b$ {9 z( ?5 C - 2.3978952727983707+ o6 m; h3 M, a3 v4 P' V) ]. |8 o
- >>> math.log(20)% G. i8 A" A. g: J( v! h
- 2.995732273553991
复制代码
; y* p, s4 V3 f; tmath.sin(x) 求x(x为弧度)的正弦值6 V$ H7 `" z7 r0 |. K/ }+ @0 p5 H- Y
- #求x(x为弧度)的正弦值
$ F6 x# Q' F% |7 N+ }$ h9 v; q - sin(x) j; D! l8 u4 u& Z1 Y
- Return the sine of x (measured in radians).1 Z8 Q l( e/ Q
- >>> math.sin(math.pi/4)! D7 I) m/ |# r t$ v
- 0.7071067811865475, T; P2 |2 d) W" M& [ k
- >>> math.sin(math.pi/2)6 A7 E* ~# L5 e4 y; |6 f
- 1.0
( A6 p( a' q: y7 }. }, p - >>> math.sin(math.pi/3). y& n% n# P/ \; K9 Q5 s% P$ n
- 0.8660254037844386
复制代码 : s# y+ P+ c. c; J J' s
math.cos(x) 求x的余弦,x必须是弧度
2 _* t' R. x! P; F7 |- #求x的余弦,x必须是弧度6 t# x& r6 X7 [5 t- W
- cos(x)6 n& f* `3 O; b5 r: Z! o! o
- Return the cosine of x (measured in radians).
( O/ Z! S3 o0 C0 |4 l- _: y: t - #math.pi/4表示弧度,转换成角度为45度
0 o, H* E$ i% |/ L, g: _+ { - >>> math.cos(math.pi/4)0 d0 m. C$ N/ Y" x, d/ O
- 0.7071067811865476+ F; ]+ e, L" h
- math.pi/3表示弧度,转换成角度为60度" \4 z) }( @; ?3 @
- >>> math.cos(math.pi/3)" I# _ o) t8 F0 Q# f
- 0.5000000000000001
; Z8 K" t7 w J) F1 b- e' i - math.pi/6表示弧度,转换成角度为30度
f9 h+ [3 Z; L+ L$ J- S - >>> math.cos(math.pi/6): h. e/ s; v" Z# ~8 ^/ l
- 0.8660254037844387
复制代码
0 W3 P7 ~8 j5 }% Smath.tan(x) 返回x(x为弧度)的正切值
4 _% O- b/ r' g7 Z; M5 R- #返回x(x为弧度)的正切值0 [: h' |) f# Z' j- X+ a, y `
- tan(x)" E! n7 N" L3 t0 c$ T. t
- Return the tangent of x (measured in radians).
/ W& _8 o6 B) \2 o. ^* A ^3 y- h9 U - >>> math.tan(math.pi/4)* Z, \; B. h+ _2 ?3 e8 X
- 0.9999999999999999
" s5 v" g% U5 `; O a$ D! I% E - >>> math.tan(math.pi/6)
1 t$ N3 }0 m* H; M; A. _' }. C1 K - 0.5773502691896257+ k0 y8 j* t7 P! \
- >>> math.tan(math.pi/3)1 W. ]6 Q, }$ E+ A" ~
- 1.7320508075688767
复制代码 0 `% o! b d2 M9 T
math.degrees(x) 把x从弧度转换成角度
3 \8 v* J( I" ?* V" h9 f- #把x从弧度转换成角度 e9 |/ k3 |% O
- degrees(x)
5 ?. a5 C: Y. v# Q. p( E - Convert angle x from radians to degrees.
0 K _; a& n- F( p
+ ^( \4 L' `/ N% F) h- >>> math.degrees(math.pi/4)
1 ~$ q% F+ z0 N2 U2 `( ^2 M - 45.0/ w0 P' t( W. h1 F8 _; \
- >>> math.degrees(math.pi)
% t+ \; ]- ~. L# Q - 180.0) z3 @. [( F6 d8 h% B2 B6 _
- >>> math.degrees(math.pi/6)4 u" y: I$ {' v8 r) C; m+ l$ `" l
- 29.9999999999999969 \- ]$ e( g7 a3 y5 I Q
- >>> math.degrees(math.pi/3)
& z. d1 ~/ E- d c1 D) R& L - 59.99999999999999
复制代码
- ~0 c4 F8 f/ |' v5 V$ | gmath.radians(x) 把角度x转换成弧度
8 I, u9 f0 i; x1 C9 z# P, b- #把角度x转换成弧度$ j+ o$ {; x# i/ r5 O
- radians(x)
( x! N8 W% N. h' {4 `! j - Convert angle x from degrees to radians.
5 j% V4 w4 ^) |' k - >>> math.radians(45)( P9 }6 c6 Y }0 t/ l
- 0.7853981633974483* t6 ?, X z& O; N, j2 A3 |9 i
- >>> math.radians(60)# Z2 s7 n8 J6 [% ^5 Y1 q
- 1.0471975511965976
复制代码 * v1 E& E0 b" y4 Y0 X
math.copysign(x,y) 把y的正负号加到x前面,可以使用0! O! t4 p7 ~6 u: ^" }
- #把y的正负号加到x前面,可以使用0
8 M- g' }* }. h6 w: _ - copysign(x, y)
k8 y# `5 J- ^8 y, `4 g - Return a float with the magnitude (absolute value) of x but the sign
) ~9 n4 c0 k) w5 y! }0 C" Y* x$ ` - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' A6 `% u& s9 T3 V+ W
- returns -1.0.
: A& [* S6 u }0 W0 F# N
% k8 j4 e( u* g4 D3 s9 |7 h" b1 B% N* |- >>> math.copysign(2,3) `) a5 r- m3 J. i
- 2.0 I9 R7 q% M/ S7 v# Z5 F9 {" I
- >>> math.copysign(2,-3)( f: v. d/ g2 `$ i8 ~9 j
- -2.0
7 L9 }4 ^; w1 X9 N - >>> math.copysign(3,8)0 w7 V9 C3 o% M {
- 3.0
5 f5 l$ h$ K7 d3 w2 h7 X' K - >>> math.copysign(3,-8)
- U& M, v5 H# M" v" o6 f - -3.0
复制代码 4 r i- o( \3 ?! j
math.exp(x) 返回math.e,也就是2.71828的x次方
) F5 u" e" d! b( Z- #返回math.e,也就是2.71828的x次方2 ^7 y" ^$ @/ @3 u4 x
- exp(x): B" F- M' k! Q% [1 Q
- Return e raised to the power of x.) k$ S& [) y# d9 A! J: q6 H
- " B( @+ `# w3 I3 Y, u y( p
- >>> math.exp(1)
! Q- l% b+ S4 S0 w6 i- S# {& i# d - 2.7182818284590457 f- Q4 N! z, j/ f1 z
- >>> math.exp(2)8 W9 v3 o7 M8 s" k. |! W3 z3 M
- 7.38905609893065$ E" @0 a1 D% n
- >>> math.exp(3)3 V* _9 ]# K( P x4 ]5 {
- 20.085536923187668
复制代码 % a* M) q9 V z$ T5 k) z
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
. Z+ K: V% T) h) z, w3 B8 X( u9 o- #返回math.e的x(其值为2.71828)次方的值减1
! r6 w9 ~5 _0 d1 G8 d; |) [! e' Z - expm1(x)' t. z: j, i6 P$ W" l
- Return exp(x)-1.4 q( S8 e2 n4 F
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 M7 X. ~$ i8 i" e$ Y" u9 V& x
* G' ~' V3 K8 P- >>> math.expm1(1)
D' d' C8 C$ z! e - 1.718281828459045
. ]% g$ A: w3 }5 C - >>> math.expm1(2)5 [2 S9 P. e7 X& v! e4 x
- 6.389056098930658 g! B$ R8 ^! J
- >>> math.expm1(3)
( Q u: }. ~6 ^" U - 19.085536923187668
复制代码 4 y% K; v4 t, p% ^' l
math.fabs(x) 返回x的绝对值
2 i; a# {+ I* E6 _( H" [# v- #返回x的绝对值- k1 C- P8 x) V
- fabs(x)
) u5 ]# k' H: t% g - Return the absolute value of the float x.2 w% u1 @8 B4 Q9 W
- , D \" c/ t# T* D) j
- >>> math.fabs(-0.003)% I, q7 i% G" t% m
- 0.003
$ i$ ^* o' \: n& A# ? - >>> math.fabs(-110)
- y3 x0 a' I8 o! d" p, B6 b2 g8 e - 110.0" X' }, n! F8 S. C& B. Z$ v
- >>> math.fabs(100)1 ^1 P9 U! P/ N) @
- 100.0
复制代码 0 b- y/ Q! X, z" R8 y' w8 W
math.factorial(x) 取x的阶乘的值 }% N9 J% D0 }9 q4 i5 W* e( S
- #取x的阶乘的值
$ i9 k6 _8 m S t - factorial(x) -> Integral
$ o% \4 k7 B0 f! |; R - Find x!. Raise a ValueError if x is negative or non-integral.
7 G9 D- V, N& P6 U - >>> math.factorial(1)
- \0 a! }" Y$ ]3 F - 1* j1 V# r+ U; i$ H3 X
- >>> math.factorial(2)* B7 L; A9 q9 R# \7 g
- 2$ R* U, O- i: z
- >>> math.factorial(3)$ x5 f# J, D7 \$ ?7 Q9 j/ B6 w
- 6& c1 a# O& G9 P4 x. f; z
- >>> math.factorial(5)
$ w- [) |8 {# K6 r - 120
' ], d; A& ^" x3 j - >>> math.factorial(10)2 ?9 {% i% R; k- f9 N
- 3628800
复制代码 % f, d' i2 @3 E! x' k4 n
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数, |; z0 Y" p: Z" d' `
- #得到x/y的余数,其值是一个浮点数
+ n9 b6 [8 F+ Q7 R/ {- C2 Y - fmod(x, y)
% P; h+ L$ x V7 E! b7 l8 k - Return fmod(x, y), according to platform C. x % y may differ.' j' o# u# U: w B+ k
- >>> math.fmod(20,3)
- A* o+ @- {7 }4 r& O" E& g - 2.0$ z+ t! v9 ]1 N* \# Y6 u" K2 b& n
- >>> math.fmod(20,7)
5 p7 y# o! x+ b - 6.0
复制代码 $ H2 J k0 P. n3 |& k+ h: D& G
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) L5 A) m0 F7 }& O0 ^
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 e- ~: }+ }6 X, I - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
s1 [: I9 D. b- h- {+ G T - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
/ ^% h! e: ]& R( q- N5 v - frexp(x)
* N8 \! ?$ ~5 S" u7 o - Return the mantissa and exponent of x, as pair (m, e).
/ h5 e% Q. w1 B - m is a float and e is an int, such that x = m * 2.**e." ? Z: W8 y; W$ q
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.! _6 q2 s! p3 Y v
- >>> math.frexp(10)
6 j: q% z3 @; b* ] - (0.625, 4)4 v4 V- t; ~, ^; D& }: ~+ Y
- >>> math.frexp(75)7 f8 {8 B8 `/ [: v% y
- (0.5859375, 7)) v* R+ n( f7 C% \3 g
- >>> math.frexp(-40), K; p1 Z5 m. m8 s7 C/ ^. a1 I! t; G
- (-0.625, 6)
+ I4 ~( d; y9 { - >>> math.frexp(-100)
1 s, m# g! N5 [ ? - (-0.78125, 7)9 Q' [( X- s. j$ r2 U
- >>> math.frexp(100)
3 s( l$ S' ^8 H' ^ - (0.78125, 7)
复制代码 8 `: }: e& x3 u; N: k
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
: R' {; X- @, b) b- #对迭代器里的每个元素进行求和操作
- z! ^; o& D/ ~, h2 I* ^$ _ }5 v - fsum(iterable)1 X K$ }6 h8 u3 @2 p
- Return an accurate floating point sum of values in the iterable.) n' v2 A+ N( F4 m; F8 {+ y
- Assumes IEEE-754 floating point arithmetic.! B5 K0 S# x x4 u/ {' V
- >>> math.fsum([1,2,3,4])
% k8 G$ q" s, ?( c" ^6 Q - 10.03 V; G) I) T2 D8 U, s
- >>> math.fsum((1,2,3,4))8 ^5 Z2 s, Z; D) Z
- 10.0( w# r: U( e8 W/ p- e9 q9 d7 f
- >>> math.fsum((-1,-2,-3,-4))8 _, D2 m0 {9 e/ s T
- -10.0; ~ J" T' {& m/ R3 j6 ]( B
- >>> math.fsum([-1,-2,-3,-4])0 ]+ C1 R0 P9 b" {- Y! C$ t+ l
- -10.0
复制代码
6 l0 T' l& v5 b6 g2 u! g1 kmath.gcd(x,y) 返回x和y的最大公约数
5 B6 ~& j# o- P i" f4 ~- #返回x和y的最大公约数
$ w5 l/ n. T5 W' k/ Z+ n - gcd(x, y) -> int0 ~$ }3 _! u) s: V1 S% R
- greatest common divisor of x and y
9 w+ Z7 C3 i+ c: Y! c - >>> math.gcd(8,6)2 a4 B) j* `7 u3 ]7 W
- 24 P8 Y v1 l6 b) y' a5 |
- >>> math.gcd(40,20)
* C8 F1 X* L: c' E" N - 202 p3 a( i) F+ R. r& D
- >>> math.gcd(8,12)
3 v1 n' v% Z7 G: Z) Q0 `$ l - 4
复制代码 * f$ U# Y- H! N- I& k
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False8 g. f/ W$ ?. M" n+ n1 T
- #得到(x**2+y**2),平方的值! u9 m: W# Y6 E. P1 Z5 P& Y! V3 r
- hypot(x, y)
& _3 M; u6 Y# n+ Z - Return the Euclidean distance, sqrt(x*x + y*y).; Q3 {, w& @4 T0 n/ R2 N* h5 ]
- >>> math.hypot(3,4)
, X/ F+ Y% @. m5 J4 l - 5.0
: {. @4 e( r9 }( u. L; D - >>> math.hypot(6,8)
* B8 C0 ~0 s: |; ]0 L - 10.0
复制代码 ( f/ G! e% K$ Y; Z2 Z
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False- X# Z5 B; n( l/ n
- #如果x是不是无穷大的数字,则返回True,否则返回False
C4 C, R" S8 O# y+ P. v1 y) j' B' U* J - isfinite(x) -> bool( j* l* M0 J1 X) a* g* K& \
- Return True if x is neither an infinity nor a NaN, and False otherwise.
0 L, T2 i* o3 c4 W - >>> math.isfinite(100)
' n M+ y }! l9 I6 y4 |) T - True2 {1 v7 N. h$ x4 Y. O/ A7 s
- >>> math.isfinite(0)
5 W. D% T: W1 ^3 M% y - True# \2 g* r V1 p' f' C
- >>> math.isfinite(0.1)
! \' [0 y/ x& _# H8 p# S - True
& E2 ]* t" V9 S, n. Q6 \' g - >>> math.isfinite("a")- m+ S! l/ @# u$ b3 v' R2 U3 t& F
- >>> math.isfinite(0.0001)
6 D7 y% q8 }" V - True
复制代码 7 p2 `" e7 u8 @8 q. U7 F
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
' u1 f6 p, C& f% w& o- #如果x是正无穷大或负无穷大,则返回True,否则返回False( `& P* J& v+ D& N) E( L* @
- isinf(x) -> bool
" i' I6 B/ ?8 v7 Q4 _: O, F3 \, D k - Return True if x is a positive or negative infinity, and False otherwise.+ s' R9 G: c8 Z' ?3 C
- >>> math.isinf(234)) g- `% @5 f' j9 ?& B. ^" s
- False
) B8 P, L- v0 {& ] - >>> math.isinf(0.1)8 J* g9 l4 w1 E
- False
复制代码
3 y' P5 A) Y2 j5 D# ?4 N0 W" G Z: `math.isnan(x) 如果x不是数字True,否则返回False& Y6 }# G6 Q7 ]
- #如果x不是数字True,否则返回False
. C% F0 L/ |: q5 K4 f - isnan(x) -> bool
: B; ^3 P: }5 t( W0 ]* q - Return True if x is a NaN (not a number), and False otherwise.
1 o8 |% c4 O5 M* P o3 d( R - >>> math.isnan(23), q! O; [# J# ~
- False
% S% T+ W6 t5 n - >>> math.isnan(0.01)& H+ Q; s3 } L8 k
- False
复制代码
8 g2 |) @! ]2 u% u' e$ Amath.ldexp(x,i) 返回x*(2**i)的值9 ~6 S* \) o$ b0 H* A
- #返回x*(2**i)的值
, z+ B$ p+ P, A$ c F. f3 j6 ~2 U - ldexp(x, i)
# ~5 i X) `) [- n. s! I/ S1 r3 }$ X - Return x * (2**i).
1 R+ x8 o3 i7 s; p+ u6 E$ y$ ^ - >>> math.ldexp(5,5)
" o" C# F- g. R- g$ _0 b - 160.0
+ D+ a7 E/ s1 n3 X - >>> math.ldexp(3,5)$ a0 P+ M. n V
- 96.0
复制代码 # K5 B$ v! D w& k4 W$ [4 u
math.log10(x) 返回x的以10为底的对数
" `; H' m1 n* E6 ]; u- #返回x的以10为底的对数5 ^# ?! O |% o& D
- log10(x)
, o8 N2 g1 N0 T8 I: i - Return the base 10 logarithm of x.& v. @# t, z4 @/ M+ W
- >>> math.log10(10)
. n0 Q9 M+ u$ |4 Z) n7 g - 1.0) Z7 D( z) i6 q# l
- >>> math.log10(100)
9 U' A, {0 O& p( ]2 l - 2.04 I3 ^% g& |0 j8 C2 B- V
- #即10的1.3次方的结果为20) `% ` Z5 r: V: j+ T
- >>> math.log10(20)
4 m) l) j' u+ K8 o% T$ Q - 1.3010299956639813
复制代码 # E+ d7 }0 [: S$ d, l9 L
math.log1p(x) 返回x+1的自然对数(基数为e)的值
7 b* P- [; X" ^$ J) l% R- #返回x+1的自然对数(基数为e)的值
# L' w: o C, W- M& y - log1p(x)
; `- e- [- K+ O - Return the natural logarithm of 1+x (base e).
1 D; f6 r% s7 ^ - The result is computed in a way which is accurate for x near zero.. H' N8 m6 ~. x/ p, ^
- >>> math.log(10)
4 z3 V; a# }% g+ I6 W& M& d - 2.302585092994046
2 k7 D: P/ P9 M2 W - >>> math.log1p(10)3 I- T( `$ z7 J0 {
- 2.3978952727983707
5 m3 j7 u L% k - >>> math.log(11)
; n6 t; _ {% ?4 l! w - 2.3978952727983707
复制代码
5 `8 h3 f( q& @+ k* i/ Cmath.log2(x) 返回x的基2对数
+ D8 ~0 |3 N2 x, {$ P- #返回x的基2对数/ B$ p6 K1 _0 W! _3 h: O
- log2(x)4 r3 y& `7 w2 X4 ^& f$ w% A% q
- Return the base 2 logarithm of x.
' u" e8 _/ V& x5 P+ `: M2 a7 ` - >>> math.log2(32)' \# L' J+ W S: ^6 L1 s
- 5.0
6 ?" B$ F; `( w! V$ s) f - >>> math.log2(20); |( \+ x6 B A7 \" R
- 4.321928094887363
, `0 w- l0 W- O6 c6 W( V# | - >>> math.log2(16)
) `5 }5 i+ h+ E4 i - 4.0
复制代码 ; J5 F3 ~, h$ F& B8 M; G
math.modf(x) 返回由x的小数部分和整数部分组成的元组 P3 Y% Y8 _+ e7 g- }
- #返回由x的小数部分和整数部分组成的元组
' y8 I1 S( m& W! R& M - modf(x)0 j! g. m& @* p1 C# r
- Return the fractional and integer parts of x. Both results carry the sign9 x. D" R$ ?3 c
- of x and are floats.
" ]" N; m* J; W: q j - >>> math.modf(math.pi)
+ T- c" F8 }; F5 m$ }9 V% [ - (0.14159265358979312, 3.0)
Z. q- g! o& \ x3 ^. K6 H - >>> math.modf(12.34)
9 b: [ B- e$ r; W7 A - (0.33999999999999986, 12.0)
复制代码
+ X( G- J0 {) r1 }math.sqrt(x) 求x的平方根; ` R2 P" H9 n+ R
- #求x的平方根4 U5 ^- \6 e8 f
- sqrt(x)
* H. e8 @& R8 H( {3 {6 k - Return the square root of x.
9 }' E1 G1 a. j" w - >>> math.sqrt(100)* d3 W% j3 c1 r6 k5 ^3 a0 f- }
- 10.0
* P6 T, P9 K# {( S+ g - >>> math.sqrt(16)
9 i" v. k* Z" S6 q- I - 4.0
* \6 |6 s7 s3 x: o8 H" k& S/ x. \ - >>> math.sqrt(20)
& }4 g3 @9 b' N8 {4 }0 J - 4.47213595499958
复制代码 $ k% a3 @' W& T
math.trunc(x) 返回x的整数部分- #返回x的整数部分2 E: B. a9 Z) i- r8 L! M: \! ?
- trunc(x:Real) -> Integral
5 N5 I& L! N8 }+ f( Z+ x& I - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
& T2 d+ o9 y! [* _! o( \4 i) K. @ - >>> math.trunc(6.789)& _, v) |- a0 V
- 6
" w% Y: |4 `4 O, }6 l! x. q3 ^ - >>> math.trunc(math.pi)
/ r' z* N/ h6 U9 w+ f: U& J* D - 3- _/ p- S+ n* @6 F1 i; j
- >>> math.trunc(2.567)9 h8 l0 C) A$ Q/ j* G, j: T) T$ G- ]
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|