Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple fmod-Question

Reply
Thread Tools

Simple fmod-Question

 
 
Maic Schmidt
Guest
Posts: n/a
 
      11-28-2008
Hi,

2 mod 0.2 should be 0, right?
But it isnt:

#include <math.h>
#include <stdio.h>

int main(void)
{
double x=2;
double y=0.2;
printf("%g mod %g = %g\n",x,y,fmod(x,y));
return 0;
}
=> 2 mod 0.2 = 0.2

Why not?

(Visual Studio 2005/2003 on XP/W2K)


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-28-2008
Maic Schmidt wrote:

> Hi,
>
> 2 mod 0.2 should be 0, right?
> But it isnt:
>
> #include <math.h>
> #include <stdio.h>
>
> int main(void)
> {
> double x=2;
> double y=0.2;
> printf("%g mod %g = %g\n",x,y,fmod(x,y));
> return 0;
> }
> => 2 mod 0.2 = 0.2
>
> Why not?


Maybe, because y is not exactly 0.2. As I see, you are printing results with
six significant digits. This may not be enough to see the differences.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      11-28-2008
On 28 Nov., 18:47, "Maic Schmidt" <(E-Mail Removed)> wrote:
> Hi,
>
> 2 mod 0.2 should be 0, right?

Yes.

> But it isnt:
>
> #include <math.h>
> #include <stdio.h>
>
> int main(void)
> {
> * double x=2;
> * double y=0.2;
> * printf("%g mod %g = %g\n",x,y,fmod(x,y));
> * return 0;}
>
> => 2 mod 0.2 = 0.2
>
> Why not?


Because what you wrote as 0.2 isn't 0.2, but some number close to that
value. Just as 1/3 cant be represented precisely as a fraction,
neither can 0.2 (and 0.1 for that matter).

The result is accurate, but your perception about the representation
of floating point numbers is insufficient.

/Peter
 
Reply With Quote
 
Maic Schmidt
Guest
Posts: n/a
 
      11-28-2008
Yes, you are right,
y was 0.20000000000000001

Think a have to look carefully for rounding errors with double...

I fear of checking them for 0, because my next task is a solver for
linear equations with gauss.

Thanks

Maic


"Kai-Uwe Bux" <(E-Mail Removed)> schrieb im Newsbeitrag
news:493032ac$0$17069$(E-Mail Removed)...
> Maic Schmidt wrote:
>
> > Hi,
> >
> > 2 mod 0.2 should be 0, right?
> > But it isnt:
> >
> > #include <math.h>
> > #include <stdio.h>
> >
> > int main(void)
> > {
> > double x=2;
> > double y=0.2;
> > printf("%g mod %g = %g\n",x,y,fmod(x,y));
> > return 0;
> > }
> > => 2 mod 0.2 = 0.2
> >
> > Why not?

>
> Maybe, because y is not exactly 0.2. As I see, you are printing results

with
> six significant digits. This may not be enough to see the differences.
>
>
> Best
>
> Kai-Uwe Bux



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments