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

Discussion in 'C++' started by fcvcnet, Sep 5, 2006.

1. ### fcvcnetGuest

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" <> Ð´ÈëÏûÏ¢ÐÂÎÅ:edin4f\$ug1\$...
> fcvcnet <> 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, Sep 5, 2006

2. ### Guest

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" <> Ð´ÈëÏûÏ¢ÐÂÎÅ:edin4f\$ug1\$...
> > fcvcnet <> 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

, Sep 5, 2006

3. ### Thomas TutoneGuest

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.
>

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

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

Best regards,

Tom

Thomas Tutone, Sep 5, 2006
4. ### LRGuest

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.

http://www.parashift.com/c++-faq-lite/newbie.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

LR, Sep 5, 2006
5. ### Jerry CoffinGuest

In article <44fcf3e2\$0\$6855\$>,
says...

[ ... ]

> http://www.parashift.com/c++-faq-lite/newbie.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.

Jerry Coffin, Sep 5, 2006
6. ### Philip PotterGuest

"fcvcnet" <> wrote in message
news:edipup\$s6p\$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.

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

Philip Potter, Sep 5, 2006
7. ### Stuart GolodetzGuest

> "Steve Pope" <>
> Ð´ÈëÏûÏ¢ÐÂÎÅ:edin4f\$ug1\$...
>> fcvcnet <> 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" <> wrote in message
news:edipup\$s6p\$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.

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

Stuart Golodetz, Sep 5, 2006
8. ### fcvcnetGuest

Thank you very much!

"Philip Potter" <>
??????:edjduq\$...
> "fcvcnet" <> wrote in message
> news:edipup\$s6p\$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.

>
> 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
>

fcvcnet, Sep 6, 2006
9. ### =?iso-8859-1?q?Kirit_S=E6lensminde?=Guest

Jerry Coffin wrote:
> In article <44fcf3e2\$0\$6855\$>,
> says...
> > To start, please see
> > http://www.parashift.com/c++-faq-lite/newbie.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/papers/comparingfloats/comparingfloats.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

=?iso-8859-1?q?Kirit_S=E6lensminde?=, Sep 6, 2006
10. ### Default UserGuest

fcvcnet wrote:

> Thank you very much!

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

Default User, Sep 6, 2006