问题描述
此代码,使用visual studio .NET 2003编译,
double a = 95.022,b = 0.01;
printf("%lf - 楼层(%lf /%lf)*%lf =%。17lf \ n,a,a,b,b,a -
楼层(a / b)* b);
a = 95.021,b = 0.01;
printf("%lf - floor(%lf /%lf)*%lf =%。17lf \ n",a, a,b,b,a -
楼(a / b)* b);
a = 95.020,b = 0.01;
printf ("%lf - floor(%lf /%lf)*%lf =%。17lf \ n",a,a,b,b,a -
楼(a / b) * b);
a = 95.022,b = 0.01;
printf(" fmod(%lf,%lf)=%。17lf \ n" ;,a,b,fmod(a,b));
a = 95.021,b = 0.01;
printf(" fmod(%lf,%lf)= %。17lf \ n",a,b,fmod(a,b));
a = 95.020,b = 0.01;
printf(" fmod(%) lf,%lf)=%。17lf \ n",a,b,fmod(a,b));
生成此输出:
95.022000 - floor(95.022000 / 0.010000)* 0.010000 =
0.00200000000000955
95.021000 - 楼层(95.021000 / 0.010000)* 0.010000 =
0.00100000000000477
95.020000 - 楼层(95.020000 / 0.010000)* 0.010000 =
0.00000000000000000
fmod(95.022000,0.010000)= 0.00200000000000359
fmod(95.021000,0.010000)= 0.00099999999999882
fmod(95.020000,0.010000)= 0.00999999999999404
一切都有意义,除了最后一行。为什么fmod返回
0.01而不是0.0?
This code, compiled with visual studio .NET 2003,
double a = 95.022, b = 0.01;
printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a -
floor(a / b) * b);
a = 95.021, b = 0.01;
printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a -
floor(a / b) * b);
a = 95.020, b = 0.01;
printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a -
floor(a / b) * b);
a = 95.022, b = 0.01;
printf ("fmod(%lf, %lf) = %.17lf\n", a, b, fmod(a, b));
a = 95.021, b = 0.01;
printf ("fmod(%lf, %lf) = %.17lf\n", a, b, fmod(a, b));
a = 95.020, b = 0.01;
printf ("fmod(%lf, %lf) = %.17lf\n", a, b, fmod(a, b));
generates this output:
95.022000 - floor(95.022000 / 0.010000) * 0.010000 =
0.00200000000000955
95.021000 - floor(95.021000 / 0.010000) * 0.010000 =
0.00100000000000477
95.020000 - floor(95.020000 / 0.010000) * 0.010000 =
0.00000000000000000
fmod(95.022000, 0.010000) = 0.00200000000000359
fmod(95.021000, 0.010000) = 0.00099999999999882
fmod(95.020000, 0.010000) = 0.00999999999999404
everything makes sense, except for the last line. why does fmod return
0.01 instead of 0.0?
推荐答案
我不知道。
我得到的结果与我相似自制fmod。
/ * BEGIN new.c输出* /
fs_fmod(95.022000,0.010000)是0.002000
fs_fmod(95.021000,0.010000)是0.001000
fs_fmod(95.020000,0.010000)是0.010000
/ * END new.c输出* /
/ * BEGIN new.c * /
#include< stdio.h>
#include< float .h>
double fs_fmod(double x,double y);
int main(无效)
{
puts(" / * BEGIN new.c output * / \ n");
printf(" fs_fmod(95.022000,0.010000)is%f \\ \\ n"
,fs_fmod(95.022000,0.010000));
printf(" fs_fmod(95.021000,0.010000)is%f\ n"
,fs_fmod(95.021000,0.010000));
printf(" fs_fmod(95.020000,0.010000)是%f \\ n n"
,fs_fmod(95.020000,0.010000));
puts(" \ n / * END new.c output * /");
返回0;
}
double fs_fmod(double x,double y)
{
加倍a,b;
const double c = x;
if(0> c){
x = -x;
}
if(0> y){
y = - y;
}
if(y!= 0&& DBL_MAX> = y&& DBL_MAX> = x){
while(x> = y){
a = x / 2;
b = y;
while(a> = b){
b * = 2;
}
x - = b;
}
} else {
x = 0;
}
返回0> C ? -x:x;
}
/ * END new.c * /
-
pete
I don''t know.
I get similar results with my homemade fmod.
/* BEGIN new.c output */
fs_fmod(95.022000, 0.010000) is 0.002000
fs_fmod(95.021000, 0.010000) is 0.001000
fs_fmod(95.020000, 0.010000) is 0.010000
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
#include <float.h>
double fs_fmod(double x, double y);
int main(void)
{
puts("/* BEGIN new.c output */\n");
printf("fs_fmod(95.022000, 0.010000) is %f\n"
, fs_fmod(95.022000, 0.010000));
printf("fs_fmod(95.021000, 0.010000) is %f\n"
, fs_fmod(95.021000, 0.010000));
printf("fs_fmod(95.020000, 0.010000) is %f\n"
, fs_fmod(95.020000, 0.010000));
puts("\n/* END new.c output */");
return 0;
}
double fs_fmod(double x, double y)
{
double a, b;
const double c = x;
if (0 > c) {
x = -x;
}
if (0 > y) {
y = -y;
}
if (y != 0 && DBL_MAX >= y && DBL_MAX >= x) {
while (x >= y) {
a = x / 2;
b = y;
while (a >= b) {
b *= 2;
}
x -= b;
}
} else {
x = 0;
}
return 0 > c ? -x : x;
}
/* END new.c */
--
pete
因为你的程序中没有任何文字可以表示
完全在二进制浮点数。
请参阅comp.lang.c常见问题解答的第14节,< http://www.c-faq.com/> ;.
-
Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
Because none of the literals in your program can be represented
exactly in binary floating-point.
See section 14 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于FMOD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!