how can modf(), fmod() return 1.0 for the fractional part, why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bsabiston@gmail.com

    how can modf(), fmod() return 1.0 for the fractional part, why?

    Hi, I'm trying to get the fractional part of a floating point
    number. I've tried fmod() and modf(), and while both do work, they
    also occasionally return 1.0 for the fractional part of the number
    instead of 0.0. This is on a mac using xcode. This should not ever
    happen should it?

    Thanks
    Bob


  • Richard Tobin

    #2
    Re: how can modf(), fmod() return 1.0 for the fractional part, why?

    In article <e956e321-7437-4682-a2a3-f0163bfa17df@q9 g2000hsb.google groups.com>,
    <bsabiston@gmai l.comwrote:
    >Hi, I'm trying to get the fractional part of a floating point
    >number. I've tried fmod() and modf(), and while both do work, they
    >also occasionally return 1.0 for the fractional part of the number
    >instead of 0.0. This is on a mac using xcode. This should not ever
    >happen should it?
    How do you know that they're returning 1.0? Are you printing it out?
    Bear in mind that a value like 0.9999999999999 may get printed as 1.0
    if you don't use enough digits.

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.

    Comment

    • bsabiston@gmail.com

      #3
      Re: how can modf(), fmod() return 1.0 for the fractional part, why?

      On Sep 29, 4:09 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
      In article <e956e321-7437-4682-a2a3-f0163bfa1...@q9 g2000hsb.google groups.com>,
      >
       <bsabis...@gmai l.comwrote:
      Hi,  I'm trying to get the fractional part of a floating point
      number.  I've tried fmod() and modf(), and while both do work, they
      also occasionally return 1.0 for the fractional part of the number
      instead of 0.0.  This is on a mac using xcode.  This should not ever
      happen should it?
      >
      How do you know that they're returning 1.0?  Are you printing it out?
      Bear in mind that a value like 0.9999999999999 may get printed as 1.0
      if you don't use enough digits.
      >
      -- Richard
      --
      Please remember to mention me / in tapes you leave behind.
      hmm. well maybe that is happening. I'm using printf("%f
      \n",fmod(val) );


      Comment

      • jameskuyper@verizon.net

        #4
        Re: how can modf(), fmod() return 1.0 for the fractional part, why?

        bsabis...@gmail .com wrote:
        Hi, I'm trying to get the fractional part of a floating point
        number. I've tried fmod() and modf(), and while both do work, they
        also occasionally return 1.0 for the fractional part of the number
        instead of 0.0. This is on a mac using xcode. This should not ever
        happen should it?

        My best guess is that you're getting a number that is only slightly
        smaller than 1.0, and when you print it out, it gets rounded to 1.0.

        Have you checked the return value for equality with 1.0? If you are
        just printing it out, are you printing it with enough significant
        digits to distinguish nextafter(1.0, 0.0) from 1.0? What do you get if
        you use the following printf() statement?

        #include <float.h>
        #include <stdio.h>

        printf("%.*g\n" , DBL_DIG+1, fmod(x, 1.0))

        Comment

        • Default User

          #5
          Re: how can modf(), fmod() return 1.0 for the fractional part, why?

          bsabiston@gmail .com wrote:
          Hi, I'm trying to get the fractional part of a floating point
          number. I've tried fmod() and modf(), and while both do work, they
          also occasionally return 1.0 for the fractional part of the number
          instead of 0.0. This is on a mac using xcode. This should not ever
          happen should it?
          Show us your code.




          Brian

          Comment

          • Flash Gordon

            #6
            Re: how can modf(), fmod() return 1.0 for the fractional part, why?

            bsabiston@gmail .com wrote, On 29/09/08 22:17:
            On Sep 29, 4:09 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
            >In article <e956e321-7437-4682-a2a3-f0163bfa1...@q9 g2000hsb.google groups.com>,
            >>
            > <bsabis...@gmai l.comwrote:
            >>Hi, I'm trying to get the fractional part of a floating point
            >>number. I've tried fmod() and modf(), and while both do work, they
            >>also occasionally return 1.0 for the fractional part of the number
            >>instead of 0.0. This is on a mac using xcode. This should not ever
            >>happen should it?
            >How do you know that they're returning 1.0? Are you printing it out?
            >Bear in mind that a value like 0.9999999999999 may get printed as 1.0
            >if you don't use enough digits.
            >
            hmm. well maybe that is happening. I'm using printf("%f
            \n",fmod(val) );
            That does not disagree with what Richard suggested. In fact, since the
            default precision for %f is only 6 digits I believe you are guaranteed
            to need more digits to be certain the number is 1.0 or greater.
            --
            Flash Gordon
            If spamming me sent it to smap@spam.cause way.com
            If emailing me use my reply-to address
            See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

            Comment

            • Eric Sosman

              #7
              Re: how can modf(), fmod() return 1.0 for the fractional part, why?

              bsabiston@gmail .com wrote:
              Hi, I'm trying to get the fractional part of a floating point
              number. I've tried fmod() and modf(), and while both do work, they
              also occasionally return 1.0 for the fractional part of the number
              instead of 0.0. This is on a mac using xcode. This should not ever
              happen should it?
              No, it should not. What evidence can you offer that it did?

              Before answering, please ponder this: If the value 0.9999998723
              is printed to six decimal digits' precision (the default for "%f"),
              what output should you expect? If this little thought experiment
              doesn't cause light to dawn, provide more detail. Actual code, for
              starters.

              --
              Eric.Sosman@sun .com

              Comment

              • Martin Ambuhl

                #8
                Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                bsabiston@gmail .com wrote:
                Hi, I'm trying to get the fractional part of a floating point
                number. I've tried fmod() and modf(), and while both do work, they
                also occasionally return 1.0 for the fractional part of the number
                instead of 0.0. This is on a mac using xcode. This should not ever
                happen should it?
                Run the following, modifying EXTRADIGITS as required, and watch the
                output. Consider how that might pertain to your question.

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

                #define EXTRADIGITS 2

                int main()
                {
                double intpart, fraction, decrement = 1, value;
                int i;
                printf("Demonst ration of modf and default precision\n");
                for (i = 0; i <= DBL_DIG + EXTRADIGITS; i++, decrement /= 10.) {
                value = 1 - decrement;
                printf("decreme nt = %g (default), %.*g;\n"
                " 1-decrement = %g (default), %.*g\n",
                decrement, DBL_DIG, decrement, value, DBL_DIG, value);
                fraction = modf(value, &intpart);
                printf("modf returns:\n"
                " fraction = %g (default), %.*g,\n"
                " intpart = %g (default), %.*g.\n\n",
                fraction, DBL_DIG, fraction, intpart, DBL_DIG, intpart);
                }
                return 0;
                }



                Demonstration of modf and default precision
                decrement = 1 (default), 1;
                1-decrement = 0 (default), 0
                modf returns:
                fraction = 0 (default), 0,
                intpart = 0 (default), 0.

                decrement = 0.1 (default), 0.1;
                1-decrement = 0.9 (default), 0.9
                modf returns:
                fraction = 0.9 (default), 0.9,
                intpart = 0 (default), 0.

                decrement = 0.01 (default), 0.01;
                1-decrement = 0.99 (default), 0.99
                modf returns:
                fraction = 0.99 (default), 0.99,
                intpart = 0 (default), 0.

                decrement = 0.001 (default), 0.001;
                1-decrement = 0.999 (default), 0.999
                modf returns:
                fraction = 0.999 (default), 0.999,
                intpart = 0 (default), 0.

                decrement = 0.0001 (default), 0.0001;
                1-decrement = 0.9999 (default), 0.9999
                modf returns:
                fraction = 0.9999 (default), 0.9999,
                intpart = 0 (default), 0.

                decrement = 1e-05 (default), 1e-05;
                1-decrement = 0.99999 (default), 0.99999
                modf returns:
                fraction = 0.99999 (default), 0.99999,
                intpart = 0 (default), 0.

                decrement = 1e-06 (default), 1e-06;
                1-decrement = 0.999999 (default), 0.999999
                modf returns:
                fraction = 0.999999 (default), 0.999999,
                intpart = 0 (default), 0.

                decrement = 1e-07 (default), 1e-07;
                1-decrement = 1 (default), 0.9999999
                modf returns:
                fraction = 1 (default), 0.9999999,
                intpart = 0 (default), 0.

                decrement = 1e-08 (default), 1e-08;
                1-decrement = 1 (default), 0.99999999
                modf returns:
                fraction = 1 (default), 0.99999999,
                intpart = 0 (default), 0.

                decrement = 1e-09 (default), 1e-09;
                1-decrement = 1 (default), 0.999999999
                modf returns:
                fraction = 1 (default), 0.999999999,
                intpart = 0 (default), 0.

                decrement = 1e-10 (default), 1e-10;
                1-decrement = 1 (default), 0.9999999999
                modf returns:
                fraction = 1 (default), 0.9999999999,
                intpart = 0 (default), 0.

                decrement = 1e-11 (default), 1e-11;
                1-decrement = 1 (default), 0.99999999999
                modf returns:
                fraction = 1 (default), 0.99999999999,
                intpart = 0 (default), 0.

                decrement = 1e-12 (default), 1e-12;
                1-decrement = 1 (default), 0.999999999999
                modf returns:
                fraction = 1 (default), 0.999999999999,
                intpart = 0 (default), 0.

                decrement = 1e-13 (default), 1e-13;
                1-decrement = 1 (default), 0.9999999999999
                modf returns:
                fraction = 1 (default), 0.9999999999999 ,
                intpart = 0 (default), 0.

                decrement = 1e-14 (default), 1e-14;
                1-decrement = 1 (default), 0.9999999999999 9
                modf returns:
                fraction = 1 (default), 0.9999999999999 9,
                intpart = 0 (default), 0.

                decrement = 1e-15 (default), 1e-15;
                1-decrement = 1 (default), 0.9999999999999 99
                modf returns:
                fraction = 1 (default), 0.9999999999999 99,
                intpart = 0 (default), 0.

                decrement = 1e-16 (default), 1e-16;
                1-decrement = 1 (default), 1
                modf returns:
                fraction = 1 (default), 1,
                intpart = 0 (default), 0.

                decrement = 1e-17 (default), 1e-17;
                1-decrement = 1 (default), 1
                modf returns:
                fraction = 0 (default), 0,
                intpart = 1 (default), 1.

                Comment

                • Keith Thompson

                  #9
                  Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                  bsabiston@gmail .com writes:
                  Hi, I'm trying to get the fractional part of a floating point
                  number. I've tried fmod() and modf(), and while both do work, they
                  also occasionally return 1.0 for the fractional part of the number
                  instead of 0.0. This is on a mac using xcode. This should not ever
                  happen should it?
                  Recommended reading: Section 14 of the comp.lang.c FAQ,
                  <http://www.c-faq.com/>.

                  Additional recommended reading: the rest of it.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • blargg

                    #10
                    Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                    In article <qqu7r5xto4.ln2 @news.flash-gordon.me.uk>,
                    spam@flash-gordon.me.uk wrote:
                    bsabiston@gmail .com wrote, On 29/09/08 22:17:
                    On Sep 29, 4:09 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                    In article
                    <e956e321-7437-4682-a2a3-f0163bfa1...@q9 g2000hsb.google groups.com>,
                    >
                    <bsabis...@gmai l.comwrote:
                    >Hi, I'm trying to get the fractional part of a floating point
                    >number. I've tried fmod() and modf(), and while both do work, they
                    >also occasionally return 1.0 for the fractional part of the number
                    >instead of 0.0. This is on a mac using xcode. This should not ever
                    >happen should it?
                    How do you know that they're returning 1.0? Are you printing it out?
                    Bear in mind that a value like 0.9999999999999 may get printed as 1.0
                    if you don't use enough digits.
                    hmm. well maybe that is happening. I'm using printf("%f
                    \n",fmod(val) );
                    >
                    That does not disagree with what Richard suggested. In fact, since the
                    default precision for %f is only 6 digits I believe you are guaranteed
                    to need more digits to be certain the number is 1.0 or greater.
                    And the best way to find out whether it's 1.0 or greater is to write code:

                    if ( n >= 1.0 )
                    printf( "1.0 or greater\n" );

                    I never trust debuggers and printf to tell the whole truth when finding
                    what values variables hold.

                    Comment

                    • user923005

                      #11
                      Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                      On Sep 29, 4:32 pm, blargg....@gish puppy.com (blargg) wrote:
                      In article <qqu7r5xto4.... @news.flash-gordon.me.uk>,
                      >
                      s...@flash-gordon.me.uk wrote:
                      bsabis...@gmail .com wrote, On 29/09/08 22:17:
                      On Sep 29, 4:09 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                      >In article
                      >
                      <e956e321-7437-4682-a2a3-f0163bfa1...@q9 g2000hsb.google groups.com>,
                      >
                      >
                      >
                      >
                      >
                      >
                      >
                      > <bsabis...@gmai l.comwrote:
                      >>Hi,  I'm trying to get the fractional part of a floating point
                      >>number.  I've tried fmod() and modf(), and while both do work, they
                      >>also occasionally return 1.0 for the fractional part of the number
                      >>instead of 0.0.  This is on a mac using xcode.  This should notever
                      >>happen should it?
                      >How do you know that they're returning 1.0?  Are you printing it out?
                      >Bear in mind that a value like 0.9999999999999 may get printed as 1.0
                      >if you don't use enough digits.
                      >
                      hmm.  well maybe that is happening.  I'm using printf("%f
                      \n",fmod(val) );
                      >
                      That does not disagree with what Richard suggested. In fact, since the
                      default precision for %f is only 6 digits I believe you are guaranteed
                      to need more digits to be certain the number is 1.0 or greater.
                      >
                      And the best way to find out whether it's 1.0 or greater is to write code:
                      >
                      if ( n >= 1.0 )
                          printf( "1.0 or greater\n" );
                      >
                      I never trust debuggers and printf to tell the whole truth when finding
                      what values variables hold.
                      In line with C-FAQ 14.5, I would say that sometimes that is the right
                      thing to do and sometimes it is the wrong thing to do.
                      It would be especially worrisome if you were looking for mathematical
                      differences rather than numerical.
                      Things like:
                      y = cos(0.0);
                      y = pow(2.0,28.0);
                      y = sqrt(34567*3456 7);
                      should not be expected to return an integer, even though
                      mathematically they should.
                      So if we are wondering "Is cos(0.0) less than 1?" a comparison
                      operator on the expression is not a reliable way to tell.

                      Comment

                      • James Kuyper

                        #12
                        Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                        user923005 wrote:
                        On Sep 29, 4:32 pm, blargg....@gish puppy.com (blargg) wrote:
                        >In article <qqu7r5xto4.... @news.flash-gordon.me.uk>,
                        >>
                        >s...@flash-gordon.me.uk wrote:
                        >>bsabis...@gma il.com wrote, On 29/09/08 22:17:
                        ....
                        >>>hmm. well maybe that is happening. I'm using printf("%f
                        >>>\n",fmod(val ));
                        >>That does not disagree with what Richard suggested. In fact, since the
                        >>default precision for %f is only 6 digits I believe you are guaranteed
                        >>to need more digits to be certain the number is 1.0 or greater.
                        >And the best way to find out whether it's 1.0 or greater is to write code:
                        >>
                        >if ( n >= 1.0 )
                        > printf( "1.0 or greater\n" );
                        >>
                        >I never trust debuggers and printf to tell the whole truth when finding
                        >what values variables hold.
                        >
                        In line with C-FAQ 14.5, I would say that sometimes that is the right
                        thing to do and sometimes it is the wrong thing to do.
                        It would be especially worrisome if you were looking for mathematical
                        differences rather than numerical.
                        Things like:
                        y = cos(0.0);
                        y = pow(2.0,28.0);
                        y = sqrt(34567*3456 7);
                        should not be expected to return an integer, even though
                        mathematically they should.
                        So if we are wondering "Is cos(0.0) less than 1?" a comparison
                        operator on the expression is not a reliable way to tell.
                        There's a key difference here. For real x, cos(x)<=1.0. However, for
                        real x and y, with y!=0.0, the standard's requirements, as specified in
                        7.12.10.1p3, mean that

                        fabs(fmod(x, y)) < y

                        The difference between <= and < is precisely the difference between
                        conforming to the standard's specification for fmod() in 7.12.10.1p3,
                        and not conforming.

                        Comment

                        • Dik T. Winter

                          #13
                          Re: how can modf(), fmod() return 1.0 for the fractional part, why?

                          In article <caf1ed1d-02f0-408e-9cf2-fe0f41de24a7@59 g2000hsb.google groups.comuser9 23005 <dcorbit@connx. comwrites:
                          ....
                          In line with C-FAQ 14.5, I would say that sometimes that is the right
                          thing to do and sometimes it is the wrong thing to do.
                          It would be especially worrisome if you were looking for mathematical
                          differences rather than numerical.
                          Things like:
                          y = cos(0.0);
                          y = pow(2.0,28.0);
                          y = sqrt(34567*3456 7);
                          should not be expected to return an integer, even though
                          mathematically they should.
                          So if we are wondering "Is cos(0.0) less than 1?" a comparison
                          operator on the expression is not a reliable way to tell.
                          If the question is intended as "is cos(0.0) mathematically less than 1?" you
                          are right, but in that case a computer program is not able to provide a
                          reliable answer at all. On the other hand, if the question is "is the value
                          returned by cos(0.0) less than 1?" a comparison operator is the thing to use.

                          And even the condition: "fabs(cos(x )) <= 1.0" makes sense. I have seen
                          programs crash because they assumed it would be true, the implementation by
                          Cody and Waite is one that can fail here. In Ada the situation is better
                          because of the stringent requirement for special functions (as I wrote it
                          in the draft for that: "when a function receives model numbers and the
                          exact mathematical result is a model number, that number should be returned").
                          --
                          dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                          home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                          Comment

                          Working...