C++ STL之 #include <math>头文件
C++ 头文件math中包含数学中常用的函数,其中包括:
<1> 三角函数
cos, sin, tan
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double degrees, cos_result, sin_result, tan_result; degrees = 60.0; cos_result = cos ( degrees * PI / 180.0 ); sin_result = sin ( degrees * PI / 180.0 ); tan_result = tan ( degrees * PI / 180.0 ); printf ("The cosine of %.1f degrees is %f.\n", degrees, cos_result ); printf ("The sine of %.1f degrees is %f.\n", degrees, sin_result ); printf ("The tangent of %.1f degrees is %f.\n", degrees, tan_result ); return 0; }
执行结果: The cosine of 60.0 degrees is 0.500000. The sine of 60.0 degrees is 0.866025. The tangent of 60.0 degrees is 1.732051.
acos, asin, atan
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double value, acos_result, asin_result, atan_result; value = 0.5; acos_result = acos (value) * 180.0 / PI; asin_result = asin (value) * 180.0 / PI; atan_result = atan (value) * 180.0 / PI; printf ("The arc cosine of %.1f is %.1f degrees.\n", value, acos_result); printf ("The arc sine of %.1f is %.1f degrees.\n", value, asin_result); printf ("The arc tangent of %.1f is %.1f degrees.\n", value, atan_result); return 0; }
执行结果: The arc cosine of 0.5 is 60.0 degrees. The arc sine of 0.5 is 30.0 degrees. The arc tangent of 0.5 is 26.6 degrees.
<2> 指数和对数函数
exp,exp2
#include <stdio.h> #include <math.h> int main () { double param, exp_result, exp2_result; param = 5.0; exp_result = exp (param); exp2_result = exp2 (param); printf ("The exponential value of %.1f is %.1f.\n", param, exp_result ); printf ("2 ^ %.1f = %.1f.\n", param, exp2_result ); return 0; }
执行结果: The exponential value of 5.0 is 148.4. 2 ^ 5.0 = 32.0.
log,log2,logb,log10
#include <stdio.h> #include <math.h> int main () { double param, log_result, log2_result, logb_result, log10_result; param = 8; log_result = log (param); log2_result = log2 (param); logb_result = logb (param); log10_result = log10(param); printf ("log(%f) = %f\n", param, log_result); printf ("log2(%f) = %f.\n", param, log2_result); printf ("logb(%f) = %f.\n", param, logb_result ); printf ("log10(%f) = %f.\n", param, log10_result); return 0; }
执行结果: log(8.000000) = 2.079442 log2(8.000000) = 3.000000. logb(8.000000) = 3.000000. log10(8.000000) = 0.903090.
<3> 幂函数
#include <stdio.h> /* printf */ #include <math.h> /* pow */ int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%f) = %f\n", param, result ); printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) ); return 0; }
执行结果: sqrt(1024.000000) = 32.000000 7 ^ 3 = 343.000000
<4> 舍入和余数函数
ceil, floor, round,fmod
#include <stdio.h> #include <math.h> int main () { printf ( "ceil的功能是上取\n" ); printf ( "ceil of 2.3 is %.1f\n", ceil(2.3) ); printf ( "ceil of 3.8 is %.1f\n", ceil(3.8) ); printf ( "ceil of -2.3 is %.1f\n", ceil(-2.3) ); printf ( "ceil of -3.8 is %.1f\n", ceil(-3.8) ); printf ( "floor的功能是下取\n" ); printf ( "floor of 2.3 is %.1lf\n", floor (2.3) ); printf ( "floor of 3.8 is %.1lf\n", floor (3.8) ); printf ( "floor of -2.3 is %.1lf\n", floor (-2.3) ); printf ( "floor of -3.8 is %.1lf\n", floor (-3.8) ); printf ( "round 的功能四舍五入\n" ); printf ( "round of 2.3 is %.1lf\n", round (2.3) ); printf ( "round of 3.8 is %.1lf\n", round (3.8) ); printf ( "round of -2.3 is %.1lf\n", round (-2.3) ); printf ( "round of -3.8 is %.1lf\n", round (-3.8) ); printf ( "fmod 的功能浮点数取模\n" ); printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) ); printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) ); return 0; }
执行结果: ceil的功能是上取 ceil of 2.3 is 3.0 ceil of 3.8 is 4.0 ceil of -2.3 is -2.0 ceil of -3.8 is -3.0 floor的功能是下取 floor of 2.3 is 2.0 floor of 3.8 is 3.0 floor of -2.3 is -3.0 floor of -3.8 is -4.0 round 的功能四舍五入 round of 2.3 is 2.0 round of 3.8 is 4.0 round of -2.3 is -2.0 round of -3.8 is -4.0 fmod 的功能浮点数取模 fmod of 5.3 / 2 is 1.300000 fmod of 18.5 / 4.2 is 1.700000
<5> 最小、最大、差分函数
fmin,fmax
#include <stdio.h> #include <math.h> int main () { printf ("fmin (100.0, 1.0) = %f\n", fmin(100.0,1.0)); printf ("fmin (-100.0, 1.0) = %f\n", fmin(-100.0,1.0)); printf ("fmin (-100.0, -1.0) = %f\n", fmin(-100.0,-1.0)); printf ("fmax (100.0, 1.0) = %f\n", fmax(100.0,1.0)); printf ("fmax (-100.0, 1.0) = %f\n", fmax(-100.0,1.0)); printf ("fmax (-100.0, -1.0) = %f\n", fmax(-100.0,-1.0)); return 0; }
执行结果: fmin (100.0, 1.0) = 1.000000 fmin (-100.0, 1.0) = -100.000000 fmin (-100.0, -1.0) = -100.000000 fmax (100.0, 1.0) = 100.000000 fmax (-100.0, 1.0) = 1.000000 fmax (-100.0, -1.0) = -1.000000
<6> 其他函数
fabs是头文件math.h中取绝对值方法
abs是头文件cmath中取绝对值方法
#include <stdio.h> #include <math.h> #include <cmath> int main () { printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) ); printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) ); printf ("The absolute value of 3.1416 is %f\n", std::abs (3.1416) ); printf ("The absolute value of -10.6 is %f\n", std::abs (-10.6) ); return 0; }
执行结果: The absolute value of 3.1416 is 3.141600 The absolute value of -10.6 is 10.600000 The absolute value of 3.1416 is 3.141600 The absolute value of -10.6 is 10.600000