floating point problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #16
    Re: floating point problems

    Coos Haak <chforth@hccnet .nl> writes:[color=blue]
    > Op 2 Feb 2006 16:58:41 -0800 schreef G Patel:[color=green]
    >> One of the cases has a triangle with a side that is of length sqrt(41),
    >> which is irrational. So when the other function gets it and resquares
    >> it, accuracy is lost. Yet his testing program wants an exactly
    >> solution (no approximate one allowed for this case).[/color]
    >
    > <OT>
    > This is a rectacngular triangle with sides 4, 5 and sqrt(41).
    > See your Math book!
    > </OT>[/color]

    This is usually referred to as a "right triangle", not a "rectangula r
    triangle". ("rectangula r" refers to a 4-sided polygon all of whose
    vertices are 90 degress.)

    But if a test case provides a triangle one of whose sides is merely *a
    close approximatation to* sqrt(41), it's foolish to expect an exact
    result.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • CBFalconer

      #17
      Re: floating point problems

      G Patel wrote:[color=blue]
      >
      > I'm having trouble with floating point problems.
      >[/color]
      .... snip ...[color=blue]
      >
      > example: deltax = 1 deltay = 1999998
      >
      > What this distance function returns is exactly what would be returned
      > if only one of the dimensions were 1999998.
      >
      > This is crucial because I need the distance formula to work within a
      > certain tolerance so I can check to see if triangles are right or
      > oblique.
      >
      > Does anyone know any tricks that I can use to preserve the "effect" of
      > the small deltax in the distance?[/color]

      Welcome to the world of floating point. They are intrinsically
      inexact. You have to learn to analyze the errors. A good
      reference is Knuths TAOCP.

      Please make sure you read the references in my sig. below BEFORE
      posting again.

      --
      "If you want to post a followup via groups.google.c om, don't use
      the broken "Reply" link at the bottom of the article. Click on
      "show options" at the top of the article, then click on the
      "Reply" at the bottom of the article headers." - Keith Thompson
      More details at: <http://cfaj.freeshell. org/google/>
      Also see <http://www.safalra.com/special/googlegroupsrep ly/>


      Comment

      • Richard Bos

        #18
        Re: floating point problems

        Mark McIntyre <markmcintyre@s pamcop.net> wrote:
        [color=blue]
        > On 2 Feb 2006 15:01:21 -0800, in comp.lang.c , "G Patel"
        > <gaya.patel@gma il.com> wrote:
        >[color=green]
        > >Any idea what this -0.0000 ? How can I compare that to 0?[/color]
        >
        > Welcome to the Real World (tm), where maths is not exact.[/color]

        Actually, that's the problem. In the Real World, maths _is_ exact.
        Gravity works to an infinity of decimals. It's just computers (and
        similar devices) which have problems with that; the rest of the world
        doesn't see the problem.

        Richard

        Comment

        • Antonio Contreras

          #19
          Re: floating point problems

          Keith Thompson wrote:[color=blue]
          > Coos Haak <chforth@hccnet .nl> writes:[color=green]
          > > Op 2 Feb 2006 16:58:41 -0800 schreef G Patel:[color=darkred]
          > >> One of the cases has a triangle with a side that is of length sqrt(41),
          > >> which is irrational. So when the other function gets it and resquares
          > >> it, accuracy is lost. Yet his testing program wants an exactly
          > >> solution (no approximate one allowed for this case).[/color]
          > >
          > > <OT>
          > > This is a rectacngular triangle with sides 4, 5 and sqrt(41).
          > > See your Math book!
          > > </OT>[/color]
          >
          > This is usually referred to as a "right triangle", not a "rectangula r
          > triangle". ("rectangula r" refers to a 4-sided polygon all of whose
          > vertices are 90 degress.)[/color]

          [...]

          In spanish a right triangle is called "triángulo rectángulo" which
          translated literally to english would give "rectangula r triangle". I
          for one find that error understandable.

          Comment

          • Jordan Abel

            #20
            Re: floating point problems

            On 2006-02-03, Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:[color=blue]
            > Mark McIntyre <markmcintyre@s pamcop.net> wrote:
            >[color=green]
            >> On 2 Feb 2006 15:01:21 -0800, in comp.lang.c , "G Patel"
            >> <gaya.patel@gma il.com> wrote:
            >>[color=darkred]
            >> >Any idea what this -0.0000 ? How can I compare that to 0?[/color]
            >>
            >> Welcome to the Real World (tm), where maths is not exact.[/color]
            >
            > Actually, that's the problem. In the Real World, maths _is_ exact.[/color]

            Except for quantum mechanics, the heisenberg uncertainty principle, etc
            [color=blue]
            > Gravity works to an infinity of decimals.[/color]

            Just keep telling yourself that.
            [color=blue]
            > It's just computers (and similar devices) which have problems with
            > that; the rest of the world doesn't see the problem.
            >
            > Richard[/color]

            Comment

            • CBFalconer

              #21
              Re: floating point problems

              Richard Bos wrote:[color=blue]
              > Mark McIntyre <markmcintyre@s pamcop.net> wrote:[color=green]
              >> "G Patel" <gaya.patel@gma il.com> wrote:
              >>[color=darkred]
              >>> Any idea what this -0.0000 ? How can I compare that to 0?[/color]
              >>
              >> Welcome to the Real World (tm), where maths is not exact.[/color]
              >
              > Actually, that's the problem. In the Real World, maths _is_
              > exact. Gravity works to an infinity of decimals. It's just
              > computers (and similar devices) which have problems with
              > that; the rest of the world doesn't see the problem.[/color]

              Actually such men as Cantor and Dedekind resolved those inexact
              maths problems well over a century ago, with rigid definitions of
              limits and other inexact concepts. "When, for any epsilon, we can
              find an n such that ...".

              --
              "The power of the Executive to cast a man into prison without
              formulating any charge known to the law, and particularly to
              deny him the judgement of his peers, is in the highest degree
              odious and is the foundation of all totalitarian government
              whether Nazi or Communist." -- W. Churchill, Nov 21, 1943


              Comment

              • Coos Haak

                #22
                Re: floating point problems

                Op Fri, 03 Feb 2006 01:10:39 GMT schreef Keith Thompson:
                [color=blue]
                > Coos Haak <chforth@hccnet .nl> writes:[color=green]
                >> Op 2 Feb 2006 16:58:41 -0800 schreef G Patel:[color=darkred]
                >>> One of the cases has a triangle with a side that is of length sqrt(41),
                >>> which is irrational. So when the other function gets it and resquares
                >>> it, accuracy is lost. Yet his testing program wants an exactly
                >>> solution (no approximate one allowed for this case).[/color]
                >>
                >> <OT>
                >> This is a rectacngular triangle with sides 4, 5 and sqrt(41).
                >> See your Math book!
                >> </OT>[/color]
                >
                > This is usually referred to as a "right triangle", not a "rectangula r
                > triangle". ("rectangula r" refers to a 4-sided polygon all of whose
                > vertices are 90 degress.)[/color]

                In Dutch it's 'rechthoekige driehoek' and a 'rechte driehoek' does not
                exist. So it's just the other way round here ;-)
                [color=blue]
                > But if a test case provides a triangle one of whose sides is merely *a
                > close approximatation to* sqrt(41), it's foolish to expect an exact
                > result.[/color]
                Agreed.

                Coos
                --
                CHForth, 16 bit DOS applications

                Comment

                • CBFalconer

                  #23
                  Re: floating point problems

                  Coos Haak wrote:[color=blue]
                  > Op Fri, 03 Feb 2006 01:10:39 GMT schreef Keith Thompson:
                  >[/color]
                  .... snip ...[color=blue]
                  >[color=green]
                  >> But if a test case provides a triangle one of whose sides is
                  >> merely *a close approximatation to* sqrt(41), it's foolish to
                  >> expect an exact result.[/color]
                  >
                  > Agreed.[/color]

                  But if the expression "sqrt(41)" is never evaluated it is still
                  possible to make exact comparisons with other quantities, such as
                  "sqrt(4*4 + 5*5)". As others have pointed out in this very
                  thread. You just have to think a little, instead of applying the
                  magic crank. This involves realizing that floating point values are
                  inherently approximations.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson
                  More details at: <http://cfaj.freeshell. org/google/>
                  Also see <http://www.safalra.com/special/googlegroupsrep ly/>


                  Comment

                  • Mark McIntyre

                    #24
                    Re: floating point problems

                    On Fri, 03 Feb 2006 01:10:39 GMT, in comp.lang.c , Keith Thompson
                    <kst-u@mib.org> wrote:
                    [color=blue]
                    >This is usually referred to as a "right triangle",[/color]

                    In UK English, a right-angle triangle.
                    Mark McIntyre
                    --
                    "Debugging is twice as hard as writing the code in the first place.
                    Therefore, if you write the code as cleverly as possible, you are,
                    by definition, not smart enough to debug it."
                    --Brian Kernighan

                    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                    Comment

                    • Mark McIntyre

                      #25
                      Re: floating point problems

                      On Fri, 03 Feb 2006 09:53:48 -0500, in comp.lang.c , CBFalconer
                      <cbfalconer@yah oo.com> wrote:
                      [color=blue]
                      >Actually such men as Cantor and Dedekind resolved those inexact
                      >maths problems well over a century ago, with rigid definitions of
                      >limits and other inexact concepts. "When, for any epsilon, we can
                      >find an n such that ...".[/color]

                      You mean they used rigidly defined areas of doubt and uncertainty.

                      Wow, deep thought indeed.

                      Mark McIntyre
                      --
                      "Debugging is twice as hard as writing the code in the first place.
                      Therefore, if you write the code as cleverly as possible, you are,
                      by definition, not smart enough to debug it."
                      --Brian Kernighan

                      ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                      Comment

                      • Mark McIntyre

                        #26
                        Re: floating point problems

                        On Fri, 03 Feb 2006 07:41:26 GMT, in comp.lang.c ,
                        rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                        [color=blue]
                        >Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                        >[color=green]
                        >> On 2 Feb 2006 15:01:21 -0800, in comp.lang.c , "G Patel"
                        >> <gaya.patel@gma il.com> wrote:
                        >>[color=darkred]
                        >> >Any idea what this -0.0000 ? How can I compare that to 0?[/color]
                        >>
                        >> Welcome to the Real World (tm), where maths is not exact.[/color]
                        >
                        >Actually, that's the problem. In the Real World, maths _is_ exact.[/color]

                        Well, kinda. In the actual Real World, you have to use tools to do
                        maths and those tools are not exact. In the intellectual Real World,
                        maths is done in brains, and is therefore exact.

                        Pick a real world...


                        Mark McIntyre
                        --
                        "Debugging is twice as hard as writing the code in the first place.
                        Therefore, if you write the code as cleverly as possible, you are,
                        by definition, not smart enough to debug it."
                        --Brian Kernighan

                        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                        Comment

                        • Christian Bau

                          #27
                          Re: floating point problems

                          In article <1138928321.471 862.14980@o13g2 000cwo.googlegr oups.com>,
                          "G Patel" <gaya.patel@gma il.com> wrote:
                          [color=blue]
                          > Christian Bau wrote:[color=green]
                          > > In article <1138925921.972 033.243230@g44g 2000cwa.googleg roups.com>,
                          > > "G Patel" <gaya.patel@gma il.com> wrote:
                          > >[color=darkred]
                          > > > Christian Bau wrote:
                          > > > > In article <1138919097.179 821.174410@g49g 2000cwa.googleg roups.com>,
                          > > > > "G Patel" <gaya.patel@gma il.com> wrote:
                          > > > >
                          > > > > > Now I still have a bug happening near the end of my program. When I
                          > > > > > do
                          > > > > > the test for right triangle, I take the difference between (hyp^2)
                          > > > > > and
                          > > > > > the sum of the other two sides squared. Sometimes this results in
                          > > > > > the
                          > > > > > floating point value -0.00000 (i checked by printing it out). When I
                          > > > > > compare the result with 0.0 it fails.
                          > > > >
                          > > > > It looks like you are calculating a square root, and then you take the
                          > > > > result and square it immediately. That is a bit pointless, isn't it?
                          > > > > Just takes code and execution time and adds a bit of rounding error.
                          > > > > Better write a function that returns the square of the distance between
                          > > > > two points.
                          > > >
                          > > > My hands are tied because I have to use the distance function's
                          > > > distances inside my right triangle function.[/color]
                          > >
                          > > If it is a homework problem, tell your instructor that his method is
                          > > stupid. If it is not a homework problem, your hands are not tied.
                          > >[/color]
                          >
                          > It is a school assignment and I have the program working except that
                          > our programs are testing automatically by some program.
                          >
                          > He lets us know 8 of the cases, the rest are hidden. 2 of the cases
                          > seem impossible to implement in computers (and yes, we have to sqrt in
                          > a distance function, and then resquare it in the actualy "figuring out"
                          > function).
                          >
                          > One of the cases has a triangle with a side that is of length sqrt(41),
                          > which is irrational. So when the other function gets it and resquares
                          > it, accuracy is lost. Yet his testing program wants an exactly
                          > solution (no approximate one allowed for this case).
                          >
                          > The other case has a triangle with 2 really large sides and one very
                          > small sides. He wants us to get it within some tolerance, but i can't
                          > seem to "keep" the information from the small side to "stick" within
                          > the sqrt function.
                          >
                          > I am going to email him and tell him that his cases are ridiculous (in
                          > a nice way).[/color]

                          No, the test cases are not a problem. Putting a completely unnecessary
                          complication into the problem _is_ a problem.

                          There is a way around this. Your program started with integer values.
                          Each side of the triangle is the square root of an integer. If you
                          square it, the result is again an integer, except for rounding errors,
                          which means it is only close to an integer. Now you have to check whether

                          a*a + b*b - c*c == 0.0

                          or not. The squares should be integers; therefore the sum on the left
                          side should be an integer. It won't be an integer because of rounding
                          errors. Lets say you got a result of 0.01: You know you don't have the
                          exact result, because the exact result must be an integer. So you can
                          assume that the correct result is the nearest integer. To check whether
                          the correct result is zero, check whether the absolute value is less
                          than 1/2.

                          Comment

                          • Christian Bau

                            #28
                            Re: floating point problems

                            In article <43E3C8D7.942CA 825@yahoo.com>,
                            CBFalconer <cbfalconer@yah oo.com> wrote:
                            [color=blue]
                            > Coos Haak wrote:[color=green]
                            > > Op Fri, 03 Feb 2006 01:10:39 GMT schreef Keith Thompson:
                            > >[/color]
                            > ... snip ...[color=green]
                            > >[color=darkred]
                            > >> But if a test case provides a triangle one of whose sides is
                            > >> merely *a close approximatation to* sqrt(41), it's foolish to
                            > >> expect an exact result.[/color]
                            > >
                            > > Agreed.[/color]
                            >
                            > But if the expression "sqrt(41)" is never evaluated it is still
                            > possible to make exact comparisons with other quantities, such as
                            > "sqrt(4*4 + 5*5)". As others have pointed out in this very
                            > thread. You just have to think a little, instead of applying the
                            > magic crank. This involves realizing that floating point values are
                            > inherently approximations.[/color]

                            In the case at hand, it is guaranteed that square roots of integer
                            values are taken. The final result is a floating-point number which is
                            guaranteed to be an integer, except for rounding errors. You can find
                            the exact value if you can guarantee the rounding error is less than one
                            half.

                            Comment

                            Working...