problem with fmod()

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

    problem with fmod()

    Hi,

    I have a problem with the function fmod.

    Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?

    tia bummerland
  • Erwin Moller

    #2
    Re: problem with fmod()

    bummerland@gmai l.com wrote:
    Hi,
    >
    I have a problem with the function fmod.
    >
    Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
    >
    tia bummerland
    Hi,

    Read the usercontributed notes on www.php.net for that function.
    Returns the floating point remainder (modulo) of the division of the arguments


    It seems to be broken.
    Also you'll find a few replacing algoritms.

    Regards,
    Erwin Moller

    Comment

    • bummerland

      #3
      Re: problem with fmod()

      On 29 Nov., 13:18, Erwin Moller
      <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
      bummerl...@gmai l.com wrote:
      Hi,
      >
      I have a problem with the function fmod.
      >
      Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
      >
      tia bummerland
      >
      Hi,
      >
      Read the usercontributed notes onwww.php.netfo r that function.http://nl3.php.net/manual/en/function.fmod.php
      >
      It seems to be broken.
      Also you'll find a few replacing algoritms.
      >
      Regards,
      Erwin Moller
      Thanks.

      Comment

      • Tim Roberts

        #4
        Re: problem with fmod()

        bummerland@gmai l.com wrote:
        >
        >I have a problem with the function fmod.
        >
        >Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
        Because floating point arithmetic is not exact. The number "5.7" cannot be
        represented exactly in binary. So, it's quite likely that you are really
        computing something like fmod(5.69999999 999999, 0.1), and that result would
        be 0.0999999999999 9, which will print as 0.1.

        It's not the function that is broken, I am afraid. It is your
        expectations.
        --
        Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Erwin Moller

          #5
          Re: problem with fmod()

          Tim Roberts wrote:
          bummerland@gmai l.com wrote:
          >I have a problem with the function fmod.
          >>
          >Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
          >
          Because floating point arithmetic is not exact. The number "5.7" cannot be
          represented exactly in binary. So, it's quite likely that you are really
          computing something like fmod(5.69999999 999999, 0.1), and that result would
          be 0.0999999999999 9, which will print as 0.1.
          >
          It's not the function that is broken, I am afraid. It is your
          expectations.
          Hi Tim,

          The same thought crossed my mind.
          But what is the use of fmod if it uses unexcact representations internally?
          I mean, if you offer such a function, I would expect it does what it
          claims to do (eg in their examples).
          And thus NOT use 'normal' floating points representation, but something
          that respects 6.0 as 6.0 and not 5.9999999999999 .
          (I must admit I don't know HOW, but I am sure some smart guy can figure
          something out.)

          Wouldn't it be better to dump fmod al together?
          I mean, what to expect from the function?
          What is your opinion?

          Regards,
          Erwin Moller

          Comment

          • Jerry Stuckle

            #6
            Re: problem with fmod()

            Erwin Moller wrote:
            Tim Roberts wrote:
            >bummerland@gmai l.com wrote:
            >>I have a problem with the function fmod.
            >>>
            >>Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
            >>
            >Because floating point arithmetic is not exact. The number "5.7"
            >cannot be
            >represented exactly in binary. So, it's quite likely that you are really
            >computing something like fmod(5.69999999 999999, 0.1), and that result
            >would
            >be 0.0999999999999 9, which will print as 0.1.
            >>
            >It's not the function that is broken, I am afraid. It is your
            >expectations .
            >
            Hi Tim,
            >
            The same thought crossed my mind.
            But what is the use of fmod if it uses unexcact representations internally?
            I mean, if you offer such a function, I would expect it does what it
            claims to do (eg in their examples).
            And thus NOT use 'normal' floating points representation, but something
            that respects 6.0 as 6.0 and not 5.9999999999999 .
            (I must admit I don't know HOW, but I am sure some smart guy can figure
            something out.)
            >
            Wouldn't it be better to dump fmod al together?
            I mean, what to expect from the function?
            What is your opinion?
            >
            Regards,
            Erwin Moller
            >
            Erwin,

            It's a limitation of all floating point calculations. And how can you
            correct 5.99999999999 to 6.0 and not be sure the former was correct?

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Erwin Moller

              #7
              Re: problem with fmod()

              Jerry Stuckle wrote:
              Erwin Moller wrote:
              >Tim Roberts wrote:
              >>bummerland@gmai l.com wrote:
              >>>I have a problem with the function fmod.
              >>>>
              >>>Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
              >>>
              >>Because floating point arithmetic is not exact. The number "5.7"
              >>cannot be
              >>represented exactly in binary. So, it's quite likely that you are
              >>really
              >>computing something like fmod(5.69999999 999999, 0.1), and that result
              >>would
              >>be 0.0999999999999 9, which will print as 0.1.
              >>>
              >>It's not the function that is broken, I am afraid. It is your
              >>expectation s.
              >>
              >Hi Tim,
              >>
              >The same thought crossed my mind.
              >But what is the use of fmod if it uses unexcact representations
              >internally?
              >I mean, if you offer such a function, I would expect it does what it
              >claims to do (eg in their examples).
              >And thus NOT use 'normal' floating points representation, but
              >something that respects 6.0 as 6.0 and not 5.9999999999999 .
              >(I must admit I don't know HOW, but I am sure some smart guy can
              >figure something out.)
              >>
              >Wouldn't it be better to dump fmod al together?
              >I mean, what to expect from the function?
              >What is your opinion?
              >>
              >Regards,
              >Erwin Moller
              >>
              >
              Erwin,
              >
              It's a limitation of all floating point calculations. And how can you
              correct 5.99999999999 to 6.0 and not be sure the former was correct?
              Hi Jerry,

              I am aware of the the problems concerning internal representation of
              floating point numbers.

              If I say to YOU 6.0 you know I don't say 5.999999999, right?
              But if you are a computer you represent 6.0 to something that could
              resemble 5.99999999 more than 6.0.

              I just wonder why PHP offers fmod() while we all know it won't work in
              many/most cases as expected because of internal floating point storage
              (and thus changing values).

              So, as far as I can see:
              1) PHP shouldn't offer fmod() as it is now.
              or
              2) PHP should offer a fmod() that uses some other math behind the scenes.
              Maybe with old fixed-point representation?
              Not sure.
              (Erwin is not a mathematician)
              There must be libs that address this issue.

              My main problem with fmod() is that it seems quite useless in its
              current form.

              Regards,
              Erwin Moller

              Comment

              • Jerry Stuckle

                #8
                Re: problem with fmod()

                Erwin Moller wrote:
                Jerry Stuckle wrote:
                >Erwin Moller wrote:
                >>Tim Roberts wrote:
                >>>bummerland@gmai l.com wrote:
                >>>>I have a problem with the function fmod.
                >>>>>
                >>>>Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
                >>>>
                >>>Because floating point arithmetic is not exact. The number "5.7"
                >>>cannot be
                >>>represente d exactly in binary. So, it's quite likely that you are
                >>>really
                >>>computing something like fmod(5.69999999 999999, 0.1), and that
                >>>result would
                >>>be 0.0999999999999 9, which will print as 0.1.
                >>>>
                >>>It's not the function that is broken, I am afraid. It is your
                >>>expectations .
                >>>
                >>Hi Tim,
                >>>
                >>The same thought crossed my mind.
                >>But what is the use of fmod if it uses unexcact representations
                >>internally?
                >>I mean, if you offer such a function, I would expect it does what it
                >>claims to do (eg in their examples).
                >>And thus NOT use 'normal' floating points representation, but
                >>something that respects 6.0 as 6.0 and not 5.9999999999999 .
                >>(I must admit I don't know HOW, but I am sure some smart guy can
                >>figure something out.)
                >>>
                >>Wouldn't it be better to dump fmod al together?
                >>I mean, what to expect from the function?
                >>What is your opinion?
                >>>
                >>Regards,
                >>Erwin Moller
                >>>
                >>
                >Erwin,
                >>
                >It's a limitation of all floating point calculations. And how can you
                >correct 5.99999999999 to 6.0 and not be sure the former was correct?
                >
                Hi Jerry,
                >
                I am aware of the the problems concerning internal representation of
                floating point numbers.
                >
                If I say to YOU 6.0 you know I don't say 5.999999999, right?
                But if you are a computer you represent 6.0 to something that could
                resemble 5.99999999 more than 6.0.
                >
                Sure. But that's how floating point calculations work. How would the
                computer know the answer wasn't actually 5.99999999? It's outside the
                available precision. And the same problem occurs in C/C++'s fmod()
                implementation.
                I just wonder why PHP offers fmod() while we all know it won't work in
                many/most cases as expected because of internal floating point storage
                (and thus changing values).
                >
                Don't know. I never found much good use for it anyway. Didn't in
                C/C++, either.
                So, as far as I can see:
                1) PHP shouldn't offer fmod() as it is now.
                or
                I wouldn't miss it.
                2) PHP should offer a fmod() that uses some other math behind the scenes.
                Maybe with old fixed-point representation?
                Not sure.
                Very difficult to do. The first problem is both 5.7 and 0.1 cannot be
                expressed in floating point. So to do it with accuracy, they would have
                to get rid of the IEEE implementation of floating point altogether and
                go do packed decimal or similar.

                And that will make all calculations much more difficult, because
                processors will handle the IEEE format transparently, but (other than
                IBM mainframes) won't do packed decimal. So instead of one instruction
                to do floating point add/subtract/multiply/divide, they would have to
                have a whole routine to handle it.
                (Erwin is not a mathematician)
                There must be libs that address this issue.
                >
                My main problem with fmod() is that it seems quite useless in its
                current form.
                >
                Regards,
                Erwin Moller
                >
                Nope, no libs to handle it. It's a restriction of floating point.

                Just like in decimal - 10 / 3 * 3 = 9.999999999, and not 10.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...