本篇博客是对 Linux_C 函数库
中用于 数学计算
函数(例如: sqrt()
, abs()
,pow()
等函数)的总结,在此与大家分享
代码范例:
#include
#include
int main()
{
int answer = 0;
answer = abs(-12);
printf(" | -12 | = %d\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = 0.0;
answer = fabs (-3.141592);
printf("| -3.141592 | = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
long int answer = 0;
answer = labs (-2000);
printf("| -2000 | = %ld\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double value[] = { 4.8, 1.12, -2.2, 0 };
int i = 0;
for (i = 0; value[i] != 0; i++)
{
printf("%f => %f\n", value[i], ceil(value[i]));
}
return 0;
}
代码范例:
#include
#include
int main()
{
double answer = 0.0;
answer = exp(10);
printf("e^10 = %f\n", answer);
return 0;
}
执行结果:
代码范例:
/* 计算 3*(2^2) = 12 */
#include
#include
int main()
{
int exp = 2;
double x = 3.0;
double answer = 0.0;
answer = ldexp(x, exp);
printf("3*2^(2) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
int exp = 0;
double fraction = 0.0;
fraction = frexp(1024, &exp);
printf("exp = %d\n", exp);
printf("fraction = %f\n", fraction);
return 0;
}
代码范例:
#include
#include
int main()
{
double answer = 0.0;
answer = log(100);
printf("log(100) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = 0.0;
answer = log10(100);
printf("log10(100) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
/* 分解 3.14159 的整数和小数部分 */
#include
#include
int main()
{
double integral = 0.0;
double fractional = 0.0;
fractional = modf(3.14159, &integral);
printf("integral = %f\n", integral);
printf("fractional = %f\n", fractional);
return 0;
}
代码范例:
#include
#include
int main()
{
double answer = 0.0;
answer = pow(2, 10);
printf("2^10 = %f\n", answer);
return 0;
}
执行结果:
代码范例:
/* 计算 200 的平方根值 */
#include
#include
int main()
{
double root = 0.0;
root = sqrt(200);
printf("answer is %f\n", root);
return 0;
}
执行结果:
代码范例:
/* 计算点 (3, 4) 至原点的距离 */
#include
#include
int main()
{
double distance = 0.0;
distance = hypot(3, 4);
printf("distance of the point (3, 4) from the origin is %f\n", distance);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double angle = 0.0;
angle = asin(0.5);
printf("angle = %f\n", angle);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double angle = 0.0;
angle = acos(0.5);
printf("angle = %f\n", angle);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double angle = 0.0;
angle = atan(1);
printf("angle = %f\n", angle);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double angle = 0.0;
angle = atan2(1, 2);
printf("angle = %f\n", angle);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = sin(0.5);
printf("sin(0.5) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = cos(0.5);
printf("cos [0.5] = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = tan(0.5);
printf("tan(0.5) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = sinh(0.5);
printf("sinh(0.5) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = cosh(0.5);
printf("cosh (0.5) = %f\n", answer);
return 0;
}
执行结果:
代码范例:
#include
#include
int main()
{
double answer = tanh(0.5);
printf("tanh(0.5) = %f\n", answer);
return 0;
}
执行结果:
typedef struct
{
int quot; /* 商数 */
int rem; /* 余数 */
} div_t;
代码范例:
#include
#include
int main()
{
div_t answer;
answer = div(67, 4);
printf("Quotient = %d, remainder = %d\n", answer.quot,answer.rem);
return 0;
}
执行结果:
typedef struct
{
long int quot; /* 商数 */
long int rem; /* 余数 */
} ldiv_t;
代码范例:
/* 计算 2345678 / 76542 的商及余数 */
#include
#include
int main()
{
ldiv_t answer;
answer = ldiv(2345678, 76542);
printf("Quotient = %ld, remainder = %ld\n", answer.quot, answer.rem);
return 0;
}
执行结果:
◆ 快速回顾
◆ 其他博客 @ https://blog.csdn.net/姜小逗
◆ 相关博客