Velocity Reviews > C++ > Re: faq how to code an expression of fraction in if-clause?

# Re: faq how to code an expression of fraction in if-clause?

fcvcnet
Guest
Posts: n/a

 09-05-2006
Thanks.
I want to judge if two plane are parallel.

The two plane equatin are

P: a1x + b1y + c1z +d1 = 0
Q: a2x + b2y + c2z +d2 = 0

if the two plane is parlle, there shoudle be
a1 b1 c1
-- = -- = --
a2 b2 c2

but if I code like this , it is absolutely wrong.

if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for programe
{
...
}

So I wanna ask how to code for this problem.

Thanks you very much.

"Steve Pope" <(E-Mail Removed)> 写入消息新闻:edin4f\$ug1\$(E-Mail Removed)...
> fcvcnet <(E-Mail Removed)> wrote:
>
>>Hi,
>> How to code an expression of fraction in if-clause?
>>For example;
>>a1 b1 c1
>>-- = -- = --
>>a2 b2 c2
>>
>>Thanks a lot!

>
> You're going to have to try asking your question in more
> detail. It is not clear what you are asking.
>
> Steve

starffly@gmail.com
Guest
Posts: n/a

 09-05-2006
whether the result of a1/a2 equals b1/b2 or not depends on the type of
them and the precision of calculation. i think it can work well on this
premise.
fcvcnet wrote:
> Thanks.
> I want to judge if two plane are parallel.
>
> The two plane equatin are
>
> P: a1x + b1y + c1z +d1 = 0
> Q: a2x + b2y + c2z +d2 = 0
>
> if the two plane is parlle, there shoudle be
> a1 b1 c1
> -- = -- = --
> a2 b2 c2
>
> but if I code like this , it is absolutely wrong.
>
> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for programe
> {
> ...
> }
>
> So I wanna ask how to code for this problem.
>
> Thanks you very much.
>
> "Steve Pope" <(E-Mail Removed)> 写入消息新闻:edin4f\$ug1\$(E-Mail Removed)...
> > fcvcnet <(E-Mail Removed)> wrote:
> >
> >>Hi,
> >> How to code an expression of fraction in if-clause?
> >>For example;
> >>a1 b1 c1
> >>-- = -- = --
> >>a2 b2 c2
> >>
> >>Thanks a lot!

> >
> > You're going to have to try asking your question in more
> > detail. It is not clear what you are asking.
> >
> > Steve

Thomas Tutone
Guest
Posts: n/a

 09-05-2006
fcvcnet wrote:

> I want to judge if two plane are parallel.
>
> The two plane equatin are
>
> P: a1x + b1y + c1z +d1 = 0
> Q: a2x + b2y + c2z +d2 = 0
>
> if the two plane is parlle, there shoudle be
> a1 b1 c1
> -- = -- = --
> a2 b2 c2
>
> but if I code like this , it is absolutely wrong.
>
> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for programe
> {
> ...
> }
>
> So I wanna ask how to code for this problem.
>

Your question is answered in the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-29.17

Please read the rest of the FAQ too - I suspect you will find it
useful.

Best regards,

Tom

LR
Guest
Posts: n/a

 09-05-2006
fcvcnet wrote:

> Thanks.
> I want to judge if two plane are parallel.
>
> The two plane equatin are
>
> P: a1x + b1y + c1z +d1 = 0
> Q: a2x + b2y + c2z +d2 = 0
>
> if the two plane is parlle, there shoudle be
> a1 b1 c1
> -- = -- = --
> a2 b2 c2
>
> but if I code like this , it is absolutely wrong.
>
> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for programe
> {
> ...
> }
>
> So I wanna ask how to code for this problem.

To start, please see
http://www.parashift.com/c++-faq-lit...html#faq-29.17

I suspect that you may have to do something like

const double af = a1/a2; // check for a2,b2,c2 == 0.0
const double bf = b1/b2;
const double cf = c1/c2;

// does YCFE(a,b) == YCFE(b,a)?
const bool theyAreEqual =
YourCompareFunctionEqual(af,bf) &&
YourCompareFunctionEqual(bf,cf) &&
YourCompareFunctionEqual(af,cf);

if(theyAreEqual) {
// whatever you wanted to do here....
}

LR

Jerry Coffin
Guest
Posts: n/a

 09-05-2006
In article <44fcf3e2\$0\$6855\$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

> To start, please see
> http://www.parashift.com/c++-faq-lit...html#faq-29.17

IMO, a better place to look would be:

http://tinyurl.com/qcmyd

The IsEqual given in the FAQ is basically broken -- it treats tolerance
within which numbers are considered equal as an absolute value. In
reality, it's a relative value -- roughly speaking, a percentage of the
magnitudes of the numbers involved.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Philip Potter
Guest
Posts: n/a

 09-05-2006
"fcvcnet" <(E-Mail Removed)> wrote in message
news:edipup\$s6p\$(E-Mail Removed)99.com...
> Thanks.
> I want to judge if two plane are parallel.
>
> The two plane equatin are
>
> P: a1x + b1y + c1z +d1 = 0
> Q: a2x + b2y + c2z +d2 = 0
>
> if the two plane is parlle, there shoudle be
> a1 b1 c1
> -- = -- = --
> a2 b2 c2
>
> but if I code like this , it is absolutely wrong.
>
> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for

programe
> {
> ...
> }
>
> So I wanna ask how to code for this problem.

Please don't top-post. Your reply belongs below or interspersed with the
message you are replying to.

Are a1/a2/b1/b2/c1/c2 integer types or floating-point types? If
floating-point, others have answered the question for you.

If integer, then a1/a2 won't give the result you want. Integer division
throws away any remainder. However, you can use mathematical identities to
get round this problem:

a1/a2 == b1/b2 => a1*b2 == b1*a2

But if your numbers hold large values, you may need to worry about overflow.
If a1*b2 or b1*a2 will ever equal more than 32767, you should use "long"

Philip

Stuart Golodetz
Guest
Posts: n/a

 09-05-2006
> "Steve Pope" <(E-Mail Removed)>
> 写入消息新闻:edin4f\$ug1\$(E-Mail Removed)...
>> fcvcnet <(E-Mail Removed)> wrote:
>>
>>>Hi,
>>> How to code an expression of fraction in if-clause?
>>>For example;
>>>a1 b1 c1
>>>-- = -- = --
>>>a2 b2 c2
>>>
>>>Thanks a lot!

>>
>> You're going to have to try asking your question in more
>> detail. It is not clear what you are asking.
>>
>> Steve

"fcvcnet" <(E-Mail Removed)> wrote in message
news:edipup\$s6p\$(E-Mail Removed)99.com...
> Thanks.
> I want to judge if two plane are parallel.
>
> The two plane equatin are
>
> P: a1x + b1y + c1z +d1 = 0
> Q: a2x + b2y + c2z +d2 = 0
>
> if the two plane is parlle, there shoudle be
> a1 b1 c1
> -- = -- = --
> a2 b2 c2
>
> but if I code like this , it is absolutely wrong.
>
> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for
> programe
> {
> ...
> }
>
> So I wanna ask how to code for this problem.
>
> Thanks you very much.

Please don't top-post (rearranged). In answer to your question, whilst your
problem as stated will be solved by looking at the FAQ reference someone
else gave you, I think you might be better off (from a stability point of
view) with a different algorithm. Specifically, I think you would be better
off determining whether the angle between the two normal vectors is less
than some small number epsilon. The angle can be calculated using the dot
product formula:

u . v = u.x*v.x + u.y*v.y + u.z*v.z = |u| * |v| * cos angle

HTH,
Stu

fcvcnet
Guest
Posts: n/a

 09-06-2006
Thank you very much!

"Philip Potter" <(E-Mail Removed)>
??????:edjduq\$(E-Mail Removed)...
> "fcvcnet" <(E-Mail Removed)> wrote in message
> news:edipup\$s6p\$(E-Mail Removed)99.com...
>> Thanks.
>> I want to judge if two plane are parallel.
>>
>> The two plane equatin are
>>
>> P: a1x + b1y + c1z +d1 = 0
>> Q: a2x + b2y + c2z +d2 = 0
>>
>> if the two plane is parlle, there shoudle be
>> a1 b1 c1
>> -- = -- = --
>> a2 b2 c2
>>
>> but if I code like this , it is absolutely wrong.
>>
>> if ((a1/a2)==(b1/b2) && (b1/b2)==(c1/c2)) <-- This is not right for

> programe
>> {
>> ...
>> }
>>
>> So I wanna ask how to code for this problem.

>
> Please don't top-post. Your reply belongs below or interspersed with the
> message you are replying to.
>
> Are a1/a2/b1/b2/c1/c2 integer types or floating-point types? If
> floating-point, others have answered the question for you.
>
> If integer, then a1/a2 won't give the result you want. Integer division
> throws away any remainder. However, you can use mathematical identities to
> get round this problem:
>
> a1/a2 == b1/b2 => a1*b2 == b1*a2
>
> But if your numbers hold large values, you may need to worry about
> overflow.
> If a1*b2 or b1*a2 will ever equal more than 32767, you should use "long"
> instead of "int".
>
> Philip
>

=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a

 09-06-2006

Jerry Coffin wrote:
> In article <44fcf3e2\$0\$6855\$(E-Mail Removed)>,
> (E-Mail Removed) says...
> > To start, please see
> > http://www.parashift.com/c++-faq-lit...html#faq-29.17

>
> IMO, a better place to look would be:
>
> http://tinyurl.com/qcmyd
>
> The IsEqual given in the FAQ is basically broken -- it treats tolerance
> within which numbers are considered equal as an absolute value. In
> reality, it's a relative value -- roughly speaking, a percentage of the
> magnitudes of the numbers involved.

http://www.cygnus-software.com/paper...ringfloats.htm

Not sure how useful it is in practice - depends on a number of factors,
but maybe the approach can be adapted for the platform you are
targeting.

K

Default User
Guest
Posts: n/a

 09-06-2006
fcvcnet wrote:

> Thank you very much!

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian