马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
) k) T: G- d7 k8 G" x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, s$ }5 {3 P" I9 w
: T( `: M' X; o5 B9 h方法1:
2 `9 ~0 m: _+ a9 m: i- >>> import math6 W. n$ G6 f% E* b/ _& S; v" C, ~
- >>> math.sqrt(9)& Y6 `% C1 q) Y, u! r- w
- 3.0
复制代码 方法2:
: j2 r; o. x4 ]4 o- >>> from math import sqrt0 H0 u- i2 y2 `- [* K/ p
- >>> sqrt(9)
; r L. V+ N# |% f' T" g - 3.0
复制代码 / \6 u0 Q1 V0 H9 {) F
# Z2 y3 i7 L* i2 J& k; N$ p5 qmath.e 表示一个常量
% c. j( t' x2 R8 S- #表示一个常量: H" g" ?6 S X! a4 ?3 P( c
- >>> math.e9 f6 B) S+ G; {2 A
- 2.718281828459045
复制代码
9 B" E! _& g6 V9 e& Pmath.pi 数字常量,圆周率
$ Q* n$ p; |+ [1 ^- #数字常量,圆周率4 K7 R/ }# S5 K$ B6 }5 p/ W) W
- >>> print(math.pi)2 ^2 n/ c2 s; f
- 3.141592653589793
复制代码
3 X5 M7 X) j/ m, |5 k1 \# V5 Xmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x4 u% j ?$ K. g! E0 z3 f; Q& b$ t( \, T
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x0 G1 | q$ h3 G1 X. t
- ceil(x)
, R1 }, H* Y( s - Return the ceiling of x as an int.
1 J R$ R# r3 ^ - This is the smallest integral value >= x.1 ?" j9 ?+ f% ~
4 W, a+ q8 Z: j7 h7 _; O- >>> math.ceil(4.01)+ Q- C4 o, [" f# d
- 5
8 r! w, `2 V ?4 F6 s( ] - >>> math.ceil(4.99)
: O2 o3 v% L1 l2 Y% B - 52 O9 U3 A0 y% E1 s) e
- >>> math.ceil(-3.99)
- _% N4 { o# U6 N8 G0 g - -3# M2 x0 h3 C2 Q5 J0 W+ n2 T
- >>> math.ceil(-3.01)
! W% \7 W9 G9 p/ A, }& r - -3
复制代码
0 [( ]4 U( D' |* }5 Z H& Cmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ x3 d8 L! G" a& X2 G- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, M/ z7 w$ r, y( J. o7 U$ A" f - floor(x)
4 b l4 O0 s' ^1 F7 |2 c - Return the floor of x as an int.
5 e6 }/ O) q) s# L- B8 z0 h - This is the largest integral value <= x.- H/ c' B9 `8 R9 K. I7 x/ n
- >>> math.floor(4.1)) ~5 V% |8 a$ u) Z
- 4. Z% y3 g! P8 [/ S) ^# u! A9 V
- >>> math.floor(4.999)) Q% u4 e) E5 n/ X0 s' R/ I5 L) @# _
- 4) @/ l5 ~, I6 q0 y8 Q: ?5 U
- >>> math.floor(-4.999)4 x2 F, Z( F0 o9 c- `" L
- -5
k% z. h* c: r) H - >>> math.floor(-4.01)
# v1 R6 B# k; m1 o, P - -5
复制代码
4 M8 R2 d5 y e6 w6 cmath.pow(x,y) 返回x的y次方,即x**y
" }3 Y# a4 G4 V1 {& v. x- #返回x的y次方,即x**y# k6 R @% b3 A4 Z% Y
- pow(x, y)
, M" y, M% Q4 k A. a& _ - Return x**y (x to the power of y).$ }5 G! _: z; E2 l( f! x
- >>> math.pow(3,4). z/ e+ k2 H/ P# b; E. m( v( U
- 81.0& O- K& q; I& {) a- e
- >>>
; \9 A0 v$ U' r5 [: B - >>> math.pow(2,7)# _) w$ t4 l1 F/ h
- 128.0
复制代码 Z: A2 `: ]* N/ d5 c7 N0 f+ r
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 C( f, S- `2 d6 e* j
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( c! Y9 @4 b1 }
- log(x[, base]) O/ e2 c- |3 s$ b" ~1 S
- Return the logarithm of x to the given base.: x! M! `. }5 K
- If the base not specified, returns the natural logarithm (base e) of x.
9 }. H$ E& c* p5 }0 V - >>> math.log(10)
9 a c8 j) W0 |' w - 2.302585092994046% ~- Z: p) j [) ?
- >>> math.log(11)
' Q/ L3 h/ }1 s' D2 P `( p; i - 2.3978952727983707' G) D3 B* ]) |+ a4 K2 g
- >>> math.log(20), t5 c {! h' g
- 2.995732273553991
复制代码 . `. `( U% u0 I0 A9 r. h
math.sin(x) 求x(x为弧度)的正弦值
) `2 D* K9 @# a. {- #求x(x为弧度)的正弦值/ S1 [2 e. f3 O [5 \: ]
- sin(x)7 [! B! K0 Z* @3 \4 K8 J& J
- Return the sine of x (measured in radians).2 W6 G% |& V* _ z, w! ?5 o, d
- >>> math.sin(math.pi/4)- j0 t/ c% B# w3 \4 M% O
- 0.7071067811865475
4 u& S7 P+ F2 S+ n; u ^+ N - >>> math.sin(math.pi/2)$ J: |0 j `1 `1 B$ p. X& ?
- 1.0
0 V1 `( j: u# ~9 @" U4 J" q - >>> math.sin(math.pi/3)0 B9 ^4 M5 b4 b' x
- 0.8660254037844386
复制代码
1 T: e" E: K+ U) W- ~7 W5 U& Imath.cos(x) 求x的余弦,x必须是弧度) \1 x9 Q# U$ F* Y
- #求x的余弦,x必须是弧度6 y D$ r; P$ v1 o0 u
- cos(x)
1 G( X2 q b* o# i - Return the cosine of x (measured in radians).- i4 R2 O. }, i% z, g6 U. ^! t) E
- #math.pi/4表示弧度,转换成角度为45度% n" n# T: _! \3 U$ p* s6 Y8 S
- >>> math.cos(math.pi/4)* M, F+ W: C/ Z2 E A
- 0.7071067811865476
8 |/ n1 G0 N& S! c1 x6 ~% t - math.pi/3表示弧度,转换成角度为60度; B$ }& D! ~1 l) k' ]( j" {
- >>> math.cos(math.pi/3)
& N- @' Y. w1 l& j' C - 0.5000000000000001- S* @" U# _: E8 X, Y
- math.pi/6表示弧度,转换成角度为30度
3 { S: `) _" r- x, Q1 u6 \/ H - >>> math.cos(math.pi/6)6 l* |/ ], r9 p2 Q L6 T
- 0.8660254037844387
复制代码 5 g# Y8 ?: g, L- J" j: R9 {1 J
math.tan(x) 返回x(x为弧度)的正切值) S, I# E+ ~* G" v
- #返回x(x为弧度)的正切值
; I# ~0 A2 I! [; `) R1 G( f - tan(x)& j5 \" x1 M1 D8 _ z: @7 F
- Return the tangent of x (measured in radians).8 F7 A6 x0 x( f$ z1 y- J
- >>> math.tan(math.pi/4)/ ^% w- O# s( I; |! t: X9 j9 u
- 0.9999999999999999
' x/ |7 U! K) A! s - >>> math.tan(math.pi/6)
3 I1 u1 ]' j0 k+ N! b - 0.5773502691896257
$ a" F" Y5 v7 `, K9 `& I# B! ~( C - >>> math.tan(math.pi/3)
$ x1 K+ N2 c! x; } - 1.7320508075688767
复制代码
8 l. m. o- k! P) H0 S) |math.degrees(x) 把x从弧度转换成角度2 I5 i2 M7 @8 M* [) S
- #把x从弧度转换成角度, S2 r% O$ r1 u# F
- degrees(x)
6 e( W( ~7 J Z% ^4 n' q - Convert angle x from radians to degrees.
9 x9 M& ?, p; S1 ~. q$ } - , q ~' J7 ^* u% Q( d$ I7 B. r
- >>> math.degrees(math.pi/4)
3 \8 U/ Z# \2 e6 ~( G2 [5 c - 45.0
8 Q6 }3 U' L4 [ W' B; e+ l- s9 D - >>> math.degrees(math.pi)6 l! U0 _ Q2 C+ H3 E
- 180.04 o( ~5 i9 Q3 m0 y
- >>> math.degrees(math.pi/6)! u4 t9 D% C/ y( u8 a8 r
- 29.9999999999999964 H9 D7 E6 l: Z6 q0 S, X# W
- >>> math.degrees(math.pi/3)
, y8 P; X/ t0 a+ l- M. u% V" J4 v - 59.99999999999999
复制代码 5 q* k% U. t& g3 `, _' S5 _& j! K5 z! V T
math.radians(x) 把角度x转换成弧度
9 Q: b5 q9 u0 B* m- #把角度x转换成弧度
% A5 y( o2 f- n" p4 g - radians(x)8 A$ @5 @! B5 w4 z$ A
- Convert angle x from degrees to radians.7 O# m, H4 g9 ?4 _' k2 G: t5 B
- >>> math.radians(45)
: E5 A" y# {- n- N. K - 0.7853981633974483
2 L/ Q. I: R: c3 H - >>> math.radians(60)% O/ J( Y4 k/ `: e3 a# l" L
- 1.0471975511965976
复制代码
! M2 b" s: i1 h: L/ _math.copysign(x,y) 把y的正负号加到x前面,可以使用0
, w$ I2 C) S# u8 }, N, l# o$ q- #把y的正负号加到x前面,可以使用04 S' \+ Z) ?3 `" t! v
- copysign(x, y)1 ]6 s Q* ?; x
- Return a float with the magnitude (absolute value) of x but the sign
+ ?9 z- r; E3 N6 n0 C6 E% d$ o - of y. On platforms that support signed zeros, copysign(1.0, -0.0) , K* `* \# `8 r# s7 U% H# x k. t" d
- returns -1.0.
: E5 j+ t( x" `% E
1 W& d1 I( b# Z/ s! L) Q- >>> math.copysign(2,3)+ W& Q; ~9 I3 F# m- C4 B7 B K6 U9 D
- 2.0; J$ g6 b# c: B' X* i. R% k- \
- >>> math.copysign(2,-3)
/ o6 D: [9 \7 c( C/ a, j3 v - -2.0* ?& v* _ r- Z# c
- >>> math.copysign(3,8)+ W% k& j% z) r
- 3.0
) t$ {% s: x1 g6 T - >>> math.copysign(3,-8)
8 e8 v( i. ` c8 p% ` - -3.0
复制代码
" q, _) c% _: s; U4 @math.exp(x) 返回math.e,也就是2.71828的x次方
- a% |* q7 d3 h; } W6 K8 c- #返回math.e,也就是2.71828的x次方0 `( p8 h$ u( R& Z# |
- exp(x)
1 N- X9 \# P; Z6 ?0 } - Return e raised to the power of x.# i4 Q3 ?& c- Y. Q
# _% Y: d4 T! ?! R- >>> math.exp(1), u# [( r+ g8 J! e! [1 z) v
- 2.718281828459045; j- c" O5 E* T9 \+ e+ U: e
- >>> math.exp(2)
5 b2 g2 p) M' g8 L; K2 {7 c - 7.38905609893065
9 h* b9 z) S$ T3 H+ k5 Q - >>> math.exp(3)
; \3 ?2 n1 V9 @4 }4 T8 X) B; i( \6 O - 20.085536923187668
复制代码 5 V, Y5 |' t) k% O
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
1 ^& f3 ~' k1 F3 i, h7 }' `* M- #返回math.e的x(其值为2.71828)次方的值减1( T& {/ ~: Q" _9 Z w: y: v
- expm1(x). o% B+ W2 i0 h% L' j
- Return exp(x)-1.
) C4 f5 D7 u% u _2 Y - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 s- F/ H) \- w( z9 @' C5 c5 ]
) W3 A1 S$ C, z2 ]& r- >>> math.expm1(1)3 f' k9 {- |% F1 B2 r
- 1.718281828459045
% }" a c+ [+ n+ c' M - >>> math.expm1(2)
* {( z T% e% J; ^! ?+ C( B: u - 6.38905609893065! I0 ?- x: u, c6 E( f% M- H/ a
- >>> math.expm1(3), d# j3 s% ]$ y8 J$ }4 x
- 19.085536923187668
复制代码
- ^, a" {( u) B; D3 T# ^' t: ymath.fabs(x) 返回x的绝对值) P& |1 o- j3 f! H: d
- #返回x的绝对值0 r6 H5 O1 X3 |3 u! K$ C
- fabs(x)5 w3 X6 e# a% T# N- ^5 E- x) b; ^, k
- Return the absolute value of the float x.6 V( ~1 g/ K. D) e" p( s
2 E; @# j: }. Q% u$ h- >>> math.fabs(-0.003)! Q' E4 J; @$ L$ P+ Y8 X& c# a
- 0.003
9 w* L7 y( B: n0 @9 k! v! ~ - >>> math.fabs(-110)7 F3 c2 L9 E0 R/ h4 W
- 110.0
) J! k# ?" l2 k5 V! H - >>> math.fabs(100)! h5 l) i& {5 P, [8 [0 N3 C
- 100.0
复制代码 # S" F+ h! i. s
math.factorial(x) 取x的阶乘的值
6 y3 e- f! F( t; ]; m8 J' t- #取x的阶乘的值6 X# J- ]9 O, \
- factorial(x) -> Integral5 }2 T$ k, o4 w+ {2 r! F
- Find x!. Raise a ValueError if x is negative or non-integral.
! j% `; }. g* a/ h - >>> math.factorial(1)
' N/ |5 T/ g) R# M) ^, q8 J - 1
7 Y& @; l3 X8 R8 M7 y - >>> math.factorial(2)5 c9 I, y4 v8 {+ H0 C, k
- 2
% Q5 r4 E3 u% V; U+ I% ?6 q - >>> math.factorial(3)7 b9 t! {% ~4 t) }/ k3 l
- 6
1 q7 e& C5 v4 W6 R6 I - >>> math.factorial(5)
& g% z, n/ k: k, f1 m. Z% w - 120
) J) N) r1 L$ H# _5 \- j/ l6 B - >>> math.factorial(10); z% x( R6 Z; R. {: n+ s4 _) R9 s4 `
- 3628800
复制代码 ! i' |( P+ f8 j3 j
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 N7 K6 v9 j# h1 T: d! y
- #得到x/y的余数,其值是一个浮点数
u+ L3 g7 {! E5 h# v - fmod(x, y)/ d2 Z4 {: T+ q: G, Q
- Return fmod(x, y), according to platform C. x % y may differ., ]' Q7 T" d1 S1 q( w4 V4 @, N) z
- >>> math.fmod(20,3)2 }" a- {: e2 w7 _9 }9 z
- 2.0( o3 @. E. `- V1 P2 _& t1 `
- >>> math.fmod(20,7)
( P5 B( p% I! V7 G! Y+ S - 6.0
复制代码
- y$ P; T J4 M& A% Zmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ |4 {' @. ^" J- K- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
; k6 \) K: ?% |& M4 `. n* f - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. D8 a9 m4 |' [9 d
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
2 T! s+ _8 u9 V - frexp(x)- v l5 A- E& l. g9 [
- Return the mantissa and exponent of x, as pair (m, e).$ n8 F6 Z K k6 Y. h
- m is a float and e is an int, such that x = m * 2.**e.! L0 P u# ^# m, p
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0./ a% I. W; g/ G. }. a
- >>> math.frexp(10)
& r8 h3 M9 r1 _' X' f" { - (0.625, 4)8 Q. p$ e+ i& }2 F" |
- >>> math.frexp(75)
/ ]' V3 X3 G$ K) s1 {2 J: Z - (0.5859375, 7)4 Q8 O+ \ N+ ^ x; u0 T
- >>> math.frexp(-40)7 N3 P% F P0 K7 S9 R" I$ A( |
- (-0.625, 6)
" D4 b H0 W! J, @ j& h e - >>> math.frexp(-100)1 q: p' `1 L6 h+ y5 S" K
- (-0.78125, 7)% |7 b# I" [: p# n' ~- B
- >>> math.frexp(100)% a5 s9 \/ E0 z5 ^- B$ o/ Y6 s# ]$ Y
- (0.78125, 7)
复制代码
3 g8 E9 l" V, O/ }* u/ rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
, b4 |: X5 P/ m9 W- #对迭代器里的每个元素进行求和操作
9 I$ s4 ?6 a, L3 G9 W - fsum(iterable)
) J- q- [: a w8 \- V - Return an accurate floating point sum of values in the iterable.) x7 ]8 `1 c# X1 t) h
- Assumes IEEE-754 floating point arithmetic.- Z, Q6 @: ~0 O" z: v! W# C
- >>> math.fsum([1,2,3,4])
3 [. a9 Z5 E8 D+ q - 10.08 Y# Z: n9 R* T, S8 Z7 @4 Y& e
- >>> math.fsum((1,2,3,4)) k" o X- M4 C% M0 S
- 10.0' X0 v* e! D. e% O
- >>> math.fsum((-1,-2,-3,-4))
# @0 |3 w1 {1 v2 ]( s* t - -10.0
9 r8 l& N3 X; Y f - >>> math.fsum([-1,-2,-3,-4])
: n6 a2 s1 a- n$ V* |8 Y - -10.0
复制代码
5 f8 n$ \- r: C1 V4 U; Zmath.gcd(x,y) 返回x和y的最大公约数4 {+ [$ o( l; X$ p' S1 L
- #返回x和y的最大公约数- h5 Z- F6 E9 _& h3 f' u* i$ Y
- gcd(x, y) -> int/ ^% |0 F" \2 p+ r
- greatest common divisor of x and y
; f+ [* j7 l1 [( c: A# f4 O - >>> math.gcd(8,6) `' A1 u. @: H7 R; m8 D3 }) m* I
- 2
0 v2 d5 `1 A C- |6 z - >>> math.gcd(40,20)! p, {$ v7 n1 U* U0 W o$ d
- 20/ j. F- h; K0 e7 L9 w n9 ^. H+ w2 B% O
- >>> math.gcd(8,12)+ |+ ^0 G# Y2 H0 Q
- 4
复制代码
7 v/ S6 ~0 V/ N% Bmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
1 h) A# D( g& Y! ?- #得到(x**2+y**2),平方的值
7 {, ^( Y; R0 u3 Q7 d - hypot(x, y)
+ b& W7 L* _( p$ d, D+ U - Return the Euclidean distance, sqrt(x*x + y*y).
2 R0 ?; T, t7 N2 r8 `5 V - >>> math.hypot(3,4)- R' Q; z p' T, L+ [7 l
- 5.0
[' B2 V9 l; ^) V - >>> math.hypot(6,8)
$ A# P8 g+ @! o& I - 10.0
复制代码
9 _$ D* c& m% \1 A# k1 h$ xmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False! H6 S& p! K) _1 P
- #如果x是不是无穷大的数字,则返回True,否则返回False+ Y, m) c( o7 z4 [$ S
- isfinite(x) -> bool
: D) t+ y; @1 H2 ` - Return True if x is neither an infinity nor a NaN, and False otherwise.
1 A: @! V: }! c5 b" N; M; X8 p - >>> math.isfinite(100)
" C' m1 v. ?3 D: P* t/ M$ h - True
5 D2 [$ N/ D- r y+ `: ] - >>> math.isfinite(0)
& j* z0 j* ?; x - True! D7 f A# G% v+ N
- >>> math.isfinite(0.1)
7 r8 `) t; o. U f% v - True4 w' ~7 O& w; q; A% c0 j
- >>> math.isfinite("a")
# J" G! a* @' i0 S$ {0 z - >>> math.isfinite(0.0001)
8 |6 E- U/ Y1 `4 }4 Z6 M/ E- w: G - True
复制代码
2 |( o0 `: y T- ]. v2 @8 {math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False) Z* n: d" ^6 w1 y9 u
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
, W& b2 {" o! ?. x; ]8 C/ a/ c - isinf(x) -> bool
6 p7 m4 a: o2 O- l - Return True if x is a positive or negative infinity, and False otherwise.
. y3 W2 y$ D% b2 m$ I- { - >>> math.isinf(234)
+ a: K* m3 d$ f2 f" Z4 c - False3 ]4 e* U6 q( q1 n
- >>> math.isinf(0.1)
. [# U. y2 }" w6 p3 z - False
复制代码 7 `' c L5 ^4 Y n* @. s! c% y3 |# Z. {
math.isnan(x) 如果x不是数字True,否则返回False
3 u$ p! N1 ]+ |/ `0 r+ C- #如果x不是数字True,否则返回False7 O4 y! V) H" f
- isnan(x) -> bool
( X% X$ }! Q+ A4 e: u) G4 E& m# G) |; O - Return True if x is a NaN (not a number), and False otherwise.
+ b4 Y5 c+ x5 \8 x& v- o4 C& k, h - >>> math.isnan(23)1 E( r- |" @4 E- x3 C
- False* e' c# p! q$ |0 M
- >>> math.isnan(0.01)
$ o6 X" m; w5 I* w+ l5 e Q - False
复制代码
, O7 i E4 @- ^( {, D. k: y ?; _math.ldexp(x,i) 返回x*(2**i)的值/ z* B. B' F6 E" G1 p
- #返回x*(2**i)的值
! Y2 B) P; x1 v) _ - ldexp(x, i)
- `" e5 e6 D2 h; P0 Q2 x - Return x * (2**i).+ N/ a" _4 V1 R i" k
- >>> math.ldexp(5,5)" Q) g$ y1 G) t' Y
- 160.0
7 f5 n2 Z# f5 |; g' ` b - >>> math.ldexp(3,5)' y' S8 w. ~# {, T: u) d
- 96.0
复制代码
L* E: @/ t. e. G0 `' [" `) }math.log10(x) 返回x的以10为底的对数. l. O+ d }% \) W
- #返回x的以10为底的对数0 B; L5 g7 n" ^
- log10(x)
; M" `1 q% I' D4 b3 Z - Return the base 10 logarithm of x.
3 h2 c2 e/ d8 Y! ^ - >>> math.log10(10)
' } T. a8 ? M2 b - 1.0% |. w6 L1 d# m- X9 B- ^
- >>> math.log10(100)
4 m7 E- h5 K5 \, j i* a% A - 2.0
& M* y/ o7 d4 ]3 [ - #即10的1.3次方的结果为20
* ~9 d- Y( ]+ i- R1 V' P4 w! q - >>> math.log10(20)2 v, P( t$ P ]4 l
- 1.3010299956639813
复制代码 , M9 E# \6 n+ m f
math.log1p(x) 返回x+1的自然对数(基数为e)的值+ I" M) Y* I( `0 n0 u D, y
- #返回x+1的自然对数(基数为e)的值
! v* p3 L4 _4 G. B - log1p(x)# r' f* D% H9 z8 f- M) `$ U$ a: h; ]# b
- Return the natural logarithm of 1+x (base e).
+ [3 m* W; ?2 T9 F: n. g - The result is computed in a way which is accurate for x near zero.% U- X) J) k+ g s m# R! x
- >>> math.log(10)
6 O/ v+ o" l \; _ - 2.3025850929940464 t+ q* N& M5 z! ~$ T& H
- >>> math.log1p(10)
/ ?- p+ k) w& O' c - 2.39789527279837077 ~: R# y4 O f6 K0 q
- >>> math.log(11)
' T# V" ]0 A$ ^( K& g1 A& G/ g( b$ ] - 2.3978952727983707
复制代码 & a+ d5 V& M$ T0 g z, l* o+ ]6 Y
math.log2(x) 返回x的基2对数! d4 D6 r, m, u U# u- ^9 C
- #返回x的基2对数
2 H0 H6 K7 U! u4 D0 H1 s - log2(x)
* \4 g" S; P, K: q2 |3 h; j( ~6 z - Return the base 2 logarithm of x.! y. k$ o: z( c) S' u1 S
- >>> math.log2(32)
: `+ f1 u+ r9 o" p* |# x - 5.06 E0 B& h& |5 c" G# X
- >>> math.log2(20)
* @ i6 \: N- y1 a - 4.321928094887363 v" ~9 a' N( m# X4 G6 p
- >>> math.log2(16)7 Z5 _( Q$ N4 M
- 4.0
复制代码
' ?( v- s2 p7 o" f2 o& Smath.modf(x) 返回由x的小数部分和整数部分组成的元组
$ w9 w: ^; B8 R" m% P3 m$ @5 l- #返回由x的小数部分和整数部分组成的元组
: \$ B# }* x! f' c. t Y+ _ - modf(x)
% h! N: y- J1 ?8 C; ]$ C* w - Return the fractional and integer parts of x. Both results carry the sign! i. L$ R+ }5 ]- p, y' K
- of x and are floats.# ]# Y' X P" g/ z4 i
- >>> math.modf(math.pi)
! L, o" F1 q9 q9 c) u - (0.14159265358979312, 3.0)% ^- G" z0 H' O0 @
- >>> math.modf(12.34)
+ U# [" a W4 q& o$ l! b) T - (0.33999999999999986, 12.0)
复制代码 3 \) w8 r/ |+ j$ U4 {0 G
math.sqrt(x) 求x的平方根
4 K/ W0 H+ [+ e5 t6 V# C& @7 {- #求x的平方根
9 {" b! D! P$ h- e4 ^ U' t - sqrt(x)
4 J; J# Z% y: {9 S' e1 B - Return the square root of x.! V9 ?: X+ a1 T3 Z( ?: y4 A
- >>> math.sqrt(100)/ y" J/ S* ?6 _. C. ~8 M. y# v7 \
- 10.0
' P' U1 C/ w! s" x5 h% t+ I& j0 ~ - >>> math.sqrt(16)) E! ]6 M; s2 }4 ]3 C$ J! v. l
- 4.0
2 N% S( o* k# [' K$ `, T - >>> math.sqrt(20)
9 w* @0 }6 ^2 ~: X) D( e - 4.47213595499958
复制代码 . a; Y m0 [8 l" M, w
math.trunc(x) 返回x的整数部分- #返回x的整数部分
p1 F1 I3 y# l4 e3 f - trunc(x:Real) -> Integral
* |( z. D" k1 K$ [" h! D - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
) x6 m! }$ \+ K0 M$ }: ?2 f - >>> math.trunc(6.789)$ q. m8 a. W3 J8 ~
- 6
0 {* o+ G# A2 n# E( W# L - >>> math.trunc(math.pi)
1 D: R8 K4 e2 s - 3
6 `- S0 K, W, y6 e2 ~; u# Q - >>> math.trunc(2.567)% [! T6 n9 ]% f/ J5 V5 |0 u
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|