|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
h' p8 y( ]4 m& i' ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。( u* h4 D+ Q. P% k1 ]6 j# t
( G5 N' m/ P$ `4 L6 h
方法1:$ [4 Z2 D' |, v c4 S$ l
- >>> import math6 z0 y" G: ^7 {) n2 \
- >>> math.sqrt(9)
! S# l. G$ h" P8 f9 b' p - 3.0
复制代码 方法2: o" f5 P$ s6 d. c
- >>> from math import sqrt
$ Z" @0 G9 E X6 }6 t1 T - >>> sqrt(9)" ]* f+ U6 u" n9 T0 ^: c( y, c2 }
- 3.0
复制代码 N( _' {# p; R; I3 X, m; g. M
4 {5 j9 Q4 `/ d+ G1 K( X4 J0 l0 Amath.e 表示一个常量: f6 j7 S/ f# Y( {
- #表示一个常量
4 R6 @, C4 u4 U6 W2 @! a - >>> math.e
0 ?) y% D, s% ^ - 2.718281828459045
复制代码
) @3 T9 g: [0 m K! h2 omath.pi 数字常量,圆周率+ H: O4 [: s1 ]6 @
- #数字常量,圆周率
# r0 t: N. Q* e& f! K7 x) P: u - >>> print(math.pi)
+ K; @! y5 `" E4 l7 ^ F - 3.141592653589793
复制代码 1 X( b* n# i! ~8 ?; {
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! V7 @6 b% X# F3 v; r& {- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
# I+ m/ z8 B! t; N$ \ - ceil(x)
2 q F# F# M; {: {. }) ~ - Return the ceiling of x as an int.9 C" b- p2 h; D" u$ Q+ O( I4 m
- This is the smallest integral value >= x.
. y/ ?$ I- c4 }; @ - 6 ^2 C& E6 c2 y5 V t- t u" h
- >>> math.ceil(4.01). J7 Z% j9 n7 V: e
- 5
" l" }0 r: L o7 u0 Q1 I - >>> math.ceil(4.99)7 l3 }* q$ z4 }2 i
- 5
5 o$ G0 I# p( W9 Z/ b) R( I- { - >>> math.ceil(-3.99)- A' { n# k% e: y' R7 y' Y
- -3
$ ~( d: m+ |, ~" E6 v% ?& A - >>> math.ceil(-3.01)
% f+ `* P% r' p( e1 o - -3
复制代码
8 L9 |/ ?( v. y0 qmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 L6 h* ?- c) A# M, A6 I+ w3 @- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 H3 d6 M- m$ _7 g4 M1 w7 f
- floor(x)- P) k% X! f; x$ Q X' o
- Return the floor of x as an int.2 `% I$ `- d9 D& r0 i# z/ \
- This is the largest integral value <= x.% E" x: K6 f2 L
- >>> math.floor(4.1)" w9 |0 @% v J% t5 E
- 4- I$ T+ B8 J$ x0 I1 U& ?6 e
- >>> math.floor(4.999)8 j9 c. V# n$ e8 Z- s" g4 }
- 4
) V* r3 f- S$ x1 m7 q" I - >>> math.floor(-4.999), {2 M, Z$ i# \ w) U3 f* L, `
- -5) g' A9 O& e0 Z0 E4 G6 D2 `- h
- >>> math.floor(-4.01)" w) U1 a2 {5 g" d9 I8 s
- -5
复制代码 2 y. Q% M. K- b, N7 ?
math.pow(x,y) 返回x的y次方,即x**y. t) G! f: q+ \ q; f" w6 y
- #返回x的y次方,即x**y- e9 r ?, X) e8 N Y- H
- pow(x, y): e$ x# R }9 g5 z( d; [. r/ f( |
- Return x**y (x to the power of y).
2 _' b8 O S/ M! y6 g0 X - >>> math.pow(3,4)1 V7 ^' _2 k N/ O/ e0 H& F
- 81.0
% i# g& }7 X& W, i4 P* g - >>>
; q& \2 T8 D6 |7 W2 x% Q2 q! n8 l - >>> math.pow(2,7)" j# x$ _+ i$ b' k, i5 W
- 128.0
复制代码
% d j: m* W) e [9 Z' E1 Cmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' k1 {& D$ q; P: p1 k- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 x& W$ B/ ~/ z" _% d - log(x[, base])) | `8 j5 p2 Y, y& k4 C
- Return the logarithm of x to the given base.+ ^1 ~0 [- I! o* p5 i& ~2 d, v
- If the base not specified, returns the natural logarithm (base e) of x.
! ~: ~" N- Q i - >>> math.log(10)( _* @- n" B! C2 [
- 2.3025850929940466 ?: U- w3 T% ?$ `
- >>> math.log(11)* {8 U d7 a: d
- 2.39789527279837076 i. d( Z. j% i! v+ `2 H
- >>> math.log(20)& i3 V4 e0 J( p
- 2.995732273553991
复制代码
/ R9 q; [& y! G* Xmath.sin(x) 求x(x为弧度)的正弦值; E/ n: i' X/ Y7 V& e
- #求x(x为弧度)的正弦值
( X3 P- B. L$ q- a( n) t1 S - sin(x)* f& P* F' d" i3 q" v% E' c7 N. N
- Return the sine of x (measured in radians).
% v7 C' q/ Z! ^( \ - >>> math.sin(math.pi/4)3 Q3 W. i+ z) n1 E+ b( ^- n! D8 L
- 0.7071067811865475
- X& b3 t$ `4 n7 u) A - >>> math.sin(math.pi/2)
4 e' z3 C5 p8 b# K/ ^' z' F; s% E - 1.0, c5 A+ L$ S6 O) @$ ~7 D
- >>> math.sin(math.pi/3)
( h: D& _0 q2 B7 X - 0.8660254037844386
复制代码 & f0 ?7 f8 R) L B; s
math.cos(x) 求x的余弦,x必须是弧度. [0 e& R |6 S q- J, i/ t
- #求x的余弦,x必须是弧度
+ O8 X5 C% o9 w% N* |" } - cos(x)
/ S' W: _# J/ ~: f% ?' C - Return the cosine of x (measured in radians).9 q! }- m1 u6 o. H" R5 p: L1 U5 X1 T
- #math.pi/4表示弧度,转换成角度为45度
9 ^+ g1 J( K* ^ - >>> math.cos(math.pi/4)
2 |! n: H8 t# D - 0.7071067811865476+ |; Z' ~ ~' a U, F9 L
- math.pi/3表示弧度,转换成角度为60度2 Q6 Z& o0 x0 J i. p
- >>> math.cos(math.pi/3)
7 v$ z# d6 [6 N+ i - 0.5000000000000001
U* W) H l* R0 P$ v4 [ - math.pi/6表示弧度,转换成角度为30度
, j! Z( a8 w' p2 M, M& J8 J - >>> math.cos(math.pi/6)% I+ s H4 d/ l% u: G, L
- 0.8660254037844387
复制代码
, Q7 E6 o& T1 Z+ x' ~5 n2 X* imath.tan(x) 返回x(x为弧度)的正切值4 E3 T1 K2 o! K/ O$ \9 h
- #返回x(x为弧度)的正切值
: K4 R. N% @7 K. ^( _; c3 P - tan(x)/ w4 @8 S0 }$ S; \7 F
- Return the tangent of x (measured in radians).
: O5 _0 O, K5 M - >>> math.tan(math.pi/4)1 } f G0 k4 b% \
- 0.9999999999999999
, z! E: ^$ o# J# T - >>> math.tan(math.pi/6)
. l7 z% M, W. Q0 m1 F% d - 0.5773502691896257, O! ~- I6 M% X0 b$ P. @+ t
- >>> math.tan(math.pi/3)5 T! i$ H. y4 ^
- 1.7320508075688767
复制代码 * `' G/ `) r; d6 ?
math.degrees(x) 把x从弧度转换成角度, Z: f h6 A$ A0 D
- #把x从弧度转换成角度4 \: }: l4 A- g
- degrees(x)
( Y% F' ~1 T4 n2 m - Convert angle x from radians to degrees.
2 @4 a3 X5 R6 l$ ~7 k! |5 \2 ^0 m
8 X! G" e' S2 B' {- >>> math.degrees(math.pi/4)
; q2 F$ {4 V, ^4 ~8 v - 45.02 I6 |1 b, R3 f9 Z4 b+ S
- >>> math.degrees(math.pi)- R' }5 Z: v6 N1 T1 i3 ~# B6 {# f
- 180.0* ^# b! `: F' y8 F5 b
- >>> math.degrees(math.pi/6)
# `& X/ P; |6 F# `; s0 O+ C+ ? - 29.999999999999996# ^ K3 A4 w/ k F0 [
- >>> math.degrees(math.pi/3): Q& z6 V4 c3 d3 A; N5 B
- 59.99999999999999
复制代码
7 O2 t, P# T7 m5 M+ lmath.radians(x) 把角度x转换成弧度
8 b: q/ {1 J2 @- #把角度x转换成弧度2 j- a6 |3 r# X- k
- radians(x)
4 X6 _% r3 W; z1 b - Convert angle x from degrees to radians.8 @# b2 ]6 P" B/ L2 V1 e
- >>> math.radians(45) c" O$ N% W! ]
- 0.7853981633974483
4 s0 l. W) I2 x6 {! R - >>> math.radians(60)) b9 S8 X0 e2 f7 o" v' X5 Z0 f
- 1.0471975511965976
复制代码
3 L: w. `* }+ M2 @, p: e# Smath.copysign(x,y) 把y的正负号加到x前面,可以使用0
+ G! R/ J6 R' }* u' V' {/ }- #把y的正负号加到x前面,可以使用0; e, d C5 v" B" J8 z5 V9 J4 |$ g
- copysign(x, y)2 M7 j# g9 |. u/ H$ A U
- Return a float with the magnitude (absolute value) of x but the sign
0 `. f, P, L. \( \7 J - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
3 g( C3 [ ]0 m9 [/ z - returns -1.0.
' U9 ?& B4 G, V3 k4 e
2 o2 m6 I6 t5 _6 s6 s/ P0 w- >>> math.copysign(2,3)
5 g3 [ R" c. m% n. \ - 2.0
, i+ u; }9 x( L1 W* s0 g( Z2 T - >>> math.copysign(2,-3)
% U _- V! V& \ - -2.05 L$ ]7 W8 E1 G5 t. ]' f( K6 k
- >>> math.copysign(3,8)" x, j9 a0 u/ h' ]
- 3.0
& _6 b! q0 @2 c" j0 ^ - >>> math.copysign(3,-8)0 y' d" U9 j2 W" {; j/ h9 O' N: a
- -3.0
复制代码
% a9 v' A4 H% n. }2 E3 I% ]math.exp(x) 返回math.e,也就是2.71828的x次方( u* T7 {" _2 S% P
- #返回math.e,也就是2.71828的x次方$ t5 a7 e$ M9 R9 }+ D Z
- exp(x)
- o4 ]3 Q% ?4 c - Return e raised to the power of x.3 H# j* T& B+ j7 R9 _- q
- 0 r" P! K! y3 c- a+ \" B" E9 T
- >>> math.exp(1)
; m$ D1 T0 R- h5 O2 v - 2.718281828459045/ L$ r; v/ ]" z: e: w
- >>> math.exp(2). l: l, f5 ] L" r L
- 7.38905609893065% D9 W% c5 o" `7 j1 T& L7 L
- >>> math.exp(3)
$ s. n3 T8 Z5 z) n& t! T- w( C) Z - 20.085536923187668
复制代码 / j1 `7 H Y% E! `/ C7 T4 L
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1* l" z0 u; }* x
- #返回math.e的x(其值为2.71828)次方的值减1; Q5 V5 q( k* a+ E! r* R) X! _/ C
- expm1(x): J' w3 B# I* W! j! i
- Return exp(x)-1.
% _8 N$ a0 B1 w6 s - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 [8 j. X8 k) ?9 T2 C( Z9 f' `" I
8 E' |7 K/ b- K! w* M% J- >>> math.expm1(1)+ k9 @2 G% r6 w$ v
- 1.718281828459045
& O' S* f i( L I6 | - >>> math.expm1(2)
) y" ?$ n$ v) G! \" {# v @1 j" k - 6.389056098930657 z) h7 z, y6 `
- >>> math.expm1(3)
|8 @6 p# o" O5 I; A - 19.085536923187668
复制代码
% J7 L/ {2 r& zmath.fabs(x) 返回x的绝对值! b1 B' R7 l$ @* }0 O, M( d
- #返回x的绝对值% \( ]9 ]% {3 l9 C
- fabs(x)
% }. Q- ~ o" f. F% y; ^ - Return the absolute value of the float x.1 e. M; y4 w$ r! T) x
5 _/ k" u5 C- i( |6 |$ ~8 s- >>> math.fabs(-0.003); \' J5 X3 T) h) Y+ z
- 0.003! B/ `& w" F" \( i) _2 b
- >>> math.fabs(-110)
4 Z( w; N# u" _1 r3 y - 110.0
% `( H# M; m7 X9 s - >>> math.fabs(100)
7 g/ Z5 q% V+ o7 E: u: a - 100.0
复制代码
5 ^) q! z/ }, T9 w3 V; Qmath.factorial(x) 取x的阶乘的值: M; ]# Y% L3 p* t! x5 Q( C9 X
- #取x的阶乘的值" Q9 o; h; U a9 k8 H: H3 R9 r/ _7 I
- factorial(x) -> Integral) l4 }8 C) W5 O
- Find x!. Raise a ValueError if x is negative or non-integral.) a5 O- u7 R+ u. y$ ^3 v X, u" \
- >>> math.factorial(1)
+ N0 W0 A2 }, K/ J6 \% i - 1/ e: K7 |7 W. r$ Q/ T
- >>> math.factorial(2)
C9 o0 K: g. A, v - 2( L7 M1 r/ t+ J% C% L! v
- >>> math.factorial(3)
3 x# u. U( J2 T1 G - 6
; K7 M$ |/ r/ _7 Y! d - >>> math.factorial(5)
2 P, W! X1 I( I- E5 J6 b) H - 120$ F2 C: ]% m0 P( ^! q" ?! W
- >>> math.factorial(10)' k2 e* b) r H6 @$ @8 Z. H/ M ~
- 3628800
复制代码 ! K, Z+ }3 @4 O, Q- E
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数! \8 B5 m. j# @: [) |; T
- #得到x/y的余数,其值是一个浮点数: I9 ^; b$ c, M, B+ i# I0 X. b
- fmod(x, y)* Q4 P- t/ A) V: W9 \
- Return fmod(x, y), according to platform C. x % y may differ.+ F1 ]# m7 f: v
- >>> math.fmod(20,3)- T2 @% f! g+ c0 Y$ f. x: o9 I
- 2.0
) F* P3 V) `3 v' Y - >>> math.fmod(20,7)0 w& G0 R0 n! B5 s. j# J
- 6.0
复制代码 $ x7 c& z& [! x- q Y) \, [3 @: h
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围: ~% r M8 n0 v
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 t6 X; S; D2 R
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
& R0 x$ d% v: X% F2 ~3 X - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
% c o: G- n, S6 k/ I - frexp(x)9 ~! x4 X \9 p1 h, S
- Return the mantissa and exponent of x, as pair (m, e).% I& R+ P4 H2 }: y- o6 H
- m is a float and e is an int, such that x = m * 2.**e.
" F0 H4 b+ b% F* D) X* a - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 h1 r# \0 \! ~/ ^
- >>> math.frexp(10)
. g- Q: A" e6 H, Q2 w5 u - (0.625, 4)" I! ^; J; r) h, S& L" P2 A
- >>> math.frexp(75), M/ h6 Y* w% a8 z, K/ P7 j
- (0.5859375, 7)
0 h1 a( X# P0 U+ r* n9 S$ D - >>> math.frexp(-40)
$ o. u1 k' F0 h' G8 K! Y1 d" i - (-0.625, 6)9 J0 p( D l; Y4 W v- p/ R6 g
- >>> math.frexp(-100)
: y4 f/ ?8 S- u: ?3 G - (-0.78125, 7)
6 l. v1 H$ j& f2 v - >>> math.frexp(100)
( n# \! G6 R. P! S+ ~% g6 } - (0.78125, 7)
复制代码 0 {6 h2 n! a. J- Y$ ?
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& D0 l) D3 A; z6 J! @, J- #对迭代器里的每个元素进行求和操作
u) H9 v7 K5 z" |- S# _. a+ z7 K0 Q - fsum(iterable)
9 ?! G5 W7 [; t. I, v - Return an accurate floating point sum of values in the iterable.
$ ]& @' D* t: k/ u2 C. z/ w - Assumes IEEE-754 floating point arithmetic.1 i% ]: |0 m1 Y6 d
- >>> math.fsum([1,2,3,4]) h; K v8 f- H! W4 d3 B6 [2 e. x* N
- 10.0
/ ]; k# G8 L4 E1 ~+ ` - >>> math.fsum((1,2,3,4))5 P+ |+ S; W9 M$ a% G; v
- 10.05 ^3 R# z( d5 T& a z
- >>> math.fsum((-1,-2,-3,-4))% D& t8 H( N% w# h* e7 [9 K. i+ v
- -10.0
/ e4 j( c F; w' \9 n5 R" M* M - >>> math.fsum([-1,-2,-3,-4])7 I0 n, f. `( j( \9 h! ?; ~3 `6 j
- -10.0
复制代码
: \* f+ X7 ?7 |; }; C% {* l6 P' ~math.gcd(x,y) 返回x和y的最大公约数
a( Q- Q+ w; d: v7 \( V- J8 A D- #返回x和y的最大公约数
/ H) D& U0 p, ^- S* Q: \. Q - gcd(x, y) -> int7 z0 @3 e* N; R' l0 t7 }* x
- greatest common divisor of x and y
6 O' T/ h& B* y ]/ V9 D5 k - >>> math.gcd(8,6)( @' d) H- R' B9 z, C
- 22 Z3 ]) x9 ^2 W' n, I. c
- >>> math.gcd(40,20)/ V# Z) k3 |) T( d0 T% Z. v% t
- 20( G% n. q, t1 ?8 p. b
- >>> math.gcd(8,12)) c N3 p$ G% s
- 4
复制代码
$ a! S3 i% i9 z4 Y# z. Pmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False# Q7 v; |3 M) q# O7 X9 b0 r
- #得到(x**2+y**2),平方的值
2 c8 u$ C$ E0 w( \8 h5 _4 h - hypot(x, y)
0 H: I. O; @, z& S* F( J/ R - Return the Euclidean distance, sqrt(x*x + y*y).
/ W' h) o* w. u - >>> math.hypot(3,4)/ t9 |/ Z: t z7 ^7 w0 }
- 5.0
6 V) C' y" i& F3 `8 Z7 M1 h! v% g2 ~( E - >>> math.hypot(6,8)9 i9 c( H" F* t# w/ }6 w
- 10.0
复制代码
2 G: {, n$ ^- Y9 m# ]# Smath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
7 Q) z. ^1 h" E* J- #如果x是不是无穷大的数字,则返回True,否则返回False
* }# g6 K! T4 K9 v# [: N" M& t) x - isfinite(x) -> bool
2 f9 k& x1 \9 e$ a - Return True if x is neither an infinity nor a NaN, and False otherwise.
$ d+ W7 K9 T; M1 W" d3 d - >>> math.isfinite(100)
, t- d x! L9 k: G; [' l - True
3 L4 ~, \; s, ~4 r$ V - >>> math.isfinite(0)% O+ r" W. y8 o& k" J
- True
8 C) c' W* n) n: I0 U0 P - >>> math.isfinite(0.1)
( X- t, W( T. C+ W; V - True
: l5 C8 A( Z- r7 z% n - >>> math.isfinite("a")" { _$ }# R" C% P
- >>> math.isfinite(0.0001)) ^6 v+ S4 M% M& k! |
- True
复制代码 & f/ r& ~2 m8 k0 M9 w+ x. |
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False; p% S" E; f: v
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
& n4 O i& \& g1 H) X4 W - isinf(x) -> bool
% l0 Y( \, v) {8 Q - Return True if x is a positive or negative infinity, and False otherwise.- j! M+ [5 @+ N. [" z6 l
- >>> math.isinf(234)
# v, K! s" t. O+ y2 C* U - False
( K8 R- J# G' i! }5 R) y - >>> math.isinf(0.1)# P9 O( b* F6 j0 E; k2 j
- False
复制代码 . i( k& d( l* n5 t. f3 E% a1 c
math.isnan(x) 如果x不是数字True,否则返回False% g. J5 Y: k) x7 d9 x
- #如果x不是数字True,否则返回False4 c: r. @- {8 |1 }' i$ H \8 F; Q; N ]
- isnan(x) -> bool
) l ?5 J6 L' ^0 `# M6 w7 r - Return True if x is a NaN (not a number), and False otherwise.* ?# n; I. y J* P
- >>> math.isnan(23): P H% P5 K, h; v k
- False# X$ j( `) Q4 r4 ~9 s7 |
- >>> math.isnan(0.01)" [! _! s1 V7 L8 M" z# J
- False
复制代码
4 D- S* A" g" U+ Z4 Z0 X5 h: |math.ldexp(x,i) 返回x*(2**i)的值
/ U7 z9 R7 u0 z i- #返回x*(2**i)的值
& G' x& g6 ?0 u; E0 s6 d* _ - ldexp(x, i)9 A8 K) P( _4 @% ^$ }
- Return x * (2**i).
& ]& Q# V( C" K - >>> math.ldexp(5,5)" P4 G, u) J$ A0 c9 p4 q+ t! |
- 160.04 Q$ K3 D3 d- F0 {1 u3 _$ g7 J
- >>> math.ldexp(3,5)% p S/ _* G+ b2 Q4 c
- 96.0
复制代码 ) \8 I4 t% M% u9 |! s' N
math.log10(x) 返回x的以10为底的对数
3 m3 ~6 V& l( _) _1 R J U- #返回x的以10为底的对数
9 Q. l% _/ d6 [5 z - log10(x)% }% [5 ]( _8 V( G9 ~
- Return the base 10 logarithm of x.
6 ~- X3 a1 I/ A3 t! r H+ Q - >>> math.log10(10). [- Q4 |# C, [) F
- 1.04 w6 Q$ K R, n$ v2 F( e# l! f
- >>> math.log10(100)
# ?3 e1 ]( @( R& p* L! _ - 2.0: n9 q" p3 P7 P8 a
- #即10的1.3次方的结果为20: F) n& v- u' n3 c7 ?) u
- >>> math.log10(20)
& @3 R5 P0 ]0 A$ D) ^. S) ?/ v - 1.3010299956639813
复制代码 6 p0 P4 K9 J4 I
math.log1p(x) 返回x+1的自然对数(基数为e)的值
. e$ A* `. i2 o* Z- #返回x+1的自然对数(基数为e)的值
# ~2 C2 r! G7 V$ g - log1p(x)/ r z; E% {9 O1 X
- Return the natural logarithm of 1+x (base e).8 r+ j @9 K7 ]* c! y: }8 ^3 K
- The result is computed in a way which is accurate for x near zero.
+ r) d" a$ h2 E6 Y - >>> math.log(10)2 [# j m0 y5 s1 A @
- 2.3025850929940463 W$ X0 x" Z, ^6 p
- >>> math.log1p(10) ?' C& _$ ?7 D- {) m: M
- 2.39789527279837071 T* N: b( `& U& q
- >>> math.log(11)
% v# q9 F. f7 f5 G T; W% R - 2.3978952727983707
复制代码
5 `3 z1 Y' t' P3 b: hmath.log2(x) 返回x的基2对数
% l& ~" h K# T4 W- b# ^- #返回x的基2对数
. ~" W" K9 I1 S0 q: I+ z$ T - log2(x), }8 X6 Q i1 ~+ h4 U9 u
- Return the base 2 logarithm of x.
0 S5 E$ Q% ]1 ~4 m0 [ - >>> math.log2(32)* K ^$ l/ W3 Y, E( K2 b) t& z+ D
- 5.0
4 }9 o- r, W4 I - >>> math.log2(20)
" [# n# M/ C' C! W" y - 4.321928094887363
& O8 n5 }: w2 Y. `4 W1 i - >>> math.log2(16)" s# H6 D2 E+ D4 K
- 4.0
复制代码
2 g; d% s/ H% T ] Y/ J% vmath.modf(x) 返回由x的小数部分和整数部分组成的元组
6 W& M- k' h2 M# V( [0 F! M' ~4 M: N- #返回由x的小数部分和整数部分组成的元组6 R7 l5 q' q) j" s& q" G
- modf(x)6 Q8 B7 V- |2 C0 K
- Return the fractional and integer parts of x. Both results carry the sign/ Q: i. n% i6 W0 x
- of x and are floats.# x5 x& M+ ^+ j$ B% S
- >>> math.modf(math.pi)4 w' m1 R# T- Q; X1 Q- K( K
- (0.14159265358979312, 3.0)" A% c6 Y6 d9 Y, q1 i
- >>> math.modf(12.34)5 L5 L5 O# n" O2 l. e7 O
- (0.33999999999999986, 12.0)
复制代码
: u4 m, I& ^$ R4 }7 ?% u3 \& W; \math.sqrt(x) 求x的平方根9 j: ]( F p7 N" j- U# K, f
- #求x的平方根7 Z n* \3 X" l4 u# T
- sqrt(x)
g+ m6 n S' N - Return the square root of x.8 ~$ ~9 p" H5 J# X- D! ?) j1 P: a
- >>> math.sqrt(100)9 \/ {* b/ a3 g( g0 O: {9 y9 O& q
- 10.08 k, Z8 D# E- ^) W) n8 r
- >>> math.sqrt(16)
$ L& M+ g( g+ ?1 b - 4.0- P z9 w% s$ k, z" Q
- >>> math.sqrt(20)2 `; s0 J8 S g% ~7 C
- 4.47213595499958
复制代码 2 F) T: F7 n" P& I U: r8 N
math.trunc(x) 返回x的整数部分- #返回x的整数部分: {' W C; v. J$ p, J1 k D0 Y
- trunc(x:Real) -> Integral
* y: y( u8 F4 v0 M; B7 ? - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
9 {2 j7 j4 C% |4 p v - >>> math.trunc(6.789)2 G; s0 k8 y/ y0 j
- 6
* z5 L5 ?0 f) N; b - >>> math.trunc(math.pi)& `# o2 |( L- Y1 q l
- 35 k" d6 j: t z7 Q
- >>> math.trunc(2.567)
* b5 s2 l* e2 d% @# T - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|