马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# o0 I5 S2 Z( H4 l N1 ~8 N
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- i* P4 {9 L" W$ b* z
6 [( N5 c0 L' A% j# @. D方法1:
: P5 ?0 M) m! H4 T: `0 }4 `- >>> import math
' Z3 U# Z6 e* p( l; I - >>> math.sqrt(9)* q T, n4 Q" f+ @( `- v
- 3.0
复制代码 方法2:
& M$ W; a" r0 a3 b* ?- >>> from math import sqrt/ S/ b0 Z2 B- t& ^' B+ T
- >>> sqrt(9)
4 {+ U: ?4 X r8 Y" ^6 {1 j1 T - 3.0
复制代码
8 {5 [- D) D* h) S! V% @8 P* ~
' R6 X9 x9 j! \' v# t% B1 c2 G, amath.e 表示一个常量
1 {! F E$ D2 ?6 f- d- Z) G. |- #表示一个常量
" {7 X+ s( K% L7 @) L- t( y8 _1 s - >>> math.e3 G8 c% d" G6 Q \
- 2.718281828459045
复制代码
9 }; u0 x6 u# }* N; b/ f {: amath.pi 数字常量,圆周率3 m' ^7 E, d3 J
- #数字常量,圆周率( O# ?) N: C2 U. U, f! L
- >>> print(math.pi)* t8 X, M' G3 E4 z' I0 c6 Y
- 3.141592653589793
复制代码
2 k# a: M" o1 z; i# ~math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
) ?! r$ V+ y. B/ h( h, w& {' a- B- #取大于等于x的最小的整数值,如果x是一个整数,则返回x: F5 a7 x9 F9 R- Q
- ceil(x)) A# ?: _% v+ P: b1 }0 i& J5 \
- Return the ceiling of x as an int.( w$ Q' g% F+ Z
- This is the smallest integral value >= x.4 M; e" |- Q/ S. T! h3 h
* a, B6 t& A+ z4 @ E6 g: f* s/ v- >>> math.ceil(4.01)1 P- a) b: E+ E" h0 f* {& n' A! D
- 5
, {/ H2 F+ Y, c8 k - >>> math.ceil(4.99)7 q+ l) y, F, r! h3 x
- 5
4 X2 C# W, b/ U4 [+ k3 x; Y - >>> math.ceil(-3.99)
( s! c: `8 f; [# X/ d - -3% g& o2 z& D+ v7 T3 o( D
- >>> math.ceil(-3.01)
{0 t- i7 f, `% z - -3
复制代码 2 c8 ?. X$ D# \+ p1 ^7 ^7 P, g
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) V, A$ s3 F0 f* K' C8 M. |- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 s0 ?; s% ?4 A& B
- floor(x), Y* n- ?* U# M+ u8 E0 ~
- Return the floor of x as an int.
, F, M$ w3 I2 |" d4 D. k* A - This is the largest integral value <= x.
: a8 l9 S# I. C+ m7 ]5 H4 y- T3 Y - >>> math.floor(4.1)4 \: K( z0 h% R- ]
- 4
. j& L& u7 D3 m9 n0 n" ]# W$ J* B - >>> math.floor(4.999)% Q" F5 k9 U% \8 W8 f
- 4
4 s5 A( A; k% @6 R# n9 k - >>> math.floor(-4.999)
. `( H" o* M. J, X7 [ - -5
6 G B+ X) B" _- `$ E q8 V - >>> math.floor(-4.01)6 j# x5 m6 H& I( s8 [9 P1 w
- -5
复制代码 ! k3 e& o/ R, s8 z- c
math.pow(x,y) 返回x的y次方,即x**y
3 z/ Z3 j7 Q/ E' R! j* }* g- #返回x的y次方,即x**y/ n1 @+ E0 F$ t+ v
- pow(x, y); P* [: f$ E E3 B% O7 T( a% f
- Return x**y (x to the power of y).
" ]' i! x: [7 D, c7 j - >>> math.pow(3,4)
; z( Y* z) u: @* ^) s; k' `9 r, S - 81.0
5 i! }2 D# Y% c) w, f+ { - >>> % f( D$ |; G: {
- >>> math.pow(2,7)& B4 j- e/ P/ u4 O. |! u
- 128.0
复制代码
4 N7 B* Q) K# s7 [( G% ^math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 E) t2 T: E+ V; z$ Y- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 L3 @7 N$ | r' q8 v$ B+ q7 q6 Z
- log(x[, base])1 P) |# J2 Q9 I1 M8 y& m; }
- Return the logarithm of x to the given base.
& {6 P K X6 z( Q; @1 p. E - If the base not specified, returns the natural logarithm (base e) of x.1 e/ f- ^* s4 H8 t' i* a
- >>> math.log(10)4 q! ] z- m0 n% V2 ~
- 2.302585092994046
0 r/ \! Y0 _6 w - >>> math.log(11)- ]- }) r1 {" q4 A
- 2.3978952727983707
# F# i, H6 \4 v - >>> math.log(20)
* h* R* k5 Q- M6 y0 ?9 i - 2.995732273553991
复制代码 1 G, d+ t7 _/ Y) a8 X+ w
math.sin(x) 求x(x为弧度)的正弦值, f. S; Z! {' D3 X* C) X7 z
- #求x(x为弧度)的正弦值
% X& h! r, F B) b. b - sin(x)
, F4 }4 |) l2 C3 L- [ - Return the sine of x (measured in radians).
! R$ V3 z0 G# ` F( Q - >>> math.sin(math.pi/4)
8 @' k: v5 @/ P - 0.7071067811865475
9 Q+ w( c3 Q/ v" b( B7 l - >>> math.sin(math.pi/2)
' U6 V! n: g/ e5 ?4 ^ - 1.0
) ~; W8 k- q/ k5 G - >>> math.sin(math.pi/3)- M) [$ Y+ e5 k2 y" w& T- f- R
- 0.8660254037844386
复制代码
$ l& ~5 X0 H' y) }( o3 Z% X0 i( kmath.cos(x) 求x的余弦,x必须是弧度6 a7 I* A, U: h8 [# q: W* I+ t
- #求x的余弦,x必须是弧度) x! j% C3 o9 ]' R0 M
- cos(x)
4 D# [; M# Q- J$ I& n& i* \ r5 W - Return the cosine of x (measured in radians).4 [# [: J9 M( k( X/ u& m+ T9 g
- #math.pi/4表示弧度,转换成角度为45度7 s% T n! E! x' B8 h; ~
- >>> math.cos(math.pi/4)# M5 q. V$ A" a: ?" t
- 0.7071067811865476& D+ P" e, P5 T. K! T, J
- math.pi/3表示弧度,转换成角度为60度
( t7 Q- n6 |$ |' T/ P - >>> math.cos(math.pi/3)
5 H r( G V7 r# O3 I& Y - 0.5000000000000001( e# z- J9 B" s, c' u% t, k
- math.pi/6表示弧度,转换成角度为30度
( U5 ^ Z: p3 x& W& A! r; J - >>> math.cos(math.pi/6)4 c: u* v( U/ A
- 0.8660254037844387
复制代码 ; m; n/ }1 w! w o/ X* i
math.tan(x) 返回x(x为弧度)的正切值+ `4 Z* }, }( m9 ~* z
- #返回x(x为弧度)的正切值! J+ v5 z) i7 c/ o8 s3 ?( J! y
- tan(x)
6 W2 M; i! @8 I9 j- o" n l; O- b$ [ - Return the tangent of x (measured in radians).
* G$ U5 v5 L) d' n - >>> math.tan(math.pi/4)! }4 H4 d. x8 D# x0 ^' v( W( }. S
- 0.9999999999999999
2 P) U0 J8 Z# c3 O; N6 ?; H - >>> math.tan(math.pi/6)6 B& d1 Y. C; w0 t7 r
- 0.5773502691896257
1 X: G0 y8 g$ g" u( ^+ o - >>> math.tan(math.pi/3)
" x( W+ N8 d: |: E% ]% V - 1.7320508075688767
复制代码 # V% u- z$ A$ `0 x" J, o) N0 s
math.degrees(x) 把x从弧度转换成角度
7 I8 F: T5 k9 \/ N3 \- #把x从弧度转换成角度4 |3 t4 m% ^' i1 z5 N4 F
- degrees(x)
2 P# l6 v6 f w" ~5 f% V. c5 d - Convert angle x from radians to degrees.
6 X1 F' [! O5 a. Q9 c& v( i' D
0 A$ x. b. g9 r& S c# Q- >>> math.degrees(math.pi/4)) e3 u ~8 c' ?$ ^# Z0 [
- 45.07 t1 v! c- ?# o/ Z- b, m( j1 y3 c
- >>> math.degrees(math.pi)
?) |0 W' X4 c5 [+ W/ V$ p - 180.0
4 ^+ i; o( b9 H8 \- R - >>> math.degrees(math.pi/6)) e z# d6 B7 X ^; h: G
- 29.999999999999996
+ Q3 B2 p/ ^9 B4 ?4 p$ J5 ~7 F0 v - >>> math.degrees(math.pi/3)
/ A9 a" S4 s- p9 F- f0 L9 t( L$ b& s - 59.99999999999999
复制代码 9 N6 d; e! c4 Q/ ?. R8 u
math.radians(x) 把角度x转换成弧度9 Y( h- O- h& A8 s) W- \7 G
- #把角度x转换成弧度# b. W9 k% m- q& Q/ V. z/ O9 x" ~/ ?
- radians(x)% s4 H4 E. k1 U3 O
- Convert angle x from degrees to radians.
) u! }& Z/ f9 i; e - >>> math.radians(45)6 }9 `+ G8 e0 z# e: a6 w! W
- 0.78539816339744832 v' k3 H% K0 V" Q0 n4 D
- >>> math.radians(60)7 Y/ J0 z; ~0 {( {5 c$ i1 n
- 1.0471975511965976
复制代码 7 s4 z' Q6 p, m$ c6 P) J5 F- N
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
& n+ c$ X# M' h- #把y的正负号加到x前面,可以使用0
/ u* a5 p5 W5 ` \2 r0 [ - copysign(x, y)
A6 i; G7 m3 t - Return a float with the magnitude (absolute value) of x but the sign 3 [6 P- A1 G' q/ E; N) a! a7 N: f4 ^: O
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
0 X: M* b* x/ u2 _( S - returns -1.0.
4 ~& s, _" c1 ?7 G O( H3 z
- m _ v% M0 ]4 |! b l- >>> math.copysign(2,3)3 s0 s& p7 F3 O; ]
- 2.0( W& i6 ^5 L) I
- >>> math.copysign(2,-3)
% Q3 o0 |; {/ T: `5 w @ - -2.0
* P6 O# _/ Q4 G2 j; f - >>> math.copysign(3,8)
1 F% o& D0 i1 ] - 3.0
% v( X: ?8 B. C2 N" D* |1 X+ [. \ - >>> math.copysign(3,-8)
6 ]; S* I! [0 V2 q; W - -3.0
复制代码
( u6 K6 B3 W3 l7 I7 x% B* F/ `math.exp(x) 返回math.e,也就是2.71828的x次方$ |9 |7 {6 {$ p! U! B; b
- #返回math.e,也就是2.71828的x次方
9 t: ?! c3 c& k0 b2 e - exp(x)
: S# x1 R* W1 E+ {) Z( j; e - Return e raised to the power of x. r* Y7 \+ g- E
- 9 j! v! g( y S1 f1 z$ U
- >>> math.exp(1)! a5 @$ E2 j: {; a+ P# Q$ X
- 2.718281828459045
+ O6 E4 Q i! n* { - >>> math.exp(2)" o; ^1 q: `( J& ?: d# f
- 7.38905609893065
' @& Z q7 E8 H2 W. N - >>> math.exp(3)8 M+ W% }; V( Q
- 20.085536923187668
复制代码
8 X, F" s/ v* s9 j6 R7 imath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减12 L5 J& I! ?8 P1 }. Y6 \* z# T% Z
- #返回math.e的x(其值为2.71828)次方的值减1
; i) t) `7 q" n# ^2 v% S6 L - expm1(x)& T8 H+ K( c: n
- Return exp(x)-1.9 C2 A4 y7 z) }2 ]* r5 L$ h
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
/ H7 d& t( T! E( D; m5 G. u - 4 w1 F8 M0 K" e, r+ M% u
- >>> math.expm1(1)
U" q% V K! b- v - 1.718281828459045% `9 s. X, ^- s ]8 z' y/ a( M" _+ {
- >>> math.expm1(2)
# G0 W# J2 K! C: C; v! P) V! P" e - 6.38905609893065# K( Y- s/ |$ e+ \
- >>> math.expm1(3)
- g+ u; A2 p" t) V) F$ U5 X/ H - 19.085536923187668
复制代码
% L# p3 x& N% A; v0 R6 H" bmath.fabs(x) 返回x的绝对值/ d2 ~8 r+ O7 w0 j. y
- #返回x的绝对值1 I- e( R5 M7 @8 ]" K o
- fabs(x)
; P! Y2 ^+ U: z, H- V1 c; h - Return the absolute value of the float x.
! v. m2 V, W8 O( G, h/ }! Q
5 v0 d* }1 q3 n1 F+ ?- >>> math.fabs(-0.003)" F7 [3 T2 ]1 c* ]3 o
- 0.003/ m6 M4 `. X$ `9 k9 c [
- >>> math.fabs(-110)
* o, ~7 B8 M/ T0 ~8 e& V, N - 110.01 h' g l! a: v( k& d
- >>> math.fabs(100)
l% X3 h' l$ B" x' K% g! J# S - 100.0
复制代码
# s2 o! Y# O& ^$ o# y0 \5 Y0 rmath.factorial(x) 取x的阶乘的值
" i3 z2 R6 x2 l3 X0 D- #取x的阶乘的值! q4 j& w+ C: Z. z
- factorial(x) -> Integral
8 _- C- _4 A: n& \, m - Find x!. Raise a ValueError if x is negative or non-integral.% ]# ?# X" m% ?/ F9 \1 s/ a3 t
- >>> math.factorial(1)
$ e" M0 i* `/ i' g7 h. Z" N - 1
% P: t6 i9 W# U! {# s" C; [ - >>> math.factorial(2); B* g/ A! b& p6 i3 m
- 2
2 c7 k" g' e' B6 A) w - >>> math.factorial(3)$ q2 ^; w' Q% D" [" x |% q0 X
- 6- l0 W9 h' B3 P1 r, c$ L6 s% C- k
- >>> math.factorial(5)
0 M" p$ y( _, p- y- X - 120 I6 E/ j6 y) N7 W
- >>> math.factorial(10), M+ r6 a6 `0 c0 ~
- 3628800
复制代码 ' n. d% f5 ^1 b$ T" m; z& y
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数8 K7 E* F9 Z) C: E7 u: O+ w1 t
- #得到x/y的余数,其值是一个浮点数
+ d% g! e! ^- g+ |7 z - fmod(x, y); N& S9 O- A1 y9 H
- Return fmod(x, y), according to platform C. x % y may differ.6 ~, ~/ D6 ~& y' T/ q8 d
- >>> math.fmod(20,3)
D/ H( G" r( K! C1 q- X - 2.0: Z! }! N: H7 N+ N( H! F3 r
- >>> math.fmod(20,7)! o8 Q }* f. l0 K, L9 p, h
- 6.0
复制代码
' `7 l- E" b( Y: C2 Ymath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 r, w4 ^' a% b0 f# f2 j
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ R3 [& o# W$ M+ C
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值& W) g3 I; e8 @, k6 F3 D
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
* F% P7 P6 c' {- j+ g0 V - frexp(x)
! V/ e: ~& g4 ]7 r& X! w. J4 P - Return the mantissa and exponent of x, as pair (m, e).
& ]' D2 d% t) f( h) |. Q - m is a float and e is an int, such that x = m * 2.**e.
7 t6 T/ I, ~0 ^# Z7 y6 n, q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
- b6 C0 ~# N! a) L. G" r+ w - >>> math.frexp(10)' V7 {3 c: a/ T
- (0.625, 4)# y: m& n- v. ~7 P5 V
- >>> math.frexp(75)1 u* T$ B- y6 Q8 B
- (0.5859375, 7)8 q9 y* A6 L! {" k% Q" I& M
- >>> math.frexp(-40)
4 U# i0 |( B* O - (-0.625, 6)
2 ]3 b0 U1 W# S" y/ g- C, r - >>> math.frexp(-100)
' O) f& A3 } _: }8 y - (-0.78125, 7)
% d: ^3 b( s# q; B% z, v0 x2 I! a - >>> math.frexp(100)
' @9 N7 \6 u/ V4 w4 N% O - (0.78125, 7)
复制代码
- l/ o7 P# @/ vmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)& V- `/ {# K& i& \
- #对迭代器里的每个元素进行求和操作% B7 e+ L" Q. n2 G E
- fsum(iterable)$ o" U% w ^: e& R& y
- Return an accurate floating point sum of values in the iterable.
' c; C7 t' C5 i! M% G7 m - Assumes IEEE-754 floating point arithmetic.: J9 \+ E( u3 j5 m. ~
- >>> math.fsum([1,2,3,4])
; i h3 H9 _1 ]5 ^* I - 10.0
. ?3 v! E. Z' F" w; e' x' t - >>> math.fsum((1,2,3,4))
/ f, S) X1 w0 [4 R4 ~# \ - 10.0
0 V2 e$ r( Y, T6 I: u! n6 }2 O - >>> math.fsum((-1,-2,-3,-4))1 a* f* E. |' U( {% f4 X
- -10.0& }0 q- `0 m" t( v% s
- >>> math.fsum([-1,-2,-3,-4])
0 {: C2 r0 d, k* H - -10.0
复制代码 4 X- p1 r; Y4 Z p$ a! u
math.gcd(x,y) 返回x和y的最大公约数
% c* T3 e. @# a% a3 s+ Z" L- O% \- #返回x和y的最大公约数* C& u c# H; j1 X: {( j4 F
- gcd(x, y) -> int
5 m0 U; j4 G; A$ p) h$ d9 `+ s - greatest common divisor of x and y
) z$ J7 ~. `- H. _) v - >>> math.gcd(8,6)6 b! w& b, E6 Y( t, |
- 2
4 u/ g: n9 A. i3 p9 i - >>> math.gcd(40,20)5 o- r2 p4 u" d, N) M2 v- y$ T+ N# i
- 20( _ ~1 _9 N6 }6 {/ L8 @: a
- >>> math.gcd(8,12)
, j) _: i \0 w( T, r' ^, N7 J - 4
复制代码 * c- K4 c$ k# q- r
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False; y# Z+ q! U5 d) x
- #得到(x**2+y**2),平方的值& g* T# d% h7 x3 g& S$ Z
- hypot(x, y)+ ~5 U7 v6 L/ P2 R! S% k
- Return the Euclidean distance, sqrt(x*x + y*y).
@. \. F+ D: V( g8 T - >>> math.hypot(3,4)
) M$ O! }' f/ K+ L$ {9 d - 5.0
# G/ c% c0 y% W; c5 ^% Y2 S" N - >>> math.hypot(6,8)$ _7 Q; ]; Z: Y4 n4 O& W
- 10.0
复制代码 * W- R" _$ W) f& O# _- g# p" i' M
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False' V) j2 I; J8 |! `( @9 \+ h0 H( E
- #如果x是不是无穷大的数字,则返回True,否则返回False
; j0 B+ l% B7 m! ?6 x6 U - isfinite(x) -> bool v- f& j2 G; E! D
- Return True if x is neither an infinity nor a NaN, and False otherwise.8 s- Q5 O7 v: U6 }3 B) |- y
- >>> math.isfinite(100)
U- q% }! q$ S: r1 j" v. B: Q - True
% |/ x' ~ u6 y9 A9 W - >>> math.isfinite(0)
' q. e1 G, r, _& }/ r7 N - True& W2 E0 ~6 }! |% k2 W
- >>> math.isfinite(0.1)
6 E3 U( _/ r5 k. R2 O6 e - True
1 ?; b) M1 G( g' C' _% z: J - >>> math.isfinite("a")) w/ |5 s3 k9 w* I
- >>> math.isfinite(0.0001)1 C" K# C3 G: F9 ?. \2 O" H
- True
复制代码
0 ~: d" r1 I/ {math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False4 z- ~* F* ^. d0 a, n M
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
, C- y# Z( h" ^4 M$ J2 E3 O - isinf(x) -> bool
) v$ Y* v7 k; ?0 P$ r3 { - Return True if x is a positive or negative infinity, and False otherwise.
; o3 c; B2 ~) D8 e" Y3 a& F* k - >>> math.isinf(234)
8 I4 @! C" o& S, P- G4 T6 ]1 n X - False
* ]6 |4 V' |8 {2 d) v: ? - >>> math.isinf(0.1)
8 a4 J9 g; p n! V2 U$ `1 w - False
复制代码 ; d' K' T0 k1 m2 f* S
math.isnan(x) 如果x不是数字True,否则返回False" c g5 {4 S3 J# s
- #如果x不是数字True,否则返回False
% U! |: j5 o/ v - isnan(x) -> bool% X$ k4 L' n) d% k, r5 I2 b9 C
- Return True if x is a NaN (not a number), and False otherwise.- j1 a9 t7 l& c
- >>> math.isnan(23)
! H n$ }/ u; O8 z - False
2 g. |; q9 Z* k3 T9 _ - >>> math.isnan(0.01)" d1 S/ z8 ^$ T& c1 s7 L3 w: T
- False
复制代码 & J& f! V& \, y* h6 l
math.ldexp(x,i) 返回x*(2**i)的值9 I- S/ s | u4 O ?: I/ Q
- #返回x*(2**i)的值
( f3 V2 \; {: Y* |/ b& w - ldexp(x, i)
9 p4 [2 N% k7 m3 s1 h T - Return x * (2**i).
8 P8 W$ ~- o, b% k1 W6 C' Q - >>> math.ldexp(5,5)
" j* M: u" s7 q - 160.0
0 u6 O6 M7 C; L& V, e, s6 c$ H! Z& \ - >>> math.ldexp(3,5)/ R; [0 P& A7 l# T# m% N" x
- 96.0
复制代码
! h! t7 j B9 v3 x7 Vmath.log10(x) 返回x的以10为底的对数6 c) D& }8 {7 P7 L2 ~, `
- #返回x的以10为底的对数
J2 V" m5 w R& U i* b - log10(x): s ]; ~9 x+ P) ]7 u4 l
- Return the base 10 logarithm of x.
* r; a P. J) R8 K, X3 ?6 f - >>> math.log10(10)
3 {1 M$ ?- E- Y- H+ d, F3 V! n - 1.0: b' Z |$ m# C6 b% G. `: t) D
- >>> math.log10(100)
. }+ x @" z3 F: U$ a - 2.0+ q+ |5 r% I8 H I$ Y% j( T
- #即10的1.3次方的结果为20# B7 c7 E- g$ f) l8 x3 {
- >>> math.log10(20)/ x8 U) `' W c. Q
- 1.3010299956639813
复制代码 4 |: x/ `% ?2 `% G9 P$ Q, [
math.log1p(x) 返回x+1的自然对数(基数为e)的值/ B; I/ X h# Q; Y: I' b
- #返回x+1的自然对数(基数为e)的值2 V+ f! s$ A; z8 i* p
- log1p(x)) p0 c3 N9 x$ b# s# Z' Z
- Return the natural logarithm of 1+x (base e).. L' w- A$ s1 t" R" ^
- The result is computed in a way which is accurate for x near zero.( ~8 d# c& Y z n0 q1 C* A
- >>> math.log(10)
7 W$ `1 x! h$ z3 G. ? K& o - 2.302585092994046
; J4 a0 b0 K p; e - >>> math.log1p(10)8 V- C" ~# J" t9 T- H( [
- 2.39789527279837072 E2 R" I2 U" O$ j5 e4 \( Z" w, z
- >>> math.log(11)- z. a! o# F+ b @; E; E
- 2.3978952727983707
复制代码
% R$ ~( [! c1 P* A. omath.log2(x) 返回x的基2对数# v! \9 s+ i$ q1 i5 y8 T
- #返回x的基2对数* r7 v/ N9 n l7 x8 K
- log2(x)
x+ a# w3 W9 h( \ - Return the base 2 logarithm of x.: {& V& n+ f2 b$ w' |6 D
- >>> math.log2(32)
0 \! G" [8 ]6 e( K t- ~" M& K - 5.0
. D: G1 x! g7 x$ B' H) v - >>> math.log2(20)8 @+ {* Y' W& |4 W4 `- C
- 4.3219280948873639 g+ Q# L8 |7 l E6 n
- >>> math.log2(16)9 I, d# V6 i6 l* _: t
- 4.0
复制代码 3 k( E: [6 g, v# R2 N, J8 P
math.modf(x) 返回由x的小数部分和整数部分组成的元组
. g. h5 F( L+ a6 ~' }6 \9 l- #返回由x的小数部分和整数部分组成的元组
! G2 H9 V' B' V( C" {1 n. o5 r - modf(x); V$ d7 A3 W* g; }2 m& p
- Return the fractional and integer parts of x. Both results carry the sign
7 B( @9 v9 e. u* p: u' o( ? - of x and are floats.2 g& O( H6 u, ^7 U) i7 O% O
- >>> math.modf(math.pi)" g, \) d6 F8 B% s8 v- G
- (0.14159265358979312, 3.0)
0 K7 L" Y. w5 t, b - >>> math.modf(12.34)
! K5 x6 n$ i1 I$ \ - (0.33999999999999986, 12.0)
复制代码
& z5 H8 H' b* K+ Nmath.sqrt(x) 求x的平方根
, k4 i4 ?" N+ k- Y# d* X* F- #求x的平方根
# e3 z, r- P) g$ k3 o+ j" l% ^ - sqrt(x)
3 P3 i# [- d6 V* g - Return the square root of x.
$ `, p* L' q3 o3 n' w7 P - >>> math.sqrt(100)
9 n/ X l W& Q4 X* k - 10.0) R. W1 Q, p0 x# T, ]* f2 W4 H8 l
- >>> math.sqrt(16)
; B0 D; f& X, n: t8 e5 w - 4.08 f% c; v+ K% p
- >>> math.sqrt(20)0 p2 y1 E* A7 K. \, m
- 4.47213595499958
复制代码 ) Q' b" W7 J9 a, I6 S: ~# o+ N! Y
math.trunc(x) 返回x的整数部分- #返回x的整数部分
$ F! B# ^8 i7 K/ s, N: t6 m - trunc(x:Real) -> Integral9 S7 }; y, r9 h4 n
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
% H( P4 e) w" v - >>> math.trunc(6.789)2 T- J, l, p' ^9 E% t
- 6
8 O9 a" }/ ]" N* o# N - >>> math.trunc(math.pi)# |: l( m1 r4 g9 h' @& K; W0 _# Q b9 T
- 34 i8 w0 z! \) b5 n1 U- k# n
- >>> math.trunc(2.567)& q8 M0 W% T7 v; C9 x' S
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|