马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* _5 u" y% [; }' Q# U" M
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" g( ~& @& M2 C* E" D! h
/ p2 V( f* s) w, Z方法1:
6 p) t( B1 X. [5 o) g4 X7 |: k( D- >>> import math8 }- F; x x2 R6 w+ V- S* ^
- >>> math.sqrt(9)
# M p7 Q. e, T0 h - 3.0
复制代码 方法2:
+ W' t% [+ N& k4 h& X) y: z6 F5 G3 B- >>> from math import sqrt
5 Z0 J' C) _$ F' l5 [: R7 Y - >>> sqrt(9)% y, m( `; D& ^5 Z
- 3.0
复制代码
$ B/ F" M' ~3 K' t0 Z# u9 N ; w7 T* P$ ?! o2 m4 \9 ]* ], S" g
math.e 表示一个常量
* `- ]6 m' q: r1 k+ d# a! K- #表示一个常量
1 [$ [4 A5 \2 k2 Y - >>> math.e
5 ? W, c+ g3 t1 K - 2.718281828459045
复制代码 3 u3 m7 B* q- Y# j/ p; c
math.pi 数字常量,圆周率4 a: H0 N2 N0 X: f0 m
- #数字常量,圆周率* o _7 E% D0 W1 b/ }
- >>> print(math.pi)
# l: P# R/ [% |4 ?% e - 3.141592653589793
复制代码
( a. X/ s' `, D5 gmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
, S7 d( ?2 ^. P9 Q; k% i1 F/ Z& ?- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
" @7 ]. {) W8 s' L. d# L9 F% j* U - ceil(x), I9 n( E' N( |6 R2 u
- Return the ceiling of x as an int.
/ i2 w/ j/ l2 q3 N+ i$ R0 ]% O - This is the smallest integral value >= x.0 w/ r9 Q8 ~$ \" q9 V* q7 T8 t
- 9 j& E+ C% X7 g6 X* ^5 C; {
- >>> math.ceil(4.01)
7 ^) U* g+ e+ }+ D6 v$ O - 5
6 _5 O7 e+ a; Y% M! Q9 y# k* T - >>> math.ceil(4.99)
0 R/ D3 f! P2 [9 h - 5' Q2 k3 o. ?+ i1 b7 P8 e! k7 {
- >>> math.ceil(-3.99)8 i; A/ ~" J3 d/ M5 P7 x
- -3
) x- }$ Y2 ?' ]4 x9 }/ E - >>> math.ceil(-3.01)$ e$ r4 [* E& z" Y5 y, M: Q
- -3
复制代码 1 D3 m3 w( d. a' ~# r6 K1 u
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 q! o B: d9 A5 d3 D1 r' V
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身 P+ y0 e& A0 M! j5 i' N$ ]- B
- floor(x)7 [: }7 x- a+ ]- s6 C4 p
- Return the floor of x as an int.0 X' I* a9 J5 ]- X0 m
- This is the largest integral value <= x.
2 S: W5 p6 b& p/ a# X - >>> math.floor(4.1)
+ @3 ^ ^8 J& `8 O" I - 47 l Z4 S* r- r8 u
- >>> math.floor(4.999)( v, g) `9 p/ @2 s! e! }& x
- 4* L4 R2 f3 V' k- V9 i
- >>> math.floor(-4.999)+ J; S4 l$ E- h. b( e+ j% h' g5 v
- -5
. m, n0 |( g& n8 K5 o+ I; w - >>> math.floor(-4.01)/ A; `1 D& ]% o& Y' A; d
- -5
复制代码
* K" q w$ P* _% O( Imath.pow(x,y) 返回x的y次方,即x**y
1 s, f( J1 B3 p- #返回x的y次方,即x**y* d2 V/ u7 z; K
- pow(x, y)! R" k4 Q6 Q& m/ s, O$ B
- Return x**y (x to the power of y).9 U, i# {; t& x6 d- U
- >>> math.pow(3,4) ^' g# |) }* T
- 81.0
( p; |5 N4 `0 _7 ] - >>> , C% W2 e. Z# f) `7 {% N2 j Y1 \5 y4 Q
- >>> math.pow(2,7)# X2 P( Y8 K) Y- C
- 128.0
复制代码
2 v# t- M. r7 Umath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ \9 G. k# A# X3 r
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# T1 W( @1 Q6 z6 a1 m" o - log(x[, base]). e" P# E9 I/ i* b
- Return the logarithm of x to the given base.- a8 }# S$ b5 v
- If the base not specified, returns the natural logarithm (base e) of x.) i9 \5 f: F L% M* b& x
- >>> math.log(10); i6 B" y8 }! @# d* V
- 2.302585092994046" i8 p3 k2 B8 y) b1 E
- >>> math.log(11)
# G8 E' |; {4 U6 S+ Y, V - 2.3978952727983707$ f7 p% k2 j7 g3 S8 ]3 ?
- >>> math.log(20)* [( ?0 }- g/ d) o" |( X5 B7 g
- 2.995732273553991
复制代码 ' ?/ u: ?9 @7 a) i0 N. z2 U. _
math.sin(x) 求x(x为弧度)的正弦值
' `, A3 ~% c1 I' c# b; N9 I1 s- #求x(x为弧度)的正弦值
$ p4 C, k* z6 D* N - sin(x)! c0 X3 P4 A6 m0 j* ^+ h
- Return the sine of x (measured in radians).' u6 @4 s3 i7 B7 {0 r
- >>> math.sin(math.pi/4)/ y* p8 g: f1 m; T
- 0.7071067811865475
1 J% v5 g2 L. w* U - >>> math.sin(math.pi/2)2 W* |# K* G+ v7 M, J
- 1.0
- }2 L( B- t+ n0 g0 C - >>> math.sin(math.pi/3)
' b* K5 t6 B! u' y+ Y' J - 0.8660254037844386
复制代码 2 b" Y6 m3 {) y, P5 o, C
math.cos(x) 求x的余弦,x必须是弧度' v n5 I0 A& B/ x) } G
- #求x的余弦,x必须是弧度
" g. k( ]* ^ Y) t, ~ - cos(x)
: _1 ]! W1 J1 n9 u+ q, w - Return the cosine of x (measured in radians)., @# o. n; g, \$ R, i
- #math.pi/4表示弧度,转换成角度为45度
1 ^: B7 }& U, U' C - >>> math.cos(math.pi/4)
# P6 q+ R [& H. s/ j1 d - 0.7071067811865476" k" [% H V$ z) X& k! {# F7 T
- math.pi/3表示弧度,转换成角度为60度
0 j. M, v2 \- V0 _# ?& { - >>> math.cos(math.pi/3)) r$ J0 v, P3 |3 b, u, U" @
- 0.5000000000000001
, t5 o5 ^# b3 W6 J - math.pi/6表示弧度,转换成角度为30度
) X; T0 b; [0 @) o/ [) X8 e - >>> math.cos(math.pi/6)
9 [# @5 M; M- C - 0.8660254037844387
复制代码 5 @7 n) r( E2 `& S2 o9 z
math.tan(x) 返回x(x为弧度)的正切值
/ Q( {0 \/ t5 W! c! `% N( s, ~$ X- #返回x(x为弧度)的正切值5 u# v0 e/ L5 A1 y
- tan(x)
( Q: V9 H5 f; l( q% K+ n5 w5 g- ~ - Return the tangent of x (measured in radians).6 \. o) Y# ? C3 q9 {9 ~8 M
- >>> math.tan(math.pi/4)
" N, ]' b! |4 E, P( ^- V" y) v% X - 0.9999999999999999
+ _% }, K6 S" k* j2 y6 t& t - >>> math.tan(math.pi/6)" H5 w* X6 a, G2 w7 N% M9 ^2 p
- 0.5773502691896257* t! b& X0 `/ }
- >>> math.tan(math.pi/3)
. Z o z8 r6 ` - 1.7320508075688767
复制代码 9 t. q( e, e; [ x/ W, A
math.degrees(x) 把x从弧度转换成角度
. B: e' ^) `/ M7 m, O+ c1 e- #把x从弧度转换成角度# E" ]' Z' d/ g' d
- degrees(x)# U4 z- x" r/ M6 Z& `, E
- Convert angle x from radians to degrees.( ~) G9 q/ l3 M3 Q0 k
5 Y( r1 n' L& o: X- >>> math.degrees(math.pi/4)
* e( B7 z7 j& K - 45.08 E" z( a+ B2 ~$ ~
- >>> math.degrees(math.pi)* Q. d' `; n" w
- 180.0
3 i5 U' ?8 {7 c& w' i* ]" I* a - >>> math.degrees(math.pi/6)
% G1 |0 F/ K1 d - 29.999999999999996
# v; ]! a. c" d* V- M - >>> math.degrees(math.pi/3)7 P* X6 n; A, m; n, \' @
- 59.99999999999999
复制代码 ) l9 |) t0 j" T
math.radians(x) 把角度x转换成弧度/ Q6 [% g( L, U
- #把角度x转换成弧度. _4 | K: V; L& {5 I0 M( [
- radians(x)
# O3 S0 U: i$ a5 z: `. ~5 t, } - Convert angle x from degrees to radians.' k- {. T. [" S+ O: _
- >>> math.radians(45)
5 h. i8 m1 h i8 K2 F) B Y - 0.78539816339744834 k% X' i5 ~( K' }0 {" S. Z6 Y, }, Y/ k
- >>> math.radians(60)+ P( J* b5 _; ~# a; k" n9 D
- 1.0471975511965976
复制代码
7 _: g8 l, X X( X" X0 l8 u( B7 mmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
. C. P: | O4 F' d7 p P6 z3 r- #把y的正负号加到x前面,可以使用0
; O1 l' T' a* [, ^. W- ^9 Y - copysign(x, y)
^: l9 S) p2 }2 Q3 u - Return a float with the magnitude (absolute value) of x but the sign
) n1 `1 Y; u3 T N - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) R9 b6 a+ k+ W" `8 a% _1 Q
- returns -1.0.6 ^4 w! O! ?& W2 G% I6 o
$ o0 D0 I! g% Y: d- >>> math.copysign(2,3)
& A0 {3 t I& z1 j - 2.0
$ O* {/ f! D4 D - >>> math.copysign(2,-3)1 D8 w0 O$ G1 d7 h L. F
- -2.0
9 s2 L- g& K% h0 `5 G+ ^% d4 C - >>> math.copysign(3,8)0 b8 W7 @' d' ^9 `
- 3.0) l8 X ~' ^+ J4 S( X' `' v R1 Y
- >>> math.copysign(3,-8) e" S+ A6 m) F# b% p: }$ R0 d
- -3.0
复制代码
" H$ C. Z/ ~" ~# k- m, N0 t: U* ~math.exp(x) 返回math.e,也就是2.71828的x次方' g& ]2 A ?& S Q4 o% [- X% I
- #返回math.e,也就是2.71828的x次方5 \4 ]$ T+ x/ i
- exp(x) A E. [. X% s. O' n! R
- Return e raised to the power of x.3 G' e( P! K3 {3 T& F
. U: ?3 v/ G( ]8 |7 O- >>> math.exp(1)7 X+ {0 Q+ d1 e) ?8 U3 T
- 2.718281828459045
4 d8 i3 b( d, I' p( t - >>> math.exp(2): J p! o: v* z9 W) d. E/ p
- 7.38905609893065
3 M4 c+ t% C; Y3 b& x+ D! o' K - >>> math.exp(3)9 ~, \( l; v {8 {) c2 M
- 20.085536923187668
复制代码
9 \$ @( D9 L: u) _math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
. l5 k c9 w, ?5 ]5 G8 n- #返回math.e的x(其值为2.71828)次方的值减1
" ]$ K$ t5 N0 N& A" e - expm1(x)
( y M) t. A4 h, ^% c/ ^ - Return exp(x)-1.% `0 i- H `/ W' z5 |- b9 x. D7 y
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.) w6 F4 c6 z+ x
- ( ~1 X" v) f# K( N4 p m c3 c$ H
- >>> math.expm1(1)
5 g' D0 i0 v7 B: u - 1.7182818284590458 d6 n7 @! s1 r6 S2 d
- >>> math.expm1(2)
! ^9 U, H7 _! j - 6.38905609893065# }! ~& m& [( G4 U: \2 h
- >>> math.expm1(3)9 ]4 s& }& Y7 K9 N0 L( X/ m- B
- 19.085536923187668
复制代码 / W# J: e) B+ s1 v
math.fabs(x) 返回x的绝对值8 g- @6 Q- e& m1 J1 g9 e* J
- #返回x的绝对值+ J9 Q- H# R. ?
- fabs(x)& ~/ x4 f( t$ b8 Q$ t
- Return the absolute value of the float x.
) x1 Z% h' h) u. X: I$ F% i8 r
8 q8 P, I4 c+ ]6 L( n- >>> math.fabs(-0.003)
9 l% s4 d2 Z- }) f - 0.003! [9 r& Q; M: p
- >>> math.fabs(-110)
8 B" {$ @0 V, D - 110.0
% G4 g b0 o/ M6 k1 o$ M - >>> math.fabs(100)
$ \6 I# |! M3 a# |, f7 b - 100.0
复制代码
2 T- B( q" R8 A9 r4 }7 h: r2 zmath.factorial(x) 取x的阶乘的值
0 R& q" u/ u7 I( ~* `8 n- #取x的阶乘的值* V3 `, ~9 p& A, f' d! [/ _( d- q
- factorial(x) -> Integral7 S9 |- J5 U! S/ Y# |! t7 C8 z
- Find x!. Raise a ValueError if x is negative or non-integral.
6 v1 z4 s1 e a$ ]" G# ?& B - >>> math.factorial(1)
( ^7 H' [1 n4 @ - 1! F; w2 A9 a5 r
- >>> math.factorial(2)8 b: j5 m2 s7 h2 E3 S
- 25 R3 D& N0 z! }, f- }0 k6 w
- >>> math.factorial(3)
6 S" Q0 \2 J7 n5 ]/ z1 L0 \" V - 6 H/ I/ s2 l! Z! N
- >>> math.factorial(5)
) ^' q! H* w9 D3 r) G- y2 ] - 120
$ f c1 f' O2 ? s7 M2 a' H8 G - >>> math.factorial(10)6 `3 ~: z7 \$ i8 \
- 3628800
复制代码
( A7 I y8 f2 e7 tmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
- ^6 [" `, w6 b- Z8 N- #得到x/y的余数,其值是一个浮点数! p) R1 j/ q% ~8 c# E4 G& K% ~9 o9 {; t
- fmod(x, y)
# ?1 ]+ ~8 T# P. X) |4 o. C3 S - Return fmod(x, y), according to platform C. x % y may differ.
( o, O9 x! T! J; `8 y* O - >>> math.fmod(20,3)
. I3 y8 J* o1 @9 ~2 o; Y - 2.0$ z6 l' v! \) t- s5 Q
- >>> math.fmod(20,7)
; @ M' x' M$ s: G - 6.0
复制代码
9 L. u& f1 g( Kmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" o8 N- u3 ^& P/ z
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' {% L) @% q% _# B( G1 S
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值+ Z* t7 M1 H, e' j
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1% T* Q3 B/ e# e9 v4 H+ i
- frexp(x)
: e% r: H1 Q( ~ - Return the mantissa and exponent of x, as pair (m, e).- q/ s4 e' l4 P) h
- m is a float and e is an int, such that x = m * 2.**e.# j0 s9 K G2 C) ^" }7 r) `: b
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0., O5 I+ B6 Q: i' K8 J1 \
- >>> math.frexp(10)
! ` r, r! x* D - (0.625, 4)- T7 X7 w6 h2 l. G1 w
- >>> math.frexp(75)9 W: Y2 W8 T$ L I& M
- (0.5859375, 7)
' h' I, n: K( n# V - >>> math.frexp(-40)
/ Y2 U: L, C) G3 C8 b: V. y - (-0.625, 6)
! T4 \% Z8 z9 P! ~ - >>> math.frexp(-100) k& D$ c' q5 }2 e. D
- (-0.78125, 7)& r" z4 _3 T1 k3 k1 v/ G
- >>> math.frexp(100)
# B3 P/ }3 o- |' @ - (0.78125, 7)
复制代码
: w* H% X6 ~5 v3 p( tmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)# F; e, x! U) z4 i+ g# A: D$ x
- #对迭代器里的每个元素进行求和操作 n6 t' Y7 \& v. R5 ]3 u0 l3 c
- fsum(iterable)& i/ g6 d9 b- ?+ `! C/ |; a& G5 Q" _2 h
- Return an accurate floating point sum of values in the iterable.
# t1 h, R' V2 J* g; m. B, e- ~ - Assumes IEEE-754 floating point arithmetic.+ r7 f4 y" h# `# a
- >>> math.fsum([1,2,3,4])# v# V% Q/ y, l
- 10.02 C0 a5 _/ c1 g
- >>> math.fsum((1,2,3,4))# v' b. q5 Y2 f& I& S. D8 p$ }
- 10.0. ^$ d' J% q- i& I, O9 I
- >>> math.fsum((-1,-2,-3,-4))5 g+ {4 }9 x9 {2 Y- A4 Y4 j
- -10.0
: Q- e9 j9 X+ |2 [3 a - >>> math.fsum([-1,-2,-3,-4])- Q5 d' @1 @4 f# J0 I
- -10.0
复制代码 - s$ Y% V! ] s" E9 _
math.gcd(x,y) 返回x和y的最大公约数
' R+ m# E7 }5 N$ Q- R5 h- #返回x和y的最大公约数
1 y6 L0 x* N# M9 E! I4 @6 u - gcd(x, y) -> int
/ z; X O2 j2 N. m( T5 u - greatest common divisor of x and y
0 |1 P4 A; A4 ^# O; @$ Y% ^ - >>> math.gcd(8,6)
7 R: n, x8 N0 y M& S - 2
/ F0 Z4 C5 g8 h% H4 I, J2 z - >>> math.gcd(40,20)
, l+ {6 P% |3 M7 K& | - 20( R j) y$ J% ]* U3 F& ~& x: ?8 ]
- >>> math.gcd(8,12)
* x% f5 Q) h4 P2 h - 4
复制代码
5 n G2 C2 w9 j7 q' Xmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
. g. E2 V# M5 h6 u! n( d& k! R- #得到(x**2+y**2),平方的值0 d1 S' [! s/ s, V
- hypot(x, y) c& o/ e. [+ l0 Y
- Return the Euclidean distance, sqrt(x*x + y*y).
# i& j% d" z( z+ I, B- u - >>> math.hypot(3,4)
" c; [" O: u, g. `) z# N. X' R, b; u - 5.05 s a0 a6 S% m" c% g
- >>> math.hypot(6,8)! p7 V% c* y/ j: y0 ?
- 10.0
复制代码 ( l+ y- c# W1 [! @" r. h, V
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
; a s6 X" Q" H A- #如果x是不是无穷大的数字,则返回True,否则返回False
9 _) C" V2 ^/ e9 h0 t - isfinite(x) -> bool
. J/ s% Z: b' O3 } - Return True if x is neither an infinity nor a NaN, and False otherwise.0 ^2 R- B4 _* H0 |& g
- >>> math.isfinite(100)- U/ {. F3 A& C1 z a3 x
- True
0 h, ^9 L& n6 f1 P - >>> math.isfinite(0)
* s% J2 h# p8 R2 e) V6 w - True
7 [" v0 R" U# p! ]) p; A5 E - >>> math.isfinite(0.1)6 m$ r9 J4 N i0 } H2 e
- True
* Z" J7 P) @5 N - >>> math.isfinite("a")$ b! B H* j4 R% X" G0 O1 h. F
- >>> math.isfinite(0.0001); L) I8 _; I$ U/ g8 Y
- True
复制代码 2 O5 D, r3 ? h+ K& P1 M
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
" S! v& i/ B6 b0 {- #如果x是正无穷大或负无穷大,则返回True,否则返回False
# M" Y, f7 v: \7 e. L - isinf(x) -> bool" M( _- D- K) B' x
- Return True if x is a positive or negative infinity, and False otherwise.& d+ n2 d8 Z/ W; b! I
- >>> math.isinf(234)
# x( I# K) h9 P* x - False
3 K, r( ?! y3 d$ {. @; ~ - >>> math.isinf(0.1). G T0 L2 a- m0 M1 H
- False
复制代码 ( |: W+ Y: Z A8 b( B6 w; G
math.isnan(x) 如果x不是数字True,否则返回False) I$ ?: v+ a# z8 v: o
- #如果x不是数字True,否则返回False
' T* V+ y: c0 U/ H) C& E8 A - isnan(x) -> bool& K+ E: w" V) a! j
- Return True if x is a NaN (not a number), and False otherwise.
, h: d2 {) c9 w5 u - >>> math.isnan(23)
! w! U8 {6 m$ y8 t - False E7 F c8 }: ^' e( P; I1 ~' o9 H
- >>> math.isnan(0.01)
9 |- i9 V K8 L/ `& N1 R - False
复制代码 ; J5 T, Q, [. g- g6 D3 P
math.ldexp(x,i) 返回x*(2**i)的值
3 X- W" O( T5 x) E9 [- #返回x*(2**i)的值3 ]' a; ?% p7 ?0 Q5 v) `
- ldexp(x, i)6 P$ F+ i6 l, B/ O8 H6 Z7 k) j
- Return x * (2**i).
. @0 M( N- f" e# } - >>> math.ldexp(5,5)
* k5 j$ \+ b9 {: @8 x2 G+ M. t - 160.0" S3 a+ B% T0 B, \7 S/ ~
- >>> math.ldexp(3,5)9 e1 y# {0 Z( o# L
- 96.0
复制代码 " O5 |% ~% N2 V
math.log10(x) 返回x的以10为底的对数9 t, @1 e+ w0 P, Q* ~ J
- #返回x的以10为底的对数
" M( o2 C' h" r. Y - log10(x); ^9 ^# g* Q! {' H# D% L
- Return the base 10 logarithm of x.9 ?/ |3 p/ v( M1 n* I
- >>> math.log10(10)
: G* ]" e7 c. U" k" x7 { - 1.0) v6 I+ a9 g Q) y
- >>> math.log10(100)
, |2 k& p3 B, P1 q r* Z - 2.0
* R& i% P X' z5 s! Z( J - #即10的1.3次方的结果为20* D+ a' Y* ]: y
- >>> math.log10(20)
! D1 E! S6 M. S4 n - 1.3010299956639813
复制代码
' p( ]6 n2 q) g5 l: n/ }math.log1p(x) 返回x+1的自然对数(基数为e)的值5 i) X$ p/ e: b/ T
- #返回x+1的自然对数(基数为e)的值
8 P' r3 H8 A8 P. V - log1p(x)
9 K& h4 _* p% \, U8 a. o - Return the natural logarithm of 1+x (base e).! ^' \+ o6 x7 M% f4 C
- The result is computed in a way which is accurate for x near zero.
* Z) ^' s2 Y" Y1 m - >>> math.log(10): @7 \$ ?6 d% j; K7 }& }' @. K! t
- 2.302585092994046% S- a) g" W* c% l( S4 v6 [$ ~5 V
- >>> math.log1p(10)
- A& }5 U. }! l* G; s - 2.3978952727983707 t% v$ t/ S/ _1 s
- >>> math.log(11)
7 f0 V* b3 Y+ ?% E+ O& } - 2.3978952727983707
复制代码 ( W9 X5 D& d$ q; i4 X/ ~: \" r
math.log2(x) 返回x的基2对数
+ ~$ A a& m# G3 a3 V) U- #返回x的基2对数$ ~) v& \0 p9 L" w# T2 U) q
- log2(x)
) m0 d* {+ s; t; g! u, R( j; h3 f - Return the base 2 logarithm of x.
4 _8 Z4 u* j0 W( v" @ - >>> math.log2(32): r+ o9 N& u0 N6 n( w' D
- 5.0. W6 I% U' C* P* v e
- >>> math.log2(20); h& q( @& x' U
- 4.321928094887363$ l E5 Z( ]. e) O2 p& s
- >>> math.log2(16)- X! {, O2 P0 f9 |( G) o% I8 d3 w
- 4.0
复制代码
5 y! u" O/ F3 l1 b) J/ Kmath.modf(x) 返回由x的小数部分和整数部分组成的元组
* } {+ T1 \- L! R: z- #返回由x的小数部分和整数部分组成的元组" y3 I' D) N! l+ Q/ h* R+ x: n
- modf(x)" x1 n' W) _5 ^; F) I
- Return the fractional and integer parts of x. Both results carry the sign
4 h- d4 ^' m r) j, q' r - of x and are floats.. `4 o7 p/ t* B% q$ t: p2 S
- >>> math.modf(math.pi)
1 V+ U- p. @% e I; f - (0.14159265358979312, 3.0)
1 u* _4 D3 I2 U T4 Q1 q0 O - >>> math.modf(12.34)
# `. M ]' w: c. z - (0.33999999999999986, 12.0)
复制代码
8 S* H: ?( z, o4 ^2 V7 C! C3 tmath.sqrt(x) 求x的平方根
& P- H: M1 c5 X2 w' S, L% V9 O/ r. d- #求x的平方根
+ C b" d% Q0 e - sqrt(x)* J* I8 d# m- {+ C* D1 `3 B
- Return the square root of x. u& U1 s S0 G- f
- >>> math.sqrt(100)& y/ e3 b. K. D2 I
- 10.0$ V# f/ ^2 g* i- R! j4 ~5 j7 P
- >>> math.sqrt(16)! Q% c8 D. A& \0 m- m: b, p
- 4.0" D1 U/ C/ L! ]* i
- >>> math.sqrt(20)
5 I% k7 K" @1 j& } - 4.47213595499958
复制代码 # ^: _' l3 K1 l7 j. |3 b
math.trunc(x) 返回x的整数部分- #返回x的整数部分- @! w: b# {5 e K4 p* m
- trunc(x:Real) -> Integral
b9 d: {0 z: m - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
8 p! H4 i0 \" N; H - >>> math.trunc(6.789)) F9 M+ j7 ]" P/ Z7 E
- 6
9 c0 Q. K- k" B$ S& x" W - >>> math.trunc(math.pi)" u6 a9 ? A. R! d4 l8 B
- 38 b- r# Z+ M8 J
- >>> math.trunc(2.567). w4 V, N* n' A8 ^$ t; K
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|