马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
' s9 d6 I1 e5 }9 o0 y2 \
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
2 n6 u& k, R1 R9 S+ s7 B4 ?& }9 V5 _
方法1:
- V* P: W+ ?& ]. `- ]3 }- >>> import math4 q/ G% G" @( B
- >>> math.sqrt(9)
r _% _) n7 h4 U, g- z - 3.0
复制代码 方法2:! ]! S1 l5 w3 }6 Z, Y* O$ s9 g
- >>> from math import sqrt/ M% F+ X0 R9 V: M& A0 D
- >>> sqrt(9)
! C/ r. \; p8 L - 3.0
复制代码 * M7 w; F0 x& t2 e
* ~1 v$ j( T O8 M
math.e 表示一个常量9 U7 _: P2 b7 e: `4 S; v
- #表示一个常量. H- r) ~; s7 g9 z
- >>> math.e' I) r) W/ U& |2 Q' p1 k% y
- 2.718281828459045
复制代码
% z* G' q, o5 V4 W( a$ imath.pi 数字常量,圆周率
1 |6 M* n; W# {- #数字常量,圆周率
3 ~ t& \# M; ~) u9 Z: q# L+ P1 t - >>> print(math.pi)
; j% `* C6 H6 t$ K- ?( L - 3.141592653589793
复制代码
1 Q, i$ p7 F: @2 e! rmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x1 S7 x* c) u* J$ q9 v
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 g" ^$ j4 E6 C: O' M, Y - ceil(x)9 M, e# X1 B( c! r* [
- Return the ceiling of x as an int.8 _8 O/ p$ h& h Q* Q: c2 h" j
- This is the smallest integral value >= x.
9 U( P" `; r" T$ {) n - 1 G) k* i& x+ K( ]/ f3 _: W" \4 e
- >>> math.ceil(4.01)
7 r0 w3 p; J* @$ \* g4 ^! \. D - 56 ^( W. e6 [2 B! @9 N
- >>> math.ceil(4.99)/ H& z5 L5 S, m0 Q, C
- 57 ^& e2 [( m/ l# n- Q
- >>> math.ceil(-3.99)/ P2 j9 [( ^: k
- -3
8 V# o. R" R7 M- K4 j' S' \3 { - >>> math.ceil(-3.01)
6 U2 b9 R) e1 w N: A% K - -3
复制代码
/ A V4 Z* y0 H$ a6 v6 Rmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 w- q U/ T8 n0 G1 N& j }: T" n' W6 A
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- L9 a# ]9 J7 e& s
- floor(x)
7 c5 ~" O$ E, ~' r- }/ C% k - Return the floor of x as an int.
[8 _" [+ X/ @1 y- O - This is the largest integral value <= x.
% ^4 ~& {# l# u - >>> math.floor(4.1)
7 l7 M6 S5 L$ g - 4
# z0 t8 a$ @. @' ]* ^* { - >>> math.floor(4.999)
8 `. {+ f7 b! P* [1 l6 Y - 40 @4 J6 O5 z7 I1 x9 k+ D
- >>> math.floor(-4.999); Y; Y& F0 }6 O. Z' L) B' G
- -51 ~3 f% A1 X+ z1 R% E
- >>> math.floor(-4.01)1 G6 q2 K6 r6 o( u9 E1 c' g) N9 I$ Z
- -5
复制代码
8 |# z( C, r, A1 _1 r& [math.pow(x,y) 返回x的y次方,即x**y% X; { i; P q, `" D
- #返回x的y次方,即x**y
2 H4 O9 w: \* U# w9 g - pow(x, y)
; R4 N9 _" Z8 y$ M5 v& N- p - Return x**y (x to the power of y).
+ z$ a0 [8 \" v3 t( H - >>> math.pow(3,4)4 M2 r% j- m0 N( H' b/ H7 o
- 81.08 M- h5 ~/ ]/ i0 m" z0 ]6 G$ R
- >>>
. t8 m/ M5 h; g/ P9 x- d0 }( w - >>> math.pow(2,7)" `5 @/ d4 J) l3 h& N/ J
- 128.0
复制代码 8 Q0 J& D6 s; i# T# x
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* A/ |- j3 ]1 i- g- N: `7 F! t
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 l: U. o6 Q! M6 ~% U5 Q - log(x[, base])! q) I# l8 z5 d
- Return the logarithm of x to the given base., l+ M# V" w L8 F
- If the base not specified, returns the natural logarithm (base e) of x.
" u: |9 ]# u3 D8 u& K2 Y+ I, h; f - >>> math.log(10)
! B- T3 {6 [- @7 e1 P - 2.302585092994046
5 X( |) [) o8 _7 [- r( `$ Y) D - >>> math.log(11)
% L" U+ W% y' o - 2.3978952727983707
% L. t' i* n* P% E8 V3 c* Y - >>> math.log(20)+ P W/ B5 E' p# Y6 M, D `
- 2.995732273553991
复制代码
8 X$ E2 H8 N% ]- h# e, R- Y0 Vmath.sin(x) 求x(x为弧度)的正弦值2 e$ A& E+ O+ k% `/ \, U! d
- #求x(x为弧度)的正弦值
& i" S1 J( |6 f( p: M - sin(x)7 |9 S, R r1 {+ t3 S. v
- Return the sine of x (measured in radians).7 s: D0 B+ F- H( O7 L" [
- >>> math.sin(math.pi/4)
- o) v8 Z3 a( u) s - 0.70710678118654751 {, Y( Y Z+ I k. \9 R
- >>> math.sin(math.pi/2)
7 R3 h0 C- ^4 B+ P# R- t - 1.0
' ~7 C$ A3 b5 y ^, i) C - >>> math.sin(math.pi/3)
" j$ E0 [3 q7 e+ Z3 z | - 0.8660254037844386
复制代码 & s3 T& a7 Q/ z7 G! M9 U6 L
math.cos(x) 求x的余弦,x必须是弧度
5 v/ U+ K' k7 s) n- #求x的余弦,x必须是弧度2 v- w# U' w" A
- cos(x)$ z. X3 q5 t# Y6 `
- Return the cosine of x (measured in radians).
* z6 g1 r7 }1 ?; q4 ~ - #math.pi/4表示弧度,转换成角度为45度0 j, D8 R8 x; j% l! w
- >>> math.cos(math.pi/4)
* R) v( b: W8 \! I5 N - 0.7071067811865476
3 z/ T m. ^ I( B U+ m4 | - math.pi/3表示弧度,转换成角度为60度5 z R7 z( m$ J( J6 ?
- >>> math.cos(math.pi/3)6 m# N0 o* u- ]4 H
- 0.5000000000000001
& g Z; U+ N$ K e- } - math.pi/6表示弧度,转换成角度为30度
' ^" X. Q: f) j' k& R6 c - >>> math.cos(math.pi/6): y1 J6 y l; \) a. r+ [2 ~
- 0.8660254037844387
复制代码 " @! u+ `: A9 ], w
math.tan(x) 返回x(x为弧度)的正切值
+ K" q' J! }8 Q3 y- #返回x(x为弧度)的正切值1 u$ w) H2 i8 y, j4 H6 ~
- tan(x)# v k( Q& V7 M2 N8 ~7 f3 n! {
- Return the tangent of x (measured in radians).
/ u: v5 w( [0 f9 m1 o! M u - >>> math.tan(math.pi/4)
. j5 |( P- E1 b' x- G( L- ~ - 0.9999999999999999
$ N) l3 L( m! k5 {) v& h* v9 @+ v* X! Q - >>> math.tan(math.pi/6)
) s. ]- E) w, f - 0.57735026918962573 [5 r9 [- ^! T O9 X ]' ?5 I
- >>> math.tan(math.pi/3)0 ?, q0 ? S& x0 S Z
- 1.7320508075688767
复制代码 - d: E0 F* e" I/ g, m
math.degrees(x) 把x从弧度转换成角度- J+ _ F; S, j6 s$ e0 t' C+ z
- #把x从弧度转换成角度9 P0 u/ ?0 p3 ~3 d" G
- degrees(x): d* X9 D3 w2 ^1 J: x5 P' |
- Convert angle x from radians to degrees.
; E- Z7 K# ?: ?) G5 t4 A% v% `7 G; G
% R% e' E! s# _$ N- >>> math.degrees(math.pi/4)
- z" q* s+ `3 k5 `' M0 q2 [ - 45.09 P2 |: N$ s+ r4 {
- >>> math.degrees(math.pi); M0 s/ ?+ V1 ?3 A! w
- 180.02 p& N7 F! L) Z6 s
- >>> math.degrees(math.pi/6)/ R& \5 x4 ^# \' T$ R0 a; I3 T
- 29.999999999999996
3 w4 E" m# |' y - >>> math.degrees(math.pi/3)
; `$ |" M! @( F* K$ d1 k - 59.99999999999999
复制代码
9 F% ~; N+ O7 ]1 dmath.radians(x) 把角度x转换成弧度5 p! i( Z! L2 }' N0 g# F' y7 Q
- #把角度x转换成弧度
" E( i+ Z; Q9 j" R/ C$ { - radians(x)
+ Y3 d0 [& U+ |2 [# x4 | - Convert angle x from degrees to radians.
3 E! r' u( t/ Q9 }0 }& C - >>> math.radians(45)
+ v* |' c* _7 Z$ p$ g- v t - 0.7853981633974483( ]5 P6 f+ k4 V; a& v
- >>> math.radians(60)
+ o5 G4 u! [- g# } - 1.0471975511965976
复制代码 $ U/ |! D9 {$ D9 ?9 @. T( }" g
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
+ k: m* Y# [5 C, f0 }) r- J8 T- #把y的正负号加到x前面,可以使用0* q- I* I) t0 R* B4 c$ a4 j
- copysign(x, y)8 V( l4 Y7 E% Z B* [3 s# _4 q
- Return a float with the magnitude (absolute value) of x but the sign
" s0 ~8 s. ]* l; E0 | - of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) I$ F6 H0 Y# f. M" C6 g
- returns -1.0.9 X1 Z2 h& m- C, V0 y
- 0 z* @- b- Q, C$ G* [( q+ g
- >>> math.copysign(2,3)% V& j4 y7 X! N& O% o. g
- 2.0
6 U: L7 n9 {$ x0 A9 `9 C! |$ q Y - >>> math.copysign(2,-3)
& f+ t' D) _/ t: W5 q' L( O - -2.0 F1 H! E6 N9 s- _& Z
- >>> math.copysign(3,8)- N" @$ O/ w- u1 }. m K0 W% h1 C1 \
- 3.0
v7 A, b2 A9 E3 f- l - >>> math.copysign(3,-8), i1 s9 F0 `6 O! Z5 g
- -3.0
复制代码
( A& o0 N& I4 vmath.exp(x) 返回math.e,也就是2.71828的x次方7 V6 V' }. C/ i# o$ z; p+ g
- #返回math.e,也就是2.71828的x次方1 K$ ]- C4 ?' ]! s o5 \$ F5 ]
- exp(x)
9 L: i0 c( O2 b1 k- r& k - Return e raised to the power of x.
9 u) M, m& A9 [5 `, B: b/ F - ' [, Q7 t+ K, I
- >>> math.exp(1)
) {& {/ P: b( Z+ k! T7 g9 y, i - 2.718281828459045
0 w4 E" [% I+ q, u0 g - >>> math.exp(2)
+ u8 F: B; a. K" Q t - 7.38905609893065 J; n4 A4 a/ S: k: \$ z' Z3 W
- >>> math.exp(3)
( W' O$ ^8 t; s3 h0 n" C' Z0 w: O - 20.085536923187668
复制代码 % x' Q6 }- D8 `+ f9 U, I7 H
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
0 A8 w a' g! T' C; t- #返回math.e的x(其值为2.71828)次方的值减13 O/ B- j; |1 Y
- expm1(x)
# Z& g; I _$ t4 @8 D- z - Return exp(x)-1.
! ~; J$ |6 E$ i8 a9 g. M( ]( A - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.! m z4 e* Z) F3 p3 S
- / {. X% g* g' R5 Y6 k& p- \7 @
- >>> math.expm1(1)0 L- d' s% g" I# d0 @ _
- 1.718281828459045
: ?6 S2 {- q" k. w. S - >>> math.expm1(2)
" A! u9 S" ?* n/ c - 6.389056098930652 r) j: Y6 k9 {
- >>> math.expm1(3)
B0 L |( g) [7 e+ [+ I - 19.085536923187668
复制代码 8 O6 A. L8 p% Y' M- N) ]( W
math.fabs(x) 返回x的绝对值
& V' X+ l) C7 l; W7 ]- #返回x的绝对值+ ^! X9 F' ]/ a' X6 G
- fabs(x)* d7 V+ V" m9 b2 |+ c" v9 A; n8 [$ }
- Return the absolute value of the float x. u8 |( m+ k3 }: q
: r$ D5 X. Y4 @0 s( M* W% o5 H- >>> math.fabs(-0.003)1 \/ c$ D$ | V4 H( s- B+ V9 X
- 0.003! K5 e& w/ b+ a8 r
- >>> math.fabs(-110)
9 E/ u/ ?( ?% y( T4 r - 110.08 K6 U9 W) V" f6 C. p
- >>> math.fabs(100)
" p" B6 U6 W4 h/ n! [: u$ s - 100.0
复制代码 ( h: n( f9 m! J& e
math.factorial(x) 取x的阶乘的值6 a9 r0 n4 Q! t
- #取x的阶乘的值; f H2 H4 _+ n+ C" G* [
- factorial(x) -> Integral
5 o7 ^0 \5 U7 E" I - Find x!. Raise a ValueError if x is negative or non-integral.
0 d3 a% b9 q0 w3 l! t/ l F ` - >>> math.factorial(1)+ O& b( X( F% P
- 1" t% q; p2 H$ n+ l4 F
- >>> math.factorial(2)$ I8 P* ?% C, ^: W/ ]. s4 t/ F
- 2& S( z0 z, U8 v4 U/ ~ g% a6 |
- >>> math.factorial(3)
$ \% F2 C- U% S9 I- w; w! h6 H - 6
7 w# |" q) d* ^ - >>> math.factorial(5)& j6 K* k, |. b% O. C; {- h/ e
- 1209 ~5 S( [5 k0 Q
- >>> math.factorial(10)$ E$ M1 `# w3 {# I0 i: g1 V. w& L
- 3628800
复制代码 ! ?9 \6 C9 O+ T2 D( V- l
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数 U. ?& C4 [. K" ~" Z+ U! B
- #得到x/y的余数,其值是一个浮点数 V: u7 N/ m* G/ T" u7 s' O8 d
- fmod(x, y)
* q: h5 y! e8 r- n8 X! q* i - Return fmod(x, y), according to platform C. x % y may differ." h% o- ?" H/ t. N$ ]
- >>> math.fmod(20,3)
! ` r5 N+ V! [. A - 2.0# Z1 f' e* L0 D; f; A
- >>> math.fmod(20,7)
, \( e' R/ F/ b: p - 6.0
复制代码
5 A( M8 S9 k+ A- Q- O8 r; dmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 ~7 G0 S8 Z% b5 U+ o2 C5 u1 E/ b- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,2 ?( [ ?6 O* j* z% a* y. I
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 [" \% U/ D3 X! K% D
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1* [/ K1 |1 Z- v' n* R$ ]1 o
- frexp(x)
# p9 K2 l8 U# `$ }- k1 g4 J - Return the mantissa and exponent of x, as pair (m, e).
7 {1 E; i- B% l& T - m is a float and e is an int, such that x = m * 2.**e.
8 L$ r& o- J6 |2 V. u0 F - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- T* H+ R# {% i0 j- x" A) L
- >>> math.frexp(10)
9 P0 T1 ?4 v( Y2 `( b. H- |, r - (0.625, 4): U" W J8 ?; Y: F6 ~7 E
- >>> math.frexp(75)
* L. o( q/ X$ F) ~" `# y - (0.5859375, 7)* o) m4 O4 X' }% x
- >>> math.frexp(-40)
! M3 Z5 d' n- t* O' U5 w - (-0.625, 6), k' ?, Y- s. j/ t
- >>> math.frexp(-100)
# V9 ]1 G; }! S - (-0.78125, 7). ?; [; H( E% g) k' A
- >>> math.frexp(100)
. ~& T- d5 z5 u% {. x+ o( O6 T - (0.78125, 7)
复制代码
; q: y* u2 J# W+ o& J& W; zmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列). w3 P q# J7 }! [: W8 T9 X
- #对迭代器里的每个元素进行求和操作# T# a/ z- t+ S6 a c' W& q
- fsum(iterable)* `) [+ I, U, R9 j/ F2 H+ Z5 \6 H/ E
- Return an accurate floating point sum of values in the iterable.
; u. J' Z. O8 q4 ~( q - Assumes IEEE-754 floating point arithmetic.
, \$ h. P* }5 V5 C. H - >>> math.fsum([1,2,3,4])
, \6 Q. \: u8 }$ S' D0 _( ^ r4 A - 10.0
. @( g$ J- \8 _: P$ e( ] ~" b$ O- Y - >>> math.fsum((1,2,3,4))
, G- O% e; U+ u; ^+ M5 o' \ - 10.0/ A2 M9 D9 {5 d, U
- >>> math.fsum((-1,-2,-3,-4))
2 n3 _3 @( Z( D* _3 ], U' z - -10.00 Y/ K' H7 s9 w/ g) @+ B
- >>> math.fsum([-1,-2,-3,-4])
' D9 A* v5 S8 g3 [- B3 M - -10.0
复制代码
/ G) ]4 B7 u- Z* ]- ?6 wmath.gcd(x,y) 返回x和y的最大公约数* W1 j3 x3 x) C. c6 o
- #返回x和y的最大公约数0 z" F1 `9 ?4 P
- gcd(x, y) -> int7 j/ Z/ a4 t6 h5 Y+ t# Q0 r
- greatest common divisor of x and y
% q. l9 R6 w: I7 [2 } - >>> math.gcd(8,6)
1 i* Q2 P" X5 B- F' q4 i; C! A - 2
" a# C2 b6 e% U" L. V! S, ^* O0 u - >>> math.gcd(40,20)
( w0 v( A R5 @) ] - 20# J* [) E1 F% U
- >>> math.gcd(8,12)
: m6 e5 C6 ~( `7 A5 T2 E1 c - 4
复制代码
% L' [' ?4 @$ b @+ ~; Qmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False5 a0 e: A: }) U' e$ F
- #得到(x**2+y**2),平方的值
6 t8 a/ S; |) `( ] - hypot(x, y)& m4 _4 r6 H# m b1 t& w
- Return the Euclidean distance, sqrt(x*x + y*y).
3 u" W1 v$ j( e. ?" K3 Z - >>> math.hypot(3,4)
* F% o+ y1 N5 t# M- r9 v( d) i+ b - 5.0
7 a$ a1 Q8 O9 t- ] w - >>> math.hypot(6,8)1 m! G- H# w7 R, o# V
- 10.0
复制代码 8 ^; R: y: ]# n9 N% ^* P
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False9 p7 y: U2 ?3 i9 y1 ]: b
- #如果x是不是无穷大的数字,则返回True,否则返回False% M4 u2 u8 R3 x
- isfinite(x) -> bool
( |+ J0 s" L0 z( g1 R9 k# q. F! k - Return True if x is neither an infinity nor a NaN, and False otherwise.
, f9 S% F) n4 o/ }2 m - >>> math.isfinite(100)
% ^9 b8 E1 E) @ - True
2 v; D9 {% `& g - >>> math.isfinite(0)
: d1 A8 Y, F' o - True
9 @8 ~, W/ O0 |0 |8 T# u) ?' S6 ^ - >>> math.isfinite(0.1)
1 h+ K% i* p% p7 {1 H! ` - True* ]3 r c% r% C: ^. U
- >>> math.isfinite("a")7 v' a; @ c/ q( r
- >>> math.isfinite(0.0001)
# V( X1 [$ G" R- N( F; W1 F, I6 t1 I. c - True
复制代码
) I3 {, g# o5 u1 a3 j+ A' [0 r& Umath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False. M7 t: ]4 L) l2 L- c" ~& W
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
* Y" i! C. Q9 f6 n$ v% I - isinf(x) -> bool
4 Z/ i0 Q0 X/ r/ K - Return True if x is a positive or negative infinity, and False otherwise.
/ G5 b: y3 e8 ]9 r9 X: t$ q - >>> math.isinf(234)
1 S7 @9 Z' V& \ - False
/ ]* |; ^0 N: Y% j - >>> math.isinf(0.1)
% o( M; L" ?( \. {/ }3 I7 x - False
复制代码
2 P" m% R9 A/ d& ?math.isnan(x) 如果x不是数字True,否则返回False
* ]/ F% ]% g' E s6 E1 V- #如果x不是数字True,否则返回False, `/ L* N$ p+ e; w( [
- isnan(x) -> bool
) R3 c( ~8 x2 }/ [/ I4 n - Return True if x is a NaN (not a number), and False otherwise.: `2 {, u5 s3 d$ q
- >>> math.isnan(23)
x/ \5 ^3 |1 y& p# T% e - False
4 ]! \9 f2 \& R& |* \ - >>> math.isnan(0.01)
0 D8 L$ H: p- q) @3 s - False
复制代码 2 ]( H2 U$ [3 @/ l+ C7 z* Q
math.ldexp(x,i) 返回x*(2**i)的值
) `( ]% E c( G! l: n* ~; z- #返回x*(2**i)的值$ V2 g8 Q" j* V. ]- {2 d
- ldexp(x, i)4 Y! k8 K" d5 k3 z
- Return x * (2**i).
) p) X( `/ D/ b8 L/ P8 j: X2 o4 C - >>> math.ldexp(5,5)3 Y/ n/ q6 s$ t2 E
- 160.0
! q- `- P. p6 ? - >>> math.ldexp(3,5)9 |; h0 \ o; v0 Y+ F/ ?
- 96.0
复制代码
$ y6 U2 C: R/ m3 b3 q1 [! omath.log10(x) 返回x的以10为底的对数
! Z3 E8 n. c( `! Z" E- #返回x的以10为底的对数
+ ?4 N' d4 K: `$ @# P - log10(x)# _5 r; {& Y* a3 ?' m
- Return the base 10 logarithm of x.0 q$ k) x$ r4 z( Y3 K+ I4 t0 m
- >>> math.log10(10)
L5 j$ V! ~+ t L - 1.0
6 A/ k9 S& c! |3 u- q" g; v( F; K7 n - >>> math.log10(100)+ W: S& I$ z9 U* W* Y- L9 C
- 2.0* `3 W8 d, ]; v( t8 Z. u
- #即10的1.3次方的结果为20
, h ~0 }9 E0 X; T8 A1 m - >>> math.log10(20)( w3 _3 e# I O" J/ ?0 \
- 1.3010299956639813
复制代码 ; {$ }& G, R: \9 ~& a. ^
math.log1p(x) 返回x+1的自然对数(基数为e)的值
7 T" _( b5 t. O, Z. F0 b- #返回x+1的自然对数(基数为e)的值
0 @! r, Z7 j2 c" P" ]5 d - log1p(x)0 y- l9 d& t" r! g4 i5 j
- Return the natural logarithm of 1+x (base e).
# n3 ~: n" R% w k1 ^! f% K- v - The result is computed in a way which is accurate for x near zero.
8 @. @, ^& m Q3 D0 L - >>> math.log(10)
0 C( S8 ?% g/ ?0 T% _" @9 p- A - 2.302585092994046
! ?1 f) y- h/ T! s/ M; M- b& o6 s - >>> math.log1p(10)
8 x3 V d! P: f: m1 h - 2.3978952727983707% w' T; c: [4 F! S4 s
- >>> math.log(11)$ c4 v+ }4 L, N/ s7 F- e
- 2.3978952727983707
复制代码
. L- X2 k4 Q) N5 a/ Ymath.log2(x) 返回x的基2对数
$ M0 ^7 l7 S* _& }, z0 I- #返回x的基2对数# p( u. ]$ f* G/ ]1 D1 q: e
- log2(x)
* g$ t: g9 W. J# z$ a$ O$ Z6 } - Return the base 2 logarithm of x.: {1 p, Y4 ]0 D& G, Z' N+ T
- >>> math.log2(32)
~) b$ i; o! o1 R, g2 L0 Z - 5.0
& q$ q% ^- v. s( @4 u2 V3 X - >>> math.log2(20)
9 ^8 Q1 y- I! L5 _+ x - 4.3219280948873639 I1 e% u/ a5 X8 L
- >>> math.log2(16)
2 I' c( X% }9 o( I% g8 u, t+ y - 4.0
复制代码
5 k- @* _/ N# ^, \# A) W9 N$ bmath.modf(x) 返回由x的小数部分和整数部分组成的元组
. m$ e0 d& [/ U g" H# C- #返回由x的小数部分和整数部分组成的元组
# n- b; h8 U& _& o% n5 ^ - modf(x)
& s" z" z3 d: d5 x7 } - Return the fractional and integer parts of x. Both results carry the sign$ ?6 O- ^: f# k7 x# M) b
- of x and are floats.
8 D9 V. R s! k4 B3 ` - >>> math.modf(math.pi)/ h7 W4 p) x) S) I
- (0.14159265358979312, 3.0)
& b+ v4 k) H5 r, b4 |1 r3 a - >>> math.modf(12.34)
- I' j6 ~7 ]1 C* s4 _ - (0.33999999999999986, 12.0)
复制代码
! A3 n2 ~5 L* }4 dmath.sqrt(x) 求x的平方根0 `1 `' W, s. P% R: Z" o k+ T+ M7 K
- #求x的平方根
v# V1 B+ n* _! ~5 H - sqrt(x)6 D- {7 F/ ^: a6 W
- Return the square root of x.% F4 r. g, [ c2 ~3 X
- >>> math.sqrt(100)! G9 u( e1 s6 }; \- B/ n5 }- i
- 10.0) U+ i. X# n! _7 I
- >>> math.sqrt(16)2 F; V3 I' \" f' E) J+ `
- 4.0
7 U" P+ {, `' q. m1 `2 i - >>> math.sqrt(20)5 r& g& R6 e7 q+ n' t0 \( }4 u& D
- 4.47213595499958
复制代码
8 @1 q6 [9 u. I2 `$ E( J% Bmath.trunc(x) 返回x的整数部分- #返回x的整数部分
l/ ^4 B4 u% q - trunc(x:Real) -> Integral7 H/ f2 r+ W" j3 ~% F
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 H2 C' R$ u8 s5 g, c - >>> math.trunc(6.789)3 [3 t0 p( W/ F) y
- 6
4 a6 B8 A# {/ n/ G9 P( u E+ z - >>> math.trunc(math.pi)+ Y' o6 D4 ^5 q
- 3( c: j! v& J: ]: r/ Z
- >>> math.trunc(2.567)6 W: H9 {% L/ q" m1 p( V
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|