FMOD [英] fmod

查看:71
本文介绍了FMOD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码,使用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?

推荐答案

jo ************ @ comcast.net 写道:
jo************@comcast.net wrote:
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?
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


jo * ***********@comcast.net 写道:
此代码,使用visual studio .NET 2003编译,

= 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 -
楼(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));

生成此输出:

95.022000 - floor(95.022000 / 0.010000)* 0.010000 =
0.00200000000000955
95.021000 - floor(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?




因为你的程序中没有任何文字可以表示

完全在二进制浮点数。


请参阅comp.lang.c常见问题解答的第14节,< http://www.c-faq.com/> ;.


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < 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.


感谢Pete。


我认为你的代码基于一个公认的计算算法

浮点模数值。假设fmod实现与

相同,或者类似的东西,fmod将会返回看似远离的东西似乎很奇怪...如果返回价值是

0.00000000000452或类似的东西,很好。但是某些东西

四舍五入到0.01 ......奇怪。


这里是对fmod的定义来自
http://www.opengroup.org/onlinepubs/.../xsh/fmod.html


fmod()函数为某个整数i返回值x - i * y,如果y为非零,则为

,结果与x和幅度相同的符号

小于y的幅度。


严格来说,0.00999999 ...是有效的返回值因为它的价格不到0.01美元......我没有通过计算,但也许

如果我上升一个,那么结果就是小于零,并且它不能为这些操作数返回一个负值,所以不知何故你还剩下

0.00999999 ......


calc.exe做对了< g>

Thanks Pete.

I take it your code is based on an accepted algorithm for computing
floating-point modulus values. Assuming fmod implementations do the
same thing, or something similar, it seems strange that fmod would
return something that seems that "far off"...if the return value was
0.00000000000452 or something like that, fine. but something that
rounds to 0.01...strange.

Here''s a defintion of fmod from
http://www.opengroup.org/onlinepubs/.../xsh/fmod.html

The fmod() function returns the value x - i * y for some integer i such
that, if y is non-zero, the result has the same sign as x and magnitude
less than the magnitude of y.

strictly speaking, 0.00999999... is a valid return value since it''s
less than 0.01...I haven''t cranked through the computation, but maybe
if i went up by one, then the result is less than zero, and it can''t
return a negative value for these operands, so somehow you''re left with
0.00999999...

calc.exe gets it right <g>


这篇关于FMOD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆