c++ math庫函數

abs

原型:extern int abs(int x);
用法:#include 
功能:求整數x的絕對值
說明:計算|x|, 當x不爲負時返回x,否則返回-x
舉例:
      
      #include 
      #include 

      main()
      {
        int x;
        clrscr();        // clear screen
        x=-5;
        printf("|%d|=%d\n",x,abs(x));
        x=0;
        printf("|%d|=%d\n",x,abs(x));
        x=+5;
        printf("|%d|=%d\n",x,abs(x));
        getchar();
        return 0;
      }
相關函數:fabs

acos
原型:extern float acos(float x);
用法:#include 
功能:求x(弧度表示)的反餘弦值
說明:x的定義域爲[-1.0,1.0],值域爲[0,π]。
舉例:
      
      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=0.32;
        printf("acos(%.2f)=%.4f",x,acos(x));
        getchar();
        return 0;
      }
相關函數:asin,atan,atan2,sin,cos,tan

asin

原型:extern float asin(float x);
用法:#include 
功能:求x(弧度表示)的反正弦值
說明:x的定義域爲[-1.0,1.0],值域爲[-π/2,+π/2]。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=0.32;
        printf("asin(%.2f)=%.4f",x,asin(x));
        getchar();
        return 0;
      }
相關函數:acos,atan,atan2,sin,cos,tan

atan

原型:extern float atan(float x);
用法:#include 
功能:求x(弧度表示)的反正切值
說明:值域爲(-π/2,+π/2)。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=0.32;
        printf("atan(%.2f)=%.4f",x,atan(x));
        getchar();
        return 0;
      }
相關函數:asin,acos,atan2,sin,cos,tan

atan2

原型:extern float atan2(float y, float x);
用法:#include 
功能:求y/x(弧度表示)的反正切值
說明:值域爲(-π/2,+π/2)。
舉例:

      #include 
      #include 
      main()
      {
        float x,y;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=0.064;
        y=0.2;
        printf("atan2(%.3f,%.2f)=%.4f",y,x,atan2(y,x));
        getchar();
        return 0;
      }
相關函數:asin,acos,atan,sin,cos,tan

ceil

原型:extern float ceil(float x);
用法:#include 
功能:求不小於x的最小整數
說明:返回x的上限,如74.12的上限爲75,-74.12的上限爲-74。返回值爲float類型。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=74.12;
        printf("ceil(%.2f)=%.0f\n",x,ceil(x));
        x=-74.12;
        printf("ceil(%.2f)=%.0f\n",x,ceil(x));
        getchar();
        return 0;
      }
相關函數:floor

cos

原型:extern float cos(float x);
用法:#include 
功能:求x(弧度表示)的餘弦值
說明:返回值在[-1.0,1.0]之間。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=PI/4.;
        printf("cos(%.4f)=%.4f\n",x,cos(x));
        getchar();
        return 0;
      }
相關函數:asin,acos,atan,atan2,sin,tan

cosh

原型:extern float cosh(float x);
用法:#include 
功能:求x的雙曲餘弦值
說明:cosh(x)=(e^x+e^(-x))/2
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=PI/4.;
        printf("cosh(%.4f)=%.4f\n",x,cosh(x));
        getchar();
        return 0;
      }
相關函數:sinh,tanh

exp

原型:extern float exp(float x);
用法:#include 
功能:求e的x次冪
說明:e=2.718281828...
舉例:

      #include 
      #include 
      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("e=%f\n",exp(1.0));
        getchar();
        return 0;
      }
相關函數:無

fabs

原型:extern float fabs(float x);
用法:#include 
功能:求浮點數x的絕對值
說明:計算|x|, 當x不爲負時返回x,否則返回-x
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=-74.12;
        printf("|%f|=%f\n",x,fabs(x));
        x=0;
        printf("|%f|=%f\n",x,fabs(x));
        x=74.12;
        printf("|%f|=%f\n",x,fabs(x));
        getchar();
        return 0;
      }
相關函數:abs

floor

原型:extern float floor(float x);
用法:#include 
功能:求不大於x的最達整數
說明:返回x的下限,如74.12的下限爲74,-74.12的下限爲-75。返回值爲float類型。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=74.12;
        printf("floor(%.2f)=%.0f\n",x,floor(x));
        x=-74.12;
        printf("floor(%.2f)=%.0f\n",x,floor(x));
        getchar();
        return 0;
      }
相關函數:ceil

fmod

原型:extern float fmod(float x, float y);
用法:#include 
功能:計算x/y的餘數
說明:返回x-n*y,符號同y。n=[x/y](向離開零的方向取整)
舉例:

      #include 
      #include 
      main()
      {
        float x,y;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=74.12;
        y=6.4;
        printf("74.12/6.4: %f\n",fmod(x,y));       
        x=74.12;
        y=-6.4;
        printf("74.12/(-6.4): %f\n",fmod(x,y));       
        getchar();
        return 0;
      }
相關函數:無

frexp

原型:extern float frexp(float x, int *exp);
用法:#include 
功能:把浮點數x分解成尾數和指數。
說明:x=m*2^exp,m爲規格化小數。返回尾數m,並將指數存入exp中。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        int exp;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=frexp(64.0,&exp);
        printf("64=%.2f*2^%d",x,exp);
        getchar();
        return 0;
      }
相關函數:ldexp,modf

hypot

原型:extern float hypot(float x, float y);
用法:#include 
功能:對於給定的直角三角形的兩個直角邊,求其斜邊的長度。
說明:返回斜邊值。
舉例:

      #include 
      #include 
      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("3^2+4^2=%.0f^2\n",hypot(3.,4.));
        printf("3.2^2+4.3^2=%.2f^2",hypot(x,y));
        getchar();
        return 0;
      }
相關函數:frexp,ldexp

ldexp

原型:extern float ldexp(float x, int exp);
用法:#include 
功能:裝載浮點數。
說明:返回x*2^exp的值。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=ldexp(1.0,6); // 1.0*2^6
        printf("2^6=%.2f",x);
        getchar();
        return 0;
      }
相關函數:frexp,modf

log

原型:extern float log(float x);
用法:#include 
功能:計算x的自然對數。
說明:x的值應大於零。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("ln(e)=%f\n", log(M_E)); // M_E is 2.71828..., defined in math.h
        getchar();
        return 0;
      }
相關函數:log10

log10

原型:extern float log10(float x);
用法:#include 
功能:計算x的常用對數。
說明:x的值應大於零。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("lg(5)=%f\n", log10(5.0));
        getchar();
        return 0;
      }
相關函數:log

modf

原型:extern float modf(float num, float *i);
用法:#include 
功能:將浮點數num分解成整數部分和小數部分。
說明:返回小數部分,將整數部分存入*i所指內存中。
舉例:

      #include 
      #include 
      main()
      {
        float x, i;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=modf(-74.12,&i);
        printf("-74.12=%.0f+(%.2f)",i,x);
        getchar();
        return 0;
      }
相關函數:frexp,ldexp

pow10

原型:extern float pow10(float x);
用法:#include 
功能:計算10的x次冪。
說明:相當於pow(10.0,x)。
舉例:

      #include 
      #include 
      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("10^3.2=%f\n",pow10(3.2));
        printf("10^3.2=%f",pow(10,3.2));
        getchar();
        return 0;
      }
相關函數:pow

pow

原型:extern float pow(float x, float y);
用法:#include 
功能:計算x的y次冪。
說明:x應大於零,返回冪指數的結果。
舉例:

      #include 
      #include 
      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("4^5=%f",pow(4.,5.));
        getchar();
        return 0;
      }
相關函數:pow10

sin

原型:extern float sin(float x);
用法:#include 
功能:計算x(弧度表示)的正弦值。
說明:x的值域爲[-1.0,1.0]。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=M_PI/2;        // M_PI=PI=3.14159265..., defined in math.h
        printf("sin(PI/2)=%f",sin(x));
        getchar();
        return 0;
      }
相關函數:asin,acos,atan,atan2,cos,tan

sinh

原型:extern float sinh(float x);
用法:#include 
功能:計算x(弧度表示)的雙曲正弦值。
說明:sinh(x)=(e^x-e^(-x))/2。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=PI/4.;
        printf("sinh(%.4f)=%.4f\n",x,sinh(x));
        getchar();
        return 0;
      }  
相關函數:cosh,tanh

sqrt

原型:extern float sqrt(float x);
用法:#include 
功能:計算x的平方根。
說明:x應大於等於零。
舉例:

      #include 
      #include 
      main()
      {
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        printf("sqrt(2000)=%f",sqrt(2000.0));
        getchar();
        return 0;
      }     
相關函數:無

tan

原型:extern float tan(float x);
用法:#include 
功能:計算x(弧度表示)的正切值。
說明:返回x的正切值。
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=M_PI/4;        // M_PI=PI=3.14159265..., defined in math.h
        printf("tan(PI/4)=%f",tan(x));
        getchar();
        return 0;
      }
相關函數:asin,acos,atan,atan2,sin,cos

tanh

原型:extern float tanh(float x);
用法:#include 
功能:求x的雙曲正切值
說明:tanh(x)=(e^x-e^(-x))/(e^2+e^(-x))
舉例:

      #include 
      #include 
      main()
      {
        float x;
        clrscr();        // clear screen
        textmode(0x00); // 6 lines per LCD screen
        x=PI/4.;
        printf("tanh(%.4f)=%.4f\n",x,tanh(x));
        getchar();
        return 0;
      }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章