Simple fmod-Question

Discussion in 'C++' started by Maic Schmidt, Nov 28, 2008.

  1. Maic Schmidt

    Maic Schmidt Guest

    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)
    Maic Schmidt, Nov 28, 2008
    #1
    1. Advertising

  2. Maic Schmidt

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Nov 28, 2008
    #2
    1. Advertising

  3. Maic Schmidt

    peter koch Guest

    On 28 Nov., 18:47, "Maic Schmidt" <> 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
    peter koch, Nov 28, 2008
    #3
  4. Maic Schmidt

    Maic Schmidt Guest

    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" <> schrieb im Newsbeitrag
    news:493032ac$0$17069$...
    > 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
    Maic Schmidt, Nov 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shewy du 80 !!

    fmod et Pause du MP3

    Shewy du 80 !!, Jun 14, 2004, in forum: C++
    Replies:
    1
    Views:
    647
    Pete C.
    Jun 14, 2004
  2. seia0106

    atan2() and fmod()

    seia0106, Jul 28, 2004, in forum: C++
    Replies:
    8
    Views:
    719
    John Harrison
    Jul 29, 2004
  3. stau

    fmod?

    stau, Dec 28, 2003, in forum: C Programming
    Replies:
    6
    Views:
    627
  4. Zunbeltz Izaola

    fmod and %

    Zunbeltz Izaola, Apr 21, 2004, in forum: Python
    Replies:
    2
    Views:
    664
    Terry Reedy
    Apr 21, 2004
  5. Lonnie Princehouse
    Replies:
    2
    Views:
    360
    Lonnie Princehouse
    Nov 5, 2005
Loading...

Share This Page