|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 R5 ]/ C. c1 k5 U7 |* ^$ O
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. d5 |" M5 ]! ?" u
* T" Y* ?! H2 b4 z; i7 R方法1:+ V1 X9 o1 I, n
- >>> import math0 V9 R& X( i- q& ^
- >>> math.sqrt(9)
! c v) _1 J8 r9 I. ` - 3.0
复制代码 方法2:
- x" m$ a( u, R! n6 T6 z0 ]- >>> from math import sqrt
4 `5 _: j9 J) C$ Z$ n ]: f7 F$ { - >>> sqrt(9)7 x% Q3 F7 r2 n! K$ I
- 3.0
复制代码 6 I" Z- n3 S6 t9 G2 ~" S2 z
8 @$ ?& H7 ?/ k/ ^8 K3 x3 e5 u1 Vmath.e 表示一个常量
* \) R5 V2 k* P& U4 P- #表示一个常量
( u4 I$ n6 W2 k$ i1 Q* Y+ t4 f/ ^5 x - >>> math.e0 w+ V6 F8 c t, Q, t. x$ e- Y
- 2.718281828459045
复制代码 8 o8 R3 p" t N0 C0 A$ i1 p Y2 m! h
math.pi 数字常量,圆周率' d# Q; N9 t6 R1 |2 J, h! k, ]
- #数字常量,圆周率
6 ^( X. C5 K5 a9 u# M0 K7 S3 p - >>> print(math.pi)/ m. |) } @$ l2 C! f- N( Y9 S% I5 W
- 3.141592653589793
复制代码 % `, N# z6 \8 g
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
# |+ C/ {7 ]3 L) ~- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 }8 M3 G5 P n; t d2 i# q - ceil(x) I4 X" e; u9 H$ C' Y
- Return the ceiling of x as an int.
* m1 G! P! Z% s5 @; Z - This is the smallest integral value >= x.
5 D4 h; U4 f3 e4 V" k7 M9 Z
t9 |$ z# W1 E- >>> math.ceil(4.01)
( }8 q0 v# ` `" T! Y4 i% o - 5' v/ U4 L f5 L' H# u( |$ z" n9 \
- >>> math.ceil(4.99)/ z0 X$ q: K8 L; V
- 5
6 I( |1 O" e3 ]) ?& X! w - >>> math.ceil(-3.99), o$ e$ z- l# @# D8 \
- -3
! B/ W) [' n' ?1 \' Z- f* s - >>> math.ceil(-3.01)
+ M+ K8 X# y5 v - -3
复制代码
' {2 }" E% k; X$ Zmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' }' K# R* f% C0 p7 B' V. l4 Z- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 K% l( o. F$ W/ D. @. y5 b7 w$ e7 W - floor(x)8 [9 X% j8 q; s3 v/ s6 p2 z
- Return the floor of x as an int.6 j9 m, Y1 @6 ^. g1 K& O
- This is the largest integral value <= x., s3 @: O0 G" W/ {) o4 }
- >>> math.floor(4.1)
6 u& j+ L7 p9 I - 4
$ c+ x- n5 J* y - >>> math.floor(4.999)4 G7 y) | f4 P' X
- 4( j4 }9 e' L8 ^
- >>> math.floor(-4.999)$ T, C/ w7 E/ F$ k# N
- -58 ^3 T( _) j. z
- >>> math.floor(-4.01)+ x9 @" v+ c4 R; L
- -5
复制代码 * O* }- e3 h8 _5 m) Q
math.pow(x,y) 返回x的y次方,即x**y" V% z) c+ B/ E$ r) z
- #返回x的y次方,即x**y) P9 }& }8 T% |8 h' G0 W1 D
- pow(x, y)# U2 Y' K* s; y' ^ @
- Return x**y (x to the power of y).
; I* N! g* Z$ b) G+ P - >>> math.pow(3,4)
2 D7 ?7 i m+ @9 y, C6 v7 W; u - 81.09 Y" [+ m9 _- E5 L8 J& m0 V0 }
- >>>
! k6 v2 c7 Q- q8 { - >>> math.pow(2,7)
]' O Y5 [, a; J% R/ m - 128.0
复制代码
% m# G+ {8 k3 G/ n6 q8 Z5 _math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% E) F8 l5 T/ F. ]) D5 {* X' z- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 a9 c, Y. [' o2 { - log(x[, base])5 X- I, W5 C/ t# o0 W/ E
- Return the logarithm of x to the given base.- H+ n8 p! K% T; g0 C
- If the base not specified, returns the natural logarithm (base e) of x.
( w& _3 H+ [3 v$ s" K - >>> math.log(10); z9 H7 m9 e" w5 D' z' F; ?8 f# t
- 2.302585092994046
( e( o+ f4 d! B3 J; |5 m& v1 ` - >>> math.log(11) D6 n3 P- m4 N! A! X; Z# ?. n/ O
- 2.39789527279837078 r/ X2 R5 A/ Z' G/ K _: y" c
- >>> math.log(20)( ~! j: M F9 q
- 2.995732273553991
复制代码
% N+ t$ ^/ N& ^* d% i: Z$ [9 Ymath.sin(x) 求x(x为弧度)的正弦值
8 d9 P/ U& W2 l. u: p- #求x(x为弧度)的正弦值
7 a% b! R9 V9 u - sin(x)
o8 B) m5 ~, G0 p( ? - Return the sine of x (measured in radians).1 y. A) ~! z; c6 `, i) ~6 @
- >>> math.sin(math.pi/4)
4 y, O y3 \& {# O3 }: J' ?: D - 0.7071067811865475
+ q; U7 }+ f# ?6 q' e - >>> math.sin(math.pi/2)
2 Q/ c5 T$ R! M! V5 } - 1.0* @' ~4 K: L {) L! {- m& O( |
- >>> math.sin(math.pi/3)
7 S. ~( b$ B9 [2 X+ v. F6 p - 0.8660254037844386
复制代码
" H# v& r/ r3 ]2 emath.cos(x) 求x的余弦,x必须是弧度, r' v1 m! {* \3 N: |1 e {5 z$ F! I
- #求x的余弦,x必须是弧度
c0 q0 F4 f& O9 m3 [! r - cos(x)
5 t5 G, S: }* I1 _( u2 e - Return the cosine of x (measured in radians).+ F1 G, d& J/ Q
- #math.pi/4表示弧度,转换成角度为45度
3 g7 k# X8 }- a, w8 n4 w) M - >>> math.cos(math.pi/4)
# P/ {% i: F8 x" n: i - 0.7071067811865476
2 p# H5 w" P/ ]! ~7 [7 Y8 x9 y& n4 X - math.pi/3表示弧度,转换成角度为60度
( \. O/ r$ u2 a" O/ K - >>> math.cos(math.pi/3)& E- `; y& E3 B3 O" o
- 0.5000000000000001
1 m& E" X1 |* S1 z - math.pi/6表示弧度,转换成角度为30度
' R B. Z3 ^: l" u1 D. c - >>> math.cos(math.pi/6)
. b& Q9 n2 D6 q6 I! S8 l% k - 0.8660254037844387
复制代码 $ C+ q$ k; I6 u. @
math.tan(x) 返回x(x为弧度)的正切值
2 p, j* ]5 l7 }1 t3 j7 t, X$ v- #返回x(x为弧度)的正切值
- r1 @2 l9 F q4 F - tan(x)' |. _- a, C) G5 g5 d0 e9 c
- Return the tangent of x (measured in radians).
4 {( S* P4 D6 e2 `" C - >>> math.tan(math.pi/4)
2 x- Z. w( W( \+ o2 u+ T - 0.99999999999999999 {6 a' D# h+ B( b: u
- >>> math.tan(math.pi/6)
{8 t# ^$ y5 R - 0.5773502691896257
! X( @ Q4 u# Y9 Y: o8 X- A, Z, x - >>> math.tan(math.pi/3)
) M& Q$ ?3 s3 i - 1.7320508075688767
复制代码 - r$ I' l7 U# A$ B w
math.degrees(x) 把x从弧度转换成角度0 T4 {% U' b, N1 y9 p/ M- J" t
- #把x从弧度转换成角度6 R: u* S! |- r% z9 t& i
- degrees(x)
( j; X: r8 h, j) q: b; d& g - Convert angle x from radians to degrees.* g# q: v# T' Z$ P* f. @* j
- * Z9 w/ D- l- g/ u
- >>> math.degrees(math.pi/4)+ x& @) f) r9 l. d0 {
- 45.0' Z) N. W" D% G% w) Y/ X& z
- >>> math.degrees(math.pi)$ q! ?3 y% D% o; V( n% N4 d
- 180.06 U ?4 o. C# \6 ]
- >>> math.degrees(math.pi/6)4 |7 ?( @2 k. Q0 @. D" h
- 29.999999999999996
4 F, [% R, k' t- j - >>> math.degrees(math.pi/3)- N8 }3 t7 T! w, G |1 R
- 59.99999999999999
复制代码
, @" v4 l1 L: amath.radians(x) 把角度x转换成弧度/ I8 o. \3 p+ W, }7 s; J3 D
- #把角度x转换成弧度
: ?# J2 Q5 r! w4 W% A( j - radians(x)" @6 y2 X5 z- ~4 o1 d+ {/ Y8 L
- Convert angle x from degrees to radians.
2 d% t- j/ P- D5 I5 N7 b/ [ - >>> math.radians(45)
* W3 g* w. c7 o+ z0 M* { - 0.7853981633974483
0 } v4 j0 T) Z; e6 h1 E - >>> math.radians(60)
" a" }% t- ~; |# e2 n - 1.0471975511965976
复制代码
' _6 h; X; K& x9 h1 R8 Emath.copysign(x,y) 把y的正负号加到x前面,可以使用0
" @- l% X' z7 b$ m: T+ D2 [/ s- #把y的正负号加到x前面,可以使用0
. K5 f. e2 P5 e) S- p3 ]( C: P- ~ - copysign(x, y)5 C8 M3 t3 s' E8 s# b5 t8 J0 g
- Return a float with the magnitude (absolute value) of x but the sign Y' S5 Y- F* o7 v7 ^0 c* W
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & \; e0 b- ~7 }' U4 R
- returns -1.0.. l+ n' V4 {& o0 q
3 h* J3 q9 _ Y- >>> math.copysign(2,3)+ w# T, `# Y; F9 S! N
- 2.0
9 f; }2 L; M J& o5 Y: F - >>> math.copysign(2,-3)
; n+ t) X* H6 o8 c! i' ]% g$ g - -2.01 Z. O8 [$ D+ }( B
- >>> math.copysign(3,8)% L. _8 E! ~ y4 k
- 3.0
* L( n8 G( H$ x - >>> math.copysign(3,-8)& Q, V$ m4 B: F- Z' F, _' f
- -3.0
复制代码
' C2 }5 f/ E8 l1 t( lmath.exp(x) 返回math.e,也就是2.71828的x次方
t- Z+ t9 k- {! S0 q! [5 X1 K- #返回math.e,也就是2.71828的x次方
4 P& s+ D& D& |3 ]1 F0 @$ E - exp(x)' _& m( k; y" s: c! b5 a1 S
- Return e raised to the power of x.
3 U8 o; c2 q- u9 L5 N1 g - $ \( h# E8 f. {5 w' k* d4 U+ m) g
- >>> math.exp(1): l9 L. L1 P% _$ J: }
- 2.718281828459045! r) x# i/ |% r. t( Y- K
- >>> math.exp(2)
; \# x7 w) Q: ^5 Y4 ]( i3 E9 T - 7.38905609893065! D& S; L! z o l
- >>> math.exp(3)% X3 X; ?/ l9 N w0 t9 u
- 20.085536923187668
复制代码 2 f+ v$ j, `5 S# S
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
9 F7 K. q7 C, U: w9 E- #返回math.e的x(其值为2.71828)次方的值减1( @0 ]4 _( e" A* }8 e5 U# y
- expm1(x)
: L6 c, C( g c' R/ Q! y, N7 Z - Return exp(x)-1.: G- A# S4 _; D q, P5 U
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
3 I6 A& m+ N% k. p' e! a - " p. i( r9 ^1 I0 k; F
- >>> math.expm1(1)
$ G- n0 P' [$ w A - 1.718281828459045. ~# _# X5 n, p
- >>> math.expm1(2)% ]6 P* d" T! ^6 [% M6 P; ^
- 6.38905609893065" y8 X+ v+ c/ `. x" b
- >>> math.expm1(3)9 M' u. I$ L; `. @: O; j2 O* ]
- 19.085536923187668
复制代码
- }, X* |# U" R: C, {; hmath.fabs(x) 返回x的绝对值
* v) A6 W8 _. E8 ?5 j p- #返回x的绝对值
- ^4 p5 s1 P6 J; T. \ - fabs(x)9 Z/ ~6 |. r: x# U& T* W: {
- Return the absolute value of the float x.
. n& l- q) `; C. X. ^
& P; U- N+ E9 ]$ x7 X. I- >>> math.fabs(-0.003)9 \$ Q' p/ c% \& G/ I3 @3 T8 Q
- 0.003) B5 o( I. _7 H5 \0 W$ C( ^
- >>> math.fabs(-110)
% v; W9 d7 p: Z) n& h0 P3 a - 110.0
1 e6 H1 j8 L$ ~& b2 M - >>> math.fabs(100)
+ P3 x7 |) N& u0 \ - 100.0
复制代码
. Z1 C3 v6 S% j% G9 ~math.factorial(x) 取x的阶乘的值
9 y+ g. i" ~/ z. h! |- #取x的阶乘的值
. Q* @& K( Q+ ^ - factorial(x) -> Integral" h2 O. v+ _7 Z1 ]( U, c$ L
- Find x!. Raise a ValueError if x is negative or non-integral.: `8 S. J/ E0 I
- >>> math.factorial(1)
( y6 N3 V( T; M- X - 11 R/ R. n/ A5 t; E+ g
- >>> math.factorial(2)8 p" B( q& M$ r1 u5 }
- 2
& L5 L0 b5 D; N - >>> math.factorial(3)2 ?. ?4 |- h0 Q7 \
- 6
* f3 C) @6 f: _, V) q+ B - >>> math.factorial(5)
) i' f2 g- C9 U5 S - 120
9 c& b" Y) U+ p7 W$ d - >>> math.factorial(10)+ S" ?* n8 v, S- s& I* O
- 3628800
复制代码 7 p& \5 I. b5 s2 |& M
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数0 p( j n; P# c' N
- #得到x/y的余数,其值是一个浮点数
; P8 l- ]( N' s) m1 b$ V7 A - fmod(x, y)
: p8 O. e$ G% L T+ Y( \- k) w - Return fmod(x, y), according to platform C. x % y may differ./ g; Y: B7 k4 G0 h
- >>> math.fmod(20,3)3 Y% x3 Z0 D2 O: Y' S( Z2 a1 ~9 j8 ]
- 2.0" W- ~6 V( `/ n8 o6 E
- >>> math.fmod(20,7)3 s8 ] V1 c' v# l, {( r
- 6.0
复制代码
. d4 I; M5 L' Z: i# F4 ^math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 N- D: X" Y% t: E) B
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
) Z0 \$ f" X1 w% ^6 M - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
; u$ U9 |& O/ o+ @, o& |* @ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1 w) ?+ c6 M1 ^, B
- frexp(x)* ~1 _$ g0 Y; a8 i
- Return the mantissa and exponent of x, as pair (m, e).
4 d, K% q# K$ U2 A2 j8 O - m is a float and e is an int, such that x = m * 2.**e.
7 J M$ _' E3 u - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0., V' i' G+ g8 _) R/ X; n0 k
- >>> math.frexp(10)1 j2 o2 l$ Q7 `; b& B
- (0.625, 4)6 T- t1 F2 U" g7 c" F
- >>> math.frexp(75)
: Z* r( M% Q% a' \, G* f - (0.5859375, 7): n9 G I9 N9 L% Z C
- >>> math.frexp(-40)0 D( q2 O) M' Z/ W
- (-0.625, 6)5 K; Q8 `( v) g# s- o
- >>> math.frexp(-100)
( j: B1 ~) W" L - (-0.78125, 7)! D7 K" @' f; ^ ]
- >>> math.frexp(100)
% q, q. A0 S) K, k3 L& F. y - (0.78125, 7)
复制代码 , E$ u" M, r9 i' w" d
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
: w; n3 y# S# E* F- #对迭代器里的每个元素进行求和操作
5 S" k3 C1 u% P2 r7 @6 C3 V# A - fsum(iterable)( c" j! m6 ]4 c* [
- Return an accurate floating point sum of values in the iterable." C) Y- r7 ^: |- }/ {1 W- B( g {
- Assumes IEEE-754 floating point arithmetic.
8 ^" }" s b; N0 }! o5 T& Y - >>> math.fsum([1,2,3,4])
( P! @$ b- n( Y8 H8 O) x - 10.0+ C% t Z; E" s4 @; O/ v
- >>> math.fsum((1,2,3,4))+ l% i6 z3 M5 `, ^
- 10.08 n# `! S) p, v2 O
- >>> math.fsum((-1,-2,-3,-4))3 A! B4 \& K8 K7 K0 R
- -10.0# A: o7 _9 K- ^7 l W0 @: h
- >>> math.fsum([-1,-2,-3,-4])
b4 {% G. F; z - -10.0
复制代码 # ?0 O `% i. [: L) k5 V. E
math.gcd(x,y) 返回x和y的最大公约数9 W1 Q* {: V% z1 d" k' T
- #返回x和y的最大公约数. _+ H8 q5 V% `) x3 R- D# u
- gcd(x, y) -> int
" X7 J: j7 }1 a- V( r - greatest common divisor of x and y
; E* L, i4 t& G: @) k7 w7 V - >>> math.gcd(8,6)& z) y# `2 K$ w5 q' v
- 2
) U, P! @4 q, M. v9 s - >>> math.gcd(40,20)
2 {+ j; P) U+ v& W+ B - 20' Z0 U1 [) ] h
- >>> math.gcd(8,12)
_- }0 \; |* q. H2 ?3 A7 W% o - 4
复制代码
' F( ~6 x7 C9 }3 z2 omath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False- q2 L+ u. g! b( B9 H7 S6 W* ~
- #得到(x**2+y**2),平方的值
5 ~% v. I6 j. c I# W) [ D6 Y - hypot(x, y)6 K- p; l! ^# |& d; d2 U! ^
- Return the Euclidean distance, sqrt(x*x + y*y).
2 t) G* j$ m" Z7 @5 g$ O+ t% I6 I - >>> math.hypot(3,4)) g! `- B& V1 f* D
- 5.0
8 L4 @$ [( C+ ^; M2 Z1 l% E+ ~ - >>> math.hypot(6,8)
: A, F% N" R- w a( x5 e, e - 10.0
复制代码 & o: X( u! ]$ [$ l* d, E
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 w% }( t% g4 x5 Y- #如果x是不是无穷大的数字,则返回True,否则返回False
. |1 m# C$ n# F - isfinite(x) -> bool
* e$ Y$ N7 N8 I5 w& o0 V) R - Return True if x is neither an infinity nor a NaN, and False otherwise.
# V2 x% {' d- A' M- P0 @ - >>> math.isfinite(100)
3 H6 O, b1 K5 T3 l0 F - True
$ f5 b: h( z8 r6 j - >>> math.isfinite(0) @" m r& p9 s
- True9 q. i j6 R, r; V
- >>> math.isfinite(0.1)) J( _/ ~5 j N" S4 {
- True% {) l& m9 D1 e
- >>> math.isfinite("a")
; b( A% y, g. t - >>> math.isfinite(0.0001)
/ ], e, i j, H, P - True
复制代码
1 H" t1 ^* v: W; F+ T3 zmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False5 V2 S7 P L6 `# b! R$ E$ h8 K
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
: u* \0 {8 M7 d p - isinf(x) -> bool) l$ { R% @. t5 }3 i6 y
- Return True if x is a positive or negative infinity, and False otherwise.
: S/ f% h z/ @ - >>> math.isinf(234); p! }1 e8 T+ J( y: l( K2 z3 h
- False
* S8 @# C; V+ H3 z* C - >>> math.isinf(0.1)
# d, w3 v5 e+ w: { - False
复制代码
. h% R9 B' |8 W7 d& w2 gmath.isnan(x) 如果x不是数字True,否则返回False/ i* L9 N" [$ T: f1 ]
- #如果x不是数字True,否则返回False
9 T- ~/ S+ i3 s5 G6 | - isnan(x) -> bool
) H6 J" Z% f. P$ T8 Q J, R - Return True if x is a NaN (not a number), and False otherwise.
4 ^% x* o; O1 G/ D - >>> math.isnan(23)9 j! e& }) r4 D4 ~9 ~2 ?- ~
- False
' I4 n) @4 a7 l9 s: M1 T - >>> math.isnan(0.01)9 e" |& U# c' l6 r( S! J
- False
复制代码
9 l& P4 A9 }- z. Nmath.ldexp(x,i) 返回x*(2**i)的值1 U L2 O2 f* N \0 U% K
- #返回x*(2**i)的值
! e- l( \! J9 N U - ldexp(x, i)
' |6 B) x, v3 l. ^, m - Return x * (2**i).
$ t1 W1 i0 k4 G. Z) ~$ A - >>> math.ldexp(5,5)
3 M3 p. I0 T' q4 d! K - 160.0
; b+ U" ?2 h* I8 F7 d% } - >>> math.ldexp(3,5)' T6 ^3 o$ Y9 N t
- 96.0
复制代码 8 [4 ?2 D8 x" J1 T% M0 f. C3 k5 d
math.log10(x) 返回x的以10为底的对数" A' W h. ^' @; Y A3 Y8 T
- #返回x的以10为底的对数
) u# J& d: i" ] - log10(x). X" N/ O2 T$ t! D7 T! E
- Return the base 10 logarithm of x.# _4 i/ N' g: x! |, i- i6 z
- >>> math.log10(10)+ f% H! u) {+ U- j/ x
- 1.0
; m. @' Q* [5 v' q/ Y( T - >>> math.log10(100)) o* P3 i4 Q" W: @) P! ~6 C. |
- 2.0* I' n0 @: c; q
- #即10的1.3次方的结果为20" M( D+ g5 ?# j% R8 A! w
- >>> math.log10(20)
* X6 }* X2 e# q; u - 1.3010299956639813
复制代码 ( x4 U" ?! _& g6 ]
math.log1p(x) 返回x+1的自然对数(基数为e)的值; F7 w$ p g ]: N
- #返回x+1的自然对数(基数为e)的值3 Q; W1 Y0 K0 X6 ]& C
- log1p(x)
) i$ u1 \# h! s& H: b! N3 L - Return the natural logarithm of 1+x (base e).
$ [$ P; L$ L4 N7 d) z" C - The result is computed in a way which is accurate for x near zero.1 ^# \6 w/ W* N: \' J9 A& P3 U9 E
- >>> math.log(10)' r; D0 q" p r( \: ]6 W- }+ w% B/ x x
- 2.302585092994046
0 W w% K! ?, ^# L0 C - >>> math.log1p(10)& e2 q; h* f( Y+ M2 }- X
- 2.39789527279837076 G9 {0 T! F7 m, }$ V9 w5 M/ J
- >>> math.log(11)1 _7 X) c+ ]3 R
- 2.3978952727983707
复制代码
# F1 r% u) \; V2 J4 J, y- tmath.log2(x) 返回x的基2对数# j/ Z) Z' z2 R# S! G5 f
- #返回x的基2对数
) X, \' p4 i7 r" M6 q2 l6 { - log2(x)
. s; B3 G: V7 S+ o2 @' n; i/ A - Return the base 2 logarithm of x.' R, P# R$ J" f4 O4 k! M3 Z+ Y
- >>> math.log2(32)
' Z7 h& [# `; X4 G- f. i! l4 m - 5.0
5 J7 U5 w- K7 @" G - >>> math.log2(20)
; Q6 X O+ ?4 T( R - 4.321928094887363) u) m4 \# \' r$ F8 O
- >>> math.log2(16); P9 S2 a K. P8 x* u
- 4.0
复制代码 : v& {3 C( o2 T5 u# ~
math.modf(x) 返回由x的小数部分和整数部分组成的元组& }4 R0 J! v4 V+ g! S+ c t
- #返回由x的小数部分和整数部分组成的元组/ b: D+ f: f/ x% ?) J
- modf(x)
/ l8 }4 z+ N) ~ W0 f+ X- z+ Y - Return the fractional and integer parts of x. Both results carry the sign
5 l! O6 Q* a9 ~2 C. X) N - of x and are floats.
4 x8 p7 g. [0 R+ Y - >>> math.modf(math.pi), T; [+ D( w, V3 f2 }7 i, ^- O" F
- (0.14159265358979312, 3.0)0 b7 T: O$ ]- c" l% q! B
- >>> math.modf(12.34)
M4 I1 v) Z0 K! o1 O* ^ - (0.33999999999999986, 12.0)
复制代码
! W6 b5 |/ o' u$ x4 u* O- Omath.sqrt(x) 求x的平方根
" `. {/ t# J+ i+ Y K8 U1 X7 s- #求x的平方根
' S4 I/ H; c' b, ` - sqrt(x)
) P6 D. ~/ ^" P1 p% w - Return the square root of x.4 c' e' ?9 ~, y1 O1 H
- >>> math.sqrt(100)9 V" P, b( z( K' A+ j
- 10.0+ y# x% ?+ |: \) E$ O$ R9 K8 {
- >>> math.sqrt(16)
, D- S+ U( u: l - 4.0- @5 j, u2 N- ]' u3 v8 q
- >>> math.sqrt(20)
5 x5 n2 i. F' k! V# s9 }6 N - 4.47213595499958
复制代码 " o& s) r, i1 {1 N
math.trunc(x) 返回x的整数部分- #返回x的整数部分) W# A+ R' }9 U2 ]' w" D
- trunc(x:Real) -> Integral/ `1 [( v1 X I; h# L' r& Y) X
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
# h* B- r& [$ i5 G; v - >>> math.trunc(6.789)
+ v) A: S. V$ k7 l+ i7 K+ \% F - 6
& B8 C5 {2 B8 C" D - >>> math.trunc(math.pi)3 G0 [" @1 Y/ Y3 p6 H7 J$ n t
- 3
0 X: W& {5 _! t6 [( L - >>> math.trunc(2.567)' M1 A& G7 v7 u
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|