马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! p2 u8 Q% j, Z+ g4 H* z8 A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 R1 ^6 F) y" A1 r
. O, E; t' j2 I5 p" ~$ |方法1:' ~2 d3 \ f+ s0 T. I o/ \3 X. K( A
- >>> import math. D# A t$ |% X3 _% U3 T
- >>> math.sqrt(9)
7 J+ K5 A; o7 v& z( h - 3.0
复制代码 方法2:4 i. O1 I# g, X# o, p2 c! L4 r) ?
- >>> from math import sqrt
) I; F: p$ ^6 H) Q: ^ - >>> sqrt(9)
' f1 ?# m4 H- [5 Y! H0 k- Z - 3.0
复制代码 & f" Z6 `* d& `/ u. i i6 _, X
?4 A$ T/ g- N* H) L
math.e 表示一个常量
( g! @* U: M6 ~$ I" K$ H3 V7 _- #表示一个常量, y" U* F6 ^' q0 s% h5 p
- >>> math.e
7 [ w2 b1 i# r0 R% ?5 Q - 2.718281828459045
复制代码 # ]8 d( K+ w0 a) U
math.pi 数字常量,圆周率7 i" d8 y- z/ U4 @" ~+ u4 p
- #数字常量,圆周率
+ \. V2 t- s5 y - >>> print(math.pi)* d# {$ o- _9 n2 f4 p: s
- 3.141592653589793
复制代码
( E! X7 d9 J, jmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x" @6 W: L2 P% C. u
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, a! ]2 Y5 V; @) o6 u
- ceil(x)7 E& r8 e2 a6 L
- Return the ceiling of x as an int.
* c! ]8 [& n. @" h - This is the smallest integral value >= x.
' [: q( \2 n% ?# L
6 ~1 W: J( n& R3 b3 h- >>> math.ceil(4.01)
5 R- v" z6 m* g2 Y1 g/ P - 5' P+ }4 T- j8 b9 N5 L4 P
- >>> math.ceil(4.99)
`4 j( C, G- F - 5
4 ]# x1 \" h8 ?% M - >>> math.ceil(-3.99)
# `/ C1 R4 c! k& S- J& O - -38 X0 W9 j& n: T! `3 T
- >>> math.ceil(-3.01)
: z. v6 U% Y8 u: L- n8 v; y: T; V2 \ - -3
复制代码 ! f# O) u, U8 s4 J
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 T$ k, u* ^5 l! |; m- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身. [2 h& y8 V; L9 z' a! [. h
- floor(x). h% b5 r3 _: k7 \+ z7 Q! ~) ^, b! u2 }
- Return the floor of x as an int. {5 f2 d' z; Y1 ~$ j5 X
- This is the largest integral value <= x.
, V5 p m& N. w; [6 B: a - >>> math.floor(4.1)" y ?$ T* H" r
- 4
7 O& C+ m: U; X3 y: x) U( k6 V - >>> math.floor(4.999)
1 G- G- l9 Q; E: L. | - 4 O5 i( Q8 {; k7 B9 `: n* F% i
- >>> math.floor(-4.999)# S" o# }5 o3 \# T
- -51 M# }- _) b$ u& j% F, y
- >>> math.floor(-4.01)2 u6 }- L( t# ?! ]& e
- -5
复制代码 5 K% Z. l c" {+ i: |, l
math.pow(x,y) 返回x的y次方,即x**y; b; M" z1 s4 E5 \' o
- #返回x的y次方,即x**y* Y5 w& i& |5 z5 L( p0 c" `: t
- pow(x, y)
5 Z4 J1 H4 O: g4 B" [6 m - Return x**y (x to the power of y).
4 @9 R- F+ c7 M: D - >>> math.pow(3,4)* L* b8 w# l Y# O& A
- 81.02 S8 `# F' g" ?8 G% Y. F
- >>> 8 Z8 z* K8 x4 \. ^( K& N( c9 k4 ~
- >>> math.pow(2,7)
& n) W( {8 W. y ?9 B - 128.0
复制代码 9 i1 b' ^ l" e+ S. U2 p
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 N. F+ N/ k; `. H% ~! W: f- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 f! i( u9 s& V7 @5 `9 y - log(x[, base]). o: Y0 x# {/ r
- Return the logarithm of x to the given base.
0 T" P6 h! R3 o5 L e$ j - If the base not specified, returns the natural logarithm (base e) of x.
* K8 a7 d: O2 d3 O, d0 O( w2 B - >>> math.log(10)
" i( g/ J0 e/ g8 \0 S/ L - 2.302585092994046+ O. t9 R/ g: [- m' |
- >>> math.log(11); P1 }5 R* c6 [ U
- 2.3978952727983707/ }& b5 V, \: I! s2 S% j& u
- >>> math.log(20)
! m$ K1 }+ N1 {! z+ O+ [ - 2.995732273553991
复制代码
4 P% A! E# Q( M' |* pmath.sin(x) 求x(x为弧度)的正弦值
* ^2 r6 B3 Z' N; K- #求x(x为弧度)的正弦值
B' G* M& D2 W: L, t' B - sin(x)
' _& u6 {" U9 o2 n9 R$ Z y - Return the sine of x (measured in radians).
& F0 e8 u5 F7 R! E - >>> math.sin(math.pi/4). L$ n) F N) [" W( Y& c
- 0.7071067811865475$ Z4 ]/ G+ M% p& f: v% c
- >>> math.sin(math.pi/2)
5 `* k4 |9 O# T# u, S" W' H - 1.0- O" K6 E& f7 V
- >>> math.sin(math.pi/3)
/ e7 I8 u: `4 _" \4 t& t - 0.8660254037844386
复制代码
) D; X# u3 _ u1 u' y, }$ dmath.cos(x) 求x的余弦,x必须是弧度
4 }% Z0 Q X( [: t- #求x的余弦,x必须是弧度( F6 t8 c/ E( ?! J4 F1 }, y* y
- cos(x)( x4 q- Y$ \% `, c' K
- Return the cosine of x (measured in radians).
6 f5 g9 [/ G7 U/ I, y - #math.pi/4表示弧度,转换成角度为45度
- @5 w) R* B+ r; Y( ~: [6 O7 |& z& o - >>> math.cos(math.pi/4)
1 C( ^5 x% s# ^+ Y! r - 0.7071067811865476& x# m" L/ z3 \
- math.pi/3表示弧度,转换成角度为60度7 c7 I, Y, c0 Y6 ]
- >>> math.cos(math.pi/3)* i8 l _! E1 @% V+ X! J) B. t
- 0.5000000000000001
: s$ ~: W# g: ^, ^3 ? - math.pi/6表示弧度,转换成角度为30度
% @7 S/ I, \' @; X+ t - >>> math.cos(math.pi/6)& ?1 k) G- j' U7 @6 ]8 C) F
- 0.8660254037844387
复制代码
" d \2 l) {/ Z: a6 }- }math.tan(x) 返回x(x为弧度)的正切值
. Z+ C) a; ~9 e i- #返回x(x为弧度)的正切值# H4 P( V' o/ j6 ?% Y+ U
- tan(x)' `* t+ h" k, q \, r
- Return the tangent of x (measured in radians).
: `, N9 {& v3 W+ O) S& d# T5 d* V. U - >>> math.tan(math.pi/4)+ J* c' K0 t3 N9 B1 ]9 l
- 0.9999999999999999' l- `# L" j3 q7 x2 e* Y
- >>> math.tan(math.pi/6)
5 L& s7 ~. [9 S' ^: }" d' T - 0.5773502691896257% O0 P0 d' L, B H, @
- >>> math.tan(math.pi/3)) O& l" a) U# G. Q* }0 s
- 1.7320508075688767
复制代码 $ M; T& N j: Y0 s
math.degrees(x) 把x从弧度转换成角度7 |; a( V4 q; r( k$ d, a/ f* A2 |
- #把x从弧度转换成角度
* J" F: c, {& B. Y O: C: L - degrees(x); A# p/ t2 a& i3 E, J
- Convert angle x from radians to degrees.- J1 z I( C$ D' @8 v5 \. i Y
- 7 V# I5 e1 @7 g
- >>> math.degrees(math.pi/4)# V o/ a7 J, \
- 45.0
# U! K; M7 e% V/ H6 V - >>> math.degrees(math.pi)# x ^' J5 r3 j5 l* `
- 180.0- S, X$ y5 q" _& j: f! d
- >>> math.degrees(math.pi/6)! l W9 {$ \; Q8 W
- 29.999999999999996
8 O9 m5 `$ V, j" t5 r - >>> math.degrees(math.pi/3)6 M/ H2 S/ Q2 [1 D
- 59.99999999999999
复制代码
4 Y, @, p4 t9 k. fmath.radians(x) 把角度x转换成弧度& U* ^- o. [. `8 G9 m5 F/ z
- #把角度x转换成弧度
6 R. T; g5 @- Z5 Y! L# n0 H) { - radians(x)
d% i& `# E6 ~6 N1 p2 F - Convert angle x from degrees to radians.: p: r) q+ P! u6 @3 _- b( s G
- >>> math.radians(45)6 p2 H/ q Y# B
- 0.7853981633974483: w- q: I' M R8 \7 {* v! c" I$ e
- >>> math.radians(60)
: u1 @9 p/ N- b& T2 c - 1.0471975511965976
复制代码 # [' m6 ]' b0 p% T
math.copysign(x,y) 把y的正负号加到x前面,可以使用04 G/ t" L# v- X. X. ~
- #把y的正负号加到x前面,可以使用00 ?: [4 k$ G) l4 H
- copysign(x, y)
0 t2 f6 r7 M& l! N, o5 n - Return a float with the magnitude (absolute value) of x but the sign * l; h2 T ]$ \
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
7 Z3 g: G4 r# i6 }/ ` - returns -1.0.
) ^5 ?& m1 ?8 } - 5 C3 a4 g9 Z1 L1 N2 \
- >>> math.copysign(2,3)' I( ~. L I- I$ R8 d1 H2 C
- 2.0" N7 J) p1 D# X" Y; Z4 h1 e
- >>> math.copysign(2,-3)
4 n: n5 K& N T+ Q* I' l" t - -2.0
' ~" u5 s6 [( r, ^5 a% K2 t - >>> math.copysign(3,8)) s2 E" |6 Y7 r$ U( U0 r
- 3.0# n/ m- b0 x# i2 w; u
- >>> math.copysign(3,-8)
/ o) W; `5 k" w, p, t - -3.0
复制代码
- a* J7 z: H ?; ^math.exp(x) 返回math.e,也就是2.71828的x次方. d# z3 d A. U$ S* ~1 t
- #返回math.e,也就是2.71828的x次方# u" E0 O" H, J- ^2 R; V* a, \* E3 i
- exp(x)
& b0 u" o' q% u. E5 [ - Return e raised to the power of x.
) W+ P0 s2 h& ?5 S - , u* m6 _: r. i$ K4 B- O
- >>> math.exp(1)4 D& E& S7 ]- a
- 2.718281828459045
9 s# I: K8 M4 z7 A7 E - >>> math.exp(2)+ {' `5 d3 ~- M+ P- w# B/ Y9 F
- 7.38905609893065* x; Q3 Q8 \1 s m9 f
- >>> math.exp(3)/ A' B) L* E& a/ {
- 20.085536923187668
复制代码
3 H$ M$ G' M1 i: Q4 d4 X& {4 R) `math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1% T9 n5 B8 U9 x+ c: r% h
- #返回math.e的x(其值为2.71828)次方的值减1* m0 E7 a* Q c
- expm1(x)4 R2 k8 W4 \6 A. B
- Return exp(x)-1.
! T" F: \# Z; W3 I4 ~# d. |( B - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 K% I/ V! P7 `" m: H+ ], M! n. i
- 5 Q0 R: C v9 @7 ` p- a) ^* w& D
- >>> math.expm1(1)& d k k3 U1 A) \
- 1.718281828459045
8 M9 O7 d0 P4 ?' T1 E - >>> math.expm1(2)
: F, \4 k; t% ~% M& d5 P' y$ z" r - 6.38905609893065
: h/ z) h' x, o - >>> math.expm1(3)5 M6 G. `: l. a; n
- 19.085536923187668
复制代码 3 C4 O$ G% K1 O# i% r
math.fabs(x) 返回x的绝对值
' B! O' E$ ^! C- #返回x的绝对值1 a+ E' z6 U- N" B( G
- fabs(x)) u# K* J' s, `! k: r) N6 i3 u, b
- Return the absolute value of the float x.! |/ A T0 x/ l& p2 L
' \2 R5 f5 ?6 [; S8 V# l3 O. r& k- >>> math.fabs(-0.003)2 u) g2 e1 x2 ]) K. P w
- 0.003
+ r( H' ? x2 |4 f* b* }6 s) p - >>> math.fabs(-110)
2 X3 N9 B7 R$ {+ v" p1 G* F$ X" O- { - 110.08 l+ i$ z. f% }. I) D
- >>> math.fabs(100)5 N" E, \9 m2 o! N& ^3 o9 m
- 100.0
复制代码 ' q3 c- ]) N9 s- p. M2 I, }
math.factorial(x) 取x的阶乘的值8 f) J% n5 w/ A, y8 _# x
- #取x的阶乘的值
% r6 C9 [) r9 ~ - factorial(x) -> Integral
0 S' q/ p8 O) M" F/ A* h" } - Find x!. Raise a ValueError if x is negative or non-integral.. W" H5 W' i& i' @; ~
- >>> math.factorial(1): P( u/ A7 Q" O, \7 _& r
- 1
. _4 b9 G$ d0 I F3 i% _ N - >>> math.factorial(2)
+ r4 X% [# |2 A% ?; G* L" B - 2
7 g$ l( U, e- i6 d C6 S - >>> math.factorial(3)0 h7 t# T# j: v! d, h) l$ }9 ?1 W
- 6
& Z& f5 O( h; a( `, S - >>> math.factorial(5)% J7 }6 R/ F4 s [$ c. h
- 120
9 ^# o% b! ^ W: ]% X, z7 `3 @ ` - >>> math.factorial(10)* l9 U$ N: M2 n2 T! x
- 3628800
复制代码 2 X0 p2 I1 Q( B# |" t: l
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数5 Q: J0 y! I, T( H& f
- #得到x/y的余数,其值是一个浮点数
+ {, t$ b; x4 G. d - fmod(x, y)
* k0 Y' _+ z7 L* a# P( ? - Return fmod(x, y), according to platform C. x % y may differ.
+ _" U% d, v4 g- h) U2 e& s7 O - >>> math.fmod(20,3)% C( D% H. z p M! l: f3 s
- 2.01 ^2 Z$ x; {( ?# u
- >>> math.fmod(20,7)
1 L: `& g5 N# G4 ^ - 6.0
复制代码 ' @- V; K, i9 {+ |9 v* b
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 _" I; u$ U3 \3 w3 M
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
; B; u( x( V2 l$ \* z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
$ m1 O& H' @/ V; t& K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 ?; R4 u+ U$ Y7 k& r - frexp(x)6 a. N: @$ a# {
- Return the mantissa and exponent of x, as pair (m, e).
% C l. ~- C+ X - m is a float and e is an int, such that x = m * 2.**e.1 P1 X' a D# a& `
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0." h! n/ \$ g, z# f3 Y; i! B4 b. C
- >>> math.frexp(10)5 m* V3 z& }+ l& t
- (0.625, 4)" S; R, t( j, W4 _3 D$ ?% E; V
- >>> math.frexp(75)
9 ^% A+ N1 x( ~* F - (0.5859375, 7)
3 h5 e' e; `$ C# o0 V, \) i - >>> math.frexp(-40)9 O- {+ k% J+ }' c
- (-0.625, 6)
- h8 f/ z, I, _. |! X8 ^3 G2 Z - >>> math.frexp(-100)% t6 s: X& n& F% d) h/ Y
- (-0.78125, 7)
- ?* ]# X+ H: C4 C- [0 o - >>> math.frexp(100)
8 E ~; S& n, t T) ~ - (0.78125, 7)
复制代码 ) k6 y% S( L3 B
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)% h. O5 U) R; r* B: A5 @% Q
- #对迭代器里的每个元素进行求和操作
( [- u& H: H" h. w: | - fsum(iterable)- i( Q- d u: l; z! b4 A3 t
- Return an accurate floating point sum of values in the iterable." v/ Y4 D: ]" v& c2 r1 j# ~2 G
- Assumes IEEE-754 floating point arithmetic.
. x" J! P8 W3 D4 g7 D - >>> math.fsum([1,2,3,4])
7 W/ C! L0 x* _: ?; w6 h1 q - 10.0
/ e0 O% B7 ~ H* a1 J4 H5 r - >>> math.fsum((1,2,3,4))% @+ _$ e. H1 a* X* j8 P1 t8 Z
- 10.0& k$ q% @$ q' v5 \; J' F6 [
- >>> math.fsum((-1,-2,-3,-4))! k; z3 y) j- ~4 A/ _
- -10.0! H* Q+ n- v# \" n: N1 P
- >>> math.fsum([-1,-2,-3,-4])# k, ]" C6 a% u( w8 d" y9 F* J
- -10.0
复制代码 * }: q0 _- s4 e/ H; y2 P
math.gcd(x,y) 返回x和y的最大公约数2 S4 p% `! b" F1 E2 {( F1 e
- #返回x和y的最大公约数5 D5 M4 T5 s" Z; V
- gcd(x, y) -> int
$ f+ E- \5 J- G, { - greatest common divisor of x and y/ G( k1 I. v& k
- >>> math.gcd(8,6)5 m# [& R1 H2 W" L1 H' Z c
- 24 j5 f; q8 y4 }8 C- f( c
- >>> math.gcd(40,20)$ _$ e. v* k" k- A- F( r# I* H
- 20
! n& j1 s u" y1 G9 Y - >>> math.gcd(8,12)
. o% L* m2 F; y4 D5 z: f - 4
复制代码
/ G% M M0 g# Smath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False! W. c- |( J. O' w b. I" Y m
- #得到(x**2+y**2),平方的值2 f$ ^! s0 f, D+ i+ h# H* n$ G
- hypot(x, y)
$ Y6 G6 |. ]8 o' t4 [/ [ - Return the Euclidean distance, sqrt(x*x + y*y)., D: m* y; m% C/ q8 O1 ~: `
- >>> math.hypot(3,4)
! G; @, {6 M& s- G$ L5 X - 5.0
* P/ s3 o k( N/ O) _ - >>> math.hypot(6,8)
1 U: F: o" C. l' c - 10.0
复制代码 7 I7 X5 ^" D. D
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
3 I" K5 k! L2 G2 B4 ~- #如果x是不是无穷大的数字,则返回True,否则返回False! [7 [4 j. ?, N$ \ c
- isfinite(x) -> bool. }4 Y5 J; t) t4 y" H- e
- Return True if x is neither an infinity nor a NaN, and False otherwise.- G. y7 r7 X: C- @
- >>> math.isfinite(100)% N* p9 Q1 N% S6 T2 E ~
- True& z' V! x( O* m8 e0 K
- >>> math.isfinite(0)0 V1 }0 C$ _" b! s8 L
- True
8 l( c9 a; P+ f8 Q2 t% ^ - >>> math.isfinite(0.1)
, b; A' ~3 |4 Y) M- k" Q" L - True
: O% [; B' ~. Y. X. V M - >>> math.isfinite("a")
5 U0 D6 f1 D, z: X# L' p - >>> math.isfinite(0.0001)
: P# J M$ r0 H' S) @$ p - True
复制代码
2 z7 _6 u4 c; T: F) |! \ Smath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
- |( q9 _9 _ z2 |) J. r( B9 h- #如果x是正无穷大或负无穷大,则返回True,否则返回False3 y9 `* N6 z8 g: `4 R3 j9 J: x* ^
- isinf(x) -> bool+ s& Z& b2 W+ O, }
- Return True if x is a positive or negative infinity, and False otherwise.7 }; d: S* |# e- P& n
- >>> math.isinf(234)/ d* V* c% `0 b8 Y
- False
' {7 |% c% s" C% g+ N- V - >>> math.isinf(0.1)
7 |' q( B5 j! j! l - False
复制代码
$ b- A% Q7 S2 y, X! Z) g' Bmath.isnan(x) 如果x不是数字True,否则返回False
/ j) _8 ]- i0 R8 o- #如果x不是数字True,否则返回False
; Y2 R4 {7 ~9 ]7 a6 W+ { - isnan(x) -> bool7 x/ \8 w, \- C
- Return True if x is a NaN (not a number), and False otherwise.
, |5 g) O* ]2 L6 w! U - >>> math.isnan(23)* V! L w g7 d. V
- False
\% O7 G# V; F0 ^; U$ n - >>> math.isnan(0.01)5 m' J$ F7 E/ A* f0 Y: h1 n
- False
复制代码
7 f# ]: r. J. V/ B& [3 o. p- Cmath.ldexp(x,i) 返回x*(2**i)的值
' z( f2 {, O2 ]: t" X e r% p- #返回x*(2**i)的值
7 I1 Y7 }8 ?) w2 }! u6 P - ldexp(x, i)
$ E0 p; _1 @) z& y) d - Return x * (2**i).8 l" h/ Z% [3 t+ k; l0 n$ d. Y
- >>> math.ldexp(5,5)
2 R- r: K6 ], x4 c7 n3 o7 l$ _' y3 n - 160.0
' d" m# E" J s3 C- z5 b" M - >>> math.ldexp(3,5)
; J% \5 P" W& l. W - 96.0
复制代码
: N1 P) v% ?) w6 C8 Pmath.log10(x) 返回x的以10为底的对数8 G8 e9 [! [8 m, I& N d
- #返回x的以10为底的对数) g3 X% a7 B$ V% Y) s
- log10(x)# k2 k9 N4 m& R% H; ~1 r$ z" S
- Return the base 10 logarithm of x.
2 c0 t* D3 l0 r" f8 N - >>> math.log10(10)# L) q0 S" Z9 h& c/ ~. S
- 1.0; f9 {# ?- C- G3 Y9 E
- >>> math.log10(100)1 @, E1 |7 Z: @/ t& y7 [
- 2.08 c/ W; U" B# Z1 P* g( N% Z
- #即10的1.3次方的结果为20, T- e* k2 p% @0 \; I
- >>> math.log10(20)
- |) U* S4 ]# G9 l: T9 B& ? - 1.3010299956639813
复制代码 : a" w8 {5 Z7 e. V* v' j
math.log1p(x) 返回x+1的自然对数(基数为e)的值
* ?' x! z) z2 r. b3 z2 m7 h- #返回x+1的自然对数(基数为e)的值
" q4 ~% s# N: h }) ]% H& d - log1p(x)9 E3 I6 @% h' r8 i
- Return the natural logarithm of 1+x (base e).( o0 Q6 d% k( T8 I0 e/ o1 s, u
- The result is computed in a way which is accurate for x near zero.9 ?# m5 s0 p9 z8 P6 k4 t- w! b' T
- >>> math.log(10)+ d) R& e' W, {# N! _7 D
- 2.302585092994046
2 M2 K" s5 Q* S0 M5 h9 v( E - >>> math.log1p(10)! y& Z$ h- r% n
- 2.3978952727983707
+ R9 m2 S) P7 z9 j - >>> math.log(11): j, I5 T1 b* ~6 v* Q/ W1 }
- 2.3978952727983707
复制代码
6 i3 x, u4 o, B, D1 \math.log2(x) 返回x的基2对数% O/ c6 A8 K8 Z6 l3 i O
- #返回x的基2对数
7 J7 z3 V/ N' L2 Y; v - log2(x)' I" ~! B! l. v. o G
- Return the base 2 logarithm of x.7 w0 @( P4 V8 O- T& T
- >>> math.log2(32), W, ~8 y w' G" z# P' Q. P
- 5.0
0 J. @4 ]( _8 v% d - >>> math.log2(20)
& B4 G5 Y) [ h6 V, W/ F - 4.321928094887363
' I0 u' N0 C0 v: I1 e! y) ]% [ - >>> math.log2(16)
. i6 D2 O) B( I2 C( l - 4.0
复制代码
9 O* i3 w# n1 M6 R; emath.modf(x) 返回由x的小数部分和整数部分组成的元组- i- I. p1 q9 p$ ?0 M
- #返回由x的小数部分和整数部分组成的元组
: B3 H9 C+ n+ M; ~) v - modf(x), d: w P+ d1 V: V/ d6 Z/ i: s5 ^
- Return the fractional and integer parts of x. Both results carry the sign
: e+ j, P5 C/ p" W! ~- A: ~9 L - of x and are floats.
* b& Q; c% U# B0 k+ A) D, H - >>> math.modf(math.pi)7 d3 K7 w& X$ S9 v' V: A
- (0.14159265358979312, 3.0)/ q7 O9 F" n7 I" t6 e' P
- >>> math.modf(12.34), E _0 H p4 |0 ^# E
- (0.33999999999999986, 12.0)
复制代码
! m2 ^( o f0 X0 Q; L: Wmath.sqrt(x) 求x的平方根" N) }3 @8 C& S) `' F! b
- #求x的平方根
6 B9 N- E+ J/ W0 \ `$ o* r3 l2 J - sqrt(x)
; ]8 z( l5 H1 _7 _6 g% q - Return the square root of x., p3 n* Y8 h g
- >>> math.sqrt(100)
3 _% t) i5 a+ E5 r, s, y - 10.0; J% ~2 `/ K3 L8 H i
- >>> math.sqrt(16)
; r$ S" m3 k# E$ |, o - 4.0
# H% k+ S+ L4 ` - >>> math.sqrt(20)
) k' I/ I3 |1 o; ^3 t, V Y. X& w - 4.47213595499958
复制代码
4 y7 w; W4 i1 n8 Q% amath.trunc(x) 返回x的整数部分- #返回x的整数部分
- t: w; N8 x" I7 u - trunc(x:Real) -> Integral' O( f" w9 k& x
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* B% Y: r( G, Q. F G( a
- >>> math.trunc(6.789)
# U! Z$ S0 [" x3 P; x9 _+ ]" F9 G - 6# E: M$ J+ u! Z
- >>> math.trunc(math.pi)
* B- L' s6 |' v; ]: F - 3' T, ]: W2 }2 S/ _
- >>> math.trunc(2.567)
% y! R2 o* S. s4 ? - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|