马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
d" R) a- R% H K- }1 N* E& o【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。5 n" N) S1 z* i3 D0 u& b# O
3 e5 ^" e( Q0 c$ {方法1:% ^5 c8 ~- [. p7 [5 C( W
- >>> import math6 \& y( M" d, B+ p
- >>> math.sqrt(9)" T9 S8 `/ T0 ^
- 3.0
复制代码 方法2:0 `* N, Z) g! h4 c" D+ S7 L
- >>> from math import sqrt8 ], D& U. [; Z, b d0 V4 ^0 s
- >>> sqrt(9)/ S0 q/ h! L) i6 m6 g& s- i
- 3.0
复制代码
, y, j4 \) n6 m& m0 `. |
, i& a* V3 t$ m3 ~" o" Fmath.e 表示一个常量( n4 @" j l6 y0 d9 {
- #表示一个常量% d( N3 w5 J8 z) s: [
- >>> math.e; h# ]: `$ r6 w. p6 Z# h5 i3 F
- 2.718281828459045
复制代码 9 {3 B& s* E9 V& z U
math.pi 数字常量,圆周率
9 n" x0 G8 _1 K/ ?9 ~- #数字常量,圆周率
2 ~8 D, w" p3 e- V4 \. E# H - >>> print(math.pi)
( X% a; a. b2 f D' ?8 a; Q3 t - 3.141592653589793
复制代码 2 b, Q8 H% ~, t# I4 ?
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
f( W& I/ B& W5 t7 Q6 u- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 B( d3 H0 Y! M5 u; B - ceil(x)
5 p6 u }! ]) n6 B3 ^/ R# V - Return the ceiling of x as an int.
: {: P! n0 ^* _' k% T - This is the smallest integral value >= x.6 m4 W9 V* H9 w8 }4 K* b
' D" l f$ D4 _; N# Q/ P- >>> math.ceil(4.01)
0 M; V0 @, T, j0 q: W& N0 F - 5
8 Q! b" Z8 k+ _7 I6 _9 ` - >>> math.ceil(4.99)2 `8 T8 q/ F; F" ]0 V/ ~& F
- 5
; H1 e* E! f4 M - >>> math.ceil(-3.99): Q% Q+ P+ P c' [
- -3
4 G- H( z. | z - >>> math.ceil(-3.01)
# M" u) [1 V5 k& U - -3
复制代码
0 t* b3 P' n3 B9 P$ a% a' Z7 hmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 [3 x' p! b8 \% e" v; T- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身& _8 W) Z( |# a$ e8 o* r
- floor(x)
* r& Z. f: c$ |+ X - Return the floor of x as an int.* F3 S: X4 }1 D. ~# e+ o; w, \
- This is the largest integral value <= x.
8 T: \8 W# a, t3 } - >>> math.floor(4.1)
7 F' n$ G- ^+ {6 u! I& y - 4: ^9 |9 j$ a7 u: J5 W- |
- >>> math.floor(4.999)+ A* _: o' X$ F
- 4
2 y+ A. L; K# Z - >>> math.floor(-4.999); J, k8 }& d# M8 S
- -55 F3 W" ?: f1 o$ s2 x- a4 M
- >>> math.floor(-4.01) o* d) `! D0 Y9 G0 A
- -5
复制代码 6 Z/ S9 s& w# I: c9 q
math.pow(x,y) 返回x的y次方,即x**y
& u9 q! V) N5 m0 z( m2 n- #返回x的y次方,即x**y
' k' z- ]) i% N: n - pow(x, y)
+ N; M9 M2 R8 n: n2 ] - Return x**y (x to the power of y).4 w2 D2 K4 M3 `/ P& q
- >>> math.pow(3,4)% k! B8 u: x$ B0 O' F. m, G% F C
- 81.0
& x# E, [1 t/ f/ U1 k% ~ - >>> H' ^3 g1 Q# Z* X& D; v) ~9 }
- >>> math.pow(2,7)3 O! _4 f# |6 Z3 ^, u
- 128.0
复制代码 ) K. F; H+ K& |) Q9 c+ a& Y
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) A) n+ P. M. Z/ o0 {& R% d1 Z
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
. m+ p$ n# h1 a! K, |5 f - log(x[, base])
3 Y* h! h) G' C - Return the logarithm of x to the given base.
" `1 C$ h0 `- _- h - If the base not specified, returns the natural logarithm (base e) of x.
$ F$ I# v- Z" h6 Y; n! Z - >>> math.log(10)6 K5 B) i7 Q, U
- 2.302585092994046- A X3 i5 K# Y5 x8 R" p2 Y" @9 T# ^( g
- >>> math.log(11)4 Z; o1 t- ~* O+ Y$ h4 \: ?
- 2.3978952727983707( D1 P3 j8 N: [1 Y+ o* s
- >>> math.log(20)
; [4 v, P& t7 _+ U% b. _' f - 2.995732273553991
复制代码
3 Z/ Z ?+ M/ i) W F( F6 Fmath.sin(x) 求x(x为弧度)的正弦值$ X8 K" ^; C( g2 O9 ]
- #求x(x为弧度)的正弦值
! b+ E' ` I h; e4 N6 {( x7 U - sin(x)) |; X( S1 x* `/ L @! Z& a
- Return the sine of x (measured in radians). k; ^" e2 ?% j
- >>> math.sin(math.pi/4)
4 u$ N2 @- [- F6 {# ?( b - 0.7071067811865475
: ^% W* M( V4 b; M. V# y - >>> math.sin(math.pi/2)" \9 s0 Y9 H+ A1 t4 m
- 1.0
7 @! e# U) j1 p O! R- t1 f - >>> math.sin(math.pi/3)
g9 ?% h3 [2 j& L5 k7 o2 [+ Y# _ - 0.8660254037844386
复制代码
8 ~: I8 r- g2 f) J: Q$ Cmath.cos(x) 求x的余弦,x必须是弧度7 ^$ c, r4 e7 M3 w# O
- #求x的余弦,x必须是弧度
) N/ |1 @% b+ E' c/ p& O - cos(x)
) g. p* y$ k0 f" z - Return the cosine of x (measured in radians).
A6 a P* Y# T/ G7 z - #math.pi/4表示弧度,转换成角度为45度4 p8 K: Y3 p( t5 c- j) d
- >>> math.cos(math.pi/4)
' E" o; H. o* K- L - 0.7071067811865476
3 e J j9 n- S7 o" \4 C' m* V - math.pi/3表示弧度,转换成角度为60度- _/ k3 E3 I( _% A$ K7 c: X" m. q
- >>> math.cos(math.pi/3)* x! o( z! G2 ]. C S: q
- 0.50000000000000013 s( _3 [% j3 F5 W4 m) E# v
- math.pi/6表示弧度,转换成角度为30度8 ^" F0 J% H0 b! M& I( K4 l
- >>> math.cos(math.pi/6)
1 S! j! T o/ {# g - 0.8660254037844387
复制代码 5 q& Y" R! T, y( l5 i% N! a& u1 H
math.tan(x) 返回x(x为弧度)的正切值3 Y+ I/ |4 h/ N( G$ f$ W' f& t
- #返回x(x为弧度)的正切值! m+ e) B* d- Y: l
- tan(x)
2 ~/ M/ y5 T" ~9 m - Return the tangent of x (measured in radians).
: W0 T3 S) W6 r" f& Z4 E4 b1 \1 J( Y0 i - >>> math.tan(math.pi/4)
5 l1 x5 U& e" s# U- H - 0.9999999999999999
- V( y8 i: m2 F - >>> math.tan(math.pi/6)4 ]8 R- A6 _; Y& I+ }
- 0.5773502691896257
2 S5 |9 W) ^7 {) p - >>> math.tan(math.pi/3)
: O6 Y7 Y% l. V' R* c& S' a8 @ - 1.7320508075688767
复制代码 1 o$ o7 P* e( @: A7 v$ i7 [
math.degrees(x) 把x从弧度转换成角度1 p% B( U6 C4 l6 y
- #把x从弧度转换成角度/ d$ i# Z) S( X ~1 F5 \! j2 Q
- degrees(x)
: U7 V6 B! j$ t0 z& | r9 H- v - Convert angle x from radians to degrees.
5 s# \5 D0 v1 D P1 ^$ I - . B w- Y( u5 [% {
- >>> math.degrees(math.pi/4)
# [7 n$ L# L/ Q9 c. s) i - 45.01 j+ J! x& t7 d8 O
- >>> math.degrees(math.pi)
; z# m" i: D2 E - 180.0
3 z9 ]+ U9 u5 U+ f - >>> math.degrees(math.pi/6)
) d* E7 G7 A" Y1 f - 29.999999999999996
$ ^% ] `" q7 m, @. b8 x2 e - >>> math.degrees(math.pi/3)
4 M& t8 r% z1 ~' r4 n, `) P0 [3 Y - 59.99999999999999
复制代码 9 y+ q6 c F5 f- S7 k2 X
math.radians(x) 把角度x转换成弧度
4 G9 `/ U: P% q+ f" A- #把角度x转换成弧度% r1 z6 W- V' R- \+ l8 k% J
- radians(x)
. d1 [& i: }4 k! h n - Convert angle x from degrees to radians.
; a% ?9 i4 ?% a" z5 _4 x; X& G9 I( o - >>> math.radians(45)
9 P! k" w. h: B! @& ^4 _ - 0.78539816339744834 T7 L/ ~; ~, y. x* j
- >>> math.radians(60)
* m& N" ~" u& t: B - 1.0471975511965976
复制代码
! D8 U! s/ f9 i( n" z9 wmath.copysign(x,y) 把y的正负号加到x前面,可以使用0. _; j- [3 }* v O
- #把y的正负号加到x前面,可以使用0 M% G) s5 s: H1 F( f6 N: w! g
- copysign(x, y) I) m& X* ~& B G
- Return a float with the magnitude (absolute value) of x but the sign
' i& P8 @$ u ?& z m( g G4 J - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
8 }0 _; K2 \; z, H" A - returns -1.0.
7 ^5 p2 P0 A2 M1 J& i
+ [7 p" L, H6 C% i7 [: J1 Q% h- >>> math.copysign(2,3)7 `. M0 F N/ k0 k! V8 m
- 2.02 o0 f# ^8 Q6 `
- >>> math.copysign(2,-3)* h6 q" D l- I' z$ o# r7 T: p8 d
- -2.0
% c* a, }; q+ g. V - >>> math.copysign(3,8)
$ F7 y, E6 ?. u( L - 3.06 Z6 \4 ]8 p( N3 c8 {; S
- >>> math.copysign(3,-8)
* a/ g, ^2 {. A6 c4 ]3 N - -3.0
复制代码 , b+ P& O# O- s! w
math.exp(x) 返回math.e,也就是2.71828的x次方7 N9 w9 P! J& J& O# \' R' J
- #返回math.e,也就是2.71828的x次方$ Z0 w8 K# Z# l& m- P4 F
- exp(x)
6 k2 @5 S1 @" P9 I& J - Return e raised to the power of x.! a( ~' _: v0 W# P9 E
- $ m% n5 f2 U0 [6 c+ R& A
- >>> math.exp(1)
q. G# s2 P$ @4 V2 P; \7 ^1 i - 2.718281828459045 A, F: B: } k# `+ v4 t9 h, H, w
- >>> math.exp(2)
! g/ x0 h3 H# a% C& A! g- t - 7.38905609893065$ O) @' j( Q( `4 B- _5 _' H0 `
- >>> math.exp(3)
- [, O% M3 N! Z, j' J+ S! G. k - 20.085536923187668
复制代码
- Q/ z E; U2 X6 ~* P- Ymath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减17 |' ~/ O$ L9 K3 G8 n
- #返回math.e的x(其值为2.71828)次方的值减16 [6 H* v+ U$ T' a0 J8 ?
- expm1(x)0 ? M& T# @4 }0 j9 H
- Return exp(x)-1.6 h. V7 O/ v+ C3 x: m9 ?1 [" h
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
4 ~* O) E, n7 T- I4 p& L& f - ; A; o, M2 n+ n
- >>> math.expm1(1)
# s( E5 b/ a, c: g - 1.718281828459045" |. O2 Y6 T2 ^' C2 y) J
- >>> math.expm1(2)
4 @4 d* ?) `* m. N1 N% ]; z4 l - 6.389056098930659 Y5 P5 @- `: f7 j; l! K) ~3 T8 a& X
- >>> math.expm1(3)0 g- U" K0 B2 b2 |& X; C. r
- 19.085536923187668
复制代码 ! _5 |+ k2 r* E
math.fabs(x) 返回x的绝对值
1 z& x8 l. p5 u% b( [8 Z- #返回x的绝对值; Y: e' r8 t7 l' \4 s) \
- fabs(x)" M' H1 e/ e/ n. P* Y
- Return the absolute value of the float x.( k) o5 ^+ @7 t" ~% m% n
- 8 i. W" B; s$ v. q$ H8 o; U* ?
- >>> math.fabs(-0.003)( W/ z9 C: V- z' ^) o& P
- 0.0038 U5 p+ B2 f/ x; q
- >>> math.fabs(-110)
$ \2 x# y% }6 F/ \6 u7 g( v0 M, d* g8 m - 110.0
- r, L( N3 h$ ]% R - >>> math.fabs(100)+ x7 g c4 x/ V! U3 W2 {! l4 x) g: T
- 100.0
复制代码 ' y: h8 j; R# r/ O
math.factorial(x) 取x的阶乘的值, _! Y& G0 ^6 U$ P; Q7 f& z
- #取x的阶乘的值
+ \+ H( o( R9 a+ o6 a) g - factorial(x) -> Integral
, q( t6 r2 B5 t- I3 h - Find x!. Raise a ValueError if x is negative or non-integral.
( P3 x5 e& k7 s( J - >>> math.factorial(1)" A* M% @- d- F. k- m/ z" G
- 1, t. _& A0 u4 l
- >>> math.factorial(2)1 F- @5 Z* S7 H) X
- 2$ r: D3 N* }* n2 L" v+ @. T( L8 q
- >>> math.factorial(3)
9 a* E3 [. P5 X0 } - 6
/ s, w/ J0 k8 | - >>> math.factorial(5)
/ Q5 _. R) G" Z4 C n - 120* L' y( _- r. [) L
- >>> math.factorial(10)4 L+ z' T) G# m5 f
- 3628800
复制代码
5 [( H' s2 h$ d2 g' ?1 X4 K, ]math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
7 g" S' O' K' _$ H" i9 v- #得到x/y的余数,其值是一个浮点数. I6 i% Y1 D5 U* |0 b8 H
- fmod(x, y)" d5 z. V6 d8 p4 j* {: g; ?* w2 f
- Return fmod(x, y), according to platform C. x % y may differ." Y3 I1 z' k, o
- >>> math.fmod(20,3)
, |3 a8 D4 ]- ]+ y2 ` - 2.09 k1 p t5 E/ [- [7 n
- >>> math.fmod(20,7)2 L8 d# B( n/ X/ B5 k: n' N1 @. U
- 6.0
复制代码 & n+ v! p, A$ @6 H. q5 j# @/ w
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& c2 g* G! w; d5 p1 e
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,& J# b6 q0 \6 b8 x/ f! g
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 @5 C; D( R8 @
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
h$ |# D$ a9 }# h" q - frexp(x)
5 s+ R/ M$ [# Y8 J( R/ e% ~ - Return the mantissa and exponent of x, as pair (m, e).& L9 a! \% N; M" y! N% {! i
- m is a float and e is an int, such that x = m * 2.**e.- ]2 ^; u+ n, n( r; p
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.. w8 O6 m5 b. I+ X, }: Z: a
- >>> math.frexp(10)$ b6 g1 W, @: b" \8 Y n* ^
- (0.625, 4)
' K0 V, c1 D! U( A - >>> math.frexp(75)
! _8 ~/ Q7 [9 `! G) X - (0.5859375, 7)$ V8 S, N; h# G' v& c
- >>> math.frexp(-40)
+ F9 m1 Y% t' I. G( K - (-0.625, 6)' x$ M9 x" V' K; [6 f" q
- >>> math.frexp(-100): z& t2 s0 m' P) v& n/ X( T# ?, c' s
- (-0.78125, 7)& c6 m/ L' x2 U" k
- >>> math.frexp(100)
- s9 w: \; d$ T4 K& v+ s - (0.78125, 7)
复制代码 1 T2 S: v- p9 y: y
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)% o8 D& Q4 G. }0 U* T0 ]7 D5 }
- #对迭代器里的每个元素进行求和操作; A- F1 ~% {+ B; G s3 b$ q' y# Y r
- fsum(iterable); E8 h" Z6 W: `, L, U
- Return an accurate floating point sum of values in the iterable.
3 M" S) L% Z% g) @' G - Assumes IEEE-754 floating point arithmetic./ F9 ^' Z2 U; s0 B
- >>> math.fsum([1,2,3,4])$ X) ?( N5 b4 Y# Y8 }8 C5 |6 X
- 10.0) M5 Q" g, |0 M8 u+ a9 i
- >>> math.fsum((1,2,3,4))! N$ r! }1 z7 `! c! ^) Z
- 10.0
8 b& _& w+ w- V - >>> math.fsum((-1,-2,-3,-4))
% S6 |- W% |) C v - -10.0+ D5 \( t* u" p. m- n/ b9 ~+ h8 f
- >>> math.fsum([-1,-2,-3,-4])
. w5 x& ]9 [1 W i: ?( _ - -10.0
复制代码
1 i5 `1 _6 p/ h6 b4 cmath.gcd(x,y) 返回x和y的最大公约数
. x6 l( J* ?1 ?0 m; v- #返回x和y的最大公约数
: [! M4 u$ t& X6 h0 D8 k - gcd(x, y) -> int
7 ~+ x$ ` P; D1 M: u5 T9 y" S - greatest common divisor of x and y" U4 [. e" |" a% G+ Z2 s: T
- >>> math.gcd(8,6)( c" v0 s$ l0 K3 K
- 2
( b: o1 ` ?9 V) e$ g4 B - >>> math.gcd(40,20)2 r/ V! M; h3 j& q
- 20. c. p, @+ l9 T+ a
- >>> math.gcd(8,12) b7 u2 r+ A, I# b; q8 b! y S5 \
- 4
复制代码 i7 l: A- R1 w. |. z/ g, j
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 X8 ^: r4 i4 L; X0 r6 f
- #得到(x**2+y**2),平方的值" G! [. M# f/ B* p
- hypot(x, y)
- L3 N! S# g: j9 e& i& F) G - Return the Euclidean distance, sqrt(x*x + y*y).
8 j6 J T5 O9 I |# r - >>> math.hypot(3,4)) A0 `- h9 m1 A
- 5.0
$ n+ @2 L" l& u1 C3 J# p* ~8 p - >>> math.hypot(6,8)
6 ^8 |# ^ H$ I4 Q6 G8 R$ y - 10.0
复制代码 8 U* i6 S& c. P. {9 z3 b- [2 i
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
# x$ a9 v+ k8 M, r# `- #如果x是不是无穷大的数字,则返回True,否则返回False
4 |/ A) j: b3 j9 ^" D - isfinite(x) -> bool y9 `2 v$ E2 O
- Return True if x is neither an infinity nor a NaN, and False otherwise.
2 X9 U5 k/ S" P( E - >>> math.isfinite(100)" x. F4 Z) V0 d3 |( \
- True
/ v* i, `, T: U. w0 A - >>> math.isfinite(0)
) G f2 s! E' B8 D - True
7 u- o: I, ~0 L& r u+ n5 p! ` - >>> math.isfinite(0.1)
9 B0 p1 Q) f) F - True; j N( P0 W p) l
- >>> math.isfinite("a")9 w( Z' o! V1 K, e
- >>> math.isfinite(0.0001)2 Y; e: V8 ^& D; |( c' b5 v, V
- True
复制代码
& w+ O# Q$ |- E% `4 R; kmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False$ X1 D- R# T, f7 b9 s
- #如果x是正无穷大或负无穷大,则返回True,否则返回False8 P7 C! n2 U t6 c, E% b
- isinf(x) -> bool
e, D% [: O3 q( ^$ o | - Return True if x is a positive or negative infinity, and False otherwise.
; U) T; [+ b9 h4 a6 R5 Y& b2 P - >>> math.isinf(234)
1 O% B! N$ | z1 L* P+ u% n( U - False
o- J. J! j) e+ D# } - >>> math.isinf(0.1)) u R9 J$ q0 c' n' n
- False
复制代码
2 p, q3 s) M i9 W0 g2 [) Cmath.isnan(x) 如果x不是数字True,否则返回False2 v; a& `+ M& q# G* V
- #如果x不是数字True,否则返回False
' b4 }, \9 ?" W& m. o4 t; ]' ` - isnan(x) -> bool
% N( w3 L2 {+ h - Return True if x is a NaN (not a number), and False otherwise.9 N2 d& g4 u( s b# Q2 p
- >>> math.isnan(23)+ j3 a* X' |( l9 c; t* ~* G
- False) v) z- E! q. V. r
- >>> math.isnan(0.01)6 z% I9 \' f8 U. c& f7 m3 y
- False
复制代码 " _$ ] r6 v- y' f7 A
math.ldexp(x,i) 返回x*(2**i)的值
8 _9 ]* s: Y+ q7 g& b F- #返回x*(2**i)的值
. c0 D% b! D. [8 _! P - ldexp(x, i)" R$ I# ^! Q( `3 V( }% R
- Return x * (2**i). b: A7 V! X- v9 U! Z
- >>> math.ldexp(5,5)
/ D( E7 v1 ~; M: |# |$ S5 R - 160.09 K* t, q- @( J( X$ G' @. y! I+ W
- >>> math.ldexp(3,5)
. d, |. x* }, T# C - 96.0
复制代码
" p; c) z7 u, E* Xmath.log10(x) 返回x的以10为底的对数
& ~3 p$ t4 M1 I. I- #返回x的以10为底的对数, k7 ?# C- M6 y; j( g! q c1 c8 I
- log10(x)
% l+ Q: Z! v8 o4 N3 i; k- T- ? - Return the base 10 logarithm of x.
3 j" E9 Y( l- ^- g+ e- M1 {% c - >>> math.log10(10)
3 b9 O) C& y& _! p9 U7 Q) d - 1.0
( j9 o6 k! W. v* N( | - >>> math.log10(100)
: V5 k D4 T' }& E1 Z - 2.0
' M. V# w& |6 U% }4 h - #即10的1.3次方的结果为20. ?$ v3 |* @3 \% m# {+ G
- >>> math.log10(20)) K5 C/ u' o: q' y6 Q' f' e; v
- 1.3010299956639813
复制代码
9 x4 _0 Y7 k8 Ymath.log1p(x) 返回x+1的自然对数(基数为e)的值
$ ~5 b% W: ?3 K O+ o/ s6 o- #返回x+1的自然对数(基数为e)的值: x d* M1 Z) v( i! B* B
- log1p(x)% T Z" |* v4 w2 r
- Return the natural logarithm of 1+x (base e).3 P% O% M7 `- G9 H
- The result is computed in a way which is accurate for x near zero.
+ i4 o& _7 q6 K/ T* r/ ] |1 C - >>> math.log(10)
1 I. h ~3 z3 i$ L8 X - 2.302585092994046+ G9 p; ^) ~& h& j- U
- >>> math.log1p(10); Q$ o7 z5 n0 H3 @8 F8 U3 B) ~
- 2.3978952727983707
9 \: [5 B9 j% M+ }% W* T1 X, K - >>> math.log(11)( B! S* }4 s8 @" v8 [: l9 v
- 2.3978952727983707
复制代码 1 C) H/ Z3 ~: n6 X, Z
math.log2(x) 返回x的基2对数
% F0 _; [# g; g8 u( a/ l- #返回x的基2对数( U: a! x- f; Z- N$ [6 ?1 C: j9 r
- log2(x)" Y. o) m, C S9 i
- Return the base 2 logarithm of x.2 G6 p8 [ K8 Y3 y* k) O- G4 p
- >>> math.log2(32)
' Q( Z: Y* V9 I& u' I! o, N$ O. ] - 5.08 J9 L9 e x+ s8 O6 ^) f" l3 W8 A
- >>> math.log2(20)
/ l2 |. _, }& x, r - 4.321928094887363$ W$ I2 ?" Q/ G$ r7 }) p0 z
- >>> math.log2(16)7 \1 Q9 Z2 x5 n, H
- 4.0
复制代码
. N) s, x7 x7 u8 C. o1 ~* }4 o2 |math.modf(x) 返回由x的小数部分和整数部分组成的元组
$ t7 W$ Z: \3 z* w6 d- #返回由x的小数部分和整数部分组成的元组2 @8 Q$ L- _: B- r0 d6 \% k! j
- modf(x)0 y* M' {" J: j2 V- E
- Return the fractional and integer parts of x. Both results carry the sign
, [4 H# x/ `+ t - of x and are floats.4 C6 Y4 x8 ~7 X+ n' R9 F# k) N
- >>> math.modf(math.pi)
. Z! R2 _* u* q - (0.14159265358979312, 3.0): j# s& f# ~0 D
- >>> math.modf(12.34)
; } [9 q- \9 m( _ - (0.33999999999999986, 12.0)
复制代码 ) |7 I" E) i# w
math.sqrt(x) 求x的平方根
t( n: i5 D- _- #求x的平方根
7 T9 d8 k& J4 b2 g+ a0 G - sqrt(x)3 s2 D% @. [& }# Q6 L' t
- Return the square root of x.4 k' S) I9 h, o3 M. M0 ?
- >>> math.sqrt(100)
8 \ D" j6 ]/ k; v2 v" f, J - 10.0
) L9 y' y2 A/ J - >>> math.sqrt(16)/ @: }9 J* s+ E% ?' l
- 4.0" t% N \- M W+ h3 t+ P( M' Q
- >>> math.sqrt(20)
: [# V) R$ m& x2 A. k' Z' i - 4.47213595499958
复制代码 " t: u0 m1 @2 e2 k H; N; \+ ^1 N5 A
math.trunc(x) 返回x的整数部分- #返回x的整数部分. L( m4 A0 y. ~* ~5 a
- trunc(x:Real) -> Integral
6 o4 k( ^ u4 d0 W Y - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% `+ [# \5 `1 p1 o
- >>> math.trunc(6.789)
& w" }2 S& K4 G( l& `0 I& p - 6# q5 K& H, `. i2 C( t5 i8 E, K* E4 J+ N
- >>> math.trunc(math.pi); Y7 a' U' G8 @! F1 ^0 s
- 3" \+ w; F0 x4 Y: K. Y# L, X
- >>> math.trunc(2.567)% r( j( d$ [6 L( B
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|