Re: floating point problems



In article <1138925921.972033.243230@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"G Patel" <gaya.patel@xxxxxxxxx> wrote:

Christian Bau wrote:
In article <1138919097.179821.174410@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"G Patel" <gaya.patel@xxxxxxxxx> 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.

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.

If you are given three points (x0, y0), (x1, y1), and (x2, y2), the
easiest way to determine whether there is a right angle at (x0, y0) is
the following:

Subtract (x0, y0) from (x1, y1) and (x2, y2), so the whole triangle is
moved to bring the first point to (0, 0)

x1 -= x0; y1 -= y0;
x2 -= x0; y2 -= y1;

Then all you do is to check whether

x1*y1 == - x2*y2

If they are the same, you have a right angle. If they are not the same,
you don't have a right angle. That's all.
.



Relevant Pages

  • intelligence
    ... then a triangle, therefore is missing the square. ... D the half squares are turned around 90° against the clockwise direction, ... square and circle and indicate at the same ...
    (misc.news.internet.discuss)
  • Re: floating point problems
    ... It looks like you are calculating a square root, ... Better write a function that returns the square of the distance between ... distances inside my right triangle function. ... therefore the sum on the left ...
    (comp.lang.c)
  • Re: "Euclids Fourteenth Book
    ... square on the hypotenuse. ... right triangle itself. ... Given that areas of plane figures scale as the square of any ... If you add curvature the Pythagorean theorem holds ...
    (talk.origins)
  • Re: 3 quilt patterns
    ... A technique cannot be copyrighted, but the specific instructions, written ... three of the sides to inscribe a triangle in this, ... making what I called a "triangle in a square." ... As for the pattern being ...
    (rec.crafts.textiles.quilting)
  • Re: floating point problems
    ... It looks like you are calculating a square root, ... Better write a function that returns the square of the distance between ... distances inside my right triangle function. ... and then resquare it in the actualy "figuring out" ...
    (comp.lang.c)