淡泊明志 宁静致远
分类: C/C++
2006-12-05 17:54:55
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
double my_floor(double x)
{
register double ret;
unsigned short int temp1,
temp2;
__asm__("fnstcw %0" :
"=m" (temp1));
temp2 = (temp1 & 0xf3ff) | 0x0400; /* rounding down */
__asm__ ("fldcw %0" : :
"m" (temp2));
__asm__ ("frndint" : "=t"
(ret) : "0" (x));
__asm__ ("fldcw %0" : :
"m" (temp1));
return ret;
}//向下取整
//下面是俺自己写的向下取整的函数
double my_floor01(double x)
{
double y=x;
if( (*( ( (int *) &y)+1) & 0x80000000) != 0) //或者if(x<0)
return
(float)((int)x)-1;
else
return
(float)((int)x);
}
int main()
{
double a =
88.8;
printf("floor(%f) =
%f\n",a,my_floor(a));
a = -88.8;
printf("floor(%f) =
%f\n",a,my_floor(a));
printf("****************************\n");
a = 88.8;
printf("floor(%f) =
%f\n",a,my_floor01(a));
a = -88.8;
printf("floor(%f) =
%f\n",a,my_floor01(a));
system("pause");
return 0;
}