這是我的程式範例,主要是參考http://www.cplusplus.com/reference/clibrary/cmath/,如有侵權請通知我
以下就是我的例子,細節可以參考上面的網址,主要在與如何使用這些函數,得到我們想要的數學結果,所以我就不多做解譯了
#include <iostream>
#include <cstdlib>
#include <cmath>
#define PI 3.14159265
using namespace std;
int main(void)
{
//三角函數,其中cot=1/tan, sec=1/sin, cos=1/csc
double param, result;
int n;
param = 45.0;
result = sin (param*PI/180);
printf ("The sine of %1f degrees is %1f. \n", param, result );
result = cos (param*PI/180);
printf ("The cosine of %1f degrees is %1f. \n", param, result );
result = tan (param*PI/180);
printf ("The tangent of %1f degrees is %1f. \n", param, result );
param = 0.5;
result = asin (param) * 180.0 / PI;
printf ("The arc sin of %1f is %1f degrees\n", param, result);
result = acos (param) * 180.0 / PI;
printf ("The arc cos of %1f is %1f degrees\n", param, result);
result = atan (param) * 180.0 / PI;
printf ("The arc tan of %1f is %1f degrees\n", param, result);
//是y/x的atan值,只是會利用正負號去判斷在那一個象限
double x, y;
x = -10.0;
y = 10.0;
result = atan2(y,x)*180.0/PI;
printf ("The arc tan for (x=%1f, y=%1f) is %1f degrees\n", x, y, result);
//雙曲線比較少用到,先跳過
//指數和對數相關
param=1.0;
result=exp(param); //自然指數
printf("The exponential value of %1f is %1f. \n", param, result);
param=27.0;
result=frexp(param, &n); //判斷2的次方數,多少比例
printf("%1f * 2^%d = %1f \n", result, n, param);
param=0.9;
n=5;
result=ldexp(param, n);
printf("%f * 2^%d = %f \n", param, n, result);
param=100000;
result=log(param); //以e為底
printf("ln(%f)=%f \n", param, result);
param=100000;
result=log10(param); //以10為底
printf("ln(%f)=%f \n", param, result);
param=3.111222;
double fract_part, int_part;
fract_part=modf(param, &int_part); //分割分數的整數和小數部分
printf("%1f = %1f + %1f \n", param, int_part, fract_part);
printf("7^3=%1f \n", pow(7,3.0)); //x的y次方
printf("sqrt(49)=%f \n", sqrt(49)); //開根號
printf("ceil of 2.333 is %1f \n", ceil(2.333)); //ceiling and floor
printf("ceil of -2.333 is %1f \n", ceil(-2.333));
printf("floor of 2.333 is %1f \n", floor(2.333));
printf("floor of -2.333 is %1f \n", floor(-2.333));
printf("The absolute value of 2.333 is %1f \n", fabs(2.333)); //絕對值
printf("The absolute value of -2.333 is %1f \n", fabs(-2.333));
printf("fmod of 7.888/2 is %1f \n", fmod(7.888,2)); //求餘數,可有小數位數
system("pause");
return 0;
}