马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ G! \: i' N5 p3 y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
! N" W( y; C; W( @( s- i) L* X8 u# k. n; n; H
方法1:
8 N" \% `% l8 B& v% N- x: s- >>> import math
+ y) T0 v9 r9 B - >>> math.sqrt(9)
8 b1 z1 g1 C5 t. C# d - 3.0
复制代码 方法2:! _* W" X$ Z/ M$ n
- >>> from math import sqrt$ w+ l' _" \# b7 e/ O& w
- >>> sqrt(9)
% b/ @0 A" E) U/ I3 o9 a8 M - 3.0
复制代码 / ~1 Z( w: f0 }- s( k( ~. n# t7 T3 y
' U' z6 g" E& E- S1 {, j) W
math.e 表示一个常量
/ P7 e( s6 L: v# {+ o3 Y- #表示一个常量
$ I$ F9 D4 y6 O" L7 y. x; t - >>> math.e/ @( z+ O9 o( k3 L$ U
- 2.718281828459045
复制代码 $ I/ v+ v& }+ @" n- m: `. E5 z6 j$ I
math.pi 数字常量,圆周率6 r* [5 \6 i; Y
- #数字常量,圆周率- p* t9 a% g8 V" \% T1 a# @
- >>> print(math.pi)
+ i6 I) H" o. Z' K* m# V - 3.141592653589793
复制代码 1 {8 s- _) r$ s$ R2 |) c5 \7 r: ], m# P
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 X4 s9 q+ Q2 d) i+ r- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
Y5 ?3 z% f' E, c0 O - ceil(x)" J+ p% M; R% }! p3 s# M- Y" k
- Return the ceiling of x as an int.$ Y8 r" a. ^) l( m: Z% B7 O
- This is the smallest integral value >= x. f& a, X8 i; B3 n
: _+ s0 H3 ?% V; W- E+ v' F( m- >>> math.ceil(4.01)4 }( q( u7 `4 J* s/ f0 M B
- 5% g' `$ `- Y1 [! p6 h, ~) r
- >>> math.ceil(4.99)4 ?8 K, w, w$ ^. ]6 `6 t! d0 o# u8 v
- 5# {( p9 `8 w. \
- >>> math.ceil(-3.99)
: F" J9 e. b9 A1 X4 s$ i - -3
' E4 i N9 y0 L7 z - >>> math.ceil(-3.01)9 ^( A9 F4 h* p' h( c, D1 p6 o
- -3
复制代码
) o# d3 {! X# H" d! O) S- D- E; rmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' |2 R. X1 K7 L" f- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ T @& s5 P O# r1 Z
- floor(x)& `) T3 X1 T' [) x+ \+ Q3 b0 ^
- Return the floor of x as an int.
9 i( S$ ?4 n$ C- ?/ w! P) E - This is the largest integral value <= x.# H! q }# G4 C$ \: p2 |" ]
- >>> math.floor(4.1)8 `7 `- l' ?" N o
- 4, }1 O- }* e3 q6 {, W$ |% t/ q& D! t
- >>> math.floor(4.999)
+ Y$ a- q0 n% s+ `4 u7 X+ r - 43 U% T0 ^8 {9 K& a" Y0 p
- >>> math.floor(-4.999), v7 s1 V) q! w9 V
- -5
. S% s( N! C+ V' ^; `, E - >>> math.floor(-4.01)" _; d' H) n/ p( `6 I
- -5
复制代码
* f9 a9 k o4 ` Qmath.pow(x,y) 返回x的y次方,即x**y
, I# _7 a& m0 q. d" G& Z& ?- #返回x的y次方,即x**y
* p5 I p/ q/ u& b* B - pow(x, y)
) D+ k) w; v# U% t3 T - Return x**y (x to the power of y).
0 i& J/ c2 O- X+ x7 L0 N H. K - >>> math.pow(3,4)' h# \6 I8 ]( {5 |! u% H. r- w
- 81.0' W; m( i* w6 g0 ~# J# w7 Z7 s
- >>>
/ g( d: o, B' N% _ | - >>> math.pow(2,7)
* i; `" U/ ]" e; [% y - 128.0
复制代码 & F1 {4 P2 x1 @# a* ?/ ?' C
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 M& m7 h; P: H- s- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* r4 H4 f5 c: C; k' J( ^
- log(x[, base])
8 K: P. |& `. R: ^2 y- @9 v - Return the logarithm of x to the given base.
6 ~2 F) R' h/ L0 Q6 { - If the base not specified, returns the natural logarithm (base e) of x.. A/ m: H) P' q+ W9 o) K2 b
- >>> math.log(10)) W! y! u, ]# V$ `& p; \
- 2.3025850929940463 a! [& r( T. d/ J& r
- >>> math.log(11)- Y! `! h& f# a1 A1 c1 O2 W' i% {
- 2.3978952727983707; x7 T$ j+ r8 y7 ~
- >>> math.log(20)7 P* N1 N7 O% \8 r
- 2.995732273553991
复制代码
# J" v2 K9 D2 V% s# h Emath.sin(x) 求x(x为弧度)的正弦值
9 P' I3 Q) _" U1 B- #求x(x为弧度)的正弦值' x; d3 _ W" \( K+ w
- sin(x)
! W$ X2 ?$ I5 n% T4 _ - Return the sine of x (measured in radians).
' L& R, o/ R$ y - >>> math.sin(math.pi/4)
- z8 j! S$ ~& J6 k6 {+ h - 0.70710678118654758 S4 P# Y* M8 s* f5 {
- >>> math.sin(math.pi/2)5 G/ _8 @$ R2 t0 G! N2 N
- 1.0" [1 q. o+ N9 o1 }
- >>> math.sin(math.pi/3)9 E$ x& t5 K) M" Q( G8 S
- 0.8660254037844386
复制代码
& d1 C' |7 T$ c: B4 ~+ x! bmath.cos(x) 求x的余弦,x必须是弧度
4 q: Y9 @- a1 a+ Y- #求x的余弦,x必须是弧度
1 C) a3 o! y0 P - cos(x)& ~, h1 F4 f9 f9 w3 c
- Return the cosine of x (measured in radians).
! c3 b. n+ B5 z$ L - #math.pi/4表示弧度,转换成角度为45度8 l' f, {. [1 k2 `; h
- >>> math.cos(math.pi/4)
0 q+ `+ |0 S1 K# }# o - 0.70710678118654765 P9 A5 f; j% Q. `2 x# b
- math.pi/3表示弧度,转换成角度为60度" i6 s: ~8 t4 P& |+ Z
- >>> math.cos(math.pi/3)
; h% F4 r/ Z/ A/ w - 0.5000000000000001; r8 ~, z5 t" l
- math.pi/6表示弧度,转换成角度为30度* F1 C& S" U9 d- ~" F' J& k
- >>> math.cos(math.pi/6): b- c$ K( h, B3 D. D$ L
- 0.8660254037844387
复制代码
* @( I1 K' L; C1 w( U% Cmath.tan(x) 返回x(x为弧度)的正切值* d. m$ e& R. [" m% B
- #返回x(x为弧度)的正切值
( `, e$ f1 Z3 K0 _% B. P - tan(x)
" q8 J! k7 r; k6 D - Return the tangent of x (measured in radians).' Y" e3 P: u7 L( d0 k
- >>> math.tan(math.pi/4)
) d4 E8 |$ X0 s5 v& T% j4 g+ p - 0.9999999999999999
' A% J* ]( Y+ |# ]# v; O! x - >>> math.tan(math.pi/6)
- P# V: @ [( ]1 c- F! s - 0.5773502691896257
. s! j; M' _6 _+ p1 c% f4 W$ F! t - >>> math.tan(math.pi/3)4 h0 D9 Y' `* x) |) I
- 1.7320508075688767
复制代码 : K" O' _; w! a8 m5 |
math.degrees(x) 把x从弧度转换成角度6 Y |1 @1 y( ~2 k4 y
- #把x从弧度转换成角度
% o7 y( A1 E+ q - degrees(x)
( v8 n4 u: r: q6 m - Convert angle x from radians to degrees.7 o) ~# H, s1 H0 ~
; H) l/ K8 w" Y" H* O: D+ J- >>> math.degrees(math.pi/4)
! t$ m, }2 G5 P( n. F - 45.0
% J& n, d6 Q/ R- L7 I8 f - >>> math.degrees(math.pi) ^9 x8 \0 r! k
- 180.0& p2 A1 w+ w, l: t6 j
- >>> math.degrees(math.pi/6)
2 o3 w" h: g: W, W5 C& o9 Z: h - 29.999999999999996
- V$ c: S0 H: t4 g - >>> math.degrees(math.pi/3)
( |* K/ Y# ^1 n - 59.99999999999999
复制代码 # W9 p# i+ v8 I+ l8 [! |9 D, \6 Y# E
math.radians(x) 把角度x转换成弧度
! u: V) ^8 g$ Y' C& n' o, {+ k- #把角度x转换成弧度
. A* L) J0 ]. `9 d/ t, k - radians(x)
5 l5 N8 k' {1 h+ P4 h9 k8 a* S - Convert angle x from degrees to radians.* ~ C0 i, D1 B ]2 d" n
- >>> math.radians(45)
) U) o4 I* ?! G% V$ p0 B - 0.7853981633974483$ D2 e' X, H \/ i, C9 V
- >>> math.radians(60)
( ~0 p8 j( F# r) H6 O1 `/ o - 1.0471975511965976
复制代码
+ w% R2 H5 a1 ~math.copysign(x,y) 把y的正负号加到x前面,可以使用0
, K! f6 k" r8 [! c" ?1 `! n- #把y的正负号加到x前面,可以使用0
. R$ _) ~, y. F9 M# R - copysign(x, y)6 q% |% J5 q. X D3 a- G# \" @
- Return a float with the magnitude (absolute value) of x but the sign
3 Y8 Z5 _9 c2 V4 Z - of y. On platforms that support signed zeros, copysign(1.0, -0.0) / G1 q7 {6 j) l; }
- returns -1.0." f8 ^" ^+ s7 |9 @+ Q' s
- x0 t$ i5 N2 m& M4 h
- >>> math.copysign(2,3)
7 p: t0 @7 }* N! C- r - 2.04 Y3 X1 m; L7 A# |1 P) u
- >>> math.copysign(2,-3)8 k& y4 J6 l# T: [, g
- -2.0; q; v5 O& O+ ~9 @2 P/ T+ ^
- >>> math.copysign(3,8)
) u; d+ w9 L+ f( c0 P0 Z - 3.0
4 w, h8 {, c, y- Y - >>> math.copysign(3,-8)
7 i& [+ W Y( Y% A- k5 a - -3.0
复制代码
- }0 Z3 M8 D9 L Y- w, A6 ]math.exp(x) 返回math.e,也就是2.71828的x次方
1 q- I: n! R. K e7 P8 C( a- #返回math.e,也就是2.71828的x次方& C8 H3 i8 ^; V# p4 ~6 O
- exp(x)2 f: q2 w7 @$ R+ R1 L
- Return e raised to the power of x.0 ?1 a9 T) m }
- ; @# j/ j% [( Q/ |! m
- >>> math.exp(1)
) g8 _) g6 ?# ` f - 2.718281828459045
2 |- A- T8 }! C - >>> math.exp(2)
2 S0 B. W/ U# F# M; a9 _ - 7.38905609893065
2 q+ q& K4 k9 }- B8 T - >>> math.exp(3)/ D' ]; m+ t! x: Q
- 20.085536923187668
复制代码
% j% j0 M f% Omath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
" k5 G4 z G, X+ r- #返回math.e的x(其值为2.71828)次方的值减19 E2 E ~" L S+ n6 _# W
- expm1(x)
. z6 V7 v# e/ K! F \% k$ K: d6 M" ~ - Return exp(x)-1.
( {. V) r" i t7 a1 a5 r; ]1 G0 i& e - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.& _" D5 A6 p. D6 H' j" D/ X* t8 w7 J
) s; T7 H% c! O' I* y- >>> math.expm1(1)
6 A5 H# m5 H' K1 o' Q - 1.718281828459045
0 H0 S2 W3 L4 A# F: `5 P3 s - >>> math.expm1(2)
9 D& [+ _( e V- r9 i# S4 S0 { - 6.389056098930654 S4 E* e0 \3 ]$ ~- W, t
- >>> math.expm1(3)
8 D+ e" K. V F. T, H( c7 A- B - 19.085536923187668
复制代码 , O) m/ c4 Q3 m. Z$ j$ X
math.fabs(x) 返回x的绝对值- T; p, b* [9 [! _4 T6 s
- #返回x的绝对值
$ c1 d% N6 S+ @- f8 k0 w - fabs(x), R8 c# ^6 a- e
- Return the absolute value of the float x.& Y( K' A3 W- u/ `; u2 q, Z
- - O) m6 D+ \0 x3 ~
- >>> math.fabs(-0.003)
1 O( C# N- G4 A: `( x% ~ - 0.003
7 {$ G2 v5 v# m9 D: A" [; E - >>> math.fabs(-110)& ^. M; S5 x( n4 u" r9 E+ K
- 110.0
) _9 x* B( X3 ^, H - >>> math.fabs(100)2 t) s" O6 C9 o
- 100.0
复制代码 $ N6 l" f6 ~3 i
math.factorial(x) 取x的阶乘的值" G7 h2 r4 \) n1 I% G
- #取x的阶乘的值
* H9 h- \; S5 w# r% E/ ^2 z - factorial(x) -> Integral' W# l( X, `" l% l
- Find x!. Raise a ValueError if x is negative or non-integral.7 I" t5 f- A* S8 c+ d8 v8 \2 I1 ^
- >>> math.factorial(1)
0 E E; J/ D9 F - 1
9 h/ m+ J+ S$ s - >>> math.factorial(2)4 k, {( s, {* N. {
- 2
* Z0 F% h; L3 E. g - >>> math.factorial(3)! S1 G+ Z- b# w1 M, n) @) w
- 6
. G: C1 X3 O8 O - >>> math.factorial(5)( ~0 l K) l$ \$ g
- 120
( D: {/ f1 z* b6 \* }& H$ x - >>> math.factorial(10)) ~1 Z* V1 {$ O5 L1 T3 f
- 3628800
复制代码 9 D& {/ n2 P% v. g
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数( V# @1 ?. {+ p6 E0 Z
- #得到x/y的余数,其值是一个浮点数3 X* _8 i0 q& w* @0 G9 I* |
- fmod(x, y)- C6 F3 u6 N/ K& ]3 p" @9 d& k7 O4 ~/ B
- Return fmod(x, y), according to platform C. x % y may differ.7 `: x4 [1 g8 f$ w0 l
- >>> math.fmod(20,3)" ^4 E D4 {4 f/ [
- 2.03 G J; Y. w4 ?' [( R6 p! |2 n3 m) V3 p
- >>> math.fmod(20,7)
! ~1 R! d: C! p0 a6 X - 6.0
复制代码
' e/ {/ V# @- V, N5 Amath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% y1 Q, A2 w. G8 P7 U: k; q- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 [) d' w6 c. O+ S
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 [' G. y- }2 l& v2 m
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) M4 N+ w2 A- C
- frexp(x)% _. _6 ~# B) k; u; j& B
- Return the mantissa and exponent of x, as pair (m, e).
& _3 t* Z" e4 B9 j* a9 f1 m - m is a float and e is an int, such that x = m * 2.**e.
! T- w9 z" B' u" s) c& g6 c - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- e8 \/ x$ t- w. u4 j
- >>> math.frexp(10). H: M6 o7 \& F4 K- j: r- z8 [4 t
- (0.625, 4)
0 u7 R0 d, k9 O6 ?& P2 S - >>> math.frexp(75)1 w m* i* w$ w* s! m( V7 |6 U7 z
- (0.5859375, 7): t; v. ^% w/ b0 k" C
- >>> math.frexp(-40)
7 \' R }+ Y6 d# a$ u# y' h7 ~ - (-0.625, 6)
/ Y" c! S% p) l. e$ `+ X8 R) Q1 c - >>> math.frexp(-100)7 t4 r0 D% u) W1 K0 N
- (-0.78125, 7)& g N; d ~' i0 Y$ D# o2 J% i
- >>> math.frexp(100)
* d' w# h$ \3 ]2 k$ W - (0.78125, 7)
复制代码 / k3 S7 Y6 o/ U. v* ^: ]- B
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 ~' o# W, t6 j; m: t: t' K9 G- #对迭代器里的每个元素进行求和操作
- D# k& l" X& Q& N3 p - fsum(iterable)
4 a7 O. X$ p' I2 X0 R - Return an accurate floating point sum of values in the iterable.
, _: d$ ~- [, `. t - Assumes IEEE-754 floating point arithmetic.
2 E; e1 _- Z! s8 M4 y - >>> math.fsum([1,2,3,4])
- f* \% W3 \( u - 10.0
; I4 N$ b, U1 F4 n9 N& o0 G7 l - >>> math.fsum((1,2,3,4))/ O! c6 z E2 f0 M- z( q6 k" A
- 10.0
) S; a% b- h% R- b - >>> math.fsum((-1,-2,-3,-4))2 i/ D& j' n$ n6 g
- -10.0" d8 x$ f$ v6 j2 z! Y* j0 r" P
- >>> math.fsum([-1,-2,-3,-4])& j' X2 F' }5 Q" ?" |1 o" a) N
- -10.0
复制代码
8 U7 r, K) e; v( gmath.gcd(x,y) 返回x和y的最大公约数
3 {- {3 k( p% V$ p' ^7 g- #返回x和y的最大公约数
2 p+ @) A4 f3 E' D3 q5 _: t3 ^ - gcd(x, y) -> int
. o, K6 o8 a$ s* F# W) f - greatest common divisor of x and y3 Q* ~! q6 R" A9 p# ~4 M1 ]5 Q
- >>> math.gcd(8,6)3 g2 Y/ P+ x( T1 I9 ^3 R
- 2! h: k- F3 X$ R3 z- m6 e8 ?
- >>> math.gcd(40,20). X8 C T" x a6 q5 {9 e
- 20* K6 o2 ?0 i# o: w3 I: }* g8 H) Y
- >>> math.gcd(8,12)
3 ~3 {: }2 b& J/ _) k$ g - 4
复制代码 W% r, }; n2 Z! U) S, ~
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False. Z( Y7 _$ Y9 B, t
- #得到(x**2+y**2),平方的值
6 Y- v8 t. n6 _! z' w. [# ` - hypot(x, y)# F7 }6 g+ h3 ~" Q- e
- Return the Euclidean distance, sqrt(x*x + y*y).! Q1 h5 G" Y$ E& T) B( Q3 y: u
- >>> math.hypot(3,4)8 Q* I- j7 T. j
- 5.0# G7 g* O2 R$ b& ^
- >>> math.hypot(6,8); ?( t4 E- H. j& u. d9 y. Y
- 10.0
复制代码 6 Q' j% i9 l" q9 [9 H# L k3 P) f
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False7 ^3 k( |6 X d- L, _3 _
- #如果x是不是无穷大的数字,则返回True,否则返回False" O4 N; P( b' c% J. D
- isfinite(x) -> bool
( K& g+ W! `6 h8 P' p: m' ? - Return True if x is neither an infinity nor a NaN, and False otherwise.
6 q# a9 u, R Z9 W& u - >>> math.isfinite(100)7 w+ R! i0 H3 l) R: m% V
- True
$ e- o5 I; ^5 O9 @! I+ C6 M - >>> math.isfinite(0) R" _' E% E/ y. |( o5 F5 r
- True
1 X; T. v" u# G' R& w# r4 }3 F - >>> math.isfinite(0.1)
6 R" g7 s" a6 v e - True
) Z$ e% _; Z" y" D0 J3 D - >>> math.isfinite("a")
4 l {6 S. N, J, }" G. ] - >>> math.isfinite(0.0001)
; F. q! u: y" P - True
复制代码
' L4 H8 G7 Z1 A3 F2 Smath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 ]2 U: S0 D, t, {5 X4 |
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
8 A# Y6 K+ p$ e, s6 t - isinf(x) -> bool
% ^- f; C$ w* d - Return True if x is a positive or negative infinity, and False otherwise.
9 j7 `/ K* A9 ^! Z x. t6 r2 P% ? - >>> math.isinf(234)
) V Y R3 ^, d( I$ T0 c7 V - False3 y3 K+ s3 j0 @
- >>> math.isinf(0.1) O0 _9 O5 s2 p e
- False
复制代码
1 r. o: w& ^, E0 C3 Q4 i* r( G- _4 D; {2 bmath.isnan(x) 如果x不是数字True,否则返回False( l p7 A5 w/ d
- #如果x不是数字True,否则返回False
: p' U: {% _3 c/ v - isnan(x) -> bool, M( b# _0 P) S# T8 G# _
- Return True if x is a NaN (not a number), and False otherwise.( P* I, p) X+ M8 q- K" A" T$ f
- >>> math.isnan(23)4 e5 H- H; C# x% h4 K" n# a
- False) @2 y* s( M+ W W6 {
- >>> math.isnan(0.01)
& }0 Z$ d' t' l" R- h0 V4 y1 | - False
复制代码 ; ~/ t+ E C+ A1 D% Q& q
math.ldexp(x,i) 返回x*(2**i)的值
" a. L @7 e, u+ J% l- #返回x*(2**i)的值0 L+ |& m! z6 H9 _
- ldexp(x, i)
* B- E i$ s% Z1 r - Return x * (2**i).
0 `& {' `; w' k3 n7 n/ ?5 n - >>> math.ldexp(5,5)
7 E4 c+ S+ P2 q( { - 160.0
$ `. n6 E9 n9 ? - >>> math.ldexp(3,5)4 y5 |6 x! ]+ J
- 96.0
复制代码
" O" P. t, l) k+ D$ zmath.log10(x) 返回x的以10为底的对数
' w8 y8 }$ r( U( Q( s f* y3 T0 f2 W- #返回x的以10为底的对数, U# C* d' M4 T
- log10(x)5 @* p( R/ A3 S' l: `# |9 B, Z
- Return the base 10 logarithm of x.* O+ ~1 U# |- H" T. ~4 C
- >>> math.log10(10)! r0 F$ Q+ h* J, @; q' q
- 1.00 f4 _% g: g) ] w7 \
- >>> math.log10(100)
7 x4 z5 H1 Z: E) r5 p# V) Y - 2.0' W$ V9 W% y5 j. B" _
- #即10的1.3次方的结果为20
' X1 }( y( ?7 [# ]% D# X7 j - >>> math.log10(20)
5 l) c' U, [% [4 L; \) Y. c0 ]! N - 1.3010299956639813
复制代码 + J8 U) A9 J; h8 y* ^6 c& l" r
math.log1p(x) 返回x+1的自然对数(基数为e)的值$ s( G4 g$ K( N
- #返回x+1的自然对数(基数为e)的值
, ~+ {' a6 ^, E4 G5 g - log1p(x)
& J# ^( H, b, A$ K0 G+ [7 P - Return the natural logarithm of 1+x (base e).
1 e% [9 i8 q2 ] - The result is computed in a way which is accurate for x near zero.
7 j7 N; ]. `& c8 h - >>> math.log(10)$ J9 T# j/ G1 `+ [* G, ~
- 2.302585092994046
3 B5 r7 i3 ?8 G9 N6 X( R - >>> math.log1p(10)# Q2 o' S8 t9 J) c6 q
- 2.3978952727983707
+ ~( {5 n7 q' v; h: I - >>> math.log(11)
4 |9 ]" r }; x1 ^: p - 2.3978952727983707
复制代码 `) S- X+ J$ p5 |8 F
math.log2(x) 返回x的基2对数
) j" m$ |2 w6 _- P6 e/ A- #返回x的基2对数9 F5 T4 ^% Y+ ]1 v% f
- log2(x)
! m3 q V) [% B - Return the base 2 logarithm of x.; k# l, a' Y, z& H4 q$ N* `
- >>> math.log2(32)
+ i; o# N- X0 _, O5 o - 5.0
8 w; ?/ K% D, Z# X' I6 W - >>> math.log2(20)7 J0 w$ G: z* ^/ W* v. n- N
- 4.321928094887363* V- {8 `" `7 b. m
- >>> math.log2(16)6 C6 h+ n" ]6 T i/ E( A
- 4.0
复制代码
) \+ p4 @/ t2 amath.modf(x) 返回由x的小数部分和整数部分组成的元组+ N0 X0 q8 h2 c# A. G2 @6 b+ _
- #返回由x的小数部分和整数部分组成的元组
& r9 }2 ^- X' Y4 ` - modf(x)# o8 ~" Y5 ~. L) W8 V2 G5 L
- Return the fractional and integer parts of x. Both results carry the sign
5 Q/ | k4 z! E+ e - of x and are floats.: ]# }( t+ b% S
- >>> math.modf(math.pi)
- ~( Q0 d1 \5 t, s' f; [" L$ a) L - (0.14159265358979312, 3.0). G! p9 e, {6 k$ y; S' f
- >>> math.modf(12.34): R. e( G( B6 R
- (0.33999999999999986, 12.0)
复制代码
; V: ?" p9 o. j' _4 Rmath.sqrt(x) 求x的平方根
- R/ k! P; I2 n- #求x的平方根
" F2 m, E: g1 B+ A% L - sqrt(x)0 a+ R4 Q; L5 \; j9 y& E
- Return the square root of x.
' `' v3 B4 m" c - >>> math.sqrt(100)/ H7 [2 {( I' k$ X- C2 ~, C( K4 [0 B
- 10.0
# K/ o X$ c) P4 _" B - >>> math.sqrt(16)
1 _4 M) w* v, I: x - 4.0* P& e1 h% S. ]4 V6 p
- >>> math.sqrt(20)
& h+ Z. m& o7 J' a" W1 i - 4.47213595499958
复制代码 # S2 P: R' X l: E: M5 T
math.trunc(x) 返回x的整数部分- #返回x的整数部分0 v: m% U5 L) e
- trunc(x:Real) -> Integral$ P4 e/ x* x* o+ C6 y1 g
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
% s f0 i# g* v$ a( ?; { - >>> math.trunc(6.789)8 J8 H7 Z" }! T7 k
- 6% P; b' U4 C7 m5 [( j. Y0 O8 [
- >>> math.trunc(math.pi)& w& z( C6 }6 }
- 3; Z, a/ P& p4 j/ \8 G
- >>> math.trunc(2.567)
0 T; g0 U, J. m. w& H9 } - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|