C语言常用数学函数及其用法

C语言常用数学函数及其用法


转帖地址:http://old.blog.edu.cn/user5/282721/archives/2008/2098559.shtml



三角函数:(所有参数必须为弧度)
 
 1.acos


  函数申明:acos  (double x);
   用途:用来返回给定的 X 的反余弦函数。


 2.asin


   函数申明:asin  (double x);
   用途:用来返回给定的 X 的反正弦函数。


 3.atan 


   函数申明:atan  (double x);
   用途:用来返回给定的 X 的反正切函数。


 4.sin


   函数声明:sin   (double x);
   用途:用来返回给定的 X 的正弦值。


 5.cos


   函数声明:cos   (double x);
   用途:用来返回给定的 X 的余弦值。


 6.tan


   函数声明:tan   (double x);
   用途:用来返回给定的 X 的正切值。


 7.atan2


   函数声明:atan2 (double y, double x);
   用途:返回给定的 X 及 Y 坐标值的反正切值
 
其他函数:


 8.atof 


  函数名: atof  (const char *s);
  功  能: 把字符串转换成浮点数
  用  法: double atof(const char *nptr);
  程序例:
   #i nclude <stdlib.h>
   #i nclude <stdio.h>


   int main(void)
   {


    float arg,*point=&arg;
    float f;
    char *str = "12345.67";


    f = atof(str);
    printf("string = %s float = %f\n", str, f);
    return 0;
   }


 9. ceil  和 floor


   函数名: ceil  
                 floor 
   功  能: 向上舍入
        向下舍入
   用  法: double ceil(double x);
        double floor(double x);
   程序例:


   #i nclude<math.h>


   int main(void)
   {
    double number = 123.54;
    double down, up;


    down = floor(number);
    up = ceil(number);


    printf("original number     %5.2lf\n", number);
    printf("number rounded down %5.2lf\n", down);
    printf("number rounded up   %5.2lf\n", up);


    return 0;
  }该程序运行结果:original number     123.54
                   number rounded down 123.00
                   number rounded up   124.00
 


 10.fabs


    函数名:fabs
    功能:求浮点数x的绝对值.
    用法:fabs  (double x);


 11.fmod


    函数名: fmod
    功  能: 计算x对y的模, 即x/y的余数
    用  法: double fmod(double x, double y);
    程序例:


    #i nclude <stdio.h>
    #i nclude <math.h>


    int main(void)
    {
     double x = 5.0, y = 2.0;
     double result;


     result = fmod(x,y);
     printf("The remainder of (%lf / %lf) is \
          %lf\n", x, y, result);
     return 0;
    }


 12.abs


   函数名:abs
   功能:返回整型数的绝对值.
   用法:Abs(number)
        number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0.


幂指数:


 13.exp


    函数名:exp
    功能:返回 e 的 n 次幂.
    用法:exp   (double x);


 14.frexp


    函数名: frexp
    功  能: 把一个双精度数分解为尾数的指数
    用  法: double frexp(double value, int *eptr);
    程序例:


    #i nclude <math.h>
    #i nclude <stdio.h>


    int main(void)
    {
      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\n", exponent);
      return 0;
    }


 15.log


    函数名:log
    功 能: 自然对数函数ln(x) 
    用 法: double log(double x); 
    程序例:


    #i nclude <math.h>
    #i nclude <stdio.h>
    int main(void) 
    { 
     double result; 
     double x = 8.6872; 
     result = log(x); 
     printf("The natural log of %lf is %lf\n", x, result); 
     return 0; 
    }


   log(x,y)=ln(y)/ln(x)


 16.ldexp


    函数名: ldexp 
    功 能: 计算value*(2的exp幂 ).
    用 法: double ldexp(double value, int exp); 
    程序例: 
    #i nclude 
    #i nclude 
    int main(void) 
    { 
     double value; 
     double x = 2; 
/* ldexp raises 2 by a power of 3 then multiplies the result by 2 */ 
     value = ldexp(x,3); 
     printf("The ldexp value is: %lf\n", value); 
     return 0; 
    } 运行结果为:2*2^3=16.
 
 17.log10


    函数名:log10
    功能:返回以 10 为底的对数.
    用法:log10 (double x);


 18.sqrt


    函数名:sqrt
    功能:返回指定数字的平方根.
    用法:sqrt  (double x);


 19.modf


    函数名:modf
    功  能: 把数分为指数和尾数
    用  法: double modf(double value, double *iptr);
    程序例:
    #i nclude <math.h>
    #i nclude <stdio.h>
    int main(void)
   {
    double fraction, integer;
    double number = 100000.567;
    fraction = modf(number, &integer);
    printf("The whole and fractional parts of %lf are %lf and %lf\n",number, integer, fraction);
    return 0;
   }


 20.pow


    函数名:pow
    功能:返回指定数字的指定次幂.
    用法:pow   (double x, double y);(将返回x的y次幂)
 
双曲函数:
 
 21.cosh


    函数名:cosh 
    功能:返回指定角度的双曲余弦值.
    用法:Double  Cosh(double x(以弧度计量的角度)) ;


 22.sinh


    函数名:sinh
    功能:返回指定角度的双曲正弦值。
    用法:sinh  (double x);(其中参数x必须为弧度制)
 
 23.tanh


     函数名:tanh
    功能:回指定角度的双曲正切值.

    用法:tanh  (double x);


[补充]C语言常用数学函数及其用法
yuyoutan 发表于 - 2008-4-18 19:27:00
 


C库函数中没有求平方的函数,因为求平方实在是太简单了:我们可以通过以下俩种方法来求:


#i nclude<stdio.h>
main()
{double squar,x;
scanf("%lf",&x);
squar=x*x;
printf("the squar of %lf is %lf\n",x,squar);
}


如果觉得麻烦的话,你可以把它直接定义一个自己的函数,然后以后要用的话,直接引用就行.
#i nclude<stdio.h>
double SQUAR(double a)
{double squar;
squar=a*a;
return squar;
}
main()
{double squar,x;
scanf("%lf",&x);
squar=SQUAR(x);
printf("the squar of %lf is %lf\n",x,squar);
}


 


C中也没有给出如何求解log(a)[b];对于这个,我们可以利用数学关系来求,即换底公式


       log(a)[b]=log(e)[b]/ log(e)[a]        (由于C库函数中含有求自然对数的函数 log()


我们同样可以把它定义为自己的函数,以便以后直接引用:


#i nclude<stdio.h>
#i nclude<math.h>
double LOG(double x,double y)
{double result;
 result=log(y)/log(x);
 return(result);
}
main()
{double a,b,value;
 scanf("%lf,%lf",&a,&b);
 value=LOG(a,b);
 printf("\nthe value is:%lf\n",value);
}


至于如何将自己所定义的函数保留下来以便随时都可以调用,只需要将功能函数保存为*.h文件,并将之保存到C标准库函数的相应文件夹中,就可以了..................


其实C标准库中有很多数学函数并没有囊括在内,我们都可以自己定义与之对应的函数.然后将之添加到C库中,以便自己以后的使用方便.

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值