[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Simple fmod-Question

Maic Schmidt

11/28/2008 5:47:00 PM

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)


5 Answers

Kai-Uwe Bux

11/28/2008 6:04:00 PM

0

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

peter koch

11/28/2008 6:21:00 PM

0

On 28 Nov., 18:47, "Maic Schmidt" <pri...@maicschmidt.de> 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

Maic Schmidt

11/28/2008 6:24:00 PM

0

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" <jkherciueh@gmx.net> schrieb im Newsbeitrag
news:493032ac$0$17069$6e1ede2f@read.cnntp.org...
> 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


W.T.S.

4/27/2011 11:45:00 AM

0

In article <4l4ubg.hti.19.1@news.alt.net>, jvisions@live.com says...
>
> "satyr" <RsEaMtOyVrE@infidels.org> wrote in message
> news:5h8fr61sgu4hjeclcic41f38vj3aj2f1h7@4ax.com...
> > On Wed, 27 Apr 2011 00:47:24 -0400, "J" <jvisions@live.com> wrote:
> >
> >>A 'think tank' wasn't necessary to come to this conclusion. They could
> >>have
> >>easily asked any U.S. serviceman who was stationed in England during WWII.
> >>It's been a known fact here in 'the states' that a few beers could land
> >>any
> >>British girl in the sack. What the UK could use is some morality and teach
> >>it's women to start acting like ladies.
> >
> > Whereas the men will jump in the sack totally sober but prefer a few
> > beers anyway. So how do you get a good catholic girl into bed?
>
> British or American? An American catholic girl you'd have to marry first; a
> British girl will set you back the price of a 'Guinness'.
"J" doesn't know much about American (or even Irish Catholic girls)
Catholic girls. American Catholic girls spend the most time out of
wedlock pregnant than any other girl. Catholic girls get the most
abortions, and that's a _fact_. There's a brisk business of
transporting Irish Catholic girls to London for abortions, how special!
There's nothing so likely to get a girl to open up than being Catholic.
--
http://www.rhrealitycheck.org/p...

satyr

4/28/2011 3:33:00 AM

0

On Wed, 27 Apr 2011 01:15:57 -0400, "J" <jvisions@live.com> wrote:

>
>"satyr" <RsEaMtOyVrE@infidels.org> wrote in message
>news:5h8fr61sgu4hjeclcic41f38vj3aj2f1h7@4ax.com...
>> On Wed, 27 Apr 2011 00:47:24 -0400, "J" <jvisions@live.com> wrote:
>>
>>>
>>>A 'think tank' wasn't necessary to come to this conclusion. They could
>>>have
>>>easily asked any U.S. serviceman who was stationed in England during WWII.
>>>It's been a known fact here in 'the states' that a few beers could land
>>>any
>>>British girl in the sack. What the UK could use is some morality and teach
>>>it's women to start acting like ladies.
>>
>> Whereas the men will jump in the sack totally sober but prefer a few
>> beers anyway. So how do you get a good catholic girl into bed?
>
>
>
>
>
>
>British or American? An American catholic girl you'd have to marry first; a
>British girl will set you back the price of a 'Guinness'.

BaHaha The definition of an American catholic virgin is an ugly
12-year-old.

--
satyr #1953
Chairman, EAC Church Taxation Subcommittee
Director, Gideon Bible Alternative Fuel Project
Supervisor, EAC Fossil Casting Lab