马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
1 J7 @2 `; Z! ^( `【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 x) E/ V2 b. c5 C
5 T# ?4 ?0 [# y' T3 w: V
方法1:" s4 E0 C( e; Q& a0 n* i
- >>> import math6 `( X. G7 R9 }. C e* ^
- >>> math.sqrt(9)
0 V4 @# [. b9 M9 T7 X - 3.0
复制代码 方法2:
6 M. }+ ]8 E% A- >>> from math import sqrt
$ y( }+ `3 ~/ } - >>> sqrt(9)
- v5 Z) T, Y+ H, B7 }4 } - 3.0
复制代码
7 I0 e6 H5 Z% u$ ]$ Q( q 3 v! b/ z- ~) {/ t5 j
math.e 表示一个常量
/ d' n# x! \/ \1 W( j1 N8 }4 ~/ ~- #表示一个常量" [5 d) s1 R, E6 x6 t
- >>> math.e u3 z. {5 `' u' y
- 2.718281828459045
复制代码
' _* Y3 X( F8 R% f7 ]math.pi 数字常量,圆周率
( T# C8 J6 E, \' g9 g, l2 I- #数字常量,圆周率3 L. a) Q+ o/ v% ~! x
- >>> print(math.pi)7 f! e2 l! S$ m: i5 s6 q$ D# u
- 3.141592653589793
复制代码 3 ?# V! p; g' \/ U2 g' V
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ c' x- `" |0 L) H7 M1 o) e. F- #取大于等于x的最小的整数值,如果x是一个整数,则返回x: c, t; H1 z' N5 z; ?, | J; b9 S
- ceil(x)3 b$ f, o2 I; R/ {
- Return the ceiling of x as an int.* l3 l, _+ O. W( Z) ^6 t2 q
- This is the smallest integral value >= x.
$ v$ E" o: h: I) D9 D - , B" i$ ?4 p- R' Q5 r5 @
- >>> math.ceil(4.01)
/ c G7 M: V: U, a3 @7 r - 5
) u; n a) _9 J, |6 r - >>> math.ceil(4.99)# D" u( I: l0 H( D
- 5
: x) F5 p- {) \+ u1 p; G - >>> math.ceil(-3.99); }" B. T$ k. k' Y0 I4 D
- -3
( s* c A' ?0 y! J, q. Y - >>> math.ceil(-3.01)
( a6 {! [# v! t1 ~; t! z+ V; N2 v - -3
复制代码
) U T) x0 L3 U" z2 G1 gmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% i& u9 {5 M1 o' }1 j$ f, U- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 P* q4 O. b0 C; [! l+ ? - floor(x)
) f1 M; L: Z; B) g - Return the floor of x as an int.
0 D; X: X1 L- K5 E - This is the largest integral value <= x.
. i. |# o' ?' }0 M9 }& L) {; b3 K1 ^ - >>> math.floor(4.1)
, z4 A& n, S& s- W9 O( i/ g - 4
% j# T+ W2 p' p! T/ {6 W$ | - >>> math.floor(4.999)2 q' h' F- r' d7 ^' W: I- j/ s
- 4
+ d N7 f/ x+ X - >>> math.floor(-4.999)
) j8 G0 [1 R7 ^, _! Y - -5
' v" i* J' P( F) o4 e6 S - >>> math.floor(-4.01)
! g% K) X: E4 Y( w+ Y6 [ - -5
复制代码 " r2 p1 m2 G9 ~# _( R$ e" A
math.pow(x,y) 返回x的y次方,即x**y/ y) R* e& ~. u4 g2 G
- #返回x的y次方,即x**y
3 p1 o+ J c2 E: ?! G% S. x - pow(x, y)
9 y. b% r2 j6 t* S3 H! G, G# `6 D4 B" e& ^ - Return x**y (x to the power of y)., U( v! ~# m# P) ^( E
- >>> math.pow(3,4)2 A/ T$ O x3 G$ R; t
- 81.0
. D7 t/ z( C% \ - >>> ) c) V& `5 K4 a/ \7 R
- >>> math.pow(2,7)
' n1 R# V, [6 ?; _ - 128.0
复制代码
; U$ i3 ]3 ^% {+ j7 {! {$ |math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( }( Q6 l# P+ K/ Y% E- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 f m" o4 O, V! |% M: u; V - log(x[, base])- e8 Y* s: _& |9 P, @ D# s V3 W
- Return the logarithm of x to the given base.
& c7 c! B$ N$ D - If the base not specified, returns the natural logarithm (base e) of x.
3 e& m0 D( h+ m% |! V# w" Q - >>> math.log(10)$ X; [+ x5 J3 l+ `; r
- 2.302585092994046
/ _1 l6 x+ V& m+ W( X" g - >>> math.log(11)
; N. F# V" x. g& @' N& k4 x% u - 2.3978952727983707+ E' f% M% e2 q- V S4 h) C
- >>> math.log(20)
2 Y1 Y# o+ ~3 j% _+ v3 ? - 2.995732273553991
复制代码 ( t7 S- i t3 I9 ?: ~" S
math.sin(x) 求x(x为弧度)的正弦值
8 {" s) e q$ `% Y2 J9 X- #求x(x为弧度)的正弦值0 j7 w' M9 t& E* K' O) Y- P. G
- sin(x)
5 H7 x4 r& H7 ~5 Z - Return the sine of x (measured in radians).
) s% U# ~% M- w( {3 _ - >>> math.sin(math.pi/4)
5 j) _' E, y, M- G9 T2 e7 a: d+ w - 0.70710678118654755 G) ]. F# g$ Y9 f+ X3 k
- >>> math.sin(math.pi/2)
4 K. l4 t4 {4 P: d) ?/ n3 T - 1.0% G- e8 s! c( f8 z0 T F
- >>> math.sin(math.pi/3)
2 t. }: F8 R: L5 g0 n# `. x - 0.8660254037844386
复制代码 % ~+ F% m$ A$ g) e' m& C4 q+ R
math.cos(x) 求x的余弦,x必须是弧度: I: e+ K) K# d; \
- #求x的余弦,x必须是弧度# c) Q" Y8 I! J9 G3 l0 E" h% O
- cos(x)5 D+ U1 X7 `, @4 M. Y7 m. K5 G8 b
- Return the cosine of x (measured in radians).$ Z. n8 v% f# q+ ]
- #math.pi/4表示弧度,转换成角度为45度( B E+ D& C9 {% C, o
- >>> math.cos(math.pi/4)+ [& x- ~3 L, t
- 0.7071067811865476
, R3 r8 M& i$ g; |- D t - math.pi/3表示弧度,转换成角度为60度
, j) x! |: J% d# z3 g; F - >>> math.cos(math.pi/3)( Y, ]9 k# C- s; p
- 0.5000000000000001
: h7 d$ t+ S) a& J! ]& ]( p# _1 I - math.pi/6表示弧度,转换成角度为30度
+ |$ m- ~1 u/ v* z3 N0 d. u7 _2 ] - >>> math.cos(math.pi/6), I4 R4 B, p$ w; {
- 0.8660254037844387
复制代码
2 {" B, Y/ M4 i& W$ e) Tmath.tan(x) 返回x(x为弧度)的正切值
. F/ O; _9 { V% l$ E3 }- #返回x(x为弧度)的正切值- k& s& C8 g9 B- p# o( F( g
- tan(x)* R( U& u; x* v9 N
- Return the tangent of x (measured in radians).' f4 m. F, W) @" _2 D* J
- >>> math.tan(math.pi/4)1 D# W9 m" Y: N1 H4 {
- 0.9999999999999999
v9 c) H; n* v& C) A - >>> math.tan(math.pi/6)
& h3 b1 K+ z2 c - 0.5773502691896257! X! h0 n8 V0 A* o% z
- >>> math.tan(math.pi/3)
3 _# v2 I& S' l7 E9 `3 n9 @0 A - 1.7320508075688767
复制代码
* }( D+ C( p7 f0 U' Jmath.degrees(x) 把x从弧度转换成角度
1 ?3 I- p) y0 \ J w- #把x从弧度转换成角度
2 ]2 J9 z9 P; U% z' t( L- u - degrees(x)
7 C5 ]# s4 i1 w5 Y- z - Convert angle x from radians to degrees.8 O9 Q( K* M. l7 w9 n2 v4 m
- 2 o( G6 V2 @9 V3 ^! e
- >>> math.degrees(math.pi/4)
9 E* I% t$ L4 O' a - 45.0
7 {0 M- g% X4 L7 |% i - >>> math.degrees(math.pi)
1 }8 p& n( [2 o - 180.0
* D4 N" F9 o5 k s - >>> math.degrees(math.pi/6)
6 q% @( D2 C* z7 i5 X - 29.999999999999996
- |% n$ D. ~; e* |! l8 w7 `& F$ W - >>> math.degrees(math.pi/3)! ]% U- g7 b5 t; J! s, Y i1 ^7 c& L
- 59.99999999999999
复制代码 3 X! ~) B4 u `* p- F
math.radians(x) 把角度x转换成弧度
& l+ @% V4 \6 w( Q- #把角度x转换成弧度3 a9 e; {! W' e1 r0 k7 C
- radians(x)
" W( D- G/ @ q' I u" t - Convert angle x from degrees to radians.8 |+ c) c1 j( p' Z v2 Z
- >>> math.radians(45): s. L: n: q8 u7 t
- 0.7853981633974483$ I% D8 X6 I' |4 C
- >>> math.radians(60)
9 T+ N$ V u# i5 K" F; U/ F - 1.0471975511965976
复制代码
3 A# E R$ b, N( z$ Mmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
1 P) S( r. K, ^8 |5 x; S- #把y的正负号加到x前面,可以使用0
) O+ q. \( [; v - copysign(x, y)+ \7 l' ~7 g6 U2 ~- o
- Return a float with the magnitude (absolute value) of x but the sign
$ F8 e2 `6 ~: V/ {4 }; H9 X: x5 n - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, U: b o8 {3 E( q# ^9 G - returns -1.0.* F- t" {1 J: O) {- Y
- . @" c/ }" v% f. Y$ D6 J6 n
- >>> math.copysign(2,3)5 x! ]6 S m+ j' L r# ?& P
- 2.0 M% q# J- F5 o' ~; v9 p$ \
- >>> math.copysign(2,-3)+ G7 r9 o; Q, F; Z' [2 l
- -2.0% |$ X8 K$ Z3 @7 R- W
- >>> math.copysign(3,8)
( k X& ]0 C% r. y - 3.0
( Y7 b* M k9 b - >>> math.copysign(3,-8)( I3 L" T# H& m
- -3.0
复制代码 Q* T$ l- s0 k5 H; S
math.exp(x) 返回math.e,也就是2.71828的x次方% \6 I) \/ j. D) |# J
- #返回math.e,也就是2.71828的x次方
/ O* o# a; z' Y) T. S - exp(x)
$ u0 R. w1 B* ~0 o4 C, K- Z# _ - Return e raised to the power of x. }2 u: f2 Y: n' ?( t
! X7 w6 A, K* S; {0 Z; P2 `$ ~; `5 |- >>> math.exp(1); n0 U) \. l; J. Z; ~
- 2.718281828459045
! C6 S3 k7 C; u0 n1 C2 o - >>> math.exp(2)
" z- m8 F' l# ~" ` - 7.389056098930659 u2 Y- k7 J2 `0 p. J! [
- >>> math.exp(3); l. a8 `. @4 _' S+ B
- 20.085536923187668
复制代码 # p* j6 h3 l1 J6 `" Z
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 @7 g2 ]6 T! R) T9 x% [. ~4 P
- #返回math.e的x(其值为2.71828)次方的值减1
: J8 N5 {" C' P5 Y3 ~2 c. y - expm1(x)
+ @0 N- z/ w' U' }5 H. ~ - Return exp(x)-1.2 s O4 S/ A5 p7 v+ t: |
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
2 h9 K7 F0 m0 V' r6 ~# l& n
6 ^! u* v7 i+ L1 [" Z4 I$ n- >>> math.expm1(1)
1 Z# ]) s ?4 L3 a2 g5 r4 k - 1.718281828459045
D8 ]4 \0 f) t8 ~ - >>> math.expm1(2)8 e, ?8 ~7 M4 B& \
- 6.38905609893065
$ [3 \' P) l M# q% n - >>> math.expm1(3)+ ~" G7 w# g+ p2 n0 m3 r6 T+ H
- 19.085536923187668
复制代码 7 u) Z9 c3 e I7 x
math.fabs(x) 返回x的绝对值
+ K+ A; j1 H3 t9 J ^' h) c- #返回x的绝对值
. P- d" c) f" y - fabs(x)
$ o$ R( [0 I/ I8 ^. x - Return the absolute value of the float x.
& h0 L$ u1 m+ x# V( H0 r - - O$ X4 R5 e! H+ s1 r; B
- >>> math.fabs(-0.003)' g3 P3 R: e7 M" q0 B% c
- 0.003' \3 C* Z. m; V) c0 W
- >>> math.fabs(-110)
* U# b5 {7 l+ D - 110.0+ `5 q2 _: T/ d" E) C
- >>> math.fabs(100)4 ^, T3 y7 ~* I! f
- 100.0
复制代码
: }8 x7 A6 |2 W* m8 T& `' Zmath.factorial(x) 取x的阶乘的值
% p; x, v# K0 p% L& T& v. ^- #取x的阶乘的值
0 i4 n9 d! v7 v# C. e4 f9 R - factorial(x) -> Integral2 Z& D7 K4 x! R q9 Q
- Find x!. Raise a ValueError if x is negative or non-integral.9 s- A0 `2 t% a, q; l( X* F
- >>> math.factorial(1)# {: E7 F9 D5 R. ?: Q+ y
- 1
- `+ i" s3 K; S& ~8 Z# r - >>> math.factorial(2)
5 M: {7 O9 H$ ^& x3 G" ?2 J% W - 2
8 O, W0 b( I* F8 `5 u! h% f* R - >>> math.factorial(3)
& T3 \7 E9 d! e! B/ ?, s0 {/ Q - 60 Y. s* H9 I5 X" [
- >>> math.factorial(5)& s1 |+ V8 `2 Q1 b: p+ G. r
- 120- Z+ D- H/ e; A/ k2 s
- >>> math.factorial(10)1 `7 [3 c& |' G: V7 U* N) G! H3 ]
- 3628800
复制代码 ! G4 T B ?2 C' e2 w/ f% r/ S
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数: _) K: v, Q/ s
- #得到x/y的余数,其值是一个浮点数7 T9 N3 l. i# l' e) r
- fmod(x, y)
) r: V0 [3 `; Y+ V# c" H- M - Return fmod(x, y), according to platform C. x % y may differ.
$ ^$ R1 [9 h0 M s: N f - >>> math.fmod(20,3)
]0 R" Z" V9 m" z( Z# k - 2.00 [+ I3 R9 \( N
- >>> math.fmod(20,7)) r7 U2 i% ~ ]2 p8 u* h
- 6.0
复制代码
" j5 `2 [! S% ]0 Vmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: _+ f7 h6 `' j1 @, O0 J) o- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 w4 \( j; c% G+ e8 W - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
3 Y6 X8 ]" a( L; A6 n: L% k2 K# F. \ - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 S8 G+ z! a, P- F7 g# Q- N G, d
- frexp(x)+ S+ L( O c# z
- Return the mantissa and exponent of x, as pair (m, e). t% E: F. b, c! J* J+ i
- m is a float and e is an int, such that x = m * 2.**e.3 z4 e* {& `8 f5 S/ d" d
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
: k" o# a, L" u4 {' O1 W - >>> math.frexp(10)$ r& N. ^2 l9 \/ Z$ _
- (0.625, 4)
7 _( p! s5 T3 k+ f. n1 j - >>> math.frexp(75)5 V, e8 C# a) m) U; n5 m
- (0.5859375, 7)
2 V- t3 q6 L9 S5 [. s, K/ n- L- @ - >>> math.frexp(-40)+ e" R: ?' l" V" t% M$ E
- (-0.625, 6)
/ p4 N, e/ s6 K* ]: s- @" M9 }4 C# P - >>> math.frexp(-100)
' e% m7 P8 w6 @/ u9 N - (-0.78125, 7)
7 i. t" G% m5 [9 \% _0 C - >>> math.frexp(100)! g1 {5 c5 {* x8 ^
- (0.78125, 7)
复制代码
1 J) o1 o6 I- V3 smath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
% _; [: H0 z4 P, T- z- #对迭代器里的每个元素进行求和操作2 |+ A: }& @7 @$ r+ V3 Z
- fsum(iterable)
: K+ q+ F9 P# c* w4 w9 E7 C+ a4 b - Return an accurate floating point sum of values in the iterable.
8 U8 w: w4 G, K4 m2 b6 w) } - Assumes IEEE-754 floating point arithmetic.
& X+ F; N T9 v1 I: D8 U" o Q: y* v - >>> math.fsum([1,2,3,4])
9 w" ?6 U' u3 q% C0 A - 10.0
# M+ H- b- H, J! l& p - >>> math.fsum((1,2,3,4))
& j$ ~% n) W$ r+ c% ~1 X; v - 10.08 @ Z4 {* U! ]' N3 a
- >>> math.fsum((-1,-2,-3,-4))
; [% O, p8 T( o - -10.0. z/ _5 Z# @2 t
- >>> math.fsum([-1,-2,-3,-4])+ O, E( A/ T; E6 W# B- B
- -10.0
复制代码
; c$ s7 q# a; f5 j% Ymath.gcd(x,y) 返回x和y的最大公约数
% L. ^8 @( x2 T- #返回x和y的最大公约数
6 w9 K- f5 c- g1 `$ J - gcd(x, y) -> int2 b/ w" R# g3 v4 _ @7 ?, ?
- greatest common divisor of x and y0 O2 T" b; C* a& ^, n) \) |
- >>> math.gcd(8,6)
( ~# G8 M4 Z$ C( S3 U2 {) | - 2
5 `0 T/ y! [% P! X. ~. a. k - >>> math.gcd(40,20), {! ^5 r" j0 I7 \: w# R( V- b
- 20
! e/ ^# t3 [, R3 l+ K$ v - >>> math.gcd(8,12)
, {- i. n; |5 m - 4
复制代码
) O& j7 Q+ O8 q" d9 D$ u: Y. Wmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 w O8 F3 D* {+ _8 E
- #得到(x**2+y**2),平方的值% F5 e* ? `0 n
- hypot(x, y). v. t4 m. I- l9 |
- Return the Euclidean distance, sqrt(x*x + y*y).
6 C& ` e9 j" o - >>> math.hypot(3,4)
! L; s: e) Y3 x2 M8 g6 [* R, g - 5.00 ]) H8 o0 _$ `5 Z4 J* g# ]+ n. R! ?
- >>> math.hypot(6,8)
7 W; [5 U2 U+ ] - 10.0
复制代码 3 B3 E7 i& V3 B7 `3 H
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
( D" H: {: ?4 B5 _; j8 i- S- #如果x是不是无穷大的数字,则返回True,否则返回False! y1 l& L4 l, i
- isfinite(x) -> bool
" X( M9 t( M0 E - Return True if x is neither an infinity nor a NaN, and False otherwise.4 \6 D2 z* I W q$ G+ G5 R
- >>> math.isfinite(100)
2 O# ~* g, V. {5 X. U - True* U y( ^! l) L6 q9 z9 J; x" J3 n
- >>> math.isfinite(0)9 I% m: V3 q Q- w$ g% ?! _% L
- True
1 F6 J7 F2 r- T% F: B+ |$ N' y - >>> math.isfinite(0.1)% h: i) n. P2 { J
- True& b; d: I2 r* D2 B
- >>> math.isfinite("a")
6 B+ `# S. w7 W7 {' ~% A - >>> math.isfinite(0.0001)
0 K; Q6 h0 a" T1 x4 g3 _# R2 y - True
复制代码
. n) U' e4 k1 E0 _: Gmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False4 P8 M9 t3 e- k/ \
- #如果x是正无穷大或负无穷大,则返回True,否则返回False2 M m5 |; P6 g* j( ^
- isinf(x) -> bool% v+ |/ g5 ~+ J/ F* e7 ?
- Return True if x is a positive or negative infinity, and False otherwise.( u1 }0 u8 G! a" X& }
- >>> math.isinf(234)
* E N( U: J( {+ {" i: ~ - False- z, a- U- ^; [
- >>> math.isinf(0.1). ?5 P& I! c' u- U- d
- False
复制代码
6 l/ Q" ]% B! I& x, I1 T* Omath.isnan(x) 如果x不是数字True,否则返回False
4 F: B- g6 l; _5 u& r- #如果x不是数字True,否则返回False i% ~' P( o: N' ^( ^
- isnan(x) -> bool9 H2 Z# E5 R J! D( E2 Q
- Return True if x is a NaN (not a number), and False otherwise.1 A# Q- S1 _7 I( a5 |% P8 q
- >>> math.isnan(23)
1 d4 R% r* {2 u' ?6 T a& P - False' @% M9 c! k% v9 `, J, I
- >>> math.isnan(0.01)1 @) q1 n& k' D' `* y
- False
复制代码 & r: _) z; n7 X3 e; }! T& H
math.ldexp(x,i) 返回x*(2**i)的值, k! p$ C9 F% W
- #返回x*(2**i)的值
7 n C b. J$ X9 {. a1 U - ldexp(x, i)2 w* m) u' Y9 i0 A) b
- Return x * (2**i).' B& R% d9 {7 n/ s6 B
- >>> math.ldexp(5,5)+ P8 Y1 v8 d+ T3 \& R
- 160.0
* l2 y3 Q0 k* z- @0 {9 ] - >>> math.ldexp(3,5)% B, Z Z8 {; Z: a1 }4 N
- 96.0
复制代码 2 U c- ]) x, M8 @4 {
math.log10(x) 返回x的以10为底的对数
) e/ \( M5 T t4 B* \- #返回x的以10为底的对数6 o& f9 R3 P0 z
- log10(x)4 N* c+ A2 l! E* D6 k+ q
- Return the base 10 logarithm of x.
5 Q2 Y/ F/ K- f- q0 J - >>> math.log10(10)
# d: Y7 O& b. P3 a- \ - 1.0
7 T+ L# J/ L3 C Z+ X+ n - >>> math.log10(100)
- ]7 _7 Y8 n N: e; V/ m4 F - 2.0) l; c% ]3 `* t$ m
- #即10的1.3次方的结果为20 K5 h/ L8 x% S2 v4 Z V7 _
- >>> math.log10(20)/ A- ?/ Z8 `( s* a4 e& b
- 1.3010299956639813
复制代码
/ s0 H3 |$ j' G4 Omath.log1p(x) 返回x+1的自然对数(基数为e)的值( ~( k* e1 f& Y4 y& P" N* F
- #返回x+1的自然对数(基数为e)的值
9 r5 @2 W; Z; { - log1p(x)
- `; G. Q3 p6 Y+ ?/ u( f - Return the natural logarithm of 1+x (base e).6 Z/ S3 r) M; q& r' b9 a, s
- The result is computed in a way which is accurate for x near zero.
, i2 k3 l9 v$ V. f* P - >>> math.log(10), {3 `' t6 D; P$ D. W6 I& X
- 2.302585092994046
8 V4 h. R ~& K0 } u0 l8 ]* i - >>> math.log1p(10)) v0 d" t2 u# T, A
- 2.39789527279837077 F# [* ]) u7 l! u* H: C, Z# a5 G
- >>> math.log(11)$ K4 [. m Y1 @
- 2.3978952727983707
复制代码
2 f. a! i! L+ V/ b. dmath.log2(x) 返回x的基2对数
7 w! [% B0 C+ ?! t; [- #返回x的基2对数
, S7 A+ @6 {6 d - log2(x)
2 ^' B6 n5 K8 T' ^1 \- A2 L0 k - Return the base 2 logarithm of x." x/ k% c" A/ D9 M& T4 B4 c$ W8 I
- >>> math.log2(32)
) X: `1 Y6 b! {% x6 ?; [, y( p% H - 5.0
/ ]* z/ W/ X- c; | - >>> math.log2(20)" A0 J: \) M. K7 U
- 4.321928094887363
# g' p# w0 b/ T* u! Y+ B( \ - >>> math.log2(16)1 m! ^6 H5 ]+ h: V4 u7 y# x. }/ q1 X
- 4.0
复制代码
w% E. E: O% Y; Emath.modf(x) 返回由x的小数部分和整数部分组成的元组
; a& N4 M8 d4 O- #返回由x的小数部分和整数部分组成的元组
9 f R! C9 ]5 Y' C$ \% L - modf(x)
. E& U3 r4 a& g0 Y7 N, U/ J - Return the fractional and integer parts of x. Both results carry the sign
, s+ @- ?( {6 E - of x and are floats.5 q! e: I% o" i6 C
- >>> math.modf(math.pi)+ q# ~ o( }8 k8 f, Y- v
- (0.14159265358979312, 3.0)
- A6 L! V) W6 W! U6 V7 R - >>> math.modf(12.34)& R2 h \1 i) }. O
- (0.33999999999999986, 12.0)
复制代码
A0 |; U6 G5 W" q# a- Y1 jmath.sqrt(x) 求x的平方根# d* V" F/ H/ B5 S" F( e' a7 D
- #求x的平方根( y8 m1 _3 a! [2 \4 q6 g
- sqrt(x)1 _: o8 c' y/ @7 _
- Return the square root of x.% z/ b: V9 R2 V( g* Y J7 P9 u
- >>> math.sqrt(100) |9 L# j# M/ Z. T0 d
- 10.0! y: s/ ^4 y/ U" F
- >>> math.sqrt(16)1 ]2 i' B; R2 \# o3 S& k. x( I
- 4.0
! C' C6 V9 O. T7 b1 S# t - >>> math.sqrt(20)6 \+ w" f0 R& v: f$ {5 |
- 4.47213595499958
复制代码
+ W5 Y7 j: H7 }, Y7 Hmath.trunc(x) 返回x的整数部分- #返回x的整数部分8 z( Q8 b4 }! `9 Q. e. }. p
- trunc(x:Real) -> Integral
' @+ \, G! Z+ l5 n ~; j - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* R* h7 ]& T# R
- >>> math.trunc(6.789)
7 @/ K# R; i& ]' f: z1 s - 6
4 a1 k3 r. ]+ C - >>> math.trunc(math.pi)
+ U. b$ v' i( t7 k3 X( h6 f! c, l - 3
7 {( |+ h$ B# ]+ h( A0 `. Z% R - >>> math.trunc(2.567)4 G. e4 I" L* {, R0 C# }3 E
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|