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

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

  1. fcvcnet

    fcvcnet Guest

    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
    #1
    1. Advertising

  2. fcvcnet

    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
    #2
    1. Advertising

  3. 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-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
    #3
  4. fcvcnet

    LR Guest

    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-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
    #4
  5. fcvcnet

    Jerry Coffin Guest

    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.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 5, 2006
    #5
  6. "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.


    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
     
    Philip Potter, Sep 5, 2006
    #6
  7. > "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.


    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
     
    Stuart Golodetz, Sep 5, 2006
    #7
  8. fcvcnet

    fcvcnet Guest

    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.

    >
    > 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
    >
     
    fcvcnet, Sep 6, 2006
    #8
  9. 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.


    There is also this page:

    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
    #9
  10. fcvcnet

    Default User Guest

    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
     
    Default User, Sep 6, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jam
    Replies:
    2
    Views:
    740
  2. Peter

    String Fraction to Number

    Peter, May 6, 2004, in forum: Java
    Replies:
    6
    Views:
    1,060
    Mason Bryant
    May 6, 2004
  3. Replies:
    4
    Views:
    394
    Guillaume
    Nov 20, 2005
  4. Peter Otten
    Replies:
    16
    Views:
    750
    Raymond Hettinger
    Apr 5, 2005
  5. fcvcnet
    Replies:
    2
    Views:
    307
    fcvcnet
    Sep 5, 2006
Loading...

Share This Page