淡泊明志 宁静致远
分类: C/C++
2006-12-08 12:44:04
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
计算x/y的余数。返回x-n*y,符号同y。
n=[x/y](向离开零的方向取整)
*/
double my_fmod01(double x, double y)
{
register double ret;
__asm__(
"1: fprem\n\t"
"fstsw %%ax\n\t"
"sahf\n\t"
"jp 1b"
: "=t" (ret)
: "0" (x), "u" (y)
: "ax", "cc"
);
return ret;
}
double my_fmod02(double x, double y)
{
double temp,
ret;
if (y == 0.0)
return
0.0;
temp = floor(x/y);
ret = x - temp * y;
if ((x <
0.0) != (y < 0.0))
ret = ret - y;
return ret;
}
int main()
{
double x,y;
x = 80.8,y = 3.0;
printf("fmod(%f,%f) = %f\n",x,y,fmod(x,y));
printf("my_fmod01(%f,%f) =
%f\n",x,y,my_fmod01(x,y));
printf("my_fmod02(%f,%f) =
%f\n",x,y,my_fmod01(x,y));
printf("\n******************************************\n");
x = -55.968,y = 8.8;
printf("fmod(%f,%f) = %f\n",x,y,fmod(x,y));
printf("my_fmod01(%f,%f) =
%f\n",x,y,my_fmod01(x,y));
printf("my_fmod02(%f,%f) =
%f\n",x,y,my_fmod01(x,y));
system("pause");
return 0;
}