马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" O+ V0 V% p# N* N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 O" m3 v) }! s% @1 \; D
! ?& W7 f' o1 Z' G- Z$ `& Y: D方法1:
9 m L M1 N4 I% b8 q" t- q% E- >>> import math
" `% y& Y8 {! e, \ - >>> math.sqrt(9)
8 Y+ @* N% ]. W6 g3 Z4 J7 t8 s- q - 3.0
复制代码 方法2:4 H$ k) i" {# Y9 \( |
- >>> from math import sqrt" y! _- X# Y8 @
- >>> sqrt(9)
. S: j2 n1 Y$ {* u - 3.0
复制代码 4 E+ G9 D- E X7 h3 S
# s2 P7 o! n7 V2 m( [+ i
math.e 表示一个常量
% ~; V$ v/ h/ M7 @3 F1 l! T- #表示一个常量& @) t0 g' d8 s# ^# W- [7 V# m
- >>> math.e
, Z& H- F/ o7 U' J/ a) d& v2 k - 2.718281828459045
复制代码
3 l: k6 Q4 E, |math.pi 数字常量,圆周率8 P/ I0 g$ A8 Z$ ]8 t6 P6 n3 q
- #数字常量,圆周率# C% L5 \/ R1 Z: h
- >>> print(math.pi)
3 S- q6 K3 `0 x - 3.141592653589793
复制代码 ; \1 s( K# G# F* B
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x5 S5 J# R7 o& v3 E# z
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
% a& I3 c# k3 k/ n4 S - ceil(x)( h5 Q g9 X1 K0 Z/ L! d+ v; z7 X
- Return the ceiling of x as an int.
1 ^& g) `7 n# j - This is the smallest integral value >= x.* O* n6 M- | r5 a/ s* K- }; k
% z% {# G! r% w0 W) G |- >>> math.ceil(4.01)
7 Q% ?6 ~9 D9 j8 L6 {! [ - 5
0 P" W9 G% z& r1 I6 Z - >>> math.ceil(4.99)
D E. i. B L7 t% { - 5
8 W/ ^0 z. I5 J: _ L/ J - >>> math.ceil(-3.99)% d$ K8 x9 z, c# x- I2 S; G6 N8 X2 d: L
- -3
% Z) q$ \9 A" L/ B; n - >>> math.ceil(-3.01)9 _; C6 z6 Z; n! f, \ m
- -3
复制代码 + \+ k% }; J5 w& q
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ d$ f4 M0 B) o- e- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ w$ G! P2 i' F! i0 _; x - floor(x)
1 E- H+ A" J. W1 v- i9 F - Return the floor of x as an int.
3 m6 \. k; q* ~% L4 ] J/ c - This is the largest integral value <= x.8 c0 `' g) r5 S. n& {* a" U
- >>> math.floor(4.1)
+ x' p- n# S, d" U/ Z/ P - 4
( a) f- L* ], w - >>> math.floor(4.999)
8 s7 V& V* L0 r8 r - 4
" r& g5 f; P& k8 q% P - >>> math.floor(-4.999)
: p* ~0 x% b) |; G7 D0 {8 g: q - -59 }+ f( T- _+ {6 l6 N( r
- >>> math.floor(-4.01)
3 N5 i" a' E [8 `9 U5 ` - -5
复制代码 ; ?! q2 O# O) Y8 b
math.pow(x,y) 返回x的y次方,即x**y1 W0 L* B4 W ^0 @& C" x) A4 r3 _0 k+ \
- #返回x的y次方,即x**y i2 n5 \" P8 L
- pow(x, y)
5 D, ]4 B, X1 i" m' H - Return x**y (x to the power of y)./ L! O' ?# Q# Y6 Y
- >>> math.pow(3,4)
/ K% P9 h3 L% v6 s - 81.0& u f$ N) _! y
- >>> + S; H4 D9 s9 s# h
- >>> math.pow(2,7)! b) o& ?* c8 [8 R8 |5 t5 m
- 128.0
复制代码 8 ]6 F3 h, X3 M2 G
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( K4 D7 h( t9 e3 \* @/ G- Q2 A- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' ?9 S) @+ |# y) A; T3 w - log(x[, base])% j+ E( X6 H* c
- Return the logarithm of x to the given base., H- V/ [: V5 D) P5 Z" z
- If the base not specified, returns the natural logarithm (base e) of x.
- i) y' R8 y# _ b: w7 \+ S - >>> math.log(10)* F7 S7 P* N1 U- R) P% g- t q
- 2.302585092994046
% ]* D, g) c$ ?# D5 A5 ~2 o: @ - >>> math.log(11)
8 W9 R' \6 t1 `0 i; x' C - 2.3978952727983707/ H$ X# |, @; | _, {3 X, |
- >>> math.log(20)
D# T0 d$ T6 J9 W: I% }$ R - 2.995732273553991
复制代码
( ]9 v3 j9 ~" cmath.sin(x) 求x(x为弧度)的正弦值$ n: j a5 g5 M$ |
- #求x(x为弧度)的正弦值- _5 d" ~7 L1 K. s' R, n: z
- sin(x)( Q6 X+ G2 a# M) R F$ N Q* O8 `+ {
- Return the sine of x (measured in radians).
' O( Q1 A5 \9 q - >>> math.sin(math.pi/4)
0 i7 n- _* y" |/ m5 @% p6 R! A: h - 0.7071067811865475
6 i4 U, y' J* k6 z- h, m& z - >>> math.sin(math.pi/2)
3 i* |- Y% Q0 z* E, H* } - 1.0& M1 p" S; m+ h5 E0 l
- >>> math.sin(math.pi/3)
+ X# {4 ?$ F: D# q& I% v0 ^1 S - 0.8660254037844386
复制代码
+ I0 G7 Y9 D; S( Gmath.cos(x) 求x的余弦,x必须是弧度
# Q* m8 a; `* i% U" N$ y& V- #求x的余弦,x必须是弧度
8 H' C: V* M! W, T" b% o Q - cos(x)) g6 @4 W" l" M; |
- Return the cosine of x (measured in radians).
a) X/ _, F" c7 p# C- T. c6 p - #math.pi/4表示弧度,转换成角度为45度' \8 o/ I5 S& X2 c
- >>> math.cos(math.pi/4)
# [) l9 r' b$ @, }5 s- H - 0.7071067811865476% c! P% h F2 G" ?1 @' M' @
- math.pi/3表示弧度,转换成角度为60度
" ?6 h# B o( K8 g8 h6 r3 c2 d - >>> math.cos(math.pi/3)
+ d4 \% a2 M F2 n8 [2 h0 u" S+ P - 0.5000000000000001
3 O, l4 i6 } l9 z' o' W' Y. R - math.pi/6表示弧度,转换成角度为30度
/ S; v& N9 v! j6 Z/ Q# l - >>> math.cos(math.pi/6); \3 C8 h2 z- i% @8 l3 i
- 0.8660254037844387
复制代码 6 ^' V+ ?2 A: c3 g. V: T& I
math.tan(x) 返回x(x为弧度)的正切值
! t- d* d& }) B; j, a( |- #返回x(x为弧度)的正切值2 A" k S/ h# t! m2 R; D+ u2 K
- tan(x)
) @2 T# ` ?8 v( e - Return the tangent of x (measured in radians).5 e1 v T9 C9 A: `+ Y
- >>> math.tan(math.pi/4)- K& J* ]: ^: I3 G, w% R
- 0.99999999999999997 |# L" V. c( d2 Z5 E
- >>> math.tan(math.pi/6)
! \% z9 r: O7 H: B F8 T" [6 y D - 0.57735026918962570 i$ l9 Z3 D( c1 |6 q
- >>> math.tan(math.pi/3)2 y& p% D7 {7 y/ `$ e+ N" n ], r
- 1.7320508075688767
复制代码 ) z! C& E/ T" H( S0 q
math.degrees(x) 把x从弧度转换成角度6 o4 ^) m+ U) X" s7 B: a
- #把x从弧度转换成角度
4 ^3 t2 c2 B4 h% B; K. W - degrees(x)
W. k" F" N/ j# }6 O% n5 I - Convert angle x from radians to degrees.
9 d$ M+ @* P, Z9 H
, e/ l8 r% D ^ z# H( P# X# S- >>> math.degrees(math.pi/4)
7 ]( v8 {% M8 X; q9 \ - 45.0: p7 E! c5 e4 _( l5 n9 [: {7 K
- >>> math.degrees(math.pi)5 ^/ [3 a: S `. ~" M
- 180.04 b. g7 S4 M8 {: c# `
- >>> math.degrees(math.pi/6)
/ V) p) o6 s% a# M3 t - 29.999999999999996! D% r; z0 u* A* Y
- >>> math.degrees(math.pi/3)$ U9 c0 N/ A; w) ^
- 59.99999999999999
复制代码 7 B7 O7 Z. {/ Y7 c4 | k
math.radians(x) 把角度x转换成弧度
" F0 m0 m3 `! q0 A6 z- #把角度x转换成弧度
, {+ K0 S. j# u, ?* D: ] - radians(x)
! N3 t- M+ u# x: u - Convert angle x from degrees to radians.
- S4 f. d! ^! x - >>> math.radians(45)) t) u/ g% Q$ K% X5 }. j2 {4 t2 {
- 0.7853981633974483: U6 F2 p0 j8 b5 o% c' H
- >>> math.radians(60)" y: ^( H+ j* @( g) w9 v4 i5 }
- 1.0471975511965976
复制代码 ! { [0 s x: A+ T7 s6 i, a; X! x3 w
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
: z0 T& p* a6 F( J$ C3 y- #把y的正负号加到x前面,可以使用0
$ m1 H* `: S: g: U0 p6 @ - copysign(x, y)
/ l: F& t. u* |9 t2 Z) \ - Return a float with the magnitude (absolute value) of x but the sign
/ k; T+ G5 u3 _: P9 Z - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
5 t% q9 M7 r) K, b. L" ` - returns -1.0.' F7 }' _5 U' Z7 D5 j9 |1 c
. E) J' N2 ^) m$ h( B- >>> math.copysign(2,3)
" B8 H$ U' w/ p) p - 2.0, b3 t/ h9 A- e# U. E
- >>> math.copysign(2,-3)5 o( q8 ] }0 h6 ?. n) p
- -2.0
5 n/ m1 ]8 _+ P* H - >>> math.copysign(3,8)
6 M' f" w7 `: R - 3.0 a0 V7 J# }6 P
- >>> math.copysign(3,-8)
( K8 G# g8 _5 N" F# E2 ~; ]4 C - -3.0
复制代码
+ B# L S: g+ C9 e ?7 Wmath.exp(x) 返回math.e,也就是2.71828的x次方) U4 p3 c9 h5 S
- #返回math.e,也就是2.71828的x次方
5 c7 W7 _! o ?* i) m; r: G - exp(x)3 T7 L: v6 m! b. h V5 C: x" ~
- Return e raised to the power of x.) d' ~; p% H2 x3 q! _9 I8 u
- ! S1 s. W6 A/ ?; ]& S" _4 x
- >>> math.exp(1)( L+ G2 j& I1 u) q! T
- 2.7182818284590459 k5 _* M& h( L/ _
- >>> math.exp(2)
1 }: N4 k( H9 ~7 I2 a' Y& T - 7.38905609893065 ?! {' w8 _2 B- X1 K
- >>> math.exp(3)
* g! i. n2 j/ f- W6 a* x - 20.085536923187668
复制代码
& G$ C: I; Z( U/ z! U |4 Wmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减13 l3 a) n P3 ~% B+ `. w
- #返回math.e的x(其值为2.71828)次方的值减1. |( S5 O! u3 e
- expm1(x)
' a c. d' f" z8 F. {4 `, B3 p2 ] - Return exp(x)-1.
/ F3 U% f& H5 C# \' D! J - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 `. j: u7 f2 r& v - 9 X5 H. x6 X- n! M1 q7 s% }
- >>> math.expm1(1)9 k+ [$ k* h- W" ^" ? g0 g
- 1.718281828459045
- R, Z- x+ o. U+ S- I - >>> math.expm1(2)
7 r2 v1 Q: }/ ?8 q, a2 M - 6.38905609893065+ N9 y/ e N b2 X4 i" D
- >>> math.expm1(3)
, R# ^* Y4 K3 U, @) r - 19.085536923187668
复制代码
+ `$ Q/ ~$ I% K* i0 i2 s8 Kmath.fabs(x) 返回x的绝对值: Z- o1 Y8 _$ Q% z6 k
- #返回x的绝对值; p* F! c' U$ h3 Q7 L/ U7 Z
- fabs(x)
1 _, \8 i$ d, M& b - Return the absolute value of the float x.1 k4 ^) k. ?" m
- {4 l0 P( D7 z0 ^9 y) j
- >>> math.fabs(-0.003)8 m$ w; W8 u0 l1 |$ E5 K
- 0.003
4 E; k% j" N# R- f - >>> math.fabs(-110), D. ]' A; S/ W' Q n& g
- 110.0
6 J" N8 l$ s% @' X9 n3 W0 ~ - >>> math.fabs(100)
- L% C2 l, T5 q g - 100.0
复制代码
. \3 ?5 b) M' W) L L& C1 ]math.factorial(x) 取x的阶乘的值% J+ _4 D+ x7 @+ C( h2 M4 r
- #取x的阶乘的值
# R1 F3 D: p; q& r. ^3 S' a) j - factorial(x) -> Integral3 t; @& ~+ f8 D. c1 r$ F9 s6 I
- Find x!. Raise a ValueError if x is negative or non-integral.5 j; O/ }2 P" u( p3 ^- S
- >>> math.factorial(1)% h- |& O! t( u; h& N
- 10 _2 E% t* T h5 ~- q" X6 `
- >>> math.factorial(2)
) T* U: b# p9 g6 T8 Y9 S# I# P8 C - 2
4 X# G9 y9 P) T* M - >>> math.factorial(3), _, ]& X3 k/ R: c
- 6
! Y) U; |9 {' m# f0 F9 c! | - >>> math.factorial(5). O+ C1 V# L$ n3 Y
- 120
1 P$ Q K+ F( I4 H! U% R+ g. R' P- R - >>> math.factorial(10)' U* N9 ?8 x) ]$ p2 r: a( M
- 3628800
复制代码
+ \' q( f/ R5 C4 v" Gmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数4 d8 I5 ^7 t* U+ A- K; o# I
- #得到x/y的余数,其值是一个浮点数
[# j/ _7 Q" i( k' v5 y/ i; {2 e - fmod(x, y), U3 v; k3 h8 {; H
- Return fmod(x, y), according to platform C. x % y may differ.
5 q) {% L8 \8 i' t. ~ - >>> math.fmod(20,3)
7 v0 t7 r4 U u - 2.0
& @. a0 P# i6 p3 }4 n Y' T - >>> math.fmod(20,7)" q7 s; [4 s( M8 c7 v% P1 X
- 6.0
复制代码
6 \ _5 S. I! E3 Z7 ~$ jmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ j5 F, ^& d& r8 B( j( p2 J
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, u7 Y9 o: |/ I* l( i: V
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' ] _8 M; M; q% k
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' K& k8 Y# B" ^+ t
- frexp(x)
+ w+ h$ u/ m+ z; A5 a' L' B - Return the mantissa and exponent of x, as pair (m, e).
& x9 ? n) k8 `: G1 Q. r" r - m is a float and e is an int, such that x = m * 2.**e.# \6 a" z( @9 @4 E8 v
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.# @: F) y, |) K. W
- >>> math.frexp(10)9 w; X; r1 H. G
- (0.625, 4)1 g' Y; z. x- O4 F3 L
- >>> math.frexp(75)& Z0 i. w! D3 W, }, V
- (0.5859375, 7)
0 O' S T3 g" [0 r( u' V5 n1 r% ^ - >>> math.frexp(-40)
0 E* f/ ~0 M. Q/ C% g( R - (-0.625, 6)) I% ?9 z. d- Y
- >>> math.frexp(-100)
1 e2 L$ a: h2 {. Z - (-0.78125, 7)
/ |2 Q, ]/ S" L0 l/ ]( B! I - >>> math.frexp(100)- Z; Z: o- f5 d) |$ m8 J2 E. `5 u
- (0.78125, 7)
复制代码 , d$ {' Q+ U! i7 ~
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)* z, S6 t' `4 E6 M6 y
- #对迭代器里的每个元素进行求和操作+ V+ x8 Y1 N1 i k
- fsum(iterable)* v. p% F0 j. A. s$ O
- Return an accurate floating point sum of values in the iterable.
" a. {2 J) ^ g- f! b2 w: ]3 ~ - Assumes IEEE-754 floating point arithmetic.7 _7 e9 g2 _/ T! x
- >>> math.fsum([1,2,3,4])
N! | _ x3 y9 O - 10.0) ~% f, ~& n/ ]
- >>> math.fsum((1,2,3,4))$ |$ k1 T) J# R: X
- 10.0' B0 @- v+ Z/ F2 |( J% E7 k
- >>> math.fsum((-1,-2,-3,-4))
5 c6 x# i* K3 D5 P+ w - -10.0
0 ]! _( S. p& v7 x - >>> math.fsum([-1,-2,-3,-4]), J+ u8 V) S8 r- b% C7 Z( _! C
- -10.0
复制代码 1 `0 ~3 a: v0 O% L# N4 o5 e8 b6 V$ F
math.gcd(x,y) 返回x和y的最大公约数& r( n: `8 }* Z8 Y) l" O
- #返回x和y的最大公约数
S9 ~- m! ?5 H - gcd(x, y) -> int8 |( G l$ @; n5 u- G, \
- greatest common divisor of x and y
. ]8 l# \3 ]* t, }- g. x - >>> math.gcd(8,6)
! y( o$ m t6 Y - 2
) a! e" ]3 |- T* w# d3 g0 W - >>> math.gcd(40,20)
h" c& O k6 K - 20 {% l( {' y O2 d, @
- >>> math.gcd(8,12)4 N! v8 `; {; s7 w2 a
- 4
复制代码
& q) r) ]8 v: B7 pmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False0 v3 ]$ I( F. H4 E2 i
- #得到(x**2+y**2),平方的值% y% H1 I7 ]5 [+ ~
- hypot(x, y)
$ u% X" q$ K5 {# `! V - Return the Euclidean distance, sqrt(x*x + y*y)./ e$ K4 `) Q- |. M/ t+ Y r8 M
- >>> math.hypot(3,4)8 k9 ~9 E) b: H @
- 5.0
) ^. z) B1 V; f5 M - >>> math.hypot(6,8), }( [, m# r9 J4 _9 U( ~3 f
- 10.0
复制代码 {7 `( x6 K/ E9 c2 U+ x) v, z) B
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
5 ^! B/ `* n. Y# H- z' u, _) g- #如果x是不是无穷大的数字,则返回True,否则返回False. @0 A7 B$ j- K5 r5 K7 b6 u
- isfinite(x) -> bool
# D+ `2 {0 U6 G' z5 E - Return True if x is neither an infinity nor a NaN, and False otherwise.; G4 _$ j8 F: f1 u. G& w
- >>> math.isfinite(100)
+ A* w7 C5 U7 |( @3 J' ~ - True m* e: |& l2 `' _9 B2 a1 t
- >>> math.isfinite(0)
5 D, q; y6 t1 l# o& l( c - True
, U3 D1 G! M) { - >>> math.isfinite(0.1)& a+ t! x' l! q u, c9 G/ Q: u
- True' j" U% G5 k2 X$ N v6 }
- >>> math.isfinite("a")9 @0 f* g0 E. I, i6 i# c( q
- >>> math.isfinite(0.0001)
; {/ Y9 i6 d, [6 N& G$ [8 S1 W% E" I - True
复制代码
0 I7 P' Z0 q% {math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
, n2 D2 |6 o( l! z3 Q6 c1 q- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ C& k, ?4 ^6 D7 W- P$ u! Q - isinf(x) -> bool
) c5 P Y) x' S8 j- s - Return True if x is a positive or negative infinity, and False otherwise.5 q) W, d7 K: e8 S6 z& c/ e# I7 n
- >>> math.isinf(234)
$ V: h A+ h1 v2 r5 W - False
8 \6 H( @% K& s4 @ - >>> math.isinf(0.1)
6 ^7 @% e1 d! y- O( L# w; M. @ - False
复制代码
+ c# i( t. I1 p5 k1 ]# k: E2 `math.isnan(x) 如果x不是数字True,否则返回False0 N: ?7 z; C& c' U: S: V/ ^* j
- #如果x不是数字True,否则返回False
9 V( W. O; T K. e - isnan(x) -> bool
& d3 g8 ^, ?9 |# I3 S) { - Return True if x is a NaN (not a number), and False otherwise.. ~: s) g1 D& {$ F# y1 ~, g! [
- >>> math.isnan(23)
, ]; a, K7 L; C5 W3 d. z8 O3 [ - False/ h' a4 N7 X6 L3 V* y4 P
- >>> math.isnan(0.01)# p; D/ z0 B2 B8 [ V
- False
复制代码 6 ]7 J5 k X: q7 s) ?- y
math.ldexp(x,i) 返回x*(2**i)的值
5 F" C: m# t) Z0 G' c& R9 g- #返回x*(2**i)的值& N1 t ~8 j1 j/ T: S2 `+ |
- ldexp(x, i)% s. S' e) q0 w
- Return x * (2**i).; ~+ t/ ~ {, @7 k6 a( G
- >>> math.ldexp(5,5)
/ M; c3 E- v7 X. B: L' {1 w" i - 160.0
! h9 w+ z. M, n - >>> math.ldexp(3,5)" W9 m3 }, m' ?4 z5 S& T" h
- 96.0
复制代码
1 _6 p5 B( C6 V3 a5 t( a+ jmath.log10(x) 返回x的以10为底的对数: r* ^8 w7 \/ t: A, A
- #返回x的以10为底的对数( } I- e4 m( c. X! Z) V8 r
- log10(x)
5 w1 o- {* \( U: m; q - Return the base 10 logarithm of x.5 V- _! L% ]/ W1 I
- >>> math.log10(10)
! F$ [9 T. h. i4 B5 u% C# x" h8 | - 1.0! ^1 K: p) @0 J: ~
- >>> math.log10(100)
8 U8 d; H# D) H6 T; ]- c - 2.0
: i% D3 Y/ ~/ y) o9 p" e- H; e - #即10的1.3次方的结果为20- c3 k2 y1 h% q* n* ~, C
- >>> math.log10(20)# F* S3 `4 z& O7 C! @, M
- 1.3010299956639813
复制代码
& z: h$ W" F% ]5 [math.log1p(x) 返回x+1的自然对数(基数为e)的值
, f) \ a# H7 z- #返回x+1的自然对数(基数为e)的值
: D$ z: T4 y# K# v/ W3 Z7 h - log1p(x)
8 n2 B9 e, F5 A& W+ I' ` - Return the natural logarithm of 1+x (base e).
( p) {( h1 \' d - The result is computed in a way which is accurate for x near zero.6 s, ^5 b4 F, I! _9 n# ~$ ?
- >>> math.log(10)3 Q- c n q5 y8 s6 E6 H3 |/ N# L
- 2.302585092994046
r `" d& H7 b5 v ^ w1 W - >>> math.log1p(10)
8 T1 p$ ^ A/ y* R& X0 q9 P - 2.3978952727983707) Q+ G4 c, L: s+ ]5 X8 U; Y7 ?
- >>> math.log(11)
' W5 B# e6 x7 Y* W, s6 G! o - 2.3978952727983707
复制代码
5 r+ b% ^2 Z! m l/ ] h1 \# [. Lmath.log2(x) 返回x的基2对数( c- J% n5 d+ A! |% x( T
- #返回x的基2对数
6 \6 P- Q+ @; \1 u) U* F - log2(x)) ], R; `7 g) a
- Return the base 2 logarithm of x.- \& Z0 A S/ N5 ^( I
- >>> math.log2(32)
7 o6 K8 W8 z6 s& H8 l - 5.0
' m; \: T! G& e. p. D - >>> math.log2(20)6 a p' P. c6 G& h
- 4.321928094887363
0 ]0 n z5 I v* e+ E+ {5 c4 u - >>> math.log2(16)7 \2 ~8 R- O3 Z7 ~* Y' V3 Z
- 4.0
复制代码
0 _. ]' v" {% V2 ^math.modf(x) 返回由x的小数部分和整数部分组成的元组! P5 C# [! j- ~# Q& Q
- #返回由x的小数部分和整数部分组成的元组! a* m Z$ q6 e: A9 X0 U
- modf(x)- |$ Z' y) P0 y3 T2 o
- Return the fractional and integer parts of x. Both results carry the sign- G) [% S7 D" H% ?; H% V
- of x and are floats.
/ ]( O+ O+ ?8 x. O2 ?* b9 m7 U* k - >>> math.modf(math.pi)$ k3 |5 h6 i2 U7 `4 x
- (0.14159265358979312, 3.0)
) o9 o% d9 r9 x; a9 M - >>> math.modf(12.34)
. M* F4 d3 Z3 V) f5 G8 u - (0.33999999999999986, 12.0)
复制代码 9 ~3 ?8 p2 n: Y, B
math.sqrt(x) 求x的平方根
6 u3 |6 R2 W: h- #求x的平方根7 x' N0 ?, S6 ~' D9 `! i5 @
- sqrt(x)
- U% c7 k, y" g2 [ - Return the square root of x.
, Q! L! K+ y% z - >>> math.sqrt(100)7 \9 i' x) U9 w0 }* K5 `
- 10.0/ ?# i0 |: ^: T0 s) l9 Y( Z
- >>> math.sqrt(16)
* }! Y/ D) Q8 y9 t - 4.0$ p# n/ G4 p B2 D* h( y
- >>> math.sqrt(20)
. \# @2 n& H! b& [* ^ - 4.47213595499958
复制代码
- w- v& `; ~8 U) z/ e- y4 tmath.trunc(x) 返回x的整数部分- #返回x的整数部分7 I4 U/ Z% B6 ~: \; t+ I
- trunc(x:Real) -> Integral7 B; q4 @* W" l- A# F' u
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
0 L/ f2 x! i. ?) S, `8 X1 P - >>> math.trunc(6.789)/ R7 l3 t7 c, j! H
- 6
4 L* A2 Z- K5 k c8 q - >>> math.trunc(math.pi)
& G3 [' N" ]% _* H# K - 3
: B; |8 M% E: E" C6 ? - >>> math.trunc(2.567)
% g7 k* v2 p+ a# ?* x4 ]' ~ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|