C语言中提供了丰富的语言中用符数学函数,这(zhe)些函数可以帮助我们进行各种数学计算,数学示(shi)在C语言中,函数号表数学函数定义在math.h头文件中,语言中用符因此在使(shi)用数学函数之前,数学示需要(yao)先引入该头文件,函数号(hao)表以下(xia)是语言中用符一些常用的数学函数(shu)及其使用方法:(图片来源网络,侵删)
1、数学示绝对值函数:abs(x)

abs(x)函数用于计算参数x的(de)函数号表绝对值,其原型声明如下:

#include <math.h>double abs(double x);int abs(int x);long double abs(long double x);
示例:

#include <stdio.h>#include <math.h>int main() { double a = 3.14; int b = 5; long double c = 2.71828; printf("Absolute value of %lf is: %lf",语言中用符 a, abs(a)); printf("Absolute value of %d is: %d", b, abs(b)); printf("Absolute value of %Lf is: %Lf", c, abs(c)); return 0;}输出结果:
Absolute value of 3.140000 is: 3.140000Absolute value of 5 is: 5Absolute value of 2.718280 is: 2.718280
2、平方(fang)根函数:sqrt(x)
sqrt(x)函数(shu)用(yong)于计算参数x的数学示平方根,其原型(xing)声明如下:
#include <math.h>double sqrt(double x);
示例:
#include <stdio.h>#include <math.h>int main() { double a = 9.0; double b = 25.0; double c = 4.0; printf("Square root of %lf is: %lf",函数号(hao)表 a, sqrt(a)); printf("Square root of %lf is: %lf", b, sqrt(b)); printf("Square root of %lf is: %lf", c, sqrt(c)); // 注意:sqrt(1)的结果未定义,不建议使用负数作为参数 return 0;}输出结果:
Square root of 9.000000 is: 3.000000Square root of 25.000000 is: 5.000000Square root of 4.000000 is: inf (无穷大),语言中用符因为sqrt(1)的数学示结果未定义,不建议使用负数作为参数(shu)。函数号表
C语言中提供了一些过时的三角函数,如(ru)sin(x)、cos(x)、tan(x)等(deng),这些函数已(yi)经过时,不建议使用,取而代之的是,可以使用cmath库中的对应函数,如sin(x)、cos(x)、tan(x)等,这些函数的原型声明如下(xia):
#include <math.h> // 或者 #include <cmath>(推荐)double sin(double x); // 返回x的正弦值(zhi)(弧度制)double cos(double x); // 返回x的余弦值(弧度制(zhi))double tan(double x); // 返回x的正切值(弧度制)
示例:
#include <stdio.h> // 或者(zhe) #include <cstdio>(推荐)#include <cmath> // 或者 #include <math.h>(已过时) // 根据编译器的不同,可能需要(yao)选择不同的头(tou)文件名和库名,例如在GCC编译器中,推荐使用<cmath>和<math.h>;在Visual Studio编译器中,推荐使用<cmath>和<math.h>或<mathlib>和<mspdb100.dll>,具体可参考编译器的文档和帮助。
电话:18022031060
网 址:http://bindebake.cn/
邮 箱:93643448@qq.com
地 址:上海市嘉定66号