马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ ]2 k0 u _( e- Z+ t& G U; H2 `
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。8 {) B; t ]1 X/ I! J6 i
$ d) X8 q0 @& ^% s7 p" c& K方法1:
- `5 [: ]- Q* z) O- >>> import math
! ?7 A: T! A8 e, l$ Q' }! U% u - >>> math.sqrt(9)$ B7 {* i. ?/ y% ^2 k- Y/ |# q5 j+ L
- 3.0
复制代码 方法2:
0 O. @3 u- M7 r P& P- >>> from math import sqrt/ O8 }4 Y/ y) J/ S
- >>> sqrt(9)+ k' v! ] v7 |( \: U. z" s) |
- 3.0
复制代码
" W3 I! C, {' c, T R: | ! b* ?( q$ p2 o9 K- L& c& R4 \/ H
math.e 表示一个常量
3 ?2 u, ?: { C/ Y- #表示一个常量
+ e. G W# R! q) T, @ O - >>> math.e$ C0 h( W+ U9 U. t
- 2.718281828459045
复制代码 8 X; C1 e8 B8 L; G/ x {& N* y
math.pi 数字常量,圆周率" a7 B+ z0 G0 k1 d/ ?7 A5 u0 k
- #数字常量,圆周率8 D( w0 s, K- t/ W; y8 _- `
- >>> print(math.pi)
# A) T, M3 f4 i6 M& H Z - 3.141592653589793
复制代码 % Z2 D; W _. D" q3 p
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! T4 j1 R( @4 }% m2 ^7 I1 r- #取大于等于x的最小的整数值,如果x是一个整数,则返回x g: ^* h" _: c8 E" [8 @% _7 c
- ceil(x)
* _& R0 q1 L4 h$ [, U( D - Return the ceiling of x as an int. A" @8 p) M# g' b
- This is the smallest integral value >= x.: f/ I6 E7 I h- Q( w' y+ X
- + E8 X. |$ |+ G
- >>> math.ceil(4.01)
& L7 L. f' u2 @5 @1 y$ } - 5, K' o! b) C- N. s/ H) F i `
- >>> math.ceil(4.99)5 |; [' ] x4 \2 F% n$ Y
- 5) f1 O% ?; Q& N2 G8 A5 L
- >>> math.ceil(-3.99)" m, n- j' G1 h( ^( X
- -3( I3 U8 F1 U. W u* u( ^ [; x
- >>> math.ceil(-3.01)( U, B" ?9 H# ~9 Y9 F9 {
- -3
复制代码
& H8 C1 ?3 l. s* @3 ?9 Hmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 @7 X+ [0 ^! F$ [
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身. h' V* M& G5 f
- floor(x)
# v4 Z. S( P2 @9 |- S - Return the floor of x as an int.& w M( u& b; |' `5 q
- This is the largest integral value <= x.1 D( m6 G7 \; m1 z' l
- >>> math.floor(4.1)! @* s0 v7 y. y; {( N6 P+ \1 ?# K
- 46 z4 R0 Y% T/ B1 H3 d
- >>> math.floor(4.999)2 Y; q* f' s5 X- M* e; Z4 g
- 4
! z% `0 Z5 e, h" ^/ U! I: y0 o - >>> math.floor(-4.999)
8 H3 X7 T" I( ]. i1 d' ^ - -56 @4 T5 N1 J( f: W" q
- >>> math.floor(-4.01)6 C2 I- E9 Z' k4 h
- -5
复制代码
4 F. v% v" g1 S5 V! kmath.pow(x,y) 返回x的y次方,即x**y1 j" \: r' B2 a) z) ~: _/ s, |8 c
- #返回x的y次方,即x**y; G+ w5 e1 ]. x- D
- pow(x, y)
" v4 W7 r: e8 _' J! H5 z/ Q4 k W# Y - Return x**y (x to the power of y)." R1 u& ]% d$ U. O
- >>> math.pow(3,4)) \% `. j* J3 a9 d/ q
- 81.0 }& p+ @& b& [4 L
- >>> 1 x5 T( q( G/ F
- >>> math.pow(2,7)4 r3 N2 J7 C: O$ }8 Y
- 128.0
复制代码 $ Y, y7 k c! x) F( O! e7 H
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
+ E: O% C& u O3 H& j- M) U" J- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* y: p# X; ] n
- log(x[, base])6 R, D. T+ w0 `( ~0 f- t8 \, Z
- Return the logarithm of x to the given base.2 i. x" i3 @8 t6 S" b; O1 |. x
- If the base not specified, returns the natural logarithm (base e) of x.
0 g; T/ p/ E: W2 c2 w - >>> math.log(10)5 X( m" _# l1 H% k) U
- 2.3025850929940464 o% [1 L" ]4 C) T6 m% B
- >>> math.log(11)) M: Q0 i% q* m! Y8 q* W
- 2.3978952727983707- R- f% }& m C0 O" \
- >>> math.log(20)
7 t. Q+ j; G2 y' f+ c$ ?+ M* b - 2.995732273553991
复制代码
/ C# }% g" w. ymath.sin(x) 求x(x为弧度)的正弦值( K2 f0 u( _6 l
- #求x(x为弧度)的正弦值
% n; t7 _, A2 L% T" N. [# f2 i' N - sin(x)$ |! \/ M, u3 i2 l; _- Z4 }* X7 \
- Return the sine of x (measured in radians).
+ e% b0 J' T" x7 S - >>> math.sin(math.pi/4): [, z# e: U* s8 F
- 0.7071067811865475
( J% a2 ?9 Q2 V3 }+ t' S - >>> math.sin(math.pi/2)2 a- ~2 b/ i& A- t2 q
- 1.0
' N2 V3 N+ [5 h& }) r - >>> math.sin(math.pi/3)
' w* @( ^0 ~! X( a w3 l# b% j- _ - 0.8660254037844386
复制代码 m( q% }* M1 t3 i- T4 X2 }# K
math.cos(x) 求x的余弦,x必须是弧度
$ s5 m3 N9 @6 F& ~2 O- #求x的余弦,x必须是弧度) j" g# {4 A9 B7 R: \( k( b
- cos(x)
/ C+ r8 w' [, O4 k: V" D8 ~ - Return the cosine of x (measured in radians).; a8 D. i* ^1 r# R" [4 s
- #math.pi/4表示弧度,转换成角度为45度
6 Y/ h, `9 p& M: \. a% W0 a3 R - >>> math.cos(math.pi/4)( G9 U! |( C. B/ V9 z6 c; |5 n+ g
- 0.7071067811865476
f N5 c8 A1 Z& n7 }6 K - math.pi/3表示弧度,转换成角度为60度& c0 O' e2 [; Q* _5 Y. P
- >>> math.cos(math.pi/3)
" e: b& W/ \/ s, s# l - 0.5000000000000001
. `! v( ]4 D% a& B i/ a! ?7 X; i - math.pi/6表示弧度,转换成角度为30度/ f2 n9 t! L+ d& }; ^% u
- >>> math.cos(math.pi/6)4 U. p; F( F/ e. X
- 0.8660254037844387
复制代码 6 @+ ]& n( L) Q% s& p
math.tan(x) 返回x(x为弧度)的正切值8 I S' A% J$ g K O& n/ v: r+ m) d0 J
- #返回x(x为弧度)的正切值# A- B! r* ^# j0 b6 H
- tan(x)4 E% Z7 k/ v: f* `0 t& N+ u
- Return the tangent of x (measured in radians).% p# Q9 Q3 }( z3 E
- >>> math.tan(math.pi/4)
8 W; k! e# a0 P3 d6 i6 N - 0.9999999999999999
' p: r0 z4 f8 A% x) J3 ~+ J - >>> math.tan(math.pi/6)4 g& R8 ]4 F- `9 K; O" I3 W
- 0.5773502691896257$ b% p# \# Q3 g# f
- >>> math.tan(math.pi/3)
, T: n9 B- D) M) l$ Q - 1.7320508075688767
复制代码
( Y( I( Q1 B8 P9 W0 j/ I7 |math.degrees(x) 把x从弧度转换成角度7 O- }, j; G8 r% x& X
- #把x从弧度转换成角度+ p. Z0 i ] z( U; a
- degrees(x)
1 x9 x# y) p2 q* Q7 l9 v - Convert angle x from radians to degrees.
8 z% X$ m8 `+ d( g |
7 W$ W; ?5 C+ i& z* g5 U- >>> math.degrees(math.pi/4)7 V/ S' s* }3 M8 P
- 45.0; O5 G: ]. C1 D! f( ]6 L0 {3 n
- >>> math.degrees(math.pi)
% r6 I8 i* I2 H7 A& H - 180.0
7 `% l; j/ t: Q8 a' [2 T/ H - >>> math.degrees(math.pi/6), P) ~- d: p! j9 j6 k
- 29.999999999999996
/ c9 ~/ x/ Y3 X/ x' b; c6 [- u - >>> math.degrees(math.pi/3)% [! [0 }* ]* O+ l! i2 ?/ J
- 59.99999999999999
复制代码
5 p( v! a1 R0 M7 w: Umath.radians(x) 把角度x转换成弧度- V- L) ^% e" Y, V# }" g) M4 h
- #把角度x转换成弧度
) t# P# `& ]$ ^6 M! S - radians(x); |: t" X- v0 C' c7 F+ ~' r" Y# B
- Convert angle x from degrees to radians.' c+ x1 C9 W7 I& C
- >>> math.radians(45)
+ W$ q/ R6 l# a) K1 d1 A - 0.7853981633974483
, b9 q2 ?# V5 k. @9 Y2 e" P; o - >>> math.radians(60)
5 m. Z: ^2 Z" i+ E - 1.0471975511965976
复制代码
) R, U @$ y/ N" u' N/ B5 \math.copysign(x,y) 把y的正负号加到x前面,可以使用0% K1 @3 Q, K# j% G" j
- #把y的正负号加到x前面,可以使用0
' n: |8 l% t0 p6 i' f! ? - copysign(x, y)
1 p/ J }- ~3 _- }/ ?5 J1 F3 A3 a - Return a float with the magnitude (absolute value) of x but the sign ; O4 D/ N8 ^# P8 u, ]1 Z
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) , q) r- I4 Q7 v o/ t3 k1 t" @8 i
- returns -1.0.9 a* |" n! D; e" w
- 7 J4 w7 e% Q1 b
- >>> math.copysign(2,3)
. W2 u1 b4 K4 O0 ?: V - 2.0
% S$ D" D# ?( Y7 q/ O - >>> math.copysign(2,-3)0 H; ^$ j3 [3 t! `& d6 K0 f& ^3 U
- -2.0
5 L5 v% o: a: Q' ^2 n - >>> math.copysign(3,8)* L* h# _) C- e2 R
- 3.0
$ Q/ }8 w8 }. U1 M) P K - >>> math.copysign(3,-8)/ j9 k& ]" ~+ U! d
- -3.0
复制代码
; H1 A3 m" Q) d+ C7 a7 F6 Rmath.exp(x) 返回math.e,也就是2.71828的x次方
+ W9 n' q+ \) v T- #返回math.e,也就是2.71828的x次方
1 O3 A' D0 u; I. z - exp(x)
+ b9 B3 v7 m# J! R+ ] - Return e raised to the power of x. r/ l, n% t1 _' @9 _. l; Y* L
- $ z' ?) P4 U$ \3 h; j- h" i1 b; G
- >>> math.exp(1)
4 S$ [& o6 s4 a5 [; q1 | - 2.718281828459045
5 K/ Z) j0 U; `0 d& a8 R - >>> math.exp(2)
8 V% Z, A/ t. c - 7.389056098930658 x) I1 H! s1 [( b; Y
- >>> math.exp(3)2 t6 @" d0 W! t# g
- 20.085536923187668
复制代码 + m! N$ J& ]* U9 e
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1- v( j. ?- t; A: `4 o
- #返回math.e的x(其值为2.71828)次方的值减1
G6 p* e, v- }( c - expm1(x)
+ Q4 R; O' t+ J4 ^ - Return exp(x)-1.
# t X8 I9 C( f0 |' Y - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
( I( [& Z! _' d
0 E& f0 Q6 U, j5 |4 _. M% n- >>> math.expm1(1)( ?$ K7 b7 [$ \* }
- 1.718281828459045% a; F! _4 z$ T4 F; ]' a. V* `
- >>> math.expm1(2)
" U n8 u* R* K+ g - 6.38905609893065( j. w3 \6 `$ u
- >>> math.expm1(3)
' z& t2 Q9 E( \! J5 S) i% b' C( C - 19.085536923187668
复制代码
+ S( {2 w+ P1 j$ e1 r6 o- [2 tmath.fabs(x) 返回x的绝对值& G' W4 O; `+ S" Y! x
- #返回x的绝对值
2 x9 `3 i; S$ b) c - fabs(x)' q- J2 q" y* }/ K5 Y0 a
- Return the absolute value of the float x.
/ g7 E3 ^$ p, N* G- J6 B" b: }% @
( E# b9 O' P3 B% ?+ n8 h3 Y- >>> math.fabs(-0.003)5 H% B( [" m3 ?* ?8 D7 s8 s- ~
- 0.003
2 F! F* k# ~+ Z7 i0 k. Y9 ]9 i$ F7 | - >>> math.fabs(-110)8 p1 H. f" r; _+ {9 u
- 110.0( x- b4 z- N" y! r# m
- >>> math.fabs(100)
: W, I7 C1 _- _5 H$ N# ]$ q - 100.0
复制代码
/ @, X3 l) F% ~4 u5 `6 Kmath.factorial(x) 取x的阶乘的值1 e8 L8 D7 l4 ]9 r
- #取x的阶乘的值
$ H6 c& @2 p# g! X" N, ~; P4 { - factorial(x) -> Integral
* ] \# B. `' |# C7 z - Find x!. Raise a ValueError if x is negative or non-integral.3 [( O1 |" t3 j3 |
- >>> math.factorial(1)
' A+ y N5 f% M, u& s - 1
I- `( o( N( A3 u* K' e - >>> math.factorial(2)0 |% V# `% c; @; c! c2 o
- 2
3 W( K% m) R- g( E# `. V - >>> math.factorial(3). E" ?. j; C: c/ V- d
- 6
: m5 w& F5 \$ |" l9 j& ~ - >>> math.factorial(5)" d0 V+ F L6 p
- 120( k, y {3 B6 J6 o+ L
- >>> math.factorial(10)
' Q( _4 a" W, w' d+ ^4 B; ^ - 3628800
复制代码 # n. r p8 J( t+ |- I
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数$ v% A# e1 O) l% ^. B: M; `
- #得到x/y的余数,其值是一个浮点数3 r0 M( ?8 }# z* B, E, h# A
- fmod(x, y)
' p5 h2 O6 x: C4 L0 {' M' t - Return fmod(x, y), according to platform C. x % y may differ.
+ ^* B: B. ]/ l - >>> math.fmod(20,3)3 e0 Z. A8 N* o `1 K2 D" E, }0 a
- 2.08 v- v1 i& Q: d" v2 c% [1 P9 m
- >>> math.fmod(20,7)& C$ e0 }6 r, E
- 6.0
复制代码
2 y* w1 i- q4 t; J( |; a# xmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& y1 \- M: E( j4 l+ b
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
1 W3 o) D, B) }6 @% P - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值& m0 g% _+ t( T( P
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 E3 t5 e9 A' g
- frexp(x)3 q" {' g# L2 e- _4 S& \3 s
- Return the mantissa and exponent of x, as pair (m, e).
+ L$ o2 b) L/ B3 Y - m is a float and e is an int, such that x = m * 2.**e.
9 H- [# b! @5 ^/ s! ^' d" B - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.5 }. \( n4 z5 k
- >>> math.frexp(10)& L- H& M$ t, ]5 i8 y% f0 _4 v7 m
- (0.625, 4)2 }( N; R3 d3 j* i, c3 f+ G' y
- >>> math.frexp(75)
8 n$ B# B) Z5 \- ? - (0.5859375, 7)
# E, m, ]7 i& M! x& [ C( x - >>> math.frexp(-40)
4 L/ e/ h: w1 I1 k) [ - (-0.625, 6)" Y) L/ P3 X0 {% O* A! g
- >>> math.frexp(-100)' N% U w5 U$ ?, c7 i9 d
- (-0.78125, 7)
) e0 g" B8 f- T3 @+ m2 t C, [5 \ - >>> math.frexp(100)! G) ^" a/ J5 ]; b
- (0.78125, 7)
复制代码
2 z" p7 x' p" G+ Kmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
3 L7 l8 g0 \5 D" U3 a% C7 j- #对迭代器里的每个元素进行求和操作; G$ _4 F, |; F/ |5 s6 Z
- fsum(iterable)1 a* r! Y% A% ]1 {0 M% e
- Return an accurate floating point sum of values in the iterable." @# s8 `5 H) [5 |# o" S) |; T
- Assumes IEEE-754 floating point arithmetic.0 E6 k& s- p) S7 g: n; Q0 c4 q4 d
- >>> math.fsum([1,2,3,4])% y0 n2 z! l9 [ T5 |; ^( U- a
- 10.06 d" c, l3 n! u4 N! m5 p
- >>> math.fsum((1,2,3,4))( ^) C% W/ S' w) s7 L7 a9 J* X# l
- 10.0
0 ?3 W. Q# S ? - >>> math.fsum((-1,-2,-3,-4))
6 q) n+ D# D% W& k4 V- q9 f7 Z9 G/ L - -10.0+ o7 u8 _' r+ z+ ~% k- \8 b
- >>> math.fsum([-1,-2,-3,-4])
- K0 l8 K8 E0 Y - -10.0
复制代码 / M1 _1 f" A, x/ F) Z
math.gcd(x,y) 返回x和y的最大公约数0 {% S& z1 _5 E* k4 D
- #返回x和y的最大公约数0 C0 \: |2 j9 O6 ]1 L L
- gcd(x, y) -> int
' E; e/ M* B3 z; S- ~' p - greatest common divisor of x and y8 I0 n) W/ x3 Y: P K
- >>> math.gcd(8,6)' o+ f2 X- B4 \4 f: V; m6 x: i
- 2
% c, }+ p5 H' i( }8 t7 b: r( U% a - >>> math.gcd(40,20); k6 x* T$ F" g& r1 v$ Q
- 20
, Z' U. n: M w4 g$ j; X. s, l; L - >>> math.gcd(8,12)' ?6 E; f2 x! r, r, p
- 4
复制代码
* T6 I' K/ t$ N- Jmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 s( [# E% I0 R7 F- #得到(x**2+y**2),平方的值7 T( O' v/ ^# w# b+ P o- g! d
- hypot(x, y)1 ~7 ?. H/ `* g* k+ r( ]# w
- Return the Euclidean distance, sqrt(x*x + y*y). g, S4 j2 }7 _" ]
- >>> math.hypot(3,4)
2 D) ] D2 \, m( A# g - 5.06 Q: m) m/ s; u* I" h F
- >>> math.hypot(6,8)) O; P5 T* V$ l* C' |
- 10.0
复制代码
0 l6 t3 D: m+ l. nmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False0 w/ f$ q' I+ M1 H% V4 t/ |
- #如果x是不是无穷大的数字,则返回True,否则返回False6 O8 J8 g" f+ e" ~7 M- {
- isfinite(x) -> bool* ^, O* `3 J( A" o% I6 e' X
- Return True if x is neither an infinity nor a NaN, and False otherwise.6 D2 | g! I' f
- >>> math.isfinite(100)
! n2 f1 h, I+ O - True
) C& F% [* c' [ - >>> math.isfinite(0)3 q, H/ C; i# _3 X, k- S! C4 m i
- True# [. l# L! {& w. h( Y
- >>> math.isfinite(0.1)" N/ p9 V& E+ C5 U# {2 e
- True7 }" z) y$ w* |$ X; D d# C6 O
- >>> math.isfinite("a")
8 E7 I" n4 I; b: f - >>> math.isfinite(0.0001)! i4 J0 r: a: _. j) {4 s
- True
复制代码 c; V% O& \% u$ r+ o2 y* _
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
% `" q9 L. I" m- #如果x是正无穷大或负无穷大,则返回True,否则返回False
1 |- I& b- h0 [6 N - isinf(x) -> bool6 k O s- M0 a1 f9 v
- Return True if x is a positive or negative infinity, and False otherwise.5 h0 ~7 U' [5 e: l( K
- >>> math.isinf(234)
- `& J. T2 b L0 ~; v( E' I( X - False" u* @5 ~" R* e" g/ z
- >>> math.isinf(0.1)
- b& y' K5 p1 T4 i( ?% { - False
复制代码 4 }0 s2 H7 \, _7 j0 H2 ^
math.isnan(x) 如果x不是数字True,否则返回False
. n3 e* o) \) d' ?2 i- #如果x不是数字True,否则返回False& j2 h4 K1 \( l( g/ `; R' P
- isnan(x) -> bool
& Q+ x& s9 Z, v9 }8 }4 r; u4 j - Return True if x is a NaN (not a number), and False otherwise.8 T+ }) \; M3 q1 u
- >>> math.isnan(23)) @1 A* R% W% j7 r! `/ r& j
- False
3 u) t% E- a! }6 T - >>> math.isnan(0.01)
2 [! f( g, N3 S! S; c5 ` - False
复制代码 q3 Z5 W$ M) m
math.ldexp(x,i) 返回x*(2**i)的值
* `: w: ? {. A3 ^' N- ^+ h+ Q! ^- #返回x*(2**i)的值9 Q( l# m! x4 G6 m$ \
- ldexp(x, i)
) y! Y* G( i) |4 f" c. T c9 W- E: ^ - Return x * (2**i).0 ]- k J0 S; }! @
- >>> math.ldexp(5,5)
) N( i! l" U1 X - 160.0
) p( r6 d/ r/ r4 B. b; c& c - >>> math.ldexp(3,5)
, _9 r$ l; J3 _" y7 C: D. q8 O - 96.0
复制代码 6 y: |% K5 Q1 j# D) l: x
math.log10(x) 返回x的以10为底的对数! K" e4 |- A! t1 W2 @
- #返回x的以10为底的对数
) L( f3 }+ V% o# T: Q - log10(x)! p1 N$ T' T& ?3 G4 o* J
- Return the base 10 logarithm of x.6 W' |9 S) P5 r$ f; U
- >>> math.log10(10)
$ A% k' U/ T+ d: B1 X& b& q - 1.0' W$ u; v, O j% [" n" M& {
- >>> math.log10(100)
$ ]1 J4 \7 Y* q5 a - 2.0* O" T" }0 ~+ z# g% Q* h, ^# k
- #即10的1.3次方的结果为20; L: y/ D' X, M& D" D% e5 y- G0 u
- >>> math.log10(20)/ e; C$ \1 g9 q3 T
- 1.3010299956639813
复制代码
$ |; `2 r3 B1 n+ y% X* Smath.log1p(x) 返回x+1的自然对数(基数为e)的值' P- L/ H- V A# E9 Z+ `: w+ O) w% b
- #返回x+1的自然对数(基数为e)的值
: ?- q& t1 M% C7 S( z; V; o: m# V* G - log1p(x)
9 }- F) i0 X3 o: u - Return the natural logarithm of 1+x (base e).
9 H8 A& K" n; c' W# A0 l - The result is computed in a way which is accurate for x near zero.
$ O9 t4 M4 N% b - >>> math.log(10)
$ Q& _! N+ U, v8 X1 s - 2.3025850929940466 U6 R( P# R! j# V7 `( O; h
- >>> math.log1p(10)& B0 v0 W* @+ H ~' E S+ r$ i
- 2.3978952727983707% e! s! e& T. ]) I4 L y
- >>> math.log(11)4 n( C5 m: Y, T8 `3 Q: N
- 2.3978952727983707
复制代码
2 o" `, n% O$ u; |4 ^% j ~math.log2(x) 返回x的基2对数5 k( n2 ~8 M$ h: B& V- j0 T* Z
- #返回x的基2对数% E1 r E' s/ A# k$ ]1 ^. c7 C; Z# R6 c' n
- log2(x)! g9 Q- Y) W; a- }: {
- Return the base 2 logarithm of x.
. l( |2 N0 e( J+ x( v0 H - >>> math.log2(32)& u& K* U6 u8 u3 E8 v
- 5.0
% ~# v' M) H( Q - >>> math.log2(20)2 m, n2 A1 |4 P! g2 e) G7 m; ^
- 4.321928094887363
4 {& f% x9 T1 A7 _! G - >>> math.log2(16)
; J' s0 T7 N3 a - 4.0
复制代码
! P r3 c4 a0 J& r4 }$ d" c+ Cmath.modf(x) 返回由x的小数部分和整数部分组成的元组
* c% n, E/ H' X- #返回由x的小数部分和整数部分组成的元组
+ t( Z" {( j1 J7 ? - modf(x)( g! e, s' f/ O F. O% `$ Q
- Return the fractional and integer parts of x. Both results carry the sign6 E' x' V/ ^! Q1 q
- of x and are floats.
1 Q9 T, p4 u+ Q - >>> math.modf(math.pi)
0 L- [3 B# u! D - (0.14159265358979312, 3.0), F1 i8 T" p, K0 u4 k; P
- >>> math.modf(12.34)
! r1 P/ \" l4 o - (0.33999999999999986, 12.0)
复制代码
. Y) w5 _- v1 v) z: C7 vmath.sqrt(x) 求x的平方根) G6 H& C4 a' ^% W: [# Q
- #求x的平方根
! @; [% j* P3 v* Y" s - sqrt(x)
5 a' N' N9 h$ a9 m* S - Return the square root of x./ s& t/ J* T. ]2 I+ {
- >>> math.sqrt(100)- n; ^9 b8 r- g% M$ C" O
- 10.0$ _, e& Z" h/ m9 P c* n6 `
- >>> math.sqrt(16)
3 k+ k- d! H% p* r4 [( T/ e( o - 4.0( C: Y/ e7 e! f- q" C) D
- >>> math.sqrt(20)' E* D8 f7 l6 c- B, }
- 4.47213595499958
复制代码 0 R. e% U. ?( G6 i7 }4 N
math.trunc(x) 返回x的整数部分- #返回x的整数部分
: e, J+ |+ I; ~. w* ~ - trunc(x:Real) -> Integral( S9 W( A+ G3 g5 v
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 Z- M% |9 j+ \0 I, `. _! k
- >>> math.trunc(6.789)7 a8 ]$ H+ _- N' m/ S! o8 o
- 6) G z/ O Q4 P
- >>> math.trunc(math.pi)+ a* w# k4 t- x" M1 K
- 3
# Y/ q, v4 O5 e - >>> math.trunc(2.567)
) ~1 t& h1 `* d5 A9 @ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|