马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 Y( J7 U# s& a) I【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ ]" b( a. Q7 _' X3 Q, A9 _* u' [ r5 ^' o6 G
方法1:
9 S, }9 a% F0 H- >>> import math
5 w" o" ~2 K3 z3 w8 g3 c! A9 ` - >>> math.sqrt(9)& x+ x& J' N% D8 _5 [
- 3.0
复制代码 方法2:
2 s; X/ K( P" _- >>> from math import sqrt
6 e$ ]$ N" {: F - >>> sqrt(9)- M! b [4 R2 D
- 3.0
复制代码
3 y! q- x( }# N% v- `1 E* k9 H : w5 [* H. M5 J1 E3 m+ e: @9 }8 J
math.e 表示一个常量
0 C" E- Y9 E2 n5 z- #表示一个常量% L" Z( B2 k& _4 t0 g
- >>> math.e
) H4 K1 b( L8 s5 j. n3 H# I - 2.718281828459045
复制代码
1 D( h6 I7 i/ V7 |math.pi 数字常量,圆周率- B7 u) \* X3 I/ ^
- #数字常量,圆周率
8 O4 d# [$ x- m! B% g' H% E" F- @ - >>> print(math.pi)
0 }" K/ e2 y$ }1 V/ r/ W: E - 3.141592653589793
复制代码
) @+ P, _3 u6 M1 O% Lmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
# b* c' g8 b8 m9 D! e9 P# t- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
7 p: t/ G: q; [( f; N, m - ceil(x)+ a) |, M0 ^1 q) O9 \2 E
- Return the ceiling of x as an int.
$ u+ q& \4 [+ A2 m e' K1 d - This is the smallest integral value >= x.
/ x9 E- w: B* Q2 w( F, V% s/ T6 R P - 2 |" E( u, Z8 P% p; D% |: j/ n
- >>> math.ceil(4.01) B) J: H: e! s
- 5
3 f* f5 I: D6 ^$ O5 y - >>> math.ceil(4.99)* k1 t* H/ n/ N/ ?" Y. [9 J6 Z
- 5' V1 p* p) E8 ~* d3 F9 S# u2 W& h
- >>> math.ceil(-3.99)
$ Q4 y2 `8 y. [4 ? - -3
d( h- H: J* y* ]( g - >>> math.ceil(-3.01)
1 P' S5 U& A% I! H% X - -3
复制代码 2 c/ ?; y r3 x, R7 _# M, ?9 u
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ F: Y7 } F6 `* F7 m9 \
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 a* N5 h0 _# J3 @0 c6 b
- floor(x)
: a) t& x7 M6 Z( [3 N - Return the floor of x as an int.
4 D. x' s: w% F- N- |7 ^ - This is the largest integral value <= x.0 h/ u' b8 o' C
- >>> math.floor(4.1)- j' d I8 H% d, L
- 4
& x3 j, n, P' y' B1 B - >>> math.floor(4.999)* h% j1 H4 V! v' ^0 m/ A$ _
- 4* X3 d% Q) D5 @+ R0 ]* L
- >>> math.floor(-4.999) |3 S# u% w% i! I- x' R" D& d. C
- -5
2 z+ q( S$ z; U( Q/ I - >>> math.floor(-4.01). F" Y; d3 p) A" m
- -5
复制代码
9 V4 v' ~" \- Gmath.pow(x,y) 返回x的y次方,即x**y
& O% x% _5 q, T i, g& m9 T- #返回x的y次方,即x**y" j L5 \& r4 i0 }7 A
- pow(x, y)7 {5 g6 G M7 y Z0 m
- Return x**y (x to the power of y).9 ^8 P0 O$ u5 G5 w6 v
- >>> math.pow(3,4)
- [+ o, O- v: v4 _: e - 81.0
% |: u: l( g) P; z - >>>
8 Z# S7 t9 \+ P7 T7 h1 X - >>> math.pow(2,7)6 Y! w) z' e @& f( A$ ~
- 128.0
复制代码
$ M, ~) s% M: Z" {- d: i" q5 D# jmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 U, Z8 ^( @$ m6 i- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ x9 d9 `1 h# T+ m& u - log(x[, base]): R% T$ a% y" A' \0 e3 s
- Return the logarithm of x to the given base. C% d( H* @9 I$ }) f3 m
- If the base not specified, returns the natural logarithm (base e) of x.
% T9 ^- T3 x! h - >>> math.log(10)
T& ] S- b. s6 Z9 I( {2 L: r - 2.302585092994046
9 p4 V( u I3 U+ t - >>> math.log(11)/ p9 J, S0 f7 r ]! b" K
- 2.39789527279837073 Q; G* U& ~$ r( O2 d8 Q& h7 x" t. ?
- >>> math.log(20)
( @' N1 X) b3 A8 c, s - 2.995732273553991
复制代码 1 Q4 [: y" r! y: |
math.sin(x) 求x(x为弧度)的正弦值; F } h1 x7 i8 D2 m( t& h
- #求x(x为弧度)的正弦值
/ u9 q' K8 o) G1 J3 r4 K - sin(x)6 M/ A6 Q: V* D) A! K( ?* q; J* W
- Return the sine of x (measured in radians).4 l+ N7 e) u( k/ Z9 u' }5 B
- >>> math.sin(math.pi/4)# ~8 f/ G4 v7 @* t' B) F
- 0.7071067811865475& I4 Q7 J6 w5 Q/ a
- >>> math.sin(math.pi/2)
4 S, E1 u- |8 s0 M! \ - 1.0
3 x4 v: Z. y, P6 i6 M0 Z3 i1 {9 e" { - >>> math.sin(math.pi/3)
; j( B0 E9 i1 p' c) G# z0 b& G - 0.8660254037844386
复制代码 % B( u1 j- K* Z3 i/ X! T; b
math.cos(x) 求x的余弦,x必须是弧度# o6 c( E- j" `" z ^
- #求x的余弦,x必须是弧度+ b6 {) H. p- N( Y
- cos(x)
7 \# R7 T" n) D7 {5 w& s& e# p - Return the cosine of x (measured in radians).
$ f, M K9 T! M# C - #math.pi/4表示弧度,转换成角度为45度
X/ }/ `7 Z$ Z# j2 |2 J - >>> math.cos(math.pi/4)
. p0 Y5 X6 ?! F6 `1 z - 0.70710678118654765 q* I$ a% d- }; A; k A
- math.pi/3表示弧度,转换成角度为60度. _7 u' }. L# y9 _4 s7 C
- >>> math.cos(math.pi/3)
9 {6 w+ W' x A/ u - 0.5000000000000001
) j" z! S' J% ?. f$ F9 l - math.pi/6表示弧度,转换成角度为30度
9 p' v" V. s6 {; o - >>> math.cos(math.pi/6)
+ O/ H V# l+ d: t! T - 0.8660254037844387
复制代码 ' I: d% b# T' G }5 |
math.tan(x) 返回x(x为弧度)的正切值% U. {* T( O: v1 k, s$ O
- #返回x(x为弧度)的正切值: C0 {( m/ B# B/ ?4 Z' t
- tan(x): N9 H% [0 Q5 V" n) O- J
- Return the tangent of x (measured in radians).. }- X5 C# {/ Q" ?# O, u+ ?) o
- >>> math.tan(math.pi/4)
! I* k7 A2 Q! T$ n* d" i, r - 0.9999999999999999
: q0 O6 f4 n6 S. s; H, I& D& m4 q) G - >>> math.tan(math.pi/6)
- g# u$ T1 d# @4 t5 U - 0.5773502691896257
! k& W6 J/ K. t" \" |" L4 c - >>> math.tan(math.pi/3)& S0 a) `% P. s
- 1.7320508075688767
复制代码 6 w5 V& b- L; H/ n2 Q' c
math.degrees(x) 把x从弧度转换成角度
0 b6 x& l v( I4 u( C- #把x从弧度转换成角度
% i( w$ S3 f8 Q- X - degrees(x)! l5 F# Y4 \ {! |: \, F! e r
- Convert angle x from radians to degrees.
7 r5 {4 j! |, g+ F' l; } L
: l6 R' v5 e2 G, h4 E& X. X- >>> math.degrees(math.pi/4)0 ]) Y& D' o9 R/ X
- 45.0
. X) D! `0 g: N( a2 A - >>> math.degrees(math.pi)$ [5 i/ I- |8 R0 y& x- E2 _
- 180.0
# c9 j0 @! H# X6 r( ^ Z% X9 }6 Q - >>> math.degrees(math.pi/6)/ {! B+ V' a4 K4 c8 z. I
- 29.999999999999996
- z* G9 |1 a" k% Z9 Y2 P; g1 e - >>> math.degrees(math.pi/3)
V) |, W$ ]; X; b - 59.99999999999999
复制代码
- n4 v: j: v( b7 J7 e2 ?1 }; L5 w( Hmath.radians(x) 把角度x转换成弧度6 @1 h0 g, H8 ~
- #把角度x转换成弧度
' Y4 J! B1 K c8 b6 e8 t; x4 h - radians(x)
7 |7 |4 `! v; Z, _9 y0 y- Z - Convert angle x from degrees to radians.
% e" l$ |' W% W6 A - >>> math.radians(45)
+ {4 o; R. a h - 0.7853981633974483# _ F3 w1 Z! k- H0 O: y
- >>> math.radians(60). ? d! J9 P3 a* M# J2 J
- 1.0471975511965976
复制代码 + S$ b5 F: f7 M2 S. _9 C7 W
math.copysign(x,y) 把y的正负号加到x前面,可以使用0! V$ j6 u3 Q* h0 F: g
- #把y的正负号加到x前面,可以使用0/ |; |* s4 t3 W" v5 S
- copysign(x, y)6 g" G& F/ f8 A* r" s" u( [
- Return a float with the magnitude (absolute value) of x but the sign 3 l D; i3 Y8 s/ h0 F1 U
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) B- D$ l! X5 p9 [. N2 h* e
- returns -1.0.
/ }( ^ H1 R1 ]+ S- H4 s7 e3 A! B3 [
# r5 G& e" G: n/ v% z7 R' R- >>> math.copysign(2,3)
( b/ D7 O8 I: n7 T. N( i- i: Y - 2.0
- \/ v; I% ]/ u- L+ N. L - >>> math.copysign(2,-3)
0 l3 \. q- D9 y6 `; |: E - -2.0
: L" \; b; i9 o6 V( P7 T - >>> math.copysign(3,8)
" A* M& d7 r* K$ Q* P - 3.0
* M) J" [" r& n( r3 r ]. V - >>> math.copysign(3,-8)
% @" g; I$ {3 e, _ - -3.0
复制代码 8 A _8 D. |8 v7 K; `
math.exp(x) 返回math.e,也就是2.71828的x次方
( ?9 h% G( C0 ~, L8 I; s. K- #返回math.e,也就是2.71828的x次方
5 l8 X/ I1 N! y - exp(x)
. V) o( w( S/ w! r8 V - Return e raised to the power of x.
w$ I% c8 V: x- K - 0 S/ Q4 H) E& P; z9 _9 |
- >>> math.exp(1)
# V) }9 V* \# A/ h$ k - 2.718281828459045
1 f/ u, ^. |7 y9 ^2 L2 Q - >>> math.exp(2)* B c: L% Y" e+ G
- 7.38905609893065
! a& w1 W9 {& X% ^7 E; i3 i2 P - >>> math.exp(3)0 D, d8 {& `1 D( c: Q* r
- 20.085536923187668
复制代码 4 O0 u' e4 m+ B
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
5 P6 A$ E0 E) O/ |- #返回math.e的x(其值为2.71828)次方的值减15 ^, p0 k" `- @9 C( T4 T
- expm1(x)" P3 x9 k) S0 U" s9 e
- Return exp(x)-1.
$ O; L8 ?6 z9 Y" a - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.1 D9 o8 G7 X: ^( u
- $ ]# I, [6 K! p) p
- >>> math.expm1(1)7 Z4 \2 h- |) E$ ^: R6 P( W
- 1.718281828459045
5 B; ~4 u: P( t1 R* Q8 V - >>> math.expm1(2)
/ g+ K" E; n+ ~+ D% i1 l1 V0 `# N - 6.38905609893065
8 t7 l8 y% n, r8 |4 T% ^+ ? - >>> math.expm1(3)
0 `# p( o- a7 W: m" L, d. |) r - 19.085536923187668
复制代码 $ W" J4 ^; P2 ~+ `5 {8 ?% y
math.fabs(x) 返回x的绝对值0 V' m2 [5 o( ]4 u+ l. ~
- #返回x的绝对值. W5 N" ]6 \& g% C; B' e/ E9 I `
- fabs(x)
% C% H, a& _' b3 f - Return the absolute value of the float x.; Y6 F; R4 {$ m G
! T! T/ L1 Z y- >>> math.fabs(-0.003)- Q/ G6 l5 h. s0 r7 V
- 0.003
' {) \' W' t# N$ K - >>> math.fabs(-110)' O. O5 c E% F
- 110.0
- P% V4 F! ^9 j6 J! h) o8 b' B0 H% Z - >>> math.fabs(100). a1 W; k: Y, s; r6 y. u
- 100.0
复制代码
4 s) q1 L% C( {7 [) v6 |1 ~math.factorial(x) 取x的阶乘的值
. I. b1 J/ a9 `4 o* Z- #取x的阶乘的值
- h5 j! w x2 A6 I9 F. ~8 V+ m' {; g - factorial(x) -> Integral/ M/ s& y! f% [0 N R5 a
- Find x!. Raise a ValueError if x is negative or non-integral.! p/ k5 F( c& H T
- >>> math.factorial(1)
7 K/ |( q% \7 Y) m+ H8 o; ]: v - 1
5 ] [8 [+ \8 r - >>> math.factorial(2)
2 t: J2 \. c' ?% p - 2: }4 {- n1 A! g4 B9 o R3 ]
- >>> math.factorial(3): | k1 Q) V4 }5 w+ x. O
- 6
" e0 b; |/ V" O/ ?' m! v - >>> math.factorial(5)
* m! _, d7 i9 ~& R4 |" z - 120
1 m. l, v$ e {) w6 P# Z - >>> math.factorial(10), f a6 B' o/ m* p
- 3628800
复制代码
* b7 B" H& D9 j5 m3 Lmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数6 k" |& f/ U/ @; I( X7 @
- #得到x/y的余数,其值是一个浮点数
8 l" G( P8 l2 X; G3 @0 z) A( \' y - fmod(x, y)
e$ x3 y" d# z% p, Q* l7 ] - Return fmod(x, y), according to platform C. x % y may differ.& v9 O9 n8 c1 \ i }
- >>> math.fmod(20,3)
5 y1 K' a7 \) O6 b3 [( I# B8 T - 2.0
# A6 U- f3 v% G, u* {9 F) b - >>> math.fmod(20,7)
' c0 A! g9 S. `' Z- E5 U - 6.0
复制代码 1 x7 r2 r: i# x* l1 N
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
# c( t; O) a. y" i0 m) {: A- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% c9 @- o \6 ~( [
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
1 w2 ~+ u# l# ] - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
& p& I1 [4 d5 S2 s/ } - frexp(x)
, y' R6 w. i* H. } - Return the mantissa and exponent of x, as pair (m, e).5 A c% m3 H+ \" j) A7 r
- m is a float and e is an int, such that x = m * 2.**e.
5 a4 ^" V) [ Q7 w+ K) E9 K - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
! u J$ F& A1 H* E/ | - >>> math.frexp(10)
* B" B: p3 J" j3 F$ C6 L - (0.625, 4)
9 a3 E( Z% f' ?: o: N+ Z - >>> math.frexp(75)
' m( Q! w9 f# b3 P1 R4 l5 W - (0.5859375, 7)
$ N- i% C7 Y8 m9 X) K, K( B+ s - >>> math.frexp(-40)1 k! x: {$ i9 i% j1 M
- (-0.625, 6)7 Y2 h4 q; b& ^8 h3 @
- >>> math.frexp(-100)+ s( X7 l' ~' z" X L' @1 x% {
- (-0.78125, 7)
3 l+ Q0 Y7 a# h7 A! H - >>> math.frexp(100)
v4 W, f* Q; j, B - (0.78125, 7)
复制代码 8 M, q; o5 F: V4 m# C5 ~
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列). f/ d7 S# d: n+ ?/ ?+ }- n
- #对迭代器里的每个元素进行求和操作% S6 J+ ^) d( ^3 y3 ~
- fsum(iterable); T1 }% p2 {. Z
- Return an accurate floating point sum of values in the iterable.
( C8 f* ^! m" S Z7 g) o - Assumes IEEE-754 floating point arithmetic.4 {2 g# d1 f+ s: u& l3 ^
- >>> math.fsum([1,2,3,4])
; c. [+ U& Z- q9 E3 E - 10.0( C1 k3 K% q R5 H4 s' \& B r
- >>> math.fsum((1,2,3,4))# m9 W: x( `$ M/ N
- 10.0
4 u4 N5 a7 X* t& s - >>> math.fsum((-1,-2,-3,-4))
* i) L9 U$ p) i3 D - -10.0, z ~# |; ~, a" I
- >>> math.fsum([-1,-2,-3,-4])
. A1 P6 |; t& F- d4 d - -10.0
复制代码
' O( `( r% v, _( H, m- c3 Z4 Amath.gcd(x,y) 返回x和y的最大公约数" `6 g/ N0 u/ G# ?
- #返回x和y的最大公约数
. `1 X& h2 r* f' H4 F - gcd(x, y) -> int
) t- x0 [8 m( [0 W, e( g - greatest common divisor of x and y/ v: E( |: D* U q
- >>> math.gcd(8,6)
5 o8 }& j3 J; D ?. N - 2+ A# b% R2 z @$ d9 ]( R3 D3 X
- >>> math.gcd(40,20)8 U9 V+ D! P# g, v
- 20
/ ]8 [: U+ R3 i# ~/ m - >>> math.gcd(8,12); E* m* ~! J6 t/ E
- 4
复制代码
6 g3 E+ s) K8 X6 [+ F6 Rmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
5 x' l% O( z( m; E- #得到(x**2+y**2),平方的值
/ g7 r0 d! {0 T0 n - hypot(x, y)
& s! K$ V$ Q9 l3 o8 @* F - Return the Euclidean distance, sqrt(x*x + y*y).0 A9 X" x W2 D% ~- q- `
- >>> math.hypot(3,4)
% H3 y1 g# L1 o: e7 C7 d- c - 5.0. l# i. L+ Q& J- V- p
- >>> math.hypot(6,8)
4 F. \9 ~4 D. x0 ]6 ? C - 10.0
复制代码
7 X6 Z/ |- V8 U: B) Z/ smath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
( E- q5 s& w' T% p& n6 J! e& w- #如果x是不是无穷大的数字,则返回True,否则返回False9 T# e! x. ?* C) \/ ?$ n9 L: g
- isfinite(x) -> bool0 Y7 |0 q7 X- T7 } I- v
- Return True if x is neither an infinity nor a NaN, and False otherwise.2 X2 z4 J2 E# M0 k
- >>> math.isfinite(100)) E) G" ~" A4 r7 U7 o M
- True
( t% A+ N- j8 _- a; L - >>> math.isfinite(0)& X) O7 K. P3 I! C% m q7 p
- True9 J* d) b# @1 k+ H) c b: A- I
- >>> math.isfinite(0.1)0 @/ r& P0 E$ p+ U6 c& L( v
- True4 J8 a' n' D( P- H
- >>> math.isfinite("a")/ _- U6 z2 E7 G1 G) X4 j0 x+ W
- >>> math.isfinite(0.0001)% Q3 y2 b9 X. N# Z. K$ ?/ \
- True
复制代码
. b' C" Y( c7 V) t- vmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False. M! H8 W- S5 c/ u7 s6 D& u- [
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
* Q$ ~9 u. Q1 B+ P5 S J - isinf(x) -> bool' H. R4 |! v2 e+ H5 o4 ?
- Return True if x is a positive or negative infinity, and False otherwise." A- I/ s- z. ?; B- U, W
- >>> math.isinf(234)
" Y6 }* x4 Y; \ - False2 M& V- r2 d3 H. p/ u5 U3 V9 @
- >>> math.isinf(0.1)
3 j* [ ^7 k; x" ?: `/ f - False
复制代码
; j9 g# Z; d+ t% g* G% Q emath.isnan(x) 如果x不是数字True,否则返回False6 k# I4 c: W8 C. L* t
- #如果x不是数字True,否则返回False7 F0 R* S+ p! |. B' j
- isnan(x) -> bool
5 C& _* {) e4 I! ?+ m( j( W - Return True if x is a NaN (not a number), and False otherwise.
& h+ E `# P9 J6 L) d - >>> math.isnan(23)
2 L) a9 d! ~9 k, Z - False
- h6 F' X6 [% L/ U3 t8 d4 e - >>> math.isnan(0.01)4 w% }# R; C9 w* d
- False
复制代码
1 w+ Z- _8 z1 e& F# ?math.ldexp(x,i) 返回x*(2**i)的值% M9 g5 n/ ]3 F
- #返回x*(2**i)的值$ _0 |& n! B; ^
- ldexp(x, i)/ v3 Z8 w3 W' e" e
- Return x * (2**i).7 n: ]. W) L* U' ?/ e
- >>> math.ldexp(5,5)
) I% H& U4 B* A - 160.0( [% i/ R+ P6 }6 L" [
- >>> math.ldexp(3,5); L) m4 g& u' f e6 Z+ m
- 96.0
复制代码 " A8 r @6 S- _5 _/ `- a
math.log10(x) 返回x的以10为底的对数! N( n6 Q) G7 M
- #返回x的以10为底的对数% }& U' ?- y( ? z/ s1 }# N. T `
- log10(x)( w* F) m2 {# J" q3 F1 m
- Return the base 10 logarithm of x.
. c! x6 N: y; ] j2 Q1 h4 n5 k - >>> math.log10(10)
; x2 H: G8 z6 H. B) G# J5 Z, M. S - 1.0
' M1 n+ Q" T3 {1 m% n - >>> math.log10(100)8 Z( e! v3 e: [0 \. J2 {
- 2.0
. s. L7 g3 R$ U9 N - #即10的1.3次方的结果为20
# @: x1 R( a* a - >>> math.log10(20)
% [( \$ b4 {8 i - 1.3010299956639813
复制代码 / ~. n% y- J/ B: o6 k* j$ g& P; {
math.log1p(x) 返回x+1的自然对数(基数为e)的值! l% |8 o2 ]4 Z# D( t" R
- #返回x+1的自然对数(基数为e)的值. g1 m8 Y/ Q5 g- |4 O1 A
- log1p(x)
" A" c8 w& e. Z; y - Return the natural logarithm of 1+x (base e).
: L8 ^& d" }- \) y5 o - The result is computed in a way which is accurate for x near zero.& ^4 d: M5 s3 C* o; L9 T2 \7 w+ b
- >>> math.log(10)
# z) l3 }- d; ]- i - 2.302585092994046
' _" z: ~& P: k M( ` - >>> math.log1p(10)' v& U/ ~2 K$ A( _- R
- 2.3978952727983707
: I7 g; ^7 a4 [$ G - >>> math.log(11)8 x) p0 ]8 Q2 ^9 ^+ L
- 2.3978952727983707
复制代码
6 H; F6 |& l3 a5 r& Lmath.log2(x) 返回x的基2对数8 @0 [8 K1 m$ p- k: S7 O- i
- #返回x的基2对数0 G3 f. n" Q3 ]) W$ p6 I1 W
- log2(x)0 G7 E4 m; B5 o; U
- Return the base 2 logarithm of x.
7 L6 O& Y1 T/ W+ v2 j$ m8 f s - >>> math.log2(32)
L- r. t1 N0 W2 z8 O& d w - 5.00 X' D4 _' j4 h2 k! t2 F
- >>> math.log2(20)
( \8 N, j. R$ p2 R/ y% [3 U - 4.321928094887363, q( C4 X1 c- x& j8 Z: c
- >>> math.log2(16)' R! r% S) H5 Y. W( U7 `3 X7 C
- 4.0
复制代码 * e: W. O+ H( N0 b N% { b2 p! D+ t
math.modf(x) 返回由x的小数部分和整数部分组成的元组
. c/ E( b3 n9 l" y: ^) D; Z6 z- #返回由x的小数部分和整数部分组成的元组
* g, V7 Y, w7 @9 n - modf(x)% Y7 [6 T& s+ b5 `' f
- Return the fractional and integer parts of x. Both results carry the sign
% l! ?6 {, H$ ? - of x and are floats.
' P" o! b; Z# o: _2 g - >>> math.modf(math.pi)6 k1 V. I% J3 ^
- (0.14159265358979312, 3.0)3 @0 d' |! A8 f& U9 Q, _: J
- >>> math.modf(12.34)8 w' _# A) r+ V- j$ b* p
- (0.33999999999999986, 12.0)
复制代码
; `( N: y1 S# l: ~ K6 bmath.sqrt(x) 求x的平方根
* y2 Q; n. l2 P! {# q- #求x的平方根: f2 \' X! D& C2 n8 \, R7 `
- sqrt(x)3 X1 c" D+ I% O: [ J7 n9 [: B( W
- Return the square root of x.4 V0 o/ r3 P; |8 K
- >>> math.sqrt(100)
4 _; A, V/ A2 M7 n- d+ L4 v, _ - 10.0& c, p! H& G+ i+ y: ~" \
- >>> math.sqrt(16)8 w }6 E, o! j; I+ Q, U8 O2 h# \. q
- 4.0
7 D6 a2 y ^" @' c( U' W - >>> math.sqrt(20)0 u* O1 g1 x' H, o d' `
- 4.47213595499958
复制代码
/ j+ Y' ^ u1 h6 Y3 |2 ]! Pmath.trunc(x) 返回x的整数部分- #返回x的整数部分; {8 N; W" s, y- u7 ]% Z. n6 C
- trunc(x:Real) -> Integral2 V- i: U/ D& \) O7 c- |( h
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( F# \' F( }. F2 ^9 L) N
- >>> math.trunc(6.789)$ x: G; F3 C) D. k, J0 z) U
- 6
. \3 L4 u, [& Y b6 O5 l: _, w - >>> math.trunc(math.pi)
+ f& v' B" F. W0 l9 Q - 34 R' O# h5 j% C* y6 A( h+ e, o5 t
- >>> math.trunc(2.567)
* n; w+ a5 }' U! M4 R" V2 p - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|