常用数学函数

中学数学中有以下几种函数:

一、幂函数
定义:一般地,函数y=xa叫做幂函数,前面系数必须是1,没有其它项,定义域与a的值有关系。

二、指数函数
定义:一般地,函数y=ax (a>0, a≠1),其中x是自变量,定义 域是R。

三、对数函数
定义:一般地,函数叫做对数函数,其中(x>0, a>0, a≠1)。a为底数,x为真数。它实际上是指数函数的反函数。以自然常数e为底,称为自然对数。

在很多题目中,我们都会涉及一些数学的问题,需要求数学中的量。这些计算如果自己写程序会比较麻烦,所以在C++ 的cmath库中,提供了许多可供直接使用的常用数学函数。只要在程序前加上#include <cmath>,我们就可以在程序中直接调用这些函数。下表中1-10的函数需要同学们熟练掌握。

函数原型函数功能
1int abs(int x)返回整数x的绝对值
2double round (double x)对浮点数 float 或者 double四舍五入
3double ceil(double x)返回不小于x的最小整数
4double floor(double x)返回不大于x的最大整数
5double sin(double x)反回弧度x的正弦函数值
6double cos(double x)返回弧度x的余弦函数值
7double tan(double x)反回弧度x的正切函数值
8double sqrt(double x) 返回x的平方根值
9double log(double x)返回x的自然对数ln(x)的值
10double log2(double x)返回以2为底的x的对数值
11double pow(double x, double y)返回x的y次方
12double exp(double x)返回x指数函数e^x的值
13double hypot(double x, double y)给出两个直角边x, y,计算直角三角形的斜边长
14double fabs(double x) 返回实数x的绝对值
15double acos(double x)返回x的反余弦函数
16double asin(double x)返回x的反正弦函数
17double atan(double x)返回x的反正切函数
18double atan2(double x, double y) 反回点(x,y)的极角(极坐标)
19double cosh(double x)返回x的双曲余弦函数值
20double tanh(double x) 返回x的双曲正切函数值

代码举例:

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**************************************************************** 
C++ program to illustrate some of the cmath  functions 
date: 2023-3-13
vertion: 2.0 
author: Alex Li
****************************************************************/

#include <iostream> 
#include <math.h> 
using namespace std; 

int main() { 
	int z = -10; 
	cout << "Absolute value of z=-10 : " << abs(z) << endl; 
	
	double x = 2.3; 
	cout<<"The value of x rounded to the nearest integral: "<<round(x)<<endl;
	
	double	y = 12.3; 
	cout << "Ceiling value of y=12.3 : " << ceil(y) << endl; 

	x = 4.56; 
	cout << "Floor value of x=4.56 is : " << floor(x) << endl; 


	x=2.3;
	cout << "Sine value of x=2.3 : " << sin(x) << endl; 
	cout << "Cosine value of x=2.3 : " << cos(x) << endl; 
	cout << "Tangent value of x=2.3 : " << tan(x) << endl; 

	y=0.25;
	cout << "Square root value of y=0.25 : " << sqrt(y) << endl; 

	cout << "Power value: x^y = (2.3^0.25) : " << pow(x, y) << endl; 

	y = 100.0; 
	// Natural base with 'e' 
	cout << "Log value based with e of y=100.0 is : " << log(y) << endl; 
        // Natural base with 10
	cout << "Log value based with 10 of y=100.0 is : " << log10(y) << endl; 


	x = -4.57; 
	cout << "Absolute real number value of x=-4.57 is : " << fabs(x) << endl; 
	
	
	x = 3.0; 
	y = 4.0; 
	cout << "the Hypotenuse whose legs are x=3.0 and y=4.0 is " << hypot(x, y) << endl; 

	return 0; 
} 
Scroll to Top