Re: floating point problems
- From: Christian Bau <christian.bau@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 03 Feb 2006 00:43:30 +0000
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.
.
- Follow-Ups:
- Re: floating point problems
- From: G Patel
- Re: floating point problems
- References:
- floating point problems
- From: G Patel
- Re: floating point problems
- From: P.J. Plauger
- Re: floating point problems
- From: Rod Pemberton
- Re: floating point problems
- From: G Patel
- Re: floating point problems
- From: Christian Bau
- Re: floating point problems
- From: G Patel
- floating point problems
- Prev by Date: Re: Parallel port command
- Next by Date: Re: Parallel port command
- Previous by thread: Re: floating point problems
- Next by thread: Re: floating point problems
- Index(es):
Relevant Pages
|