马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
% S1 @' o1 i/ Z' ~. A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ _/ h1 ^9 [+ B9 l3 M i- P( z* u# r; `, G
方法1:
/ ?. f: m9 E. o. b- >>> import math! [' `! k& p! W' G- D* `9 S1 W* g
- >>> math.sqrt(9)
9 x. z: S! ]. S4 \7 l - 3.0
复制代码 方法2:
8 b+ P9 R5 s+ N8 C) B- >>> from math import sqrt4 s2 ?; r% ]6 {& ^+ S4 v3 ~
- >>> sqrt(9)
0 w! p2 Z Y" F - 3.0
复制代码 - I) G/ \5 N$ Z8 o7 l1 @
) @) Z, \ {$ @% L: \: nmath.e 表示一个常量
4 ^+ U! G; t ~ r- #表示一个常量$ }; }- Y5 w' | E* n; M& j
- >>> math.e* W9 f! `" ?8 x5 {
- 2.718281828459045
复制代码 9 a$ D1 v/ I% f
math.pi 数字常量,圆周率
9 v2 f( X. w" C4 O- #数字常量,圆周率
7 v9 ]& K# f+ Z* k - >>> print(math.pi)9 D% K/ Z9 X& _2 O7 z, i
- 3.141592653589793
复制代码 - V3 T0 k: h. r7 _4 y
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x* e3 z8 D2 F, R& `% o
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
, L( ^$ H2 L& Y/ } p - ceil(x)' o' J7 m4 c' d5 S Z6 b% I
- Return the ceiling of x as an int.$ J9 ~) U9 ?3 k* y- q8 w
- This is the smallest integral value >= x.( a9 m- N7 t! f" i9 s
- # s" W( F, H; `# ^' U6 v" t/ o7 r9 G8 R
- >>> math.ceil(4.01)7 p) {- F C0 n. ~" `
- 5. s' [7 I# V% F: R8 ?2 F; z. t
- >>> math.ceil(4.99)* y0 a! r3 w7 L* k
- 5
& r% J8 E' x5 H; w( \ - >>> math.ceil(-3.99)! l' r' o& E6 o. W
- -3( z/ d T# e) @# S+ @* n
- >>> math.ceil(-3.01)* d3 S1 N9 w' h0 R1 T
- -3
复制代码
0 j f( V( ~) ymath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 @+ f0 A" N, t/ Z; p1 q" ~5 w- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 l! l2 l5 [( o
- floor(x)8 I. j3 W- C) a3 x
- Return the floor of x as an int.
! h2 p, O [# E$ U. t( X& ` - This is the largest integral value <= x.
V" @3 E& L. H# d/ y: A! h - >>> math.floor(4.1)
! C: J( \* P2 _4 C; S4 h - 4, _$ S1 J, F# L! w [
- >>> math.floor(4.999)
: M4 C" D% p' K: Z7 F8 F5 n - 4
* i0 G: W1 |6 y. N2 l7 D - >>> math.floor(-4.999)
0 v; G) X6 }7 [- ~ - -5
+ J/ v% u5 X: n% b! n: W; g - >>> math.floor(-4.01)
/ t& V( n$ o0 g1 r$ u7 ~ - -5
复制代码 ) t. A# s' ?) I' B0 n$ c+ G i: x+ v
math.pow(x,y) 返回x的y次方,即x**y
/ t, w1 ]" r" t& o- a- #返回x的y次方,即x**y5 x* L+ C8 ~# M" H
- pow(x, y)# C2 S$ Q5 l" k$ p* D: H
- Return x**y (x to the power of y).
& ?) n M% V$ A% N/ n7 @ - >>> math.pow(3,4)
( l+ N5 h6 z/ G6 | - 81.0
! W2 n4 d" B* {% E" ?% [ - >>>
1 ^ b1 }( p8 y2 Z* @: @ - >>> math.pow(2,7) Z! T( X( t1 x: ^7 ]+ Y l0 r% g
- 128.0
复制代码
* d1 F6 `+ ^1 Mmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 ]+ K( w; o; D& J
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ p8 B3 V8 w3 r$ X% _6 Y
- log(x[, base])9 l k" @ W, _# V: A
- Return the logarithm of x to the given base.. |6 F+ u" I% T+ f: c' a
- If the base not specified, returns the natural logarithm (base e) of x.
1 D( o W m& F9 E) S, M4 } - >>> math.log(10)+ |# f4 m( p1 c3 s6 \
- 2.302585092994046! S1 v, ?! G! @
- >>> math.log(11)
( ^4 Y$ n# A; H9 l4 C: I' U - 2.3978952727983707
- B/ [( q: L$ f# W - >>> math.log(20)
5 x1 ~9 P4 R7 h - 2.995732273553991
复制代码 - Z9 W8 J( ]5 u9 q* {7 \
math.sin(x) 求x(x为弧度)的正弦值
$ u1 ]/ y9 A# C2 n* {+ o3 Z- #求x(x为弧度)的正弦值4 B# k* u) J3 R, y; C( W4 K
- sin(x)
5 L% ~4 S6 T% E% K# q - Return the sine of x (measured in radians).
( Y4 b, S) q5 F$ k& g; e - >>> math.sin(math.pi/4)
. e4 v: p+ |( i0 y; L( k9 M5 A) \ - 0.7071067811865475- ]9 G6 T$ b# F L' h) ^+ s
- >>> math.sin(math.pi/2)
/ I# c/ Z. d+ S3 C* P - 1.0
0 u/ w. _# }# ^+ Z1 ~ k; L - >>> math.sin(math.pi/3)
# r$ {2 x& B$ ]! ]; d - 0.8660254037844386
复制代码 8 C2 u+ J, f( ^3 C1 s7 w
math.cos(x) 求x的余弦,x必须是弧度, }( ^. l# e+ a2 C5 b$ b2 A% S
- #求x的余弦,x必须是弧度5 P, M! u; R) u L$ N
- cos(x)4 A j: E/ f6 t8 p
- Return the cosine of x (measured in radians).
- C' R/ b9 M" m* I3 W3 d - #math.pi/4表示弧度,转换成角度为45度
1 p2 y& w, V; q$ K2 v: h, Z - >>> math.cos(math.pi/4)
# p) o: Z5 e) @7 p7 T9 }! } - 0.70710678118654760 N% C' @- {4 _ w+ X6 W3 o
- math.pi/3表示弧度,转换成角度为60度8 R% Z* i! J" m
- >>> math.cos(math.pi/3)
: {0 y# T% C+ G- @: K - 0.5000000000000001
5 y6 p/ H" D, H l1 O - math.pi/6表示弧度,转换成角度为30度/ z# r% n: t6 N* g1 h
- >>> math.cos(math.pi/6)& ~5 {( e/ l! e2 p* Z* H/ C, R
- 0.8660254037844387
复制代码 ! P! E2 x: y( ?% N* t8 ^
math.tan(x) 返回x(x为弧度)的正切值, y# L6 \9 s0 a- `# ]" J8 F' F# l
- #返回x(x为弧度)的正切值
0 g2 P( _. L7 D4 l# a7 j2 X - tan(x)2 Z$ c7 ^3 P2 G0 e) @7 v' }
- Return the tangent of x (measured in radians).
5 |4 L: b M1 j; U4 { - >>> math.tan(math.pi/4)2 I ?6 f5 F r, q% u! {1 ~
- 0.9999999999999999: `) v8 e, W/ e9 g6 d: @1 k
- >>> math.tan(math.pi/6)% {# I+ f7 {9 t; G9 w
- 0.5773502691896257
' o9 f/ v: E1 L" y8 X8 L - >>> math.tan(math.pi/3)& [' c- @' U. z* S! Z/ q
- 1.7320508075688767
复制代码 A/ \% }; U" v3 C; n
math.degrees(x) 把x从弧度转换成角度
$ F, N2 L! }8 `8 X, K! W$ r, e- #把x从弧度转换成角度
+ E& ?8 l$ V, s# m; R* v - degrees(x)* Z7 j$ E+ Z: |, _
- Convert angle x from radians to degrees.
" i' o# p7 O* `+ S( R
) o0 [' o3 q" F1 r, q( ~1 y- >>> math.degrees(math.pi/4)
$ }) _- Q3 p3 V, D - 45.0
) \ t2 b: `& I2 {$ b7 C - >>> math.degrees(math.pi)2 P! K) M8 ]4 y1 B
- 180.0
: N2 C, `6 G# a/ q! H - >>> math.degrees(math.pi/6)
+ h; M5 |% C8 ~) Y - 29.999999999999996
# S9 ]; H( ?& r# Q! Z - >>> math.degrees(math.pi/3)
& h# ~3 i, ^* d$ k - 59.99999999999999
复制代码 ! W* S7 ]0 @1 N0 K) p
math.radians(x) 把角度x转换成弧度
$ } P3 Y' s( C; {/ N% A4 R& Z- #把角度x转换成弧度0 e q: w* _" G; O8 t8 T
- radians(x)% {9 p8 `+ M2 ]6 @( G* |
- Convert angle x from degrees to radians.
5 c) \6 s6 j& a( y/ V - >>> math.radians(45)
) P7 M# _! a4 _ - 0.7853981633974483
( z$ x+ g6 P- } - >>> math.radians(60)
5 N" k2 ? }% r1 p7 S+ N0 z* z - 1.0471975511965976
复制代码 ; F u) `1 ?9 Y; X: Q' P
math.copysign(x,y) 把y的正负号加到x前面,可以使用05 X E+ }' B8 Y' F
- #把y的正负号加到x前面,可以使用02 k6 l+ a1 Z0 G: E5 s! `6 |
- copysign(x, y)1 c, ?7 `- }8 t7 Z
- Return a float with the magnitude (absolute value) of x but the sign
5 u, \6 _ E1 v/ d# `* ^ - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 B8 e! F- D5 a+ D$ K5 L" V) t - returns -1.0.
/ o; E0 D* B0 g/ c5 f1 I1 b - 2 y& P: ?( o. R
- >>> math.copysign(2,3). s5 v' F* v5 d9 e; S- P4 d
- 2.0
. X4 w# B1 a7 X! s9 `2 V - >>> math.copysign(2,-3)
( s7 [" v; k2 Q2 K& n - -2.02 K* P; O H. g0 G
- >>> math.copysign(3,8)# ]$ |" z& E; n
- 3.01 @ o! [: H- D3 B
- >>> math.copysign(3,-8)
# @$ U, E# L; L - -3.0
复制代码
" t: T, u# }& I2 @$ gmath.exp(x) 返回math.e,也就是2.71828的x次方
6 n5 j# h8 B0 R# K# v3 P/ J! }( f6 Q" L2 c- #返回math.e,也就是2.71828的x次方1 d/ `- [( l- g" V4 Q/ Z+ y
- exp(x); l" k; Q/ p" q9 L5 ?
- Return e raised to the power of x.
8 ^$ @- \& i' }' t# ]" N7 Y - 1 {+ \9 e: R' _0 y4 u/ {9 o# m
- >>> math.exp(1)1 d4 \' H; f. q& E: Z( n5 z+ c
- 2.7182818284590458 R5 a3 Y; x S6 a" q
- >>> math.exp(2)
# N* K! ?3 e% |- O% q2 G - 7.38905609893065
5 p& B, ~3 h+ ^- Q; x - >>> math.exp(3)9 D: K' C8 b; c
- 20.085536923187668
复制代码
4 e- } L5 Y9 u1 P- t9 w zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1' T1 u. A$ H% x5 `7 c
- #返回math.e的x(其值为2.71828)次方的值减1& @6 u2 Q; d1 P9 h4 y* N T
- expm1(x)
, X& l- ]4 g3 a4 ^, Y- q - Return exp(x)-1.5 u* e$ b. S. J; f
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
; c- ^: n7 [+ ~
, `- g+ k( {4 m ^- >>> math.expm1(1)
( F) j" d( K" G6 P+ ?) }5 b! c. j7 y5 u, b - 1.718281828459045
6 g9 j% X+ s: j) \/ P! @- {" N - >>> math.expm1(2)% S! V) z+ f6 L4 N/ Z/ O( m/ f
- 6.38905609893065% L5 L5 @5 J3 M. G; w
- >>> math.expm1(3)9 \$ _7 B! G; ?$ V+ x
- 19.085536923187668
复制代码 2 L1 p/ }$ O9 r3 {, v9 p( f! d
math.fabs(x) 返回x的绝对值' H; {! j. P1 Z3 f7 Q
- #返回x的绝对值
4 B. Y; Q- L) A2 K: V2 d - fabs(x)5 r) L7 R, Q4 l" f0 o b3 o, a6 z
- Return the absolute value of the float x.
2 Y9 }+ t1 r2 V* k0 [ - ' P# p$ J& R7 ?! r
- >>> math.fabs(-0.003)2 I F- ?2 F0 i6 w9 A2 n* E
- 0.003' _- e6 f8 l+ M9 c$ y9 v
- >>> math.fabs(-110)- J5 {1 _. @1 H4 G9 I6 c# a
- 110.0
! c9 z; ?; I4 [7 r2 n3 S6 P - >>> math.fabs(100)) W6 W/ @- T m& d
- 100.0
复制代码
1 y( e: z2 Z- amath.factorial(x) 取x的阶乘的值/ B% Z2 Q% p9 z1 B) I/ q9 X4 ]
- #取x的阶乘的值
/ H$ Y8 O, D$ x4 I0 m: x - factorial(x) -> Integral6 s7 n) p6 u6 ]) D7 n
- Find x!. Raise a ValueError if x is negative or non-integral.6 t- P/ {4 Z& j* i: _6 w, w
- >>> math.factorial(1)/ I& f; H$ E" a% W5 \# @. J
- 1, j, l" ?' P7 c6 Z; K
- >>> math.factorial(2)9 `' ?2 u6 j- j' y5 j' L
- 2) x. A% z% n# y( d$ h
- >>> math.factorial(3)0 E" b5 E. a2 k* o
- 64 f/ j* Z, w7 N) y3 W: k" ^6 G
- >>> math.factorial(5)
2 I# Q. C6 g$ U; k3 { - 120! s! C9 ]' l( a/ Q& x1 q8 x
- >>> math.factorial(10)
3 R) ]' F8 W! V. B( M0 J - 3628800
复制代码
( X2 I3 o, J6 c% r# S5 Qmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数/ g* w& _9 ?; I& g" a
- #得到x/y的余数,其值是一个浮点数, ~3 x' V, i( g, X
- fmod(x, y)
$ S3 Z5 T% I7 S1 s- j0 n& K - Return fmod(x, y), according to platform C. x % y may differ.
$ |0 G% K4 o1 L - >>> math.fmod(20,3)
: b, q$ \9 U" v4 A9 H - 2.0
8 y4 L% b) p5 n P$ ]6 } - >>> math.fmod(20,7)+ Q; j! ]. H0 Q' \* B! {
- 6.0
复制代码
3 h4 J4 K7 v& t P$ k! _8 jmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围% s8 o& {5 K: ?$ h+ Y* p+ I
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,& ], t0 R) E$ _2 m7 a3 G
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
& t9 B- a$ J8 b; J - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
4 t0 B& j# K% t; O9 U5 C - frexp(x)
9 S( F7 a- e: v - Return the mantissa and exponent of x, as pair (m, e).) `# [: [, X, u* Q8 _6 w
- m is a float and e is an int, such that x = m * 2.**e.
4 J# ~8 P4 O7 v* k6 u( r - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
; I( b6 ~: k: M. q: j1 n) ?1 B - >>> math.frexp(10)8 v, z( k: g% E+ k
- (0.625, 4)
( c0 i# Z5 N+ L/ O- b% M - >>> math.frexp(75)# ?9 q4 ~ I/ Z" F. X, y
- (0.5859375, 7)
* z3 v6 }3 D! F* d - >>> math.frexp(-40)
( d' M/ Y. L1 _. [/ z/ h. Y" Q - (-0.625, 6)' a* J8 H% d( ^- ~0 n5 q3 A$ W6 U2 F
- >>> math.frexp(-100)
# p# Q3 D# a; L8 d! E - (-0.78125, 7)9 ]8 v2 q' F* m' `+ L2 {
- >>> math.frexp(100)
: `8 ~+ l; @7 E3 v' ^; b - (0.78125, 7)
复制代码 1 D9 d5 R" W2 X3 [5 h5 D
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列): [2 `6 C5 Q5 g R
- #对迭代器里的每个元素进行求和操作
! N- d& O$ y/ I A) ^5 O - fsum(iterable)# W/ x) @% Q4 S4 e
- Return an accurate floating point sum of values in the iterable.4 y6 C# b9 K8 l i9 }
- Assumes IEEE-754 floating point arithmetic.2 o2 L3 p& X: I. Y
- >>> math.fsum([1,2,3,4])# S* }* _9 p7 W. n% Y8 s- d
- 10.0
5 w! v( r0 d5 {) b' D - >>> math.fsum((1,2,3,4))
+ B0 l" {! z4 Q - 10.07 ]" o7 ?. R( \, E7 s( r
- >>> math.fsum((-1,-2,-3,-4))) l3 T! u$ [3 |% R4 ~+ P, a3 t* I
- -10.0
( s6 X X/ l: I3 h4 X7 I7 S! i - >>> math.fsum([-1,-2,-3,-4])
+ q2 }5 [8 I" M2 T: i - -10.0
复制代码 ' S! B" _7 Z& Y' X" U) y
math.gcd(x,y) 返回x和y的最大公约数$ V5 v* g5 L, v2 r+ D" R! y7 S
- #返回x和y的最大公约数" Q: f4 J y2 [1 R
- gcd(x, y) -> int- b- m+ M( W1 Y3 D9 M- K
- greatest common divisor of x and y
! q, o" l8 c+ N W6 C3 X - >>> math.gcd(8,6)+ }! z2 T) h6 e3 [4 f: R
- 2
& o1 ` s5 I# j7 J0 N$ ^+ s1 r - >>> math.gcd(40,20)0 |5 a$ t$ D0 Z* A
- 200 ` c! s+ {7 C3 J0 r% L) F
- >>> math.gcd(8,12)8 z' E9 `4 m) Y9 B$ d% Q9 c( b" C0 m
- 4
复制代码 & d! ?; C" e, F, C( G. i
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 I' g6 [* W6 d( ?
- #得到(x**2+y**2),平方的值
& }5 N* M1 e3 G, [+ a. T P - hypot(x, y)8 `* n+ k6 e& }4 _% I9 B8 q
- Return the Euclidean distance, sqrt(x*x + y*y).
. i# M' [, W( W* J. B8 D* m* d - >>> math.hypot(3,4)
4 |3 I7 D5 h; ^/ w( _$ [7 { - 5.0. ~7 i* Q$ `" k
- >>> math.hypot(6,8)
' r& p' |& ~2 I2 R - 10.0
复制代码
- q* Y. a3 B. T. i$ imath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
; t: f% x8 n8 | ?* f) w- #如果x是不是无穷大的数字,则返回True,否则返回False" Q! K% A3 P( o' C4 m5 S/ M: H
- isfinite(x) -> bool
3 ]2 y- ]/ |% C0 S* |( q - Return True if x is neither an infinity nor a NaN, and False otherwise.
7 w6 r8 g# z: _ - >>> math.isfinite(100), Z |8 W) X: M2 b' U1 i
- True
3 Z& A. d% U6 S: Z - >>> math.isfinite(0)
3 L! O- i3 K9 o6 g i - True$ y) f# O* }- Z6 b! B1 y7 f
- >>> math.isfinite(0.1)
* H" g n# G' I9 V0 @% p - True
! ~8 X V! O/ W5 j+ b - >>> math.isfinite("a")# }# u5 R. \0 N2 x
- >>> math.isfinite(0.0001)
# x: g# W2 c z - True
复制代码
* y4 T+ \ _1 g, g4 dmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
t w1 `& _. Z1 Z$ L- #如果x是正无穷大或负无穷大,则返回True,否则返回False2 ] Z }6 I1 p9 @
- isinf(x) -> bool0 I1 D# |; L3 A9 O
- Return True if x is a positive or negative infinity, and False otherwise.
; y# A; X. G( c) z - >>> math.isinf(234)
# _7 r# R3 c5 {; O' I - False
$ I. ]9 g7 T4 y6 q - >>> math.isinf(0.1)0 [6 Y/ o! E2 `8 V# n5 C
- False
复制代码 - ~" e! _1 {8 U- U5 W* s
math.isnan(x) 如果x不是数字True,否则返回False
2 ^/ u' f" |: u7 z$ g/ S) x- #如果x不是数字True,否则返回False8 Q9 U5 d: }3 |; H! P/ |% ~4 Z
- isnan(x) -> bool
4 [. B; f) x) c - Return True if x is a NaN (not a number), and False otherwise.
) J# ~- d: ^# r4 U) j - >>> math.isnan(23)
6 K. H, m/ \0 T: E) t - False
* h# o; V% _: H. K& P/ @ - >>> math.isnan(0.01)
8 ?8 k k" n3 P9 b, q) R5 b - False
复制代码
. L& d: R" B% |. ~2 J% x7 B5 {' e# [3 Y" gmath.ldexp(x,i) 返回x*(2**i)的值
u& u# p7 P0 x- #返回x*(2**i)的值0 q7 r) @+ Q8 h$ ]0 {1 U* g# ]
- ldexp(x, i); k5 I* Y9 {# @3 i6 o" x
- Return x * (2**i).
9 F4 h1 u9 o } - >>> math.ldexp(5,5)
0 D; {! X8 ?2 N8 m- O6 v# A6 _ - 160.0
g8 Q/ [ O+ I: s - >>> math.ldexp(3,5)
* k, d: E7 K% S - 96.0
复制代码 7 x# ?9 H; m9 e+ d
math.log10(x) 返回x的以10为底的对数& t: t |4 D( e( |
- #返回x的以10为底的对数
E( g! O4 ~7 ^" `4 Q* J8 R - log10(x)
3 S# u- K, z. v' C( n - Return the base 10 logarithm of x.
7 h: K1 y6 J4 A" P0 J. P/ b6 X - >>> math.log10(10)
. f# O5 d) F& ~: ^ - 1.0
$ I, s4 m/ X, a. \' ~! j - >>> math.log10(100). P: ~, O: W+ Y4 `( H2 }% H- w
- 2.0
$ _ y% ]+ R1 h' n( _7 n, _ - #即10的1.3次方的结果为20/ l6 |6 B) {1 [% u
- >>> math.log10(20). K6 s: H7 N8 S# p5 Y6 s
- 1.3010299956639813
复制代码 1 l+ J( G Z# F& \8 Y9 D
math.log1p(x) 返回x+1的自然对数(基数为e)的值
9 E3 E! s; G C U; J' D- #返回x+1的自然对数(基数为e)的值
9 Z! F* X( Y% T4 j - log1p(x)+ y: k/ O2 e" {9 c# d4 _& R
- Return the natural logarithm of 1+x (base e).1 U& Y3 i& T; F6 Q( X" \' y
- The result is computed in a way which is accurate for x near zero.$ U4 Z. X9 q, j$ X- h& W: q
- >>> math.log(10)" O4 w0 |, V9 {' ^' v$ P
- 2.302585092994046! B k7 C- i: o+ }7 W
- >>> math.log1p(10)3 N: S8 o- `7 j) I! k6 Y- F
- 2.3978952727983707% _0 F, _' Y6 ?* z
- >>> math.log(11)2 a8 O7 A0 @' [" @4 u9 |0 @
- 2.3978952727983707
复制代码
. a; l) H s; |& t2 z4 ]math.log2(x) 返回x的基2对数
6 t8 Q0 U ?# G" G" j, M- #返回x的基2对数 A) y; `; _( }% v- C1 t+ r, Q" X
- log2(x)+ M3 ]7 {9 V; P8 ^/ A- A( I
- Return the base 2 logarithm of x. ^" Z, C' `; B6 U, `
- >>> math.log2(32)
8 _+ Y S* m% l: N) W0 f+ r - 5.01 ~- B6 e8 j, ~* x4 W, @1 j
- >>> math.log2(20)
- L4 b( ~/ q- q& v - 4.321928094887363' T# o" q" @2 F' L3 J0 G4 i
- >>> math.log2(16)
; {9 a/ E. n/ j# ^0 T/ ~ - 4.0
复制代码
D% \3 c- l3 |4 m: I$ qmath.modf(x) 返回由x的小数部分和整数部分组成的元组
: e& [( E( b0 b- #返回由x的小数部分和整数部分组成的元组
- u7 j2 g' V+ w& N - modf(x)
. A' o9 L: ^1 e# P% c0 A5 R - Return the fractional and integer parts of x. Both results carry the sign
; B2 p% c$ W- o& c - of x and are floats.
" g0 ?6 H2 R: S' i7 \9 D - >>> math.modf(math.pi)2 @# w( S! p# o: @
- (0.14159265358979312, 3.0)
1 O% g! Y9 ^0 ?2 b# D9 O6 t, I: o5 b - >>> math.modf(12.34)8 W2 i% w4 T B9 l7 L5 I
- (0.33999999999999986, 12.0)
复制代码
- ?, P* b$ F& [/ a) E" Kmath.sqrt(x) 求x的平方根3 c. R6 v, Q5 L' z3 |8 i" S
- #求x的平方根
0 D7 p4 t" x0 O9 K - sqrt(x)9 B: e+ B% e6 y, p1 Y
- Return the square root of x.
! d( l; e x7 ]7 V, M - >>> math.sqrt(100)
" ~; ?/ l/ h+ z: C1 K - 10.0
/ V& l. W& t* A7 ~2 I+ l7 ] - >>> math.sqrt(16)8 W+ L3 R' X7 p1 T6 I: A/ \, ^
- 4.0 }9 r# \7 g) W
- >>> math.sqrt(20)
3 P- B' m0 M0 m8 { - 4.47213595499958
复制代码 - _4 b, C( V' P8 h% u
math.trunc(x) 返回x的整数部分- #返回x的整数部分
& t5 i# r% r: h - trunc(x:Real) -> Integral6 g8 G- [1 j+ X6 ^
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.; P2 A' O( {" u! l
- >>> math.trunc(6.789)1 O7 W9 B4 s. s$ j2 U
- 6& V5 T, i3 B9 n$ ? j* t
- >>> math.trunc(math.pi)
2 i, \- h0 f, C% i - 3& W( U9 d, [. ?, K
- >>> math.trunc(2.567)
8 ]2 V& U- }' R$ e, W - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|