对数

  一般地,如果ax=N(a>0,a1) ,那么数x叫做以a为底N对数(logarithm),记做

x=logaN

其中a叫做对数的底数,N叫做真数。
  通常我们将以10为底的对数叫做常用对数,并把log10N记做lgN.另外,在科学技术中常使用以无理数e=2.71828为底数的对数,以e为底的对数称为自然对数,并且把logeN记做lnN.
    根据对数的定义,可以得到对数与指数间的关系:
a>0,a1时,ax=Nx=logaN.
由指数与对数的这个关系,可以得到关于对数的如下结论:
负数和零没有对数。
loga1=0,logaa=1.

例子:

log24=2

log5625=5

log2164=6

开方

开方,指求一个数的方根的运算,为乘方的逆运算,若一个数b为数an次方根,则bn=ana=b,读作an次方根等于b.
2次方根称为平方根,3次方根称为立方根。
在实数范围内,下式成立:
bn=a

  • 如果n为偶数,此时b=±na
  • 如果n为奇数,此时b=na

Python里求对数和开方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import math

a = math.log(27, 3) # 求以3为底27的对数
b = math.log2(1024) # 求以2为底1024的对数
c = math.log10(100) # 求以10为底100的对数
d = math.log(math.e) # 求以e为底e的对数 e = 2.71828...
e = math.log1p(math.e - 1) # 求以e为底1 + x的对数
print(a, b, c, d, e)

x1 = math.e
x2 = math.pi
x3 = math.sqrt(4)
x4 = math.pow(8, 0.5)
x5 = math.ceil(1.3)
x6 = math.floor(1.7)
x7 = math.trunc(8.39)
x8 = round(math.pi, 4)
print("math.e = ", x1)
print("math.pi = ", x2)
print("math.sqrt(4) = ", x3)
print("math.pow(8, 0.5) = ", x4)
print("math.ceil(1.3) = ", x5)
print("math.floor(1.7) = ", x6)
print("math.trunc(8.39) = ", x7)
print("round(math.pi, 4) = ", x8)
'''
output:
3.0 10.0 2.0 1.0 1.0
math.e = 2.718281828459045
math.pi = 3.141592653589793
math.sqrt(4) = 2.0
math.pow(8, 0.5) = 2.8284271247461903
math.ceil(1.3) = 2
math.floor(1.7) = 1
math.trunc(8.39) = 8
round(math.pi, 4) = 3.1416
'''

C++里求对数和开方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
using namespace std;


int main() {
int a = 10;
int b = 1024;
int c = 16;
// 以10为底a的对数
cout << log10(a) << endl;
// 以e为底exp(1)的对数,exp(1)为e的1次方
cout << log(exp(1)) << endl;
// 以2为底b的对数
cout << log2(b) << endl;
// 以4为底16的对数,和Python不同,因为C++中没有log(4, 6)这样的函数,这里用到了换底公式
cout << log(16) / log(4) << endl;
// 8的二次方根
cout << sqrt(8) << endl;
// 2.0的4.0次方
cout << pow(2.0, 4.0) << endl;
}
/**
1
1
10
2
2.82843
16
*/