⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 是关于变结构控制的一个简单实例
💻 TXT
📖 第 1 页 / 共 3 页
字号:
/*

   将浮点数x分解成整数部分和小数部分。

   返回小数部分,将整数部分存入* iptr所指内存中。

*/

double my_modf01(double x, double *iptr) 

{

   double ret = fmod(x,1.0);

   *iptr = x - ret;

   return ret;

}//这个函数算法比较简单,也容易让人理解。

 

//下面的这个函数理解起来就有点困难了。

 

typedef struct 

{

   unsigned int mantissal:32;

   unsigned int mantissah:20;

   unsigned int exponent:11;

   unsigned int sign:1;

}double_t;//这个结构体在IEEE.h定义。

double my_modf02(double x, double *y)

{

   double_t * z = (double_t *)&x;

   double_t * iptr = (double_t *)y;

 

   int j0;

   unsigned int i;

   j0 = z->exponent - 0x3ff;  /* exponent of x */  

   if(j0<20)

   {/* integer part in high x */

      if(j0<0) 

      {                  /* |x|<1 */

        *y = 0.0;

        iptr->sign = z->sign;

        return x;

      } 

      else 

      {

        if ( z->mantissah == 0 && z->mantissal == 0 ) 

        {

           *y = x;

           return 0.0;

        }

        i = (0x000fffff)>>j0;

        iptr->sign = z->sign;

        iptr->exponent = z->exponent;

        iptr->mantissah = z->mantissah&(~i);

        iptr->mantissal = 0;

        if ( x == *y ) 

        {

           x = 0.0;

           z->sign = iptr->sign;

           return x;

        }          

              return x - *y;        

      }

   } 

   else if (j0>51) 

   {             /* no fraction part */

      *y = x; 

      if ( isnan(x) || isinf(x) )

        return x;

      x = 0.0;

      z->sign = iptr->sign;

      return x;

   } 

   else 

   {                        /* fraction part in low x */

      i = ((unsigned)(0xffffffff))>>(j0-20);

      iptr->sign = z->sign;

      iptr->exponent = z->exponent;

      iptr->mantissah = z->mantissah;

      iptr->mantissal = z->mantissal&(~i);

      if ( x == *y ) 

      {

        x = 0.0;

        z->sign = iptr->sign;

        return x;

      }

      return x - *y;        

   }

}

//下面是两个要用到的函数

int isnan(double d)

{

   union 

   {

      unsigned long long l;

      double d;

   } u;

   u.d=d;

   return (u.l==0x7FF8000000000000ll || u.l==0x7FF0000000000000ll || u.l==0xfff8000000000000ll);

}

int isinf(double d) 

{

   union 

   {

      unsigned long long l;

      double d;

   } u;

   u.d=d;

   return (u.l==0x7FF0000000000000ll?1:u.l==0xFFF0000000000000ll?-1:0);

}

 

int main()

{

   double x,y;

   

   x = 12345678901.1234567;

 

   printf("%f = (%f) + (%f) \n",x,y,modf(x,&y));

   printf("%f = (%f) + (%f) \n",x,y,my_modf01(x,&y));

   printf("%f = (%f) + (%f) \n",x,y,my_modf02(x,&y));

   

   printf("\n******************************************\n");

   

   printf("%f = (%f) + (%f) \n",-x,y,modf(-x,&y));

   printf("%f = (%f) + (%f) \n",-x,y,my_modf01(-x,&y));

   printf("%f = (%f) + (%f) \n",-x,y,my_modf02(-x,&y));

   

   system("pause");

   return 0;

}

【C语言库函数源代码】

【本程序在Dev C++ 4.9.9.2 下编译通过】


/*

   hypot函数对于给定的直角三角形的两个直角边,

   求其斜边的长度。

*/

 

//一般的常规算法:

 

double my_hypot01(double x, double y)

{

   double hypotenuse;

   x = fabs(x);

   y = fabs(y);

   if (x < y) 

    {

      double temp = x;

      x = y;

      y = temp;

   }

   if (x == 0.)

      return 0.;

   else 

    {

      hypotenuse = y/x;

      return x*sqrt(1.+hypotenuse*hypotenuse);

   }

}

#define __SQRT_DBL_MAX 1.3e+154

#define __SQRT_DBL_MIN 2.3e-162

 

double my_hypot02(double x, double y)

{

   double ratio;

   double r, t, s, p, q;

   

   x = fabs(x), y = fabs(y);

   

 

   if (x < y)

   {

      double temp = x;

      x = y;

      y = temp;

   }//保持x 是两个直角边中较长的边,y是较短的边。

 

   if (y == 0.)

      return x;

   /*

      主要考虑的是当x很大而y很小,那么它们的平方和将会造成

      丢失小数的现象。首先要判断y是否是太小,x是不是太大。

      如果出现这种情况则用,第一个公式来处理。其他的则用

      这样可以让求出的斜边更加精确一些。

   */

   if ((ratio = y / x) > __SQRT_DBL_MIN && x < __SQRT_DBL_MAX)

      return x * sqrt(1. + ratio*ratio);

   else

   {//使用3次迭代是增加精确度。

      r = ratio*ratio, p = x, q = y;

      do 

      {

        t = 4.+ r;

        if (t == 4.)

           break;

        s = r / t;

        p += 2. * s * p;

        q *= s;

        r = (q / p) * (q / p);

      } while (1);

      return p;

   }

}

struct complex

{

   double x;

   double y;

}

double cabs(struct complex x)

{

   return hypot(z.x,z.y);

}//hypot 函数的封装(这里不再举调用的例子了。) 

 

# define DBL_MAX 1.79769313486231e+308

# define DBL_MIN 2.22507385850721e-308

int main(void)

{  

   printf("hypot(3, 4)                    =%25.17e\n",hypot(3., 4.));

   printf("hypot(3*10^150,4*10^150)       =%25.17g\n",hypot(3.e+150, 4.e+150));

   printf("hypot(3*10^306,4*10^306)       =%25.17g\n",hypot(3.e+306, 4.e+306));

   printf("hypot(3*10^-320,4*10^-320)     =%25.17g\n",hypot(3.e-320, 4.e-320));

   printf("hypot(0.7*DBL_MAX,0.7*DBL_MAX) =%25.17g\n",hypot(0.7*DBL_MAX,0.7*DBL_MAX));

   printf("hypot(DBL_MAX, 1.0)            =%25.17g\n",hypot(DBL_MAX, 1.0));

   printf("hypot(1.0, DBL_MAX)            =%25.17g\n",hypot(1.0, DBL_MAX));

   printf("hypot(0.0, DBL_MAX)            =%25.17g\n",hypot(0.0, DBL_MAX));

   printf("\n************************************************************\n");

   printf("hypot(3, 4)                    =%25.17e\n",my_hypot01(3., 4.));

   printf("hypot(3*10^150,4*10^150)       =%25.17g\n",my_hypot01(3.e+150, 4.e+150));

   printf("hypot(3*10^306,4*10^306)       =%25.17g\n",my_hypot01(3.e+306, 4.e+306));

   printf("hypot(3*10^-320,4*10^-320)     =%25.17g\n",my_hypot01(3.e-320, 4.e-320));

   printf("hypot(0.7*DBL_MAX,0.7*DBL_MAX) =%25.17g\n",my_hypot01(0.7*DBL_MAX,0.7*DBL_MAX));

   printf("hypot(DBL_MAX, 1.0)            =%25.17g\n",my_hypot01(DBL_MAX, 1.0));

   printf("hypot(1.0, DBL_MAX)            =%25.17g\n",my_hypot01(1.0, DBL_MAX));

   printf("hypot(0.0, DBL_MAX)            =%25.17g\n",my_hypot01(0.0, DBL_MAX));

 

 printf("\n************************************************************\n");

   printf("hypot(3, 4)                    =%25.17e\n",my_hypot02(3., 4.));

   printf("hypot(3*10^150,4*10^150)       =%25.17g\n",my_hypot02(3.e+150, 4.e+150));

   printf("hypot(3*10^306,4*10^306)       =%25.17g\n",my_hypot02(3.e+306, 4.e+306));

   printf("hypot(3*10^-320,4*10^-320)     =%25.17g\n",my_hypot02(3.e-320, 4.e-320));

   printf("hypot(0.7*DBL_MAX,0.7*DBL_MAX) =%25.17g\n",my_hypot02(0.7*DBL_MAX,0.7*DBL_MAX));

   printf("hypot(DBL_MAX, 1.0)            =%25.17g\n",my_hypot02(DBL_MAX, 1.0));

   printf("hypot(1.0, DBL_MAX)            =%25.17g\n",my_hypot02(1.0, DBL_MAX));

   printf("hypot(0.0, DBL_MAX)            =%25.17g\n",my_hypot02(0.0, DBL_MAX));

   system("pause");

   return 0;

} 

int abs(int n);
返回整型变量的绝对值。
In addition to the prototype already specified for this function, 
ANSI-C++ standard incorporates an overloaded version:
    long abs (long n );
with the same functionality as labs. 
///////////////////////////////////////////////////////////////////////////
long labs(long n);
///////////////////////////////////////////////////////////////////////////
double fabs(double x);
///////////////////////////////////////////////////////////////////////////
double sin(double x);
ANSI-C++增加了float和double重载的版本
///////////////////////////////////////////////////////////////////////////
double cos(double x);
ANSI-C++增加了float和double重载的版本
/* cos example */
#include 
#include 

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 45;
  result = cos (param*PI/180);
  printf ("Cosine of %lf degrees is %lf\n", param, result );
  return 0;
}

Output:
Cosine of 45.000000 degrees is 0.707107 
///////////////////////////////////////////////////////////////////////////
double tan(double x);
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types
///////////////////////////////////////////////////////////////////////////
double asin(double x);
计算反三角 x在-1和+1之间。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types. 
///////////////////////////////////////////////////////////////////////////
double acos(double x);
计算反三角 x在-1和+1之间。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types. 
///////////////////////////////////////////////////////////////////////////
double atan(double x);
计算反三角。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types. 
///////////////////////////////////////////////////////////////////////////
double atan2(double y,double x);
计算角度。根据y/x得到结果范围为-PI到PI
///////////////////////////////////////////////////////////////////////////
double atof(const char * string);
使用浮点数代表的字符串。数字,符号和E和e都作为有效数字的一部分。
格式通常为[whitespaces][+|-][nnnn][.nnn][e|E[+|-]nnnn]
如果发生错误返回0.0
/* atof example: sines calculator */
#include 
#include 

int main ()
{
  double n,m;
  double pi=3.1415926535;
  char szInput [256];
  printf ( "Enter degrees: " );
  gets ( szInput );
  n = atof ( szInput );
  m = sin (n*pi/180);
  printf ( "sine of %f degrees is %f\n" , n, m );
  return 0;
}

Output:
Enter degrees: 45
sine of 45.000000 degrees is 0.707101 
///////////////////////////////////////////////////////////////////////////
double ceil(double x);
返回大于或等于x的最小的整数。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types
/* ceil example */
#include 
#include 

int main ()
{
  printf ("ceil of 2.3 is %.1lf\n", ceil (2.3) );
  printf ("ceil of 3.8 is %.1lf\n", ceil (3.8) );
  printf ("ceil of -2.3 is %.1lf\n", ceil (-2.3) );
  printf ("ceil of -3.8 is %.1lf\n", ceil (-3.8) );
  return 0;
}

Output:
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
///////////////////////////////////////////////////////////////////////////
double floor(double x);
返回小于或等于x的最大整数。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types
///////////////////////////////////////////////////////////////////////////
double fmod(double x,double y);
返回x/y的余
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types
/* fmod example */
#include 
#include 

int main ()
{
  printf ("fmod of 5.3 / 2 is %lf\n", fmod (5.3,2) );
  printf ("fmod of 18.5 / 4.2 is %lf\n", fmod (18.5,4.2) );
  return 0;
}

Output:
fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000
///////////////////////////////////////////////////////////////////////////
double exp(double x);
返回x的自然指数。
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter and result, of one of 
these types
/* exp example */
#include 
#include 

int main ()
{
  double param, result;
  param = 5;
  result = exp (param);
  printf ("Exponential of %lf is %lf\n", param, result );
  return 0;
}
///////////////////////////////////////////////////////////////////////////
double ldexp(double x,int exp);
根据给定的指数和尾数得到 x * 2^exp
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter x and return value, of 
one of these types.
/* ldexp example */
#include 
#include 

int main ()
{
  double param, result;
  int n;

  param = 0.95;
  n = 4;
  result = ldexp (param , n);
  printf ("%f * 2^%d = %f\n", param, n, result);
  return 0;
}

Output:
0.950000 * 2^4 = 15.200000 
///////////////////////////////////////////////////////////////////////////
double frexp(double x,int * exp);
余数(在0.5到1之间)和指数(一个整数)的计算值。x = mantissa * 2^exponent
mantissa是被返回的值。
/* frexp example */
#include 
#include 

int main ()
{
  double param, result;
  int n;

  param = 15.2;
  result = frexp (param , &n);
  printf ("%f * 2^%d = %f\n", result, n, param);
  return 0;
}

Output:
0.950000 * 2^4 = 15.200000 
///////////////////////////////////////////////////////////////////////////
double modf(double x,double * ipart);
将x分解为两部分。整数部分存储在ipart,小数部分作为返回值
ANSI-C++ adds float and double overloaded versions of this function, 
with the same bahavior but being both, parameter x and return value, of 
one of these types. 
/* modf example */
#include 
#include 

int main ()
{
  double param, fractpart, intpart;

  param = 3.14159265;
  fractpart = modf (param , &intpart);
  printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
  return 0;
}

Output:
3.141593 = 3.000000 + 0.141593 
///////////////////////////////////////////////////////////////////////////
double log(double x);
返回x的自然对数。
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types
/* log example */
#include 
#include 

int main ()
{
  double param, result;
  param = 5.5;
  result = log (param);
  printf ("ln(%lf) = %lf\n", param, result );
  return 0;
}

Output:
ln(5.500000) = 1.704748 
///////////////////////////////////////////////////////////////////////////
double log10(double x);
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types.
///////////////////////////////////////////////////////////////////////////
double pow(double x,double y);
返回x^y
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being all of them, parameters and return value, of 
one of these types
///////////////////////////////////////////////////////////////////////////
double aqrt(double x);
返回x的平方根。
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types
///////////////////////////////////////////////////////////////////////////
double sinh(double x);
返回x的双曲正弦
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types
* sinh example */
#include 
#include 

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 45;
  result = cosh (param*PI/180);
  printf ("Hyperbolic sine of %lf degrees is %lf\n", param, result );
  return 0;
}

Output:
Hyperbolic sine of 45.000000 degrees is 0.868671 
///////////////////////////////////////////////////////////////////////////
double cosh(double x);
计算x的双曲余弦
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types
/* cosh example */
#include 
#include 

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 45;
  result = cosh (param*PI/180);
  printf ("Hyperbolic cosine of %lf degrees is %lf\n", param, result );
  return 0;
}

Output:
Hyperbolic cosine of 45.000000 degrees is 1.324609 
///////////////////////////////////////////////////////////////////////////
double tanh(double x);
计算x的双曲正切
ANSI-C++ adds float and double overloaded versions of this function, with 
the same bahavior but being both, parameter and result, of one of these 
types
/* tanh example */
#include 
#include 

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 45;
  result = tanh (param*PI/180);
  printf ("Hyperbolic tangent of %lf degrees is %lf\n", param, result );
  return 0;
}
Output:
Hyperbolic tangent of 45.000000 degrees is 0.655794 
1.int abs(int x)

这函数呢,就是求整数x的绝对值.返回的是计算出来的结果.其中有一个函数,double fabs(double x),这也是求绝对值的函数,作用不止止用于整数哦.

2.double exp(double x)

这是求e的x次方的值,也就是e^x.

3.double pow(double x,double y)

求的是x的y次方的值,就是x^y.说起这个函数,我就记得以前大学时候的一件事.那时候老师让我们写一个计数器程序,结果用到x的y次方的时候,很多同学都写成:int n=x^y;当然结果是错误的了.这就是对库函数不熟悉所造成的.

4.int rand(void)

这是随机函数,产生的是从-90到32767的随机值,用这函数的时候记得要给随机种子哦,要不得出的不是真正的随机数.产生随机种子可以用srand((unsigned int)time(NULL));这就是由时间产生的随机种子了.

5.double sqrt(double x)

这个大家应该很熟悉吧?经常用到的开方函数了.没什么好说的呵呵.

6.double floor(double x)

求出的是不大于x的最大的整数.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -