c语言中提供了丰富的数学函数,这些函数可以帮助我们进行各种数学计算,在C语言中,
数学函数定义在math.h
头文件中,因此在使用数学函数之前,需要先引入该头文件,以下是一些常用的数学函数及其使用方法:,1、绝对值函数:abs(x)
,abs(x)
函数用于计算参数x
的绝对值,其原型声明如下:,示例:,输出结果:,2、平方根函数:sqrt(x)
,sqrt(x)
函数用于计算参数x
的平方根,其原型声明如下:,示例:,输出结果:,3、三角函数:sin、cos、tan等(已过时,建议使用cmath库中的对应函数),C语言中提供了一些过时的三角函数,如sin(x)
、cos(x)
、tan(x)
等,这些函数已经过时,不建议使用,取而代之的是,可以使用cmath
库中的对应函数,如sin(x)
、cos(x)
、tan(x)
等,这些函数的原型声明如下:,示例:, ,#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.140000 Absolute value of 5 is: 5 Absolute value of 2.718280 is: 2.718280,#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)的结果未定义,不建议使用负数作为参数 return 0; }
原创文章,作者:admin,如若转载,请注明出处:https://www.vaicdn.com/news/38965.html