马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) E4 \& c1 J& |, U( l+ m; w【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 j" z E4 u) j$ g9 ?" P
$ m4 N+ j# m5 ^方法1:9 U! t+ f0 _) }2 l& w; H
- >>> import math
$ T3 }: |0 X/ F% }9 |: B+ J% R" @ - >>> math.sqrt(9)* j* M1 H0 m( C1 N: B& k
- 3.0
复制代码 方法2:
* E2 X9 k5 E* Z Y- >>> from math import sqrt
8 c. z1 B, l7 N1 g - >>> sqrt(9)
8 _' ?/ q3 o# g# W# e: C" \4 T - 3.0
复制代码
6 F. f+ Q$ @0 q$ j5 ~
1 {; K$ Y+ b! j* s! H& J/ A& ]math.e 表示一个常量
/ G1 j+ U* |& f2 I4 A' A9 g0 {) e- #表示一个常量9 c* t) J9 [; T" [
- >>> math.e( r' D6 s- [0 d2 w% T x+ X
- 2.718281828459045
复制代码
: ^9 T' J2 E$ j4 U' ~6 wmath.pi 数字常量,圆周率
: f2 C3 Q4 y2 ~! l- l. d+ p- #数字常量,圆周率
3 Q, `% b3 V, k - >>> print(math.pi)# n h1 o7 _4 t- O- b2 h
- 3.141592653589793
复制代码 " f* x" d9 Z! M
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x) ?* e; ^" u* m5 k4 F
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x. v, U! f; O# b3 w! \8 V
- ceil(x)* W0 U+ F" F* t
- Return the ceiling of x as an int.9 F0 F' _1 p! |6 n& Z4 k
- This is the smallest integral value >= x.
( G$ B0 p8 W& @; w/ u
( k8 }2 I6 V. ~, n! I- @1 ^- >>> math.ceil(4.01)/ Z6 h; p7 U1 m
- 5
& I% V- b3 c0 b. \# V' S - >>> math.ceil(4.99)
( ]4 q; O( W9 P! d) g - 55 k6 Y. W. Z/ l, g! r8 b
- >>> math.ceil(-3.99)8 W9 R2 ?4 ?8 F# A, ~8 E- v, W3 a
- -3+ m$ } a- b% S9 s' P
- >>> math.ceil(-3.01)' ?: ]9 x6 m0 r
- -3
复制代码
* o- w" g$ Q" @2 ]2 \7 B: M9 qmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
2 B1 n7 L* J; R1 T- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 Y9 \7 y5 I: k# B - floor(x): Z% `) p- q9 @! _
- Return the floor of x as an int. D- ^9 @3 `" J; T" \1 C8 r
- This is the largest integral value <= x.: D. A* F- u) _8 L1 t
- >>> math.floor(4.1)3 @; C3 V# G; D* E' t" L
- 4
) L- I% m$ {3 S; T! X1 x - >>> math.floor(4.999)
! b! T* }. f5 L$ L3 s - 4
3 g4 S. @: v# k0 v/ \( Y - >>> math.floor(-4.999)/ V8 d, r- n9 {0 h
- -5* L7 q# q' |* y# @# Q) a
- >>> math.floor(-4.01)
6 H6 I# X4 P) J, m" h3 w - -5
复制代码
7 y8 \; `& }' b1 r& X4 ymath.pow(x,y) 返回x的y次方,即x**y% M0 C8 g6 W# `; c( _) h& I: U
- #返回x的y次方,即x**y9 \8 b+ f4 e2 @! S4 y7 i. A
- pow(x, y)! _! f; I# Z! t6 I* j7 p3 I+ f
- Return x**y (x to the power of y).
. _1 w' j8 i3 Y$ y" Y6 V# M: b - >>> math.pow(3,4)4 l+ E3 l2 J$ z; a; j5 q
- 81.0
/ y; j# c+ D& b8 D - >>>
% e# w- O8 D0 ]# f0 A - >>> math.pow(2,7). F# \9 r- A7 T2 X! A t0 F, e
- 128.0
复制代码 # i8 s& [5 D4 K% M- Z2 t$ p
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 N# {! S7 S: y4 @/ `- w1 v: K- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! |5 O5 _* R5 |% E# r1 j3 C - log(x[, base])
4 @6 H& Z v# ?3 ?$ R j; q. u - Return the logarithm of x to the given base.
! H; G8 B% W$ h6 W4 L - If the base not specified, returns the natural logarithm (base e) of x.
V* K5 A- }+ D* D6 m; Q - >>> math.log(10)
7 S. X1 g3 a2 O O - 2.302585092994046
. X5 B1 O, {+ x$ \! P - >>> math.log(11)2 |7 b& b; F/ a( Z8 O
- 2.3978952727983707/ B( b+ [% g6 A. T; A
- >>> math.log(20)6 _' |( R5 q) V; u7 V& R8 V
- 2.995732273553991
复制代码
. \) J; P+ ~/ O6 Wmath.sin(x) 求x(x为弧度)的正弦值
- X* _5 X7 M ?- #求x(x为弧度)的正弦值! C9 z0 W2 J. P- C! _0 k# G" M
- sin(x)! n- Z) k i( t5 {
- Return the sine of x (measured in radians).
0 g; {/ m7 c! D( f1 i o - >>> math.sin(math.pi/4)- }# D1 [ n ~0 b4 g' U0 z
- 0.7071067811865475
! M4 \9 Z1 B: a$ F - >>> math.sin(math.pi/2); G6 J' m! b, y% K7 r& m
- 1.0
+ }* E- w1 l& Y8 ?# x: I. _/ k - >>> math.sin(math.pi/3)
( K* T! S- _! W. E - 0.8660254037844386
复制代码
1 f* a- _8 q1 l8 jmath.cos(x) 求x的余弦,x必须是弧度* M+ ~4 ~% N# ^# F
- #求x的余弦,x必须是弧度! f9 B0 I+ X+ K2 O0 i- U
- cos(x)6 B/ y2 W3 |3 e
- Return the cosine of x (measured in radians).1 t! [& o" L2 M5 h
- #math.pi/4表示弧度,转换成角度为45度
; k: \# n0 b3 t7 S- h u, Z6 L - >>> math.cos(math.pi/4)
2 K- x) U3 ?+ h) k/ j - 0.70710678118654762 F% R# o9 c7 n9 y9 V& Z- D% O" J
- math.pi/3表示弧度,转换成角度为60度
1 z5 f/ i2 y7 ~ - >>> math.cos(math.pi/3)
; U2 l7 ]& i( m# I0 [ - 0.5000000000000001* ~0 q. L- E) H* x
- math.pi/6表示弧度,转换成角度为30度4 k$ J" Q3 l$ W0 J* u& I
- >>> math.cos(math.pi/6)
' x; T5 w2 v. i5 e7 v4 E - 0.8660254037844387
复制代码
0 Y# c% H4 h. b0 k/ w/ _, vmath.tan(x) 返回x(x为弧度)的正切值
' q4 n6 Q; @; _4 ]# B3 j- f- #返回x(x为弧度)的正切值
' Y/ |' z: R4 R. W' e$ M+ Z - tan(x)
2 R2 y/ u+ n$ C o, P* I# p5 u - Return the tangent of x (measured in radians).
8 Y. F& c% k$ ~6 S+ T; ` - >>> math.tan(math.pi/4)
% z& s U+ A9 D) o" \; Y - 0.9999999999999999( \& d! W$ u0 F5 R# r
- >>> math.tan(math.pi/6)
) x4 v8 J' ?& U: e - 0.5773502691896257' s8 k/ }3 |0 h
- >>> math.tan(math.pi/3)# G! |, x3 w' z5 N7 b( s1 N
- 1.7320508075688767
复制代码 / w7 k& \) V2 ~8 U0 M9 g; Y
math.degrees(x) 把x从弧度转换成角度' l9 ^* g' c8 d* J' T0 ^
- #把x从弧度转换成角度2 d0 f3 K/ f7 Z/ v
- degrees(x)* Y/ S% a* e! d6 w8 u& z& N7 W
- Convert angle x from radians to degrees.7 s4 ^! ?9 o! C% M7 l% k, q
$ P. R% x& P t0 }- >>> math.degrees(math.pi/4): ^' t4 t! Y! M* O+ t7 ^
- 45.0# ~/ h* G) B; q+ A
- >>> math.degrees(math.pi)
0 E$ P2 a/ B1 Q a' F6 b% f - 180.0
+ D5 d5 u% l2 B$ J& V& Y$ Z - >>> math.degrees(math.pi/6)
3 Y2 C0 _8 N, Z, l - 29.999999999999996! d0 W" Q4 |2 P3 a, Z' @
- >>> math.degrees(math.pi/3)# c) k: K; X Y! ^0 G5 m
- 59.99999999999999
复制代码 / i( E0 k7 d' k" p o5 V7 ~
math.radians(x) 把角度x转换成弧度$ p$ a$ F+ @7 Q1 b
- #把角度x转换成弧度
: T6 K8 X+ Z, X) g! Q% h2 G - radians(x); S1 X% ?# j. A" L+ ^ l" _
- Convert angle x from degrees to radians.8 q( _0 x( }2 K* U0 p |* S
- >>> math.radians(45)! Y3 J" G' o$ u1 r! I
- 0.7853981633974483
; H( O! J |- g$ R; i2 S2 D7 N; x - >>> math.radians(60): m5 I L* a3 s, `: o) z
- 1.0471975511965976
复制代码 * C/ _2 W1 { e
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
4 F8 ^# H% {- ?- #把y的正负号加到x前面,可以使用0
. q3 p4 _2 o3 Q' G' `& u. q9 h - copysign(x, y)3 g; B# g5 l2 P; @+ x9 P! J6 y
- Return a float with the magnitude (absolute value) of x but the sign
3 @; e& t* _ A( q C4 k4 N - of y. On platforms that support signed zeros, copysign(1.0, -0.0) " W% ~8 K7 D/ N4 g5 t, k) A
- returns -1.0. `" B N2 r# t% z+ i/ n/ ~! h
" k" q3 L- {3 P ~- >>> math.copysign(2,3)7 m( q2 }6 ^$ @4 b
- 2.0
+ a0 \, @4 ]- t, Q/ y3 R3 R - >>> math.copysign(2,-3)
! T H8 e" S4 F6 f0 Z& K - -2.0
- a4 [% w! A' i" ]/ f" E - >>> math.copysign(3,8). N7 e6 s( C# _3 D9 z" s
- 3.0& ?+ t# F6 k7 H( b
- >>> math.copysign(3,-8) F( G: }9 R- d. K# N9 ^
- -3.0
复制代码 7 r% g' j& ?" v5 M
math.exp(x) 返回math.e,也就是2.71828的x次方8 S" [9 C& j) [, X M
- #返回math.e,也就是2.71828的x次方) l1 \7 \: i& y6 j+ T9 {
- exp(x)
1 O5 {+ Q1 j7 \& X - Return e raised to the power of x.
" n& _3 l! B0 k& ?! U; A! n/ ^+ Z
c3 j! k( m; ~1 J. c1 t- >>> math.exp(1)
0 ~: u" N7 }( M* ]: v& } - 2.7182818284590451 S! S. k2 Y" G, i+ V" P- x# {
- >>> math.exp(2)
8 n5 @# C8 z1 I4 p( a* K7 ^ - 7.38905609893065
+ C' B' h0 m) f% g* O% I - >>> math.exp(3)
, e9 [- N0 M9 R+ ]' q$ P: W, i/ s% F - 20.085536923187668
复制代码
& Z3 P0 F0 i$ emath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
; A; P3 b, F* f5 d/ t8 O- #返回math.e的x(其值为2.71828)次方的值减1
G1 g& H" u7 Q - expm1(x)
3 d- E- o& e) J; k a - Return exp(x)-1.
1 g, K6 n1 M7 X# g- z9 ? - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ ?$ m+ i; T, A& s+ s. h# O# L
! m& q7 O1 s, T; D0 L- >>> math.expm1(1)# _1 x: s1 T8 v0 C; p3 x
- 1.7182818284590452 w- Q! ]7 J L! o: L, x' g
- >>> math.expm1(2)
1 y: ]4 I' M1 G/ V3 f6 q - 6.38905609893065# M% ?+ _4 ~7 F4 h
- >>> math.expm1(3)
7 k) k; P! _# [6 H' c( Q! q - 19.085536923187668
复制代码
4 d$ c, i8 B1 fmath.fabs(x) 返回x的绝对值9 A% w2 y, j1 z+ Q
- #返回x的绝对值" m. {; F, h( C& W' U
- fabs(x)% Y* c5 M# T5 M+ ~, H9 j' m ?, `
- Return the absolute value of the float x.- r. k& X6 l; D2 D7 p( S. u
- % o: _- E, O$ N4 w7 h6 U) [
- >>> math.fabs(-0.003) C- U0 X# Y' H* z. @
- 0.0032 S- p$ M0 _& Q; ?. ?( I- ^
- >>> math.fabs(-110). {% ~' E: V* A; [& n( O% q. h* c
- 110.0; s0 e3 }: a" T/ W8 ]4 w
- >>> math.fabs(100)2 m+ X9 v- x: d# T, l
- 100.0
复制代码
+ K( b. | o* x2 f; i! v& r; qmath.factorial(x) 取x的阶乘的值3 R5 S" C1 l' A O6 u2 x3 p
- #取x的阶乘的值" B8 g4 F3 j" x9 x
- factorial(x) -> Integral
; e+ w+ L; O" Y4 P- G: H( e7 C - Find x!. Raise a ValueError if x is negative or non-integral.1 Z6 S, E) b9 ]/ m1 _: G; N
- >>> math.factorial(1); h$ R4 w0 V" a- S
- 12 y2 c1 u) k; H# A8 H- C
- >>> math.factorial(2)
# ^5 P% y X/ }9 r% c - 2
4 }: A( j; U' n7 H3 _ - >>> math.factorial(3)
* e+ Z' q& A9 F$ m: _& ~8 f - 6/ v% U$ j2 O! \3 M1 f. r5 s% e
- >>> math.factorial(5)$ N' L+ b; W1 }! f$ }! y! k
- 120, `' h8 d( d1 T/ P1 V6 J: U ]
- >>> math.factorial(10)( F! u: D% k% U# L5 Z" F6 [6 [
- 3628800
复制代码
% i8 q: l! N# f6 ?9 k6 Umath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
# `1 _) ^! R, i- #得到x/y的余数,其值是一个浮点数: ? h6 `) D7 A6 m
- fmod(x, y)
! _5 w9 X( j- Z) X - Return fmod(x, y), according to platform C. x % y may differ.8 W0 }' Y, r( z$ P: U' H$ V
- >>> math.fmod(20,3): F' r0 q9 e- U/ s
- 2.04 a: {! F! `7 P5 v6 F! Y) a7 S9 r Q
- >>> math.fmod(20,7). `- [. x$ `, W& l+ g
- 6.0
复制代码
# S `' ^# D) T+ z3 N; `( {$ l! @1 hmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& X& L- v Q9 s( v/ l2 u1 W4 ?& q% _
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
( d1 O) E" R/ Z2 { - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" p3 d( ?9 @5 e1 C
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ C+ W0 N+ M7 w, Q( C
- frexp(x): z) W( i+ |) X2 p; z
- Return the mantissa and exponent of x, as pair (m, e).
$ t% D$ L, Y+ ~# F5 x: x+ j - m is a float and e is an int, such that x = m * 2.**e.
' H1 i {( J9 p* B$ H - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
" h% k+ y( l2 I4 {/ o J - >>> math.frexp(10)
" {# U' ^4 i3 X: Z" _% l5 Q - (0.625, 4)
% p0 C# v; M4 E6 V3 V - >>> math.frexp(75)
. [! f- V% q) P* m - (0.5859375, 7)# \! q+ M: s7 H
- >>> math.frexp(-40)
" F7 B9 _0 w2 x5 K3 l, ], j8 h - (-0.625, 6)& Y0 |$ m- B& R
- >>> math.frexp(-100)
. A! p; }5 G# r, f& \6 E/ m - (-0.78125, 7), h z. H9 G8 L/ L! A
- >>> math.frexp(100)) W* A6 X( X" ?) l; ]
- (0.78125, 7)
复制代码 ' A _% X Y" r% x1 N
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)6 f3 C7 O+ Q+ I
- #对迭代器里的每个元素进行求和操作
2 { J4 v0 N+ \5 K - fsum(iterable)/ d+ m' p# M; u0 M e1 c" t4 w
- Return an accurate floating point sum of values in the iterable." s( L% t4 o Z" B* u
- Assumes IEEE-754 floating point arithmetic.
( w7 @, \9 Q. v: A# m - >>> math.fsum([1,2,3,4])
9 V% n }9 b! ~7 z7 N8 Z, C - 10.0
4 [0 c f* J5 _* p - >>> math.fsum((1,2,3,4))
* K/ W# Y" C7 i - 10.0' c% |7 `/ E0 b4 g( i
- >>> math.fsum((-1,-2,-3,-4))
3 t; W0 ?& q7 K% N, K% q" j - -10.0/ y/ n8 A3 b. V) h
- >>> math.fsum([-1,-2,-3,-4]) u, n. M3 z: R8 H, E( F& l
- -10.0
复制代码 # w/ l+ F, w6 ?3 [' x# L3 ]/ l
math.gcd(x,y) 返回x和y的最大公约数
+ m% z1 ^3 z# [' c# K: _/ C8 ?- ?- #返回x和y的最大公约数
- R1 b% R0 w- H - gcd(x, y) -> int
. `: }8 S0 y3 o* @( @" H - greatest common divisor of x and y8 g- }$ I. }& n" `9 g
- >>> math.gcd(8,6)# t! c" W) C6 u& W0 `% G
- 28 {+ f+ T+ f: D5 V/ F
- >>> math.gcd(40,20)
f) t- o/ l! M7 w! f8 A+ L# n, N7 F# p - 20
$ J0 P s. x- o+ a& ]" S - >>> math.gcd(8,12)6 Y: w1 D& l$ A9 a+ p$ m) P; z
- 4
复制代码 3 }( z. q& }' r9 f" l
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False' i a& z- {( X+ f/ D
- #得到(x**2+y**2),平方的值
$ n2 X: D7 o6 T& U" O% ~9 ^ - hypot(x, y)8 ?9 @/ I4 b7 y
- Return the Euclidean distance, sqrt(x*x + y*y).
0 c& a( D( |7 X; E4 c - >>> math.hypot(3,4)
9 _: s% K% g# C* O% d$ x2 W. p - 5.0
; l9 u" r6 x( A. G - >>> math.hypot(6,8)
+ @ E/ M* L( o, y - 10.0
复制代码 : r B: y" g, j
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
% g5 L2 ^! l6 Y' J/ s- #如果x是不是无穷大的数字,则返回True,否则返回False
2 i! M B+ q! S' Q s8 B3 L! |" [, K - isfinite(x) -> bool/ I( j, t7 S% |7 g! V4 G& m, }' F% ~
- Return True if x is neither an infinity nor a NaN, and False otherwise.! h% Q: q% @8 ^3 r( E% L8 k4 h' S
- >>> math.isfinite(100)
- y3 N4 s2 _) h$ h Z% g7 s; k+ \ - True0 U( V& l4 r0 f( F* x
- >>> math.isfinite(0)4 J- V8 \; q& L" @8 [
- True
6 t" v; |7 d/ b+ z - >>> math.isfinite(0.1)/ Q$ G6 Z) s0 r8 U. _+ w! f: A* w
- True! r7 ]% h# z" b' D
- >>> math.isfinite("a")4 }7 M/ R. n) [" ^. n) w
- >>> math.isfinite(0.0001)
; y, s4 N8 w' K9 V' r - True
复制代码
) n4 G6 P) N) z3 S Y1 C) \math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False" {% c" H% R6 w" Y0 @+ ?6 b# K
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
/ {+ G7 ^' M- N4 |" s - isinf(x) -> bool
! ^! {1 V1 d/ m. p% j; Q - Return True if x is a positive or negative infinity, and False otherwise.
3 R; ~( W4 P0 h3 \ - >>> math.isinf(234)
1 V1 h# U' i5 R$ [# X; Z& B) Q9 Z - False
x9 f; t$ I+ {: ~ [+ E. J: m; n y) Q% @ - >>> math.isinf(0.1)
" j& x: x4 L# J4 k. a - False
复制代码
3 Y* i! ~7 H8 }/ v6 Fmath.isnan(x) 如果x不是数字True,否则返回False B4 X% r% e8 y
- #如果x不是数字True,否则返回False9 P) h. {9 ?0 m, D% H, M; _
- isnan(x) -> bool
# ]$ b5 s, e0 s - Return True if x is a NaN (not a number), and False otherwise.7 s3 \! P; d# e' A, L& ^- s
- >>> math.isnan(23)
% E/ J, X) u8 `& [5 p; ^6 O- h - False4 _* i' f0 t' z2 V
- >>> math.isnan(0.01)
6 w! m b1 Q z6 d+ X l - False
复制代码
* k! A+ V# `. g2 k6 ^) Nmath.ldexp(x,i) 返回x*(2**i)的值) w! U. N& S2 J# }# O, R
- #返回x*(2**i)的值5 ]2 `- t* [# W8 M! T
- ldexp(x, i)% ?6 Z: E/ l s% j6 O$ B5 i7 G
- Return x * (2**i).
: v. {' ^ r6 |. i* Q, V# [. s - >>> math.ldexp(5,5)
( `2 w: ^- Z# ~1 V' j, V% \, x - 160.0
% `: s- S8 A8 i8 W E - >>> math.ldexp(3,5)
8 T4 _5 y" y: u# h* t - 96.0
复制代码 # X) ]1 H ]" {: r
math.log10(x) 返回x的以10为底的对数
( p0 S! ], E% f9 |. O3 u( L' K- #返回x的以10为底的对数3 @; W! e/ |% O$ D/ w; c2 N9 I
- log10(x), I3 C9 U: {3 x$ [
- Return the base 10 logarithm of x.
: F2 N6 {- p$ ~( }# c, |6 c - >>> math.log10(10)/ m& h; S" e9 ?% A4 f* X
- 1.0" F8 Y9 \. f, R6 R! R6 K
- >>> math.log10(100)+ v; B) A+ u @% e$ n/ e
- 2.0! K; F; x* ]# I- ?
- #即10的1.3次方的结果为20" h) m/ e5 @1 e( }. F: A/ G
- >>> math.log10(20)8 B; H; F- Y( W! E
- 1.3010299956639813
复制代码
1 J8 o6 Z3 W$ P. Imath.log1p(x) 返回x+1的自然对数(基数为e)的值$ \7 h9 d. v/ Z" }- q6 A: U
- #返回x+1的自然对数(基数为e)的值
x: i, Z5 @4 }3 C/ y" U4 b - log1p(x)0 a9 h8 A: [' I- O/ B% _1 f
- Return the natural logarithm of 1+x (base e).. w. {, `! l9 `
- The result is computed in a way which is accurate for x near zero. }- f G, C3 s1 @
- >>> math.log(10)
: s2 V. I9 |) y0 t. J q; T - 2.302585092994046/ k- ?2 G9 S9 b2 b4 Y9 e; K( {4 {& ?
- >>> math.log1p(10)
; K, i5 r, u: v: g4 A; `7 @ - 2.3978952727983707- O& B6 {8 L6 d/ U/ N
- >>> math.log(11)* Z8 M3 I. h# W* b) z
- 2.3978952727983707
复制代码
8 o) |5 k8 ]/ F) {" g6 Smath.log2(x) 返回x的基2对数$ l2 Z9 e2 l5 E K
- #返回x的基2对数# Q5 e5 u% X6 E. K9 p
- log2(x) n: a5 {% P6 o, S u
- Return the base 2 logarithm of x.
+ a/ J9 ^" M+ e8 j( m$ \0 x4 C0 u - >>> math.log2(32)
0 O6 m6 c# r0 ~. ^+ U0 e0 x6 w - 5.0
/ P3 `, I) y8 O% b3 q9 h - >>> math.log2(20)) T5 V- Q9 F+ O! b
- 4.321928094887363$ W3 g# e5 ?( t% a* y- D" E
- >>> math.log2(16)
% K' _9 F% B+ L$ @( w1 J - 4.0
复制代码 7 F2 v9 v0 U. ?* _
math.modf(x) 返回由x的小数部分和整数部分组成的元组
3 K+ R% G4 b; x( V% W& F/ H- #返回由x的小数部分和整数部分组成的元组: V2 F, y1 J) x3 E& V' c4 l/ ^
- modf(x)
2 e- M: Y- F0 i5 i - Return the fractional and integer parts of x. Both results carry the sign+ ^) c/ Q+ s% H* u6 l1 G$ ?7 z
- of x and are floats.; w- ^4 n S7 f* @$ e' y
- >>> math.modf(math.pi)5 c2 G% ~0 t, m; y, G
- (0.14159265358979312, 3.0)
1 t9 L9 c3 i7 O- R# q5 ]. `0 M - >>> math.modf(12.34)
' j D) J! e& D. D3 c - (0.33999999999999986, 12.0)
复制代码 " J: }, b4 ^ X* D1 l
math.sqrt(x) 求x的平方根
1 @& `: Y% j$ v A- #求x的平方根( J# h4 A/ o8 ~' `8 G
- sqrt(x)
% f# z6 l2 b9 a& O - Return the square root of x.
# X b& N4 @# V' i% a, U7 G2 U4 E( e - >>> math.sqrt(100)! | U2 W" n P
- 10.0
5 \, G/ N5 y: e5 H, Q2 a3 m0 p - >>> math.sqrt(16)
* t+ _. T+ `( ~ - 4.0
$ R. v0 W& ~( } - >>> math.sqrt(20)$ Y6 \1 I4 _1 Z# s( L) x
- 4.47213595499958
复制代码 + D* ?- f/ G W, p$ \* h6 O4 N/ m
math.trunc(x) 返回x的整数部分- #返回x的整数部分+ C0 O* Y9 h0 I/ v. F8 t
- trunc(x:Real) -> Integral
t, E$ X% H: X. E7 F6 ? - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 [2 j3 }' N: V& m
- >>> math.trunc(6.789)1 }" M3 ~4 ~3 [
- 6
1 ~+ s- }; X7 S3 Z4 A; d - >>> math.trunc(math.pi). p. M0 @# }2 {# c: I4 I2 ^4 E
- 3( Y8 @. v' X( R5 \
- >>> math.trunc(2.567)8 Y$ A6 o) j8 p) _6 h1 x- d' g
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|