淡泊明志 宁静致远
分类: C/C++
2006-12-08 13:49:07
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
将浮点数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;
}