
地 址:上海市静安66号
电 话:13332133228
网址:dsesh.com
C语言中(zhong)提供了丰富的语言中用符数学函数,这些(xie)函数可以帮助我们进行各种数学计算,数学示在C语言中,函数号表(biao)math.h头文件中,语言中用符因此在使用数学函数之前,数学示需要(yao)先引入该头(tou)文件,函数号表以下是语言中用符一些(xie)常用的数学函数及其使用方法:(图片来源网络,侵删)
1、数学示绝对值函数(shu):abs(x)

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

#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;}输出结果(guo):
Absolute value of 3.140000 is: 3.140000Absolute value of 5 is: 5Absolute value of 2.718280 is: 2.7182802、平方根函数:sqrt(x)
sqrt(x)函数用于计算参数x的数学示平方根,其原型(xing)声明如下(xia):
#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",函数号表 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)的(de)结果未定义,不建(jian)议使(shi)用负数作为参数 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)的数学示结果未定义,不建议使用负数作为参(can)数。函数号表
3、三角函数:sin、cos、tan等(deng)(已过时,建议使用cmath库中的对应函数)
C语言中提供了一些过时的(de)三角函数,如sin(x)、cos(x)、tan(x)等,这些函数已经过时,不建议使用,取而代之的是,可以使用cmath库中的对应函数,如sin(x)、cos(x)、tan(x)等(deng),这些函数(shu)的原(yuan)型声明如(ru)下:
#include <math.h> // 或(huo)者 #include <cmath>(推荐)double sin(double x); // 返回x的正弦值(弧度制)double cos(double x); // 返回x的余弦值(弧度制)double tan(double x); // 返回x的正切值(弧度制)示例:
#include <stdio.h> // 或(huo)者 #include <cstdio>(推荐)#include <cmath> // 或者 #include <math.h>(已过时) // 根据编译器的不同,可能需要选择不同的头文件名和库名(ming),例如在GCC编译器中,推荐使用<cmath>和<math.h>;在Visual Studio编译器中,推荐使用(yong)<cmath>和<math.h>或<mathlib>和<mspdb100.dll>,具体可参考编译器的(de)文(wen)档和帮助。