马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) G- X! Y; O |8 |1 @1 y0 I
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。9 v; e2 E0 B" m# n3 v ~, f
! y% h* z% G2 H8 Z; \方法1:; C& m7 E( H/ l* w
- >>> import math8 v) d) d# A0 t f, o; s( w2 `
- >>> math.sqrt(9)# S& f$ x' o- i; Y
- 3.0
复制代码 方法2:* t6 k- b' ]: Q7 l: G
- >>> from math import sqrt, B# T5 f2 U# I- L# w9 X, `7 q
- >>> sqrt(9)7 I3 y) I& t% J3 r, d; ?8 ]. H
- 3.0
复制代码 5 E* X' |8 ^! {) L
; h& ~$ {$ d, @% y% ]. Q# |
math.e 表示一个常量
8 q0 Q F) F& o- #表示一个常量
) p' `/ Z9 H; V% t- T' Q! N - >>> math.e
# @$ T; j5 n: A5 r+ z" E' z5 J" I- O" A - 2.718281828459045
复制代码 ) i) B% ~7 o' q- k# g5 v9 }% O
math.pi 数字常量,圆周率* D& ]5 Q- u9 g; @: l/ m! \
- #数字常量,圆周率
( ^) Y$ q, T' U6 Z - >>> print(math.pi)
4 J1 x/ P9 g# U - 3.141592653589793
复制代码
3 K. a* M- k% Q/ dmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x& S+ ?- K) {3 Y6 Z. n7 S; u
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 S) @6 s7 b1 M E3 c6 s4 w/ E - ceil(x). t# T c/ R+ E1 Q# E4 ~
- Return the ceiling of x as an int.
4 A2 }" O) N6 u6 X8 q% u - This is the smallest integral value >= x.
0 Q" E% r& P+ b" ~: {1 u% h
1 U0 u; B) Q Y: j e8 D* F" u- >>> math.ceil(4.01): {( m) P+ n% X- ~# A% y! L6 o
- 5) e9 P+ ~% D' |+ {& ?6 s6 w
- >>> math.ceil(4.99)
5 F. y; A) ]; Y9 l( _& k - 5, }% j" c0 l" ?, o' G
- >>> math.ceil(-3.99); g) ~# N: I$ F1 _# e
- -3/ m5 c4 ~ n' y0 ?# }
- >>> math.ceil(-3.01)9 K* K, A ^, S5 k- D) b
- -3
复制代码
4 v$ U8 s6 g0 h: fmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身) l+ X2 ^* i( Z
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 S3 u0 s# Y, w8 T Q
- floor(x)+ K5 Q1 X2 O, g* z) ?
- Return the floor of x as an int.
, d: M( y" ]7 v2 F: B - This is the largest integral value <= x. }5 |/ _; C3 }, A) _; R6 g4 o
- >>> math.floor(4.1)( i) \$ b& q& H% x2 `0 z! R
- 4
2 w* p/ d- ], c) V5 c% f6 R - >>> math.floor(4.999)
9 ?' Q$ F2 P% q+ Z' d8 m - 4
5 j) l2 T! n$ G, Q- f, _. s+ K$ b - >>> math.floor(-4.999). s1 }8 Q2 c$ |8 F: {! C
- -5
; `9 F1 O7 \ z5 f - >>> math.floor(-4.01)
9 w' r. T& |- n - -5
复制代码
" F' ^3 c' r1 b9 bmath.pow(x,y) 返回x的y次方,即x**y' _) J# \) W) R; y+ M
- #返回x的y次方,即x**y
$ j: S `' f. K - pow(x, y)/ y0 g" {# U/ e9 ?( h/ i) G p+ [$ U
- Return x**y (x to the power of y).( Z6 U( a: Q" ^0 q! t
- >>> math.pow(3,4)0 o% i! z# `+ V; Z: N: G6 o
- 81.0+ h9 N# Z; E$ w8 j7 z$ q+ f
- >>> - h$ d' G/ Z& Y' A$ F5 k* J' \) ~
- >>> math.pow(2,7)( u2 y" i+ D5 A& q
- 128.0
复制代码
2 e; }+ {7 S I) d/ n7 `2 Wmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" X8 C& l$ y J- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 V/ b' p; U6 {9 C F5 w& S
- log(x[, base])
% u( |' F2 @+ ^ - Return the logarithm of x to the given base.
( v$ D$ D- m# m) R - If the base not specified, returns the natural logarithm (base e) of x.
1 o0 _2 E( U) v4 ?) m5 x( }! D - >>> math.log(10)! a( q/ S& } ]: R% C9 P! v) L
- 2.302585092994046
5 p3 f2 R9 K* | - >>> math.log(11)
+ l3 ]4 ~8 w$ R+ `6 }/ z - 2.3978952727983707
5 g0 q* J, r" |0 E8 K. j8 [$ U - >>> math.log(20)
2 _8 H7 t" M* C+ R8 a - 2.995732273553991
复制代码
7 u+ D: t3 Y: P8 amath.sin(x) 求x(x为弧度)的正弦值/ r/ z/ P, ]. D' J* L
- #求x(x为弧度)的正弦值( B3 [9 }3 ~5 y, W5 w8 u' V
- sin(x)+ i) c5 v3 Y" G# n& D/ p+ |
- Return the sine of x (measured in radians).
2 }# S' z$ N' A6 V% G - >>> math.sin(math.pi/4); a: s5 q7 [% j# S) l7 a: s( Z% d
- 0.7071067811865475! y4 W0 z& C) s: S( I" I$ L" d2 w
- >>> math.sin(math.pi/2)
6 D5 o" i! x7 m/ |8 \, }0 k& J - 1.0
; ~3 q" U, B4 ^) m1 Y' r - >>> math.sin(math.pi/3)8 e0 I- c9 D o5 ~# H) U3 _3 ]# ?2 o+ Y
- 0.8660254037844386
复制代码 " s$ {1 t3 m# V& Z9 q& U- j
math.cos(x) 求x的余弦,x必须是弧度
) z* Z Z/ R2 O7 K+ c! {; ]+ `- #求x的余弦,x必须是弧度
9 K* i* F6 f5 } [4 |" l" N - cos(x)
3 p. ~) S2 w2 u% O) n9 [1 I. V$ S - Return the cosine of x (measured in radians).
; a5 e2 ^3 Q. w9 T+ ` - #math.pi/4表示弧度,转换成角度为45度
: j- x" A4 u4 | L) F - >>> math.cos(math.pi/4)0 e. i8 D0 p. l* O/ g
- 0.7071067811865476- n# h n5 {; {% D( m, N! }
- math.pi/3表示弧度,转换成角度为60度: l% c O6 a1 m# T- C7 Z- ^: v
- >>> math.cos(math.pi/3)$ s0 t4 Y& u% }1 g' y
- 0.5000000000000001
( s1 s. n1 X: n% I3 s/ F - math.pi/6表示弧度,转换成角度为30度$ P* R- E8 ?6 C; [
- >>> math.cos(math.pi/6)( q, ^2 A3 N5 n8 J" ]
- 0.8660254037844387
复制代码
4 q. K r; o1 m1 u, [) ~math.tan(x) 返回x(x为弧度)的正切值4 p' J ~" e& H6 o! [) \3 O
- #返回x(x为弧度)的正切值
# O6 H/ p; q6 s! Y9 l3 w - tan(x)
! P1 v4 K4 i/ W - Return the tangent of x (measured in radians).% j+ Y0 S5 B& l% l( O
- >>> math.tan(math.pi/4)
$ G; r7 U; b0 Y* Q- p - 0.9999999999999999
6 u9 a, L4 _/ y7 P; r! U - >>> math.tan(math.pi/6)
# n1 g: l# \ d! q2 j% p" t - 0.5773502691896257
. C8 l9 e& ?! K. s% b' c - >>> math.tan(math.pi/3)
- r+ b" v9 _: S2 O5 Y% G+ } E - 1.7320508075688767
复制代码
) }2 A# s; q9 W8 X+ Rmath.degrees(x) 把x从弧度转换成角度) ~& A" |# O7 ?* t, K0 A
- #把x从弧度转换成角度
& j% j$ Y4 q0 a* u - degrees(x)
( z. p, H$ A( o/ I7 \' F - Convert angle x from radians to degrees." ]6 l5 w* R* t5 S
$ ~5 r9 o! W$ A" Q U7 B) p- >>> math.degrees(math.pi/4)
! P5 h% O% W. Y, Q- {- Q2 X - 45.06 P5 L0 O8 \/ |9 a2 g9 E" s
- >>> math.degrees(math.pi)
r" _4 k! O5 }, A/ m0 m" L - 180.0
8 j! Z4 N" Y- z. i7 h1 v- }& M - >>> math.degrees(math.pi/6)
8 Y. _; f! N) q% n, i - 29.9999999999999969 X; J* a9 y8 w% W
- >>> math.degrees(math.pi/3)
! Q; D6 _4 X6 u - 59.99999999999999
复制代码 0 r7 O2 v' Q8 v+ r/ m6 j1 C
math.radians(x) 把角度x转换成弧度
& s$ Z3 X( p0 |) h9 K4 w+ J- #把角度x转换成弧度
1 y# ^4 e6 E, Q! o - radians(x)9 `& ^! a/ Y9 @) A) T
- Convert angle x from degrees to radians.
) Q; H3 Z2 q- X: v - >>> math.radians(45)
; v1 Y* {! s8 R+ { G - 0.7853981633974483& @7 e' Z0 [3 I# H
- >>> math.radians(60); H9 t# U+ e5 v2 r6 ?! r
- 1.0471975511965976
复制代码
$ {$ ^8 S4 v3 P3 W pmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
! C5 C0 _5 w' n% ^% a. H- #把y的正负号加到x前面,可以使用0 B" N7 f% m- L* o: R% ^& J
- copysign(x, y)9 i/ p2 _' b ]+ F% i3 F7 m( l5 \
- Return a float with the magnitude (absolute value) of x but the sign ! y" `( L( t% F; U3 k
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
& C% O/ a2 T+ ]& Z - returns -1.0.! u* {2 N/ @# |' Q: z1 h4 Z
1 ~) U3 ^% d5 W3 z! |6 B- >>> math.copysign(2,3)/ v% U" L1 C8 R$ | g5 ~, g4 x
- 2.0
* Q; B- f7 b5 T& ~& v3 P% y! N - >>> math.copysign(2,-3)
9 X! I5 t1 _7 X, s- W) l8 [* ` m3 b - -2.0
. ?$ _) p' O2 }' L - >>> math.copysign(3,8)1 L5 g9 V7 y- G# m" l4 ~, W1 H
- 3.06 r4 r. l4 M* x( m/ r
- >>> math.copysign(3,-8)
% G: o0 ] c: G - -3.0
复制代码
) y$ c, _% w/ U" J) ~math.exp(x) 返回math.e,也就是2.71828的x次方+ [( {; F* F! C
- #返回math.e,也就是2.71828的x次方! P! ], u+ u) f
- exp(x)& Q" ^$ X6 E: e; @9 C' t9 h2 ]* E
- Return e raised to the power of x.! ?/ ]8 V+ |* n8 N
- 9 V2 Q# D5 L# i2 i. f6 I1 w, E8 D$ v6 d
- >>> math.exp(1)
5 K+ r' @2 Y& \2 e* Q. m. ? - 2.718281828459045; S( P) k6 G; Z; b# Z
- >>> math.exp(2)/ U* i# L ~5 p) }
- 7.38905609893065
7 o- y+ i2 m0 r* |" g- X - >>> math.exp(3)2 ^& L( S" n# o
- 20.085536923187668
复制代码 ! b4 O8 I( W. A) S# p
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减17 Y: \, C, l/ i9 x( l: i& s7 \" X
- #返回math.e的x(其值为2.71828)次方的值减1- m4 R2 n( f# ?/ ?
- expm1(x)
# C- ]1 b' |, ]2 u( o8 N( w - Return exp(x)-1.8 r3 v# c; P+ Y
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
& _" }' i6 @* T+ L# B: { - 1 Y9 ^5 @4 o& o1 x/ L+ l0 }
- >>> math.expm1(1)- [3 f F. `6 H3 r# p
- 1.7182818284590453 E' q* }: w8 f" |- x3 f/ H i
- >>> math.expm1(2)
- _2 b$ I) l4 [) H& m - 6.38905609893065. f# _% S* K8 M6 \- }% u
- >>> math.expm1(3)
9 }0 H, c- n' O. d$ ?. T X - 19.085536923187668
复制代码
/ h; w# a- M/ ^4 b6 h+ H1 D) jmath.fabs(x) 返回x的绝对值6 X' H: H" ^: k; Z1 P
- #返回x的绝对值" Y+ _2 N7 N" f: a5 m
- fabs(x)# [3 t: j2 U5 O ^# v) Z
- Return the absolute value of the float x.
6 b( c. D" @! Y0 G" n
9 p' Q" ?+ \% |$ ~; K1 e- >>> math.fabs(-0.003)9 N7 i( F9 t' ~
- 0.003
9 t$ \8 i4 i' ?+ y- k$ J+ s - >>> math.fabs(-110)
2 @& u& e2 t6 A7 k: O0 W; v' p) P - 110.0
/ _% h- X1 R0 Z1 N B4 n6 | - >>> math.fabs(100)
! G( P3 F9 G/ k) ~7 y; M* S* y! k - 100.0
复制代码
! d: p$ C# K( ` m9 l) h7 Imath.factorial(x) 取x的阶乘的值5 g! q3 D. k0 M( ]
- #取x的阶乘的值6 L& {, A8 y* S
- factorial(x) -> Integral
* {" V H# ]. w. A7 E - Find x!. Raise a ValueError if x is negative or non-integral.
& C) e& D- A3 G+ s( T - >>> math.factorial(1), ]* s5 ?6 j! a; ~2 Y; E
- 17 [6 ]6 O! S+ e: ?& w8 H( r
- >>> math.factorial(2)
: f9 U1 K4 N! d) U) n7 X* n - 2
3 ]$ F j* D6 r4 C. [4 |. X& G - >>> math.factorial(3)5 D2 E( u7 T8 X' S8 V( q3 l
- 6
* r, O& d* b5 u8 f0 d - >>> math.factorial(5)8 z( J5 g4 D7 A" N( V1 w3 [
- 120' U' P1 f9 S: m! t% K
- >>> math.factorial(10)
! C9 H5 C2 A$ J# F1 k! K9 b9 | A - 3628800
复制代码 7 Z+ _8 @$ _, }: i
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数6 H- s) p8 J8 [. e( M4 `9 c4 _1 p- k
- #得到x/y的余数,其值是一个浮点数
2 x. c& s6 b- H# j8 j) R, s7 [- ` - fmod(x, y)
! N, T2 [" E6 W9 m Q - Return fmod(x, y), according to platform C. x % y may differ.
3 q& B. p$ p' J4 R' H/ U- z9 H - >>> math.fmod(20,3): o j% ^+ [1 U T( |
- 2.0
/ @$ h2 {$ _% S, b - >>> math.fmod(20,7)& t' v$ t4 n6 F
- 6.0
复制代码 1 o3 e/ D: G9 H" O' {( D! x
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
# M. n! F* N* \0 L( _# p' r9 y- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,) @/ ? `! ]8 h8 M @+ g2 A' v
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
4 y# @$ g7 b' F" U - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' X! q, ~# Q3 d _ o6 F2 \9 y
- frexp(x)9 f6 ?/ B. D) O, i- m6 H
- Return the mantissa and exponent of x, as pair (m, e)., x4 r! N `8 D* Z4 r4 W& c8 G
- m is a float and e is an int, such that x = m * 2.**e.& b4 I! B+ N [( X: Q
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
/ X' ` Y" C5 S/ F; J( D4 ~ - >>> math.frexp(10)
# ^- R! R8 U9 { J9 { - (0.625, 4)1 L! k7 L+ V: g% [6 N3 K
- >>> math.frexp(75)
$ b" h, [% x3 ?$ _' o - (0.5859375, 7)# F. x* O6 i6 h& Q! k
- >>> math.frexp(-40)5 d) }4 a/ d# r& y$ r( c/ D4 g: P
- (-0.625, 6) T: s! e# [& ^
- >>> math.frexp(-100)
5 C ~* ]$ q: O O - (-0.78125, 7)/ F& G4 `$ r5 _7 c' b7 @
- >>> math.frexp(100)
5 Z+ U% W' e# r' K3 @6 ^$ {4 ^+ m) E N - (0.78125, 7)
复制代码 0 G2 |% x' x8 F& W
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
3 l' P) ]9 ^ J6 f4 `- #对迭代器里的每个元素进行求和操作
" S+ h$ z7 C) ~( \- t - fsum(iterable)
& {- @8 m( f6 l( x; M' | - Return an accurate floating point sum of values in the iterable.
# k6 D; F+ x, I! ~5 N' e$ B; o' x - Assumes IEEE-754 floating point arithmetic.
, |% Q# l! O) z6 q. I - >>> math.fsum([1,2,3,4])
3 ^0 a" f% M1 f2 | - 10.03 T% \# ^1 A+ H/ [* C5 ?
- >>> math.fsum((1,2,3,4))) w, C$ [4 f& V2 I2 O7 q
- 10.0$ R' a4 W1 d3 E4 _5 }- T
- >>> math.fsum((-1,-2,-3,-4))5 C% C g- b3 W7 p; f- ^
- -10.0
7 w. d+ q1 |) d4 H! X - >>> math.fsum([-1,-2,-3,-4])
, C8 @% m, ^& K) M8 z0 I - -10.0
复制代码
) A" J( D; N2 nmath.gcd(x,y) 返回x和y的最大公约数
7 u D9 _) A1 \- K: Z {5 ^- #返回x和y的最大公约数
9 n4 f4 {1 o( F9 Q6 ^ - gcd(x, y) -> int
! h) U! n2 T5 Y5 }3 f - greatest common divisor of x and y) c3 a% W G6 [. E& ]
- >>> math.gcd(8,6)
$ \+ {! y7 E( m, h - 2- H- h g' \* `/ L$ u6 {
- >>> math.gcd(40,20)
) r* O9 j% f7 w: ~ - 20
9 z7 a* y4 t- C5 r2 S; } - >>> math.gcd(8,12): x, H2 E. K7 }, s: U$ N
- 4
复制代码 , X. p) x9 j( }7 @/ M' f0 q. p: L
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False. y1 g, L% [( q9 [* u
- #得到(x**2+y**2),平方的值0 n. J! r% k! O9 W U
- hypot(x, y)
2 t. H) K2 f) a! \# o' R- k3 D - Return the Euclidean distance, sqrt(x*x + y*y).
5 P5 j. N1 w2 r - >>> math.hypot(3,4)
9 Y, L, ^" R9 i4 J/ L1 G5 \ - 5.0" b3 a4 f8 h7 M+ Q: S
- >>> math.hypot(6,8)
5 Z: k1 ^0 @8 T f- _ - 10.0
复制代码 $ n& _6 }8 p4 Q9 d8 o5 P
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False3 B% I" h+ D8 U& }- C$ A2 j
- #如果x是不是无穷大的数字,则返回True,否则返回False& v! {) p* u7 J/ j/ Z/ s
- isfinite(x) -> bool
/ Z% A+ A: U) \; j0 V/ q - Return True if x is neither an infinity nor a NaN, and False otherwise./ f( F0 D) I" A3 E3 A
- >>> math.isfinite(100)
6 _! a- H/ L8 }% b - True* S$ e9 z3 l, Q$ ^
- >>> math.isfinite(0)) I: D2 R1 Y7 f/ q1 o
- True$ \& T4 h4 i+ x
- >>> math.isfinite(0.1)! ]* Q* ^& s; s8 G
- True9 G* O1 _$ U* q+ L3 }8 L; k& F. T
- >>> math.isfinite("a")( E; R5 S% _3 X; w6 A1 v+ A5 d: n" E- J
- >>> math.isfinite(0.0001)
0 d# X$ Q9 s( {, M1 g: S - True
复制代码 & ^/ p, h, B4 ^& R* j* c! D
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False0 e$ x1 M' L$ j: ^5 v" V
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
) V! U; r8 g: Q6 S7 ] - isinf(x) -> bool* H' T; m( b1 O) r
- Return True if x is a positive or negative infinity, and False otherwise.
{. [; O; c+ S - >>> math.isinf(234)- n2 d! ?& o9 {$ i& [ Q) l
- False6 c! S+ y5 W, A7 e, L& s; E
- >>> math.isinf(0.1)
; V' A- b- v8 ]: D - False
复制代码
7 y" s/ M; l8 ~# xmath.isnan(x) 如果x不是数字True,否则返回False. a0 u3 _7 ^. Z- G. f' |
- #如果x不是数字True,否则返回False0 H- h8 k5 }8 \& F: u3 f' t
- isnan(x) -> bool2 Y* r* Q# y6 H
- Return True if x is a NaN (not a number), and False otherwise.
* k! T- \* N' B" }3 R+ A9 [( U - >>> math.isnan(23)( e/ ^( y& d7 l5 g& Z- j$ u
- False
8 Q) D1 v: \+ U$ ?1 \) j0 i& A- P - >>> math.isnan(0.01)" E8 u* J* ? j6 e0 d
- False
复制代码
! V# ^+ K+ H4 B* Mmath.ldexp(x,i) 返回x*(2**i)的值$ c# u+ G4 b+ a8 x
- #返回x*(2**i)的值
) l; o. p1 F# p7 l - ldexp(x, i)
. h& W% j4 e) |+ t3 p3 Y - Return x * (2**i).
1 ~& b0 Z& o! U - >>> math.ldexp(5,5)) Q. Y. i2 H3 I8 }3 f0 @( [8 v
- 160.0( z4 l" d2 O7 {6 [' {/ r
- >>> math.ldexp(3,5)( e0 E0 J- T% W$ p* v
- 96.0
复制代码
9 @5 k; x; a5 s- }7 Gmath.log10(x) 返回x的以10为底的对数
! N; s4 \! x' j: S) U+ o- #返回x的以10为底的对数% h. z: M6 n0 L2 B2 z. `' D
- log10(x)
& @0 K2 ^, K: F( j2 ^! {9 n5 ^/ e - Return the base 10 logarithm of x.9 Z7 u2 I* Z) V6 ]: N3 m, t5 U
- >>> math.log10(10)
; e- Z8 T" N: d. w1 d; i' E4 v - 1.0
6 m2 Z5 L8 A& g) N' j" t - >>> math.log10(100)# x) ^& v! l* @
- 2.0
6 o) @- j1 y; K" z# u! I - #即10的1.3次方的结果为20% F& V: ~5 j# b, \, r9 p: X
- >>> math.log10(20)
P5 k; k0 F- p I - 1.3010299956639813
复制代码
+ _! ?; T) l! A! Dmath.log1p(x) 返回x+1的自然对数(基数为e)的值
/ E8 G8 D2 y/ o$ k- #返回x+1的自然对数(基数为e)的值
3 ^6 I' u! l0 D0 F( b& V - log1p(x). A. Q) T9 a$ k0 A6 S# z0 w" I
- Return the natural logarithm of 1+x (base e).7 o3 \$ x8 ~' h+ W5 `( j' `+ G
- The result is computed in a way which is accurate for x near zero.
; q3 J$ {+ _1 m9 b7 x - >>> math.log(10)* h4 z e! z9 S
- 2.302585092994046
$ @' T8 k/ i% G! ?( ` - >>> math.log1p(10)7 F( d5 i3 n2 u8 @' v
- 2.3978952727983707* u5 C4 E; ] d+ h \
- >>> math.log(11)
( S S. n4 G& ]( |+ U. w3 S - 2.3978952727983707
复制代码
3 F t3 Y- M/ E2 W: B3 i8 M q6 |; P. ?math.log2(x) 返回x的基2对数, u: T; {6 ?( F/ O9 X L1 {) B
- #返回x的基2对数
2 N# W" Z, z( x z, A. L - log2(x)
- A6 J, F6 b4 ?7 K7 W( x; n( T - Return the base 2 logarithm of x.
# v% m, ~ u9 Q - >>> math.log2(32)
9 A& C w0 O3 S - 5.0 t& M: ]! j. x4 n9 p- {
- >>> math.log2(20)
7 w, G, v W2 j4 z" R+ W6 s3 b - 4.321928094887363. [0 g. ~( p( n
- >>> math.log2(16)
8 L4 k% Q0 ?/ p - 4.0
复制代码 & C, w1 k8 t S9 V1 H
math.modf(x) 返回由x的小数部分和整数部分组成的元组( r2 ~: U- ` {9 _) p: q
- #返回由x的小数部分和整数部分组成的元组
( T) e1 O3 r0 T - modf(x)4 v8 S$ Z1 W) _- K! c
- Return the fractional and integer parts of x. Both results carry the sign( r4 b4 `: ?1 S1 U
- of x and are floats. J. F0 A [& _/ [- F$ D9 r
- >>> math.modf(math.pi)' I2 w4 `9 K4 d I0 I/ s
- (0.14159265358979312, 3.0)4 P+ Q# C9 ]2 M& C
- >>> math.modf(12.34)* x, U6 [1 H: e
- (0.33999999999999986, 12.0)
复制代码
, L5 c6 |' \+ p: h4 @math.sqrt(x) 求x的平方根9 J( f! M1 v5 T, [. q O
- #求x的平方根( v: Q" E& @; Y
- sqrt(x) w6 o3 L k& D4 Y" T( W0 M5 a
- Return the square root of x.; J) r- t; X1 R* h7 k k
- >>> math.sqrt(100)" {1 K& R0 ^4 f0 N2 [0 ^9 ?
- 10.0
( d- g" l6 Q+ P+ N/ X; i5 O - >>> math.sqrt(16)* z7 W4 v4 z: L4 L* }
- 4.0$ ]8 _9 j- e9 e& |, I
- >>> math.sqrt(20)
9 W t" E) o9 i% q# |7 O* w - 4.47213595499958
复制代码
6 M2 h% R. l2 Z& `, K/ d1 \math.trunc(x) 返回x的整数部分- #返回x的整数部分
1 ?5 U3 [! v) X& E, ^& }& z - trunc(x:Real) -> Integral8 f ?+ U0 p, Q6 U6 J
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method. N/ V0 I6 T6 d$ \: k' }1 E
- >>> math.trunc(6.789)! S7 W* A; X! \: V, S3 u, K+ D
- 6& N3 k$ L4 I+ P- D5 M X
- >>> math.trunc(math.pi)
. G8 D; P% T% q, k/ f5 M0 h6 y - 3. c0 D5 V9 O; |+ I- E/ Z
- >>> math.trunc(2.567)
( e+ i1 w% x/ a- [, B - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|