马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
* N# J5 p8 d z8 B' N/ L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
5 i0 H( e0 _) ?
# o E# x) ] g5 ~! B方法1:8 F$ s( ?, @- i; f* H
- >>> import math
/ K4 V" o5 L# E - >>> math.sqrt(9)
3 G' z0 F: s% q - 3.0
复制代码 方法2:
9 @$ T" b: F% O0 C3 o% `# I* i- y- >>> from math import sqrt
. J* P7 T+ Y; U. L2 | - >>> sqrt(9); F6 W6 @, n' B) R4 w
- 3.0
复制代码 - e6 V7 b' I9 o' t2 p% Q" ~. f
( ], V( u4 l7 f1 e3 X6 R% ~3 y
math.e 表示一个常量0 c4 ~$ t- @0 I3 p" e
- #表示一个常量
; ?8 N/ j. K* {4 f - >>> math.e2 N# j. `6 z D
- 2.718281828459045
复制代码 0 M2 J7 W4 n5 G& r$ W
math.pi 数字常量,圆周率
2 r1 H2 U, R4 [/ N- #数字常量,圆周率# A; |+ S- k1 l2 y+ T# H: a" r
- >>> print(math.pi)2 x8 J9 u! K7 Z6 e
- 3.141592653589793
复制代码 & }7 u3 |3 f$ T
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x# `3 I' k9 J/ }1 Q; K1 Z
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x, E! X! D+ \- H7 ^# w
- ceil(x), X. m g/ `2 x) P) @& I6 t
- Return the ceiling of x as an int.
: @. z6 K! F# H - This is the smallest integral value >= x.2 s7 K. o0 I/ `$ w1 W# y, u- q( l
7 e4 V, {3 \2 ^' |- >>> math.ceil(4.01)- n7 m8 M) P2 s. a7 r/ x6 g
- 5
* Z4 I; _3 u6 I& l - >>> math.ceil(4.99)
' o/ s: Z% N& u - 51 q; S" u3 E" u6 R- E% l9 `
- >>> math.ceil(-3.99)$ J2 f' b* Y, z$ l! Q
- -3
/ e* ~1 Z4 B8 f4 x4 f - >>> math.ceil(-3.01)4 v3 t* `% i$ ^- H( _
- -3
复制代码 - B. c$ D, O/ i0 w% T. n
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ ]9 g+ d/ e# Z6 v. k' p* `, R( O# @
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 V5 p; Q$ |0 A) u- U& \$ }9 x
- floor(x), [) N% |- t0 Y2 X
- Return the floor of x as an int.
$ t7 b+ T" E5 Z# J: _ - This is the largest integral value <= x.! b/ |: \; J3 \* }+ x5 ]
- >>> math.floor(4.1)' s* r+ k' Y& B; }% M6 Q6 |
- 4+ E: f7 W3 L8 \( O$ w
- >>> math.floor(4.999)( r( x* t1 N Z% b( v" J
- 46 B; q* M! U5 t1 q
- >>> math.floor(-4.999)6 F4 @& z( d# S& H3 ^4 v
- -5
. x8 m3 Z( b/ Y' _0 W# I - >>> math.floor(-4.01)+ j, O4 ^% g4 u1 l+ o
- -5
复制代码 3 i( v! T2 Q( H( k$ d- Z' ?
math.pow(x,y) 返回x的y次方,即x**y: O2 s7 r# m$ i6 K2 Q1 v! J
- #返回x的y次方,即x**y
$ z6 {/ T! c$ T/ a& d) y$ w1 d1 R, [$ I - pow(x, y)
c- v3 V1 I% w1 R8 ` - Return x**y (x to the power of y).5 ~! O r4 @- C. F$ K
- >>> math.pow(3,4)
% i4 I! S6 Y( f+ S - 81.0
4 k% K( Q% u1 T( _/ T - >>>
% H' q- ~' @; o% n$ f) u# \7 n - >>> math.pow(2,7)+ `. t* w k# b8 B: G- G' [9 Q3 [
- 128.0
复制代码 b6 g& i6 F1 v6 b) M
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 g8 o, w* l, D% r, u- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ I" K2 |9 F$ h& Q - log(x[, base])5 i/ O3 I) j' }$ k
- Return the logarithm of x to the given base.
* f# }& G/ l+ W' t# f# b% c, \) X0 Y6 N - If the base not specified, returns the natural logarithm (base e) of x.$ V, x! O, [, @
- >>> math.log(10)- i5 X, @0 d( K& d
- 2.3025850929940460 s) I( A( d9 \' g. Q1 Q
- >>> math.log(11)
3 x6 C+ e# S, m0 s- K - 2.3978952727983707
2 \, y1 Y; m% G - >>> math.log(20)4 s: L2 l0 [4 A$ Y6 Z
- 2.995732273553991
复制代码 ! u# s' u# ?0 M4 T$ b
math.sin(x) 求x(x为弧度)的正弦值2 |5 z8 ^9 W% C, w v4 c+ U% r9 }
- #求x(x为弧度)的正弦值
! ]3 z1 V0 u% G' Z0 @ - sin(x)
+ t2 z/ x: O2 t4 I! f$ p4 ]* t - Return the sine of x (measured in radians).
/ _* Q0 P5 p+ Z0 e; i. ~: s- x4 K - >>> math.sin(math.pi/4)
2 \; \9 s4 u1 U6 U - 0.7071067811865475
- E( u& Q5 b L7 G - >>> math.sin(math.pi/2)
6 S+ a: r- m2 G+ h/ G( n1 o2 r7 h - 1.01 u. T2 r( h# i V1 c. f
- >>> math.sin(math.pi/3) W2 S! k! U' G* {$ J" U; U" t
- 0.8660254037844386
复制代码
3 ^! J! ^! r, W- |) D5 Vmath.cos(x) 求x的余弦,x必须是弧度
9 B' o. e8 F7 s. K* K6 a1 q- #求x的余弦,x必须是弧度
- T2 @4 e! z5 _ - cos(x)
! ]0 p0 h# B+ v B - Return the cosine of x (measured in radians). }3 C/ v0 O4 d1 K2 |! z
- #math.pi/4表示弧度,转换成角度为45度
5 M) A' C* O* u7 N+ m6 B c2 _ - >>> math.cos(math.pi/4)1 D# v8 D5 M9 M/ w5 o4 Y
- 0.70710678118654765 y- B( O$ D$ Q' D5 H& p+ X
- math.pi/3表示弧度,转换成角度为60度( ]2 h7 ~+ S/ @" M; b7 e, p
- >>> math.cos(math.pi/3)
+ q R3 b: ^; I - 0.50000000000000013 H& G1 O# s, R, w5 B+ ]0 e) B
- math.pi/6表示弧度,转换成角度为30度
/ l a, ~: D$ v- _8 @ - >>> math.cos(math.pi/6)" Y: {% d3 D$ p$ N& c, u
- 0.8660254037844387
复制代码
! D2 D) q) {) {math.tan(x) 返回x(x为弧度)的正切值
! @& b5 \& T/ }4 w- #返回x(x为弧度)的正切值
, `/ t0 u! ]- A* f0 G& ~4 Q - tan(x)1 ?8 C; _6 @% A& b
- Return the tangent of x (measured in radians).
y: y5 } q- b$ o( ^/ D6 m8 Z - >>> math.tan(math.pi/4)% j% k. B1 T d8 R+ P
- 0.9999999999999999 ^8 M6 k3 a6 V& c* ^3 u$ E5 L
- >>> math.tan(math.pi/6)
+ `4 Y2 I5 K8 K6 p$ `7 o# @ - 0.5773502691896257
9 ]( p, U' Z6 Y1 @ - >>> math.tan(math.pi/3)0 }5 [7 D/ ]) W
- 1.7320508075688767
复制代码
( A7 n' u6 `0 B$ g8 K ymath.degrees(x) 把x从弧度转换成角度8 x, g+ C, b# O2 n4 Z3 v% z' M0 R8 b
- #把x从弧度转换成角度
; t* q3 ~; Q0 Q) a9 J, z - degrees(x)
0 q$ F7 n/ ^- N5 j2 B - Convert angle x from radians to degrees.. X: h0 C' b7 E8 o. b% Q7 Q$ |
- $ N$ g9 o0 Y4 h. s+ x: G
- >>> math.degrees(math.pi/4)' M p. C; i/ ~3 P8 p
- 45.0
6 q9 P1 X- `. @: o2 Q - >>> math.degrees(math.pi)
9 h Y& k$ E6 Y# `1 ]0 u - 180.0/ j; y4 v1 X/ t
- >>> math.degrees(math.pi/6)1 W! t8 b$ [5 r1 V
- 29.999999999999996, n/ U' `( z, j+ Q% m N
- >>> math.degrees(math.pi/3)
$ W, r8 P* |8 t, X0 V- l5 t, h - 59.99999999999999
复制代码
$ d" D+ G5 m/ ]. h( Imath.radians(x) 把角度x转换成弧度
4 _8 a- p$ m; v/ H; L# C- #把角度x转换成弧度
8 ~ r' i+ T+ R0 |: A3 o% D2 L/ t - radians(x)
8 i# Q- X, S8 Z4 w# c% F - Convert angle x from degrees to radians.2 n6 B/ k% [; q" U$ d6 b$ J: u# r
- >>> math.radians(45)4 \5 T5 g- g. A9 |$ v' [$ L
- 0.7853981633974483
/ M+ @* B+ K( t& r* G8 f - >>> math.radians(60)
4 ?4 n& ~/ B: s - 1.0471975511965976
复制代码 * c0 M+ C3 h2 d1 w- P
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
4 R. A" R; M$ ^% w8 a0 F- #把y的正负号加到x前面,可以使用0
- e/ n6 z* J" o# @4 L' ^: G, F - copysign(x, y)
0 c B3 T @4 z4 p2 m3 S6 a - Return a float with the magnitude (absolute value) of x but the sign
9 V% a2 b% ]3 Y) Z8 E! y - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, q% e) O2 _+ s) H+ y - returns -1.0.
1 A4 Z' h' l6 k' S4 ` - 8 d; q3 v1 }( f' y9 s$ B& |
- >>> math.copysign(2,3)
/ e5 f$ A/ b8 H$ C" ` - 2.04 k3 x& V( e9 X% R; G
- >>> math.copysign(2,-3)2 m- U* `# v/ R" Q
- -2.0
; P: t, j) P7 U% K - >>> math.copysign(3,8)- `3 W: c* j8 Z! a
- 3.0( i* `' d+ _" H3 T. }6 \& Q+ y
- >>> math.copysign(3,-8)
! f# o0 Q; Z5 Z7 E - -3.0
复制代码
" f1 d0 ^7 Y, ~5 u8 w/ \: _1 g9 cmath.exp(x) 返回math.e,也就是2.71828的x次方 [% D4 k& p3 u
- #返回math.e,也就是2.71828的x次方
6 Q4 n5 a+ W( x - exp(x)' t/ e9 }. {! s
- Return e raised to the power of x.: R) }" x9 l* d
( e; Z1 \1 ?8 p- I" N7 g- >>> math.exp(1)
7 M6 \% M; q9 N1 \1 [ - 2.7182818284590450 m) F9 u, \2 H3 D4 G& ?9 A4 k
- >>> math.exp(2)6 n9 L+ H9 M [2 B; y' Y d) ]: S
- 7.38905609893065; H% S" ]' F! v R
- >>> math.exp(3)
' e- O' g3 j+ f- ~2 A+ H% x, u - 20.085536923187668
复制代码
# }' l* x5 w5 r9 i+ g0 J+ u# wmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1" t/ }. I8 u- @3 o) T6 D" R
- #返回math.e的x(其值为2.71828)次方的值减1
2 t/ [: n! x7 O$ g- z5 s/ M - expm1(x)
% f; P5 f# {( N( M' i& M8 ? - Return exp(x)-1.% q0 i! l S: c. V/ {
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
* ]/ ]# g! w/ Q' Z& y( q
4 E3 k7 U( K, h& Z9 J- >>> math.expm1(1)
" P8 T* w0 H& |, r - 1.718281828459045
' g- ~) s: b" t! @# V v - >>> math.expm1(2)
4 E0 d8 f9 T) C/ w# p1 ?3 N+ U5 T - 6.389056098930658 f6 j: W) I2 D( Q( d' {
- >>> math.expm1(3)8 z: F; Z2 T l* m- N' ^* J' z! Z
- 19.085536923187668
复制代码 2 o! k8 r$ N$ W+ v$ B
math.fabs(x) 返回x的绝对值0 O6 A. q5 y( l# h. q
- #返回x的绝对值
6 J7 r2 f8 u- [2 s0 m- a - fabs(x)
) k B$ W l i; l( } - Return the absolute value of the float x.' \6 I1 L/ y( m0 O
4 w) `2 t; d: U" w4 U+ a- >>> math.fabs(-0.003)
0 y0 Y* v; d9 E. [& @6 B! {7 y - 0.003
7 `% u1 X0 @- l - >>> math.fabs(-110)$ R( F1 k5 |. c& x3 x X) R% C; J
- 110.0
/ X+ L8 W; ] K p, w - >>> math.fabs(100)4 G8 u! G" e, B3 e* [0 |1 U: f. L
- 100.0
复制代码
( F6 e+ g' A. ]3 W6 M/ Fmath.factorial(x) 取x的阶乘的值% S, i ?- I# k! o) Y
- #取x的阶乘的值1 S# z) l' A3 ^1 ?' m
- factorial(x) -> Integral
8 |! T; \1 F: T8 V; N4 w, O - Find x!. Raise a ValueError if x is negative or non-integral.! n- ~, N2 M7 @. T1 O4 f" {
- >>> math.factorial(1)9 H a6 P ` r" U, i4 m
- 1
# ~% W% H: J A - >>> math.factorial(2)
7 L0 f1 G/ Z; _8 x - 2. O0 b1 N. b- c( I* T" s3 q# X
- >>> math.factorial(3)
( @' n( q. s" n6 A3 b/ I* I1 ~ - 6
3 B( q+ `% |5 e W: i - >>> math.factorial(5)5 m0 C! ^* f" C! w9 Q$ _
- 1206 z7 P/ m# ]4 h6 {% i$ X3 c
- >>> math.factorial(10)( A) K. n7 D k+ t
- 3628800
复制代码 2 r: u0 p: W( i8 _' o. h
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数9 t7 R# S' d; y2 ?
- #得到x/y的余数,其值是一个浮点数
" ^8 l7 I2 y1 A# X: \! O - fmod(x, y)
- N: O! z# W. B3 ^, B6 z - Return fmod(x, y), according to platform C. x % y may differ." q# q1 `! r; E7 r5 T" ^5 a
- >>> math.fmod(20,3), ], k5 n8 z4 i! l! d0 h
- 2.0
5 i7 R$ z. Z7 ?' K - >>> math.fmod(20,7)$ h% i3 g0 m& v* K% b8 ~
- 6.0
复制代码
$ O- d6 u4 j+ I4 @$ z& A4 I U9 Rmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% D& d" N) w; o& \- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
8 T7 C3 I5 N H4 G6 P - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
+ M8 L% ~8 T/ ]1 E* i6 p( r7 M. c - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 r$ I1 o$ m4 D( X
- frexp(x)
8 L" B$ P7 S9 @9 d - Return the mantissa and exponent of x, as pair (m, e).: J3 }* D& F# j* P0 u
- m is a float and e is an int, such that x = m * 2.**e.6 l! f4 g4 R" X$ P2 `' O
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 D* ]% Z. N& C
- >>> math.frexp(10)) _8 x! f2 G, _& y, g
- (0.625, 4), D6 H7 k. m! o |
- >>> math.frexp(75)9 X; M( Q* K( A! c9 [* z1 s* [. v
- (0.5859375, 7)* B# x3 d8 E) T2 k$ a m+ @
- >>> math.frexp(-40)" i w5 [' ~" R3 I
- (-0.625, 6)
: F3 }& h, ` V$ U3 a$ M - >>> math.frexp(-100)3 K6 @& V6 M% F6 I9 `9 l
- (-0.78125, 7). W# h, z/ X r% t/ e/ a! |6 c
- >>> math.frexp(100)
/ A2 _ q8 V/ X7 V: s ~* c - (0.78125, 7)
复制代码
\, u- J+ t. @& F( ?9 w( {math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)* j6 |/ L$ j4 a& f& E
- #对迭代器里的每个元素进行求和操作
8 K5 C6 G1 J O& l* S/ O4 W8 x - fsum(iterable)5 i( g; G5 M5 q2 ?! T: M
- Return an accurate floating point sum of values in the iterable.
}" w9 R# L* A! u - Assumes IEEE-754 floating point arithmetic.
$ d9 E" R+ n# _. s; Q; |" t - >>> math.fsum([1,2,3,4])' ]# a0 S Q# `: j9 q
- 10.0
7 j- g( i* c6 n4 |5 m% |4 v - >>> math.fsum((1,2,3,4))
. w; O) h% h, G) K) ~ - 10.0
0 q/ q" ?: l+ U( P - >>> math.fsum((-1,-2,-3,-4))
* a" k5 D* s% o& B: G2 h' ~9 Q - -10.0
6 i/ }% m) y8 @/ g( h7 F& o - >>> math.fsum([-1,-2,-3,-4])- T& w) x- d9 h2 o) ^9 D; n, B1 Q
- -10.0
复制代码
: g4 R0 o6 G: _0 `, t2 H. L2 [math.gcd(x,y) 返回x和y的最大公约数
4 T# e+ b' Q9 w3 R; e- #返回x和y的最大公约数
$ B1 i2 D, ?: w+ s - gcd(x, y) -> int
$ E# `' U+ B' ~/ L o4 r/ r1 _! m - greatest common divisor of x and y
. u5 _6 k3 h- e( w( {+ Q - >>> math.gcd(8,6)1 g) m4 d% C- M" \$ d, G% A
- 2
: p% c$ ?1 H3 V9 F# y - >>> math.gcd(40,20)
8 \/ T( q$ J8 w1 O R* J7 P) ] - 20* c3 f5 j: A' W4 X' U
- >>> math.gcd(8,12)
$ }5 O, N o: V$ n7 Q - 4
复制代码 # C1 b; V( c/ i* Y- Z
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False9 u) P1 u3 K+ U; t. O! w2 P n8 i7 p
- #得到(x**2+y**2),平方的值
; E2 A6 Q5 P% u' |! K7 b) G - hypot(x, y): ?/ D4 m) m+ _0 @$ U. X$ G1 f( Y
- Return the Euclidean distance, sqrt(x*x + y*y).
* O3 {! w9 L4 T" b - >>> math.hypot(3,4)
9 i( w4 f' x) P; k. S: o - 5.0
$ x- q; ~8 B( g z; H E - >>> math.hypot(6,8)
8 j0 m1 ?2 X# e9 P! q, L# B. T& ] - 10.0
复制代码 ' T) {7 j; h7 Q+ r: ^% p% o
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False# c. x% R. \' M9 C) N2 r3 z0 ?& u
- #如果x是不是无穷大的数字,则返回True,否则返回False
5 r8 W( g$ l6 G7 ]! h7 v9 R+ t - isfinite(x) -> bool
( y: \& b* C% r# a$ @ - Return True if x is neither an infinity nor a NaN, and False otherwise. O6 h0 ?2 n% t$ {, i
- >>> math.isfinite(100)
9 r1 c @- i# |/ \4 R: G - True
- K z& j6 r- s8 w$ v - >>> math.isfinite(0)! K( z, h6 v. g
- True
; o/ Y: h- ~+ g - >>> math.isfinite(0.1)
: f- }: v# ^- } - True; O9 x' Q* ~" Y; X0 R
- >>> math.isfinite("a")
( E; y3 ?# h9 \/ u# ~* W - >>> math.isfinite(0.0001)$ `8 Z5 W) z* q; [5 g
- True
复制代码 ( q1 D: `! _& Z3 v$ u& Q: G& F2 a
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False6 d9 G* R; s9 e1 d# c3 O& a! [
- #如果x是正无穷大或负无穷大,则返回True,否则返回False+ d2 Y. X6 ]) C1 b+ s: b
- isinf(x) -> bool
7 k& W5 B' \$ [4 a+ w) O/ g6 ^ - Return True if x is a positive or negative infinity, and False otherwise.
7 e! b) s. P. u3 X+ [ - >>> math.isinf(234)
, s& \$ g8 `/ A - False
$ V! c' l1 F) `8 T* c; q& Z - >>> math.isinf(0.1) l- A6 C9 z+ I+ A: P+ F M, |4 ~
- False
复制代码
- A* \+ V9 u6 P4 R, fmath.isnan(x) 如果x不是数字True,否则返回False' b* a3 ^3 O/ k" |. T' B
- #如果x不是数字True,否则返回False
8 I: m' m, k' V0 @0 s3 O9 R2 _' u - isnan(x) -> bool
6 O7 G: E F/ r4 B( }& o - Return True if x is a NaN (not a number), and False otherwise.: z1 F( @; {3 D. w/ l& P
- >>> math.isnan(23)
# ?& P. X* G& e0 _% j! f* N - False. e' o, w. Q1 g7 y, t
- >>> math.isnan(0.01)
' T) w9 y/ ~. E8 @ E - False
复制代码
& f+ ]- W% ~8 U' umath.ldexp(x,i) 返回x*(2**i)的值6 ]& i) B4 S3 `5 J$ y
- #返回x*(2**i)的值
( X% w9 p- O8 J" `+ G - ldexp(x, i)% u Y- ~& e% s% R" L* F2 c
- Return x * (2**i).
' h) b3 ], }& k$ L% M - >>> math.ldexp(5,5)8 K" m! C# `( M# O6 F
- 160.08 a0 u: r. M( o
- >>> math.ldexp(3,5)
/ ]- i1 J) P% E, E) Z5 Q" Z% o5 Y - 96.0
复制代码 . _2 B( w+ C& _
math.log10(x) 返回x的以10为底的对数6 d5 r0 A o0 j+ x/ Z: Q
- #返回x的以10为底的对数' B& k( j7 i0 o
- log10(x)
9 A! P; u) m b - Return the base 10 logarithm of x.! [; C1 y8 J J" L2 v+ c
- >>> math.log10(10)6 h% F) a+ r: [8 b
- 1.0) N6 S( [8 x. p, u5 z
- >>> math.log10(100)2 E6 n W" R* b- j) v, |" w
- 2.0) _5 O6 v$ Q0 J& d
- #即10的1.3次方的结果为20
1 S2 R' x# I3 m, F: o/ D - >>> math.log10(20)
' l" P4 R' K/ E* @( R, h - 1.3010299956639813
复制代码
% H2 s. v8 D/ \7 d8 X, qmath.log1p(x) 返回x+1的自然对数(基数为e)的值& Y, B( G, ^2 M! U) ]7 H) z: s
- #返回x+1的自然对数(基数为e)的值6 _; Y0 b# a" ~ i
- log1p(x)
2 [( `9 [- f: ]1 }7 V - Return the natural logarithm of 1+x (base e)., s$ C- y. H$ r
- The result is computed in a way which is accurate for x near zero.
8 B" E- k7 m3 }5 [ - >>> math.log(10)
: X/ _+ m/ K% D1 M8 ^0 ] - 2.3025850929940468 X. v* T8 z0 q* E& [: m
- >>> math.log1p(10)5 d0 u t+ m& X3 v# w
- 2.3978952727983707
8 q7 ^# U2 }$ z, ]& _ n/ b - >>> math.log(11)) h1 | g! y, s# d" @! R
- 2.3978952727983707
复制代码 . K: z% \; Q, U/ F D }! F
math.log2(x) 返回x的基2对数
! b4 i& a7 f, L0 A' x- #返回x的基2对数
( {/ s/ w! ] [7 P( ` - log2(x)
( G' D" J, c, B- ?1 o: \ - Return the base 2 logarithm of x.+ Z, f% @4 ^, h
- >>> math.log2(32)) l. F# h/ {2 j2 X, }
- 5.07 t4 B( H3 w4 J3 ?' D2 ~# l- W
- >>> math.log2(20)
1 r# m) C, h6 p7 G3 p6 }$ c - 4.321928094887363
: t& w+ K& _ t, G - >>> math.log2(16)* i2 y* S1 \9 P/ _& \6 o$ D/ k7 w) o: r
- 4.0
复制代码
% m' c) W; V, W" f2 C* lmath.modf(x) 返回由x的小数部分和整数部分组成的元组
7 p$ s/ j- M$ m/ x0 c- #返回由x的小数部分和整数部分组成的元组, |( C' U5 H4 n! z
- modf(x)5 |' n ^; l4 N/ J0 i
- Return the fractional and integer parts of x. Both results carry the sign* @* w0 I m0 m% \- ~" G
- of x and are floats.) B( ?% D" H: Z, S7 D
- >>> math.modf(math.pi)+ f2 `; K4 ]- e& \
- (0.14159265358979312, 3.0)' D) ~! h8 W4 W; H7 P
- >>> math.modf(12.34)- Y( S( O: ]' c, M1 h$ s
- (0.33999999999999986, 12.0)
复制代码
6 N8 r/ _9 h3 ]3 S( W, Amath.sqrt(x) 求x的平方根
8 U$ J. B) A a* {/ c+ _: `- #求x的平方根+ ]6 y% C3 g; | V
- sqrt(x)2 |0 R( ]/ r8 n
- Return the square root of x.
+ L9 w# [/ S. {% p - >>> math.sqrt(100); r5 f( p6 t5 d5 A
- 10.0/ M$ M( G* j% J/ O0 H
- >>> math.sqrt(16); \2 `# E# I" P. I r K. {% R2 b
- 4.0
/ W% `) e2 c1 k& e - >>> math.sqrt(20)7 I+ W5 r3 S; O9 c* B1 ]
- 4.47213595499958
复制代码
" {9 t# K6 `2 K5 u8 ]0 Umath.trunc(x) 返回x的整数部分- #返回x的整数部分
# f0 Y5 o# S! t/ s, h - trunc(x:Real) -> Integral
3 O4 ?; i9 c0 e4 O% k! g - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( ?9 `) x, T. t; d1 a; y+ H4 a, t! U
- >>> math.trunc(6.789)- B/ y! u5 s% b1 b
- 63 p) k2 `4 r7 W5 m7 k( y
- >>> math.trunc(math.pi)! Y# ^- M8 b6 u0 F" [9 r# Q0 }' U
- 37 V) \6 J( V8 h& x0 y
- >>> math.trunc(2.567)
. u2 R3 c7 e6 J( j, s - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|