例231:库函数中的除法函数div()
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
// div返回的结构体
div_t result;
result = div(11, 3);
// 输出11/3 商数和余数
printf("11 divided by 3 is %d Remainder %d\n", result.quot, result.rem); // 11 divided by 3 is 3 Remainder 2
return 0;
}
例232:库函数中的浮点操作函数frexp()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
double value = 1.2345;
double mantissa;
int exponent;
// frexp把一个浮点数分解为尾数和指数, 返回尾数,指数通过指针存入exponent
mantissa = frexp(value, &exponent);
printf("Mantissa %f Exponent %d Value %f\n", mantissa, exponent, mantissa * pow(2.0, 1.0 * exponent)); // Mantissa 0.617250 Exponent 1 Value 1.234500
return 0;
}
例233:库函数中的浮点操作函数modf()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
double value = 1.2345;
double int_part;
double fraction;
// modf分解value,以得到value的整数和小数部分,返回小数部分,整数通过指针存入int_part
fraction = modf(value, &int_part);
printf("Value %f Integer part %f Fraction %f\n", value, int_part, fraction); // Value 1.234500 Integer part 1.000000 Fraction 0.234500
return 0;
}
例234:库函数中的计算函数ldexp()
#include <stdio.h>
#include <math.h>
/**
* 函数名: double ldexp(double value, int exp);
* 功 能: 计算value乘以2的exp次幂 ( value * ( 2^exp ) )
* 返回值: 返回计算结果
*/
int main(int argc, char const *argv[])
{
printf("3 * 2 raised to the 4 is %f\n", ldexp(3.0, 4));// 3 * 2 raised to the 4 is 48.000000
return 0;
}
例235:取对数函数log10()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
// log10用来取以10为底的对数
printf("Log10 of 100 is %f\n", log10(100.0)); // Log10 of 100 is 2.000000
printf("Log10 of 10000 is %f\n", log10(10000.0)); // Log10 of 10000 is 4.000000
return 0;
}
例236:指数运算函数pow10()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
// 10的-1次方...
printf("10 raised to -1 is %f\n", pow10(-1)); // 10 raised to -1 is 0.100000
printf("10 raised to 0 is %f\n", pow10(0)); // 10 raised to 0 is 1.000000
printf("10 raised to 1 is %f\n", pow10(1)); // 10 raised to 1 is 10.000000
printf("10 raised to 2 is %f\n", pow10(2)); // 10 raised to 2 is 100.000000
return 0;
}
例237:绝对值函数abs()
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
// 5的绝对值...
printf("The absolute value of %d is %d\n", 5, abs(5)); // The absolute value of 5 is 5
printf("The absolute value of %d is %d\n", 0, abs(0)); // The absolute value of 0 is 0
printf("The absolute value of %d is %d\n", -5, abs(-5)); // The absolute value of -5 is 5
return 0;
}
例238:三角运算函数cos()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
// cos(π/2)的值
printf("cosine of pi/2 is %6.4f\n", cos(3.14159/2.0)); // cosine of pi/2 is 0.0000
// cos(π)的值
printf("cosine of pi is %6.4f\n", cos(3.14159)); // cosine of pi is -1.0000
return 0;
}
例239:e为底的指数函数exp()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
double value;
// exp用来计算以e为底的指数函数
for (value = 0.0; value <= 1.0; value += 0.1)
printf("exp(%f) is %f\n", value, exp(value));
return 0;
}
例240:计算自然对数的函数log()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
// log(x) 函数用来计算x的自然对数,也即以e为底的对数
printf("Natural log of 256.0 is %f\n", log(256.0)); // Natural log of 256.0 is 5.545177
return 0;
}
例241:次方计算函数pow()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int power;
// pow(x,y)计算x的y次
for (power = -2; power <= 2; power++)
printf("10 raised to %d is %f\n", power, pow(10.0, power));
// 10 raised to -2 is 0.010000
// 10 raised to -1 is 0.100000
// 10 raised to 0 is 1.000000
// 10 raised to 1 is 10.000000
// 10 raised to 2 is 100.000000
return 0;
}
例242:三角运算函数sin()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
double radians;
for (radians = 0.0; radians < 3.1; radians += 0.1)
printf("Sine of %f is %f\n", radians, sin(radians));
return 0;
}
例243:三角运算函数tan()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
double pi = 3.14159265;
// tan(π)的值
printf("Tangent of pi is %f\n", tan(pi)); // // Tangent of pi is -0.000000
// tan(π/4)的值
printf("Tangent of pi/4 is %f\n", tan(pi / 4.0)); // Tangent of pi/4 is 1.000000
return 0;
}
例244:浮点数的绝对值函数fabs()
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
float value;
for (value = -1.0; value <= 1.0; value += 0.1)
printf("Value %f fabs %f\n", value, fabs(value));
return 0;
}