|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
, r1 v: o- O4 u1 W! l' q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ ^3 M0 X* @: `/ G3 o, L3 r( N6 `6 [+ U. Q# {% V$ n
方法1:2 o( D* h- D0 U# }$ q' f1 B0 f
- >>> import math$ o9 q1 H: S" N: v1 J( a
- >>> math.sqrt(9)
6 S, b4 d0 m5 G( |# R4 [ - 3.0
复制代码 方法2:! {/ B0 e+ s9 [- t0 \% q0 x
- >>> from math import sqrt
3 O! C9 U: y9 Z6 n& n# s/ B - >>> sqrt(9) w. p' ^0 {8 P6 a" ?7 }
- 3.0
复制代码 9 R. \6 t6 i, i3 X* }: H }
2 Z' B4 @& M% @2 b7 emath.e 表示一个常量; Q4 V8 L& D" t9 {, G/ ? f: c
- #表示一个常量- M) L1 N0 h1 S
- >>> math.e
2 j% R4 f, k9 c1 P' a K/ M- a - 2.718281828459045
复制代码 & j' n1 d! O2 E* y/ S2 {( d
math.pi 数字常量,圆周率+ ~6 `' e5 c. o
- #数字常量,圆周率
7 U& o W: U9 o4 @ - >>> print(math.pi)
6 S8 H) D* V5 k3 l- z - 3.141592653589793
复制代码 * J' n& |, ^; n5 ?% A
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x D5 o: j! T8 k3 I' P" w4 K* I
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 i. M# Y+ {8 } - ceil(x)
) j0 G5 q3 i% ^ - Return the ceiling of x as an int.
4 q9 R( o3 e0 T, b; C$ ?0 J% R/ _ - This is the smallest integral value >= x.7 ]/ R% ?. r) w9 x# }2 a" x
- 4 ]$ g) I0 R# C& ~0 M
- >>> math.ceil(4.01)2 L# U" K: _/ `' D# Z% J9 e
- 5
2 P& [) F: H* y7 N& z/ d+ _ - >>> math.ceil(4.99)
' }, t! U, x, I( @$ X# V, Z - 5
: b3 @& E2 N8 l+ g! @& S' Z" t( Y - >>> math.ceil(-3.99)& X2 D) u" g% r
- -3
/ `/ y$ k7 n4 z) h: a$ ^ - >>> math.ceil(-3.01)1 I* A- \3 l# N/ `
- -3
复制代码 ( U+ h- |+ V& Q) d+ h% H0 p
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
a" p5 q2 i' x ^+ s$ `- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! A& Z8 J( g2 S" ?% j5 a2 v - floor(x)# _+ q) J# V4 F3 W+ a+ i
- Return the floor of x as an int.
2 w5 N, p4 s/ T% @, O; m4 n" D$ w - This is the largest integral value <= x.% [+ p; i: S2 s* ~2 W# Z$ w4 ?. L
- >>> math.floor(4.1), H# i, |5 } _ Z
- 4
1 T" P! V* D" H# l j8 g } - >>> math.floor(4.999). p/ H! R2 o. e9 g
- 4
4 W: j, {* R* H$ V- k* V! `$ | - >>> math.floor(-4.999)
3 q; Z& I% b' c0 w8 C - -5# _$ ^- A, ]4 E! u: B
- >>> math.floor(-4.01)
0 P( P/ r2 }, @0 S5 d% O; k, y" q - -5
复制代码
3 I9 u" l# v6 D, W& A/ gmath.pow(x,y) 返回x的y次方,即x**y! L0 E9 b3 b" l1 ~. B* r3 f
- #返回x的y次方,即x**y. U; L' c0 t& b0 _" z
- pow(x, y)
, H2 ]) j; B- t$ Y; ^5 @$ A - Return x**y (x to the power of y).( N) G) z3 q2 _( U8 `& @
- >>> math.pow(3,4)
- g; d. ?7 @! \0 _ \, U! ? - 81.0: y6 j+ [# N. H7 q4 k5 p3 @
- >>> 9 @7 X9 Y8 D' f( B3 O9 Z. a7 @
- >>> math.pow(2,7)- J3 m8 n$ \; {4 D4 X0 U. s
- 128.0
复制代码 9 D8 H, X# {- k" p, C, q9 h
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 y2 y3 y. `: I' M- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 Z0 X+ M/ _" a2 N7 b
- log(x[, base])6 e4 @) N; M2 i9 r% f1 y
- Return the logarithm of x to the given base.
4 n# E W% y5 `$ ]1 }% Q# x - If the base not specified, returns the natural logarithm (base e) of x.
- N; a* |& U' R$ K4 u' l - >>> math.log(10)
0 m/ Q f* K0 j3 s- ? - 2.3025850929940463 A# z- C& [; O
- >>> math.log(11)
1 C2 m. ^0 |3 e+ B* G4 M4 g - 2.39789527279837079 v$ c# I/ ]4 R7 i j2 v' S& M$ e
- >>> math.log(20)
1 M i. p' F5 ] - 2.995732273553991
复制代码 ( N3 S d6 }$ J6 z
math.sin(x) 求x(x为弧度)的正弦值 ]( o. X2 ~1 F
- #求x(x为弧度)的正弦值* x+ D" q$ k9 |+ g: R! |3 Z
- sin(x)/ C; R( }7 H% G t
- Return the sine of x (measured in radians).& Z( H- h7 N$ P9 G. _) B! U
- >>> math.sin(math.pi/4)
+ r% B: { [. S/ k$ o, x* h( r5 Y i* v - 0.70710678118654756 I% C; {: M/ N- c( |4 L7 x, R
- >>> math.sin(math.pi/2)
0 {& n- Y3 {0 n: H2 O - 1.0
1 L% ?5 [& ~$ L8 c8 D9 x2 R3 j - >>> math.sin(math.pi/3)
T( L( ?) u, L( W - 0.8660254037844386
复制代码 5 h' C( q' f3 T
math.cos(x) 求x的余弦,x必须是弧度
$ l. b) C# n. i% \6 t, A- #求x的余弦,x必须是弧度) d5 k8 r( k5 L' d8 P, P$ k
- cos(x)
% m: z' S. Q: }& y3 E5 e - Return the cosine of x (measured in radians).+ I+ Q+ w3 j' ^0 ]
- #math.pi/4表示弧度,转换成角度为45度/ d: \! c. w W# c
- >>> math.cos(math.pi/4)' ~3 p4 V7 W& h8 w) b6 ?# P# F" ?1 }3 C
- 0.7071067811865476
" O1 o+ I9 x3 \7 C( s - math.pi/3表示弧度,转换成角度为60度+ c9 Q6 J" n/ y% C1 E$ g
- >>> math.cos(math.pi/3)
+ `, Q/ W. ~# R9 E: P/ u" R - 0.50000000000000018 L# U) L) L+ v4 r
- math.pi/6表示弧度,转换成角度为30度
# c( E5 Z* P% F6 g" `0 Z - >>> math.cos(math.pi/6)- Z9 n: f' K/ f6 x8 ?4 H* G
- 0.8660254037844387
复制代码
" v9 i' Q# h, O) c) zmath.tan(x) 返回x(x为弧度)的正切值% t+ p' H) S* P9 n9 ?
- #返回x(x为弧度)的正切值/ }7 Q0 ?. U5 [+ X$ O4 Y$ p
- tan(x)
0 I0 W3 u) C/ @1 h2 e" i$ { - Return the tangent of x (measured in radians).
6 c! f: p0 l# B/ u( i; P - >>> math.tan(math.pi/4)" T" w. ^; ^9 B
- 0.9999999999999999
, O' s+ I; W. A6 u* ^$ Q! a - >>> math.tan(math.pi/6)6 ` @; P3 d/ f1 q
- 0.57735026918962573 D4 L& z3 V; k' O4 _% r
- >>> math.tan(math.pi/3)9 o+ x( l% h5 N/ l
- 1.7320508075688767
复制代码 ! B1 X- G, R* G" v# n' A
math.degrees(x) 把x从弧度转换成角度
3 Y8 d& |2 ~2 J' l9 w2 T; {. i- #把x从弧度转换成角度+ T, j' N7 ~) f5 _ e1 t J; N/ e
- degrees(x)
; p! h9 V- O! P - Convert angle x from radians to degrees.
+ Z" Y7 ]8 V$ H6 p - 1 e$ b( A( m# a9 R3 D8 [+ f3 S
- >>> math.degrees(math.pi/4)
( w, R# I& E; b! n - 45.0
$ O/ A7 N+ L _. p2 C1 e: K - >>> math.degrees(math.pi)' u0 T+ k4 Z5 H1 K- w; q
- 180.0
, d9 B/ C! z2 X8 [) L: W - >>> math.degrees(math.pi/6)
6 `9 x4 _ o" F/ n" { - 29.999999999999996
& \* j" R5 Z$ ]9 x2 z" R3 G - >>> math.degrees(math.pi/3)
% [) f1 p" Q5 `* W$ v# F7 D7 C - 59.99999999999999
复制代码
8 i. W; v/ w0 Emath.radians(x) 把角度x转换成弧度! K( T' g1 X- g- S5 P5 G
- #把角度x转换成弧度: d* S9 c) L0 C( \9 Z/ Y& }
- radians(x)+ I, e! r1 Q1 h( e9 P0 R$ o
- Convert angle x from degrees to radians.
2 j/ n" h6 n2 J! o% U( p; K - >>> math.radians(45)# Q0 s* G4 {/ W/ @& F) q
- 0.7853981633974483
" h% G d3 ~% K! z n0 ^ - >>> math.radians(60)0 ? }9 m4 Z' |- M
- 1.0471975511965976
复制代码 , a9 Y r9 q0 g2 A1 g5 [* \
math.copysign(x,y) 把y的正负号加到x前面,可以使用05 S3 I3 o) }, K# e
- #把y的正负号加到x前面,可以使用09 t9 R. T* f) n( e( T
- copysign(x, y)
# U7 ^4 X g, d7 B - Return a float with the magnitude (absolute value) of x but the sign
1 g4 H" j2 P+ m% y - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
2 D0 G- g! A9 S, J# Z2 e* ~3 B/ b - returns -1.0.' h; {$ m4 W/ p2 O
: L4 @' T' B. r+ _( C- >>> math.copysign(2,3)$ _: F4 @+ Q+ v+ y& }5 L. r* m
- 2.0
/ }5 d4 {& ]; Q, M9 u! C% _+ m - >>> math.copysign(2,-3)
( D6 B+ C( Y$ w/ a - -2.0
0 d% y% K6 @1 G# U C6 i' p* u. ] - >>> math.copysign(3,8)
8 y# }: W9 c% l$ q - 3.0
! j- j2 ?; d) x( ^5 F3 K$ n" X - >>> math.copysign(3,-8)
1 I2 I' z) }# [- t! [ - -3.0
复制代码 " X3 ^- r6 f ^* d$ H/ q- W
math.exp(x) 返回math.e,也就是2.71828的x次方6 X! a$ |# E/ g/ V) k# M7 X
- #返回math.e,也就是2.71828的x次方 T1 T5 p. J1 t2 Q
- exp(x)
5 m! g1 G7 t3 W/ t7 @% h3 A- H - Return e raised to the power of x.' L/ q# v, Y4 h! W& }
/ H5 _2 z- o4 U2 |; K! J1 l: d% P1 c- >>> math.exp(1)5 z/ \6 Y8 S8 w/ c+ h
- 2.718281828459045
6 p3 y/ W) @9 E i0 v - >>> math.exp(2)3 D% I# {% Z; W
- 7.38905609893065) Z6 F3 k8 e7 v: [. I
- >>> math.exp(3)2 Q. Z2 m! f& w+ T
- 20.085536923187668
复制代码
- N; V+ t2 |3 ?5 Z5 P- ymath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
5 X% S" q- V# r; \; v0 W- #返回math.e的x(其值为2.71828)次方的值减1
* k1 f5 X9 d6 i - expm1(x)6 H6 S) b% l! x& ^
- Return exp(x)-1.
# N h: B% c2 @5 x* S - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" E$ F7 q7 W# r8 A/ J& q - 5 j8 c9 I T; Q% ^# W
- >>> math.expm1(1)2 K S; g& C N# G- X0 N a
- 1.718281828459045
; f' n$ w4 c. b U7 h: w: N - >>> math.expm1(2)
; s/ }& o- M' Y - 6.38905609893065
& I8 \; @8 @- P - >>> math.expm1(3)
" r% u/ F% U* \ w7 n1 a - 19.085536923187668
复制代码
9 e& H9 w: |5 i4 J+ x# T4 _. {math.fabs(x) 返回x的绝对值9 F' @6 v" v" d2 i
- #返回x的绝对值" d5 {2 y( U% [# c& R% Q1 ]
- fabs(x)
# g/ s! q1 t+ @- @ - Return the absolute value of the float x.
4 J7 E# L2 E u1 L1 G6 i7 J, `
$ I, k- t% K6 b; u- >>> math.fabs(-0.003)7 y$ m0 m h7 f! b; H- t
- 0.003
3 K& C7 ^! p6 B- q" q$ n% d - >>> math.fabs(-110)
& H. j* i% i# G. i; ~ - 110.0
+ ?8 `9 q9 s+ g' m - >>> math.fabs(100)9 L w3 X7 N8 d, U, U6 l5 d) z
- 100.0
复制代码
, |; b" X3 z d! Xmath.factorial(x) 取x的阶乘的值$ i. c9 w" w" {) ~4 Q9 T" m
- #取x的阶乘的值
$ {% t, y/ O- Z* Z+ x - factorial(x) -> Integral
& @6 z$ o: p4 R/ c7 j7 X - Find x!. Raise a ValueError if x is negative or non-integral.6 E6 |9 s4 n( Z# N1 O w
- >>> math.factorial(1)- y9 k. E8 O! ^8 u
- 1# {# Y, s# Y. p8 l: g
- >>> math.factorial(2)& r1 {% Y4 K1 ?& C; J5 {, p. f
- 2
) L2 e: b4 o4 G3 D0 J - >>> math.factorial(3)
) A( G5 _2 h2 c) C+ r - 6+ h2 f2 d# t8 t0 a4 F
- >>> math.factorial(5)
' @4 N% b; j) Q - 120) R. \; a0 h: ?1 j
- >>> math.factorial(10): }) Z9 s/ i% N# i
- 3628800
复制代码 ' G. L6 i6 c/ l0 q2 | _
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
5 \2 ~* p% X, ~; I4 l c- #得到x/y的余数,其值是一个浮点数
! X0 w. U; w. `( g - fmod(x, y)1 t* w, _4 E: U0 w
- Return fmod(x, y), according to platform C. x % y may differ.( F% V8 S7 p2 W5 H; w4 G
- >>> math.fmod(20,3)
+ W4 \8 H# V. v% z) r - 2.0% Q/ s0 H4 A1 e
- >>> math.fmod(20,7)
& E# }* r+ o3 a' H - 6.0
复制代码
3 X$ X; m' ^" `: C, S/ O; {math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围3 e; x6 M' K) k l& b
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
6 ~9 e$ @, q7 Z, f6 `: C - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
/ @$ c( p! R5 i8 |* ] - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 x) A& n) t2 X& w8 i- W" ~
- frexp(x)
/ q% p X7 e7 w8 v. J/ ]% a% e+ j - Return the mantissa and exponent of x, as pair (m, e).! {! f5 {0 c% E
- m is a float and e is an int, such that x = m * 2.**e./ l3 L0 Y2 |8 M
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
9 s/ D: U7 v5 }; Y - >>> math.frexp(10)
l4 m8 W! X5 I; H+ A& ?. z7 h Q! K$ w - (0.625, 4)% W- a0 e! X; y, ?* [9 _
- >>> math.frexp(75)
0 g. O+ s5 r" C) x. m - (0.5859375, 7)
% M: T. F2 [* ]2 k0 G" y8 V6 h0 A - >>> math.frexp(-40)
- X5 Y* ~( U* s5 ]: @9 } - (-0.625, 6): `2 d: R" L( O
- >>> math.frexp(-100)
; W7 M3 L! F/ f! Y( c - (-0.78125, 7)
5 O$ |$ f+ ^+ ]( E. } - >>> math.frexp(100)# ~1 |) P: l. t! g% v
- (0.78125, 7)
复制代码 - t9 q: ]% w- I$ b' `
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)9 @- _2 C/ I, _5 H: C
- #对迭代器里的每个元素进行求和操作
' U* Z2 p; ~% J) A/ z8 n - fsum(iterable)
" G% \) ]$ ~* J9 A, q' ] - Return an accurate floating point sum of values in the iterable.7 k4 R1 p( [: _# B2 K
- Assumes IEEE-754 floating point arithmetic.8 ?: k: [+ I% u9 ]
- >>> math.fsum([1,2,3,4])( J! R( B+ |8 k; Z' G* Q
- 10.0, S( L1 g# u$ Y% v
- >>> math.fsum((1,2,3,4))2 j8 _1 [# F7 z' p5 S
- 10.0
" y: }# w: ?3 i9 y - >>> math.fsum((-1,-2,-3,-4))8 H6 k& b0 ~8 J7 A2 V* H: O
- -10.0
1 x$ Z& Y/ [/ A: ?% p - >>> math.fsum([-1,-2,-3,-4])
# u" Y; j7 b) i% l2 X# u - -10.0
复制代码 ! n1 q" o k3 q I
math.gcd(x,y) 返回x和y的最大公约数5 q, G) O5 s% B1 ~! m- J0 M
- #返回x和y的最大公约数) s5 `5 J; S( d+ i/ W
- gcd(x, y) -> int9 Z" J3 W; V. f; J: s7 n! @
- greatest common divisor of x and y
{; Z. _1 `5 J8 p2 s% I - >>> math.gcd(8,6) N7 p {$ U" r% ~9 {
- 25 u9 w' I) V. N+ W" B( C: V
- >>> math.gcd(40,20)+ Q% b% T' ?/ X* b/ G$ V. s
- 20
2 r* j! ^, N& V% [3 v( G+ _, p8 r - >>> math.gcd(8,12)
7 F" }/ v$ B/ l& Y1 R9 _ - 4
复制代码
5 Y2 s: G7 v7 c4 \, m5 Umath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
/ I8 z* g; W& m- #得到(x**2+y**2),平方的值
8 m% a! H. z- \, ] - hypot(x, y)
& p/ x0 i+ {7 F# m+ ? - Return the Euclidean distance, sqrt(x*x + y*y).. ?" c& Z' U5 @
- >>> math.hypot(3,4): W0 n5 e: ~& @
- 5.0( t9 C0 h: H. w$ I/ o! }
- >>> math.hypot(6,8)0 q' ~- x5 ^# f u" ^7 j! G
- 10.0
复制代码 * n' S! g. E. l$ W
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
; _2 H0 z, M' v2 G- #如果x是不是无穷大的数字,则返回True,否则返回False
) o3 D1 h3 Q2 @8 Q- \% U' f. w - isfinite(x) -> bool
! I2 L+ c$ e* o' Y5 _4 R7 F - Return True if x is neither an infinity nor a NaN, and False otherwise.
) x `8 [$ g3 U5 H - >>> math.isfinite(100)) `( d+ R+ w# c7 c$ @
- True7 A2 ^, u, U- D" H& {
- >>> math.isfinite(0)
3 k y' c" U% w, Z/ R2 T - True
, ?$ c' ~* M; f/ \3 S W% r8 ~0 }6 T+ J - >>> math.isfinite(0.1)
, {0 D' m5 w0 B# L, J% _ - True
A7 @! g7 g1 n& Q/ }1 ` - >>> math.isfinite("a")
+ {/ N9 d S, x& K D4 Z$ C$ j8 ` - >>> math.isfinite(0.0001)/ y) t P+ ^4 t
- True
复制代码 + e7 n }+ D6 v0 c% ~
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False S( \3 P- T \' Y6 I7 w: e u
- #如果x是正无穷大或负无穷大,则返回True,否则返回False. L3 a$ X: B( |
- isinf(x) -> bool
5 {' c* B/ @) F1 h& V7 L* K% H - Return True if x is a positive or negative infinity, and False otherwise.
% G X' d: o; C( Y9 K2 ?0 q p - >>> math.isinf(234): I4 F0 K5 I3 W9 ^; I
- False
a. N; O/ \* U- V& a) Q4 B - >>> math.isinf(0.1)
% X |6 S7 w8 h - False
复制代码
4 M5 x. w! S& x; W: ^- |5 Smath.isnan(x) 如果x不是数字True,否则返回False/ ]% z$ _9 U) w# m) o6 g
- #如果x不是数字True,否则返回False% }* k9 z* E& I" S( s' j
- isnan(x) -> bool
+ n# D6 G1 z1 N' P6 n - Return True if x is a NaN (not a number), and False otherwise.
9 q, Y4 j9 \1 D5 K% _) | E2 A) r - >>> math.isnan(23)$ W+ }, L+ `' h. K3 F
- False
3 P9 s" R8 o* T" E7 A0 I - >>> math.isnan(0.01)
, k& }0 N' @( h+ s$ [ - False
复制代码
. ^8 L' _: E1 f3 F0 A2 B" gmath.ldexp(x,i) 返回x*(2**i)的值5 K0 x$ a* R, u( l
- #返回x*(2**i)的值
" m) l6 T% B9 Y - ldexp(x, i)
4 B& x* P- X. c* s9 z1 c# u - Return x * (2**i)." `0 q \# T B+ |9 R2 C# u
- >>> math.ldexp(5,5)& F: W) J4 L8 D! F! e' _; v) C6 X
- 160.0
; O9 P" y# q8 ]1 P2 z, I - >>> math.ldexp(3,5)
* [) S& \1 u# e+ R/ K - 96.0
复制代码
% W) q' M/ z+ j+ k" I: y* Zmath.log10(x) 返回x的以10为底的对数( Y, A, E* n3 u2 B* ^- Q
- #返回x的以10为底的对数4 x) h$ Q- E( |8 l8 l
- log10(x)' _# c8 j; p! _ m! S
- Return the base 10 logarithm of x.
( X T) U$ o: Y8 U0 J - >>> math.log10(10)1 h+ x6 H" k* \0 h* B% k, n
- 1.0, V2 |# z5 _. p" o" W: I7 n8 J
- >>> math.log10(100)
9 k' K) M$ t. ]" y2 t+ O* J - 2.0
# k/ a, H- _ @' } - #即10的1.3次方的结果为20
/ e" N Z4 | S' M- u E& O - >>> math.log10(20)& \7 _+ R2 ?8 I1 e
- 1.3010299956639813
复制代码
% j& b" K4 P' @* dmath.log1p(x) 返回x+1的自然对数(基数为e)的值
% \4 H w i- P- #返回x+1的自然对数(基数为e)的值. U6 ^0 s* g- s" M$ G |" @
- log1p(x)
3 U- g% j9 T2 V( m6 ` - Return the natural logarithm of 1+x (base e).0 A; B" Y& X* b$ n6 m
- The result is computed in a way which is accurate for x near zero." h. R% Y8 \7 l9 _- @. L+ @8 x
- >>> math.log(10)0 {2 O* n+ M/ W
- 2.3025850929940466 b3 J* N. \2 k" U+ X0 |- s
- >>> math.log1p(10). A7 K) b" ]% w, }* b2 h3 y
- 2.3978952727983707+ [9 A3 F9 t/ `3 g3 l, q
- >>> math.log(11)# n2 j0 T6 x! o- r, L6 A7 I6 A
- 2.3978952727983707
复制代码
( d: ~6 h/ z4 k& a2 S) Pmath.log2(x) 返回x的基2对数, n" U+ W9 }5 T! r. O6 O$ o
- #返回x的基2对数% ^/ E$ F6 h* u- [, S! v: L
- log2(x)' ^; }) P, s0 {7 g5 f, Z0 h
- Return the base 2 logarithm of x.
3 ?; X" q+ {: Z" {( v; B - >>> math.log2(32)9 J! K+ f% e$ F# b
- 5.0& U5 p; C7 e8 I; u
- >>> math.log2(20)! K, a4 e' [1 Y4 v4 X( _+ A' ~( O
- 4.321928094887363
/ z8 @( W5 `; `" H2 I, A - >>> math.log2(16)
4 Y2 A2 N7 W' W7 i: H$ Z* n - 4.0
复制代码
( _+ P$ m0 B. g) P/ Y" u, Q" h, mmath.modf(x) 返回由x的小数部分和整数部分组成的元组& q7 ^) v; G! f9 C
- #返回由x的小数部分和整数部分组成的元组
% s4 c1 {5 B4 J3 j7 i - modf(x)
3 @% f# Y! q" u# a u) _& ^$ W - Return the fractional and integer parts of x. Both results carry the sign
O, i6 ~/ {1 \5 p - of x and are floats.# q3 ~* |5 Q6 C
- >>> math.modf(math.pi)& W* }6 [7 x. \1 q# d% Q
- (0.14159265358979312, 3.0)6 K4 O) t) f3 H& C
- >>> math.modf(12.34)
+ N+ m! Q3 R1 j - (0.33999999999999986, 12.0)
复制代码
7 C) U3 i& v2 ?math.sqrt(x) 求x的平方根4 k% `- ?* ~, d7 y6 V1 k
- #求x的平方根4 Q4 s+ g2 r/ B( @$ A
- sqrt(x)
- C$ G# b- Y1 D5 U( e' P) c - Return the square root of x.- O" g+ F: m; q$ F
- >>> math.sqrt(100)
. H) q2 I$ k. t) Z, L/ Q: H5 | - 10.0+ F& K6 V$ C6 Q; F4 ^( a& G
- >>> math.sqrt(16)
* H* N9 H* x, J) A! H% f - 4.0
$ F% x' w2 A" m; R- v - >>> math.sqrt(20)
9 C# u- W- R3 Z0 }7 @3 D - 4.47213595499958
复制代码 . _2 ^ |' R/ R9 T: Q! F1 p
math.trunc(x) 返回x的整数部分- #返回x的整数部分
) G# f" C, l" ~! X" p - trunc(x:Real) -> Integral. A/ L: ]9 i$ w) r" Q3 c
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
$ n0 D" N( Q; d* U* T* q# e6 o - >>> math.trunc(6.789)1 P) F O- q" ^) t4 t
- 6; a+ r" V9 r8 [/ Z" Z0 |
- >>> math.trunc(math.pi)
! e* q7 C# Q% I* N& r% Z - 3" F6 `. k1 `$ s* f. Z8 O
- >>> math.trunc(2.567)
' ?' n2 z, J2 B4 N; J, B3 Q7 O. b - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|