马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) D7 I/ y. i i* \+ w. q% ~0 O
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& H* e# @2 J+ b) R
( w( F2 X" m4 c R: ^- M& u
方法1:
% m9 v T! V* W% x3 v. T/ y* r- >>> import math
) e: F& F( b7 J, z0 G4 } - >>> math.sqrt(9)
, f- @( ~8 j* w2 C5 s2 x5 q$ ~ - 3.0
复制代码 方法2:
0 L* G8 ~$ W1 u9 x- >>> from math import sqrt
# P% Q5 p* t6 @ m - >>> sqrt(9)1 _0 u: o8 v1 P6 p9 B/ E0 z% h
- 3.0
复制代码
1 w& _2 e- B5 w6 S0 g S, p( Q X8 y9 s5 G+ v# l
math.e 表示一个常量
. V) ^7 W; c. e% r0 k- #表示一个常量$ m; ]% l+ g$ G
- >>> math.e
. m7 O0 J$ {4 m' L' f2 D4 x - 2.718281828459045
复制代码 ' l6 }2 r, |0 J% k& W
math.pi 数字常量,圆周率# Q. N% Y$ `9 i7 ~% H# ~, p
- #数字常量,圆周率7 @7 Q6 s% J+ _7 `" D5 [2 E- G" \
- >>> print(math.pi)+ ]4 S, X' p# v! Z1 N } k, d
- 3.141592653589793
复制代码 1 X- g" a- h6 R+ L9 |6 \: T5 ?: @' A* U3 d
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ x( \1 }6 V; o" i- #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 Q& G2 e7 ^1 V$ G& Q
- ceil(x)
5 O9 q6 G7 U8 ?/ \7 ~% |% @" y - Return the ceiling of x as an int.
" U7 y+ c6 P! e4 K - This is the smallest integral value >= x.
5 t, Z# ~* K G8 R - ; k" ]. f, U* J8 ?* E% C
- >>> math.ceil(4.01)
' D3 \# S! W/ X2 \: j$ G+ N+ R9 {/ o - 5
' D* O1 F2 P) n$ H( M - >>> math.ceil(4.99). C/ O$ F+ O; B5 k! s$ Z
- 50 R/ A/ { x1 P R1 Z/ k/ l% W
- >>> math.ceil(-3.99)
+ Q- }4 t# `6 Y5 x! K - -3
, U6 `) A/ p3 b3 H8 I - >>> math.ceil(-3.01)( R# A$ v% I1 C3 G4 \- X+ P' o
- -3
复制代码
9 Q w/ N ^0 J: Nmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 W* {; w! z$ q; j/ [4 E- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身& j% b- U# ^5 `# }
- floor(x)& W. X* Q1 S! [, f, Y% j
- Return the floor of x as an int.! Q7 V' S' _9 v1 q$ @) i
- This is the largest integral value <= x.
, _4 ^' O0 m# D. T( i* z4 e - >>> math.floor(4.1)2 a* y( c! _" y( A
- 4* Y; l; R# N! n# R, g3 [) T( K: V
- >>> math.floor(4.999)' p" m4 d- i+ ~- t3 `9 H3 G
- 4
3 t' j6 L8 }( Y# } - >>> math.floor(-4.999)1 a- @; I8 f6 \
- -55 p. n5 I( i4 i6 B
- >>> math.floor(-4.01)
# C5 S9 q* t3 u9 i9 n3 P! B, n - -5
复制代码
% }# m6 F5 f3 ^; Imath.pow(x,y) 返回x的y次方,即x**y
& @8 H/ B. d* w% e0 e+ {- #返回x的y次方,即x**y5 Z9 `4 q m& j' f) m
- pow(x, y)9 i3 J6 f- w" ^! L4 V- O& P1 J( r
- Return x**y (x to the power of y).
+ S: S& F4 @/ I, o6 D9 ` - >>> math.pow(3,4)+ ]" S6 I3 I& ^. y# V
- 81.0
( S+ ^) B! O, h: Z5 T - >>> 2 k' y" v" O2 a9 @/ u9 j& Z S
- >>> math.pow(2,7)( s& w# ~0 }2 ~& y9 ]
- 128.0
复制代码
% ~" c; i! A7 ^" t/ qmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 c8 S" b, ~) x
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' h) P2 ?/ P6 u i
- log(x[, base]) l" |7 R7 i8 b# d3 |, W* q N
- Return the logarithm of x to the given base.
; {3 b& ]2 E; x' A' y - If the base not specified, returns the natural logarithm (base e) of x.
^9 V/ i* M# P) A - >>> math.log(10)
' F6 L+ x9 X* R. z - 2.302585092994046
; s/ k- H3 I e# x+ d0 ?' ? - >>> math.log(11)
. I- D! e: e% A8 f. L - 2.3978952727983707) g4 j. ^4 t: k6 [
- >>> math.log(20)
" a8 Y. ^' `" T/ w0 M/ S" l9 i - 2.995732273553991
复制代码
! _" W4 `% l" o' J2 \. q0 amath.sin(x) 求x(x为弧度)的正弦值
/ s4 v5 V( n# l/ O" [% Z) i- #求x(x为弧度)的正弦值
{- q4 U3 @1 X. _/ ? - sin(x)/ B1 Y" n( f4 x
- Return the sine of x (measured in radians).
' f" C3 X$ ^ ~ - >>> math.sin(math.pi/4)
* _" E6 ?) C- }. F! M0 ` - 0.7071067811865475
' ?0 j% \. D: y6 E& q% F - >>> math.sin(math.pi/2): _4 q9 H, K7 k
- 1.07 h- w( ]2 V8 C2 H
- >>> math.sin(math.pi/3)
8 s# b$ M/ v0 g) Q - 0.8660254037844386
复制代码
0 z6 H$ U2 E2 Qmath.cos(x) 求x的余弦,x必须是弧度
0 e! C. ~. A8 W1 g- #求x的余弦,x必须是弧度
( }* h, h) x: t - cos(x)
1 m2 i5 q2 Z- S! E# d8 O - Return the cosine of x (measured in radians).
2 T# s, i# b! _ - #math.pi/4表示弧度,转换成角度为45度
' @" O* r/ ?+ {$ U+ I - >>> math.cos(math.pi/4)) v$ P% K* y( [1 T
- 0.7071067811865476# x+ [. b; ?+ P9 e% g5 i8 H+ {( s
- math.pi/3表示弧度,转换成角度为60度: }( l9 L: M% s+ v
- >>> math.cos(math.pi/3)
' J# F/ c/ ]% a6 O- D - 0.5000000000000001
; u2 W3 V, U3 c5 n8 v! ? - math.pi/6表示弧度,转换成角度为30度
; U% j# F+ Z2 h7 z* \ - >>> math.cos(math.pi/6)) r% a$ F+ n9 y
- 0.8660254037844387
复制代码
- c) E, S' G* c4 p# u3 Tmath.tan(x) 返回x(x为弧度)的正切值* w8 x3 I( S" L3 \6 X& Z
- #返回x(x为弧度)的正切值
% D% ~5 M& ?( X - tan(x)" E. \$ K9 O( l/ @# l, C
- Return the tangent of x (measured in radians).
; A' ~3 W) u: O/ d# m6 Y, i - >>> math.tan(math.pi/4)7 _' k/ V1 Y, ]8 V! E
- 0.99999999999999992 }: N: b# W1 A; |
- >>> math.tan(math.pi/6)% s6 }8 H' l9 V
- 0.5773502691896257
- D7 {3 @) a) Q - >>> math.tan(math.pi/3)8 M$ [6 a4 }- g8 j5 }. e$ {6 V
- 1.7320508075688767
复制代码
4 _* r7 f C3 p- E w6 M% O* @math.degrees(x) 把x从弧度转换成角度* J. f: ?4 Z; P% [* K
- #把x从弧度转换成角度
3 a( Z5 g$ P8 ?5 ]" F7 B* ^ - degrees(x)
6 a$ o& c+ R7 H! _. e. d - Convert angle x from radians to degrees. p, Y" l) W! E$ o
u: v, e' {/ u( o- >>> math.degrees(math.pi/4)4 r# o/ s, O% i- R. y1 P
- 45.0& D( ]8 @5 Q7 r8 U* r# Y* q
- >>> math.degrees(math.pi)
/ j ?' @* M% {7 g6 o8 u( _ - 180.0+ q3 K9 ]9 m! w$ E" c# G3 @ j g; K
- >>> math.degrees(math.pi/6)
5 m% h! T5 {: g% r: a$ s - 29.999999999999996# o7 f3 p: \: _, Q$ ?
- >>> math.degrees(math.pi/3)
+ }9 C( N( s7 w - 59.99999999999999
复制代码 " @/ [+ M7 ~0 v, f B+ f
math.radians(x) 把角度x转换成弧度
5 M, F: X2 b0 `+ S- #把角度x转换成弧度5 J4 n5 J9 ?9 d' \, {) @& a* ^8 ]% i3 B
- radians(x)
7 X7 y0 o( A* {1 a; x - Convert angle x from degrees to radians.8 I0 }. {; B& _" d- m9 n/ P
- >>> math.radians(45)2 H! H6 y k; P9 `" d
- 0.78539816339744836 E% h$ Q+ P5 T6 n5 A$ t
- >>> math.radians(60)! a3 Z+ e; y# Y) j" L" m2 p
- 1.0471975511965976
复制代码 $ P- A. N9 J8 A% j/ q* s
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
1 ?) |4 H/ y J8 I% s- #把y的正负号加到x前面,可以使用0
+ ?# ~- ~" g' E! D1 d. ~ - copysign(x, y)) Z1 M' `. F6 C
- Return a float with the magnitude (absolute value) of x but the sign
. i! i r; \3 d - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 V# g' p/ c U# a, i7 y; S. {
- returns -1.0.
2 ?( R8 `% Y+ Z) A
( T9 M# ?& K9 j( ~: o3 E+ r0 ?2 a- >>> math.copysign(2,3)% z1 F( G! ^. k" u$ Q
- 2.0
8 s: g! T$ T3 {4 ]% U, ` - >>> math.copysign(2,-3)6 _" \$ j8 ^' d2 U
- -2.04 m/ H( z- ~- Y8 | D6 i
- >>> math.copysign(3,8)
4 k; i1 r4 D& s# M6 s - 3.09 J# T( q2 A* G+ Z0 ?& g2 M
- >>> math.copysign(3,-8)
5 N" P. @( _7 F* ? s; C - -3.0
复制代码
; N9 E9 s$ ~) n7 R2 Hmath.exp(x) 返回math.e,也就是2.71828的x次方; @$ J$ j* U! D* v7 w# E2 v5 U9 W
- #返回math.e,也就是2.71828的x次方
: p. V0 |* d. ~ - exp(x)
3 y( m' v5 |' k8 x: _ - Return e raised to the power of x.0 f; Z ?4 g. L. ?
- 9 v. `. M3 z; V' x6 m) \# E1 I
- >>> math.exp(1)$ Y- ?: y1 S7 |: ?0 C0 @
- 2.7182818284590454 p5 W$ z+ R1 T0 B
- >>> math.exp(2)
% q! S% B9 c5 w. i. Q$ f - 7.38905609893065* G: m1 Q- H+ _- ~: n9 ^
- >>> math.exp(3)! c' _+ m- Q; b9 o3 l
- 20.085536923187668
复制代码
; f8 x. @1 X- g4 `' A5 tmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1. s% q9 R k$ @ [ R, a! w, c% W
- #返回math.e的x(其值为2.71828)次方的值减1
2 \% u& Z9 X2 E! ^% w - expm1(x): ^3 Y( {; S: N \2 g6 ]9 A7 u
- Return exp(x)-1.
* t; ?: j% W6 ^6 B& E* k - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ h$ n6 \ c6 W( p
- ! Y# Q/ ?# P5 o
- >>> math.expm1(1)
1 B- k& ?3 s/ c" P( | - 1.718281828459045
! Y" I: O* G l2 B, V8 I - >>> math.expm1(2)
* g# _9 c: M: M" I% F7 h- J, A4 @ - 6.38905609893065
. v' j+ a, g0 W - >>> math.expm1(3)
) p) d }6 s) l" Z: T3 \ - 19.085536923187668
复制代码 7 U+ i3 ~9 @8 |8 c* f0 B+ Q
math.fabs(x) 返回x的绝对值
7 a8 j) X* w+ |5 C- #返回x的绝对值
, v& o% d) I; Z" J& V - fabs(x)
6 Q/ ~) Z' Y2 g$ w - Return the absolute value of the float x.2 G) n( }/ P. c+ B
9 [" o: C2 J d; Y- >>> math.fabs(-0.003): J- N _+ G! Q V/ N
- 0.003% K& G) e; B3 Z
- >>> math.fabs(-110)2 k& D+ B0 X$ [$ m9 E
- 110.0
, z, U+ O5 V8 [# l- K8 S( M - >>> math.fabs(100)
( Z7 [( Y) z2 V; `; d - 100.0
复制代码
! w( W7 s$ e& [math.factorial(x) 取x的阶乘的值! @/ v7 E( U8 c
- #取x的阶乘的值" c: r, I9 ?$ i7 O
- factorial(x) -> Integral. j% n1 ~) M* O. W5 B( G
- Find x!. Raise a ValueError if x is negative or non-integral.9 ?/ X6 f' o! w F
- >>> math.factorial(1)
" F' \# E- _: t5 v - 1# E2 l8 J" W& v4 b! `
- >>> math.factorial(2)
5 G3 M1 L j3 P0 C; q - 2
0 V* I! |4 D5 a5 W! e; a* U - >>> math.factorial(3)
( {: j: @" y, K! {: U% ?1 k$ e9 f1 p* q - 6
; r5 O( _" `6 y$ @; T - >>> math.factorial(5)
5 a! @6 d1 X0 }4 i2 ] - 120
2 I9 I! E. P3 y5 t1 Y" B - >>> math.factorial(10)
1 y! h$ x1 Y8 U Z$ s% @: O7 G! g - 3628800
复制代码
0 E# a* @ k8 a6 Fmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数' [8 t4 V1 T% J n" t' M- M# b6 Q
- #得到x/y的余数,其值是一个浮点数1 V4 n9 f0 R" A6 `5 d7 F( v
- fmod(x, y)
& R: y8 P9 p; G" F' W - Return fmod(x, y), according to platform C. x % y may differ., B, ]5 U# D3 a f4 b
- >>> math.fmod(20,3)! J. h3 F$ b( N1 V
- 2.0( }& X7 I, J9 a4 o( j; {2 ~) j
- >>> math.fmod(20,7)- h( v, a, Y3 s* D }9 q' `
- 6.0
复制代码 % ?6 g) T0 N* ?2 ^# R" x5 A
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围. c0 k/ Y' F# ?3 n/ ]; C% i
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
) l! M% _2 a1 \& c/ m1 P1 } - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
; n4 A3 s4 j9 O9 q5 T; G, F - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; I5 p& H, _4 n8 g# S
- frexp(x)/ u& `( H0 A6 I/ I% }
- Return the mantissa and exponent of x, as pair (m, e).
/ z% n8 r1 |1 Q1 C - m is a float and e is an int, such that x = m * 2.**e.
9 G* h# A1 L; x& m - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.4 q) L/ r2 ]4 u0 Y- P! i- r
- >>> math.frexp(10)
7 Z9 X8 @+ a u* ]9 } - (0.625, 4)
" K: T6 w8 {- \3 R - >>> math.frexp(75)
% W- E' V" Y, ]9 D% E; Y: N- P - (0.5859375, 7)
- d/ r1 t0 d) K" e3 I- R - >>> math.frexp(-40)) g9 k+ N( I: G( C- w
- (-0.625, 6)) I4 r6 q" O7 u6 D/ k* w/ Z
- >>> math.frexp(-100)
( \) b( D3 I! w2 G - (-0.78125, 7), K% f0 ~0 z V
- >>> math.frexp(100)
l0 a! _/ L) R; z - (0.78125, 7)
复制代码
* p. r1 J/ u' X9 B9 X* N8 |" Wmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)( u5 D- o. d/ [
- #对迭代器里的每个元素进行求和操作
1 ^: z5 e8 F, E+ d# g( A - fsum(iterable)8 S% q" E, \0 Q8 F. ]' B) O1 X
- Return an accurate floating point sum of values in the iterable.7 J8 A! ^0 H5 ^3 f3 a# f/ t
- Assumes IEEE-754 floating point arithmetic.! ]: ?9 M$ K; V" [
- >>> math.fsum([1,2,3,4])
5 w$ _: h0 w# [) }0 R) v' k" ]4 B: Y - 10.0
, w% M) F! y2 r, p4 C P4 r0 a - >>> math.fsum((1,2,3,4))7 e* c2 q/ D4 R. z3 n1 b' W
- 10.0- N! ]' t5 w: y# S3 x- Q. b* J
- >>> math.fsum((-1,-2,-3,-4))
0 y; V/ |+ I: w. L/ U: e5 `7 F; e - -10.0
2 F7 t8 V: b, D - >>> math.fsum([-1,-2,-3,-4])2 B% j' A5 f' m. \ c; F
- -10.0
复制代码 : ]7 ?. p" e0 y8 }" N" h& A
math.gcd(x,y) 返回x和y的最大公约数: T; _4 W- v" @" _! K6 b. B2 U
- #返回x和y的最大公约数
0 u; R' N0 X4 G* q- Y - gcd(x, y) -> int$ A' F3 P/ T1 J' A* J4 T' S
- greatest common divisor of x and y$ ^) M% H# O' Y/ E
- >>> math.gcd(8,6)
3 i* C8 c. x# i6 a2 m - 29 Z7 S- T: F) e1 K
- >>> math.gcd(40,20)! R% j- z4 d1 R9 I1 r: W
- 20" V' i) s. c) p$ I0 }
- >>> math.gcd(8,12)" N; F; c* F7 M2 n) J5 C; }; ~6 I8 i
- 4
复制代码 " P4 b3 M; U ~# O1 Q' d
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
$ U" ]# M6 q- D- #得到(x**2+y**2),平方的值% I8 }, P G7 t- Q% z7 q
- hypot(x, y)8 w5 c9 k. T# T
- Return the Euclidean distance, sqrt(x*x + y*y).
X# y, w: h+ s K& e$ v) } - >>> math.hypot(3,4)0 N2 n/ e; L6 k0 w) @* S1 I4 C/ v9 ?
- 5.0
# y# S# n# \1 k( s4 z- ^ - >>> math.hypot(6,8)
5 @; S6 u8 w8 I+ G* L' k1 t5 e - 10.0
复制代码 9 r) G) p6 H6 N
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False k( z: v7 @& |/ L" `5 X6 k
- #如果x是不是无穷大的数字,则返回True,否则返回False
% {2 f$ y0 y2 D) v/ t - isfinite(x) -> bool: h! g# A+ o$ e" O! r2 v
- Return True if x is neither an infinity nor a NaN, and False otherwise.
( W0 o6 v+ H2 e# r - >>> math.isfinite(100)/ b: ?# A% l3 Y. b
- True( w: Y/ m. s; p, p
- >>> math.isfinite(0)
) R# }* m5 E2 m4 E* f( |- h - True
/ b7 j, t3 a8 L. O$ C9 v - >>> math.isfinite(0.1), D. A* u8 P" b; ~
- True
. S/ K0 S# O; O0 N# v" t% p4 p - >>> math.isfinite("a")
& K0 G# l( u" b c5 n/ x! h$ g - >>> math.isfinite(0.0001)0 E6 R$ {- K8 L* T8 }- f
- True
复制代码 ( X5 c) `9 W4 } `- r! E5 r
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False" i4 ^1 S" e) o' q9 ?
- #如果x是正无穷大或负无穷大,则返回True,否则返回False4 s2 m& ^5 i! t0 Y* \
- isinf(x) -> bool
$ Q1 S2 |2 [( m - Return True if x is a positive or negative infinity, and False otherwise.7 x1 Y; ~3 B6 \) X4 Q& Q1 U8 Q
- >>> math.isinf(234)
3 y% I9 O' E1 Y. K - False o9 k( p9 ^* f* I
- >>> math.isinf(0.1)5 z* _3 J; a& P4 s! i+ Z
- False
复制代码
J0 [* q' x8 ]% F4 X' zmath.isnan(x) 如果x不是数字True,否则返回False
+ d: ]" V: |" N7 e5 n- #如果x不是数字True,否则返回False
$ e, P& p& t x. E, W7 t: Y. L5 Y' C - isnan(x) -> bool
8 [8 C$ p) E; b9 h2 \5 \1 W5 G - Return True if x is a NaN (not a number), and False otherwise.
5 k: R3 `+ @8 s: s+ S J/ m h5 p8 B - >>> math.isnan(23)3 `" G8 I9 X! G X/ T
- False
2 {/ z8 p; l/ |5 F5 A: G: _. h% z - >>> math.isnan(0.01)
. A s. s9 ~. A - False
复制代码
# o9 V. Q: y8 M9 H nmath.ldexp(x,i) 返回x*(2**i)的值
% N; x' T1 x& U7 f- #返回x*(2**i)的值
2 F9 Y+ H1 {1 o4 E g - ldexp(x, i)0 a, v( Q: B: b3 L
- Return x * (2**i).
+ q& D# G0 W7 n G) k3 J8 ? - >>> math.ldexp(5,5)2 X/ U5 u6 w5 V$ G5 F
- 160.0$ [# ?; I$ @0 {' ]
- >>> math.ldexp(3,5)
/ e: H' g( N3 i4 P Y( v$ V, y - 96.0
复制代码
- C# b; B! ^* B9 lmath.log10(x) 返回x的以10为底的对数# n( C' c5 c# G" |8 U
- #返回x的以10为底的对数
* x+ i R+ J7 D" w. q - log10(x)
$ m" W2 G2 T+ E! }" ^+ M9 T - Return the base 10 logarithm of x.6 E6 |" M- G' p/ Z+ ~
- >>> math.log10(10)- u( c% j0 D& y& W: l
- 1.0
1 {6 t$ \1 g& C2 o. Q - >>> math.log10(100)9 z8 ~6 R0 U& r3 K) `
- 2.0
, C& Q0 }% o: U' x - #即10的1.3次方的结果为20. o& o- t( e1 W! T9 u
- >>> math.log10(20)
6 f- R' V0 o9 _! \' d$ F! {6 \ - 1.3010299956639813
复制代码 * f3 ~3 `" v1 t5 g# g
math.log1p(x) 返回x+1的自然对数(基数为e)的值
3 h) T) C( `$ ], r/ T- #返回x+1的自然对数(基数为e)的值2 B) N; L J. Q
- log1p(x)0 K; F( n! Y0 ]9 J, H2 Q/ \" U1 v* ?, X
- Return the natural logarithm of 1+x (base e).' a# A( N" h$ | y+ }
- The result is computed in a way which is accurate for x near zero.
* k% ~! W' @6 t; l* R* n) p- A- s - >>> math.log(10)( Y# z, J) q6 J+ N. r
- 2.302585092994046, E" p! k; w. Z0 r0 a! O$ W
- >>> math.log1p(10), a; U% m/ F% c% H8 _% c) Y
- 2.3978952727983707/ C j( q4 j9 V
- >>> math.log(11)
/ |, J' d1 G z* `. P/ U3 t - 2.3978952727983707
复制代码
l' {/ u5 p$ s$ o1 S' @; U2 `math.log2(x) 返回x的基2对数6 b# V h( c9 {7 @9 v5 a
- #返回x的基2对数0 [ L/ n0 Q' W/ ?
- log2(x)
% j+ u) R+ l7 i5 r - Return the base 2 logarithm of x.
1 X# W( D3 s. }6 F$ Z+ Q9 O% L - >>> math.log2(32)
- l+ T! t1 e+ F7 G% B' | - 5.0
7 F+ }5 j6 b i+ t- |6 |, B3 _& ` - >>> math.log2(20)
* T; j, a$ i# q: o& T - 4.321928094887363
% z+ b7 U# m0 n1 a" C9 [: i$ g7 s - >>> math.log2(16)8 r! p( E1 _7 @: e: R+ T
- 4.0
复制代码 - a2 Q7 F: C* y4 [0 h( y3 I
math.modf(x) 返回由x的小数部分和整数部分组成的元组" G' N8 ]! n H! c
- #返回由x的小数部分和整数部分组成的元组
0 ^+ k4 e( f8 K7 j9 G5 P" g - modf(x)5 S/ Q# F$ g1 G$ f) d8 Z
- Return the fractional and integer parts of x. Both results carry the sign @7 Q \% v! D+ n! D8 r
- of x and are floats.. y m9 O% _8 U4 ]. ~
- >>> math.modf(math.pi)" [3 a* d% J6 I5 i5 m
- (0.14159265358979312, 3.0)( F7 B1 ~. T3 q; Z! P8 T6 x- ^
- >>> math.modf(12.34)1 W/ z+ n! s8 ?, j1 Z
- (0.33999999999999986, 12.0)
复制代码
" |5 V- U% K3 [. ^math.sqrt(x) 求x的平方根" d4 W/ g N! o' T( v
- #求x的平方根" P& D$ L- \: O( a9 U J1 j
- sqrt(x)8 X4 h' Y- w5 D( [% J
- Return the square root of x.! N# J) x1 F4 r+ L2 |
- >>> math.sqrt(100)0 C0 S" U* J% _
- 10.0
! A1 j* {' [: B' o - >>> math.sqrt(16)
# y& V/ ]* q0 Y" v% C: y - 4.0( k% b8 ^ \8 Y' X$ j, l, Z* `! ?+ T5 V8 c
- >>> math.sqrt(20)
" F$ u4 g9 k( K" t% x } - 4.47213595499958
复制代码
' \: ~3 Z0 q. _% H) I( Rmath.trunc(x) 返回x的整数部分- #返回x的整数部分
% N4 R% n; m* s - trunc(x:Real) -> Integral
- D+ J9 O. B0 L4 S - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
; y- g3 K7 E: V% R5 [" G5 ~ - >>> math.trunc(6.789)3 o/ k. D2 w1 B0 U
- 6! S" U8 k+ n; d
- >>> math.trunc(math.pi)
; W4 j5 Q9 h5 z! ` - 35 ~& {' {; k+ C
- >>> math.trunc(2.567)" v% L, V3 g! p( N
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|