math.h
是C语言标准库中的头文件,提供了许多与数学运算相关的函数。在本文中,我们将深入讨论一些 math.h
中常用的数学函数,包括基本算术运算、三角函数和对数函数等。
double sqrt(double x);
x
的平方根,结果是一个非负的双精度浮点数。#include <stdio.h>
#include <math.h>
int main() {
double x = 25.0;
double result = sqrt(x);
printf("sqrt(%f) = %f\n", x, result);
return 0;
}
sqrt(25.000000) = 5.000000
double fabs(double x);
x
的绝对值。# include <stdio.h>
# include <math.h>
int
main() {
double
num = -10.5;
double
result = fabs(num);
printf("fabs(%f) = %f\n", num, result);
return 0;
}
fabs(-10.500000) = 10.500000
double pow(double x, double y);
x
的y
次方。# include <stdio.h>
# include <math.h>
int
main() {
double
base = 2.0;
double
exponent = 3.0;
double
result = pow(base, exponent);
printf("%f ^ %f = %f\n", base, exponent, result);
return 0;
}
2.000000 ^ 3.000000 = 8.000000
double sin(double x);
x
的正弦值。# include <stdio.h>
# include <math.h>
int
main() {
double
angle = 30.0;
double
radians = angle * M_PI / 180.0;
double
result = sin(radians);
printf("sin(%f degrees) = %f\n", angle, result);
return 0;
}
sin(30.000000 degrees) = 0.500000
double cos(double x);
x
的余弦值。# include <stdio.h>
# include <math.h>
int
main() {
double
angle = 60.0;
double
radians = angle * M_PI / 180.0;
double
result = cos(radians);
printf("cos(%f degrees) = %f\n", angle, result);
return 0;
}
cos(60.000000 degrees) = 0.500000
double log(double x);
x
的自然对数。# include <stdio.h>
# include <math.h>
int
main() {
double
num = 2.0;
double
result = log(num);
printf("log(%f) = %f\n", num, result);
return 0;
}
log(2.000000) = 0.693147
double log10(double x);
x
的以10为底的对数。# include <stdio.h>
# include <math.h>
int
main() {
double
num = 100.0;
double
result = log10(num);
printf("log10(%f) = %f\n", num, result);
return 0;
}
log10(100.000000) = 2.000000
double exp(double x);
x
的指数值,即e^x
。#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double result = exp(x);
printf("exp(%f) = %f\n", x, result);
return 0;
}
exp(2.000000) = 7.389056
double ceil(double x);
x
的最小整数值。#include <stdio.h>
#include <math.h>
int main() {
double x = 3.14;
double result = ceil(x);
printf("ceil(%f) = %f\n", x, result);
return 0;
}
ceil(3.140000) = 4.000000
double floor(double x);
x
的最大整数值。#include <stdio.h>
#include <math.h>
int main() {
double x = 4.99;
double result = floor(x);
printf("floor(%f) = %f\n", x, result);
return 0;
}
floor(4.990000) = 4.000000
通过深入了解 math.h
头文件中的这些函数,我们可以更有效地利用C语言进行各种数学计算。这些函数为程序员提供了强大的工具,使得在实际编程中能够轻松进行复杂的数学操作。