C語言math.h中常用函數


1.絕對值
2.取整和取余
3.三角函數
4.反三角函數
5.雙曲三角函數
6.指數和對數
7.標准化浮點數
8.多項式
9.數學錯誤計算處理


1.絕對值
函數原型: int abs(int x);
函數功能: 求整數x的絕對值
int number=-1234;
abs(number);

函數原型:double fabs(double x);
函數功能:求浮點數x的絕對值.
float  number=-1234.0;
fabs(number);


函數原型:double cabs(struct complex znum)
函數功能:求復數的絕對值
參數說明:zuum為用結構struct complex表示的復數,定義如下:
              struct complex
              {
                double m;
                double n;
              }

#include <stdio.h>
#include <math.h>
int main()
{
    struct complex z;
    double val;
    z.x=2.0;
    z.y=1.0;
    val=cabs(z);
    printf("The absolute value of %.2lfi %.2lfj is %.2lf",z.x,z.y,val);
    return 0;
}

2.取整和取余
函數原型:     double ceil(double num)
函數功能:     得到不小於num的最小整數
函數返回:     用雙精度表示的最小整數


函數原型:     double floor(double x);
函數功能:     求出不大於x的最大整數.
函數返回:     該整數的雙精度實數

函數原型:double fmod (double x, double y); 返回兩參數相除x/y的余數,符號與x相同。如果y為0,則結果與具體的額實現有關
 
int main()
{
    double number=123.54;
    double down,up;
    down=floor(number);
    up=ceil(number);
    printf("original number     %10.2lf",number);//123.54
    printf("number rounded down %10.2lf",down);  //123
    printf("number rounded up   %10.2lf",up);    //124
    return 0;
}

函數名稱:     modf
函數原型:     double modf(double val,double *iptr);
函數功能:     把雙精度數val分解為整數部分和小數部分,把整數部分存到iptr指向的單元.
函數返回:     val的小數部分
參數說明:     val 待分解的數
所屬文件:     <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
    double fraction,integer;
    double number=100000.567;
    fraction=modf(number,&integer);
    printf("The whole and fractional parts of %lf are %lf and %lf",number,integer,fraction);
    return 0;
}


3.三角函數
函數原型:     double sin(double x);
函數功能:     計算sinx的值.正弦函數

函數原型:     double cos(double x);
函數功能:     計算cos(x)的值.余弦函數.

函數原型:     double tan(double x);
函數功能:     計算tan(x)的值,即計算角度x的正切數值

@函數名稱:     hypot
函數原型:     double hypot(double x,double y)
函數功能:     已知直角三角形兩個直角邊長度,求斜邊長度
函數返回:     斜邊長度
參數說明:     x,y-直角邊長度
所屬文件:     <math.h>

#include <stdio.h>
#include <math.h>
int main()
{
    double result;
    double x=3.0;
    double y=4.0;
    result=hypot(x,y);
    printf("The hypotenuse is: %lf",result);
    return 0;
}
4.反三角函數
函數原型:     double asin(double x);
函數功能:     計算sin^-1(x)的值.反正弦值函數

函數原型:     double acos(double x);
函數功能:     計算cos^-1(x)的值,反余弦函數

函數原型:     double atan(double x);
函數功能:     計算tan^-1(x)的值.

函數原型:     double atan2(double x,double y);
函數功能:     計算tan^-1/(x/y)的值.求x/y的反正切值.

5.雙曲三角函數
函數原型:     double sinh(double x);
函數功能:     計算x的雙曲正弦函數sinh(x)的值.

函數原型:     double cosh(double x);
函數功能:     計算x的雙曲余弦cosh(x)的值.

函數原型:     double tanh(double x);
函數功能:     計算x的雙曲正切函數tanh(x)的值.

#include <stdio.h>
#include <math.h>
int main()
{
    double result,x=0.5;
    result=sin(x);
    printf("The sin() of %lf is %lf",x,result);
    return 0;
}


#include <stdio.h>
#include <math.h>
int main()
{
    double result;
    double x=0.5;
    result=cosh(x);
    printf("The hyperboic cosine of %lf is %lf",x,result);
    return 0;
}

6.指數和對數
函數原型:     double exp(double x);
函數功能:     求e的x次冪

函數原型:     double fmod(double x,double y);
函數功能:     求整數x/y的余數

函數原型:    double frexp(double val,int *eptr);
函數功能:    把雙精度數val分解為數字部分(尾數)x和以2為底的指數n,即val=x*2^n,n存放在eptr指向的變量中.

函數名稱:     pow
函數原型:     double pow(double x,double y);
函數功能:     計算以x為底數的y次冪,即計算x^y的值.
函數返回:     計算結果
參數說明:     x-底數,y-冪數
所屬文件:     <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
    double x=2.0,y=3.0;
    printf("%lf raised to %lf is %lf",x,y,pow(x,y));
    return 0;
}

函數原型:     double sqrt(double x);
函數功能:     計算x的開平方.
函數返回:     計算結果
參數說明:     x>=0
所屬文件:     <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
    double x=4.0,result;
    result=sqrt(x);
    printf("The square root of %lf is %lf",x,result);
    return 0;
}

//

log(10)   以 e 為底的 10 的對數;

log10(100)   以 10 為底的 100 的對數;

如果要算別的對數 log(8) / log(2)    以 2 為底的 8 的對數;

如果要計算自然常數 e     exp(1);

//
函數原型:     double log(double x);
函數功能:     求logeX(e指的是以e為底),即計算x的自然對數(ln X)
函數返回:     計算結果
參數說明:
所屬文件:     <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
    double result;
    double x=8.6872;
    result=log(x);
    printf("The natural log of %lf is %lf",x,result);
    return 0;
}

函數名稱:     log10
函數原型:     double log10(double x);
函數功能:     求log10x(10指的是以10為底).計算x的常用對數
函數返回:     計算結果
參數說明:
所屬文件:     <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
    double result;
    double x=800.6872;
    result=log10(x);
    printf("The common log of %lf is %lf",x,result);
    return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
    double result;
    double x=4.0;
    result=exp(x);
    printf("'e' raised to the power of %lf(e^%lf)=%lf",x,x,result);
    return 0;
}


#include <math.h>
#include <stdio.h>
int main()
{
    double mantissa,number;
    int exponent;
    number=8.0;
    mantissa=frexp(number,&exponent);
    printf("The number %lf is",number);
    printf("%lf times two to the",mantissa);
    printf("power of %d",exponent);
    return 0;
}


7.標准化浮點數

函數原型:double modf (double x, double *ip); 
函數功能:將參數的整數部分通過指針回傳, 返回小數部分,整數部分保存在*ip中

函數原型: double ldexp(double x,int exponent)
函數功能: 計算x*2的exponent次冪,即2*pow(2,exponent)的數值


#include <stdio.h>
#include <math.h>
int main()
{
    double value;
    double x=2;
    value=ldexp(x,3);
    printf("The ldexp value is: %lf",value);
    return 0;
}


8.多項式

函數名稱:     poly
函數原型:     double poly(double x,int degree,double coeffs[])
函數功能:     計算多項式
函數返回:     多項式的計算結果
參數說明:     計算c[n]*x^n+c[n-1]x^n-1+.....+c[1]*x+c[0]
所屬文件:     <math.h>

#include <stdio.h>
#include <math.h>
int main()
{
    double array[]={-1.0,5.0,-2.0,1.0};
    double result;
    result=poly(2.0,3,array);
    printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lf",result);
    return 0;
}


9.數學錯誤計算處理
@函數名稱:     matherr
函數原型:     int matherr(struct exception *e)
函數功能:     數學錯誤計算處理程序
函數返回:
參數說明:     該函數不能被直接調用,而是被庫函數_matherr()調用
所屬文件:     <math.h>

#include<math.h>
int matherr(struct exception *a)
{
    return 1;
}
原文:https://blog.csdn.net/weibo1230123/article/details/81352581


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM